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>
impl<A: IVm> Object<A>
Sourcepub fn own_entries(&self) -> Vec<Property<A>> ⓘ
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>
impl<A: IVm> Object<A>
Sourcepub fn own_property(&self, key: &String<A>) -> Option<Any<A>>
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>();