diff --git a/src/node_contextify.cc b/src/node_contextify.cc index 713b0777b875bd..c6a34f1ae5a19b 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -85,7 +85,6 @@ using v8::ScriptCompiler; using v8::ScriptOrigin; using v8::String; using v8::Symbol; -using v8::Uint32; using v8::UnboundScript; using v8::Value; @@ -109,15 +108,6 @@ using v8::Value; // For every `set` of a global property, the interceptor callback defines or // changes the property both on the sandbox and the global proxy. -namespace { - -// Convert an int to a V8 Name (String or Symbol). -MaybeLocal Uint32ToName(Local context, uint32_t index) { - return Uint32::New(Isolate::GetCurrent(), index)->ToString(context); -} - -} // anonymous namespace - ContextifyContext* ContextifyContext::New(Environment* env, Local sandbox_obj, ContextOptions* options) { @@ -849,11 +839,8 @@ Intercepted ContextifyContext::IndexedPropertyQueryCallback( return Intercepted::kNo; } - Local name; - if (Uint32ToName(ctx->context(), index).ToLocal(&name)) { - return ContextifyContext::PropertyQueryCallback(name, args); - } - return Intercepted::kNo; + Local name = Uint32ToString(ctx->context(), index); + return ContextifyContext::PropertyQueryCallback(name, args); } // static @@ -866,11 +853,8 @@ Intercepted ContextifyContext::IndexedPropertyGetterCallback( return Intercepted::kNo; } - Local name; - if (Uint32ToName(ctx->context(), index).ToLocal(&name)) { - return ContextifyContext::PropertyGetterCallback(name, args); - } - return Intercepted::kNo; + Local name = Uint32ToString(ctx->context(), index); + return ContextifyContext::PropertyGetterCallback(name, args); } Intercepted ContextifyContext::IndexedPropertySetterCallback( @@ -884,11 +868,8 @@ Intercepted ContextifyContext::IndexedPropertySetterCallback( return Intercepted::kNo; } - Local name; - if (Uint32ToName(ctx->context(), index).ToLocal(&name)) { - return ContextifyContext::PropertySetterCallback(name, value, args); - } - return Intercepted::kNo; + Local name = Uint32ToString(ctx->context(), index); + return ContextifyContext::PropertySetterCallback(name, value, args); } // static @@ -901,11 +882,8 @@ Intercepted ContextifyContext::IndexedPropertyDescriptorCallback( return Intercepted::kNo; } - Local name; - if (Uint32ToName(ctx->context(), index).ToLocal(&name)) { - return ContextifyContext::PropertyDescriptorCallback(name, args); - } - return Intercepted::kNo; + Local name = Uint32ToString(ctx->context(), index); + return ContextifyContext::PropertyDescriptorCallback(name, args); } Intercepted ContextifyContext::IndexedPropertyDefinerCallback( @@ -919,11 +897,8 @@ Intercepted ContextifyContext::IndexedPropertyDefinerCallback( return Intercepted::kNo; } - Local name; - if (Uint32ToName(ctx->context(), index).ToLocal(&name)) { - return ContextifyContext::PropertyDefinerCallback(name, desc, args); - } - return Intercepted::kNo; + Local name = Uint32ToString(ctx->context(), index); + return ContextifyContext::PropertyDefinerCallback(name, desc, args); } // static diff --git a/src/node_webstorage.cc b/src/node_webstorage.cc index 5819d9bca845e0..0b301a310fa397 100644 --- a/src/node_webstorage.cc +++ b/src/node_webstorage.cc @@ -41,7 +41,6 @@ using v8::PropertyCallbackInfo; using v8::PropertyDescriptor; using v8::PropertyHandlerFlags; using v8::String; -using v8::Uint32; using v8::Value; #define THROW_SQLITE_ERROR(env, r) \ @@ -432,10 +431,6 @@ Maybe Storage::Store(Local key, Local value) { return JustVoid(); } -static MaybeLocal Uint32ToName(Local context, uint32_t index) { - return Uint32::New(Isolate::GetCurrent(), index)->ToString(context); -} - static void Clear(const FunctionCallbackInfo& info) { Storage* storage; ASSIGN_OR_RETURN_UNWRAP(&storage, info.This()); @@ -631,13 +626,7 @@ static Intercepted StorageDefiner(Local property, static Intercepted IndexedGetter(uint32_t index, const PropertyCallbackInfo& info) { Environment* env = Environment::GetCurrent(info); - Local name; - if (!Uint32ToName(env->context(), index).ToLocal(&name)) { - // There was an error converting the index to a name. - // We aren't going to return a result but let's indicate - // that we intercepted the operation. - return Intercepted::kYes; - } + Local name = Uint32ToString(env->context(), index); return StorageGetter(name, info); } @@ -645,39 +634,21 @@ static Intercepted IndexedSetter(uint32_t index, Local value, const PropertyCallbackInfo& info) { Environment* env = Environment::GetCurrent(info); - Local name; - if (!Uint32ToName(env->context(), index).ToLocal(&name)) { - // There was an error converting the index to a name. - // We aren't going to return a result but let's indicate - // that we intercepted the operation. - return Intercepted::kYes; - } + Local name = Uint32ToString(env->context(), index); return StorageSetter(name, value, info); } static Intercepted IndexedQuery(uint32_t index, const PropertyCallbackInfo& info) { Environment* env = Environment::GetCurrent(info); - Local name; - if (!Uint32ToName(env->context(), index).ToLocal(&name)) { - // There was an error converting the index to a name. - // We aren't going to return a result but let's indicate - // that we intercepted the operation. - return Intercepted::kYes; - } + Local name = Uint32ToString(env->context(), index); return StorageQuery(name, info); } static Intercepted IndexedDeleter(uint32_t index, const PropertyCallbackInfo& info) { Environment* env = Environment::GetCurrent(info); - Local name; - if (!Uint32ToName(env->context(), index).ToLocal(&name)) { - // There was an error converting the index to a name. - // We aren't going to return a result but let's indicate - // that we intercepted the operation. - return Intercepted::kYes; - } + Local name = Uint32ToString(env->context(), index); return StorageDeleter(name, info); } @@ -685,13 +656,7 @@ static Intercepted IndexedDefiner(uint32_t index, const PropertyDescriptor& desc, const PropertyCallbackInfo& info) { Environment* env = Environment::GetCurrent(info); - Local name; - if (!Uint32ToName(env->context(), index).ToLocal(&name)) { - // There was an error converting the index to a name. - // We aren't going to return a result but let's indicate - // that we intercepted the operation. - return Intercepted::kYes; - } + Local name = Uint32ToString(env->context(), index); return StorageDefiner(name, desc, info); } diff --git a/src/util.h b/src/util.h index aa2a47ce0f1ca9..2482f82fe21a9e 100644 --- a/src/util.h +++ b/src/util.h @@ -1063,6 +1063,16 @@ inline v8::MaybeLocal NewDictionaryInstanceNullProto( v8::Local tmpl, v8::MemorySpan> property_values); +// Convert an uint32 to a V8 String. +inline v8::Local Uint32ToString(v8::Local context, + uint32_t index) { + // V8 internally caches strings for small integers, and asserts that a + // non-empty string local handle is returned for `ToString`. + return v8::Uint32::New(v8::Isolate::GetCurrent(), index) + ->ToString(context) + .ToLocalChecked(); +} + } // namespace node #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS