mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-28 09:21:55 +01:00
With Options::es2015, vectorcraft-js reads and runs for-of loops and array and object destructuring (ECMA-262 11th edition, sections 13.7.5, 12.15.5 and 13.3.3) in var, let and const declarations, assignments, function and arrow parameters and catch clauses, with defaults, holes and rest elements. Object literals take shorthand and computed names. for-of, spread and array patterns use the iterator protocol and call the iterator's return method when a loop or pattern stops early. Destructuring undefined or null throws a TypeError. Object rest properties (ECMAScript 2018) stay syntax errors. A realm switched to ES2015 without Symbol.iterator walks arrays, arguments objects and strings by index. Each step takes fuel, a rest element or spread reserves the whole size of each new element against the heap limit, and nested patterns count against Options::max_depth. The test262 runner reads the for-of and dstr folders: 609 of 617 runs pass (529 before).
289 lines
11 KiB
Rust
289 lines
11 KiB
Rust
//! Destructuring and `for (… of …)` (ECMA-262 11th edition,
|
|
//! <https://262.ecma-international.org/11.0/>, sections 12.15.5, 13.3.3 and 13.7.5).
|
|
//!
|
|
//! A pattern takes the value on top of the stack. An array pattern steps the value's iterator
|
|
//! once for each element and closes it when the pattern ends with the iterator not done, and when
|
|
//! anything in the pattern throws (an exception handler around the elements). An object pattern
|
|
//! reads each property by name. A target that is not a pattern is written as an assignment
|
|
//! (`[a.b] = …`), a `var` or a `let`, `const`, parameter or catch binding, as [`Mode`] says.
|
|
|
|
use super::{CResult, Ctx, FnCompiler, internal, key_of, len_u32};
|
|
use crate::ast::{Binding, Expr, ExprId, ForInTarget, LexKind, Property, PropertyKey, PropertyValue, StmtId};
|
|
use crate::bytecode::{BlockScope, CodeKind, Op};
|
|
use crate::heap::Key;
|
|
|
|
/// How a pattern's targets are written.
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub(super) enum Mode {
|
|
/// An assignment: each target is evaluated as an assignment's target is.
|
|
Assign,
|
|
/// `var`: the variable is assigned.
|
|
Var,
|
|
/// `let`, `const`, a parameter or a catch clause: the binding is initialized.
|
|
Lexical,
|
|
}
|
|
|
|
impl FnCompiler<'_> {
|
|
/// Whether `id`, without parentheses, is an array or object pattern.
|
|
pub(super) fn is_pattern(&self, id: ExprId) -> bool {
|
|
matches!(self.ast.expr(self.ast.unparenthesized(id)), Some(Expr::Array(_) | Expr::Object(_)))
|
|
}
|
|
|
|
/// The names a binding binds, as keys.
|
|
pub(super) fn bound_keys(&self, binding: &Binding) -> Vec<Key> {
|
|
self.ast.bound_names(binding).iter().map(|n| key_of(&n.name)).collect()
|
|
}
|
|
|
|
/// `value` → nothing: writes the value to the targets of the pattern `pattern`.
|
|
pub(super) fn destructure(&mut self, pattern: ExprId, mode: Mode) -> CResult<()> {
|
|
self.enter()?;
|
|
let pattern = self.ast.unparenthesized(pattern);
|
|
self.at(self.ast.expr_span(pattern).start);
|
|
let r = match self.ast.expr(pattern) {
|
|
Some(Expr::Array(elements)) => self.array_pattern(elements, mode),
|
|
Some(Expr::Object(properties)) => self.object_pattern(properties, mode),
|
|
_ => internal("not a destructuring pattern"),
|
|
};
|
|
self.leave();
|
|
r
|
|
}
|
|
|
|
/// `value` → nothing: writes the value to a name binding or a pattern.
|
|
pub(super) fn bind_to(&mut self, binding: &Binding, mode: Mode) -> CResult<()> {
|
|
match binding {
|
|
Binding::Pattern(p) => self.destructure(*p, mode),
|
|
Binding::Name(name) => {
|
|
self.at(name.span.start);
|
|
let n = self.var_name(&name.name);
|
|
if mode == Mode::Lexical {
|
|
self.emit(Op::InitVar(n));
|
|
} else {
|
|
self.emit(Op::SetVar(n));
|
|
self.emit(Op::Pop);
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
}
|
|
|
|
/// An element's target and its default value.
|
|
fn split_default(&self, element: ExprId) -> (ExprId, Option<ExprId>) {
|
|
match self.ast.expr(element) {
|
|
Some(Expr::Assign { op: None, target, value }) => (*target, Some(*value)),
|
|
_ => (element, None),
|
|
}
|
|
}
|
|
|
|
/// Pushes what writing to `target` needs before its value, and gives how many values that is:
|
|
/// an assignment's object and key; nothing for a binding or a pattern.
|
|
fn target_refs(&mut self, target: ExprId, mode: Mode) -> CResult<u32> {
|
|
if mode != Mode::Assign || self.is_pattern(target) {
|
|
return Ok(0);
|
|
}
|
|
let before = self.depth;
|
|
self.assign_target(None, self.ast.unparenthesized(target))?;
|
|
Ok(self.depth.saturating_sub(before))
|
|
}
|
|
|
|
/// `refs… value` → nothing: writes the value to `target`.
|
|
fn write_target(&mut self, target: ExprId, mode: Mode) -> CResult<()> {
|
|
let target = self.ast.unparenthesized(target);
|
|
if self.is_pattern(target) {
|
|
return self.destructure(target, mode);
|
|
}
|
|
self.at(self.ast.expr_span(target).start);
|
|
match (mode, self.ast.expr(target)) {
|
|
(Mode::Assign, _) => {
|
|
self.store(target)?;
|
|
self.emit(Op::Pop);
|
|
}
|
|
(_, Some(Expr::Ident(name))) => {
|
|
let n = self.var_name(name);
|
|
if mode == Mode::Lexical {
|
|
self.emit(Op::InitVar(n));
|
|
} else {
|
|
self.emit(Op::SetVar(n));
|
|
self.emit(Op::Pop);
|
|
}
|
|
}
|
|
_ => return internal("a binding pattern's target is not a name"),
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// `value` → `value`, or the default value when the value is `undefined`.
|
|
fn default_value(&mut self, default: Option<ExprId>) -> CResult<()> {
|
|
let Some(default) = default else { return Ok(()) };
|
|
let skip = self.new_label();
|
|
self.emit(Op::Dup);
|
|
self.emit(Op::Undefined);
|
|
self.emit(Op::Binary(crate::ast::BinaryOp::StrictEq));
|
|
self.jump(Op::JumpIfFalse, skip);
|
|
self.emit(Op::Pop);
|
|
self.expr(default)?;
|
|
self.bind(skip);
|
|
Ok(())
|
|
}
|
|
|
|
/// `value` → nothing, for `[a, , b = 1, ...c]` (sections 12.15.5.5 and 13.3.3.6).
|
|
fn array_pattern(&mut self, elements: &[Option<ExprId>], mode: Mode) -> CResult<()> {
|
|
self.emit(Op::GetIterator);
|
|
let record = self.depth;
|
|
let start = self.pc();
|
|
for element in elements {
|
|
let Some(element) = *element else {
|
|
self.emit(Op::IterValue(0));
|
|
self.emit(Op::Pop);
|
|
continue;
|
|
};
|
|
if let Some(Expr::Spread(target)) = self.ast.expr(element) {
|
|
let under = self.target_refs(*target, mode)?;
|
|
self.emit(Op::IterRest(under));
|
|
self.write_target(*target, mode)?;
|
|
continue;
|
|
}
|
|
let (target, default) = self.split_default(element);
|
|
let under = self.target_refs(target, mode)?;
|
|
self.emit(Op::IterValue(under));
|
|
self.default_value(default)?;
|
|
self.write_target(target, mode)?;
|
|
}
|
|
let end = self.pc();
|
|
let close_throw = self.new_label();
|
|
let after = self.new_label();
|
|
self.emit(Op::IterClose);
|
|
self.jump(Op::Jump, after);
|
|
self.handlers.push((start, end, close_throw, record, self.env_depth));
|
|
self.bind(close_throw);
|
|
self.depth = record + 1;
|
|
self.emit(Op::IterCloseThrow);
|
|
self.bind(after);
|
|
self.depth = record.saturating_sub(1);
|
|
Ok(())
|
|
}
|
|
|
|
/// `value` → nothing, for `{a, b: c, [d]: e = 1}` (sections 12.15.5.4 and 13.3.3.5).
|
|
fn object_pattern(&mut self, properties: &[Property], mode: Mode) -> CResult<()> {
|
|
self.emit(Op::RequireObjectCoercible);
|
|
for p in properties {
|
|
let PropertyValue::Init(value) = p.value else { return internal("a getter or setter in a pattern") };
|
|
self.at(p.span.start);
|
|
match &p.key {
|
|
PropertyKey::Computed(e) => {
|
|
self.expr(*e)?;
|
|
self.emit(Op::ToKey);
|
|
}
|
|
key => {
|
|
let i = self.string(&key.to_utf16());
|
|
self.emit(Op::String(i));
|
|
}
|
|
}
|
|
let (target, default) = self.split_default(value);
|
|
let under = self.target_refs(target, mode)?;
|
|
self.emit(Op::PatternGet(under));
|
|
self.default_value(default)?;
|
|
self.write_target(target, mode)?;
|
|
self.emit(Op::Pop);
|
|
}
|
|
self.emit(Op::Pop);
|
|
Ok(())
|
|
}
|
|
|
|
/// `for (target of iterable) body` (sections 13.7.5.12 and 13.7.5.13): the iterable is read
|
|
/// where a `let` or `const` head's names are not yet initialized; each step's value goes to
|
|
/// the target, in a new environment for a `let` or `const` head; the iterator is closed when
|
|
/// the loop is left early, by `break`, `return`, a jump to an outer loop or an exception.
|
|
pub(super) fn for_of(&mut self, target: &ForInTarget, iterable: ExprId, body: StmtId, labels: Vec<String>) -> CResult<()> {
|
|
let scope = match target {
|
|
ForInTarget::Lexical(kind, binding) => {
|
|
let constant = *kind == LexKind::Const;
|
|
let lets = self.bound_keys(binding).into_iter().map(|k| (k, constant)).collect();
|
|
Some(self.push_scope(BlockScope { lets, funcs: Vec::new() }))
|
|
}
|
|
_ => None,
|
|
};
|
|
match scope {
|
|
Some(s) => {
|
|
self.emit(Op::PushBlock(s));
|
|
self.env_depth += 1;
|
|
self.expr(iterable)?;
|
|
self.emit(Op::PopEnv);
|
|
self.env_depth -= 1;
|
|
}
|
|
None => self.expr(iterable)?,
|
|
}
|
|
// The loop's completion value starts as `undefined` (ForIn/OfBodyEvaluation, step 2).
|
|
if self.kind != CodeKind::Function {
|
|
self.emit(Op::Undefined);
|
|
self.emit(Op::SetCompletion);
|
|
}
|
|
self.at(self.ast.expr_span(iterable).start);
|
|
self.emit(Op::GetIterator);
|
|
let record = self.depth;
|
|
let outer_env = self.env_depth;
|
|
let head = self.new_label();
|
|
let brk = self.new_label();
|
|
let done = self.new_label();
|
|
let close_throw = self.new_label();
|
|
let after = self.new_label();
|
|
self.contexts.push(Ctx::Iterator { depth: record, env_depth: outer_env });
|
|
self.bind(head);
|
|
self.jump(Op::IterStep, done);
|
|
let start = self.pc();
|
|
if let Some(s) = scope {
|
|
self.emit(Op::PushBlock(s));
|
|
self.env_depth += 1;
|
|
}
|
|
let r = self.for_of_target(target);
|
|
let r = r.and_then(|()| {
|
|
self.push_jump_ctx(labels, true, true, brk, Some(head));
|
|
if let Some(Ctx::Jump { env_depth, .. }) = self.contexts.last_mut() {
|
|
*env_depth = outer_env;
|
|
}
|
|
let r = self.stmt(body);
|
|
self.contexts.pop();
|
|
r
|
|
});
|
|
self.contexts.pop();
|
|
r?;
|
|
if scope.is_some() {
|
|
self.emit(Op::PopEnv);
|
|
self.env_depth -= 1;
|
|
}
|
|
let end = self.pc();
|
|
self.jump(Op::Jump, head);
|
|
self.handlers.push((start, end, close_throw, record, outer_env));
|
|
self.bind(close_throw);
|
|
self.depth = record + 1;
|
|
self.emit(Op::IterCloseThrow);
|
|
self.bind(brk);
|
|
self.depth = record;
|
|
self.emit(Op::IterClose);
|
|
self.jump(Op::Jump, after);
|
|
self.bind(done);
|
|
self.depth = record;
|
|
self.emit(Op::Pop);
|
|
self.bind(after);
|
|
Ok(())
|
|
}
|
|
|
|
/// `value` → nothing: a `for-of` loop's step value written to its target.
|
|
fn for_of_target(&mut self, target: &ForInTarget) -> CResult<()> {
|
|
match target {
|
|
ForInTarget::Var(d) => self.bind_to(&d.target, Mode::Var),
|
|
ForInTarget::Lexical(_, binding) => self.bind_to(binding, Mode::Lexical),
|
|
ForInTarget::Expr(e) if self.is_pattern(*e) => self.destructure(*e, Mode::Assign),
|
|
ForInTarget::Expr(e) => {
|
|
self.store_key_to(*e)?;
|
|
self.emit(Op::Pop);
|
|
Ok(())
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The key of a destructured parameter's own binding, which no script can name.
|
|
pub(super) fn hidden_param(&self, i: usize) -> Key {
|
|
key_of(&format!("\u{0}p{}", len_u32(i)))
|
|
}
|
|
}
|