Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions rust/crates/psychoscript/src/accessors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ impl Vm {
.props
.prop(key)
.filter(|p| p.flags & ACCESSOR != 0)
.map(|p| self.pair(p.value));
.map(|p| self.pair(p.value()));
let mut a = current.unwrap_or(Accessor {
get: Value::Undef,
set: Value::Undef,
Expand All @@ -292,7 +292,7 @@ impl Vm {
o.flags |= ACCESSORS;
match o.props.find(key) {
Some(i) => {
o.props.items[i].value = pair;
o.props.items[i].set_value(pair);
o.props.items[i].flags = flags;
}
None => o.props.push_new(key, pair, flags),
Expand All @@ -319,7 +319,7 @@ impl Vm {
while let Some(c) = cur {
let o = &self.heap[c as usize];
if let Some(p) = o.props.prop(key) {
return (p.flags & ACCESSOR != 0).then(|| self.pair(p.value));
return (p.flags & ACCESSOR != 0).then(|| self.pair(p.value()));
}
if self.has_own(c, key) {
return None;
Expand Down Expand Up @@ -361,7 +361,7 @@ impl Vm {
.props
.prop(k)
.filter(|p| p.flags & ACCESSOR != 0)
.map(|p| p.value)
.map(|p| p.value())
.filter(|pair| self.is_program_accessor(*pair))
.map(|pair| self.accessor_label(pair));
match label {
Expand All @@ -377,8 +377,8 @@ impl Vm {
let mut cur = Some(id);
while let Some(c) = cur {
if let Some(p) = self.heap[c as usize].props.prop(key) {
if p.flags & ACCESSOR != 0 && self.is_program_accessor(p.value) {
return Err(self.accessor_label(p.value));
if p.flags & ACCESSOR != 0 && self.is_program_accessor(p.value()) {
return Err(self.accessor_label(p.value()));
}
break;
}
Expand Down Expand Up @@ -789,7 +789,7 @@ impl Vm {
while let Some(c) = cur {
let o = &self.heap[c as usize];
if let Some(p) = o.props.prop(key) {
return p.flags & ACCESSOR != 0 && self.is_program_half(&self.pair(p.value).get);
return p.flags & ACCESSOR != 0 && self.is_program_half(&self.pair(p.value()).get);
}
if self.has_own(c, key) {
return false;
Expand Down Expand Up @@ -1015,12 +1015,12 @@ impl Vm {
self.fix_stack(id, key);
}
let p = self.heap[id as usize].props.items[i].clone();
if let Value::Uninit = p.value {
if let Value::Uninit = p.value() {
// A built-in accessor: from now on its halves are its companions.
let (get, set) = self.companions(id, key);
let pair = self.new_pair(get, set);
let o = &mut self.heap[id as usize];
o.props.items[i].value = pair;
o.props.items[i].set_value(pair);
o.props.items[i].flags = ACCESSOR;
return Some(OwnSlot {
value: SlotValue::Accessor(get, set),
Expand All @@ -1032,7 +1032,7 @@ impl Vm {
let configurable = p.flags & FIXED == 0 && !fixed_all;
let enumerable = p.flags & ENUMERABLE != 0;
if p.flags & ACCESSOR != 0 {
let a = self.pair(p.value);
let a = self.pair(p.value());
return Some(OwnSlot {
value: SlotValue::Accessor(a.get, a.set),
writable: false,
Expand All @@ -1043,7 +1043,7 @@ impl Vm {
// A sparse array's far element shares the array's attributes; its length is the array's.
let writable = p.flags & WRITABLE != 0 && !frozen;
Some(OwnSlot {
value: SlotValue::Data(p.value),
value: SlotValue::Data(p.value()),
writable,
enumerable,
configurable,
Expand Down Expand Up @@ -1444,7 +1444,7 @@ impl Vm {
let o = &mut self.heap[id as usize];
match o.props.find(key) {
Some(i) => {
o.props.items[i].value = value;
o.props.items[i].set_value(value);
o.props.items[i].flags = flags;
}
None => o.props.push_new(key, value, flags),
Expand Down
2 changes: 1 addition & 1 deletion rust/crates/psychoscript/src/builtins/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ fn tail_elem(vm: &Vm, id: Id, i: usize) -> Option<Value> {
return None;
}
let key = vm.atoms.lookup(&i.to_string())?;
vm.heap[id as usize].props.get(key).copied()
vm.heap[id as usize].props.get(key)
}

/// Element `i` as the array holds it, with a hole read as `undefined` and not looked up on the prototypes.
Expand Down
8 changes: 2 additions & 6 deletions rust/crates/psychoscript/src/builtins/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,7 @@ fn error_proto(vm: &mut Vm, callee: Id, fallback: Id) -> Id {
return proto;
};
match vm.heap[nt as usize].props.prop(vm.names.prototype) {
Some(p) if p.flags & ACCESSOR == 0 => p.value.as_obj().unwrap_or(proto),
Some(p) if p.flags & ACCESSOR == 0 => p.value().as_obj().unwrap_or(proto),
_ => proto,
}
}
Expand Down Expand Up @@ -1587,11 +1587,7 @@ pub(crate) fn revive_step(
let child = match snapshot {
Value::Obj(children) => {
let a = vm.key_of(&k)?;
vm.heap[children as usize]
.props
.get(a)
.copied()
.unwrap_or_default()
vm.heap[children as usize].props.get(a).unwrap_or_default()
}
_ => Value::Undef,
};
Expand Down
2 changes: 1 addition & 1 deletion rust/crates/psychoscript/src/builtins/sets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ fn object_groups(vm: &mut Vm, pairs: impl Iterator<Item = (Value, Value)>) -> Re
let id = vm.alloc(Obj::new(None, Kind::Ordinary));
for (k, item) in pairs {
let atom = vm.key_of(&k)?;
match vm.heap[id as usize].props.get(atom).copied() {
match vm.heap[id as usize].props.get(atom) {
Some(Value::Obj(arr)) => {
if let Kind::Array(items) = &mut vm.heap[arr as usize].kind {
items.push(item);
Expand Down
3 changes: 2 additions & 1 deletion rust/crates/psychoscript/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ pub enum Op {
CtorReturn(bool),
/// A class field, `[o, (key), v] -> [o]` (key on the stack when `.1`): defined as data, or a TypeError.
DefineClassField(Atom, bool),
NewObject,
/// A new object; the operand is how many properties its literal names, which it gets room for at once.
NewObject(u32),
NewArray(u32),
ArrayPush,
ArraySpread,
Expand Down
16 changes: 8 additions & 8 deletions rust/crates/psychoscript/src/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl Vm {
let o = &mut self.heap[id as usize];
match o.props.find(key) {
Some(i) => {
o.props.items[i].value = val;
o.props.items[i].set_value(val);
o.props.items[i].flags = flags;
}
None => o.props.push_new(key, val, flags),
Expand Down Expand Up @@ -226,7 +226,7 @@ impl Vm {
self.heap[fid as usize]
.props
.prop(self.names.name)
.is_some_and(|p| p.flags & ACCESSOR != 0 || !matches!(p.value, Value::Str(_)))
.is_some_and(|p| p.flags & ACCESSOR != 0 || !matches!(p.value(), Value::Str(_)))
}

/// `Op::SetFields`.
Expand Down Expand Up @@ -387,13 +387,13 @@ impl Vm {
while let Some(c) = cur {
if let Some(p) = self.heap[c as usize].props.prop(key) {
if p.flags & ACCESSOR == 0 {
if let Value::Uninit = p.value {
if let Value::Uninit = p.value() {
// A built-in accessor serves the receiver if it is of its kind.
return self.companion_value(receiver, key);
}
return p.value;
return p.value();
}
let pair = p.value;
let pair = p.value();
self.op_access = true;
let v = self.accessor_read(pair, receiver, key);
self.op_access = false;
Expand Down Expand Up @@ -465,9 +465,9 @@ impl Vm {
while let Some(c) = cur {
if let Some(p) = self.heap[c as usize].props.prop(key) {
if p.flags & ACCESSOR != 0 {
return Some((p.value, false));
return Some((p.value(), false));
}
return matches!(p.value, Value::Uninit).then_some((Value::Undef, true));
return matches!(p.value(), Value::Uninit).then_some((Value::Undef, true));
}
if self.has_own(c, key) {
return None;
Expand Down Expand Up @@ -583,7 +583,7 @@ impl Vm {
.props
.prop(ck)
.filter(|p| p.flags & ACCESSOR == 0)
.map(|p| p.value)?;
.map(|p| p.value())?;
if self.class_of(&ctor) == 0 {
return None;
}
Expand Down
7 changes: 6 additions & 1 deletion rust/crates/psychoscript/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3707,7 +3707,12 @@ impl<'a, 's> Compiler<'a, 's> {
}

fn object_lit(&mut self, o: &'a ObjectExpression<'a>) -> R<()> {
self.emit(Op::NewObject);
let named = o
.properties
.iter()
.filter(|p| matches!(p, ObjectPropertyKind::ObjectProperty(_)))
.count();
self.emit(Op::NewObject(named as u32));
for p in &o.properties {
match p {
ObjectPropertyKind::SpreadProperty(s) => {
Expand Down
10 changes: 7 additions & 3 deletions rust/crates/psychoscript/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1054,8 +1054,8 @@ fn plain_prop(
return None;
}
let p = o.props.items.get(fr.index(vm, top(vm, fr) + L_I))?;
(p.key == atom && p.flags & ACCESSOR == 0 && !matches!(p.value, Value::Uninit))
.then_some(p.value)
(p.key == atom && p.flags & ACCESSOR == 0 && !matches!(p.value(), Value::Uninit))
.then_some(p.value())
}

/// The key of the value the walk visits, as a `toJSON` or the replacer receives it.
Expand Down Expand Up @@ -1511,6 +1511,10 @@ impl<'s> Parser<'s> {
let id = vm.new_object();
let children = self.children(vm);
self.ws();
if self.s.get(self.i) != Some(&b'}') {
// Its count is not known yet: room for four, which most parsed objects fill without growing again.
vm.heap[id as usize].props.reserve_exact(4);
}
if self.s.get(self.i) == Some(&b'}') {
self.i += 1;
self.depth -= 1;
Expand Down Expand Up @@ -1870,7 +1874,7 @@ pub fn holds_runtime_reference(vm: &Vm, v: &Value) -> bool {
.items
.iter()
.filter(|p| p.flags & ACCESSOR == 0)
.map(|p| p.value),
.map(|p| p.value()),
);
}
false
Expand Down
4 changes: 2 additions & 2 deletions rust/crates/psychoscript/src/cross.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ fn flat(vm: &Vm, v: &Value) -> Option<Json> {
if k == "__proto__" {
continue;
}
let j = match p.value {
let j = match p.value() {
Value::Undef | Value::Uninit => continue,
Value::Null => Json::Null,
Value::Bool(b) => Json::Bool(b),
Expand Down Expand Up @@ -382,7 +382,7 @@ fn plain_json(vm: &Vm, v: &Value) -> Option<Json> {
if p.flags & ENUMERABLE == 0 || p.key <= SYM_LAST {
continue;
}
if let Some(j) = plain_json(vm, &p.value) {
if let Some(j) = plain_json(vm, &p.value()) {
out.insert(vm.atoms.text(p.key).to_owned(), j);
}
}
Expand Down
2 changes: 1 addition & 1 deletion rust/crates/psychoscript/src/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ pub(crate) fn measure(vm: &Vm) -> u64 {
crate::reach::head(o, &mut |r| w.see(r));
for p in &o.props.items {
if !skip.contains(&Some(&p.key)) {
w.value(p.value);
w.value(p.value());
}
}
}
Expand Down
48 changes: 40 additions & 8 deletions rust/crates/psychoscript/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::bytecode::FuncProto;
pub use crate::strings::{
Atom, SYM_ASYNC_ITERATOR, SYM_ITERATOR, SYM_LAST, SYM_TO_PRIMITIVE, SYM_TO_STRING_TAG,
};
use crate::value::{Id, Value};
use crate::value::{Id, Slot, Value};
use crate::vm::{NativeFn, SavedFrame};

pub const ENUMERABLE: u8 = 1;
Expand All @@ -25,13 +25,35 @@ pub const ACCESSOR: u8 = 8;
/// `stack.rs`).
pub const PENDING_STACK: u8 = 16;

/// One property: 16 bytes, its value packed in a word as on the stack.
#[derive(Clone)]
pub struct Prop {
value: Slot,
pub key: Atom,
pub value: Value,
pub flags: u8,
}

impl Prop {
#[inline(always)]
pub fn new(key: Atom, value: Value, flags: u8) -> Prop {
Prop {
value: Slot::pack(value),
key,
flags,
}
}

#[inline(always)]
pub fn value(&self) -> Value {
self.value.get()
}

#[inline(always)]
pub fn set_value(&mut self, value: Value) {
self.value = Slot::pack(value);
}
}

/// Insertion-ordered properties; small objects scan linearly, larger ones keep a hash index.
#[derive(Default, Clone)]
pub struct Props {
Expand All @@ -45,11 +67,17 @@ impl Props {
pub fn from_items(items: Vec<Prop>) -> Self {
let mut props = Props::default();
for p in items {
props.push_new(p.key, p.value, p.flags);
props.push_new(p.key, p.value(), p.flags);
}
props
}

/// Room for exactly `n` properties, for an object whose count is known as it is made.
#[inline]
pub fn reserve_exact(&mut self, n: usize) {
self.items.reserve_exact(n);
}

#[inline]
pub fn find(&self, key: Atom) -> Option<usize> {
if let Some(idx) = &self.index {
Expand All @@ -59,13 +87,13 @@ impl Props {
}

#[inline]
pub fn get(&self, key: Atom) -> Option<&Value> {
self.find(key).map(|i| &self.items[i].value)
pub fn get(&self, key: Atom) -> Option<Value> {
self.find(key).map(|i| self.items[i].value())
}

pub fn insert(&mut self, key: Atom, value: Value, flags: u8) {
if let Some(i) = self.find(key) {
self.items[i].value = value;
self.items[i].set_value(value);
return;
}
self.push_new(key, value, flags);
Expand All @@ -74,7 +102,11 @@ impl Props {
/// Appends a key known to be absent.
pub fn push_new(&mut self, key: Atom, value: Value, flags: u8) {
let i = self.items.len();
self.items.push(Prop { key, value, flags });
// The first allocation holds two properties, not the four a vector starts with: most objects are small.
if self.items.capacity() == 0 {
self.items.reserve_exact(2);
}
self.items.push(Prop::new(key, value, flags));
if let Some(idx) = &mut self.index {
idx.insert(key, i as u32);
} else if self.items.len() > INDEX_THRESHOLD {
Expand All @@ -93,7 +125,7 @@ impl Props {
let Some(i) = self.find(before) else {
return self.push_new(key, value, flags);
};
self.items.insert(i, Prop { key, value, flags });
self.items.insert(i, Prop::new(key, value, flags));
if self.index.is_some() || self.items.len() > INDEX_THRESHOLD {
let map = self
.items
Expand Down
Loading