Skip to content

Commit 46801dc

Browse files
isheludkotargos
authored andcommitted
src: migrate to new V8 interceptors API
Refs: v8#180
1 parent 7c3dce0 commit 46801dc

File tree

4 files changed

+152
-103
lines changed

4 files changed

+152
-103
lines changed

src/node_contextify.cc

Lines changed: 91 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ using v8::FunctionTemplate;
4949
using v8::HandleScope;
5050
using v8::IndexedPropertyHandlerConfiguration;
5151
using v8::Int32;
52+
using v8::Intercepted;
5253
using v8::Isolate;
5354
using v8::Just;
5455
using v8::Local;
@@ -455,14 +456,15 @@ bool ContextifyContext::IsStillInitializing(const ContextifyContext* ctx) {
455456
}
456457

457458
// static
458-
void ContextifyContext::PropertyGetterCallback(
459-
Local<Name> property,
460-
const PropertyCallbackInfo<Value>& args) {
459+
Intercepted ContextifyContext::PropertyGetterCallback(
460+
Local<Name> property, const PropertyCallbackInfo<Value>& args) {
461461
Environment* env = Environment::GetCurrent(args);
462462
ContextifyContext* ctx = ContextifyContext::Get(args);
463463

464464
// Still initializing
465-
if (IsStillInitializing(ctx)) return;
465+
if (IsStillInitializing(ctx)) {
466+
return Intercepted::kNo;
467+
}
466468

467469
Local<Context> context = ctx->context();
468470
Local<Object> sandbox = ctx->sandbox();
@@ -484,18 +486,22 @@ void ContextifyContext::PropertyGetterCallback(
484486
rv = ctx->global_proxy();
485487

486488
args.GetReturnValue().Set(rv);
489+
return Intercepted::kYes;
487490
}
491+
return Intercepted::kNo;
488492
}
489493

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

497501
// Still initializing
498-
if (IsStillInitializing(ctx)) return;
502+
if (IsStillInitializing(ctx)) {
503+
return Intercepted::kNo;
504+
}
499505

500506
Local<Context> context = ctx->context();
501507
PropertyAttribute attributes = PropertyAttribute::None;
@@ -513,8 +519,9 @@ void ContextifyContext::PropertySetterCallback(
513519
(static_cast<int>(attributes) &
514520
static_cast<int>(PropertyAttribute::ReadOnly));
515521

516-
if (read_only)
517-
return;
522+
if (read_only) {
523+
return Intercepted::kNo;
524+
}
518525

519526
// true for x = 5
520527
// false for this.x = 5
@@ -533,11 +540,16 @@ void ContextifyContext::PropertySetterCallback(
533540

534541
bool is_declared = is_declared_on_global_proxy || is_declared_on_sandbox;
535542
if (!is_declared && args.ShouldThrowOnError() && is_contextual_store &&
536-
!is_function)
537-
return;
543+
!is_function) {
544+
return Intercepted::kNo;
545+
}
538546

539-
if (!is_declared && property->IsSymbol()) return;
540-
if (ctx->sandbox()->Set(context, property, value).IsNothing()) return;
547+
if (!is_declared && property->IsSymbol()) {
548+
return Intercepted::kNo;
549+
}
550+
if (ctx->sandbox()->Set(context, property, value).IsNothing()) {
551+
return Intercepted::kNo;
552+
}
541553

542554
Local<Value> desc;
543555
if (is_declared_on_sandbox &&
@@ -551,19 +563,23 @@ void ContextifyContext::PropertySetterCallback(
551563
// We have to specify the return value for any contextual or get/set
552564
// property
553565
if (desc_obj->HasOwnProperty(context, env->get_string()).FromMaybe(false) ||
554-
desc_obj->HasOwnProperty(context, env->set_string()).FromMaybe(false))
566+
desc_obj->HasOwnProperty(context, env->set_string()).FromMaybe(false)) {
555567
args.GetReturnValue().Set(value);
568+
return Intercepted::kYes;
569+
}
556570
}
571+
return Intercepted::kNo;
557572
}
558573

559574
// static
560-
void ContextifyContext::PropertyDescriptorCallback(
561-
Local<Name> property,
562-
const PropertyCallbackInfo<Value>& args) {
575+
Intercepted ContextifyContext::PropertyDescriptorCallback(
576+
Local<Name> property, const PropertyCallbackInfo<Value>& args) {
563577
ContextifyContext* ctx = ContextifyContext::Get(args);
564578

565579
// Still initializing
566-
if (IsStillInitializing(ctx)) return;
580+
if (IsStillInitializing(ctx)) {
581+
return Intercepted::kNo;
582+
}
567583

568584
Local<Context> context = ctx->context();
569585

@@ -573,19 +589,23 @@ void ContextifyContext::PropertyDescriptorCallback(
573589
Local<Value> desc;
574590
if (sandbox->GetOwnPropertyDescriptor(context, property).ToLocal(&desc)) {
575591
args.GetReturnValue().Set(desc);
592+
return Intercepted::kYes;
576593
}
577594
}
595+
return Intercepted::kNo;
578596
}
579597

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

587605
// Still initializing
588-
if (IsStillInitializing(ctx)) return;
606+
if (IsStillInitializing(ctx)) {
607+
return Intercepted::kNo;
608+
}
589609

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

610630
Local<Object> sandbox = ctx->sandbox();
@@ -627,6 +647,9 @@ void ContextifyContext::PropertyDefinerCallback(
627647
desc.has_set() ? desc.set() : Undefined(isolate).As<Value>());
628648

629649
define_prop_on_sandbox(&desc_for_sandbox);
650+
// TODO(https://github.com/nodejs/node/issues/52634): this should return
651+
// kYes to behave according to the expected semantics.
652+
return Intercepted::kNo;
630653
} else {
631654
Local<Value> value =
632655
desc.has_value() ? desc.value() : Undefined(isolate).As<Value>();
@@ -638,26 +661,33 @@ void ContextifyContext::PropertyDefinerCallback(
638661
PropertyDescriptor desc_for_sandbox(value);
639662
define_prop_on_sandbox(&desc_for_sandbox);
640663
}
664+
// TODO(https://github.com/nodejs/node/issues/52634): this should return
665+
// kYes to behave according to the expected semantics.
666+
return Intercepted::kNo;
641667
}
668+
return Intercepted::kNo;
642669
}
643670

