Skip to content

Commit d43a0cf

Browse files
committed
src: simply uint32 to string as it must not fail
1 parent 482b191 commit d43a0cf

File tree

3 files changed

+25
-73
lines changed

3 files changed

+25
-73
lines changed

src/node_contextify.cc

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,6 @@ using v8::Value;
109109
// For every `set` of a global property, the interceptor callback defines or
110110
// changes the property both on the sandbox and the global proxy.
111111

112-
namespace {
113-
114-
// Convert an int to a V8 Name (String or Symbol).
115-
MaybeLocal<String> Uint32ToName(Local<Context> context, uint32_t index) {
116-
return Uint32::New(Isolate::GetCurrent(), index)->ToString(context);
117-
}
118-
119-
} // anonymous namespace
120-
121112
ContextifyContext* ContextifyContext::New(Environment* env,
122113
Local<Object> sandbox_obj,
123114
ContextOptions* options) {
@@ -849,11 +840,8 @@ Intercepted ContextifyContext::IndexedPropertyQueryCallback(
849840
return Intercepted::kNo;
850841
}
851842

852-
Local<String> name;
853-
if (Uint32ToName(ctx->context(), index).ToLocal(&name)) {
854-
return ContextifyContext::PropertyQueryCallback(name, args);
855-
}
856-
return Intercepted::kNo;
843+
Local<String> name = Uint32ToString(ctx->context(), index);
844+
return ContextifyContext::PropertyQueryCallback(name, args);
857845
}
858846

859847
// static
@@ -866,11 +854,8 @@ Intercepted ContextifyContext::IndexedPropertyGetterCallback(
866854
return Intercepted::kNo;
867855
}
868856

869-
Local<String> name;
870-
if (Uint32ToName(ctx->context(), index).ToLocal(&name)) {
871-
return ContextifyContext::PropertyGetterCallback(name, args);
872-
}
873-
return Intercepted::kNo;
857+
Local<String> name = Uint32ToString(ctx->context(), index);
858+
return ContextifyContext::PropertyGetterCallback(name, args);
874859
}
875860

876861
Intercepted ContextifyContext::IndexedPropertySetterCallback(
@@ -884,11 +869,8 @@ Intercepted ContextifyContext::IndexedPropertySetterCallback(
884869
return Intercepted::kNo;
885870
}
886871

887-
Local<String> name;
888-
if (Uint32ToName(ctx->context(), index).ToLocal(&name)) {
889-
return ContextifyContext::PropertySetterCallback(name, value, args);
890-
}
891-
return Intercepted::kNo;
872+
Local<String> name = Uint32ToString(ctx->context(), index);
873+
return ContextifyContext::PropertySetterCallback(name, value, args);
892874
}
893875

894876
// static
@@ -901,11 +883,8 @@ Intercepted ContextifyContext::IndexedPropertyDescriptorCallback(
901883
return Intercepted::kNo;
902884
}
903885

904-
Local<String> name;
905-
if (Uint32ToName(ctx->context(), index).ToLocal(&name)) {
906-
return ContextifyContext::PropertyDescriptorCallback(name, args);
907-
}
908-
return Intercepted::kNo;
886+
Local<String> name = Uint32ToString(ctx->context(), index);
887+
return ContextifyContext::PropertyDescriptorCallback(name, args);
909888
}
910889

911890
Intercepted ContextifyContext::IndexedPropertyDefinerCallback(
@@ -919,11 +898,8 @@ Intercepted ContextifyContext::IndexedPropertyDefinerCallback(
919898
return Intercepted::kNo;
920899
}
921900

922-
Local<String> name;
923-
if (Uint32ToName(ctx->context(), index).ToLocal(&name)) {
924-
return ContextifyContext::PropertyDefinerCallback(name, desc, args);
925-
}
926-
return Intercepted::kNo;
901+
Local<String> name = Uint32ToString(ctx->context(), index);
902+
return ContextifyContext::PropertyDefinerCallback(name, desc, args);
927903
}
928904

929905
// static

src/node_webstorage.cc

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -432,10 +432,6 @@ Maybe<void> Storage::Store(Local<Name> key, Local<Value> value) {
432432
return JustVoid();
433433
}
434434

