@@ -356,6 +356,66 @@ void PostDrain(const std::shared_ptr<CanaryRegistry>& registry) {
356356 return it != state->registry ->records .end () ? it->second : nullptr ;
357357}
358358
359+ // Set while this thread copies properties between a canary and its state, so
360+ // the canary's definer lets those defines through. Per thread rather than per
361+ // isolate: the copy runs no JS, so only its own defines reach an interceptor
362+ // on this thread meanwhile.
363+ thread_local bool copyingProperties_ = false ;
364+
365+ Local<Value> DescriptorField (Isolate* isolate, Local<Context> context, Local<Object> descriptor,
366+ const char * name) {
367+ Local<Value> value;
368+ return descriptor->Get (context, tns::ToV8String (isolate, name)).ToLocal (&value)
369+ ? value
370+ : Local<Value>(v8::Undefined (isolate));
371+ }
372+
373+ // Copies `from`'s own string- and symbol-keyed properties onto `to`, with their
374+ // attributes; an accessor keeps its functions, which run with whichever object
375+ // they are reached through as `this`.
376+ void CopyOwnProperties (Isolate* isolate, Local<Context> context, Local<Object> from,
377+ Local<Object> to) {
378+ HandleScope scope (isolate);
379+ TryCatch tc (isolate);
380+ bool wasCopying = copyingProperties_;
381+ copyingProperties_ = true ;
382+ Local<v8::Array> names;
383+ if (from->GetPropertyNames (context, KeyCollectionMode::kOwnOnly , PropertyFilter::ALL_PROPERTIES ,
384+ IndexFilter::kSkipIndices , KeyConversionMode::kConvertToString )
385+ .ToLocal (&names)) {
386+ for (uint32_t i = 0 ; i < names->Length (); i++) {
387+ Local<Value> name;
388+ if (!names->Get (context, i).ToLocal (&name) || !name->IsName ()) {
389+ continue ;
390+ }
391+ Local<v8::Name> key = name.As <v8::Name>();
392+ if (!from->HasRealNamedCallbackProperty (context, key).FromMaybe (true )) {
393+ PropertyAttribute attributes;
394+ Local<Value> value;
395+ if (from->GetRealNamedPropertyAttributes (context, key).To (&attributes) &&
396+ from->GetRealNamedProperty (context, key).ToLocal (&value)) {
397+ (void )to->DefineOwnProperty (context, key, value, attributes);
398+ }
399+ continue ;
400+ }
401+ Local<Value> descriptorValue;
402+ if (!from->GetOwnPropertyDescriptor (context, key).ToLocal (&descriptorValue) ||
403+ !descriptorValue->IsObject ()) {
404+ continue ;
405+ }
406+ Local<Object> descriptor = descriptorValue.As <Object>();
407+ PropertyDescriptor accessor (DescriptorField (isolate, context, descriptor, " get" ),
408+ DescriptorField (isolate, context, descriptor, " set" ));
409+ accessor.set_enumerable (
410+ DescriptorField (isolate, context, descriptor, " enumerable" )->BooleanValue (isolate));
411+ accessor.set_configurable (
412+ DescriptorField (isolate, context, descriptor, " configurable" )->BooleanValue (isolate));
413+ (void )to->DefineProperty (context, key, accessor);
414+ }
415+ }
416+ copyingProperties_ = wasCopying;
417+ }
418+
359419// The state object of the holder, if it has one.
360420bool ExistingState (Isolate* isolate, Local<Object> holder, Local<Object>* out) {
361421 ObjectWeakCallbackState* canary = CanaryOf (isolate, holder);
@@ -381,6 +441,9 @@ bool StateForWrite(Isolate* isolate, Local<Object> holder, Local<Object>* out) {
381441 return false ;
382442 }
383443 id object = canary->lifetimeTarget_ ;
444+ // The record died under a live canary (its token was removed): the
445+ // canary's properties seed the new state.
446+ bool replacesDied = canary->canaryRecord_ != nullptr ;
384447 // A JS-extended class's own -dealloc reports its instances' deaths.
385448 bool extended = IsExtendedInstance (object);
386449 // Allocated before the lock: a collection this allocation starts can run
@@ -432,6 +495,13 @@ bool StateForWrite(Isolate* isolate, Local<Object> holder, Local<Object>* out) {
432495 if (ObjectManager::LifetimeStats* stats = ObjectManager::GetLifetimeStats (isolate)) {
433496 stats->canaryStates ++;
434497 }
498+ if (replacesDied) {
499+ CopyOwnProperties (isolate, isolate->GetCurrentContext (), holder, freshState);
500+ }
501+ } else if (Usable (canary->canaryRecord_ )) {
502+ // Joined state another wrapper of the object wrote: the canary mirrors it.
503+ CopyOwnProperties (isolate, isolate->GetCurrentContext (),
504+ Local<Object>::New (isolate, canary->canaryRecord_ ->state ), holder);
435505 }
436506 }
437507 if (!Usable (canary->canaryRecord_ )) {
@@ -445,123 +515,102 @@ bool HasOwn(Local<Context> context, Local<Object> state, Local<v8::Name> propert
445515 return state->HasOwnProperty (context, property).FromMaybe (false );
446516}
447517
448- Intercepted Getter (Local<v8::Name> property, const PropertyCallbackInfo<Value>& info) {
449- Isolate* isolate = info.GetIsolate ();
450- Local<Object> state;
451- if (!ExistingState (isolate, info.Holder (), &state)) {
452- return Intercepted::kNo ;
453- }
454- Local<Context> context = isolate->GetCurrentContext ();
455- if (!HasOwn (context, state, property)) {
456- return Intercepted::kNo ;
457- }
458- Local<Value> value;
459- // An accessor defined through the canary runs with the canary as `this`
460- // (interceptors do not see the receiver, only the holder).
461- if (state->Get (context, property, info.Holder ()).ToLocal (&value)) {
462- info.GetReturnValue ().Set (value);
518+ // The object of `holder`'s prototype chain, `holder` included, with a real own
519+ // `property`, interceptors skipped; empty if there is none.
520+ Local<Object> OwnerOf (Local<Context> context, Local<Object> holder, Local<v8::Name> property) {
521+ Local<Object> object = holder;
522+ while (!object->HasRealNamedProperty (context, property).FromMaybe (false )) {
523+ Local<Value> prototype = object->GetPrototype ();
524+ if (!prototype->IsObject ()) {
525+ return Local<Object>();
526+ }
527+ object = prototype.As <Object>();
463528 }
464- return Intercepted:: kYes ;
529+ return object ;
465530}
466531
532+ // The handlers mirror: each copies its write into the state and declines, so
533+ // V8 also applies it to the canary, where reads find a real own property and
534+ // never call into C++. There is no getter, query, enumerator or descriptor
535+ // callback for the same reason. The handlers are masking, so they also see
536+ // names the prototype chain has. After the setter declines, V8 adds a new name
537+ // to the canary without calling the definer, so the setter itself decides what
538+ // becomes state.
467539Intercepted Setter (Local<v8::Name> property, Local<Value> value,
468540 const PropertyCallbackInfo<v8::Boolean>& info) {
469541 Isolate* isolate = info.GetIsolate ();
470- Local<Object> state;
471- if (!StateForWrite (isolate, info.Holder (), &state)) {
472- return Intercepted::kNo ;
473- }
474- Local<Context> context = isolate->GetCurrentContext ();
475- Maybe<bool > result = state->HasRealNamedCallbackProperty (context, property).FromMaybe (false )
476- ? state->Set (context, property, value, info.Holder ())
477- : state->Set (context, property, value);
478- if (result.IsJust ()) {
479- info.GetReturnValue ().Set (result.FromJust ());
480- }
481- return Intercepted::kYes ;
482- }
483-
484- Intercepted Query (Local<v8::Name> property, const PropertyCallbackInfo<v8::Integer>& info) {
485- Isolate* isolate = info.GetIsolate ();
486- Local<Object> state;
487- if (!ExistingState (isolate, info.Holder (), &state)) {
488- return Intercepted::kNo ;
542+ Local<Object> holder = info.Holder ();
543+ ObjectWeakCallbackState* canary = CanaryOf (isolate, holder);
544+ std::shared_ptr<CanaryRecord> borrowed;
545+ if (canary == nullptr ) {
546+ borrowed = BorrowedRecord (isolate, holder);
547+ if (!Usable (borrowed)) {
548+ return Intercepted::kNo ;
549+ }
489550 }
551+ CanaryRecord* record = canary != nullptr ? canary->canaryRecord_ .get () : borrowed.get ();
490552 Local<Context> context = isolate->GetCurrentContext ();
491- if (!HasOwn (context, state, property)) {
492- return Intercepted::kNo ;
553+ if (record != nullptr && !record->died .load (std::memory_order_acquire) &&
554+ !record->state .IsEmpty ()) {
555+ // Own on the state means own on the canary: a rewrite of a JS property.
556+ Local<Object> state = Local<Object>::New (isolate, record->state );
557+ if (HasOwn (context, state, property)) {
558+ if (!state->HasRealNamedCallbackProperty (context, property).FromMaybe (true )) {
559+ (void )state->Set (context, property, value);
560+ }
561+ return Intercepted::kNo ;
562+ }
493563 }
564+ // Searches the prototype chain too: absent there means a new name.
494565 PropertyAttribute attributes;
495- if (state->GetPropertyAttributes (context, property).To (&attributes)) {
496- info.GetReturnValue ().Set (v8::Integer::New (isolate, attributes));
566+ if (holder->GetRealNamedPropertyAttributes (context, property).To (&attributes)) {
567+ // V8 rejects a write to a read-only property and runs an accessor's
568+ // setter: a native property's (an accessor pair on its class's
569+ // prototype), a JS one's, or one defined on the canary, which runs with
570+ // the canary as `this` and so mirrors whatever it writes. None is state.
571+ if ((attributes & ReadOnly) != 0 ) {
572+ return Intercepted::kNo ;
573+ }
574+ Local<Object> owner = OwnerOf (context, holder, property);
575+ if (owner.IsEmpty () || owner->HasRealNamedCallbackProperty (context, property).FromMaybe (true )) {
576+ return Intercepted::kNo ;
577+ }
497578 }
498- return Intercepted::kYes ;
499- }
500-
501- Intercepted Deleter (Local<v8::Name> property, const PropertyCallbackInfo<v8::Boolean>& info) {
502- Isolate* isolate = info.GetIsolate ();
503579 Local<Object> state;
504- if (!ExistingState (isolate, info. Holder () , &state)) {
580+ if (!StateForWrite (isolate, holder , &state)) {
505581 return Intercepted::kNo ;
506582 }
507- Local<Context> context = isolate->GetCurrentContext ();
508583 if (!HasOwn (context, state, property)) {
509- return Intercepted::kNo ;
584+ (void )state->CreateDataProperty (context, property, value);
585+ } else if (!state->HasRealNamedCallbackProperty (context, property).FromMaybe (true )) {
586+ (void )state->Set (context, property, value);
510587 }
511- bool deleted;
512- if (state->Delete (context, property).To (&deleted)) {
513- info.GetReturnValue ().Set (deleted);
514- }
515- return Intercepted::kYes ;
588+ return Intercepted::kNo ;
516589}
517590
518- void Enumerator ( const PropertyCallbackInfo<v8::Array >& info) {
591+ Intercepted Deleter (Local<v8::Name> property, const PropertyCallbackInfo<v8::Boolean >& info) {
519592 Isolate* isolate = info.GetIsolate ();
520593 Local<Object> state;
521- if (!ExistingState (isolate, info.Holder (), &state)) {
522- return ;
523- }
524- Local<Context> context = isolate->GetCurrentContext ();
525- Local<v8::Array> names;
526- if (state
527- ->GetPropertyNames (context, KeyCollectionMode::kOwnOnly , PropertyFilter::ALL_PROPERTIES ,
528- IndexFilter::kSkipIndices )
529- .ToLocal (&names)) {
530- info.GetReturnValue ().Set (names);
594+ if (ExistingState (isolate, info.Holder (), &state)) {
595+ (void )state->Delete (isolate->GetCurrentContext (), property);
531596 }
597+ return Intercepted::kNo ;
532598}
533599
600+ // Object.defineProperty, define-semantics class fields and `super.x = v` on a
601+ // receiver that lacks `x` reach here.
534602Intercepted Definer (Local<v8::Name> property, const PropertyDescriptor& descriptor,
535603 const PropertyCallbackInfo<v8::Boolean>& info) {
536- Isolate* isolate = info.GetIsolate ();
537- Local<Object> state;
538- if (!StateForWrite (isolate, info.Holder (), &state)) {
604+ if (copyingProperties_) {
539605 return Intercepted::kNo ;
540606 }
541- Local<Context> context = isolate->GetCurrentContext ();
542- bool defined ;
543- if (state->DefineProperty (context, property, const_cast <PropertyDescriptor&>(descriptor))
544- .To (&defined )) {
545- info.GetReturnValue ().Set (defined );
546- }
547- return Intercepted::kYes ;
548- }
549-
550- Intercepted Descriptor (Local<v8::Name> property, const PropertyCallbackInfo<Value>& info) {
551607 Isolate* isolate = info.GetIsolate ();
552608 Local<Object> state;
553- if (!ExistingState (isolate, info.Holder (), &state)) {
554- return Intercepted::kNo ;
555- }
556- Local<Context> context = isolate->GetCurrentContext ();
557- if (!HasOwn (context, state, property)) {
558- return Intercepted::kNo ;
609+ if (StateForWrite (isolate, info.Holder (), &state)) {
610+ (void )state->DefineProperty (isolate->GetCurrentContext (), property,
611+ const_cast <PropertyDescriptor&>(descriptor));
559612 }
560- Local<Value> descriptor;
561- if (state->GetOwnPropertyDescriptor (context, property).ToLocal (&descriptor)) {
562- info.GetReturnValue ().Set (descriptor);
563- }
564- return Intercepted::kYes ;
613+ return Intercepted::kNo ;
565614}
566615
567616#if NS_CANARY_DEATH_SIGNAL == NS_CANARY_DEATH_WEAK_SWEEP
@@ -659,8 +708,24 @@ void PostSweepAfterGC(Isolate* isolate, GCType, GCCallbackFlags) {
659708 return ;
660709 }
661710 instanceTemplate->SetHandler (NamedPropertyHandlerConfiguration (
662- Getter, Setter, Query, Deleter, Enumerator, Definer, Descriptor, Local<Value>(),
663- PropertyHandlerFlags::kNonMasking ));
711+ nullptr , Setter, nullptr , Deleter, nullptr , Definer, nullptr , Local<Value>(),
712+ PropertyHandlerFlags::kNone ));
713+ }
714+
715+ void WrapperLifetimeCanary::Hydrate (Isolate* isolate, Local<Object> wrapper) {
716+ if (GetWrapperLifetimeMode () != WrapperLifetimeMode::kCanary ) {
717+ return ;
718+ }
719+ HandleScope scope (isolate);
720+ Local<Object> state;
721+ if (!ExistingState (isolate, wrapper, &state)) {
722+ return ;
723+ }
724+ Local<Context> context = isolate->GetCurrentContext ();
725+ if (context.IsEmpty ()) {
726+ context = Caches::Get (isolate)->GetContext ();
727+ }
728+ CopyOwnProperties (isolate, context, state, wrapper);
664729}
665730
666731std::shared_ptr<CanaryRecord> WrapperLifetimeCanary::Find (Isolate* isolate, id object) {
0 commit comments