Skip to content

Commit

Permalink
Merge f136b6c into b5678f0
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat committed Feb 26, 2022
2 parents b5678f0 + f136b6c commit b67ce04
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 15 deletions.
6 changes: 6 additions & 0 deletions boa_engine/src/bytecompiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ impl<'b> ByteCompiler<'b> {
Const::Bool(false) => self.emit(Opcode::PushFalse, &[]),
Const::Null => self.emit(Opcode::PushNull, &[]),
Const::Undefined => self.emit(Opcode::PushUndefined, &[]),
Const::Elision => unreachable!(),
}

if !use_expr {
Expand Down Expand Up @@ -921,6 +922,11 @@ impl<'b> ByteCompiler<'b> {
self.emit_opcode(Opcode::PopOnReturnAdd);

for element in array.as_ref() {
if let Node::Const(Const::Elision) = element {
self.emit_opcode(Opcode::PushElisionToArray);
continue;
}

self.compile_expr(element, true)?;
if let Node::Spread(_) = element {
self.emit_opcode(Opcode::InitIterator);
Expand Down
11 changes: 11 additions & 0 deletions boa_engine/src/syntax/ast/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ pub enum Const {
/// [spec]: https://tc39.es/ecma262/#sec-undefined
/// [mdn]: https://developer.mozilla.org/en-US/docs/Glossary/undefined
Undefined,

/// This represents an empty element of an array initializer.
///
/// E.g. `[,]`
///
/// More information:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-array-initializer
Elision,
}

impl From<Sym> for Const {
Expand Down Expand Up @@ -155,6 +165,7 @@ impl ToInternedString for Const {
Self::Bool(v) => v.to_string(),
Self::Null => "null".to_owned(),
Self::Undefined => "undefined".to_owned(),
Self::Elision => String::new(),
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ where
loop {
// TODO: Support all features.
while cursor.next_if(Punctuator::Comma, interner)?.is_some() {
elements.push(Node::Const(Const::Undefined));
elements.push(Node::Const(Const::Elision));
}

if cursor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn check_empty_slot() {
let mut interner = Interner::default();
check_parser(
"[,]",
vec![ArrayDecl::from(vec![Const::Undefined.into()]).into()],
vec![ArrayDecl::from(vec![Const::Elision.into()]).into()],
&mut interner,
);
}
Expand Down Expand Up @@ -65,7 +65,7 @@ fn check_numeric_array_elision() {
vec![ArrayDecl::from(vec![
Const::from(1).into(),
Const::from(2).into(),
Const::Undefined.into(),
Const::Elision.into(),
Const::from(3).into(),
])
.into()],
Expand All @@ -82,8 +82,8 @@ fn check_numeric_array_repeated_elision() {
vec![ArrayDecl::from(vec![
Const::from(1).into(),
Const::from(2).into(),
Const::Undefined.into(),
Const::Undefined.into(),
Const::Elision.into(),
Const::Elision.into(),
Const::from(3).into(),
])
.into()],
Expand Down
22 changes: 12 additions & 10 deletions boa_engine/src/value/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,18 @@ pub(crate) fn log_string_from(x: &JsValue, print_internals: bool, print_children
.map(|i| {
// Introduce recursive call to stringify any objects
// which are part of the Array
log_string_from(
v.borrow()
.properties()
.get(&i.into())
// FIXME: handle accessor descriptors
.and_then(PropertyDescriptor::value)
.unwrap_or(&JsValue::Undefined),
print_internals,
false,
)

// FIXME: handle accessor descriptors
if let Some(value) = v
.borrow()
.properties()
.get(&i.into())
.and_then(PropertyDescriptor::value)
{
log_string_from(value, print_internals, false)
} else {
String::from("<empty>")
}
})
.collect::<Vec<String>>()
.join(", ");
Expand Down
1 change: 1 addition & 0 deletions boa_engine/src/vm/code_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ impl CodeBlock {
| Opcode::PushNewArray
| Opcode::PopOnReturnAdd
| Opcode::PopOnReturnSub
| Opcode::PushElisionToArray
| Opcode::Nop => String::new(),
}
}
Expand Down
11 changes: 11 additions & 0 deletions boa_engine/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,17 @@ impl Context {
.expect("should be able to create new data property");
self.vm.push(array);
}
Opcode::PushElisionToArray => {
let array = self.vm.pop();
let o = array.as_object().expect("should always be an object");

let len = o
.length_of_array_like(self)
.expect("arrays should always have a 'length' property");

o.set("length", len + 1, true, self)?;
self.vm.push(array);
}
Opcode::PushIteratorToArray => {
let next_function = self.vm.pop();
let iterator = self.vm.pop();
Expand Down
8 changes: 8 additions & 0 deletions boa_engine/src/vm/opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ pub enum Opcode {
/// Stack: array, value **=>** array
PushValueToArray,

/// Push an empty element/hole to an array.
///
/// Operands:
///
/// Stack: array **=>** array
PushElisionToArray,

/// Push all iterator values to an array.
///
/// Operands:
Expand Down Expand Up @@ -909,6 +916,7 @@ impl Opcode {
Opcode::PushEmptyObject => "PushEmptyObject",
Opcode::PushNewArray => "PushNewArray",
Opcode::PushValueToArray => "PushValueToArray",
Opcode::PushElisionToArray => "PushElisionToArray",
Opcode::PushIteratorToArray => "PushIteratorToArray",
Opcode::Add => "Add",
Opcode::Sub => "Sub",
Expand Down

0 comments on commit b67ce04

Please sign in to comment.