435-
static MaybeLocal<String> Uint32ToName(Local<Context> context, uint32_t index) {
436-
return Uint32::New(Isolate::GetCurrent(), index)->ToString(context);
437-
}
438-
439435
static void Clear(const FunctionCallbackInfo<Value>& info) {
440436
Storage* storage;
441437
ASSIGN_OR_RETURN_UNWRAP(&storage, info.This());
@@ -631,67 +627,37 @@ static Intercepted StorageDefiner(Local<Name> property,
631627
static Intercepted IndexedGetter(uint32_t index,
632628
const PropertyCallbackInfo<Value>& info) {
633629
Environment* env = Environment::GetCurrent(info);
634-
Local<Name> name;
635-
if (!Uint32ToName(env->context(), index).ToLocal(&name)) {
636-
// There was an error converting the index to a name.
637-
// We aren't going to return a result but let's indicate
638-
// that we intercepted the operation.
639-
return Intercepted::kYes;
640-
}
630+
Local<Name> name = Uint32ToString(env->context(), index);
641631
return StorageGetter(name, info);
642632
}
643633

644634
static Intercepted IndexedSetter(uint32_t index,
645635
Local<Value> value,
646636
const PropertyCallbackInfo<void>& info) {
647637
Environment* env = Environment::GetCurrent(info);
648-
Local<Name> name;
649-
if (!Uint32ToName(env->context(), index).ToLocal(&name)) {
650-
// There was an error converting the index to a name.
651-
// We aren't going to return a result but let's indicate
652-
// that we intercepted the operation.
653-
return Intercepted::kYes;
654-
}
638+
Local<Name> name = Uint32ToString(env->context(), index);
655639
return StorageSetter(name, value, info);
656640
}
657641

658642
static Intercepted IndexedQuery(uint32_t index,
659643
const PropertyCallbackInfo<Integer>& info) {
660644
Environment* env = Environment::GetCurrent(info);
661-
Local<Name> name;
662-
if (!Uint32ToName(env->context(), index).ToLocal(&name)) {
663-
// There was an error converting the index to a name.
664-
// We aren't going to return a result but let's indicate
665-
// that we intercepted the operation.
666-
return Intercepted::kYes;
667-
}
645+
Local<Name> name = Uint32ToString(env->context(), index);
668646
return StorageQuery(name, info);
669647
}
670648

671649
static Intercepted IndexedDeleter(uint32_t index,
672650
const PropertyCallbackInfo<Boolean>& info) {
673651
Environment* env = Environment::GetCurrent(info);
674-
Local<Name> name;
675-
if (!Uint32ToName(env->context(), index).ToLocal(&name)) {
676-
// There was an error converting the index to a name.
677-
// We aren't going to return a result but let's indicate
678-
// that we intercepted the operation.
679-
return Intercepted::kYes;
680-
}
652+
Local<Name> name = Uint32ToString(env->context(), index);
681653
return StorageDeleter(name, info);
682654
}
683655

684656
static Intercepted IndexedDefiner(uint32_t index,
685657
const PropertyDescriptor& desc,
686658
const PropertyCallbackInfo<void>& info) {
687659
Environment* env = Environment::GetCurrent(info);
688-
Local<Name> name;
689-
if (!Uint32ToName(env->context(), index).ToLocal(&name)) {
690-
// There was an error converting the index to a name.
691-
// We aren't going to return a result but let's indicate
692-
// that we intercepted the operation.
693-
return Intercepted::kYes;
694-
}
660+
Local<Name> name = Uint32ToString(env->context(), index);
695661
return StorageDefiner(name, desc, info);
696662
}
697663

src/util.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,16 @@ inline v8::MaybeLocal<v8::Object> NewDictionaryInstanceNullProto(
10631063
v8::Local<v8::DictionaryTemplate> tmpl,
10641064
v8::MemorySpan<v8::MaybeLocal<v8::Value>> property_values);
10651065

1066+
// Convert an uint32 to a V8 String.
1067+
v8::Local<v8::String> Uint32ToString(v8::Local<v8::Context> context,
1068+
uint32_t index) {
1069+
// V8 internally caches strings for small integers, and asserts that a
1070+
// non-empty string local handle is returned for `ToString`.
1071+
return v8::Uint32::New(v8::Isolate::GetCurrent(), index)
1072+
->ToString(context)
1073+
.ToLocalChecked();
1074+
}
1075+
10661076
} // namespace node
10671077

10681078
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

0 commit comments

Comments
 (0)