Skip to content

Commit e243d1d

Browse files
legendecastargos
authored andcommitted
src: simply uint32 to string as it must not fail
PR-URL: #60846 Refs: #53959 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 0415f7f commit e243d1d

File tree

3 files changed

+25
-75
lines changed

3 files changed

+25
-75
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) {
@@ -845,11 +835,8 @@ Intercepted ContextifyContext::IndexedPropertyQueryCallback(
845835
return Intercepted::kNo;
846836
}
847837

848-
Local<String> name;
849-
if (Uint32ToName(ctx->context(), index).ToLocal(&name)) {
850-
return ContextifyContext::PropertyQueryCallback(name, args);
851-
}
852-
return Intercepted::kNo;
838+
Local<String> name = Uint32ToString(ctx->context(), index);
839+
return ContextifyContext::PropertyQueryCallback(name, args);
853840
}
854841

855842
// static
@@ -862,11 +849,8 @@ Intercepted ContextifyContext::IndexedPropertyGetterCallback(
862849
return Intercepted::kNo;
863850
}
864851

865-
Local<String> name;
866-
if (Uint32ToName(ctx->context(), index).ToLocal(&name)) {
867-
return ContextifyContext::PropertyGetterCallback(name, args);
868-
}
869-
return Intercepted::kNo;
852+
Local<String> name = Uint32ToString(ctx->context(), index);
853+
return ContextifyContext::PropertyGetterCallback(name, args);
870854
}
871855

872856
Intercepted ContextifyContext::IndexedPropertySetterCallback(
@@ -880,11 +864,8 @@ Intercepted ContextifyContext::IndexedPropertySetterCallback(
880864
return Intercepted::kNo;
881865
}
882866

883-
Local<String> name;
884-
if (Uint32ToName(ctx->context(), index).ToLocal(&name)) {
885-
return ContextifyContext::PropertySetterCallback(name, value, args);
886-
}
887-
return Intercepted::kNo;
867+
Local<String> name = Uint32ToString(ctx->context(), index);
868+
return ContextifyContext::PropertySetterCallback(name, value, args);
888869
}
889870

890871
// static
@@ -897,11 +878,8 @@ Intercepted ContextifyContext::IndexedPropertyDescriptorCallback(
897878
return Intercepted::kNo;
898879
}
899880

900-
Local<String> name;
901-
if (Uint32ToName(ctx->context(), index).ToLocal(&name)) {
902-
return ContextifyContext::PropertyDescriptorCallback(name, args);
903-
}
904-
return Intercepted::kNo;
881+
Local<String> name = Uint32ToString(ctx->context(), index);
882+
return ContextifyContext::PropertyDescriptorCallback(name, args);
905883
}
906884

907885
Intercepted ContextifyContext::IndexedPropertyDefinerCallback(
@@ -915,11 +893,8 @@ Intercepted ContextifyContext::IndexedPropertyDefinerCallback(
915893
return Intercepted::kNo;
916894
}
917895

918-
Local<String> name;
919-
if (Uint32ToName(ctx->context(), index).ToLocal(&name)) {
920-
return ContextifyContext::PropertyDefinerCallback(name, desc, args);
921-
}
922-
return Intercepted::kNo;
896+
Local<String> name = Uint32ToString(ctx->context(), index);
897+
return ContextifyContext::PropertyDefinerCallback(name, desc, args);
923898
}
924899

925900
// static

src/node_webstorage.cc

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ using v8::PropertyCallbackInfo;
4141
using v8::PropertyDescriptor;
4242
using v8::PropertyHandlerFlags;
4343
using v8::String;
44-
using v8::Uint32;
4544
using v8::Value;
4645

4746
#define THROW_SQLITE_ERROR(env, r) \
@@ -432,10 +431,6 @@ Maybe<void> Storage::Store(Local<Name> key, Local<Value> value) {
432431
return JustVoid();
433432
}
434433

435-
static MaybeLocal<String> Uint32ToName(Local<Context> context, uint32_t index) {
436-
return Uint32::New(Isolate::GetCurrent(), index)->ToString(context);
437-
}
438-
439434
static void Clear(const FunctionCallbackInfo<Value>& info) {
440435
Storage* storage;
441436
ASSIGN_OR_RETURN_UNWRAP(&storage, info.This());
@@ -631,67 +626,37 @@ static Intercepted StorageDefiner(Local<Name> property,
631626
static Intercepted IndexedGetter(uint32_t index,
632627
const PropertyCallbackInfo<Value>& info) {
633628
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-
}
629+
Local<Name> name = Uint32ToString(env->context(), index);
641630
return StorageGetter(name, info);
642631
}
643632

644633
static Intercepted IndexedSetter(uint32_t index,
645634
Local<Value> value,
646635
const PropertyCallbackInfo<void>& info) {
647636
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-
}
637+
Local<Name> name = Uint32ToString(env->context(), index);
655638
return StorageSetter(name, value, info);
656639
}
657640

658641
static Intercepted IndexedQuery(uint32_t index,
659642
const PropertyCallbackInfo<Integer>& info) {
660643
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-
}
644+
Local<Name> name = Uint32ToString(env->context(), index);
668645
return StorageQuery(name, info);
669646
}
670647

671648
static Intercepted IndexedDeleter(uint32_t index,
672649
const PropertyCallbackInfo<Boolean>& info) {
673650
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-
}
651+
Local<Name> name = Uint32ToString(env->context(), index);
681652
return StorageDeleter(name, info);
682653
}
683654

684655
static Intercepted IndexedDefiner(uint32_t index,
685656
const PropertyDescriptor& desc,
686657
const PropertyCallbackInfo<void>& info) {
687658
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-
}
659+
Local<Name> name = Uint32ToString(env->context(), index);
695660
return StorageDefiner(name, desc, info);
696661
}
697662

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+
inline 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)