Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 10 additions & 35 deletions src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<String> Uint32ToName(Local<Context> context, uint32_t index) {
return Uint32::New(Isolate::GetCurrent(), index)->ToString(context);
}

} // anonymous namespace

ContextifyContext* ContextifyContext::New(Environment* env,
Local<Object> sandbox_obj,
ContextOptions* options) {
Expand Down Expand Up @@ -849,11 +839,8 @@ Intercepted ContextifyContext::IndexedPropertyQueryCallback(
return Intercepted::kNo;
}

Local<String> name;
if (Uint32ToName(ctx->context(), index).ToLocal(&name)) {
return ContextifyContext::PropertyQueryCallback(name, args);
}
return Intercepted::kNo;
Local<String> name = Uint32ToString(ctx->context(), index);
return ContextifyContext::PropertyQueryCallback(name, args);
}

// static
Expand All @@ -866,11 +853,8 @@ Intercepted ContextifyContext::IndexedPropertyGetterCallback(
return Intercepted::kNo;
}

Local<String> name;
if (Uint32ToName(ctx->context(), index).ToLocal(&name)) {
return ContextifyContext::PropertyGetterCallback(name, args);
}
return Intercepted::kNo;
Local<String> name = Uint32ToString(ctx->context(), index);
return ContextifyContext::PropertyGetterCallback(name, args);
}

Intercepted ContextifyContext::IndexedPropertySetterCallback(
Expand All @@ -884,11 +868,8 @@ Intercepted ContextifyContext::IndexedPropertySetterCallback(
return Intercepted::kNo;
}

Local<String> name;
if (Uint32ToName(ctx->context(), index).ToLocal(&name)) {
return ContextifyContext::PropertySetterCallback(name, value, args);
}
return Intercepted::kNo;
Local<String> name = Uint32ToString(ctx->context(), index);
return ContextifyContext::PropertySetterCallback(name, value, args);
}

// static
Expand All @@ -901,11 +882,8 @@ Intercepted ContextifyContext::IndexedPropertyDescriptorCallback(
return Intercepted::kNo;
}

Local<String> name;
if (Uint32ToName(ctx->context(), index).ToLocal(&name)) {
return ContextifyContext::PropertyDescriptorCallback(name, args);
}
return Intercepted::kNo;
Local<String> name = Uint32ToString(ctx->context(), index);
return ContextifyContext::PropertyDescriptorCallback(name, args);
}

Intercepted ContextifyContext::IndexedPropertyDefinerCallback(
Expand All @@ -919,11 +897,8 @@ Intercepted ContextifyContext::IndexedPropertyDefinerCallback(
return Intercepted::kNo;
}

Local<String> name;
if (Uint32ToName(ctx->context(), index).ToLocal(&name)) {
return ContextifyContext::PropertyDefinerCallback(name, desc, args);
}
return Intercepted::kNo;
Local<String> name = Uint32ToString(ctx->context(), index);
return ContextifyContext::PropertyDefinerCallback(name, desc, args);
}

// static
Expand Down
45 changes: 5 additions & 40 deletions src/node_webstorage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand Down Expand Up @@ -432,10 +431,6 @@ Maybe<void> Storage::Store(Local<Name> key, Local<Value> value) {
return JustVoid();
}

static MaybeLocal<String> Uint32ToName(Local<Context> context, uint32_t index) {
return Uint32::New(Isolate::GetCurrent(), index)->ToString(context);
}

static void Clear(const FunctionCallbackInfo<Value>& info) {
Storage* storage;
ASSIGN_OR_RETURN_UNWRAP(&storage, info.This());
Expand Down Expand Up @@ -631,67 +626,37 @@ static Intercepted StorageDefiner(Local<Name> property,
static Intercepted IndexedGetter(uint32_t index,
const PropertyCallbackInfo<Value>& info) {
Environment* env = Environment::GetCurrent(info);
Local<Name> 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> name = Uint32ToString(env->context(), index);
return StorageGetter(name, info);
}

static Intercepted IndexedSetter(uint32_t index,
Local<Value> value,
const PropertyCallbackInfo<void>& info) {
Environment* env = Environment::GetCurrent(info);
Local<Name> 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> name = Uint32ToString(env->context(), index);
return StorageSetter(name, value, info);
}

static Intercepted IndexedQuery(uint32_t index,
const PropertyCallbackInfo<Integer>& info) {
Environment* env = Environment::GetCurrent(info);
Local<Name> 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> name = Uint32ToString(env->context(), index);
return StorageQuery(name, info);
}

static Intercepted IndexedDeleter(uint32_t index,
const PropertyCallbackInfo<Boolean>& info) {
Environment* env = Environment::GetCurrent(info);
Local<Name> 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> name = Uint32ToString(env->context(), index);
return StorageDeleter(name, info);
}

static Intercepted IndexedDefiner(uint32_t index,
const PropertyDescriptor& desc,
const PropertyCallbackInfo<void>& info) {
Environment* env = Environment::GetCurrent(info);
Local<Name> 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> name = Uint32ToString(env->context(), index);
return StorageDefiner(name, desc, info);
}

Expand Down
10 changes: 10 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,16 @@ inline v8::MaybeLocal<v8::Object> NewDictionaryInstanceNullProto(
v8::Local<v8::DictionaryTemplate> tmpl,
v8::MemorySpan<v8::MaybeLocal<v8::Value>> property_values);

// Convert an uint32 to a V8 String.
inline v8::Local<v8::String> Uint32ToString(v8::Local<v8::Context> 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
Expand Down
Loading