Skip to main content

Object

Struct Object 

Source
pub struct Object<A: IVm>(/* private fields */);
Expand description
use nanvm_lib::{vm::{Object, ToObject, IVm, Array, Any, ToAny}, naive::Naive};
fn object_test<A: IVm>() {
    let b: Object<A> = [("a".into(), true.to_any())].to_object();
    let b1: Object<A> = [("a".into(), true.to_any())].to_object();
    assert_eq!(b, b);
    assert_ne!(b, b1);
    let ac: Any<A> = b.clone().to_any();
    let d: Object<A> = ac.try_into().unwrap();
    assert_eq!(d, b);
}

object_test::<Naive>();

Implementations§

Source§

impl<A: IVm> Object<A>

Source

pub fn own_entries(&self) -> Vec<Property<A>> ⓘ

The own enumerable string-keyed entries, as [[OwnPropertyKeys]] lists them: each key once, holding its last value at its first position, array-index keys first, ascending by value, then every other key in insertion order. The one view JSON.stringify writes and an object spread copies (Any::to_json, Any::object_spread), so the two cannot disagree on order.

An object’s property list is never deduplicated on construction (Object::own_property’s doc comment), so one pass over it keeps each key’s first position and overwrites its value on every later write, finding the key’s slot through a map ordered by String’s Ord: O(n log n) in the number of properties, where a scan per key would be quadratic.

use nanvm_lib::{naive::Naive, vm::{Any, IVm, Object, String, ToAny, ToObject}};
fn own_entries_test<A: IVm>() {
    let o: Object<A> = [
        ("b".into(), true.to_any()),
        ("1".into(), true.to_any()),
        ("b".into(), false.to_any()),
    ].to_object();
    let keys: Vec<String<A>> = o.own_entries().into_iter().map(|(k, _)| k).collect();
    assert_eq!(keys, [String::from("1"), String::from("b")]);
}
own_entries_test::<Naive>();
Source§

impl<A: IVm> Object<A>

Source

pub fn own_property(&self, key: &String<A>) -> Option<Any<A>>

Object.getOwnPropertyDescriptor(self, key)?.value — a flat lookup by key, with no getter invocation and no prototype chain to walk. nanvm-lib objects have no __proto__/prototype chain at all, so this is simply what property lookup already is here — there is no second, chain-walking accessor for this to differ from. None is “no such own property”, distinct from Some of a present undefined, which Any::has_entry tells apart where Any::entry answers undefined for both; a caller that wants the value itself uses this.

Searches from the last entry backward: an object’s property list is never deduplicated on construction (fjs/edag/module.f.mjs’s own comment on '{}': “duplicate keys are allowed with the later entry winning”), so the last-written entry for a repeated key is the one a lookup must answer with, not the first.

use nanvm_lib::{naive::Naive, vm::{Any, IVm, Object, ToObject}};
fn own_property_test<A: IVm>() {
    let o: Object<A> = [("a".into(), Any::undefined())].to_object();
    assert_eq!(o.own_property(&"a".into()), Some(Any::undefined()));
    assert_eq!(o.own_property(&"b".into()), None);
}

own_property_test::<Naive>();

Trait Implementations§

Source§

impl<A: Clone + IVm> Clone for Object<A>

Source§

fn clone(&self) -> Object<A>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<A: IVm> Debug for Object<A>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<A: IVm> Default for Object<A>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<A: IVm> From<Object<A>> for Unpacked<A>

Source§

fn from(value: Object<A>) -> Self

Converts to this type from the input type.
Source§

impl<A: IVm> Index<u32> for Object<A>

Source§

fn index(&self, index: u32) -> &Self::Output

Currently panics if out of bounds. TODO: Future versions may change to return Nullish::Undefined.

Source§

type Output = (String<A>, Any<A>)

The returned type after indexing.
Source§

impl<A: IVm> IntoIterator for Object<A>

Source§

type Item = (String<A>, Any<A>)

The type of the elements being iterated over.
Source§

type IntoIter = IndexIter<u32, Object<A>>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<A: IVm> PartialEq for Object<A>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<A: IVm> TryFrom<Any<A>> for Object<A>

Source§

type Error = Any<A>

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

fn try_from(value: Any<A>) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

§

impl<A> Freeze for Object<A>
where <A as IVm>::InternalObject: Freeze,

§

impl<A> RefUnwindSafe for Object<A>

§

impl<A> Send for Object<A>
where <A as IVm>::InternalObject: Send,

§

impl<A> Sync for Object<A>
where <A as IVm>::InternalObject: Sync,

§

impl<A> Unpin for Object<A>
where <A as IVm>::InternalObject: Unpin,

§

impl<A> UnsafeUnpin for Object<A>

§

impl<A> UnwindSafe for Object<A>
where <A as IVm>::InternalObject: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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<W> SizedIndex<u32> for W
where W: ContainerWrapper,

Source§

fn length(&self) -> u32

Source§

fn is_empty(&self) -> bool

Source§

fn last(&self) -> Option<&Self::Output>

Source§

fn index_iter(self) -> IndexIter<I, Self> ⓘ
where Self: Sized,

Source§

impl<T> ToAny for T

Source§

fn to_any<A: IVm>(self) -> Any<A>
where Self: Into<Unpacked<A>>,

Source§

impl<T> ToArray for T

Source§

fn to_array<A: IVm>(self) -> Array<A>
where Self: Sized + IntoIterator<Item = Any<A>>,

Source§

impl<T> ToObject for T

Source§

fn to_object<A: IVm>(self) -> Object<A>
where Self: Sized + IntoIterator<Item = Property<A>>,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T

Source§

fn try_to_string<A: IVm>(self) -> Result<String<A>, Any<A>>
where Self: Sized + IntoIterator<Item = Result<u16, Any<A>>>,

Source§

fn to_string<A: IVm>(self) -> String<A>
where Self: Sized + IntoIterator<Item = u16>,

Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.