@@ -2616,9 +2616,13 @@ pub fn jsonStringify(ctx: *anyopaque, this: Value, args: []const Value) HostErro
26162616 self .env = env ;
26172617 };
26182618 const a = self .arena ;
2619- const cycle_allocator = gc_mod .temporaryAllocator (a );
2620- var st = Stringifier { .self = self , .cycle_allocator = cycle_allocator };
2621- defer st .active .deinit (a , cycle_allocator );
2619+ const temporary_allocator = gc_mod .temporaryAllocator (a );
2620+ var st = Stringifier {
2621+ .self = self ,
2622+ .cycle_allocator = temporary_allocator ,
2623+ .output_allocator = temporary_allocator ,
2624+ };
2625+ defer st .active .deinit (a , temporary_allocator );
26222626
26232627 // arg 1 — replacer: a callable (transform) or an array (property allowlist).
26242628 const replacer = arg (args , 1 );
@@ -2698,15 +2702,19 @@ pub fn jsonStringify(ctx: *anyopaque, this: Value, args: []const Value) HostErro
26982702 const holder = (try self .newObject ()).asObj ();
26992703 try holder .setOwn (self .arena , self .root_shape , "" , arg (args , 0 ));
27002704 var buf : std .ArrayListUnmanaged (u8 ) = .empty ;
2705+ defer buf .deinit (temporary_allocator );
27012706 if (! try st .serialize (& buf , Value .obj (holder ), "" )) return Value .undef ();
2702- return try Value .strOwned (a , try buf .toOwnedSlice (a ));
2707+ if (temporary_allocator .ptr == a .ptr and temporary_allocator .vtable == a .vtable )
2708+ return try Value .strOwned (a , try buf .toOwnedSlice (a ));
2709+ return try Value .strAlloc (a , buf .items );
27032710}
27042711
27052712/// Carries the `JSON.stringify` options (replacer / allowlist / indent gap)
27062713/// and the cycle-detection stack across the recursive serialization.
27072714const Stringifier = struct {
27082715 self : * Interpreter ,
27092716 cycle_allocator : std.mem.Allocator ,
2717+ output_allocator : std.mem.Allocator ,
27102718 replacer_fn : ? Value = null ,
27112719 allow : ? []const []const u8 = null ,
27122720 gap : []const u8 = "" ,
@@ -2725,6 +2733,7 @@ const Stringifier = struct {
27252733 defer self .depth -= 1 ;
27262734 try self .stackGuard ();
27272735 const a = self .arena ;
2736+ const output_allocator = st .output_allocator ;
27282737 var v = try self .getProperty (holder , key );
27292738 if (v .isObject () and ! v .asObj ().is_symbol ) {
27302739 const tj = try self .getProperty (v , "toJSON" );
@@ -2736,7 +2745,7 @@ const Stringifier = struct {
27362745 // A JSON.rawJSON object emits its validated text verbatim.
27372746 if (v .isObject () and v .asObj ().behavior .is_raw_json ) {
27382747 const raw = (v .asObj ().getOwn ("rawJSON" ) orelse Value .str ("" )).asStr ();
2739- try buf .appendSlice (a , raw );
2748+ try buf .appendSlice (output_allocator , raw );
27402749 return true ;
27412750 }
27422751 // SerializeJSONProperty: a [[NumberData]] wrapper → ToNumber, a
@@ -2762,13 +2771,13 @@ const Stringifier = struct {
27622771 return self .throwError ("TypeError" , "Do not know how to serialize a BigInt" );
27632772 switch (v .kind ()) {
27642773 .undefined = > return false ,
2765- .null = > try buf .appendSlice (a , "null" ),
2766- .boolean = > try buf .appendSlice (a , if (v .asBool ()) "true" else "false" ),
2774+ .null = > try buf .appendSlice (output_allocator , "null" ),
2775+ .boolean = > try buf .appendSlice (output_allocator , if (v .asBool ()) "true" else "false" ),
27672776 .number = > {
27682777 const n = v .asNum ();
2769- try buf .appendSlice (a , if (std .math .isNan (n ) or std .math .isInf (n )) "null" else try value .numberToString (a , n ));
2778+ try buf .appendSlice (output_allocator , if (std .math .isNan (n ) or std .math .isInf (n )) "null" else try value .numberToString (a , n ));
27702779 },
2771- .string = > try writeJsonString (a , buf , try v .asWtf8 (a )),
2780+ .string = > try writeJsonString (output_allocator , buf , try v .asWtf8 (a )),
27722781 .object = > {
27732782 const o = v .asObj ();
27742783 if (o .isCallableObject () or o .is_symbol ) return false ; // functions/symbols omitted
@@ -2800,34 +2809,36 @@ const Stringifier = struct {
28002809
28012810 fn serializeArray (st : * Stringifier , buf : * std .ArrayListUnmanaged (u8 ), holder : Value , shape : * value .Object ) HostError ! void {
28022811 const a = st .self .arena ;
2812+ const output_allocator = st .output_allocator ;
28032813 // SerializeJSONArray reads the length via LengthOfArrayLike (Get) — for a
28042814 // Proxy that runs the "length" trap (and propagates an abrupt completion).
28052815 const len : usize = if (holder .isObject () and (holder .asObj ().proxyHandler () != null or holder .asObj ().proxy_revoked ))
28062816 interpreter .toLen (try st .self .toNumberV (try st .self .getProperty (holder , "length" )))
28072817 else
28082818 shape .arrayLength ();
28092819 if (len == 0 ) {
2810- try buf .appendSlice (a , "[]" );
2820+ try buf .appendSlice (output_allocator , "[]" );
28112821 return ;
28122822 }
28132823 const outer = st .indent .items .len ;
28142824 try st .indent .appendSlice (a , st .gap );
2815- try buf .append (a , '[' );
2825+ try buf .append (output_allocator , '[' );
28162826 var i : usize = 0 ;
28172827 while (i < len ) : (i += 1 ) {
2818- if (i != 0 ) try buf .append (a , ',' );
2828+ if (i != 0 ) try buf .append (output_allocator , ',' );
28192829 try st .newlineIndent (buf );
28202830 const key = try std .fmt .allocPrint (a , "{d}" , .{i });
2821- if (! try st .serialize (buf , holder , key )) try buf .appendSlice (a , "null" );
2831+ if (! try st .serialize (buf , holder , key )) try buf .appendSlice (output_allocator , "null" );
28222832 }
28232833 st .indent .shrinkRetainingCapacity (outer );
28242834 try st .newlineIndent (buf );
2825- try buf .append (a , ']' );
2835+ try buf .append (output_allocator , ']' );
28262836 }
28272837
28282838 fn serializeObject (st : * Stringifier , buf : * std .ArrayListUnmanaged (u8 ), v : Value , shape : * value .Object ) HostError ! void {
28292839 const self = st .self ;
28302840 const a = self .arena ;
2841+ const output_allocator = st .output_allocator ;
28312842 const keys = if (st .allow ) | al | al else try st .jsonObjectKeys (v .asObj (), shape );
28322843 // A Proxy's enumerability comes from [[GetOwnProperty]] (the
28332844 // getOwnPropertyDescriptor trap), which EnumerableOwnPropertyNames runs
@@ -2836,8 +2847,8 @@ const Stringifier = struct {
28362847 const is_proxy = v .asObj ().proxyHandler () != null or v .asObj ().proxy_revoked ;
28372848 const outer = st .indent .items .len ;
28382849 try st .indent .appendSlice (a , st .gap );
2839- var tmp : std .ArrayListUnmanaged (u8 ) = .empty ;
28402850 var count : usize = 0 ;
2851+ try buf .append (output_allocator , '{' );
28412852 for (keys ) | k | {
28422853 if (jsonHiddenKey (k )) continue ;
28432854 if (st .allow == null ) {
@@ -2847,25 +2858,28 @@ const Stringifier = struct {
28472858 } else shape .getAttr (k ).enumerable ;
28482859 if (! enumerable ) continue ;
28492860 }
2850- var member : std .ArrayListUnmanaged (u8 ) = .empty ;
2851- if (! try st .serialize (& member , v , k )) continue ; // omitted property
2852- if (count != 0 ) try tmp .append (a , ',' );
2853- try st .newlineIndentTo (& tmp , st .indent .items );
2854- try writeJsonString (a , & tmp , value .decodeStringKey (k ));
2855- try tmp .append (a , ':' );
2856- if (st .gap .len != 0 ) try tmp .append (a , ' ' );
2857- try tmp .appendSlice (a , member .items );
2861+ // Serialize directly into the authoritative output. A temporary
2862+ // member buffer recopies a successful descendant suffix at every
2863+ // ancestor, making a depth-n chain quadratic in output bytes. The
2864+ // mark retains exact omission semantics without publishing a comma
2865+ // or key when SerializeJSONProperty returns undefined.
2866+ const mark = buf .items .len ;
2867+ if (count != 0 ) try buf .append (output_allocator , ',' );
2868+ try st .newlineIndent (buf );
2869+ try writeJsonString (output_allocator , buf , value .decodeStringKey (k ));
2870+ try buf .append (output_allocator , ':' );
2871+ if (st .gap .len != 0 ) try buf .append (output_allocator , ' ' );
2872+ if (! try st .serialize (buf , v , k )) {
2873+ buf .shrinkRetainingCapacity (mark );
2874+ continue ;
2875+ }
28582876 count += 1 ;
28592877 }
28602878 st .indent .shrinkRetainingCapacity (outer );
2861- if (count == 0 ) {
2862- try buf .appendSlice (a , "{}" );
2863- return ;
2879+ if (count != 0 ) {
2880+ try st .newlineIndent (buf );
28642881 }
2865- try buf .append (a , '{' );
2866- try buf .appendSlice (a , tmp .items );
2867- try st .newlineIndent (buf );
2868- try buf .append (a , '}' );
2882+ try buf .append (output_allocator , '}' );
28692883 }
28702884
28712885 fn jsonObjectKeys (st : * Stringifier , object : * value.Object , shape : * value.Object ) HostError ! []const []const u8 {
@@ -2886,8 +2900,8 @@ const Stringifier = struct {
28862900 }
28872901 fn newlineIndentTo (st : * Stringifier , buf : * std .ArrayListUnmanaged (u8 ), ind : []const u8 ) HostError ! void {
28882902 if (st .gap .len == 0 ) return ;
2889- try buf .append (st .self . arena , '\n ' );
2890- try buf .appendSlice (st .self . arena , ind );
2903+ try buf .append (st .output_allocator , '\n ' );
2904+ try buf .appendSlice (st .output_allocator , ind );
28912905 }
28922906};
28932907
0 commit comments