Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement destructuring assignments #1406

Merged
merged 8 commits into from
Aug 21, 2021
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
89 changes: 67 additions & 22 deletions boa/src/bytecompiler.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
syntax::ast::{
node::{GetConstField, GetField, Identifier, StatementList},
node::{Declaration, GetConstField, GetField, Identifier, StatementList},
op::{AssignOp, BinOp, BitOp, CompOp, LogOp, NumOp, UnaryOp},
Const, Node,
},
Expand Down Expand Up @@ -588,35 +588,80 @@ impl ByteCompiler {
match node {
Node::VarDeclList(list) => {
for decl in list.as_ref() {
let index = self.get_or_insert_name(decl.name());
self.emit(Opcode::DefVar, &[index]);

if let Some(expr) = decl.init() {
self.compile_expr(expr, true);
self.emit(Opcode::InitLexical, &[index]);
};
match decl {
Declaration::Identifier { ident, .. } => {
let index = self.get_or_insert_name(ident.as_ref());
self.emit(Opcode::DefVar, &[index]);

if let Some(expr) = decl.init() {
self.compile_expr(expr, true);
self.emit(Opcode::InitLexical, &[index]);
};
}
Declaration::Pattern(pattern) => {
for ident in pattern.idents() {
let index = self.get_or_insert_name(ident);
self.emit(Opcode::DefVar, &[index]);

if let Some(expr) = decl.init() {
self.compile_expr(expr, true);
self.emit(Opcode::InitLexical, &[index]);
};
}
}
}
}
}
Node::LetDeclList(list) => {
for decl in list.as_ref() {
let index = self.get_or_insert_name(decl.name());
self.emit(Opcode::DefLet, &[index]);

if let Some(expr) = decl.init() {
self.compile_expr(expr, true);
self.emit(Opcode::InitLexical, &[index]);
};
match decl {
Declaration::Identifier { ident, .. } => {
let index = self.get_or_insert_name(ident.as_ref());
self.emit(Opcode::DefLet, &[index]);

if let Some(expr) = decl.init() {
self.compile_expr(expr, true);
self.emit(Opcode::InitLexical, &[index]);
};
}
Declaration::Pattern(pattern) => {
for ident in pattern.idents() {
let index = self.get_or_insert_name(ident);
self.emit(Opcode::DefLet, &[index]);

if let Some(expr) = decl.init() {
self.compile_expr(expr, true);
self.emit(Opcode::InitLexical, &[index]);
};
}
}
}
}
}
Node::ConstDeclList(list) => {
for decl in list.as_ref() {
let index = self.get_or_insert_name(decl.name());
self.emit(Opcode::DefConst, &[index]);

if let Some(expr) = decl.init() {
self.compile_expr(expr, true);
self.emit(Opcode::InitLexical, &[index]);
};
match decl {
Declaration::Identifier { ident, .. } => {
let index = self.get_or_insert_name(ident.as_ref());
self.emit(Opcode::DefConst, &[index]);

if let Some(expr) = decl.init() {
self.compile_expr(expr, true);
self.emit(Opcode::InitLexical, &[index]);
};
}
Declaration::Pattern(pattern) => {
for ident in pattern.idents() {
let index = self.get_or_insert_name(ident);
self.emit(Opcode::DefConst, &[index]);

if let Some(expr) = decl.init() {
self.compile_expr(expr, true);
self.emit(Opcode::InitLexical, &[index]);
};
}
}
}
}
}
Node::If(node) => {
Expand Down
71 changes: 71 additions & 0 deletions boa/src/object/gcobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,77 @@ impl GcObject {
// 16. Return desc.
Ok(desc.build())
}

/// `7.3.25 CopyDataProperties ( target, source, excludedItems )`
///
/// More information:
/// - [ECMAScript][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-copydataproperties
#[inline]
pub fn copy_data_properties<K>(
&mut self,
source: &JsValue,
excluded_keys: Vec<K>,
context: &mut Context,
) -> Result<()>
where
K: Into<PropertyKey>,
{
// 1. Assert: Type(target) is Object.
// 2. Assert: excludedItems is a List of property keys.
// 3. If source is undefined or null, return target.
if source.is_null_or_undefined() {
return Ok(());
}

// 4. Let from be ! ToObject(source).
let from = source
.to_object(context)
.expect("function ToObject should never complete abruptly here");

// 5. Let keys be ? from.[[OwnPropertyKeys]]().
// 6. For each element nextKey of keys, do
let excluded_keys: Vec<PropertyKey> = excluded_keys.into_iter().map(|e| e.into()).collect();
for key in from.own_property_keys() {
// a. Let excluded be false.
let mut excluded = false;

// b. For each element e of excludedItems, do
for e in &excluded_keys {
// i. If SameValue(e, nextKey) is true, then
if *e == key {
// 1. Set excluded to true.
excluded = true;
break;
}
}
// c. If excluded is false, then
if !excluded {
// i. Let desc be ? from.[[GetOwnProperty]](nextKey).
let desc = from.__get_own_property__(&key);

// ii. If desc is not undefined and desc.[[Enumerable]] is true, then
if let Some(desc) = desc {
if let Some(enumerable) = desc.enumerable() {
if enumerable {
// 1. Let propValue be ? Get(from, nextKey).
let prop_value = from.__get__(&key, from.clone().into(), context)?;

// 2. Perform ! CreateDataPropertyOrThrow(target, nextKey, propValue).
self.create_data_property_or_throw(key, prop_value, context)
.expect(
"CreateDataPropertyOrThrow should never complete abruptly here",
);
}
}
}
}
}

// 7. Return target.
Ok(())
}
}

impl AsRef<GcCell<Object>> for GcObject {
Expand Down
2 changes: 1 addition & 1 deletion boa/src/property/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ impl From<PropertyDescriptorBuilder> for PropertyDescriptor {
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-ispropertykey
#[derive(Trace, Finalize, Debug, Clone)]
#[derive(Trace, Finalize, PartialEq, Debug, Clone)]
pub enum PropertyKey {
String(JsString),
Symbol(JsSymbol),
Expand Down
Loading