644671
// static
645-
void ContextifyContext::PropertyDeleterCallback(
646-
Local<Name> property,
647-
const PropertyCallbackInfo<Boolean>& args) {
672+
Intercepted ContextifyContext::PropertyDeleterCallback(
673+
Local<Name> property, const PropertyCallbackInfo<Boolean>& args) {
648674
ContextifyContext* ctx = ContextifyContext::Get(args);
649675

650676
// Still initializing
651-
if (IsStillInitializing(ctx)) return;
677+
if (IsStillInitializing(ctx)) {
678+
return Intercepted::kNo;
679+
}
652680

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

655-
if (success.FromMaybe(false))
656-
return;
683+
if (success.FromMaybe(false)) {
684+
return Intercepted::kNo;
685+
}
657686

658687
// Delete failed on the sandbox, intercept and do not delete on
659688
// the global object.
660689
args.GetReturnValue().Set(false);
690+
return Intercepted::kYes;
661691
}
662692

663693
// static
@@ -677,76 +707,83 @@ void ContextifyContext::PropertyEnumeratorCallback(
677707
}
678708

679709
// static
680-
void ContextifyContext::IndexedPropertyGetterCallback(
681-
uint32_t index,
682-
const PropertyCallbackInfo<Value>& args) {
710+
Intercepted ContextifyContext::IndexedPropertyGetterCallback(
711+
uint32_t index, const PropertyCallbackInfo<Value>& args) {
683712
ContextifyContext* ctx = ContextifyContext::Get(args);
684713

685714
// Still initializing
686-
if (IsStillInitializing(ctx)) return;
715+
if (IsStillInitializing(ctx)) {
716+
return Intercepted::kNo;
717+
}
687718

688-
ContextifyContext::PropertyGetterCallback(
719+
return ContextifyContext::PropertyGetterCallback(
689720
Uint32ToName(ctx->context(), index), args);
690721
}
691722

692-
693-
void ContextifyContext::IndexedPropertySetterCallback(
723+
Intercepted ContextifyContext::IndexedPropertySetterCallback(
694724
uint32_t index,
695725
Local<Value> value,
696-
const PropertyCallbackInfo<Value>& args) {
726+
const PropertyCallbackInfo<void>& args) {
697727
ContextifyContext* ctx = ContextifyContext::Get(args);
698728

699729
// Still initializing
700-
if (IsStillInitializing(ctx)) return;
730+
if (IsStillInitializing(ctx)) {
731+
return Intercepted::kNo;
732+
}
701733

702-
ContextifyContext::PropertySetterCallback(
734+
return ContextifyContext::PropertySetterCallback(
703735
Uint32ToName(ctx->context(), index), value, args);
704736
}
705737

706738
// static
707-
void ContextifyContext::IndexedPropertyDescriptorCallback(
708-
uint32_t index,
709-
const PropertyCallbackInfo<Value>& args) {
739+
Intercepted ContextifyContext::IndexedPropertyDescriptorCallback(
740+
uint32_t index, const PropertyCallbackInfo<Value>& args) {
710741
ContextifyContext* ctx = ContextifyContext::Get(args);
711742

712743
// Still initializing
713-
if (IsStillInitializing(ctx)) return;
744+
if (IsStillInitializing(ctx)) {
745+
return Intercepted::kNo;
746+
}
714747

715-
ContextifyContext::PropertyDescriptorCallback(
748+
return ContextifyContext::PropertyDescriptorCallback(
716749
Uint32ToName(ctx->context(), index), args);
717750
}
718751

719-
720-
void ContextifyContext::IndexedPropertyDefinerCallback(
752+
Intercepted ContextifyContext::IndexedPropertyDefinerCallback(
721753
uint32_t index,
722754
const PropertyDescriptor& desc,
723-
const PropertyCallbackInfo<Value>& args) {
755+
const PropertyCallbackInfo<void>& args) {
724756
ContextifyContext* ctx = ContextifyContext::Get(args);
725757

726758
// Still initializing
727-
if (IsStillInitializing(ctx)) return;
759+
if (IsStillInitializing(ctx)) {
760+
return Intercepted::kNo;
761+
}
728762

729-
ContextifyContext::PropertyDefinerCallback(
763+
return ContextifyContext::PropertyDefinerCallback(
730764
Uint32ToName(ctx->context(), index), desc, args);
731765
}
732766

733767
// static
734-
void ContextifyContext::IndexedPropertyDeleterCallback(
735-
uint32_t index,
736-
const PropertyCallbackInfo<Boolean>& args) {
768+
Intercepted ContextifyContext::IndexedPropertyDeleterCallback(
769+
uint32_t index, const PropertyCallbackInfo<Boolean>& args) {
737770
ContextifyContext* ctx = ContextifyContext::Get(args);
738771

739772
// Still initializing
740-
if (IsStillInitializing(ctx)) return;
773+
if (IsStillInitializing(ctx)) {
774+
return Intercepted::kNo;
775+
}
741776

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

744-
if (success.FromMaybe(false))
745-
return;
779+
if (success.FromMaybe(false)) {
780+
return Intercepted::kNo;
781+
}
746782

747783
// Delete failed on the sandbox, intercept and do not delete on
748784
// the global object.
749785
args.GetReturnValue().Set(false);
786+
return Intercepted::kYes;
750787
}
751788

752789
void ContextifyScript::CreatePerIsolateProperties(

src/node_contextify.h

Lines changed: 17 additions & 20 deletions
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_;

0 commit comments

Comments
 (0)