Skip to content
Open
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
8 changes: 8 additions & 0 deletions core/engine/benches/bench_scripts/string_code_point_sum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(() => {
let sum = "";
let string = "Hello, world!!!";
for (let i = 0; i < string.length; ++i) {
sum += string.charCodeAt(i).toString(16);
}
return sum;
})();
1 change: 1 addition & 0 deletions core/engine/benches/full.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ macro_rules! full_benchmarks {
}

full_benchmarks!(
{"String Code Point Sum", string_code_point_sum},
{"Symbols", symbol_creation},
{"For loop", for_loop},
{"Fibonacci", fibonacci},
Expand Down
16 changes: 3 additions & 13 deletions core/engine/src/bytecompiler/declaration/declaration_pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,7 @@ impl ByteCompiler<'_> {

match name {
PropertyName::Literal(ident) => {
self.emit_get_property_by_name(
&dst,
object,
object,
ident.sym(),
);
self.emit_get_property_by_name(&dst, None, object, ident.sym());
let key = self.register_allocator.alloc();
self.emit_push_literal(
Literal::String(
Expand Down Expand Up @@ -162,7 +157,7 @@ impl ByteCompiler<'_> {
PropertyName::Literal(ident) => {
compiler.emit_get_property_by_name(
&dst,
object,
None,
object,
ident.sym(),
);
Expand Down Expand Up @@ -209,12 +204,7 @@ impl ByteCompiler<'_> {

match name {
PropertyName::Literal(ident) => {
self.emit_get_property_by_name(
&dst,
object,
object,
ident.sym(),
);
self.emit_get_property_by_name(&dst, None, object, ident.sym());
}
PropertyName::Computed(node) => {
let key = self.register_allocator.alloc();
Expand Down
18 changes: 14 additions & 4 deletions core/engine/src/bytecompiler/expression/assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,11 @@ impl ByteCompiler<'_> {
let object = compiler.register_allocator.alloc();
compiler.compile_expr(access.target(), &object);

compiler.emit_get_property_by_name(dst, &object, &object, name.sym());
compiler.emit_get_property_by_name(dst, None, &object, name.sym());

early_exit = emit(&mut compiler, dst, assign.rhs(), assign.op());

compiler.emit_set_property_by_name(dst, &object, &object, name.sym());
compiler.emit_set_property_by_name(dst, None, &object, name.sym());

compiler.register_allocator.dealloc(object);
}
Expand Down Expand Up @@ -247,11 +247,21 @@ impl ByteCompiler<'_> {
compiler.bytecode.emit_super(object.variable());
compiler.bytecode.emit_this(receiver.variable());

compiler.emit_get_property_by_name(dst, &receiver, &object, name.sym());
compiler.emit_get_property_by_name(
dst,
Some(&receiver),
&object,
name.sym(),
);

early_exit = emit(&mut compiler, dst, assign.rhs(), assign.op());

compiler.emit_set_property_by_name(dst, &receiver, &object, name.sym());
compiler.emit_set_property_by_name(
dst,
Some(&receiver),
&object,
name.sym(),
);

compiler.register_allocator.dealloc(receiver);
compiler.register_allocator.dealloc(object);
Expand Down
7 changes: 1 addition & 6 deletions core/engine/src/bytecompiler/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,7 @@ impl ByteCompiler<'_> {
self.compile_expr(access.target(), &this);
match access.field() {
PropertyAccessField::Const(ident) => {
self.emit_get_property_by_name(
&function,
&this,
&this,
ident.sym(),
);
self.emit_get_property_by_name(&function, None, &this, ident.sym());
}
PropertyAccessField::Expr(field) => {
let key = self.register_allocator.alloc();
Expand Down
23 changes: 14 additions & 9 deletions core/engine/src/bytecompiler/expression/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,15 @@ impl ByteCompiler<'_> {

match access.field() {
PropertyAccessField::Const(ident) => {
compiler.emit_get_property_by_name(dst, &object, &object, ident.sym());
compiler.emit_get_property_by_name(dst, None, &object, ident.sym());
let value = compiler.register_allocator.alloc();
if increment {
compiler.bytecode.emit_inc(value.variable(), dst.variable());
} else {
compiler.bytecode.emit_dec(value.variable(), dst.variable());
}

compiler.emit_set_property_by_name(
&value,
&object,
&object,
ident.sym(),
);
compiler.emit_set_property_by_name(&value, None, &object, ident.sym());

if !post {
compiler
Expand Down Expand Up @@ -184,7 +179,12 @@ impl ByteCompiler<'_> {
compiler.bytecode.emit_super(object.variable());
compiler.bytecode.emit_this(receiver.variable());

compiler.emit_get_property_by_name(dst, &receiver, &object, ident.sym());
compiler.emit_get_property_by_name(
dst,
Some(&receiver),
&object,
ident.sym(),
);

let value = compiler.register_allocator.alloc();
if increment {
Expand All @@ -193,7 +193,12 @@ impl ByteCompiler<'_> {
compiler.bytecode.emit_dec(value.variable(), dst.variable());
}

compiler.emit_set_property_by_name(&value, &receiver, &object, ident.sym());
compiler.emit_set_property_by_name(
&value,
Some(&receiver),
&object,
ident.sym(),
);
if !post {
compiler
.bytecode
Expand Down
70 changes: 49 additions & 21 deletions core/engine/src/bytecompiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ use boa_ast::{
use boa_gc::Gc;
use boa_interner::{Interner, Sym};
use boa_macros::js_str;
use boa_string::StaticJsStrings;
use rustc_hash::FxHashMap;
use thin_vec::ThinVec;

Expand Down Expand Up @@ -921,7 +922,7 @@ impl<'ctx> ByteCompiler<'ctx> {
fn emit_get_property_by_name(
&mut self,
dst: &Register,
receiver: &Register,
receiver: Option<&Register>,
value: &Register,
ident: Sym,
) {
Expand All @@ -933,18 +934,32 @@ impl<'ctx> ByteCompiler<'ctx> {
};
self.ic.push(InlineCache::new(name.clone()));

self.bytecode.emit_get_property_by_name(
dst.variable(),
receiver.variable(),
value.variable(),
ic_index.into(),
);
if let Some(receiver) = receiver {
self.bytecode.emit_get_property_by_name_with_this(
dst.variable(),
receiver.variable(),
value.variable(),
ic_index.into(),
);
} else if name == &StaticJsStrings::LENGTH {
self.bytecode.emit_get_length_property(
dst.variable(),
value.variable(),
ic_index.into(),
);
} else {
self.bytecode.emit_get_property_by_name(
dst.variable(),
value.variable(),
ic_index.into(),
);
}
}

fn emit_set_property_by_name(
&mut self,
value: &Register,
receiver: &Register,
receiver: Option<&Register>,
object: &Register,
ident: Sym,
) {
Expand All @@ -956,12 +971,20 @@ impl<'ctx> ByteCompiler<'ctx> {
};
self.ic.push(InlineCache::new(name.clone()));

self.bytecode.emit_set_property_by_name(
value.variable(),
receiver.variable(),
object.variable(),
ic_index.into(),
);
if let Some(receiver) = receiver {
self.bytecode.emit_set_property_by_name_with_this(
value.variable(),
receiver.variable(),
object.variable(),
ic_index.into(),
);
} else {
self.bytecode.emit_set_property_by_name(
value.variable(),
object.variable(),
ic_index.into(),
);
}
}

fn emit_type_error(&mut self, message: &str) {
Expand Down Expand Up @@ -1119,7 +1142,7 @@ impl<'ctx> ByteCompiler<'ctx> {

match access.field() {
PropertyAccessField::Const(ident) => {
compiler.emit_get_property_by_name(dst, &object, &object, ident.sym());
compiler.emit_get_property_by_name(dst, None, &object, ident.sym());
}
PropertyAccessField::Expr(expr) => {
let key = compiler.register_allocator.alloc();
Expand Down Expand Up @@ -1157,7 +1180,12 @@ impl<'ctx> ByteCompiler<'ctx> {
compiler.bytecode.emit_this(receiver.variable());
match access.field() {
PropertyAccessField::Const(ident) => {
compiler.emit_get_property_by_name(dst, &receiver, &value, ident.sym());
compiler.emit_get_property_by_name(
dst,
Some(&receiver),
&value,
ident.sym(),
);
}
PropertyAccessField::Expr(expr) => {
let key = compiler.register_allocator.alloc();
Expand Down Expand Up @@ -1222,7 +1250,7 @@ impl<'ctx> ByteCompiler<'ctx> {
let object = self.register_allocator.alloc();
self.compile_expr(access.target(), &object);
let value = expr_fn(self);
self.emit_set_property_by_name(value, &object, &object, name.sym());
self.emit_set_property_by_name(value, None, &object, name.sym());
self.register_allocator.dealloc(object);
}
PropertyAccessField::Expr(expr) => {
Expand Down Expand Up @@ -1271,7 +1299,7 @@ impl<'ctx> ByteCompiler<'ctx> {

let value = expr_fn(self);

self.emit_set_property_by_name(value, &receiver, &object, name.sym());
self.emit_set_property_by_name(value, Some(&receiver), &object, name.sym());

self.register_allocator.dealloc(receiver);
self.register_allocator.dealloc(object);
Expand Down Expand Up @@ -1393,7 +1421,7 @@ impl<'ctx> ByteCompiler<'ctx> {

match access.field() {
PropertyAccessField::Const(ident) => {
self.emit_get_property_by_name(dst, this, this, ident.sym());
self.emit_get_property_by_name(dst, None, this, ident.sym());
}
PropertyAccessField::Expr(field) => {
let key = self.register_allocator.alloc();
Expand Down Expand Up @@ -1422,7 +1450,7 @@ impl<'ctx> ByteCompiler<'ctx> {

match access.field() {
PropertyAccessField::Const(ident) => {
self.emit_get_property_by_name(dst, this, &object, ident.sym());
self.emit_get_property_by_name(dst, Some(this), &object, ident.sym());
}
PropertyAccessField::Expr(expr) => {
let key = self.register_allocator.alloc();
Expand Down Expand Up @@ -1525,7 +1553,7 @@ impl<'ctx> ByteCompiler<'ctx> {
self.bytecode.emit_move(this.variable(), value.variable());
match field {
PropertyAccessField::Const(name) => {
self.emit_get_property_by_name(value, value, value, name.sym());
self.emit_get_property_by_name(value, None, value, name.sym());
}
PropertyAccessField::Expr(expr) => {
let key = self.register_allocator.alloc();
Expand Down
17 changes: 17 additions & 0 deletions core/engine/src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,23 @@ impl JsValue {
}
}

pub(crate) fn base_class(&self, context: &Context) -> JsResult<JsObject> {
let constructors = context.intrinsics().constructors();
match self.variant() {
JsVariant::Undefined | JsVariant::Null => Err(JsNativeError::typ()
.with_message("cannot convert 'null' or 'undefined' to object")
.into()),
JsVariant::Boolean(_) => Ok(constructors.boolean().prototype()),
JsVariant::Integer32(_) | JsVariant::Float64(_) => {
Ok(constructors.number().prototype())
}
JsVariant::String(_) => Ok(constructors.string().prototype()),
JsVariant::Symbol(_) => Ok(constructors.symbol().prototype()),
JsVariant::BigInt(_) => Ok(constructors.bigint().prototype()),
JsVariant::Object(object) => Ok(object.clone()),
}
}

/// Converts the value to a `PropertyKey`, that can be used as a key for properties.
///
/// See <https://tc39.es/ecma262/#sec-topropertykey>
Expand Down
39 changes: 35 additions & 4 deletions core/engine/src/vm/code_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,30 @@ impl CodeBlock {
Instruction::DeletePropertyByName { object, name_index } => {
format!("object:{object}, name_index:{name_index}")
}
Instruction::GetLengthProperty {
dst,
value,
ic_index,
} => {
let ic = &self.ic[u32::from(*ic_index) as usize];
format!(
"dst:{dst}, value:{value}, shape:0x{:x}]",
ic.shape.borrow().to_addr_usize(),
)
}
Instruction::GetPropertyByName {
dst,
value,
ic_index,
} => {
let ic = &self.ic[u32::from(*ic_index) as usize];
format!(
"dst:{dst}, value:{value}, ic:[name:{}, shape:0x{:x}]",
ic.name.to_std_string_escaped(),
ic.shape.borrow().to_addr_usize(),
)
}
Instruction::GetPropertyByNameWithThis {
dst,
receiver,
value,
Expand All @@ -640,6 +663,17 @@ impl CodeBlock {
)
}
Instruction::SetPropertyByName {
value,
object,
ic_index,
} => {
let ic = &self.ic[u32::from(*ic_index) as usize];
format!(
"object:{object}, value:{value}, ic:shape:0x{:x}",
ic.shape.borrow().to_addr_usize(),
)
}
Instruction::SetPropertyByNameWithThis {
value,
receiver,
object,
Expand Down Expand Up @@ -901,10 +935,7 @@ impl CodeBlock {
| Instruction::Reserved57
| Instruction::Reserved58
| Instruction::Reserved59
| Instruction::Reserved60
| Instruction::Reserved61
| Instruction::Reserved62
| Instruction::Reserved63 => unreachable!("Reserved opcodes are unreachable"),
| Instruction::Reserved60 => unreachable!("Reserved opcodes are unreachable"),
}
}
}
Expand Down
Loading
Loading