@@ -919,25 +919,35 @@ function scopeDeferredResult(value, scope) {
919919/** One carrier mid-walk on the direct road's descent. Module-private, so a
920920 * user value can never satisfy the driver's `instanceof` dispatch. */
921921class ScopeFrame {
922- constructor ( value , next , keys , descriptors ) {
922+ constructor ( value , prototype , keys , descriptors ) {
923923 this . value = value ;
924- this . next = next ;
924+ this . prototype = prototype ;
925+ this . next = null ;
925926 this . keys = keys ;
926927 this . descriptors = descriptors ;
927928 this . i = 0 ;
928929 this . changed = false ;
929930 }
930931}
931932
933+ /** The frame's shallow-rebuilt shell, built on first need: most carriers
934+ * hold nothing to bind and hand the author's container back unbuilt. */
935+ function frameShell ( frame ) {
936+ return ( frame . next ??= frame . keys
937+ ? Object . create ( frame . prototype , frame . descriptors )
938+ : frame . value . slice ( ) ) ;
939+ }
940+
932941/** Resolve one nested value for the direct road's carrier descent: leaves,
933942 * already-walked entries and deferred bodies answer immediately; a plain
934- * object or array answers a frame for the driver, its shallow-rebuilt shell
935- * pre-registered so cycles resolve to the shell being built . */
943+ * object or array answers a frame for the driver, pre-registered so a cycle
944+ * builds and resolves to its shell. */
936945function enterScopeCarrier ( value , state ) {
937946 if ( value === null || ( typeof value !== "object" && typeof value !== "function" ) ) return value ;
938947 if ( state . seen . has ( value ) ) {
939948 state . cyclic . add ( value ) ;
940- return state . seen . get ( value ) ;
949+ const seen = state . seen . get ( value ) ;
950+ return seen instanceof ScopeFrame ? frameShell ( seen ) : seen ;
941951 }
942952 const bound = bindDeferredBody ( value , state . scope ) ;
943953 if ( bound !== value ) {
@@ -952,9 +962,9 @@ function enterScopeCarrier(value, state) {
952962 state . seen . set ( value , value ) ;
953963 return value ;
954964 }
955- const next = value . slice ( ) ;
956- state . seen . set ( value , next ) ;
957- return new ScopeFrame ( value , next , null , null ) ;
965+ const frame = new ScopeFrame ( value , null , null , null ) ;
966+ state . seen . set ( value , frame ) ;
967+ return frame ;
958968 }
959969 const prototype = Object . getPrototypeOf ( value ) ;
960970 if ( ( prototype !== Object . prototype && prototype !== null ) || Object . isFrozen ( value ) ) {
@@ -965,10 +975,14 @@ function enterScopeCarrier(value, state) {
965975 // accessors and non-writable data stay exactly as authored (accessors are
966976 // the caller's to invoke, non-writable slots are out of the carrier set);
967977 // only writable enumerable data slots are candidates for binding.
968- const descriptors = Object . getOwnPropertyDescriptors ( value ) ;
969- const next = Object . create ( prototype , descriptors ) ;
970- state . seen . set ( value , next ) ;
971- return new ScopeFrame ( value , next , Object . keys ( value ) , descriptors ) ;
978+ const frame = new ScopeFrame (
979+ value ,
980+ prototype ,
981+ Object . keys ( value ) ,
982+ Object . getOwnPropertyDescriptors ( value )
983+ ) ;
984+ state . seen . set ( value , frame ) ;
985+ return frame ;
972986}
973987
974988// The descent is ITERATIVE for the same reason the codec road's walk is
@@ -1014,8 +1028,8 @@ function scopeDeferredContents(root, scope) {
10141028 }
10151029 }
10161030 if ( bound !== original ) {
1017- if ( top . keys ) top . next [ top . keys [ i ] ] = bound ;
1018- else top . next [ i ] = bound ;
1031+ if ( top . keys ) frameShell ( top ) [ top . keys [ i ] ] = bound ;
1032+ else frameShell ( top ) [ i ] = bound ;
10191033 top . changed = true ;
10201034 }
10211035 top . i ++ ;
@@ -1029,7 +1043,8 @@ function scopeDeferredContents(root, scope) {
10291043 // took the shell; otherwise the caller keeps the author's container
10301044 let out ;
10311045 if ( top . changed || state . cyclic . has ( top . value ) ) {
1032- out = top . next ;
1046+ out = frameShell ( top ) ;
1047+ state . seen . set ( top . value , out ) ;
10331048 } else {
10341049 state . seen . set ( top . value , top . value ) ;
10351050 out = top . value ;
0 commit comments