Skip to content

Commit e3d5b7a

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

File tree

3 files changed

+25
-74
lines changed

3 files changed

+25
-74
lines changed

src/node_contextify.cc

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ using v8::ScriptCompiler;
8585
using v8::ScriptOrigin;
8686
using v8::String;
8787
using v8::Symbol;
88-
using v8::Uint32;
8988
using v8::UnboundScript;
9089
using v8::Value;
9190

@@ -109,15 +108,6 @@ using v8::Value;
109108
// For every `set` of a global property, the interceptor callback defines or
110109
// changes the property both on the sandbox and the global proxy.
111110

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-
121111
ContextifyContext* ContextifyContext::New(Environment* env,
122112
Local<Object> sandbox_obj,
123113
ContextOptions* options) {
@@ -849,11 +839,8 @@ Intercepted ContextifyContext::IndexedPropertyQueryCallback(
849839
return Intercepted::kNo;
850840
}
851841

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

859846
// static
@@ -866,11 +853,8 @@ Intercepted ContextifyContext::IndexedPropertyGetterCallback(
866853
return Intercepted::kNo;
867854
}
868855

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

876860
Intercepted ContextifyContext::IndexedPropertySetterCallback(
@@ -884,11 +868,8 @@ Intercepted ContextifyContext::IndexedPropertySetterCallback(
884868
return Intercepted::kNo;
885869
}
886870

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

894875
// static
@@ -901,11 +882,8 @@ Intercepted ContextifyContext::IndexedPropertyDescriptorCallback(
901882
return Intercepted::kNo;
902883
}
903884

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

911889
Intercepted ContextifyContext::IndexedPropertyDefinerCallback(
@@ -919,11 +897,8 @@ Intercepted ContextifyContext::IndexedPropertyDefinerCallback(
919897
return Intercepted::kNo;
920898
}
921899

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

929904
// 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)