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 array_buffer_storage(&self) -> Option<Rc<RefCell<BufferStorage>>>

The storage of this object, when it is an ArrayBuffer.

Source

pub fn set_array_buffer(&self, storage: Rc<RefCell<BufferStorage>>)

Make this object an ArrayBuffer owning storage.

Source

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

The entries of this object, when it is a Map or a Set.

Source

pub fn collection_is_set(&self) -> Option<bool>

Whether this object is a Set rather than a Map.

Source

pub fn set_collection( &self, heap: &Heap, entries: Vec<(Value, Value)>, is_set: bool, )

Make this object a Map or Set holding entries.

Source

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.

Source

pub fn weak_is_set(&self) -> Option<bool>

Whether this object is a WeakSet rather than a WeakMap.

Source

pub fn set_weak_collection( &self, heap: &Heap, entries: Vec<(WeakKey, Value)>, is_set: bool, )

Make this object a WeakMap or WeakSet, replacing any entries it had.

Source

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.

Source

pub fn weak_delete(&self, key: &Value) -> bool

Remove key from a weak collection, answering whether it was there.

Source

pub fn weak_ref_target(&self) -> Option<WeakKey>

The target of a WeakRef, when this object is one.

Source

pub fn set_weak_ref(&self, target: WeakKey)

Make this object a WeakRef to target.

Source

pub fn finalization_state(&self) -> Option<Box<FinalizationState>>

The registry state, when this object is a FinalizationRegistry.

Source

pub fn set_finalization_state(&self, state: FinalizationState)

Replace a registry’s state.

Source

pub fn typed_array_parts( &self, ) -> Option<(Gc<JsObject>, ElementType, usize, usize)>

(buffer, element, offset, length), when this object is a typed-array view.

Source

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.

Source

pub fn native_creates_own_instance(&self) -> bool

Whether this native constructor builds its own instance, so new must not pass one.

Source

pub fn set_data_view(&self, buffer: Gc<JsObject>, offset: usize, length: usize)

Make this object a DataView over buffer.

Source

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.

Source

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.

Source

pub fn typed_array_element_type(&self) -> Option<ElementType>

The element type, when this object is a typed-array view.

Source

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.

Source

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.

Source

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

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. 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.

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 is_constructor(&self) -> bool

Whether this object has a [[Construct]], i.e. may be the target of new or Reflect.construct.

Source

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.

Source

pub fn proxy_state(&self) -> Option<ProxyRef>

This object’s proxy slots, when it is one (revoked or not).

Source

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.

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 home_object(&self) -> Option<Gc<JsObject>>

The object a method was defined on, for super to resolve against.

Source

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.

Source

pub fn private_brand(&self) -> Option<Gc<JsSymbol>>

The brand symbol this object owns as a class home object, if it is one.

Source

pub fn set_private_brand(&self, symbol: Gc<JsSymbol>)

Record symbol as the brand this object owns.

Source

pub fn carries_brand(&self, symbol: &Gc<JsSymbol>) -> bool

Whether this object carries symbol among its brands.

This is the question check_brand asks of the receiver, and the one add_brand asks before adding — a second add_brand with the same symbol is the reference’s “private method is already present”.

Source

pub fn add_brand(&self, symbol: Gc<JsSymbol>)

Give this object the brand symbol.

Source

pub fn is_class_constructor(&self) -> bool

Whether this is a class constructor.

Source

pub fn set_class_constructor(&self, yes: bool)

Mark this function as a class constructor.

Source

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

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

Source

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.

Source

pub fn is_array(&self) -> bool

Whether this object has array elements at all.

Source

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.

Source

pub fn disposable_stack(&self) -> Option<StackState>

The held resources, when this object is a DisposableStack.

Source

pub fn set_disposable_stack(&self, state: StackState)

Make this object a DisposableStack holding state.

Source

pub fn set_regexp(&self, data: RegExpData)

Make this object a RegExp carrying data.

Source

pub fn is_regexp(&self) -> bool

Whether this object is a RegExp.

Source

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

An array’s element count, when this object is an array.

Source

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

Read one array element.

Source

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

Append to an array and return the new length.

Source

pub fn array_set(&self, heap: &Heap, 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, heap: &Heap, 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, heap: &Heap) -> bool

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

Source

