Skip to content

Commit 323f95d

Browse files
committedMay 8, 2024
src: migrate to new V8 interceptors API
Refs: v8#180 PR-URL: #52745 Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
1 parent fce31fc commit 323f95d

File tree

4 files changed

+152
-103
lines changed

4 files changed

+152
-103
lines changed
 

‎src/node_contextify.cc

+91-54
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ using v8::FunctionTemplate;
5151
using v8::HandleScope;
5252
using v8::IndexedPropertyHandlerConfiguration;
5353
using v8::Int32;
54+
using v8::Intercepted;
5455
using v8::Isolate;
5556
using v8::Just;
5657
using v8::Local;
@@ -458,14 +459,15 @@ bool ContextifyContext::IsStillInitializing(const ContextifyContext* ctx) {
458459
}
459460

460461
// static
461-
void ContextifyContext::PropertyGetterCallback(
462-
Local<Name> property,
463-
const PropertyCallbackInfo<Value>& args) {
462+
Intercepted ContextifyContext::PropertyGetterCallback(
463+
Local<Name> property, const PropertyCallbackInfo<Value>& args) {
464464
Environment* env = Environment::GetCurrent(args);
465465
ContextifyContext* ctx = ContextifyContext::Get(args);
466466

467467
// Still initializing
468-
if (IsStillInitializing(ctx)) return;
468+
if (IsStillInitializing(ctx)) {
469+
return Intercepted::kNo;
470+
}
469471

470472
Local<Context> context = ctx->context();
471473
Local<Object> sandbox = ctx->sandbox();
@@ -487,18 +489,22 @@ void ContextifyContext::PropertyGetterCallback(
487489
rv = ctx->global_proxy();
488490

489491
args.GetReturnValue().Set(rv);
492+
return Intercepted::kYes;
490493
}
494+
return Intercepted::kNo;
491495
}
492496

493497
// static
494-
void ContextifyContext::PropertySetterCallback(
498+
Intercepted ContextifyContext::PropertySetterCallback(
495499
Local<Name> property,
496500
Local<Value> value,
497-
const PropertyCallbackInfo<Value>& args) {
501+
const PropertyCallbackInfo<void>& args) {
498502
ContextifyContext* ctx = ContextifyContext::Get(args);
499503

500504
// Still initializing
501-
if (IsStillInitializing(ctx)) return;
505+
if (IsStillInitializing(ctx)) {
506+
return Intercepted::kNo;
507+
}
502508

503509
Local<Context> context = ctx->context();
504510
PropertyAttribute attributes = PropertyAttribute::None;
@@ -516,8 +522,9 @@ void ContextifyContext::PropertySetterCallback(
516522
(static_cast<int>(attributes) &
517523
static_cast<int>(PropertyAttribute::ReadOnly));
518524

519-
if (read_only)
520-
return;
525+
if (read_only) {
526+
return Intercepted::kNo;
527+
}
521528

522529
// true for x = 5
523530
// false for this.x = 5
@@ -536,11 +543,16 @@ void ContextifyContext::PropertySetterCallback(
536543

537544
bool is_declared = is_declared_on_global_proxy || is_declared_on_sandbox;
538545
if (!is_declared && args.ShouldThrowOnError() && is_contextual_store &&
539-
!is_function)
540-
return;
546+
!is_function) {
547+
return Intercepted::kNo;
548+
}
541549

542-
if (!is_declared && property->IsSymbol()) return;
543-
if (ctx->sandbox()->Set(context, property, value).IsNothing()) return;
550+
if (!is_declared && property->IsSymbol()) {
551+
return Intercepted::kNo;
552+
}
553+
if (ctx->sandbox()->Set(context, property, value).IsNothing()) {
554+
return Intercepted::kNo;
555+
}
544556

545557
Local<Value> desc;
546558
if (is_declared_on_sandbox &&
@@ -554,19 +566,23 @@ void ContextifyContext::PropertySetterCallback(
554566
// We have to specify the return value for any contextual or get/set
555567
// property
556568
if (desc_obj->HasOwnProperty(context, env->get_string()).FromMaybe(false) ||
557-
desc_obj->HasOwnProperty(context, env->set_string()).FromMaybe(false))
569+
desc_obj->HasOwnProperty(context, env->set_string()).FromMaybe(false)) {
558570
args.GetReturnValue().Set(value);
571+
return Intercepted::kYes;
572+
}
559573
}
574+
return Intercepted::kNo;
560575
}
561576

562577
// static
563-
void ContextifyContext::PropertyDescriptorCallback(
564-
Local<Name> property,
565-
const PropertyCallbackInfo<Value>& args) {
578+
Intercepted ContextifyContext::PropertyDescriptorCallback(
579+
Local<Name> property, const PropertyCallbackInfo<Value>& args) {
566580
ContextifyContext* ctx = ContextifyContext::Get(args);
567581

568582
// Still initializing
569-
if (IsStillInitializing(ctx)) return;
583+
if (IsStillInitializing(ctx)) {
584+
return Intercepted::kNo;
585+
}
570586

571587
Local<Context> context = ctx->context();
572588

@@ -576,19 +592,23 @@ void ContextifyContext::PropertyDescriptorCallback(
576592
Local<Value> desc;
577593
if (sandbox->GetOwnPropertyDescriptor(context, property).ToLocal(&desc)) {
578594
args.GetReturnValue().Set(desc);
595+
return Intercepted::kYes;
579596
}
580597
}
598+
return Intercepted::kNo;
581599
}
582600

583601
// static
584-
void ContextifyContext::PropertyDefinerCallback(
602+
Intercepted ContextifyContext::PropertyDefinerCallback(
585603
Local<Name> property,
586604
const PropertyDescriptor& desc,
587-
const PropertyCallbackInfo<Value>& args) {
605+
const PropertyCallbackInfo<void>& args) {
588606
ContextifyContext* ctx = ContextifyContext::Get(args);
589607

590608
// Still initializing
591-
if (IsStillInitializing(ctx)) return;
609+
if (IsStillInitializing(ctx)) {
610+
return Intercepted::kNo;
611+
}
592612

593613
Local<Context> context = ctx->context();
594614
Isolate* isolate = context->GetIsolate();
@@ -607,7 +627,7 @@ void ContextifyContext::PropertyDefinerCallback(
607627
// If the property is set on the global as neither writable nor
608628
// configurable, don't change it on the global or sandbox.
609629
if (is_declared && read_only && dont_delete) {
610-
return;
630+
return Intercepted::kNo;
611631
}
612632

613633
Local<Object> sandbox = ctx->sandbox();
@@ -630,6 +650,9 @@ void ContextifyContext::PropertyDefinerCallback(
630650
desc.has_set() ? desc.set() : Undefined(isolate).As<Value>());
631651

632652
define_prop_on_sandbox(&desc_for_sandbox);
653+
// TODO(https://github.com/nodejs/node/issues/52634): this should return
654+
// kYes to behave according to the expected semantics.
655+
return Intercepted::kNo;
633656
} else {
634657
Local<Value> value =
635658
desc.has_value() ? desc.value() : Undefined(isolate).As<Value>();
@@ -641,26 +664,33 @@ void ContextifyContext::PropertyDefinerCallback(
641664
PropertyDescriptor desc_for_sandbox(value);
642665
define_prop_on_sandbox(&desc_for_sandbox);
643666
}
667+
// TODO(https://github.com/nodejs/node/issues/52634): this should return
668+
// kYes to behave according to the expected semantics.
669+
return Intercepted::kNo;
644670
}
671+
return Intercepted::kNo;
645672
}
646673

647674
// static
648-
void ContextifyContext::PropertyDeleterCallback(
649-
Local<Name> property,
650-
const PropertyCallbackInfo<Boolean>& args) {
675+
Intercepted ContextifyContext::PropertyDeleterCallback(
676+
Local<Name> property, const PropertyCallbackInfo<Boolean>& args) {
651677
ContextifyContext* ctx = ContextifyContext::Get(args);
652678

653679
// Still initializing
654-
if (IsStillInitializing(ctx)) return;
680+
if (IsStillInitializing(ctx)) {
681+
return Intercepted::kNo;
682+
}
655683

656684
Maybe<bool> success = ctx->sandbox()->Delete(ctx->context(), property);
657685

658-
if (success.FromMaybe(false))
659-
return;
686+
if (success.FromMaybe(false)) {
687+
return Intercepted::kNo;
688+
}
660689

661690
// Delete failed on the sandbox, intercept and do not delete on
662691
// the global object.
663692
args.GetReturnValue().Set(false);
693+
return Intercepted::kYes;
664694
}
665695

666696
// static
@@ -680,76 +710,83 @@ void ContextifyContext::PropertyEnumeratorCallback(
680710
}
681711

682712
// static
683-
void ContextifyContext::IndexedPropertyGetterCallback(
684-
uint32_t index,
685-
const PropertyCallbackInfo<Value>& args) {
713+
Intercepted ContextifyContext::IndexedPropertyGetterCallback(
714+
uint32_t index, const PropertyCallbackInfo<Value>& args) {
686715
ContextifyContext* ctx = ContextifyContext::Get(args);
687716

688717
// Still initializing
689-
if (IsStillInitializing(ctx)) return;
718+
if (IsStillInitializing(ctx)) {
719+
return Intercepted::kNo;
720+
}
690721

691-
ContextifyContext::PropertyGetterCallback(
722+
return ContextifyContext::PropertyGetterCallback(
692723
Uint32ToName(ctx->context(), index), args);
693724
}
694725

695-
696-
void ContextifyContext::IndexedPropertySetterCallback(
726+
Intercepted ContextifyContext::IndexedPropertySetterCallback(
697727
uint32_t index,
698728
Local<Value> value,
699-
const PropertyCallbackInfo<Value>& args) {
729+
const PropertyCallbackInfo<void>& args) {
700730
ContextifyContext* ctx = ContextifyContext::Get(args);
701731

702732
// Still initializing
703-
if (IsStillInitializing(ctx)) return;
733+
if (IsStillInitializing(ctx)) {
734+
return Intercepted::kNo;
735+
}
704736

705-
ContextifyContext::PropertySetterCallback(
737+
return ContextifyContext::PropertySetterCallback(
706738
Uint32ToName(ctx->context(), index), value, args);
707739
}
708740

709741
// static
710-
void ContextifyContext::IndexedPropertyDescriptorCallback(
711-
uint32_t index,
712-
const PropertyCallbackInfo<Value>& args) {
742+
Intercepted ContextifyContext::IndexedPropertyDescriptorCallback(
743+
uint32_t index, const PropertyCallbackInfo<Value>& args) {
713744
ContextifyContext* ctx = ContextifyContext::Get(args);
714745

715746
// Still initializing
716-
if (IsStillInitializing(ctx)) return;
747+
if (IsStillInitializing(ctx)) {
748+
return Intercepted::kNo;
749+
}
717750

718-
ContextifyContext::PropertyDescriptorCallback(
751+
return ContextifyContext::PropertyDescriptorCallback(
719752
Uint32ToName(ctx->context(), index), args);
720753
}
721754

722-
723-
void ContextifyContext::IndexedPropertyDefinerCallback(
755+
Intercepted ContextifyContext::IndexedPropertyDefinerCallback(
724756
uint32_t index,
725757
const PropertyDescriptor& desc,
726-
const PropertyCallbackInfo<Value>& args) {
758+
const PropertyCallbackInfo<void>& args) {
727759
ContextifyContext* ctx = ContextifyContext::Get(args);
728760

729761
// Still initializing
730-
if (IsStillInitializing(ctx)) return;
762+
if (IsStillInitializing(ctx)) {
763+
return Intercepted::kNo;
764+
}
731765

732-
ContextifyContext::PropertyDefinerCallback(
766+
return ContextifyContext::PropertyDefinerCallback(
733767
Uint32ToName(ctx->context(), index), desc, args);
734768
}
735769

736770
// static
737-
void ContextifyContext::IndexedPropertyDeleterCallback(
738-
uint32_t index,
739-
const PropertyCallbackInfo<Boolean>& args) {
771+
Intercepted ContextifyContext::IndexedPropertyDeleterCallback(
772+
uint32_t index, const PropertyCallbackInfo<Boolean>& args) {
740773
ContextifyContext* ctx = ContextifyContext::Get(args);
741774

742775
// Still initializing
743-
if (IsStillInitializing(ctx)) return;
776+
if (IsStillInitializing(ctx)) {
777+
return Intercepted::kNo;
778+
}
744779

745780
Maybe<bool> success = ctx->sandbox()->Delete(ctx->context(), index);
746781

747-
if (success.FromMaybe(false))
748-
return;
782+
if (success.FromMaybe(false)) {
783+
return Intercepted::kNo;
784+
}
749785

750786
// Delete failed on the sandbox, intercept and do not delete on
751787
// the global object.
752788
args.GetReturnValue().Set(false);
789+
return Intercepted::kYes;
753790
}
754791

755792
void ContextifyScript::CreatePerIsolateProperties(

‎src/node_contextify.h

+17-20
Original file line numberDiff line numberDiff line change
@@ -96,42 +96,39 @@ class ContextifyContext : public BaseObject {
9696
const errors::TryCatchScope& try_catch);
9797
static void WeakCallback(
9898
const v8::WeakCallbackInfo<ContextifyContext>& data);
99-
static void PropertyGetterCallback(
99+
static v8::Intercepted PropertyGetterCallback(
100100
v8::Local<v8::Name> property,
101101
const v8::PropertyCallbackInfo<v8::Value>& args);
102-
static void PropertySetterCallback(
102+
static v8::Intercepted PropertySetterCallback(
103103
v8::Local<v8::Name> property,
104104
v8::Local<v8::Value> value,
105-
const v8::PropertyCallbackInfo<v8::Value>& args);
106-
static void PropertyDescriptorCallback(
105+
const v8::PropertyCallbackInfo<void>& args);
106+
static v8::Intercepted PropertyDescriptorCallback(
107107
v8::Local<v8::Name> property,
108108
const v8::PropertyCallbackInfo<v8::Value>& args);
109-
static void PropertyDefinerCallback(
109+
static v8::Intercepted PropertyDefinerCallback(
110110
v8::Local<v8::Name> property,
111111
const v8::PropertyDescriptor& desc,
112-
const v8::PropertyCallbackInfo<v8::Value>& args);
113-
static void PropertyDeleterCallback(
112+
const v8::PropertyCallbackInfo<void>& args);
113+
static v8::Intercepted PropertyDeleterCallback(
114114
v8::Local<v8::Name> property,
115115
const v8::PropertyCallbackInfo<v8::Boolean>& args);
116116
static void PropertyEnumeratorCallback(
117117
const v8::PropertyCallbackInfo<v8::Array>& args);
118-
static void IndexedPropertyGetterCallback(
119-
uint32_t index,
120-
const v8::PropertyCallbackInfo<v8::Value>& args);
121-
static void IndexedPropertySetterCallback(
118+
static v8::Intercepted IndexedPropertyGetterCallback(
119+
uint32_t index, const v8::PropertyCallbackInfo<v8::Value>& args);
120+
static v8::Intercepted IndexedPropertySetterCallback(
122121
uint32_t index,
123122
v8::Local<v8::Value> value,
124-
const v8::PropertyCallbackInfo<v8::Value>& args);
125-
static void IndexedPropertyDescriptorCallback(
126-
uint32_t index,
127-
const v8::PropertyCallbackInfo<v8::Value>& args);
128-
static void IndexedPropertyDefinerCallback(
123+
const v8::PropertyCallbackInfo<void>& args);
124+
static v8::Intercepted IndexedPropertyDescriptorCallback(
125+
uint32_t index, const v8::PropertyCallbackInfo<v8::Value>& args);
126+
static v8::Intercepted IndexedPropertyDefinerCallback(
129127
uint32_t index,
130128
const v8::PropertyDescriptor& desc,
131-
const v8::PropertyCallbackInfo<v8::Value>& args);
132-
static void IndexedPropertyDeleterCallback(
133-
uint32_t index,
134-
const v8::PropertyCallbackInfo<v8::Boolean>& args);
129+
const v8::PropertyCallbackInfo<void>& args);
130+
static v8::Intercepted IndexedPropertyDeleterCallback(
131+
uint32_t index, const v8::PropertyCallbackInfo<v8::Boolean>& args);
135132

136133
v8::Global<v8::Context> context_;
137134
std::unique_ptr<v8::MicrotaskQueue> microtask_queue_;

‎src/node_env_var.cc

+33-19
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ using v8::DontEnum;
1616
using v8::FunctionTemplate;
1717
using v8::HandleScope;
1818
using v8::Integer;
19+
using v8::Intercepted;
1920
using v8::Isolate;
2021
using v8::Just;
2122
using v8::Local;
@@ -336,24 +337,27 @@ Maybe<bool> KVStore::AssignToObject(v8::Isolate* isolate,
336337
return Just(true);
337338
}
338339

339-
static void EnvGetter(Local<Name> property,
340-
const PropertyCallbackInfo<Value>& info) {
340+
static Intercepted EnvGetter(Local<Name> property,
341+
const PropertyCallbackInfo<Value>& info) {
341342
Environment* env = Environment::GetCurrent(info);
342343
CHECK(env->has_run_bootstrapping_code());
343344
if (property->IsSymbol()) {
344-
return info.GetReturnValue().SetUndefined();
345+
info.GetReturnValue().SetUndefined();
346+
return Intercepted::kYes;
345347
}
346348
CHECK(property->IsString());
347349
MaybeLocal<String> value_string =
348350
env->env_vars()->Get(env->isolate(), property.As<String>());
349351
if (!value_string.IsEmpty()) {
350352
info.GetReturnValue().Set(value_string.ToLocalChecked());
353+
return Intercepted::kYes;
351354
}
355+
return Intercepted::kNo;
352356
}
353357

354-
static void EnvSetter(Local<Name> property,
355-
Local<Value> value,
356-
const PropertyCallbackInfo<Value>& info) {
358+
static Intercepted EnvSetter(Local<Name> property,
359+
Local<Value> value,
360+
const PropertyCallbackInfo<void>& info) {
357361
Environment* env = Environment::GetCurrent(info);
358362
CHECK(env->has_run_bootstrapping_code());
359363
// calling env->EmitProcessEnvWarning() sets a variable indicating that
@@ -369,35 +373,40 @@ static void EnvSetter(Local<Name> property,
369373
"the "
370374
"value to a string before setting process.env with it.",
371375
"DEP0104")
372-
.IsNothing())
373-
return;
376+
.IsNothing()) {
377+
return Intercepted::kNo;
378+
}
374379
}
375380

376381
Local<String> key;
377382
Local<String> value_string;
378383
if (!property->ToString(env->context()).ToLocal(&key) ||
379384
!value->ToString(env->context()).ToLocal(&value_string)) {
380-
return;
385+
return Intercepted::kNo;
381386
}
382387

383388
env->env_vars()->Set(env->isolate(), key, value_string);
384389

385-
// Whether it worked or not, always return value.
386-
info.GetReturnValue().Set(value);
390+
return Intercepted::kYes;
387391
}
388392

389-
static void EnvQuery(Local<Name> property,
390-
const PropertyCallbackInfo<Integer>& info) {
393+
static Intercepted EnvQuery(Local<Name> property,
394+
const PropertyCallbackInfo<Integer>& info) {
391395
Environment* env = Environment::GetCurrent(info);
392396
CHECK(env->has_run_bootstrapping_code());
393397
if (property->IsString()) {
394398
int32_t rc = env->env_vars()->Query(env->isolate(), property.As<String>());
395-
if (rc != -1) info.GetReturnValue().Set(rc);
399+
if (rc != -1) {
400+
// Return attributes for the property.
401+
info.GetReturnValue().Set(v8::None);
402+
return Intercepted::kYes;
403+
}
396404
}
405+
return Intercepted::kNo;
397406
}
398407

399-
static void EnvDeleter(Local<Name> property,
400-
const PropertyCallbackInfo<Boolean>& info) {
408+
static Intercepted EnvDeleter(Local<Name> property,
409+
const PropertyCallbackInfo<Boolean>& info) {
401410
Environment* env = Environment::GetCurrent(info);
402411
CHECK(env->has_run_bootstrapping_code());
403412
if (property->IsString()) {
@@ -407,6 +416,7 @@ static void EnvDeleter(Local<Name> property,
407416
// process.env never has non-configurable properties, so always
408417
// return true like the tc39 delete operator.
409418
info.GetReturnValue().Set(true);
419+
return Intercepted::kYes;
410420
}
411421

412422
static void EnvEnumerator(const PropertyCallbackInfo<Array>& info) {
@@ -417,9 +427,9 @@ static void EnvEnumerator(const PropertyCallbackInfo<Array>& info) {
417427
env->env_vars()->Enumerate(env->isolate()));
418428
}
419429

420-
static void EnvDefiner(Local<Name> property,
421-
const PropertyDescriptor& desc,
422-
const PropertyCallbackInfo<Value>& info) {
430+
static Intercepted EnvDefiner(Local<Name> property,
431+
const PropertyDescriptor& desc,
432+
const PropertyCallbackInfo<void>& info) {
423433
Environment* env = Environment::GetCurrent(info);
424434
if (desc.has_value()) {
425435
if (!desc.has_writable() ||
@@ -430,6 +440,7 @@ static void EnvDefiner(Local<Name> property,
430440
"configurable, writable,"
431441
" and enumerable "
432442
"data descriptor");
443+
return Intercepted::kYes;
433444
} else if (!desc.configurable() ||
434445
!desc.enumerable() ||
435446
!desc.writable()) {
@@ -438,6 +449,7 @@ static void EnvDefiner(Local<Name> property,
438449
"configurable, writable,"
439450
" and enumerable "
440451
"data descriptor");
452+
return Intercepted::kYes;
441453
} else {
442454
return EnvSetter(property, desc.value(), info);
443455
}
@@ -447,12 +459,14 @@ static void EnvDefiner(Local<Name> property,
447459
"'process.env' does not accept an"
448460
" accessor(getter/setter)"
449461
" descriptor");
462+
return Intercepted::kYes;
450463
} else {
451464
THROW_ERR_INVALID_OBJECT_DEFINE_PROPERTY(env,
452465
"'process.env' only accepts a "
453466
"configurable, writable,"
454467
" and enumerable "
455468
"data descriptor");
469+
return Intercepted::kYes;
456470
}
457471
}
458472

‎src/node_external_reference.h

+11-10
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,17 @@ class ExternalReferenceRegistry {
6767
V(v8::AccessorSetterCallback) \
6868
V(v8::AccessorNameGetterCallback) \
6969
V(v8::AccessorNameSetterCallback) \
70-
V(v8::GenericNamedPropertyDefinerCallback) \
71-
V(v8::GenericNamedPropertyDeleterCallback) \
72-
V(v8::GenericNamedPropertyEnumeratorCallback) \
73-
V(v8::GenericNamedPropertyQueryCallback) \
74-
V(v8::GenericNamedPropertySetterCallback) \
75-
V(v8::IndexedPropertySetterCallback) \
76-
V(v8::IndexedPropertyDefinerCallback) \
77-
V(v8::IndexedPropertyDeleterCallback) \
78-
V(v8::IndexedPropertyQueryCallback) \
79-
V(v8::IndexedPropertyDescriptorCallback) \
70+
V(v8::NamedPropertyGetterCallback) \
71+
V(v8::NamedPropertyDefinerCallback) \
72+
V(v8::NamedPropertyDeleterCallback) \
73+
V(v8::NamedPropertyEnumeratorCallback) \
74+
V(v8::NamedPropertyQueryCallback) \
75+
V(v8::NamedPropertySetterCallback) \
76+
V(v8::IndexedPropertyGetterCallbackV2) \
77+
V(v8::IndexedPropertySetterCallbackV2) \
78+
V(v8::IndexedPropertyDefinerCallbackV2) \
79+
V(v8::IndexedPropertyDeleterCallbackV2) \
80+
V(v8::IndexedPropertyQueryCallbackV2) \
8081
V(const v8::String::ExternalStringResourceBase*)
8182

8283
#define V(ExternalReferenceType) \

0 commit comments

Comments
 (0)
Please sign in to comment.