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 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.
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 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 array_length(&self) -> Option<usize>
pub fn array_length(&self) -> Option<usize>
length for an array, reading it out of the element vector.
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, length: usize) -> bool
pub fn array_set_length(&self, 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.
Sourcepub fn array_truncate(&self, length: usize) -> bool
pub fn array_truncate(&self, 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(&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 define(&self, name: &str, property: Property)
pub fn define(&self, name: &str, property: Property)
Define or replace an own property, keeping the original position when the name already exists.
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 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 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 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 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_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_enumerable_keys(&self) -> Vec<String>
pub fn own_enumerable_keys(&self) -> Vec<String>
The own enumerable property names, in definition order.