@@ -39006,38 +39006,49 @@ fn queueMicrotaskFn(ctx: *anyopaque, this: Value, args: []const Value) value.Hos
3900639006fn structuredCloneFn(ctx: *anyopaque, this: Value, args: []const Value) value.HostError!Value {
3900739007 _ = this;
3900839008 const self: *Interpreter = @ptrCast(@alignCast(ctx));
39009- const v = arg0(args);
39009+ const argument_roots = try self.pushTempRootSlice(args);
39010+ defer self.restoreTempRoots(argument_roots);
39011+ // This consumer finishes with its wire bytes before returning. Worker and
39012+ // embedding callers keep their explicit, independently owned wire buffers.
39013+ var operation_arena = std.heap.ArenaAllocator.init(self.scratch_allocator orelse gc_mod.temporaryAllocator(self.arena));
39014+ defer operation_arena.deinit();
39015+ const operation_allocator = operation_arena.allocator();
3901039016 // Collect + validate the transfer list before serializing.
39011- var transfer: std.ArrayListUnmanaged(*value.Object) = .empty;
39012- if (args.len > 1 and !args[1].isUndefined() and !args[1].isNull()) {
39013- if (!args[1].isObject()) return self.throwError("TypeError", "structuredClone options must be an object");
39014- const tv = try self.getProperty(args[1], "transfer");
39017+ var transfer: std.ArrayListUnmanaged(struct { slot: usize, fallback: Value }) = .empty;
39018+ const options = if (args.len > 1) self.tempRoot(argument_roots + 1, args[1]) else Value.undef();
39019+ if (!options.isUndefined() and !options.isNull()) {
39020+ if (!options.isObject()) return self.throwError("TypeError", "structuredClone options must be an object");
39021+ const tv = try self.getProperty(options, "transfer");
3901539022 if (!tv.isUndefined() and !tv.isNull()) {
3901639023 if (!tv.isObject() or !tv.asObj().is_array)
3901739024 return self.throwError("TypeError", "structuredClone transfer must be an array");
3901839025 // Snapshot the transfer list before validating entries. The list is
3901939026 // ordinary JS input, so a shared-realm sibling thread may mutate its
3902039027 // element storage while this builtin runs; validation itself must
3902139028 // not borrow `elements.items` directly.
39022- for (try tv.asObj().internalElementsSnapshot(self.arena)) |entry| {
39029+ const snapshot = try tv.asObj().internalElementsSnapshot(operation_allocator);
39030+ const transfer_roots = try self.pushTempRootSlice(snapshot);
39031+ for (snapshot, 0..) |fallback, index| {
39032+ const entry = self.tempRoot(transfer_roots + index, fallback);
3902339033 if (!entry.isObject() or entry.asObj().arrayBuffer() == null)
3902439034 return self.throwDOMException("DataCloneError", "only ArrayBuffers are transferable");
3902539035 const ab = entry.asObj().arrayBuffer().?;
3902639036 if (ab.is_shared) return self.throwDOMException("DataCloneError", "a SharedArrayBuffer is not transferable");
3902739037 if (ab.isDetached()) return self.throwDOMException("DataCloneError", "cannot transfer a detached ArrayBuffer");
3902839038 if (ab.immutable) return self.throwDOMException("DataCloneError", "cannot transfer an immutable ArrayBuffer");
3902939039 if (ab.is_wasm_memory) return self.throwDOMException("DataCloneError", "cannot transfer a WebAssembly.Memory buffer");
39030- for (transfer.items) |seen| if (seen == entry.asObj())
39040+ for (transfer.items) |seen| if (self.tempRoot( seen.slot, seen.fallback).asObj() == entry.asObj())
3903139041 return self.throwDOMException("DataCloneError", "duplicate transferable");
39032- try transfer.append(self.arena, entry.asObj() );
39042+ try transfer.append(operation_allocator, .{ .slot = transfer_roots + index, .fallback = fallback } );
3903339043 }
3903439044 }
3903539045 }
39036- const bytes = try structured_clone.serialize(self, self.arena, v);
39046+ const bytes = try structured_clone.serialize(self, operation_allocator, if (args.len > 0) self.tempRoot(argument_roots, args[0]) else Value.undef());
39047+ defer structured_clone.releaseSerialized(bytes);
3903739048 // The move: the clone owns a copy of the bytes; the sources detach now
3903839049 // (after a fully successful serialize, so a DataCloneError mid-graph
3903939050 // leaves the inputs untouched).
39040- for (transfer.items) |o| o .arrayBuffer().?.setDetached(true);
39051+ for (transfer.items) |entry| self.tempRoot(entry.slot, entry.fallback).asObj() .arrayBuffer().?.setDetached(true);
3904139052 return structured_clone.deserialize(self, bytes);
3904239053}
3904339054
0 commit comments