Skip to main content

JsObject

Struct JsObject 

Source
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

Source

pub fn ordinary(heap: &Heap, proto: Option<Gc<JsObject>>) -> Gc<JsObject>

An ordinary object with the given prototype.

Source

pub fn array( heap: &Heap, proto: Option<Gc<JsObject>>, capacity: usize, ) -> Gc<JsObject>

An array with the given prototype and initial capacity.

Source

pub fn function( heap: &Heap, proto: Option<Gc<JsObject>>, callable: Callable, ) -> Gc<JsObject>

A callable object.

Source

pub fn with<R>(&self, f: impl FnOnce(&ObjectData) -> R) -> R

Run f with the object’s data borrowed.

Source

pub fn with_mut<R>(&self, f: impl FnOnce(&mut ObjectData) -> R) -> R

Run f with the object’s data mutably borrowed.

Source

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.

Source

pub fn proto(&self) -> Option<Gc<JsObject>>

The prototype.

Source

pub fn set_proto(&self, proto: Option<Gc<JsObject>>)

Replace the prototype.

Source

pub fn array_elements(&self) -> Option<Vec<Value>>

The elements of an array object, if this is one.

Source

pub fn is_array(&self) -> bool

Whether this object has array elements at all.

Source

pub fn array_length(&self) -> Option<usize>

length for an array, reading it out of the element vector.

Source

pub fn array_get(&self, index: usize) -> Option<Value>

Read one array element.

Source

pub fn array_push(&self, value: Value) -> usize

Append to an array and return the new length.

Source

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.

Source

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.

Source

pub fn array_push_hole(&self) -> bool

Store value as a plain element without creating holes, used by push.

Source

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.

Source

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.

Source

pub fn own_property(&self, name: &str) -> Option<Property>

The own data property name, if there is one.

Source

pub fn has_own(&self, name: &str) -> bool

Whether the object has an own property name.

Source

pub fn define(&self, name: &str, property: Property)

Define or replace an own property, keeping the original position when the name already exists.

Source

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.

Source

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.

Source

pub fn delete(&self, name: &str) -> bool

Delete an own property, reporting whether it was there and removable.

Source

pub fn is_extensible(&self) -> bool

Whether new properties may be added.

Source

pub fn set_extensible(&self, extensible: bool)

Make the object non-extensible.

Source

pub fn elements_writable(&self) -> bool

Whether array elements may be replaced.

Source

pub fn set_elements_writable(&self, writable: bool)

Make array elements read-only, as Object.freeze does.

Source

pub fn element_attributes(&self, index: u32) -> Option<Property>

The attributes recorded for an array index, if defineProperty set any.

Source

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.

Source

pub fn own_keys(&self) -> Vec<String>

The own property names, in definition order.

Source

pub fn own_enumerable_keys(&self) -> Vec<String>

The own enumerable property names, in definition order.

Source

pub fn chain(&self) -> Vec<Gc<JsObject>>

The prototype chain, starting with the prototypes of this object.

Trait Implementations§

Source§

impl Trace for JsObject

Source§

fn trace(&self, tracer: &mut Tracer<'_>)

Report every Gc owned by self to tracer.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.