pub fn array_set_length(&self, heap: &Heap, 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.

Source

pub fn array_truncate(&self, heap: &Heap, 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.

Source

pub fn own_property_key(&self, key: &PropertyKey) -> Option<Property>

The own data property key, if there is one.

Source

pub fn address(&self) -> usize

The address of this object, for an inline cache’s hash.

Only ever hashed, never dereferenced — and a hash collision is caught by the re-validation, never trusted. Compare a cached handle instead when identity matters.

Source

pub fn cached_data_property( &self, index: usize, key: &PropertyKey, ) -> Option<Value>

The data property at index, if the table still has key there as a plain value.

This is the read half of an inline cache: the caller remembers where a property was found on a particular object, and this re-checks the two things that could have changed — the entry moving (the table is a Vec, so a delete shifts it) and the property becoming an accessor. Re-checking is what makes the cache safe without shape/hidden-class machinery: there is no pointer arithmetic on a remembered index, only a bounds-checked lookup followed by an equality test.

Source

pub fn own_property_index(&self, key: &PropertyKey) -> Option<(usize, Value)>

Where key sits in this object’s own property table, for caching.

Source

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

The own data property name, if there is one.

Source

pub fn has_own_key(&self, key: &PropertyKey) -> bool

Whether the object has an own property key.

Source

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

Whether the object has an own property name.

Source

pub fn define_key(&self, key: &PropertyKey, property: Property)

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

Source

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

Define or replace an own property under a string key.

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 define_data_key(&self, key: &PropertyKey, value: Value)

Define an own data property under key.

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 write_value_key(&self, key: &PropertyKey, value: Value) -> bool

Store a value into an existing own data property under key.

Source

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

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

Source

pub fn delete_key(&self, key: &PropertyKey) -> bool

Delete an own property under key.

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 length_writable(&self) -> bool

Whether an array’s length may be changed.

Source

pub fn set_length_writable(&self, writable: bool)

Record whether length may be changed.

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_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.

Source

pub fn first_undeletable_at_or_above(&self, from: usize) -> Option<usize>

The lowest index at or above from whose element cannot be deleted.

Callers use this to refuse a shrinking length (§10.4.2.4 deletes every index at or above the new length, and a non-configurable one makes that impossible). Scanning from..length instead is what a sparse array turns into an hour-long loop: after x[2**32 - 2] = 1, x.length = 2 would ask element_is_undeletable four billion times. Only two things can be undeletable — a frozen object (every element) and an index with a recorded descriptor (a handful) — so this looks at exactly those.

Source

pub fn last_undeletable_at_or_above( &self, from: usize, below: usize, ) -> Option<usize>

The highest index in from..below whose element cannot be deleted.

The spec’s ArraySetLength walks down from the current length and stops at the first element it cannot delete, so the answer it needs is the last one.

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<Wtf8>

The own property names, in definition order. Symbol keys are excluded: Object.keys and for…in cannot see them.

Source

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

The own enumerable property names, in definition order.

Source

pub fn own_symbol_keys(&self) -> Vec<Gc<JsSymbol>>

The own symbol keys, in definition order, for Object.getOwnPropertySymbols.

Source

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.

Source

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.

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 external_bytes(&self) -> usize

Everything this object holds that is not its own box: element slots, the property table, Map/Set entries, an ArrayBuffer’s bytes.

Counted by capacity, not length, because that is what the allocator actually holds — a 1000-element array with a 1024-slot vector costs 1024 slots, and reporting 1000 would make the limit wrong by whatever the vectors over-allocate (up to 2×).

Growth in place is reported to the heap as it happens (see JsObject::array_push and friends) and this same number is subtracted when the object is freed, which is why the two have to agree.

try_borrow because this can be called from inside the collector, where a mutation elsewhere in the engine may hold the borrow; a container that is being mutated at this instant is a container we cannot measure, and answering 0 for it is better than panicking mid-allocation.

Source§

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

Report every Gc owned by self to tracer.

Auto Trait Implementations§

§

impl !Freeze for JsObject

§

impl !RefUnwindSafe for JsObject

§

impl !Send for JsObject

§

impl !Sync for JsObject

§

impl !UnwindSafe for JsObject

§

impl Forget for JsObject

§

impl Move for JsObject

§

impl Unpin for JsObject
where *const GcBox<JsObject> is !null: Unpin, *const u8 is !null: Unpin, Rc<[u8]>: Unpin, *const GcBox<JsSymbol> is !null: Unpin, *const GcBox<JsString> is !null: Unpin, *const GcBox<BigInt> is !null: Unpin, *const GcBox<CompiledCode> is !null: Unpin, *const GcBox<ClosureEnv> is !null: Unpin, Rc<RefCell<Box<dyn for<'i> FnMut(&mut Interp<'i>, Value, &[Value]) -> Result<Value, Signal>>>>: Unpin, Rc<RefCell<ProxyState>>: Unpin, *const GcBox<Cell> is !null: Unpin, Rc<Regex>: Unpin, Rc<RefCell<BufferStorage>>: Unpin, Box<GeneratorState>: Unpin, Rc<RefCell<PromiseState>>: Unpin, Box<FinalizationState>: Unpin,

§

impl UnsafeUnpin for JsObject

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.