Skip to content

Commit

Permalink
chore: use strings from env
Browse files Browse the repository at this point in the history
instead instanciate`String::NewFromUtf8Literal`

Refs: nodejs#54213 (comment)
  • Loading branch information
tpoisseau committed Sep 4, 2024
1 parent 30fca87 commit 1c6eecc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 25 deletions.
5 changes: 5 additions & 0 deletions src/env_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,10 @@
V(ipv4_string, "IPv4") \
V(ipv6_string, "IPv6") \
V(isclosing_string, "isClosing") \
V(isfinished_string, "isFinished") \
V(issuer_string, "issuer") \
V(issuercert_string, "issuerCertificate") \
V(iterator_string, "Iterator") \
V(jwk_crv_string, "crv") \
V(jwk_d_string, "d") \
V(jwk_dp_string, "dp") \
Expand Down Expand Up @@ -232,6 +234,7 @@
V(nistcurve_string, "nistCurve") \
V(node_string, "node") \
V(nsname_string, "nsname") \
V(num_cols_string, "num_cols") \
V(object_string, "Object") \
V(ocsp_request_string, "OCSPRequest") \
V(oncertcb_string, "oncertcb") \
Expand Down Expand Up @@ -278,6 +281,7 @@
V(priority_string, "priority") \
V(process_string, "process") \
V(promise_string, "promise") \
V(prototype_string, "prototype") \
V(psk_string, "psk") \
V(pubkey_string, "pubkey") \
V(public_exponent_string, "publicExponent") \
Expand Down Expand Up @@ -322,6 +326,7 @@
V(standard_name_string, "standardName") \
V(start_time_string, "startTime") \
V(state_string, "state") \
V(statement_string, "statement") \
V(stats_string, "stats") \
V(status_string, "status") \
V(stdio_string, "stdio") \
Expand Down
36 changes: 11 additions & 25 deletions src/node_sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -516,14 +516,11 @@ void StatementSync::IterateReturnCallback(

auto self = args.This();
// iterator has fetch all result or break, prevent next func to return result
self->Set(context,
String::NewFromUtf8Literal(isolate, "is_finished"),
Boolean::New(isolate, true))
self->Set(context, env->isfinished_string(), Boolean::New(isolate, true))
.ToChecked();

auto external_stmt = Local<External>::Cast(
self->Get(context, String::NewFromUtf8Literal(isolate, "statement"))
.ToLocalChecked());
self->Get(context, env->statement_string()).ToLocalChecked());
auto stmt = static_cast<StatementSync*>(external_stmt->Value());
if (!stmt->IsFinalized()) {
sqlite3_reset(stmt->statement_);
Expand All @@ -549,8 +546,7 @@ void StatementSync::IterateNextCallback(

// skip iteration if is_finished
auto is_finished = Local<Boolean>::Cast(
self->Get(context, String::NewFromUtf8Literal(isolate, "is_finished"))
.ToLocalChecked());
self->Get(context, env->isfinished_string()).ToLocalChecked());
if (is_finished->Value()) {
LocalVector<Name> keys(isolate, {env->done_string(), env->value_string()});
LocalVector<Value> values(isolate,
Expand All @@ -564,13 +560,11 @@ void StatementSync::IterateNextCallback(
}

auto external_stmt = Local<External>::Cast(
self->Get(context, String::NewFromUtf8Literal(isolate, "statement"))
.ToLocalChecked());
self->Get(context, env->statement_string()).ToLocalChecked());
auto stmt = static_cast<StatementSync*>(external_stmt->Value());
auto num_cols =
Local<Integer>::Cast(
self->Get(context, String::NewFromUtf8Literal(isolate, "num_cols"))
.ToLocalChecked())
self->Get(context, env->num_cols_string()).ToLocalChecked())
->Value();

THROW_AND_RETURN_ON_BAD_STATE(
Expand All @@ -583,9 +577,7 @@ void StatementSync::IterateNextCallback(

// cleanup when no more rows to fetch
sqlite3_reset(stmt->statement_);
self->Set(context,
String::NewFromUtf8Literal(isolate, "is_finished"),
Boolean::New(isolate, true))
self->Set(context, env->isfinished_string(), Boolean::New(isolate, true))
.ToChecked();

LocalVector<Name> keys(isolate, {env->done_string(), env->value_string()});
Expand Down Expand Up @@ -653,11 +645,10 @@ void StatementSync::Iterate(const FunctionCallbackInfo<Value>& args) {
Local<Object> global = context->Global();
Local<Value> js_iterator;
Local<Value> js_iterator_prototype;
if (!global->Get(context, String::NewFromUtf8Literal(isolate, "Iterator"))
.ToLocal(&js_iterator))
if (!global->Get(context, env->iterator_string()).ToLocal(&js_iterator))
return;
if (!js_iterator.As<Object>()
->Get(context, String::NewFromUtf8Literal(isolate, "prototype"))
->Get(context, env->prototype_string())
.ToLocal(&js_iterator_prototype))
return;

Expand All @@ -670,27 +661,22 @@ void StatementSync::Iterate(const FunctionCallbackInfo<Value>& args) {
num_cols_pd.set_enumerable(false);
num_cols_pd.set_configurable(false);
iterable_iterator
->DefineProperty(
context, String::NewFromUtf8Literal(isolate, "num_cols"), num_cols_pd)
->DefineProperty(context, env->num_cols_string(), num_cols_pd)
.ToChecked();

auto stmt_pd =
v8::PropertyDescriptor(v8::External::New(isolate, stmt), false);
stmt_pd.set_enumerable(false);
stmt_pd.set_configurable(false);
iterable_iterator
->DefineProperty(
context, String::NewFromUtf8Literal(isolate, "statement"), stmt_pd)
iterable_iterator->DefineProperty(context, env->statement_string(), stmt_pd)
.ToChecked();

auto is_finished_pd =
v8::PropertyDescriptor(v8::Boolean::New(isolate, false), true);
stmt_pd.set_enumerable(false);
stmt_pd.set_configurable(false);
iterable_iterator
->DefineProperty(context,
String::NewFromUtf8Literal(isolate, "is_finished"),
is_finished_pd)
->DefineProperty(context, env->isfinished_string(), is_finished_pd)
.ToChecked();

args.GetReturnValue().Set(iterable_iterator);
Expand Down

0 comments on commit 1c6eecc

Please sign in to comment.