The C-style API calls to read and write structured clones are fragile –
they rely on the caller to properly handle ownership of the clone data, and
the handling of the input data as well as the interpretation of the contents
of the clone buffer are dependent on the callbacks passed in. If you
serialize and deserialize with different callbacks, the results are
questionable.
Wrapper to relace JSNative for JSPropertySpecs and JSFunctionSpecs. This will
allow us to pass one JSJitInfo per function with the property/function spec,
without additional field overhead.
Description of a property. JS_DefineProperties and JS_InitClass take arrays
of these and define many properties at once. JS_PSG, JS_PSGS and JS_PS_END
are helper macros for defining such arrays.
JSStructuredCloneData represents structured clone data together with the
information needed to read/write/transfer/free the records within it, in the
form of a set of callbacks.
already_AddRefed cooperates with reference counting smart pointers to enable
you to assign in a pointer without |AddRef|ing it. You might want to use
this as a return type from a function that returns an already |AddRef|ed
pointer. Or, you might want to use this as a parameter type in a function
that wants to force a transfer-of-ownership from a RefPtr in the caller (for
example, if the function expects callers to pass in a newly-created object,
which the function then takes ownership of).
An enum that describes what this getter/setter/method aliases. This
determines what things can be hoisted past this call, and if this
call is movable what it can be hoisted past.
A JS context always has an “owner thread”. The owner thread is set when the
context is created (to the current thread) and practically all entry points
into the JS engine check that a context (or anything contained in the
context: runtime, compartment, object, etc) is only touched by its owner
thread. Embeddings may check this invariant outside the JS engine by calling
JS_AbortIfWrongThread (which will abort if not on the owner thread, even for
non-debug builds).
Copies all own properties and private fields from |obj| to |target|. Both
|obj| and |target| must not be cross-compartment wrappers because we have to
enter their realms.
For JS_DecodeBytes, set *dstlenp to the size of the destination buffer before
the call; on return, *dstlenp contains the number of characters actually
stored. To determine the necessary destination buffer size, make a sizing
call that passes nullptr for dst.
Freeze obj, and all objects it refers to, recursively. This will not recurse
through non-extensible objects, on the assumption that those are already
deep-frozen.
Encode string into a buffer. The function does not stores an additional
zero byte. The function returns (size_t) -1 if the string can not be
encoded into bytes with no error reported. Otherwise it returns the number
of bytes that are necessary to encode the string. If that exceeds the
length parameter, the string will be cut and only length bytes will be
written into the buffer.
If the given object is an exception object, the exception will have (or be
able to lazily create) an error report struct, and this function will return
the address of that struct. Otherwise, it returns nullptr. The lifetime
of the error report struct that might be returned is the same as the
lifetime of the exception object.
As above, but providing an explicit scope chain. envChain must not include
the global object on it; that’s implicit. It needs to contain the other
objects that should end up on the script’s scope chain.
Return the ArrayBuffer or SharedArrayBuffer underlying an ArrayBufferView.
This may return a detached buffer. |obj| must be an object that would
return true for JS_IsArrayBufferViewObject().
Return a “fixed” pointer (one that will not move during a GC) to the
ArrayBufferView’s data. Note that this will not keep the object alive; the
holding object should be rooted or traced. If the view is storing the data
inline, this will copy the data to the provided buffer, returning nullptr if
bufSize is inadequate.
Return the ‘callbacks’ arg passed to JS_NewExternalStringLatin1,
JS_NewExternalUCString, JS_NewMaybeExternalStringLatin1,
or JS_NewMaybeExternalUCString.
If |obj| (underneath any functionally-transparent wrapper proxies) has as
its [[GetPrototypeOf]] trap the ordinary [[GetPrototypeOf]] behavior defined
for ordinary objects, set |*isOrdinary = true| and store |obj|’s prototype
in |result|. Otherwise set |*isOrdinary = false|. In case of error, both
outparams have unspecified value.
Get number of bytes in the string encoding (without accounting for a
terminating zero bytes. The function returns (size_t) -1 if the string
can not be encoded into bytes and reports an error using cx accordingly.
Spidermonkey does not have a good way of keeping track of what compartments
should be marked on their own. We can mark the roots unconditionally, but
marking GC things only relevant in live compartments is hard. To mitigate
this, we create a static trace hook, installed on each global object, from
which we can be sure the compartment is relevant, and mark it.
Copy the own properties of src to dst in a fast way. src and dst must both
be native and must be in the compartment of cx. They must have the same
class, the same parent, and the same prototype. Class reserved slots will
NOT be copied.
Check whether obj supports JS_GetArrayBufferView* APIs. Note that this may
return false if a security wrapper is encountered that denies the
unwrapping. If this test or one of the more specific tests succeeds, then it
is safe to call the various ArrayBufferView accessor JSAPI calls defined
below.
Determine whether obj is extensible. Extensible objects can have new
properties defined on them. Inextensible objects can’t, and their
[[Prototype]] slot is fixed as well.
Test whether the given chars + length are a valid ECMAScript identifier.
This version is infallible, so just returns whether the chars are an
identifier.
Check whether obj supports JS_GetTypedArray* APIs. Note that this may return
false if a security wrapper is encountered that denies the unwrapping. If
this test or one of the JS_Is*Array tests succeeds, then it is safe to call
the various accessor JSAPI calls defined below.
This function calls |compartmentCallback| on every compartment until either
all compartments have been iterated or CompartmentIterResult::Stop is
returned. Beware that there is no guarantee that the compartment will survive
after the callback returns. Also, barriers are disabled via the TraceSession.
This function calls |compartmentCallback| on every compartment in the given
zone until either all compartments have been iterated or
CompartmentIterResult::Stop is returned. Beware that there is no guarantee
that the compartment will survive after the callback returns. Also, barriers
are disabled via the TraceSession.
Mark a jsid after entering a new compartment. Different zones separately
mark the ids in a runtime, and this must be used any time an id is obtained
from one compartment and then used in another compartment, unless the two
compartments are guaranteed to be in the same zone.
If the bufSize passed to JS_GetArrayBufferViewFixedData is at least this
many bytes, then any copied data is guaranteed to fit into the provided
buffer.
Create a new DataView using the given buffer for storage. The given buffer
must be an ArrayBuffer or SharedArrayBuffer (or a cross-compartment wrapper
of either type), and the offset and length must fit within the bounds of the
buffer. Currently, nullptr will be returned and an exception will be thrown
if these conditions do not hold, but do not depend on that behavior.
Create a dependent string, i.e., a string that owns no character storage,
but that refers to a slice of another string’s chars. Dependent strings
are mutable by definition, so the thread safety comments above apply.
Fill “properties” with a list of standard class names that have not yet been
resolved on “obj”. This can be used as (part of) a newEnumerate class hook
on a global. Already-resolved things are excluded because they might have
been deleted by script after being resolved and enumeration considers
already-defined properties anyway.
Fill “properties” with a list of standard class names. This can be used for
proxies that want to define behavior that looks like enumerating a global
without touching the global itself.
Create a new JSString whose chars member may refer to external memory.
If a new external string is allocated, |*allocatedExternal| is set to true.
Otherwise the returned string is either not an external string or an
external string allocated by a previous call and |*allocatedExternal| is set
to false. If |*allocatedExternal| is false, |fin| won’t be called.
A constructor can request that the JS engine create a default new ‘this’
object of the given class, using the callee to determine parentage and
[[Prototype]].
Allocate an object in exactly the same way as JS_NewObjectWithGivenProto, but
without invoking the metadata callback on it. This allows creation of
internal bookkeeping objects that are guaranteed to not have metadata
attached to them.
Infallible predicate to test whether obj is a function object (faster than
comparing obj’s class name to “Function”, but equivalent unless someone has
overwritten the “Function” identifier with a different constructor and then
created instances using that constructor that might be passed in as obj).
Performs the JSON.parse operation as specified by ECMAScript, using the
given |reviver| argument as the corresponding optional argument to that
function.
Performs the JSON.parse operation as specified by ECMAScript, using the
given |reviver| argument as the corresponding optional argument to that
function.
Resolve id, which must contain either a string or an int, to a standard
class name in obj if possible, defining the class’s constructor and/or
prototype and storing true in *resolved. If id does not name a standard
class or a top-level property induced by initializing a standard class,
store false in *resolved and just return true. Return false on error,
as usual for bool result-typed API entry points.
This function can be used to track memory used by ICU. If it is called, it
must be called before JS_Init. Don’t use it unless you know what you’re
doing!
Attempt to make the [[Prototype]] of |obj| immutable, such that any attempt
to modify it will fail. If an error occurs during the attempt, return false
(with a pending exception set, depending upon the nature of the error). If
no error occurs, return true with |*succeeded| set to indicate whether the
attempt successfully made the [[Prototype]] immutable.
Performs the JSON.stringify operation, as specified by ECMAScript, except
writing stringified data by exactly one call of |callback|, passing |data| as
argument.
Given a buffer, return false if the buffer might become a valid JavaScript
script with the addition of more lines, or true if the validity of such a
script is conclusively known (because it’s the prefix of a valid script –
and possibly the entirety of such a script).
Same as above, but for buffers that will be used with the BYOB
(Bring Your Own Buffer) JSString creation functions, such as
JS_NewLatin1String and JS_NewUCString
Called when freeing a transferable handled by the embedding. Note that it
should never trigger a garbage collection (and will assert in a
debug build if it does.)
UniquePtr is a smart pointer that wholly owns a resource. Ownership may be
transferred out of a UniquePtr through explicit action, but otherwise the
resource is destroyed when the UniquePtr is destroyed.
Finalize obj, which the garbage collector has determined to be unreachable
from other live objects or from GC roots. Obviously, finalizers must never
store a reference to obj.
The type of ObjectOps::funToString. This callback allows an object to
provide a custom string to use when Function.prototype.toString is invoked on
that object. A null return value means OOM.
A class with a resolve hook can optionally have a mayResolve hook. This hook
must have no side effects and must return true for a given id if the resolve
hook may resolve this id. This is useful when we’re doing a “pure” lookup: if
mayResolve returns false, we know we don’t have to call the effectful resolve
hook.
The type of ObjectOps::enumerate. This callback overrides a portion of
SpiderMonkey’s default [[Enumerate]] internal method. When an ordinary object
is enumerated, that object and each object on its prototype chain is tested
for an enumerate op, and those ops are called in order. The properties each
op adds to the ‘properties’ vector are added to the set of values the for-in
loop will iterate over. All of this is nonstandard.
Callback used by the wrap hook to ask the embedding to prepare an object
for wrapping in a context. This might include unwrapping other wrappers
or even finding a more suitable object for the new compartment. If |origObj|
is non-null, then it is the original object we are going to swap into during
a transplant.
Resolve a lazy property named by id in obj by defining it directly in obj.
Lazy properties are those reflected from some peer native property space
(e.g., the DOM attributes for a given node reflected as obj) on demand.
Function type for trace operation of the class called to enumerate all
traceable things reachable from obj’s private data structure. For each such
thing, a trace implementation must call JS::TraceEdge on the thing’s
location.
Callback used to ask the embedding for the cross compartment wrapper handler
that implements the desired prolicy for this kind of object in the
destination compartment. |obj| is the object to be wrapped. If |existing| is
non-nullptr, it will point to an existing wrapper object that should be
re-used if possible. |existing| is guaranteed to be a cross-compartment
wrapper with a lazily-defined prototype and the correct global. It is
guaranteed not to wrap a function.
This is called when JS_ReadStructuredClone receives a transferable object
not known to the engine. If this hook does not exist or returns false, the
JS engine calls the reportError op if set, otherwise it throws a
DATA_CLONE_ERR DOM Exception. This method is called before any other
callback and must return a non-null object in returnObject on success.
Called when a SharedArrayBuffer (including one owned by a Wasm memory object)
has been processed in context cx by structured cloning. If receiving is
true then the SAB has been received from a channel and a new SAB object has
been created; if false then an existing SAB has been serialized onto a
channel.
This is called when serialization or deserialization encounters an error.
To follow HTML5, the application must throw a DATA_CLONE_ERR DOMException
with error set to one of the JS_SCERR_* values.
Called when JS_WriteStructuredClone receives a transferable object not
handled by the engine. If this hook does not exist or returns false, the JS
engine will call the reportError hook or fall back to throwing a
DATA_CLONE_ERR DOM Exception. This method is called before any other
callback.
Structured data serialization hook. The engine can write primitive values,
Objects, Arrays, Dates, RegExps, TypedArrays, ArrayBuffers, Sets, Maps,
and SharedTypedArrays. Any other type of object requires application support.
This callback must first use the JS_WriteUint32Pair API to write an object
header, passing a value greater than JS_SCTAG_USER to the tag parameter.
Then it can use the JS_Write* APIs to write any other relevant parts of
the value v to the writer w. closure is any value passed to the
JS_WriteStructuredClone function.