pub struct JsObject { /* private fields */ }Expand description
A JavaScript object.
The interior mutability is what makes const o = {}; o.x = 1 work: the handle
is immutable, the object it points at is not.
Implementations§
Source§impl JsObject
impl JsObject
Sourcepub fn array_buffer_storage(&self) -> Option<Rc<RefCell<BufferStorage>>>
pub fn array_buffer_storage(&self) -> Option<Rc<RefCell<BufferStorage>>>
The storage of this object, when it is an ArrayBuffer.
Sourcepub fn set_array_buffer(&self, storage: Rc<RefCell<BufferStorage>>)
pub fn set_array_buffer(&self, storage: Rc<RefCell<BufferStorage>>)
Make this object an ArrayBuffer owning storage.
Sourcepub fn collection_entries(&self) -> Option<Vec<(Value, Value)>>
pub fn collection_entries(&self) -> Option<Vec<(Value, Value)>>
The entries of this object, when it is a Map or a Set.
Sourcepub fn collection_is_set(&self) -> Option<bool>
pub fn collection_is_set(&self) -> Option<bool>
Whether this object is a Set rather than a Map.
Sourcepub fn set_collection(&self, entries: Vec<(Value, Value)>, is_set: bool)
pub fn set_collection(&self, entries: Vec<(Value, Value)>, is_set: bool)
Make this object a Map or Set holding entries.
Sourcepub fn weak_entries(&self) -> Option<Vec<(Value, Value)>>
pub fn weak_entries(&self) -> Option<Vec<(Value, Value)>>
This object’s weak entries, when it is a WeakMap or WeakSet.
Expired handles are filtered out here rather than at the call site, so every reader sees the same live view of the map and a dead key can never be observed.
Sourcepub fn weak_is_set(&self) -> Option<bool>
pub fn weak_is_set(&self) -> Option<bool>
Whether this object is a WeakSet rather than a WeakMap.
Sourcepub fn set_weak_collection(&self, entries: Vec<(WeakKey, Value)>, is_set: bool)
pub fn set_weak_collection(&self, entries: Vec<(WeakKey, Value)>, is_set: bool)
Make this object a WeakMap or WeakSet, replacing any entries it had.
Sourcepub fn weak_set(&self, key: WeakKey, value: Value)
pub fn weak_set(&self, key: WeakKey, value: Value)
Store one entry in a weak collection, replacing the value for a live equal key.
The comparison is pointer identity: a weak key has to be an object, and two objects are the same key exactly when they are the same object. Expired entries are dropped on the way through, which keeps the vector from growing without bound in a program that churns keys.
Sourcepub fn weak_delete(&self, key: &Value) -> bool
pub fn weak_delete(&self, key: &Value) -> bool
Remove key from a weak collection, answering whether it was there.
Sourcepub fn weak_ref_target(&self) -> Option<WeakKey>
pub fn weak_ref_target(&self) -> Option<WeakKey>
The target of a WeakRef, when this object is one.
Sourcepub fn set_weak_ref(&self, target: WeakKey)
pub fn set_weak_ref(&self, target: WeakKey)
Make this object a WeakRef to target.
Sourcepub fn finalization_state(&self) -> Option<Box<FinalizationState>>
pub fn finalization_state(&self) -> Option<Box<FinalizationState>>
The registry state, when this object is a FinalizationRegistry.
Sourcepub fn set_finalization_state(&self, state: FinalizationState)
pub fn set_finalization_state(&self, state: FinalizationState)
Replace a registry’s state.
Sourcepub fn typed_array_parts(
&self,
) -> Option<(Gc<JsObject>, ElementType, usize, usize)>
pub fn typed_array_parts( &self, ) -> Option<(Gc<JsObject>, ElementType, usize, usize)>
(buffer, element, offset, length), when this object is a typed-array view.
Sourcepub fn is_native_function(&self) -> bool
pub fn is_native_function(&self) -> bool
Whether this callable is a native function rather than compiled JavaScript.
construct uses it to decide who builds the instance: a native constructor creates
its own, after validating its arguments.
Sourcepub fn native_creates_own_instance(&self) -> bool
pub fn native_creates_own_instance(&self) -> bool
Whether this native constructor builds its own instance, so new must not pass one.
Sourcepub fn set_data_view(&self, buffer: Gc<JsObject>, offset: usize, length: usize)
pub fn set_data_view(&self, buffer: Gc<JsObject>, offset: usize, length: usize)
Make this object a DataView over buffer.
Sourcepub fn data_view_parts(&self) -> Option<(Gc<JsObject>, usize, usize)>
pub fn data_view_parts(&self) -> Option<(Gc<JsObject>, usize, usize)>
The (buffer, offset, length) of a DataView, or None when this is not one.
Sourcepub fn set_typed_array(
&self,
buffer: Gc<JsObject>,
element: ElementType,
offset: usize,
length: usize,
)
pub fn set_typed_array( &self, buffer: Gc<JsObject>, element: ElementType, offset: usize, length: usize, )
Make this object a typed-array view of length elements.
Sourcepub fn typed_array_element_type(&self) -> Option<ElementType>
pub fn typed_array_element_type(&self) -> Option<ElementType>
The element type, when this object is a typed-array view.
Sourcepub fn typed_array_length(&self) -> Option<usize>
pub fn typed_array_length(&self) -> Option<usize>
How many elements the view exposes, or None when this is not a view.
A detached buffer reports 0: the storage is gone, so there is nothing to index.
Sourcepub fn typed_array_element(&self, index: usize) -> Option<Value>
pub fn typed_array_element(&self, index: usize) -> Option<Value>
Read one view element.
Out-of-range and detached reads are undefined rather than an error, which is what
makes a[999] and a detached a[0] behave the way the language says.
Sourcepub fn typed_array_set(
&self,
index: usize,
value: Value,
) -> Option<Result<(), &'static str>>
pub fn typed_array_set( &self, index: usize, value: Value, ) -> Option<Result<(), &'static str>>
Store one view element, applying the type’s wrap or clamp.
Some(Ok(())) stored, Some(Err(())) refused the value — a Number into a BigInt
view, which the caller turns into a TypeError — and None when this is not a
view at all or the index is out of range, which is a silent no-op.
Source§impl JsObject
impl JsObject
Sourcepub fn ordinary(heap: &Heap, proto: Option<Gc<JsObject>>) -> Gc<JsObject>
pub fn ordinary(heap: &Heap, proto: Option<Gc<JsObject>>) -> Gc<JsObject>
An ordinary object with the given prototype.
Sourcepub fn array(
heap: &Heap,
proto: Option<Gc<JsObject>>,
capacity: usize,
) -> Gc<JsObject>
pub fn array( heap: &Heap, proto: Option<Gc<JsObject>>, capacity: usize, ) -> Gc<JsObject>
An array with the given prototype and initial capacity.
An empty array with room for capacity elements.
The length starts at 0: capacity is a Vec hint, not a length. Callers that want
holes use Self::array_set_length, which no longer allocates them.
Sourcepub fn function(
heap: &Heap,
proto: Option<Gc<JsObject>>,
callable: Callable,
) -> Gc<JsObject>
pub fn function( heap: &Heap, proto: Option<Gc<JsObject>>, callable: Callable, ) -> Gc<JsObject>
A callable object.
Sourcepub fn with<R>(&self, f: impl FnOnce(&ObjectData) -> R) -> R
pub fn with<R>(&self, f: impl FnOnce(&ObjectData) -> R) -> R
Run f with the object’s data borrowed.
Sourcepub fn with_mut<R>(&self, f: impl FnOnce(&mut ObjectData) -> R) -> R
pub fn with_mut<R>(&self, f: impl FnOnce(&mut ObjectData) -> R) -> R
Run f with the object’s data mutably borrowed.
Sourcepub fn is_callable(&self) -> bool
pub fn is_callable(&self) -> bool
Whether this object can be called.
Covers every Callable variant, Bound included: typeof bound === "function" and bound() works, so from a script’s point of view a bound
function is a function.
Sourcepub fn is_constructor(&self) -> bool
pub fn is_constructor(&self) -> bool
Whether this object has a [[Construct]], i.e. may be the target of new or
Reflect.construct.
Sourcepub fn set_constructible(&self, yes: bool)
pub fn set_constructible(&self, yes: bool)
Give this function a [[Construct]].
Only for callables that are constructors by definition: ordinary functions, class
constructors, and the built-in constructors. Everything else — methods, arrows,
generators, async functions — keeps the default false.
Sourcepub fn proxy_state(&self) -> Option<ProxyRef>
pub fn proxy_state(&self) -> Option<ProxyRef>
This object’s proxy slots, when it is one (revoked or not).
Sourcepub fn proxy(heap: &Heap, target: Gc<JsObject>, handler: Value) -> Gc<JsObject>
pub fn proxy(heap: &Heap, target: Gc<JsObject>, handler: Value) -> Gc<JsObject>
A proxy over target reading its traps from handler.
The prototype stays empty on purpose: [[GetPrototypeOf]] on a proxy forwards to
the target through the getPrototypeOf trap, so copying the target’s prototype
here would be a second, disagreeing source of truth.
Sourcepub fn home_object(&self) -> Option<Gc<JsObject>>
pub fn home_object(&self) -> Option<Gc<JsObject>>
The object a method was defined on, for super to resolve against.
Sourcepub fn set_home_object(&self, home: Option<Gc<JsObject>>)
pub fn set_home_object(&self, home: Option<Gc<JsObject>>)
Record the object a method was defined on.
A method has no [[Construct]] (§10.2.1): new (o.m) is a TypeError even
though o.m is a function. Acquiring a home object is exactly the moment a
function becomes a method, so the flag is cleared here rather than left to every
method-defining call site to remember.
Sourcepub fn is_class_constructor(&self) -> bool
pub fn is_class_constructor(&self) -> bool
Whether this is a class constructor.
Sourcepub fn set_class_constructor(&self, yes: bool)
pub fn set_class_constructor(&self, yes: bool)
Mark this function as a class constructor.
Sourcepub fn array_elements(&self) -> Option<Vec<Value>>
pub fn array_elements(&self) -> Option<Vec<Value>>
The elements of an array object, if this is one.
Sourcepub fn property_entries(&self) -> Vec<(PropertyKey, Property)>
pub fn property_entries(&self) -> Vec<(PropertyKey, Property)>
Every own key with its descriptor, array indices included.
JsObject::own_property_keys answers the same question without the
descriptors; a proxy invariant needs both at once, because it has to check the
value of a key it is told about and the presence of a key it is not.
Sourcepub fn regexp(&self) -> Option<RegExpData>
pub fn regexp(&self) -> Option<RegExpData>
length for an array, reading it out of the element vector.
The compiled pattern, when this object is a RegExp.
Sourcepub fn disposable_stack(&self) -> Option<StackState>
pub fn disposable_stack(&self) -> Option<StackState>
The held resources, when this object is a DisposableStack.
Sourcepub fn set_disposable_stack(&self, state: StackState)
pub fn set_disposable_stack(&self, state: StackState)
Make this object a DisposableStack holding state.
Sourcepub fn set_regexp(&self, data: RegExpData)
pub fn set_regexp(&self, data: RegExpData)
Make this object a RegExp carrying data.
Sourcepub fn array_length(&self) -> Option<usize>
pub fn array_length(&self) -> Option<usize>
An array’s element count, when this object is an array.
Sourcepub fn array_push(&self, value: Value) -> usize
pub fn array_push(&self, value: Value) -> usize
Append to an array and return the new length.
Sourcepub fn array_set(&self, index: usize, value: Value) -> bool
pub fn array_set(&self, index: usize, value: Value) -> bool
Store into an array, and return whether the write happened.
The gap an out-of-range store leaves behind is filled with Value::Hole, not
undefined: a = [1]; a[3] = 4 has holes at 1 and 2, which forEach skips
and the debug printer reports. A store of undefined is a real element.
Sourcepub fn array_store(&self, index: usize, value: Value) -> bool
pub fn array_store(&self, index: usize, value: Value) -> bool
Store an element, honouring its attributes, reporting whether it happened.
Array methods that write back (sort, reverse, splice, …) use this rather
than Self::array_set: the spec has them go through [[Set]], so a frozen
element makes them throw instead of being quietly overwritten.
Sourcepub fn array_push_hole(&self) -> bool
pub fn array_push_hole(&self) -> bool
Store value as a plain element without creating holes, used by push.
Sourcepub fn array_set_length(&self, new_length: usize) -> bool
pub fn array_set_length(&self, new_length: usize) -> bool
Set the length of an array, truncating or extending with holes.
a.length = 5 on a two-element array leaves three holes, so the fill value is
Value::Hole rather than undefined.
Growing only records the new length: the extra slots are holes, and a hole needs
no storage. That is what makes a = []; a.length = 4294967295 an O(1) operation
instead of a 62 GB allocation.
Sourcepub fn array_truncate(&self, new_length: usize) -> bool
pub fn array_truncate(&self, new_length: usize) -> bool
Truncate an array to length elements without extending it.
This is what arr.length = n does for n smaller than the current length;
it is separate from Self::array_set_length because growing an array by
assignment produces holes, which are indistinguishable from undefined
here.
Sourcepub fn own_property_key(&self, key: &PropertyKey) -> Option<Property>
pub fn own_property_key(&self, key: &PropertyKey) -> Option<Property>
The own data property key, if there is one.
Sourcepub fn own_property(&self, name: &str) -> Option<Property>
pub fn own_property(&self, name: &str) -> Option<Property>
The own data property name, if there is one.
Sourcepub fn has_own_key(&self, key: &PropertyKey) -> bool
pub fn has_own_key(&self, key: &PropertyKey) -> bool
Whether the object has an own property key.
Sourcepub fn define_key(&self, key: &PropertyKey, property: Property)
pub fn define_key(&self, key: &PropertyKey, property: Property)
Define or replace an own property, keeping the original position when the key already exists.
Sourcepub fn define(&self, name: &str, property: Property)
pub fn define(&self, name: &str, property: Property)
Define or replace an own property under a string key.
Sourcepub fn define_data(&self, name: &str, value: Value)
pub fn define_data(&self, name: &str, value: Value)
Define an own data property, with the default attributes.
This creates: on an existing property it overwrites the attributes too, so
it is the wrong call for a plain assignment. Use Self::write_value for that.
Sourcepub fn define_data_key(&self, key: &PropertyKey, value: Value)
pub fn define_data_key(&self, key: &PropertyKey, value: Value)
Define an own data property under key.
Sourcepub fn write_value(&self, name: &str, value: Value) -> bool
pub fn write_value(&self, name: &str, value: Value) -> bool
Store a value into an existing own data property, keeping its attributes.
Returns false when the property is absent, is an accessor, or is not
writable. An assignment has to go through this rather than
Self::define_data, because redefining is how a non-configurable global
silently became configurable again and started answering delete with true.
Sourcepub fn write_value_key(&self, key: &PropertyKey, value: Value) -> bool
pub fn write_value_key(&self, key: &PropertyKey, value: Value) -> bool
Store a value into an existing own data property under key.
Sourcepub fn delete(&self, name: &str) -> bool
pub fn delete(&self, name: &str) -> bool
Delete an own property, reporting whether it was there and removable.
Sourcepub fn delete_key(&self, key: &PropertyKey) -> bool
pub fn delete_key(&self, key: &PropertyKey) -> bool
Delete an own property under key.
Sourcepub fn is_extensible(&self) -> bool
pub fn is_extensible(&self) -> bool
Whether new properties may be added.
Sourcepub fn set_extensible(&self, extensible: bool)
pub fn set_extensible(&self, extensible: bool)
Make the object non-extensible.
Sourcepub fn elements_writable(&self) -> bool
pub fn elements_writable(&self) -> bool
Whether array elements may be replaced.
Sourcepub fn length_writable(&self) -> bool
pub fn length_writable(&self) -> bool
Whether an array’s length may be changed.
Sourcepub fn set_length_writable(&self, writable: bool)
pub fn set_length_writable(&self, writable: bool)
Record whether length may be changed.
Sourcepub fn set_elements_writable(&self, writable: bool)
pub fn set_elements_writable(&self, writable: bool)
Make array elements read-only, as Object.freeze does.
Sourcepub fn element_attributes(&self, index: u32) -> Option<Property>
pub fn element_attributes(&self, index: u32) -> Option<Property>
The attributes recorded for an array index, if defineProperty set any.
Sourcepub fn element_is_undeletable(&self, index: u32) -> bool
pub fn element_is_undeletable(&self, index: u32) -> bool
Whether delete at index would be rejected.
Separate from JsObject::element_is_locked: a store needs the element to be
non-writable, but a delete — which is what truncating an array does — only needs
it to be non-configurable. Object.defineProperty(a, "3", {configurable: false})
leaves a[3] writable and yet undeletable, so a.length = 2 must fail.
Sourcepub fn element_is_locked(&self, index: u32) -> bool
pub fn element_is_locked(&self, index: u32) -> bool
Whether a store at index would be rejected.
An element is locked either by Object.freeze on the whole object or by a
non-configurable, non-writable descriptor recorded for that one index.
Sourcepub fn own_keys(&self) -> Vec<Wtf8>
pub fn own_keys(&self) -> Vec<Wtf8>
The own property names, in definition order. Symbol keys are excluded:
Object.keys and for…in cannot see them.
Sourcepub fn own_enumerable_keys(&self) -> Vec<Wtf8>
pub fn own_enumerable_keys(&self) -> Vec<Wtf8>
The own enumerable property names, in definition order.
Sourcepub fn own_symbol_keys(&self) -> Vec<Gc<JsSymbol>>
pub fn own_symbol_keys(&self) -> Vec<Gc<JsSymbol>>
The own symbol keys, in definition order, for Object.getOwnPropertySymbols.
Sourcepub fn own_property_keys(&self) -> Vec<PropertyKey>
pub fn own_property_keys(&self) -> Vec<PropertyKey>
Every own key, strings first then symbols — the order Reflect.ownKeys and
Object.getOwnPropertyDescriptors report, and the order JSON.stringify
walks before it skips the symbols.
Sourcepub fn own_property_keys_in_definition_order(&self) -> Vec<PropertyKey>
pub fn own_property_keys_in_definition_order(&self) -> Vec<PropertyKey>
Own property keys in definition order, with symbols interleaved.
JsObject::own_property_keys answers in the spec’s order — integer-like keys
ascending, then strings, then symbols — because that is what Reflect.ownKeys and
for...in must do. A dumper has the opposite requirement: quickjs’s
JS_PrintValue walks the shape, so {[Symbol("t")]: 1, y: 2} prints the symbol
first. That is not observable from a script except through such a dump, which is
why this is a separate accessor rather than a change to own_property_keys.