1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef js_StructuredClone_h
8 #define js_StructuredClone_h
10 #include "mozilla/Attributes.h"
11 #include "mozilla/BufferList.h"
12 #include "mozilla/MemoryReporting.h"
19 #include "js/AllocPolicy.h"
20 #include "js/RootingAPI.h"
21 #include "js/TypeDecls.h"
23 #include "js/Vector.h"
26 * API for safe passing of structured data, HTML 2018 Feb 21 section 2.7.
27 * <https://html.spec.whatwg.org/multipage/structured-data.html>
29 * This is a serialization scheme for JS values, somewhat like JSON. It
30 * preserves some aspects of JS objects (strings, numbers, own data properties
31 * with string keys, array elements) but not others (methods, getters and
32 * setters, prototype chains). Unlike JSON, structured data:
34 * - can contain cyclic references.
36 * - handles Maps, Sets, and some other object types.
38 * - supports *transferring* objects of certain types from one realm to
39 * another, rather than cloning them.
41 * - is specified by a living standard, and continues to evolve.
43 * - is encoded in a nonstandard binary format, and is never exposed to Web
44 * content in its serialized form. It's used internally by the browser to
45 * send data from one thread/realm/domain to another, not across the
49 struct JSStructuredCloneReader
;
50 struct JSStructuredCloneWriter
;
53 * The structured-clone serialization format version number.
55 * When serialized data is stored as bytes, e.g. in your Firefox profile, later
56 * versions of the engine may have to read it. When you upgrade Firefox, we
57 * don't crawl through your whole profile converting all saved data from the
58 * previous version of the serialization format to the latest version. So it is
59 * normal to have data in old formats stored in your profile.
61 * The JS engine can *write* data only in the current format version.
63 * It can *read* any data written in the current version, and data written for
64 * DifferentProcess scope in earlier versions.
67 * ## When to bump this version number
69 * When making a change so drastic that the JS engine needs to know whether
70 * it's reading old or new serialized data in order to handle both correctly,
71 * increment this version number. Make sure the engine can still read all
72 * old data written with previous versions.
74 * If StructuredClone.cpp doesn't contain code that distinguishes between
75 * version 8 and version 9, there should not be a version 9.
77 * Do not increment for changes that only affect SameProcess encoding.
79 * Increment only for changes that would otherwise break old serialized data.
80 * Do not increment for new data types. (Rationale: Modulo bugs, older versions
81 * of the JS engine can already correctly throw errors when they encounter new,
82 * unrecognized features. A version number bump does not actually help them.)
84 #define JS_STRUCTURED_CLONE_VERSION 8
89 * Indicates the "scope of validity" of serialized data.
91 * Writing plain JS data produces an array of bytes that can be copied and
92 * read in another process or whatever. The serialized data is Plain Old Data.
93 * However, HTML also supports `Transferable` objects, which, when cloned, can
94 * be moved from the source object into the clone, like when you take a
95 * photograph of someone and it steals their soul.
96 * See <https://developer.mozilla.org/en-US/docs/Web/API/Transferable>.
97 * We support cloning and transferring objects of many types.
99 * For example, when we transfer an ArrayBuffer (within a process), we "detach"
100 * the ArrayBuffer, embed the raw buffer pointer in the serialized data, and
101 * later install it in a new ArrayBuffer in the destination realm. Ownership
102 * of that buffer memory is transferred from the original ArrayBuffer to the
103 * serialized data and then to the clone.
105 * This only makes sense within a single address space. When we transfer an
106 * ArrayBuffer to another process, the contents of the buffer must be copied
107 * into the serialized data. (The original ArrayBuffer is still detached,
108 * though, for consistency; in some cases the caller shouldn't know or care if
109 * the recipient is in the same process.)
111 * ArrayBuffers are actually a lucky case; some objects (like MessagePorts)
112 * can't reasonably be stored by value in serialized data -- it's pointers or
115 * So there is a tradeoff between scope of validity -- how far away the
116 * serialized data may be sent and still make sense -- and efficiency or
117 * features. The read and write algorithms therefore take an argument of this
118 * type, allowing the user to control those trade-offs.
120 enum class StructuredCloneScope
: uint32_t {
122 * The most restrictive scope, with greatest efficiency and features.
124 * When writing, this means: The caller promises that the serialized data
125 * will **not** be shipped off to a different process or stored in a
126 * database. However, it may be shipped to another thread. It's OK to
127 * produce serialized data that contains pointers to data that is safe to
128 * send across threads, such as array buffers. In Rust terms, the
129 * serialized data will be treated as `Send` but not `Copy`.
131 * When reading, this means: Accept transferred objects and buffers
132 * (pointers). The caller promises that the serialized data was written
133 * using this API (otherwise, the serialized data may contain bogus
134 * pointers, leading to undefined behavior).
136 * Starts from 1 because there used to be a SameProcessSameThread enum value
137 * of 0 and these values are encoded into the structured serialization format
138 * as part of the SCTAG_HEADER, and IndexedDB persists the representation to
144 * When writing, this means we're writing for an audience in a different
145 * process. Produce serialized data that can be sent to other processes,
146 * bitwise copied, or even stored as bytes in a database and read by later
147 * versions of Firefox years from now. The HTML5 spec refers to this as
148 * "ForStorage" as in StructuredSerializeForStorage, though we use
149 * DifferentProcess for IPC as well as storage.
151 * Transferable objects are limited to ArrayBuffers, whose contents are
152 * copied into the serialized data (rather than just writing a pointer).
154 * When reading, this means: Do not accept pointers.
159 * Handle a backwards-compatibility case with IndexedDB (bug 1434308): when
160 * reading, this means to treat legacy SameProcess data as if it were
163 * Do not use this for writing; use DifferentProcess instead.
165 DifferentProcessForIndexedDB
,
168 * Existing code wants to be able to create an uninitialized
169 * JSStructuredCloneData without knowing the scope, then populate it with
170 * data (at which point the scope *is* known.)
175 * This scope is used when the deserialization context is unknown. When
176 * writing, DifferentProcess or SameProcess scope is chosen based on the
177 * nature of the object.
182 enum TransferableOwnership
{
183 /** Transferable data has not been filled in yet */
184 SCTAG_TMO_UNFILLED
= 0,
186 /** Structured clone buffer does not yet own the data */
187 SCTAG_TMO_UNOWNED
= 1,
189 /** All values at least this large are owned by the clone buffer */
190 SCTAG_TMO_FIRST_OWNED
= 2,
192 /** Data is a pointer that can be freed */
193 SCTAG_TMO_ALLOC_DATA
= 2,
195 /** Data is a memory mapped pointer */
196 SCTAG_TMO_MAPPED_DATA
= 3,
199 * Data is embedding-specific. The engine can free it by calling the
200 * freeTransfer op. The embedding can also use SCTAG_TMO_USER_MIN and
201 * greater, up to 32 bits, to distinguish specific ownership variants.
203 SCTAG_TMO_CUSTOM
= 4,
208 class CloneDataPolicy
{
209 bool allowIntraClusterClonableSharedObjects_
;
210 bool allowSharedMemoryObjects_
;
213 // The default is to deny all policy-controlled aspects.
216 : allowIntraClusterClonableSharedObjects_(false),
217 allowSharedMemoryObjects_(false) {}
219 // SharedArrayBuffers and WASM modules can only be cloned intra-process
220 // because the shared memory areas are allocated in process-private memory or
221 // because there are security issues of sharing them cross agent clusters.
222 // y default, we don't allow shared-memory and intra-cluster objects. Clients
223 // should therefore enable these 2 clone features when needed.
225 void allowIntraClusterClonableSharedObjects() {
226 allowIntraClusterClonableSharedObjects_
= true;
229 bool areIntraClusterClonableSharedObjectsAllowed() const {
230 return allowIntraClusterClonableSharedObjects_
;
233 void allowSharedMemoryObjects() { allowSharedMemoryObjects_
= true; }
235 bool areSharedMemoryObjectsAllowed() const {
236 return allowSharedMemoryObjects_
;
243 * Read structured data from the reader r. This hook is used to read a value
244 * previously serialized by a call to the WriteStructuredCloneOp hook.
246 * tag and data are the pair of uint32_t values from the header. The callback
247 * may use the JS_Read* APIs to read any other relevant parts of the object
248 * from the reader r. closure is any value passed to the JS_ReadStructuredClone
249 * function. Return the new object on success, nullptr on error/exception.
251 typedef JSObject
* (*ReadStructuredCloneOp
)(
252 JSContext
* cx
, JSStructuredCloneReader
* r
,
253 const JS::CloneDataPolicy
& cloneDataPolicy
, uint32_t tag
, uint32_t data
,
257 * Structured data serialization hook. The engine can write primitive values,
258 * Objects, Arrays, Dates, RegExps, TypedArrays, ArrayBuffers, Sets, Maps,
259 * and SharedTypedArrays. Any other type of object requires application support.
260 * This callback must first use the JS_WriteUint32Pair API to write an object
261 * header, passing a value greater than JS_SCTAG_USER to the tag parameter.
262 * Then it can use the JS_Write* APIs to write any other relevant parts of
263 * the value v to the writer w. closure is any value passed to the
264 * JS_WriteStructuredClone function.
266 * Return true on success, false on error/exception.
268 typedef bool (*WriteStructuredCloneOp
)(JSContext
* cx
,
269 JSStructuredCloneWriter
* w
,
270 JS::HandleObject obj
,
271 bool* sameProcessScopeRequired
,
275 * This is called when JS_WriteStructuredClone is given an invalid transferable.
276 * To follow HTML5, the application must throw a DATA_CLONE_ERR DOMException
277 * with error set to one of the JS_SCERR_* values.
279 typedef void (*StructuredCloneErrorOp
)(JSContext
* cx
, uint32_t errorid
,
280 void* closure
, const char* errorMessage
);
283 * This is called when JS_ReadStructuredClone receives a transferable object
284 * not known to the engine. If this hook does not exist or returns false, the
285 * JS engine calls the reportError op if set, otherwise it throws a
286 * DATA_CLONE_ERR DOM Exception. This method is called before any other
287 * callback and must return a non-null object in returnObject on success.
289 typedef bool (*ReadTransferStructuredCloneOp
)(
290 JSContext
* cx
, JSStructuredCloneReader
* r
, uint32_t tag
, void* content
,
291 uint64_t extraData
, void* closure
, JS::MutableHandleObject returnObject
);
294 * Called when JS_WriteStructuredClone receives a transferable object not
295 * handled by the engine. If this hook does not exist or returns false, the JS
296 * engine will call the reportError hook or fall back to throwing a
297 * DATA_CLONE_ERR DOM Exception. This method is called before any other
300 * tag: indicates what type of transferable this is. Must be greater than
301 * 0xFFFF0201 (value of the internal SCTAG_TRANSFER_MAP_PENDING_ENTRY)
303 * ownership: see TransferableOwnership, above. Used to communicate any needed
304 * ownership info to the FreeTransferStructuredCloneOp.
306 * content, extraData: what the ReadTransferStructuredCloneOp will receive
308 typedef bool (*TransferStructuredCloneOp
)(JSContext
* cx
,
309 JS::Handle
<JSObject
*> obj
,
313 JS::TransferableOwnership
* ownership
,
314 void** content
, uint64_t* extraData
);
317 * Called when freeing an unknown transferable object. Note that it
318 * should never trigger a garbage collection (and will assert in a
319 * debug build if it does.)
321 typedef void (*FreeTransferStructuredCloneOp
)(
322 uint32_t tag
, JS::TransferableOwnership ownership
, void* content
,
323 uint64_t extraData
, void* closure
);
326 * Called when the transferring objects are checked. If this function returns
327 * false, the serialization ends throwing a DataCloneError exception.
329 typedef bool (*CanTransferStructuredCloneOp
)(JSContext
* cx
,
330 JS::Handle
<JSObject
*> obj
,
331 bool* sameProcessScopeRequired
,
335 * Called when a SharedArrayBuffer (including one owned by a Wasm memory object)
336 * has been processed in context `cx` by structured cloning. If `receiving` is
337 * true then the SAB has been received from a channel and a new SAB object has
338 * been created; if false then an existing SAB has been serialized onto a
341 * If the callback returns false then the clone operation (read or write) will
344 typedef bool (*SharedArrayBufferClonedOp
)(JSContext
* cx
, bool receiving
,
347 struct JSStructuredCloneCallbacks
{
348 ReadStructuredCloneOp read
;
349 WriteStructuredCloneOp write
;
350 StructuredCloneErrorOp reportError
;
351 ReadTransferStructuredCloneOp readTransfer
;
352 TransferStructuredCloneOp writeTransfer
;
353 FreeTransferStructuredCloneOp freeTransfer
;
354 CanTransferStructuredCloneOp canTransfer
;
355 SharedArrayBufferClonedOp sabCloned
;
358 enum OwnTransferablePolicy
{
360 * The buffer owns any Transferables that it might contain, and should
361 * properly release them upon destruction.
363 OwnsTransferablesIfAny
,
366 * Do not free any Transferables within this buffer when deleting it. This
367 * is used to mark as clone buffer as containing data from another process,
368 * and so it can't legitimately contain pointers. If the buffer claims to
369 * have transferables, it's a bug or an attack. This is also used for
370 * abandon(), where a buffer still contains raw data but the ownership has
371 * been given over to some other entity.
373 IgnoreTransferablesIfAny
,
376 * A buffer that cannot contain Transferables at all. This usually means
377 * the buffer is empty (not yet filled in, or having been cleared).
383 class SharedArrayRawBuffer
;
385 class SharedArrayRawBufferRefs
{
387 SharedArrayRawBufferRefs() = default;
388 SharedArrayRawBufferRefs(SharedArrayRawBufferRefs
&& other
) = default;
389 SharedArrayRawBufferRefs
& operator=(SharedArrayRawBufferRefs
&& other
);
390 ~SharedArrayRawBufferRefs();
392 [[nodiscard
]] bool acquire(JSContext
* cx
, SharedArrayRawBuffer
* rawbuf
);
393 [[nodiscard
]] bool acquireAll(JSContext
* cx
,
394 const SharedArrayRawBufferRefs
& that
);
395 void takeOwnership(SharedArrayRawBufferRefs
&&);
399 js::Vector
<js::SharedArrayRawBuffer
*, 0, js::SystemAllocPolicy
> refs_
;
402 template <typename T
, typename AllocPolicy
>
403 struct BufferIterator
;
407 * JSStructuredCloneData represents structured clone data together with the
408 * information needed to read/write/transfer/free the records within it, in the
409 * form of a set of callbacks.
411 class MOZ_NON_MEMMOVABLE JS_PUBLIC_API JSStructuredCloneData
{
413 using BufferList
= mozilla::BufferList
<js::SystemAllocPolicy
>;
414 using Iterator
= BufferList::IterImpl
;
417 static const size_t kStandardCapacity
= 4096;
421 // The (address space, thread) scope within which this clone is valid. Note
422 // that this must be either set during construction, or start out as
423 // Unassigned and transition once to something else.
424 JS::StructuredCloneScope scope_
;
426 const JSStructuredCloneCallbacks
* callbacks_
= nullptr;
427 void* closure_
= nullptr;
428 OwnTransferablePolicy ownTransferables_
=
429 OwnTransferablePolicy::NoTransferables
;
430 js::SharedArrayRawBufferRefs refsHeld_
;
432 friend struct JSStructuredCloneWriter
;
433 friend class JS_PUBLIC_API JSAutoStructuredCloneBuffer
;
434 template <typename T
, typename AllocPolicy
>
435 friend struct js::BufferIterator
;
438 // The constructor must be infallible but SystemAllocPolicy is not, so both
439 // the initial size and initial capacity of the BufferList must be zero.
440 explicit JSStructuredCloneData(JS::StructuredCloneScope scope
)
441 : bufList_(0, 0, kStandardCapacity
, js::SystemAllocPolicy()),
445 ownTransferables_(OwnTransferablePolicy::NoTransferables
) {}
447 // Steal the raw data from a BufferList. In this case, we don't know the
448 // scope and none of the callback info is assigned yet.
449 JSStructuredCloneData(BufferList
&& buffers
, JS::StructuredCloneScope scope
)
450 : bufList_(std::move(buffers
)),
454 ownTransferables_(OwnTransferablePolicy::NoTransferables
) {}
455 MOZ_IMPLICIT
JSStructuredCloneData(BufferList
&& buffers
)
456 : JSStructuredCloneData(std::move(buffers
),
457 JS::StructuredCloneScope::Unassigned
) {}
458 JSStructuredCloneData(JSStructuredCloneData
&& other
) = default;
459 JSStructuredCloneData
& operator=(JSStructuredCloneData
&& other
) = default;
460 ~JSStructuredCloneData() { discardTransferables(); }
462 void setCallbacks(const JSStructuredCloneCallbacks
* callbacks
, void* closure
,
463 OwnTransferablePolicy policy
) {
464 callbacks_
= callbacks
;
466 ownTransferables_
= policy
;
469 [[nodiscard
]] bool Init(size_t initialCapacity
= 0) {
470 return bufList_
.Init(0, initialCapacity
);
473 JS::StructuredCloneScope
scope() const {
474 if (scope_
== JS::StructuredCloneScope::UnknownDestination
) {
475 return JS::StructuredCloneScope::DifferentProcess
;
480 void sameProcessScopeRequired() {
481 if (scope_
== JS::StructuredCloneScope::UnknownDestination
) {
482 scope_
= JS::StructuredCloneScope::SameProcess
;
486 void initScope(JS::StructuredCloneScope newScope
) {
487 MOZ_ASSERT(Size() == 0, "initScope() of nonempty JSStructuredCloneData");
488 if (scope() != JS::StructuredCloneScope::Unassigned
) {
489 MOZ_ASSERT(scope() == newScope
,
490 "Cannot change scope after it has been initialized");
495 size_t Size() const { return bufList_
.Size(); }
497 const Iterator
Start() const { return bufList_
.Iter(); }
499 [[nodiscard
]] bool Advance(Iterator
& iter
, size_t distance
) const {
500 return iter
.AdvanceAcrossSegments(bufList_
, distance
);
503 [[nodiscard
]] bool ReadBytes(Iterator
& iter
, char* buffer
,
505 return bufList_
.ReadBytes(iter
, buffer
, size
);
508 // Append new data to the end of the buffer.
509 [[nodiscard
]] bool AppendBytes(const char* data
, size_t size
) {
510 MOZ_ASSERT(scope() != JS::StructuredCloneScope::Unassigned
);
511 return bufList_
.WriteBytes(data
, size
);
514 // Update data stored within the existing buffer. There must be at least
515 // 'size' bytes between the position of 'iter' and the end of the buffer.
516 [[nodiscard
]] bool UpdateBytes(Iterator
& iter
, const char* data
,
518 MOZ_ASSERT(scope() != JS::StructuredCloneScope::Unassigned
);
520 size_t remaining
= iter
.RemainingInSegment();
521 size_t nbytes
= std::min(remaining
, size
);
522 memcpy(iter
.Data(), data
, nbytes
);
525 iter
.Advance(bufList_
, nbytes
);
530 char* AllocateBytes(size_t maxSize
, size_t* size
) {
531 return bufList_
.AllocateBytes(maxSize
, size
);
535 discardTransferables();
539 // Return a new read-only JSStructuredCloneData that "borrows" the contents
540 // of |this|. Its lifetime should not exceed the donor's. This is only
541 // allowed for DifferentProcess clones, so finalization of the borrowing
542 // clone will do nothing.
543 JSStructuredCloneData
Borrow(Iterator
& iter
, size_t size
,
544 bool* success
) const {
545 MOZ_ASSERT(scope() == JS::StructuredCloneScope::DifferentProcess
);
546 return JSStructuredCloneData(
547 bufList_
.Borrow
<js::SystemAllocPolicy
>(iter
, size
, success
), scope());
550 // Iterate over all contained data, one BufferList segment's worth at a
551 // time, and invoke the given FunctionToApply with the data pointer and
552 // size. The function should return a bool value, and this loop will exit
553 // with false if the function ever returns false.
554 template <typename FunctionToApply
>
555 bool ForEachDataChunk(FunctionToApply
&& function
) const {
556 Iterator iter
= bufList_
.Iter();
557 while (!iter
.Done()) {
558 if (!function(iter
.Data(), iter
.RemainingInSegment())) {
561 iter
.Advance(bufList_
, iter
.RemainingInSegment());
566 // Append the entire contents of other's bufList_ to our own.
567 [[nodiscard
]] bool Append(const JSStructuredCloneData
& other
) {
568 MOZ_ASSERT(scope() == other
.scope());
569 return other
.ForEachDataChunk(
570 [&](const char* data
, size_t size
) { return AppendBytes(data
, size
); });
573 size_t SizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf
) {
574 return bufList_
.SizeOfExcludingThis(mallocSizeOf
);
577 void discardTransferables();
580 // This internal method exposes the real value of scope_. It's meant to be
581 // used only when starting the writing.
582 JS::StructuredCloneScope
scopeForInternalWriting() const { return scope_
; }
586 * Implements StructuredDeserialize and StructuredDeserializeWithTransfer.
588 * Note: If `data` contains transferable objects, it can be read only once.
590 JS_PUBLIC_API
bool JS_ReadStructuredClone(
591 JSContext
* cx
, const JSStructuredCloneData
& data
, uint32_t version
,
592 JS::StructuredCloneScope scope
, JS::MutableHandleValue vp
,
593 const JS::CloneDataPolicy
& cloneDataPolicy
,
594 const JSStructuredCloneCallbacks
* optionalCallbacks
, void* closure
);
597 * Implements StructuredSerialize, StructuredSerializeForStorage, and
598 * StructuredSerializeWithTransfer.
600 * Note: If the scope is DifferentProcess then the cloneDataPolicy must deny
601 * shared-memory objects, or an error will be signaled if a shared memory object
604 JS_PUBLIC_API
bool JS_WriteStructuredClone(
605 JSContext
* cx
, JS::HandleValue v
, JSStructuredCloneData
* data
,
606 JS::StructuredCloneScope scope
, const JS::CloneDataPolicy
& cloneDataPolicy
,
607 const JSStructuredCloneCallbacks
* optionalCallbacks
, void* closure
,
608 JS::HandleValue transferable
);
610 JS_PUBLIC_API
bool JS_StructuredCloneHasTransferables(
611 JSStructuredCloneData
& data
, bool* hasTransferable
);
613 JS_PUBLIC_API
bool JS_StructuredClone(
614 JSContext
* cx
, JS::HandleValue v
, JS::MutableHandleValue vp
,
615 const JSStructuredCloneCallbacks
* optionalCallbacks
, void* closure
);
618 * The C-style API calls to read and write structured clones are fragile --
619 * they rely on the caller to properly handle ownership of the clone data, and
620 * the handling of the input data as well as the interpretation of the contents
621 * of the clone buffer are dependent on the callbacks passed in. If you
622 * serialize and deserialize with different callbacks, the results are
625 * JSAutoStructuredCloneBuffer wraps things up in an RAII class for data
626 * management, and uses the same callbacks for both writing and reading
627 * (serializing and deserializing).
629 class JS_PUBLIC_API JSAutoStructuredCloneBuffer
{
630 JSStructuredCloneData data_
;
634 JSAutoStructuredCloneBuffer(JS::StructuredCloneScope scope
,
635 const JSStructuredCloneCallbacks
* callbacks
,
637 : data_(scope
), version_(JS_STRUCTURED_CLONE_VERSION
) {
638 data_
.setCallbacks(callbacks
, closure
,
639 OwnTransferablePolicy::NoTransferables
);
642 JSAutoStructuredCloneBuffer(JSAutoStructuredCloneBuffer
&& other
);
643 JSAutoStructuredCloneBuffer
& operator=(JSAutoStructuredCloneBuffer
&& other
);
645 ~JSAutoStructuredCloneBuffer() { clear(); }
647 JSStructuredCloneData
& data() { return data_
; }
648 bool empty() const { return !data_
.Size(); }
652 JS::StructuredCloneScope
scope() const { return data_
.scope(); }
655 * Adopt some memory. It will be automatically freed by the destructor.
656 * data must have been allocated by the JS engine (e.g., extracted via
657 * JSAutoStructuredCloneBuffer::steal).
659 void adopt(JSStructuredCloneData
&& data
,
660 uint32_t version
= JS_STRUCTURED_CLONE_VERSION
,
661 const JSStructuredCloneCallbacks
* callbacks
= nullptr,
662 void* closure
= nullptr);
665 * Release the buffer and transfer ownership to the caller.
667 void steal(JSStructuredCloneData
* data
, uint32_t* versionp
= nullptr,
668 const JSStructuredCloneCallbacks
** callbacks
= nullptr,
669 void** closure
= nullptr);
672 * Abandon ownership of any transferable objects stored in the buffer,
673 * without freeing the buffer itself. Useful when copying the data out into
674 * an external container, though note that you will need to use adopt() to
675 * properly release that data eventually.
678 data_
.ownTransferables_
= OwnTransferablePolicy::IgnoreTransferablesIfAny
;
681 bool read(JSContext
* cx
, JS::MutableHandleValue vp
,
682 const JS::CloneDataPolicy
& cloneDataPolicy
= JS::CloneDataPolicy(),
683 const JSStructuredCloneCallbacks
* optionalCallbacks
= nullptr,
684 void* closure
= nullptr);
686 bool write(JSContext
* cx
, JS::HandleValue v
,
687 const JSStructuredCloneCallbacks
* optionalCallbacks
= nullptr,
688 void* closure
= nullptr);
690 bool write(JSContext
* cx
, JS::HandleValue v
, JS::HandleValue transferable
,
691 const JS::CloneDataPolicy
& cloneDataPolicy
,
692 const JSStructuredCloneCallbacks
* optionalCallbacks
= nullptr,
693 void* closure
= nullptr);
695 size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf
) {
696 return data_
.SizeOfExcludingThis(mallocSizeOf
);
699 size_t sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf
) {
700 return mallocSizeOf(this) + sizeOfExcludingThis(mallocSizeOf
);
704 // Copy and assignment are not supported.
705 JSAutoStructuredCloneBuffer(const JSAutoStructuredCloneBuffer
& other
) =
707 JSAutoStructuredCloneBuffer
& operator=(
708 const JSAutoStructuredCloneBuffer
& other
) = delete;
711 // The range of tag values the application may use for its own custom object
713 #define JS_SCTAG_USER_MIN ((uint32_t)0xFFFF8000)
714 #define JS_SCTAG_USER_MAX ((uint32_t)0xFFFFFFFF)
716 #define JS_SCERR_RECURSION 0
717 #define JS_SCERR_TRANSFERABLE 1
718 #define JS_SCERR_DUP_TRANSFERABLE 2
719 #define JS_SCERR_UNSUPPORTED_TYPE 3
720 #define JS_SCERR_SHMEM_TRANSFERABLE 4
721 #define JS_SCERR_TYPED_ARRAY_DETACHED 5
722 #define JS_SCERR_WASM_NO_TRANSFER 6
723 #define JS_SCERR_NOT_CLONABLE 7
724 #define JS_SCERR_NOT_CLONABLE_WITH_COOP_COEP 8
726 JS_PUBLIC_API
bool JS_ReadUint32Pair(JSStructuredCloneReader
* r
, uint32_t* p1
,
729 JS_PUBLIC_API
bool JS_ReadBytes(JSStructuredCloneReader
* r
, void* p
,
732 JS_PUBLIC_API
bool JS_ReadTypedArray(JSStructuredCloneReader
* r
,
733 JS::MutableHandleValue vp
);
735 JS_PUBLIC_API
bool JS_WriteUint32Pair(JSStructuredCloneWriter
* w
, uint32_t tag
,
738 JS_PUBLIC_API
bool JS_WriteBytes(JSStructuredCloneWriter
* w
, const void* p
,
741 JS_PUBLIC_API
bool JS_WriteString(JSStructuredCloneWriter
* w
,
742 JS::HandleString str
);
744 JS_PUBLIC_API
bool JS_WriteTypedArray(JSStructuredCloneWriter
* w
,
747 JS_PUBLIC_API
bool JS_ObjectNotWritten(JSStructuredCloneWriter
* w
,
748 JS::HandleObject obj
);
750 JS_PUBLIC_API
JS::StructuredCloneScope
JS_GetStructuredCloneScope(
751 JSStructuredCloneWriter
* w
);
753 #endif /* js_StructuredClone_h */