From b4dc7f9c044eba4c8a1fd6c14c58ff70a785a53e Mon Sep 17 00:00:00 2001 From: HalidOdat Date: Sat, 22 Aug 2020 03:36:51 +0200 Subject: [PATCH 1/4] Context feature - Move `Console` to `Context` - Change `Context::global()` to `Context::global_object()` - Remove some `use std::borrow::Borrow` - Add some pub exports - Add `Context::eval()` - Deprecate forward_val, forward, exec - Make boa_cli use Context::eval() - Deprecated forward forward_val and exec --- boa/benches/exec.rs | 73 ++-- boa/examples/classes.rs | 22 +- boa/src/builtins/array/mod.rs | 111 ++---- boa/src/builtins/array/tests.rs | 88 ++--- boa/src/builtins/bigint/conversions.rs | 4 +- boa/src/builtins/bigint/mod.rs | 20 +- boa/src/builtins/bigint/tests.rs | 61 ++- boa/src/builtins/boolean/mod.rs | 14 +- boa/src/builtins/boolean/tests.rs | 11 +- boa/src/builtins/console/mod.rs | 40 +- boa/src/builtins/console/tests.rs | 24 +- boa/src/builtins/date/mod.rs | 26 +- boa/src/builtins/date/tests.rs | 183 ++++----- boa/src/builtins/error/mod.rs | 10 +- boa/src/builtins/error/range.rs | 10 +- boa/src/builtins/error/reference.rs | 10 +- boa/src/builtins/error/syntax.rs | 10 +- boa/src/builtins/error/type.rs | 10 +- boa/src/builtins/function/mod.rs | 19 +- boa/src/builtins/function/tests.rs | 9 +- boa/src/builtins/global_this/mod.rs | 6 +- boa/src/builtins/infinity/mod.rs | 4 +- boa/src/builtins/json/mod.rs | 12 +- boa/src/builtins/json/tests.rs | 72 ++-- boa/src/builtins/map/mod.rs | 22 +- boa/src/builtins/map/tests.rs | 44 +-- boa/src/builtins/math/mod.rs | 78 ++-- boa/src/builtins/math/tests.rs | 101 ++--- boa/src/builtins/mod.rs | 6 +- boa/src/builtins/nan/mod.rs | 4 +- boa/src/builtins/number/mod.rs | 58 +-- boa/src/builtins/number/tests.rs | 119 ++---- boa/src/builtins/object/gcobject.rs | 14 +- boa/src/builtins/object/mod.rs | 49 ++- boa/src/builtins/object/tests.rs | 27 +- boa/src/builtins/regexp/mod.rs | 32 +- boa/src/builtins/regexp/tests.rs | 16 +- boa/src/builtins/string/mod.rs | 81 ++-- boa/src/builtins/string/tests.rs | 113 ++---- boa/src/builtins/symbol/mod.rs | 12 +- boa/src/builtins/symbol/tests.rs | 11 +- boa/src/builtins/undefined/mod.rs | 4 +- boa/src/builtins/value/equality.rs | 4 +- boa/src/builtins/value/mod.rs | 55 ++- boa/src/builtins/value/operations.rs | 38 +- boa/src/builtins/value/tests.rs | 260 +++++-------- boa/src/context.rs | 496 +++++++++++++++++++++++++ boa/src/exec/array/mod.rs | 4 +- boa/src/exec/block/mod.rs | 6 +- boa/src/exec/break_node/mod.rs | 14 +- boa/src/exec/break_node/tests.rs | 9 +- boa/src/exec/call/mod.rs | 8 +- boa/src/exec/conditional/mod.rs | 11 +- boa/src/exec/declaration/mod.rs | 14 +- boa/src/exec/exception.rs | 96 ----- boa/src/exec/field/mod.rs | 6 +- boa/src/exec/identifier/mod.rs | 4 +- boa/src/exec/iteration/mod.rs | 53 ++- boa/src/exec/mod.rs | 345 +---------------- boa/src/exec/new/mod.rs | 4 +- boa/src/exec/object/mod.rs | 10 +- boa/src/exec/operator/mod.rs | 17 +- boa/src/exec/return_smt/mod.rs | 8 +- boa/src/exec/spread/mod.rs | 4 +- boa/src/exec/statement_list.rs | 10 +- boa/src/exec/switch/mod.rs | 20 +- boa/src/exec/tests.rs | 75 ++-- boa/src/exec/throw/mod.rs | 4 +- boa/src/exec/try_node/mod.rs | 4 +- boa/src/lib.rs | 50 +-- boa/src/syntax/mod.rs | 3 + boa_cli/src/main.rs | 10 +- boa_wasm/src/lib.rs | 5 +- 73 files changed, 1452 insertions(+), 1835 deletions(-) create mode 100644 boa/src/context.rs delete mode 100644 boa/src/exec/exception.rs diff --git a/boa/benches/exec.rs b/boa/benches/exec.rs index c768fb8707b..1397e0304be 100644 --- a/boa/benches/exec.rs +++ b/boa/benches/exec.rs @@ -1,6 +1,11 @@ //! Benchmarks of the whole execution engine in Boa. -use boa::{exec::Interpreter, realm::Realm, Executable, Parser}; +use boa::{ + context::Context, + exec::Executable, + realm::Realm, + syntax::{Lexer, Parser}, +}; use criterion::{black_box, criterion_group, criterion_main, Criterion}; #[cfg(all(target_arch = "x86_64", target_os = "linux", target_env = "gnu"))] @@ -18,8 +23,7 @@ static SYMBOL_CREATION: &str = include_str!("bench_scripts/symbol_creation.js"); fn symbol_creation(c: &mut Criterion) { // Create new Realm and interpreter. - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); // Parse the AST nodes. let nodes = Parser::new(SYMBOL_CREATION.as_bytes()).parse_all().unwrap(); @@ -34,8 +38,7 @@ static FOR_LOOP: &str = include_str!("bench_scripts/for_loop.js"); fn for_loop_execution(c: &mut Criterion) { // Create new Realm and interpreter. - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); // Parse the AST nodes. let nodes = Parser::new(FOR_LOOP.as_bytes()).parse_all().unwrap(); @@ -50,8 +53,7 @@ static FIBONACCI: &str = include_str!("bench_scripts/fibonacci.js"); fn fibonacci(c: &mut Criterion) { // Create new Realm and interpreter. - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); // Parse the AST nodes. let nodes = Parser::new(FIBONACCI.as_bytes()).parse_all().unwrap(); @@ -66,8 +68,7 @@ static OBJECT_CREATION: &str = include_str!("bench_scripts/object_creation.js"); fn object_creation(c: &mut Criterion) { // Create new Realm and interpreter. - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); // Parse the AST nodes. let nodes = Parser::new(OBJECT_CREATION.as_bytes()).parse_all().unwrap(); @@ -82,8 +83,7 @@ static OBJECT_PROP_ACCESS_CONST: &str = include_str!("bench_scripts/object_prop_ fn object_prop_access_const(c: &mut Criterion) { // Create new Realm and interpreter. - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); // Parse the AST nodes. let nodes = Parser::new(OBJECT_PROP_ACCESS_CONST.as_bytes()) @@ -100,8 +100,7 @@ static OBJECT_PROP_ACCESS_DYN: &str = include_str!("bench_scripts/object_prop_ac fn object_prop_access_dyn(c: &mut Criterion) { // Create new Realm and interpreter. - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); // Parse the AST nodes. let nodes = Parser::new(OBJECT_PROP_ACCESS_DYN.as_bytes()) @@ -118,8 +117,7 @@ static REGEXP_LITERAL_CREATION: &str = include_str!("bench_scripts/regexp_litera fn regexp_literal_creation(c: &mut Criterion) { // Create new Realm and interpreter. - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); // Parse the AST nodes. let nodes = Parser::new(REGEXP_LITERAL_CREATION.as_bytes()) @@ -136,8 +134,7 @@ static REGEXP_CREATION: &str = include_str!("bench_scripts/regexp_creation.js"); fn regexp_creation(c: &mut Criterion) { // Create new Realm and interpreter. - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); // Parse the AST nodes. let nodes = Parser::new(REGEXP_CREATION.as_bytes()).parse_all().unwrap(); @@ -152,8 +149,7 @@ static REGEXP_LITERAL: &str = include_str!("bench_scripts/regexp_literal.js"); fn regexp_literal(c: &mut Criterion) { // Create new Realm and interpreter. - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); // Parse the AST nodes. let nodes = Parser::new(REGEXP_LITERAL.as_bytes()).parse_all().unwrap(); @@ -168,8 +164,7 @@ static REGEXP: &str = include_str!("bench_scripts/regexp.js"); fn regexp(c: &mut Criterion) { // Create new Realm and interpreter. - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); // Parse the AST nodes. let nodes = Parser::new(REGEXP.as_bytes()).parse_all().unwrap(); @@ -183,8 +178,7 @@ fn regexp(c: &mut Criterion) { static ARRAY_ACCESS: &str = include_str!("bench_scripts/array_access.js"); fn array_access(c: &mut Criterion) { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let nodes = Parser::new(ARRAY_ACCESS.as_bytes()).parse_all().unwrap(); @@ -196,8 +190,7 @@ fn array_access(c: &mut Criterion) { static ARRAY_CREATE: &str = include_str!("bench_scripts/array_create.js"); fn array_creation(c: &mut Criterion) { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let nodes = Parser::new(ARRAY_CREATE.as_bytes()).parse_all().unwrap(); @@ -209,8 +202,7 @@ fn array_creation(c: &mut Criterion) { static ARRAY_POP: &str = include_str!("bench_scripts/array_pop.js"); fn array_pop(c: &mut Criterion) { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let nodes = Parser::new(ARRAY_POP.as_bytes()).parse_all().unwrap(); @@ -222,8 +214,7 @@ fn array_pop(c: &mut Criterion) { static STRING_CONCAT: &str = include_str!("bench_scripts/string_concat.js"); fn string_concat(c: &mut Criterion) { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let nodes = Parser::new(STRING_CONCAT.as_bytes()).parse_all().unwrap(); @@ -235,8 +226,7 @@ fn string_concat(c: &mut Criterion) { static STRING_COMPARE: &str = include_str!("bench_scripts/string_compare.js"); fn string_compare(c: &mut Criterion) { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let nodes = Parser::new(STRING_COMPARE.as_bytes()).parse_all().unwrap(); @@ -248,8 +238,7 @@ fn string_compare(c: &mut Criterion) { static STRING_COPY: &str = include_str!("bench_scripts/string_copy.js"); fn string_copy(c: &mut Criterion) { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let nodes = Parser::new(STRING_COPY.as_bytes()).parse_all().unwrap(); @@ -261,8 +250,7 @@ fn string_copy(c: &mut Criterion) { static NUMBER_OBJECT_ACCESS: &str = include_str!("bench_scripts/number_object_access.js"); fn number_object_access(c: &mut Criterion) { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let nodes = Parser::new(NUMBER_OBJECT_ACCESS.as_bytes()) .parse_all() @@ -276,8 +264,7 @@ fn number_object_access(c: &mut Criterion) { static BOOLEAN_OBJECT_ACCESS: &str = include_str!("bench_scripts/boolean_object_access.js"); fn boolean_object_access(c: &mut Criterion) { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let nodes = Parser::new(BOOLEAN_OBJECT_ACCESS.as_bytes()) .parse_all() @@ -291,8 +278,7 @@ fn boolean_object_access(c: &mut Criterion) { static STRING_OBJECT_ACCESS: &str = include_str!("bench_scripts/string_object_access.js"); fn string_object_access(c: &mut Criterion) { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let nodes = Parser::new(STRING_OBJECT_ACCESS.as_bytes()) .parse_all() @@ -306,8 +292,7 @@ fn string_object_access(c: &mut Criterion) { static ARITHMETIC_OPERATIONS: &str = include_str!("bench_scripts/arithmetic_operations.js"); fn arithmetic_operations(c: &mut Criterion) { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let nodes = Parser::new(ARITHMETIC_OPERATIONS.as_bytes()) .parse_all() @@ -321,8 +306,7 @@ fn arithmetic_operations(c: &mut Criterion) { static CLEAN_JS: &str = include_str!("bench_scripts/clean_js.js"); fn clean_js(c: &mut Criterion) { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let nodes = Parser::new(CLEAN_JS.as_bytes()).parse_all().unwrap(); c.bench_function("Clean js (Execution)", move |b| { b.iter(|| black_box(&nodes).run(&mut engine).unwrap()) @@ -332,8 +316,7 @@ fn clean_js(c: &mut Criterion) { static MINI_JS: &str = include_str!("bench_scripts/mini_js.js"); fn mini_js(c: &mut Criterion) { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let nodes = Parser::new(MINI_JS.as_bytes()).parse_all().unwrap(); c.bench_function("Mini js (Execution)", move |b| { b.iter(|| black_box(&nodes).run(&mut engine).unwrap()) diff --git a/boa/examples/classes.rs b/boa/examples/classes.rs index 1ddb74ccd00..e0bd322f138 100644 --- a/boa/examples/classes.rs +++ b/boa/examples/classes.rs @@ -4,10 +4,7 @@ use boa::{ property::Attribute, value::Value, }, - exec::Interpreter, - forward_val, - realm::Realm, - Result, + Context, Result, }; use gc::{Finalize, Trace}; @@ -33,7 +30,7 @@ struct Person { // or any function that matches that signature. impl Person { /// This function says hello - fn say_hello(this: &Value, _: &[Value], ctx: &mut Interpreter) -> Result { + fn say_hello(this: &Value, _: &[Value], ctx: &mut Context) -> Result { // We check if this is an object. if let Some(object) = this.as_object() { // If it is we downcast the type to type `Person`. @@ -64,7 +61,7 @@ impl Class for Person { const LENGTH: usize = 2; // This is what is called when we do `new Person()` - fn constructor(_this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + fn constructor(_this: &Value, args: &[Value], ctx: &mut Context) -> Result { // we get the first arguemnt of undefined if the first one is unavalable and call `to_string`. // // This is equivalent to `String(arg)`. @@ -122,15 +119,14 @@ impl Class for Person { } fn main() { - let realm = Realm::create(); - let mut context = Interpreter::new(realm); + let mut context = Context::new(); // we register the global class `Person`. context.register_global_class::().unwrap(); - forward_val( - &mut context, - r" + context + .eval( + r" let person = new Person('John', 19); person.sayHello(); @@ -145,6 +141,6 @@ fn main() { console.log(person.inheritedProperty); console.log(Person.prototype.inheritedProperty === person.inheritedProperty); ", - ) - .unwrap(); + ) + .unwrap(); } diff --git a/boa/src/builtins/array/mod.rs b/boa/src/builtins/array/mod.rs index 58596a94593..ffd5c0c5e0d 100644 --- a/boa/src/builtins/array/mod.rs +++ b/boa/src/builtins/array/mod.rs @@ -19,13 +19,10 @@ use crate::{ property::{Attribute, Property}, value::{same_value_zero, Value}, }, - exec::Interpreter, + context::Context, BoaProfiler, Result, }; -use std::{ - borrow::Borrow, - cmp::{max, min}, -}; +use std::cmp::{max, min}; /// JavaScript `Array` built-in implementation. #[derive(Debug, Clone, Copy)] @@ -39,7 +36,7 @@ impl Array { pub(crate) const LENGTH: usize = 1; /// Creates a new `Array` instance. - pub(crate) fn new_array(interpreter: &Interpreter) -> Result { + pub(crate) fn new_array(interpreter: &Context) -> Result { let array = Value::new_object(Some( &interpreter .realm() @@ -54,10 +51,9 @@ impl Array { .environment .get_binding_value("Array") .expect("Array was not initialized") - .borrow() .get_field(PROTOTYPE), ); - array.borrow().set_field("length", Value::from(0)); + array.set_field("length", Value::from(0)); Ok(array) } @@ -106,12 +102,12 @@ impl Array { } /// Create a new array - pub(crate) fn make_array(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn make_array(this: &Value, args: &[Value], ctx: &mut Context) -> Result { // Make a new Object which will internally represent the Array (mapping // between indices and values): this creates an Object with no prototype // Set Prototype - let prototype = ctx.realm.global_obj.get_field("Array").get_field(PROTOTYPE); + let prototype = ctx.global_object().get_field("Array").get_field(PROTOTYPE); this.as_object_mut() .expect("this should be an array object") @@ -165,7 +161,7 @@ impl Array { pub(crate) fn is_array( _this: &Value, args: &[Value], - _interpreter: &mut Interpreter, + _interpreter: &mut Context, ) -> Result { match args.get(0).and_then(|x| x.as_object()) { Some(object) => Ok(Value::from(object.is_array())), @@ -185,7 +181,7 @@ impl Array { /// /// [spec]: https://tc39.es/ecma262/#sec-array.prototype.concat /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat - pub(crate) fn concat(this: &Value, args: &[Value], _: &mut Interpreter) -> Result { + pub(crate) fn concat(this: &Value, args: &[Value], _: &mut Context) -> Result { if args.is_empty() { // If concat is called with no arguments, it returns the original array return Ok(this.clone()); @@ -222,7 +218,7 @@ impl Array { /// /// [spec]: https://tc39.es/ecma262/#sec-array.prototype.push /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push - pub(crate) fn push(this: &Value, args: &[Value], _: &mut Interpreter) -> Result { + pub(crate) fn push(this: &Value, args: &[Value], _: &mut Context) -> Result { let new_array = Self::add_to_array_object(this, args)?; Ok(new_array.get_field("length")) } @@ -237,7 +233,7 @@ impl Array { /// /// [spec]: https://tc39.es/ecma262/#sec-array.prototype.pop /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop - pub(crate) fn pop(this: &Value, _: &[Value], _: &mut Interpreter) -> Result { + pub(crate) fn pop(this: &Value, _: &[Value], _: &mut Context) -> Result { let curr_length = this.get_field("length").as_number().unwrap() as i32; if curr_length < 1 { @@ -260,11 +256,7 @@ impl Array { /// /// [spec]: https://tc39.es/ecma262/#sec-array.prototype.foreach /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach - pub(crate) fn for_each( - this: &Value, - args: &[Value], - interpreter: &mut Interpreter, - ) -> Result { + pub(crate) fn for_each(this: &Value, args: &[Value], ctx: &mut Context) -> Result { if args.is_empty() { return Err(Value::from("Missing argument for Array.prototype.forEach")); } @@ -278,7 +270,7 @@ impl Array { let element = this.get_field(i); let arguments = [element, Value::from(i), this.clone()]; - interpreter.call(callback_arg, &this_arg, &arguments)?; + ctx.call(callback_arg, &this_arg, &arguments)?; } Ok(Value::undefined()) @@ -296,7 +288,7 @@ impl Array { /// /// [spec]: https://tc39.es/ecma262/#sec-array.prototype.join /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join - pub(crate) fn join(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn join(this: &Value, args: &[Value], ctx: &mut Context) -> Result { let separator = if args.is_empty() { String::from(",") } else { @@ -329,7 +321,7 @@ impl Array { /// [spec]: https://tc39.es/ecma262/#sec-array.prototype.tostring /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toString #[allow(clippy::wrong_self_convention)] - pub(crate) fn to_string(this: &Value, _args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn to_string(this: &Value, _args: &[Value], ctx: &mut Context) -> Result { let method_name = "join"; let mut arguments = vec![Value::from(",")]; // 2. @@ -337,8 +329,7 @@ impl Array { // 3. if !method.is_function() { method = ctx - .realm - .global_obj + .global_object() .get_field("Object") .get_field(PROTOTYPE) .get_field("toString"); @@ -369,7 +360,7 @@ impl Array { /// [spec]: https://tc39.es/ecma262/#sec-array.prototype.reverse /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse #[allow(clippy::else_if_without_else)] - pub(crate) fn reverse(this: &Value, _: &[Value], _: &mut Interpreter) -> Result { + pub(crate) fn reverse(this: &Value, _: &[Value], _: &mut Context) -> Result { let len = this.get_field("length").as_number().unwrap() as i32; let middle: i32 = len.wrapping_div(2); @@ -408,7 +399,7 @@ impl Array { /// /// [spec]: https://tc39.es/ecma262/#sec-array.prototype.shift /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift - pub(crate) fn shift(this: &Value, _: &[Value], _: &mut Interpreter) -> Result { + pub(crate) fn shift(this: &Value, _: &[Value], _: &mut Context) -> Result { let len = this.get_field("length").as_number().unwrap() as i32; if len == 0 { @@ -449,7 +440,7 @@ impl Array { /// /// [spec]: https://tc39.es/ecma262/#sec-array.prototype.unshift /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift - pub(crate) fn unshift(this: &Value, args: &[Value], _: &mut Interpreter) -> Result { + pub(crate) fn unshift(this: &Value, args: &[Value], _: &mut Context) -> Result { let len = this.get_field("length").as_number().unwrap() as i32; let arg_c: i32 = args.len() as i32; @@ -494,11 +485,7 @@ impl Array { /// /// [spec]: https://tc39.es/ecma262/#sec-array.prototype.every /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every - pub(crate) fn every( - this: &Value, - args: &[Value], - interpreter: &mut Interpreter, - ) -> Result { + pub(crate) fn every(this: &Value, args: &[Value], interpreter: &mut Context) -> Result { if args.is_empty() { return Err(Value::from( "missing callback when calling function Array.prototype.every", @@ -540,11 +527,7 @@ impl Array { /// /// [spec]: https://tc39.es/ecma262/#sec-array.prototype.map /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map - pub(crate) fn map( - this: &Value, - args: &[Value], - interpreter: &mut Interpreter, - ) -> Result { + pub(crate) fn map(this: &Value, args: &[Value], interpreter: &mut Context) -> Result { if args.is_empty() { return Err(Value::from( "missing argument 0 when calling function Array.prototype.map", @@ -591,7 +574,7 @@ impl Array { /// /// [spec]: https://tc39.es/ecma262/#sec-array.prototype.indexof /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf - pub(crate) fn index_of(this: &Value, args: &[Value], _: &mut Interpreter) -> Result { + pub(crate) fn index_of(this: &Value, args: &[Value], _: &mut Context) -> Result { // If no arguments, return -1. Not described in spec, but is what chrome does. if args.is_empty() { return Ok(Value::from(-1)); @@ -644,11 +627,7 @@ impl Array { /// /// [spec]: https://tc39.es/ecma262/#sec-array.prototype.lastindexof /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf - pub(crate) fn last_index_of( - this: &Value, - args: &[Value], - _: &mut Interpreter, - ) -> Result { + pub(crate) fn last_index_of(this: &Value, args: &[Value], _: &mut Context) -> Result { // If no arguments, return -1. Not described in spec, but is what chrome does. if args.is_empty() { return Ok(Value::from(-1)); @@ -695,11 +674,7 @@ impl Array { /// /// [spec]: https://tc39.es/ecma262/#sec-array.prototype.find /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find - pub(crate) fn find( - this: &Value, - args: &[Value], - interpreter: &mut Interpreter, - ) -> Result { + pub(crate) fn find(this: &Value, args: &[Value], interpreter: &mut Context) -> Result { if args.is_empty() { return Err(Value::from( "missing callback when calling function Array.prototype.find", @@ -734,7 +709,7 @@ impl Array { pub(crate) fn find_index( this: &Value, args: &[Value], - interpreter: &mut Interpreter, + interpreter: &mut Context, ) -> Result { if args.is_empty() { return Err(Value::from( @@ -773,7 +748,7 @@ impl Array { /// /// [spec]: https://tc39.es/ecma262/#sec-array.prototype.fill /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill - pub(crate) fn fill(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn fill(this: &Value, args: &[Value], ctx: &mut Context) -> Result { let len: i32 = this.get_field("length").as_number().unwrap() as i32; let default_value = Value::undefined(); @@ -813,11 +788,7 @@ impl Array { /// /// [spec]: https://tc39.es/ecma262/#sec-array.prototype.includes /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes - pub(crate) fn includes_value( - this: &Value, - args: &[Value], - _: &mut Interpreter, - ) -> Result { + pub(crate) fn includes_value(this: &Value, args: &[Value], _: &mut Context) -> Result { let search_element = args.get(0).cloned().unwrap_or_else(Value::undefined); let length = this.get_field("length").as_number().unwrap() as i32; @@ -847,11 +818,7 @@ impl Array { /// /// [spec]: https://tc39.es/ecma262/#sec-array.prototype.slice /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice - pub(crate) fn slice( - this: &Value, - args: &[Value], - interpreter: &mut Interpreter, - ) -> Result { + pub(crate) fn slice(this: &Value, args: &[Value], interpreter: &mut Context) -> Result { let new_array = Self::new_array(interpreter)?; let len = this.get_field("length").as_number().unwrap() as i32; @@ -896,11 +863,7 @@ impl Array { /// /// [spec]: https://tc39.es/ecma262/#sec-array.prototype.filter /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter - pub(crate) fn filter( - this: &Value, - args: &[Value], - interpreter: &mut Interpreter, - ) -> Result { + pub(crate) fn filter(this: &Value, args: &[Value], interpreter: &mut Context) -> Result { if args.is_empty() { return Err(Value::from( "missing argument 0 when calling function Array.prototype.filter", @@ -950,11 +913,7 @@ impl Array { /// /// [spec]: https://tc39.es/ecma262/#sec-array.prototype.some /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some - pub(crate) fn some( - this: &Value, - args: &[Value], - interpreter: &mut Interpreter, - ) -> Result { + pub(crate) fn some(this: &Value, args: &[Value], interpreter: &mut Context) -> Result { if args.is_empty() { return Err(Value::from( "missing callback when calling function Array.prototype.some", @@ -997,11 +956,7 @@ impl Array { /// /// [spec]: https://tc39.es/ecma262/#sec-array.prototype.reduce /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce - pub(crate) fn reduce( - this: &Value, - args: &[Value], - interpreter: &mut Interpreter, - ) -> Result { + pub(crate) fn reduce(this: &Value, args: &[Value], interpreter: &mut Context) -> Result { let this = this.to_object(interpreter)?; let callback = match args.get(0) { Some(value) if value.is_function() => value, @@ -1062,7 +1017,7 @@ impl Array { pub(crate) fn reduce_right( this: &Value, args: &[Value], - interpreter: &mut Interpreter, + interpreter: &mut Context, ) -> Result { let this = this.to_object(interpreter)?; let callback = match args.get(0) { @@ -1135,8 +1090,8 @@ impl Array { /// Initialise the `Array` object on the global object. #[inline] - pub(crate) fn init(interpreter: &mut Interpreter) -> (&'static str, Value) { - let global = interpreter.global(); + pub(crate) fn init(interpreter: &mut Context) -> (&'static str, Value) { + let global = interpreter.global_object(); let _timer = BoaProfiler::global().start_event(Self::NAME, "init"); // Create prototype diff --git a/boa/src/builtins/array/tests.rs b/boa/src/builtins/array/tests.rs index 378ef0d9ad9..00aba0a96a6 100644 --- a/boa/src/builtins/array/tests.rs +++ b/boa/src/builtins/array/tests.rs @@ -1,9 +1,8 @@ -use crate::{exec::Interpreter, forward, realm::Realm}; +use crate::{context::Context, forward}; #[test] fn is_array() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var empty = []; var new_arr = new Array(); @@ -44,8 +43,7 @@ fn is_array() { #[ignore] fn concat() { //TODO: array display formatter - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var empty = new Array(); var one = new Array(1); @@ -53,22 +51,21 @@ fn concat() { eprintln!("{}", forward(&mut engine, init)); // Empty ++ Empty let ee = forward(&mut engine, "empty.concat(empty)"); - assert_eq!(ee, String::from("[]")); + assert_eq!(ee, "[]"); // Empty ++ NonEmpty let en = forward(&mut engine, "empty.concat(one)"); - assert_eq!(en, String::from("[a]")); + assert_eq!(en, "[a]"); // NonEmpty ++ Empty let ne = forward(&mut engine, "one.concat(empty)"); - assert_eq!(ne, String::from("a.b.c")); + assert_eq!(ne, "a.b.c"); // NonEmpty ++ NonEmpty let nn = forward(&mut engine, "one.concat(one)"); - assert_eq!(nn, String::from("a.b.c")); + assert_eq!(nn, "a.b.c"); } #[test] fn join() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var empty = [ ]; var one = ["a"]; @@ -88,8 +85,7 @@ fn join() { #[test] fn to_string() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var empty = [ ]; var one = ["a"]; @@ -109,8 +105,7 @@ fn to_string() { #[test] fn every() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); // taken from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every let init = r#" var empty = []; @@ -154,8 +149,7 @@ fn every() { #[test] fn find() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" function comp(a) { return a == "a"; @@ -169,8 +163,7 @@ fn find() { #[test] fn find_index() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let code = r#" function comp(item) { @@ -195,8 +188,7 @@ fn find_index() { #[test] fn push() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var arr = [1, 2]; "#; @@ -210,8 +202,7 @@ fn push() { #[test] fn pop() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var empty = [ ]; var one = [1]; @@ -232,8 +223,7 @@ fn pop() { #[test] fn shift() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var empty = [ ]; var one = [1]; @@ -254,8 +244,7 @@ fn shift() { #[test] fn unshift() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var arr = [3, 4]; "#; @@ -269,8 +258,7 @@ fn unshift() { #[test] fn reverse() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var arr = [1, 2]; var reversed = arr.reverse(); @@ -284,8 +272,7 @@ fn reverse() { #[test] fn index_of() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var empty = [ ]; var one = ["a"]; @@ -348,8 +335,7 @@ fn index_of() { #[test] fn last_index_of() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var empty = [ ]; var one = ["a"]; @@ -412,8 +398,7 @@ fn last_index_of() { #[test] fn fill_obj_ref() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); // test object reference forward(&mut engine, "a = (new Array(3)).fill({});"); @@ -423,8 +408,7 @@ fn fill_obj_ref() { #[test] fn fill() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); forward(&mut engine, "var a = [1, 2, 3];"); assert_eq!( @@ -519,8 +503,7 @@ fn fill() { #[test] fn includes_value() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var empty = [ ]; var one = ["a"]; @@ -558,8 +541,7 @@ fn includes_value() { #[test] fn map() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let js = r#" var empty = []; @@ -622,8 +604,7 @@ fn map() { #[test] fn slice() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var empty = [ ].slice(); var one = ["a"].slice(); @@ -646,8 +627,7 @@ fn slice() { #[test] fn for_each() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var a = [2, 3, 4, 5]; var sum = 0; @@ -669,8 +649,7 @@ fn for_each() { #[test] fn for_each_push_value() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var a = [1, 2, 3, 4]; function callingCallback(item, index, list) { @@ -690,8 +669,7 @@ fn for_each_push_value() { #[test] fn filter() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let js = r#" var empty = []; @@ -760,8 +738,7 @@ fn filter() { #[test] fn some() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var empty = []; @@ -809,8 +786,7 @@ fn some() { #[test] fn reduce() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var arr = [1, 2, 3, 4]; @@ -919,8 +895,7 @@ fn reduce() { #[test] fn reduce_right() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var arr = [1, 2, 3, 4]; @@ -1042,8 +1017,7 @@ fn reduce_right() { #[test] fn call_array_constructor_with_one_argument() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var empty = new Array(0); diff --git a/boa/src/builtins/bigint/conversions.rs b/boa/src/builtins/bigint/conversions.rs index 572e2899769..81752f0f0c6 100644 --- a/boa/src/builtins/bigint/conversions.rs +++ b/boa/src/builtins/bigint/conversions.rs @@ -2,7 +2,7 @@ use super::BigInt; use crate::{ builtins::{Number, Value}, - exec::Interpreter, + context::Context, }; use num_traits::cast::{FromPrimitive, ToPrimitive}; @@ -17,7 +17,7 @@ impl BigInt { /// /// [spec]: https://tc39.es/ecma262/#sec-stringtobigint #[inline] - pub(crate) fn from_string(string: &str, _ctx: &mut Interpreter) -> Result { + pub(crate) fn from_string(string: &str, _ctx: &mut Context) -> Result { if string.is_empty() { return Ok(BigInt::from(0)); } diff --git a/boa/src/builtins/bigint/mod.rs b/boa/src/builtins/bigint/mod.rs index e2e9ffe879b..44db6291db8 100644 --- a/boa/src/builtins/bigint/mod.rs +++ b/boa/src/builtins/bigint/mod.rs @@ -18,7 +18,7 @@ use crate::{ object::ObjectData, value::{RcBigInt, Value}, }, - exec::Interpreter, + context::Context, BoaProfiler, Result, }; @@ -61,7 +61,7 @@ impl BigInt { /// /// [spec]: https://tc39.es/ecma262/#sec-thisbigintvalue #[inline] - fn this_bigint_value(value: &Value, ctx: &mut Interpreter) -> Result { + fn this_bigint_value(value: &Value, ctx: &mut Context) -> Result { match value { // 1. If Type(value) is BigInt, return value. Value::BigInt(ref bigint) => return Ok(bigint.clone()), @@ -91,7 +91,7 @@ impl BigInt { /// /// [spec]: https://tc39.es/ecma262/#sec-bigint-objects /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/BigInt - pub(crate) fn make_bigint(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn make_bigint(_: &Value, args: &[Value], ctx: &mut Context) -> Result { let data = match args.get(0) { Some(ref value) => value.to_bigint(ctx)?, None => RcBigInt::from(Self::from(0)), @@ -110,7 +110,7 @@ impl BigInt { /// [spec]: https://tc39.es/ecma262/#sec-bigint.prototype.tostring /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/toString #[allow(clippy::wrong_self_convention)] - pub(crate) fn to_string(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn to_string(this: &Value, args: &[Value], ctx: &mut Context) -> Result { let radix = if !args.is_empty() { args[0].to_integer(ctx)? as i32 } else { @@ -135,7 +135,7 @@ impl BigInt { /// /// [spec]: https://tc39.es/ecma262/#sec-bigint.prototype.valueof /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/valueOf - pub(crate) fn value_of(this: &Value, _args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn value_of(this: &Value, _args: &[Value], ctx: &mut Context) -> Result { Ok(Value::from(Self::this_bigint_value(this, ctx)?)) } @@ -146,7 +146,7 @@ impl BigInt { /// [spec]: https://tc39.es/ecma262/#sec-bigint.asintn /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/asIntN #[allow(clippy::wrong_self_convention)] - pub(crate) fn as_int_n(_this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn as_int_n(_this: &Value, args: &[Value], ctx: &mut Context) -> Result { let (modulo, bits) = Self::calculate_as_uint_n(args, ctx)?; if bits > 0 && modulo >= BigInt::from(2).pow(&BigInt::from(bits as i64 - 1)) { @@ -165,7 +165,7 @@ impl BigInt { /// [spec]: https://tc39.es/ecma262/#sec-bigint.asuintn /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/asUintN #[allow(clippy::wrong_self_convention)] - pub(crate) fn as_uint_n(_this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn as_uint_n(_this: &Value, args: &[Value], ctx: &mut Context) -> Result { let (modulo, _) = Self::calculate_as_uint_n(args, ctx)?; Ok(Value::from(modulo)) @@ -176,7 +176,7 @@ impl BigInt { /// This function expects the same arguments as `as_uint_n` and wraps the value of a `BigInt`. /// Additionally to the wrapped unsigned value it returns the converted `bits` argument, so it /// can be reused from the `as_int_n` method. - fn calculate_as_uint_n(args: &[Value], ctx: &mut Interpreter) -> Result<(BigInt, u32)> { + fn calculate_as_uint_n(args: &[Value], ctx: &mut Context) -> Result<(BigInt, u32)> { use std::convert::TryFrom; let undefined_value = Value::undefined(); @@ -200,8 +200,8 @@ impl BigInt { /// Initialise the `BigInt` object on the global object. #[inline] - pub fn init(interpreter: &mut Interpreter) -> (&'static str, Value) { - let global = interpreter.global(); + pub fn init(interpreter: &mut Context) -> (&'static str, Value) { + let global = interpreter.global_object(); let _timer = BoaProfiler::global().start_event(Self::NAME, "init"); let prototype = Value::new_object(Some(global)); diff --git a/boa/src/builtins/bigint/tests.rs b/boa/src/builtins/bigint/tests.rs index d816fac806b..e190496ecbc 100644 --- a/boa/src/builtins/bigint/tests.rs +++ b/boa/src/builtins/bigint/tests.rs @@ -1,9 +1,8 @@ -use crate::{forward, Interpreter, Realm}; +use crate::{forward, Context}; #[test] fn equality() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(forward(&mut engine, "0n == 0n"), "true"); assert_eq!(forward(&mut engine, "1n == 0n"), "false"); @@ -56,8 +55,7 @@ fn equality() { #[test] fn bigint_function_conversion_from_integer() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(forward(&mut engine, "BigInt(1000)"), "1000n"); assert_eq!( @@ -72,8 +70,7 @@ fn bigint_function_conversion_from_integer() { #[test] fn bigint_function_conversion_from_rational() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(forward(&mut engine, "BigInt(0.0)"), "0n"); assert_eq!(forward(&mut engine, "BigInt(1.0)"), "1n"); @@ -82,8 +79,7 @@ fn bigint_function_conversion_from_rational() { #[test] fn bigint_function_conversion_from_rational_with_fractional_part() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let scenario = r#" try { @@ -100,8 +96,7 @@ fn bigint_function_conversion_from_rational_with_fractional_part() { #[test] fn bigint_function_conversion_from_null() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let scenario = r#" try { @@ -118,8 +113,7 @@ fn bigint_function_conversion_from_null() { #[test] fn bigint_function_conversion_from_undefined() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let scenario = r#" try { @@ -136,8 +130,7 @@ fn bigint_function_conversion_from_undefined() { #[test] fn bigint_function_conversion_from_string() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(forward(&mut engine, "BigInt('')"), "0n"); assert_eq!( @@ -152,24 +145,21 @@ fn bigint_function_conversion_from_string() { #[test] fn add() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(forward(&mut engine, "10000n + 1000n"), "11000n"); } #[test] fn sub() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(forward(&mut engine, "10000n - 1000n"), "9000n"); } #[test] fn mul() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!( forward(&mut engine, "123456789n * 102030n"), @@ -179,32 +169,28 @@ fn mul() { #[test] fn div() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(forward(&mut engine, "15000n / 50n"), "300n"); } #[test] fn div_with_truncation() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(forward(&mut engine, "15001n / 50n"), "300n"); } #[test] fn r#mod() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(forward(&mut engine, "15007n % 10n"), "7n"); } #[test] fn pow() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!( forward(&mut engine, "100n ** 10n"), @@ -214,8 +200,7 @@ fn pow() { #[test] fn to_string() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(forward(&mut engine, "1000n.toString()"), "\"1000\""); assert_eq!(forward(&mut engine, "1000n.toString(2)"), "\"1111101000\""); @@ -225,8 +210,7 @@ fn to_string() { #[test] fn as_int_n() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(forward(&mut engine, "BigInt.asIntN(0, 1n)"), "0n"); assert_eq!(forward(&mut engine, "BigInt.asIntN(1, 1n)"), "-1n"); @@ -286,8 +270,7 @@ fn as_int_n() { #[test] fn as_int_n_errors() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_throws(&mut engine, "BigInt.asIntN(-1, 0n)", "RangeError"); assert_throws(&mut engine, "BigInt.asIntN(-2.5, 0n)", "RangeError"); @@ -301,8 +284,7 @@ fn as_int_n_errors() { #[test] fn as_uint_n() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(forward(&mut engine, "BigInt.asUintN(0, -2n)"), "0n"); assert_eq!(forward(&mut engine, "BigInt.asUintN(0, -1n)"), "0n"); @@ -353,8 +335,7 @@ fn as_uint_n() { #[test] fn as_uint_n_errors() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_throws(&mut engine, "BigInt.asUintN(-1, 0n)", "RangeError"); assert_throws(&mut engine, "BigInt.asUintN(-2.5, 0n)", "RangeError"); @@ -366,7 +347,7 @@ fn as_uint_n_errors() { assert_throws(&mut engine, "BigInt.asUintN(0n, 0n)", "TypeError"); } -fn assert_throws(engine: &mut Interpreter, src: &str, error_type: &str) { +fn assert_throws(engine: &mut Context, src: &str, error_type: &str) { let result = forward(engine, src); assert!(result.contains(error_type)); } diff --git a/boa/src/builtins/boolean/mod.rs b/boa/src/builtins/boolean/mod.rs index 2d907cddaec..719ad7321a8 100644 --- a/boa/src/builtins/boolean/mod.rs +++ b/boa/src/builtins/boolean/mod.rs @@ -15,7 +15,7 @@ mod tests; use super::function::{make_builtin_fn, make_constructor_fn}; use crate::{ builtins::{object::ObjectData, value::Value}, - exec::Interpreter, + context::Context, BoaProfiler, Result, }; @@ -36,7 +36,7 @@ impl Boolean { /// - [ECMAScript reference][spec] /// /// [spec]: https://tc39.es/ecma262/#sec-thisbooleanvalue - fn this_boolean_value(value: &Value, ctx: &mut Interpreter) -> Result { + fn this_boolean_value(value: &Value, ctx: &mut Context) -> Result { match value { Value::Boolean(boolean) => return Ok(*boolean), Value::Object(ref object) => { @@ -57,7 +57,7 @@ impl Boolean { pub(crate) fn construct_boolean( this: &Value, args: &[Value], - _: &mut Interpreter, + _: &mut Context, ) -> Result { // Get the argument, if any let data = args.get(0).map(|x| x.to_boolean()).unwrap_or(false); @@ -75,7 +75,7 @@ impl Boolean { /// [spec]: https://tc39.es/ecma262/#sec-boolean-object /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean/toString #[allow(clippy::wrong_self_convention)] - pub(crate) fn to_string(this: &Value, _: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn to_string(this: &Value, _: &[Value], ctx: &mut Context) -> Result { let boolean = Self::this_boolean_value(this, ctx)?; Ok(Value::from(boolean.to_string())) } @@ -89,14 +89,14 @@ impl Boolean { /// [spec]: https://tc39.es/ecma262/#sec-boolean.prototype.valueof /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean/valueOf #[inline] - pub(crate) fn value_of(this: &Value, _: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn value_of(this: &Value, _: &[Value], ctx: &mut Context) -> Result { Ok(Value::from(Self::this_boolean_value(this, ctx)?)) } /// Initialise the `Boolean` object on the global object. #[inline] - pub(crate) fn init(interpreter: &mut Interpreter) -> (&'static str, Value) { - let global = interpreter.global(); + pub(crate) fn init(interpreter: &mut Context) -> (&'static str, Value) { + let global = interpreter.global_object(); let _timer = BoaProfiler::global().start_event(Self::NAME, "init"); // Create Prototype diff --git a/boa/src/builtins/boolean/tests.rs b/boa/src/builtins/boolean/tests.rs index c0eed466e39..7728fee7bba 100644 --- a/boa/src/builtins/boolean/tests.rs +++ b/boa/src/builtins/boolean/tests.rs @@ -1,11 +1,10 @@ -use crate::{builtins::value::same_value, exec::Interpreter, forward, forward_val, realm::Realm}; +use crate::{builtins::value::same_value, context::Context, forward, forward_val}; /// Test the correct type is returned from call and construct #[allow(clippy::unwrap_used)] #[test] fn construct_and_call() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var one = new Boolean(1); var zero = Boolean(0); @@ -20,8 +19,7 @@ fn construct_and_call() { #[test] fn constructor_gives_true_instance() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var trueVal = new Boolean(true); var trueNum = new Boolean(1); @@ -50,8 +48,7 @@ fn constructor_gives_true_instance() { #[test] fn instances_have_correct_proto_set() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var boolInstance = new Boolean(true); var boolProto = Boolean.prototype; diff --git a/boa/src/builtins/console/mod.rs b/boa/src/builtins/console/mod.rs index be1153d8477..e7638026014 100644 --- a/boa/src/builtins/console/mod.rs +++ b/boa/src/builtins/console/mod.rs @@ -21,7 +21,7 @@ use crate::{ function::make_builtin_fn, value::{display_obj, RcString, Value}, }, - exec::Interpreter, + context::Context, BoaProfiler, Result, }; use rustc_hash::FxHashMap; @@ -59,7 +59,7 @@ pub(crate) fn logger(msg: LogMessage, console_state: &Console) { } /// This represents the `console` formatter. -pub fn formatter(data: &[Value], ctx: &mut Interpreter) -> Result { +pub fn formatter(data: &[Value], ctx: &mut Context) -> Result { let target = data.get(0).cloned().unwrap_or_default().to_string(ctx)?; match data.len() { @@ -154,7 +154,7 @@ impl Console { /// /// [spec]: https://console.spec.whatwg.org/#assert /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/console/assert - pub(crate) fn assert(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn assert(_: &Value, args: &[Value], ctx: &mut Context) -> Result { let assertion = get_arg_at_index::(args, 0).unwrap_or_default(); if !assertion { @@ -185,7 +185,7 @@ impl Console { /// /// [spec]: https://console.spec.whatwg.org/#clear /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/console/clear - pub(crate) fn clear(_: &Value, _: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn clear(_: &Value, _: &[Value], ctx: &mut Context) -> Result { ctx.console_mut().groups.clear(); Ok(Value::undefined()) } @@ -200,7 +200,7 @@ impl Console { /// /// [spec]: https://console.spec.whatwg.org/#debug /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/console/debug - pub(crate) fn debug(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn debug(_: &Value, args: &[Value], ctx: &mut Context) -> Result { logger(LogMessage::Log(formatter(args, ctx)?), ctx.console()); Ok(Value::undefined()) } @@ -215,7 +215,7 @@ impl Console { /// /// [spec]: https://console.spec.whatwg.org/#error /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/console/error - pub(crate) fn error(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn error(_: &Value, args: &[Value], ctx: &mut Context) -> Result { logger(LogMessage::Error(formatter(args, ctx)?), ctx.console()); Ok(Value::undefined()) } @@ -230,7 +230,7 @@ impl Console { /// /// [spec]: https://console.spec.whatwg.org/#info /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/console/info - pub(crate) fn info(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn info(_: &Value, args: &[Value], ctx: &mut Context) -> Result { logger(LogMessage::Info(formatter(args, ctx)?), ctx.console()); Ok(Value::undefined()) } @@ -245,7 +245,7 @@ impl Console { /// /// [spec]: https://console.spec.whatwg.org/#log /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/console/log - pub(crate) fn log(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn log(_: &Value, args: &[Value], ctx: &mut Context) -> Result { logger(LogMessage::Log(formatter(args, ctx)?), ctx.console()); Ok(Value::undefined()) } @@ -260,7 +260,7 @@ impl Console { /// /// [spec]: https://console.spec.whatwg.org/#trace /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/console/trace - pub(crate) fn trace(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn trace(_: &Value, args: &[Value], ctx: &mut Context) -> Result { if !args.is_empty() { logger(LogMessage::Log(formatter(args, ctx)?), ctx.console()); @@ -284,7 +284,7 @@ impl Console { /// /// [spec]: https://console.spec.whatwg.org/#warn /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/console/warn - pub(crate) fn warn(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn warn(_: &Value, args: &[Value], ctx: &mut Context) -> Result { logger(LogMessage::Warn(formatter(args, ctx)?), ctx.console()); Ok(Value::undefined()) } @@ -299,7 +299,7 @@ impl Console { /// /// [spec]: https://console.spec.whatwg.org/#count /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/console/count - pub(crate) fn count(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn count(_: &Value, args: &[Value], ctx: &mut Context) -> Result { let label = match args.get(0) { Some(value) => value.to_string(ctx)?, None => "default".into(), @@ -323,7 +323,7 @@ impl Console { /// /// [spec]: https://console.spec.whatwg.org/#countreset /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/countReset - pub(crate) fn count_reset(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn count_reset(_: &Value, args: &[Value], ctx: &mut Context) -> Result { let label = match args.get(0) { Some(value) => value.to_string(ctx)?, None => "default".into(), @@ -357,7 +357,7 @@ impl Console { /// /// [spec]: https://console.spec.whatwg.org/#time /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/console/time - pub(crate) fn time(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn time(_: &Value, args: &[Value], ctx: &mut Context) -> Result { let label = match args.get(0) { Some(value) => value.to_string(ctx)?, None => "default".into(), @@ -386,7 +386,7 @@ impl Console { /// /// [spec]: https://console.spec.whatwg.org/#timelog /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/console/timeLog - pub(crate) fn time_log(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn time_log(_: &Value, args: &[Value], ctx: &mut Context) -> Result { let label = match args.get(0) { Some(value) => value.to_string(ctx)?, None => "default".into(), @@ -419,7 +419,7 @@ impl Console { /// /// [spec]: https://console.spec.whatwg.org/#timeend /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/console/timeEnd - pub(crate) fn time_end(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn time_end(_: &Value, args: &[Value], ctx: &mut Context) -> Result { let label = match args.get(0) { Some(value) => value.to_string(ctx)?, None => "default".into(), @@ -451,7 +451,7 @@ impl Console { /// /// [spec]: https://console.spec.whatwg.org/#group /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/console/group - pub(crate) fn group(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn group(_: &Value, args: &[Value], ctx: &mut Context) -> Result { let group_label = formatter(args, ctx)?; logger( @@ -473,7 +473,7 @@ impl Console { /// /// [spec]: https://console.spec.whatwg.org/#groupend /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/console/groupEnd - pub(crate) fn group_end(_: &Value, _: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn group_end(_: &Value, _: &[Value], ctx: &mut Context) -> Result { ctx.console_mut().groups.pop(); Ok(Value::undefined()) @@ -489,7 +489,7 @@ impl Console { /// /// [spec]: https://console.spec.whatwg.org/#dir /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/console/dir - pub(crate) fn dir(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn dir(_: &Value, args: &[Value], ctx: &mut Context) -> Result { let undefined = Value::undefined(); logger( LogMessage::Info(display_obj(args.get(0).unwrap_or(&undefined), true)), @@ -501,8 +501,8 @@ impl Console { /// Initialise the `console` object on the global object. #[inline] - pub(crate) fn init(interpreter: &mut Interpreter) -> (&'static str, Value) { - let global = interpreter.global(); + pub(crate) fn init(interpreter: &mut Context) -> (&'static str, Value) { + let global = interpreter.global_object(); let _timer = BoaProfiler::global().start_event(Self::NAME, "init"); let console = Value::new_object(Some(global)); diff --git a/boa/src/builtins/console/tests.rs b/boa/src/builtins/console/tests.rs index 68d820dd7b5..2ba8bad9713 100644 --- a/boa/src/builtins/console/tests.rs +++ b/boa/src/builtins/console/tests.rs @@ -1,28 +1,24 @@ use crate::{ builtins::{console::formatter, value::Value}, - exec::Interpreter, - realm::Realm, + context::Context, }; #[test] fn formatter_no_args_is_empty_string() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(formatter(&[], &mut engine).unwrap(), ""); } #[test] fn formatter_empty_format_string_is_empty_string() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let val = Value::string("".to_string()); assert_eq!(formatter(&[val], &mut engine).unwrap(), ""); } #[test] fn formatter_format_without_args_renders_verbatim() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let val = [Value::string("%d %s %% %f")]; let res = formatter(&val, &mut engine).unwrap(); assert_eq!(res, "%d %s %% %f"); @@ -30,8 +26,7 @@ fn formatter_format_without_args_renders_verbatim() { #[test] fn formatter_empty_format_string_concatenates_rest_of_args() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let val = [ Value::string(""), @@ -44,8 +39,7 @@ fn formatter_empty_format_string_concatenates_rest_of_args() { #[test] fn formatter_utf_8_checks() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let val = [ Value::string("Są takie chwile %dą %są tu%sów %привет%ź".to_string()), @@ -59,8 +53,7 @@ fn formatter_utf_8_checks() { #[test] fn formatter_trailing_format_leader_renders() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let val = [ Value::string("%%%%%".to_string()), @@ -73,8 +66,7 @@ fn formatter_trailing_format_leader_renders() { #[test] #[allow(clippy::approx_constant)] fn formatter_float_format_works() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let val = [Value::string("%f".to_string()), Value::rational(3.1415)]; let res = formatter(&val, &mut engine).unwrap(); diff --git a/boa/src/builtins/date/mod.rs b/boa/src/builtins/date/mod.rs index 9f792aaa8e7..2d7faaf3d39 100644 --- a/boa/src/builtins/date/mod.rs +++ b/boa/src/builtins/date/mod.rs @@ -8,7 +8,7 @@ use crate::{ value::PreferredType, Value, }, - BoaProfiler, Interpreter, Result, + BoaProfiler, Context, Result, }; use chrono::{prelude::*, Duration, LocalResult}; use gc::{unsafe_empty_trace, Finalize, Trace}; @@ -40,13 +40,13 @@ fn ignore_ambiguity(result: LocalResult) -> Option { macro_rules! getter_method { ($name:ident) => {{ - fn get_value(this: &Value, _: &[Value], ctx: &mut Interpreter) -> Result { + fn get_value(this: &Value, _: &[Value], ctx: &mut Context) -> Result { Ok(Value::from(this_time_value(this, ctx)?.$name())) } get_value }}; (Self::$name:ident) => {{ - fn get_value(_: &Value, _: &[Value], _: &mut Interpreter) -> Result { + fn get_value(_: &Value, _: &[Value], _: &mut Context) -> Result { Ok(Value::from(Date::$name())) } get_value @@ -55,7 +55,7 @@ macro_rules! getter_method { macro_rules! setter_method { ($name:ident($($e:expr),* $(,)?)) => {{ - fn set_value(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + fn set_value(this: &Value, args: &[Value], ctx: &mut Context) -> Result { let mut result = this_time_value(this, ctx)?; result.$name( $( @@ -246,7 +246,7 @@ impl Date { /// /// [spec]: https://tc39.es/ecma262/#sec-date-constructor /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date - pub(crate) fn make_date(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn make_date(this: &Value, args: &[Value], ctx: &mut Context) -> Result { if this.is_global() { Self::make_date_string() } else if args.is_empty() { @@ -301,7 +301,7 @@ impl Date { pub(crate) fn make_date_single( this: &Value, args: &[Value], - ctx: &mut Interpreter, + ctx: &mut Context, ) -> Result { let value = &args[0]; let tv = match this_time_value(value, ctx) { @@ -338,7 +338,7 @@ impl Date { pub(crate) fn make_date_multiple( this: &Value, args: &[Value], - ctx: &mut Interpreter, + ctx: &mut Context, ) -> Result { let year = args[0].to_number(ctx)?; let month = args[1].to_number(ctx)?; @@ -1164,7 +1164,7 @@ impl Date { /// /// [spec]: https://tc39.es/ecma262/#sec-date.now /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now - pub(crate) fn now(_: &Value, _: &[Value], _: &mut Interpreter) -> Result { + pub(crate) fn now(_: &Value, _: &[Value], _: &mut Context) -> Result { Ok(Value::from(Utc::now().timestamp_millis() as f64)) } @@ -1180,7 +1180,7 @@ impl Date { /// /// [spec]: https://tc39.es/ecma262/#sec-date.parse /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse - pub(crate) fn parse(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn parse(_: &Value, args: &[Value], ctx: &mut Context) -> Result { // This method is implementation-defined and discouraged, so we just require the same format as the string // constructor. @@ -1204,7 +1204,7 @@ impl Date { /// /// [spec]: https://tc39.es/ecma262/#sec-date.utc /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC - pub(crate) fn utc(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn utc(_: &Value, args: &[Value], ctx: &mut Context) -> Result { let year = args .get(0) .map_or(Ok(f64::NAN), |value| value.to_number(ctx))?; @@ -1242,8 +1242,8 @@ impl Date { /// Initialise the `Date` object on the global object. #[inline] - pub(crate) fn init(interpreter: &mut Interpreter) -> (&'static str, Value) { - let global = interpreter.global(); + pub(crate) fn init(interpreter: &mut Context) -> (&'static str, Value) { + let global = interpreter.global_object(); let _timer = BoaProfiler::global().start_event(Self::NAME, "init"); let prototype = Value::new_object(Some(global)); @@ -1581,7 +1581,7 @@ impl Date { /// /// [spec]: https://tc39.es/ecma262/#sec-thistimevalue #[inline] -pub fn this_time_value(value: &Value, ctx: &mut Interpreter) -> Result { +pub fn this_time_value(value: &Value, ctx: &mut Context) -> Result { if let Value::Object(ref object) = value { if let ObjectData::Date(ref date) = object.borrow().data { return Ok(*date); diff --git a/boa/src/builtins/date/tests.rs b/boa/src/builtins/date/tests.rs index fb005c3f0db..2e38ac93881 100644 --- a/boa/src/builtins/date/tests.rs +++ b/boa/src/builtins/date/tests.rs @@ -2,14 +2,14 @@ use crate::{ builtins::{object::ObjectData, Value}, - forward, forward_val, Interpreter, Realm, + forward, forward_val, Context, }; use chrono::prelude::*; // NOTE: Javascript Uses 0-based months, where chrono uses 1-based months. Many of the assertions look wrong because of // this. -fn forward_dt_utc(engine: &mut Interpreter, src: &str) -> Option { +fn forward_dt_utc(engine: &mut Context, src: &str) -> Option { let date_time = if let Ok(v) = forward_val(engine, src) { v } else { @@ -27,7 +27,7 @@ fn forward_dt_utc(engine: &mut Interpreter, src: &str) -> Option } } -fn forward_dt_local(engine: &mut Interpreter, src: &str) -> Option { +fn forward_dt_local(engine: &mut Context, src: &str) -> Option { let date_time = forward_dt_utc(engine, src); // The timestamp is converted to UTC for internal representation @@ -56,8 +56,7 @@ fn date_display() { #[test] fn date_this_time_value() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let error = forward_val( &mut engine, @@ -77,8 +76,7 @@ fn date_this_time_value() { #[test] fn date_call() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let dt1 = forward(&mut engine, "Date()"); @@ -92,8 +90,7 @@ fn date_call() -> Result<(), Box> { #[test] fn date_ctor_call() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let dt1 = forward_dt_local(&mut engine, "new Date()"); @@ -107,8 +104,7 @@ fn date_ctor_call() -> Result<(), Box> { #[test] fn date_ctor_call_string() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let date_time = forward_dt_utc(&mut engine, "new Date('2020-06-08T09:16:15.779-06:30')"); @@ -122,8 +118,7 @@ fn date_ctor_call_string() -> Result<(), Box> { #[test] fn date_ctor_call_string_invalid() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let date_time = forward_dt_local(&mut engine, "new Date('nope')"); assert_eq!(None, date_time); @@ -132,8 +127,7 @@ fn date_ctor_call_string_invalid() -> Result<(), Box> { #[test] fn date_ctor_call_number() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let date_time = forward_dt_utc(&mut engine, "new Date(1594199775779)"); assert_eq!( @@ -145,8 +139,7 @@ fn date_ctor_call_number() -> Result<(), Box> { #[test] fn date_ctor_call_date() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let date_time = forward_dt_utc(&mut engine, "new Date(new Date(1594199775779))"); @@ -159,8 +152,7 @@ fn date_ctor_call_date() -> Result<(), Box> { #[test] fn date_ctor_call_multiple() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let date_time = forward_dt_local(&mut engine, "new Date(2020, 06, 08, 09, 16, 15, 779)"); @@ -173,8 +165,7 @@ fn date_ctor_call_multiple() -> Result<(), Box> { #[test] fn date_ctor_call_multiple_90s() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let date_time = forward_dt_local(&mut engine, "new Date(99, 06, 08, 09, 16, 15, 779)"); @@ -188,8 +179,7 @@ fn date_ctor_call_multiple_90s() -> Result<(), Box> { #[test] fn date_ctor_call_multiple_nan() -> Result<(), Box> { fn check(src: &str) { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let date_time = forward_dt_local(&mut engine, src); assert_eq!(None, date_time); } @@ -207,8 +197,7 @@ fn date_ctor_call_multiple_nan() -> Result<(), Box> { #[test] fn date_ctor_now_call() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let date_time = forward(&mut engine, "Date.now()"); let dt1 = u64::from_str_radix(&date_time, 10)?; @@ -224,8 +213,7 @@ fn date_ctor_now_call() -> Result<(), Box> { #[test] fn date_ctor_parse_call() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let date_time = forward_val(&mut engine, "Date.parse('2020-06-08T09:16:15.779-07:30')"); @@ -235,8 +223,7 @@ fn date_ctor_parse_call() -> Result<(), Box> { #[test] fn date_ctor_utc_call() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let date_time = forward_val(&mut engine, "Date.UTC(2020, 06, 08, 09, 16, 15, 779)"); @@ -247,8 +234,7 @@ fn date_ctor_utc_call() -> Result<(), Box> { #[test] fn date_ctor_utc_call_nan() -> Result<(), Box> { fn check(src: &str) { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let date_time = forward_val(&mut engine, src).expect("Expected Success"); assert_eq!(Value::Rational(f64::NAN), date_time); } @@ -266,8 +252,7 @@ fn date_ctor_utc_call_nan() -> Result<(), Box> { #[test] fn date_proto_get_date_call() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_val( &mut engine, @@ -283,8 +268,7 @@ fn date_proto_get_date_call() -> Result<(), Box> { #[test] fn date_proto_get_day_call() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_val( &mut engine, @@ -299,8 +283,7 @@ fn date_proto_get_day_call() -> Result<(), Box> { #[test] fn date_proto_get_full_year_call() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_val( &mut engine, @@ -315,8 +298,7 @@ fn date_proto_get_full_year_call() -> Result<(), Box> { #[test] fn date_proto_get_hours_call() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_val( &mut engine, @@ -331,8 +313,7 @@ fn date_proto_get_hours_call() -> Result<(), Box> { #[test] fn date_proto_get_milliseconds_call() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_val( &mut engine, @@ -347,8 +328,7 @@ fn date_proto_get_milliseconds_call() -> Result<(), Box> #[test] fn date_proto_get_minutes_call() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_val( &mut engine, @@ -363,8 +343,7 @@ fn date_proto_get_minutes_call() -> Result<(), Box> { #[test] fn date_proto_get_month() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_val( &mut engine, @@ -380,8 +359,7 @@ fn date_proto_get_month() -> Result<(), Box> { #[test] fn date_proto_get_seconds() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_val( &mut engine, @@ -396,8 +374,7 @@ fn date_proto_get_seconds() -> Result<(), Box> { #[test] fn date_proto_get_time() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_val( &mut engine, @@ -417,8 +394,7 @@ fn date_proto_get_time() -> Result<(), Box> { #[test] fn date_proto_get_year() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_val( &mut engine, @@ -433,8 +409,7 @@ fn date_proto_get_year() -> Result<(), Box> { #[test] fn date_proto_get_timezone_offset() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_val( &mut engine, @@ -464,8 +439,7 @@ fn date_proto_get_timezone_offset() -> Result<(), Box> { #[test] fn date_proto_get_utc_date_call() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_val( &mut engine, @@ -481,8 +455,7 @@ fn date_proto_get_utc_date_call() -> Result<(), Box> { #[test] fn date_proto_get_utc_day_call() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_val( &mut engine, @@ -497,8 +470,7 @@ fn date_proto_get_utc_day_call() -> Result<(), Box> { #[test] fn date_proto_get_utc_full_year_call() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_val( &mut engine, @@ -513,8 +485,7 @@ fn date_proto_get_utc_full_year_call() -> Result<(), Box> #[test] fn date_proto_get_utc_hours_call() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_val( &mut engine, @@ -529,8 +500,7 @@ fn date_proto_get_utc_hours_call() -> Result<(), Box> { #[test] fn date_proto_get_utc_milliseconds_call() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_val( &mut engine, @@ -545,8 +515,7 @@ fn date_proto_get_utc_milliseconds_call() -> Result<(), Box Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_val( &mut engine, @@ -561,8 +530,7 @@ fn date_proto_get_utc_minutes_call() -> Result<(), Box> { #[test] fn date_proto_get_utc_month() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_val( &mut engine, @@ -578,8 +546,7 @@ fn date_proto_get_utc_month() -> Result<(), Box> { #[test] fn date_proto_get_utc_seconds() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_val( &mut engine, @@ -594,8 +561,7 @@ fn date_proto_get_utc_seconds() -> Result<(), Box> { #[test] fn date_proto_set_date() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_dt_local( &mut engine, @@ -627,8 +593,7 @@ fn date_proto_set_date() -> Result<(), Box> { #[test] fn date_proto_set_full_year() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_dt_local( &mut engine, @@ -700,8 +665,7 @@ fn date_proto_set_full_year() -> Result<(), Box> { #[test] fn date_proto_set_hours() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_dt_local( &mut engine, @@ -755,8 +719,7 @@ fn date_proto_set_hours() -> Result<(), Box> { #[test] fn date_proto_set_milliseconds() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_dt_local( &mut engine, @@ -784,8 +747,7 @@ fn date_proto_set_milliseconds() -> Result<(), Box> { #[test] fn date_proto_set_minutes() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_dt_local( &mut engine, @@ -831,8 +793,7 @@ fn date_proto_set_minutes() -> Result<(), Box> { #[test] fn date_proto_set_month() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_dt_local( &mut engine, @@ -869,8 +830,7 @@ fn date_proto_set_month() -> Result<(), Box> { #[test] fn date_proto_set_seconds() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_dt_local( &mut engine, @@ -907,8 +867,7 @@ fn date_proto_set_seconds() -> Result<(), Box> { #[test] fn set_year() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_dt_local( &mut engine, @@ -933,8 +892,7 @@ fn set_year() -> Result<(), Box> { #[test] fn date_proto_set_time() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_dt_local( &mut engine, @@ -950,8 +908,7 @@ fn date_proto_set_time() -> Result<(), Box> { #[test] fn date_proto_set_utc_date() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_dt_utc( &mut engine, @@ -983,8 +940,7 @@ fn date_proto_set_utc_date() -> Result<(), Box> { #[test] fn date_proto_set_utc_full_year() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_dt_utc( &mut engine, @@ -1056,8 +1012,7 @@ fn date_proto_set_utc_full_year() -> Result<(), Box> { #[test] fn date_proto_set_utc_hours() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_dt_utc( &mut engine, @@ -1111,8 +1066,7 @@ fn date_proto_set_utc_hours() -> Result<(), Box> { #[test] fn date_proto_set_utc_milliseconds() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_dt_utc( &mut engine, @@ -1140,8 +1094,7 @@ fn date_proto_set_utc_milliseconds() -> Result<(), Box> { #[test] fn date_proto_set_utc_minutes() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_dt_utc( &mut engine, @@ -1187,8 +1140,7 @@ fn date_proto_set_utc_minutes() -> Result<(), Box> { #[test] fn date_proto_set_utc_month() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_dt_utc( &mut engine, @@ -1225,8 +1177,7 @@ fn date_proto_set_utc_month() -> Result<(), Box> { #[test] fn date_proto_set_utc_seconds() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_dt_utc( &mut engine, @@ -1263,8 +1214,7 @@ fn date_proto_set_utc_seconds() -> Result<(), Box> { #[test] fn date_proto_to_date_string() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_val( &mut engine, @@ -1278,8 +1228,7 @@ fn date_proto_to_date_string() -> Result<(), Box> { #[test] fn date_proto_to_gmt_string() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_val( &mut engine, @@ -1293,8 +1242,7 @@ fn date_proto_to_gmt_string() -> Result<(), Box> { #[test] fn date_proto_to_iso_string() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_val( &mut engine, @@ -1308,8 +1256,7 @@ fn date_proto_to_iso_string() -> Result<(), Box> { #[test] fn date_proto_to_json() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_val( &mut engine, @@ -1323,8 +1270,7 @@ fn date_proto_to_json() -> Result<(), Box> { #[test] fn date_proto_to_string() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_val( &mut engine, @@ -1346,8 +1292,7 @@ fn date_proto_to_string() -> Result<(), Box> { #[test] fn date_proto_to_time_string() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_val( &mut engine, @@ -1367,8 +1312,7 @@ fn date_proto_to_time_string() -> Result<(), Box> { #[test] fn date_proto_to_utc_string() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_val( &mut engine, @@ -1382,8 +1326,7 @@ fn date_proto_to_utc_string() -> Result<(), Box> { #[test] fn date_proto_value_of() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_val( &mut engine, @@ -1397,8 +1340,7 @@ fn date_proto_value_of() -> Result<(), Box> { #[test] fn date_neg() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_val( &mut engine, @@ -1412,8 +1354,7 @@ fn date_neg() -> Result<(), Box> { #[test] fn date_json() -> Result<(), Box> { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward_val( &mut engine, diff --git a/boa/src/builtins/error/mod.rs b/boa/src/builtins/error/mod.rs index 6b97c751b9d..4d3062bd1b9 100644 --- a/boa/src/builtins/error/mod.rs +++ b/boa/src/builtins/error/mod.rs @@ -16,7 +16,7 @@ use crate::{ object::ObjectData, value::Value, }, - exec::Interpreter, + context::Context, profiler::BoaProfiler, Result, }; @@ -47,7 +47,7 @@ impl Error { pub(crate) const LENGTH: usize = 1; /// Create a new error object. - pub(crate) fn make_error(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn make_error(this: &Value, args: &[Value], ctx: &mut Context) -> Result { if let Some(message) = args.get(0) { this.set_field("message", message.to_string(ctx)?); } @@ -69,7 +69,7 @@ impl Error { /// [spec]: https://tc39.es/ecma262/#sec-error.prototype.tostring /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/toString #[allow(clippy::wrong_self_convention)] - pub(crate) fn to_string(this: &Value, _: &[Value], _: &mut Interpreter) -> Result { + pub(crate) fn to_string(this: &Value, _: &[Value], _: &mut Context) -> Result { let name = this.get_field("name"); let message = this.get_field("message"); Ok(Value::from(format!( @@ -81,8 +81,8 @@ impl Error { /// Initialise the global object with the `Error` object. #[inline] - pub(crate) fn init(interpreter: &mut Interpreter) -> (&'static str, Value) { - let global = interpreter.global(); + pub(crate) fn init(interpreter: &mut Context) -> (&'static str, Value) { + let global = interpreter.global_object(); let _timer = BoaProfiler::global().start_event(Self::NAME, "init"); let prototype = Value::new_object(Some(global)); diff --git a/boa/src/builtins/error/range.rs b/boa/src/builtins/error/range.rs index 2b5e635c99e..894b66bd5e5 100644 --- a/boa/src/builtins/error/range.rs +++ b/boa/src/builtins/error/range.rs @@ -13,7 +13,7 @@ use crate::{ builtins::{ function::make_builtin_fn, function::make_constructor_fn, object::ObjectData, value::Value, }, - exec::Interpreter, + context::Context, profiler::BoaProfiler, Result, }; @@ -30,7 +30,7 @@ impl RangeError { pub(crate) const LENGTH: usize = 1; /// Create a new error object. - pub(crate) fn make_error(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn make_error(this: &Value, args: &[Value], ctx: &mut Context) -> Result { if let Some(message) = args.get(0) { this.set_field("message", message.to_string(ctx)?); } @@ -52,7 +52,7 @@ impl RangeError { /// [spec]: https://tc39.es/ecma262/#sec-error.prototype.tostring /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/toString #[allow(clippy::wrong_self_convention)] - pub(crate) fn to_string(this: &Value, _: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn to_string(this: &Value, _: &[Value], ctx: &mut Context) -> Result { let name = this.get_field("name").to_string(ctx)?; let message = this.get_field("message").to_string(ctx)?; @@ -61,8 +61,8 @@ impl RangeError { /// Initialise the global object with the `RangeError` object. #[inline] - pub(crate) fn init(interpreter: &mut Interpreter) -> (&'static str, Value) { - let global = interpreter.global(); + pub(crate) fn init(interpreter: &mut Context) -> (&'static str, Value) { + let global = interpreter.global_object(); let _timer = BoaProfiler::global().start_event(Self::NAME, "init"); let prototype = Value::new_object(Some(global)); diff --git a/boa/src/builtins/error/reference.rs b/boa/src/builtins/error/reference.rs index 9f20117dd3e..b7e67e62920 100644 --- a/boa/src/builtins/error/reference.rs +++ b/boa/src/builtins/error/reference.rs @@ -13,7 +13,7 @@ use crate::{ builtins::{ function::make_builtin_fn, function::make_constructor_fn, object::ObjectData, value::Value, }, - exec::Interpreter, + context::Context, profiler::BoaProfiler, Result, }; @@ -29,7 +29,7 @@ impl ReferenceError { pub(crate) const LENGTH: usize = 1; /// Create a new error object. - pub(crate) fn make_error(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn make_error(this: &Value, args: &[Value], ctx: &mut Context) -> Result { if let Some(message) = args.get(0) { this.set_field("message", message.to_string(ctx)?); } @@ -51,7 +51,7 @@ impl ReferenceError { /// [spec]: https://tc39.es/ecma262/#sec-error.prototype.tostring /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/toString #[allow(clippy::wrong_self_convention)] - pub(crate) fn to_string(this: &Value, _: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn to_string(this: &Value, _: &[Value], ctx: &mut Context) -> Result { let name = this.get_field("name").to_string(ctx)?; let message = this.get_field("message").to_string(ctx)?; @@ -59,8 +59,8 @@ impl ReferenceError { } /// Initialise the global object with the `ReferenceError` object. - pub(crate) fn init(interpreter: &mut Interpreter) -> (&'static str, Value) { - let global = interpreter.global(); + pub(crate) fn init(interpreter: &mut Context) -> (&'static str, Value) { + let global = interpreter.global_object(); let _timer = BoaProfiler::global().start_event(Self::NAME, "init"); let prototype = Value::new_object(Some(global)); diff --git a/boa/src/builtins/error/syntax.rs b/boa/src/builtins/error/syntax.rs index c05f0f7fbb5..2216c27deb0 100644 --- a/boa/src/builtins/error/syntax.rs +++ b/boa/src/builtins/error/syntax.rs @@ -15,7 +15,7 @@ use crate::{ builtins::{ function::make_builtin_fn, function::make_constructor_fn, object::ObjectData, value::Value, }, - exec::Interpreter, + context::Context, profiler::BoaProfiler, Result, }; @@ -32,7 +32,7 @@ impl SyntaxError { pub(crate) const LENGTH: usize = 1; /// Create a new error object. - pub(crate) fn make_error(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn make_error(this: &Value, args: &[Value], ctx: &mut Context) -> Result { if let Some(message) = args.get(0) { this.set_field("message", message.to_string(ctx)?); } @@ -54,7 +54,7 @@ impl SyntaxError { /// [spec]: https://tc39.es/ecma262/#sec-error.prototype.tostring /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/toString #[allow(clippy::wrong_self_convention)] - pub(crate) fn to_string(this: &Value, _: &[Value], _: &mut Interpreter) -> Result { + pub(crate) fn to_string(this: &Value, _: &[Value], _: &mut Context) -> Result { let name = this.get_field("name"); let message = this.get_field("message"); // FIXME: This should not use `.display()` @@ -63,8 +63,8 @@ impl SyntaxError { /// Initialise the global object with the `SyntaxError` object. #[inline] - pub(crate) fn init(interpreter: &mut Interpreter) -> (&'static str, Value) { - let global = interpreter.global(); + pub(crate) fn init(interpreter: &mut Context) -> (&'static str, Value) { + let global = interpreter.global_object(); let _timer = BoaProfiler::global().start_event(Self::NAME, "init"); let prototype = Value::new_object(Some(global)); diff --git a/boa/src/builtins/error/type.rs b/boa/src/builtins/error/type.rs index 60fff80f962..402ad572486 100644 --- a/boa/src/builtins/error/type.rs +++ b/boa/src/builtins/error/type.rs @@ -19,7 +19,7 @@ use crate::{ builtins::{ function::make_builtin_fn, function::make_constructor_fn, object::ObjectData, value::Value, }, - exec::Interpreter, + context::Context, BoaProfiler, Result, }; @@ -35,7 +35,7 @@ impl TypeError { pub(crate) const LENGTH: usize = 1; /// Create a new error object. - pub(crate) fn make_error(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn make_error(this: &Value, args: &[Value], ctx: &mut Context) -> Result { if let Some(message) = args.get(0) { this.set_field("message", message.to_string(ctx)?); } @@ -57,7 +57,7 @@ impl TypeError { /// [spec]: https://tc39.es/ecma262/#sec-error.prototype.tostring /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/toString #[allow(clippy::wrong_self_convention)] - pub(crate) fn to_string(this: &Value, _: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn to_string(this: &Value, _: &[Value], ctx: &mut Context) -> Result { let name = this.get_field("name").to_string(ctx)?; let message = this.get_field("message").to_string(ctx)?; @@ -66,8 +66,8 @@ impl TypeError { /// Initialise the global object with the `RangeError` object. #[inline] - pub(crate) fn init(interpreter: &mut Interpreter) -> (&'static str, Value) { - let global = interpreter.global(); + pub(crate) fn init(interpreter: &mut Context) -> (&'static str, Value) { + let global = interpreter.global_object(); let _timer = BoaProfiler::global().start_event(Self::NAME, "init"); let prototype = Value::new_object(Some(global)); diff --git a/boa/src/builtins/function/mod.rs b/boa/src/builtins/function/mod.rs index 59db09eebce..9b155bec811 100644 --- a/boa/src/builtins/function/mod.rs +++ b/boa/src/builtins/function/mod.rs @@ -19,9 +19,8 @@ use crate::{ Array, }, environment::lexical_environment::Environment, - exec::Interpreter, syntax::ast::node::{FormalParameter, RcStatementList}, - BoaProfiler, Result, + BoaProfiler, Context, Result, }; use bitflags::bitflags; use gc::{unsafe_empty_trace, Finalize, Trace}; @@ -30,8 +29,8 @@ use std::fmt::{self, Debug}; #[cfg(test)] mod tests; -/// _fn(this, arguments, ctx) -> Result_ - The signature of a built-in function -pub type NativeFunction = fn(&Value, &[Value], &mut Interpreter) -> Result; +/// _fn(this, arguments, ctx) -> ResultValue_ - The signature of a built-in function +pub type NativeFunction = fn(&Value, &[Value], &mut Context) -> Result; #[derive(Clone, Copy, Finalize)] pub struct BuiltInFunction(pub(crate) NativeFunction); @@ -118,7 +117,7 @@ impl Function { param: &FormalParameter, index: usize, args_list: &[Value], - interpreter: &mut Interpreter, + interpreter: &mut Context, local_env: &Environment, ) { // Create array of values @@ -202,7 +201,7 @@ pub fn create_unmapped_arguments_object(arguments_list: &[Value]) -> Value { /// Create new function `[[Construct]]` /// // This gets called when a new Function() is created. -pub fn make_function(this: &Value, _: &[Value], _: &mut Interpreter) -> Result { +pub fn make_function(this: &Value, _: &[Value], _: &mut Context) -> Result { this.set_data(ObjectData::Function(Function::BuiltIn( BuiltInFunction(|_, _, _| Ok(Value::undefined())), FunctionFlags::CALLABLE | FunctionFlags::CONSTRUCTABLE, @@ -287,7 +286,7 @@ pub fn make_builtin_fn( name: N, parent: &Value, length: usize, - interpreter: &Interpreter, + interpreter: &Context, ) where N: Into, { @@ -297,7 +296,7 @@ pub fn make_builtin_fn( let mut function = Object::function( Function::BuiltIn(function.into(), FunctionFlags::CALLABLE), interpreter - .global() + .global_object() .get_field("Function") .get_field("prototype"), ); @@ -311,8 +310,8 @@ pub fn make_builtin_fn( /// Initialise the `Function` object on the global object. #[inline] -pub fn init(interpreter: &mut Interpreter) -> (&'static str, Value) { - let global = interpreter.global(); +pub fn init(interpreter: &mut Context) -> (&'static str, Value) { + let global = interpreter.global_object(); let _timer = BoaProfiler::global().start_event("function", "init"); let prototype = Value::new_object(Some(global)); diff --git a/boa/src/builtins/function/tests.rs b/boa/src/builtins/function/tests.rs index ca8b8156ef8..287734fa301 100644 --- a/boa/src/builtins/function/tests.rs +++ b/boa/src/builtins/function/tests.rs @@ -1,10 +1,10 @@ -use crate::{exec::Interpreter, forward, forward_val, realm::Realm}; +use crate::{forward, forward_val, Context}; #[allow(clippy::float_cmp)] #[test] fn check_arguments_object() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); + let init = r#" function jason(a, b) { return arguments[0]; @@ -26,8 +26,7 @@ fn check_arguments_object() { #[test] fn check_self_mutating_func() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let func = r#" function x() { x.y = 3; diff --git a/boa/src/builtins/global_this/mod.rs b/boa/src/builtins/global_this/mod.rs index 481b619decb..ad296a7dd22 100644 --- a/boa/src/builtins/global_this/mod.rs +++ b/boa/src/builtins/global_this/mod.rs @@ -10,7 +10,7 @@ //! [spec]: https://tc39.es/ecma262/#sec-globalthis //! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis -use crate::{builtins::value::Value, BoaProfiler, Interpreter}; +use crate::{builtins::value::Value, BoaProfiler, Context}; #[cfg(test)] mod tests; @@ -24,8 +24,8 @@ impl GlobalThis { /// Initialize the `globalThis` property on the global object. #[inline] - pub(crate) fn init(interpreter: &mut Interpreter) -> (&'static str, Value) { - let global = interpreter.global(); + pub(crate) fn init(interpreter: &mut Context) -> (&'static str, Value) { + let global = interpreter.global_object(); let _timer = BoaProfiler::global().start_event(Self::NAME, "init"); (Self::NAME, global.clone()) diff --git a/boa/src/builtins/infinity/mod.rs b/boa/src/builtins/infinity/mod.rs index 7cb9b78fff3..785226ffc89 100644 --- a/boa/src/builtins/infinity/mod.rs +++ b/boa/src/builtins/infinity/mod.rs @@ -12,7 +12,7 @@ #[cfg(test)] mod tests; -use crate::{builtins::value::Value, BoaProfiler, Interpreter}; +use crate::{builtins::value::Value, BoaProfiler, Context}; /// JavaScript global `Infinity` property. #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] @@ -24,7 +24,7 @@ impl Infinity { /// Initialize the `Infinity` property on the global object. #[inline] - pub(crate) fn init(_interpreter: &mut Interpreter) -> (&'static str, Value) { + pub(crate) fn init(_interpreter: &mut Context) -> (&'static str, Value) { let _timer = BoaProfiler::global().start_event(Self::NAME, "init"); (Self::NAME, Value::from(f64::INFINITY)) diff --git a/boa/src/builtins/json/mod.rs b/boa/src/builtins/json/mod.rs index d674d18d5b5..66dd111ed95 100644 --- a/boa/src/builtins/json/mod.rs +++ b/boa/src/builtins/json/mod.rs @@ -19,7 +19,7 @@ use crate::{ property::{Property, PropertyKey}, value::Value, }, - exec::Interpreter, + context::Context, BoaProfiler, Result, }; use serde_json::{self, Value as JSONValue}; @@ -47,7 +47,7 @@ impl Json { /// /// [spec]: https://tc39.es/ecma262/#sec-json.parse /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse - pub(crate) fn parse(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn parse(_: &Value, args: &[Value], ctx: &mut Context) -> Result { match serde_json::from_str::( &args .get(0) @@ -77,7 +77,7 @@ impl Json { /// [polyfill]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse fn walk( reviver: &Value, - ctx: &mut Interpreter, + ctx: &mut Context, holder: &mut Value, key: &PropertyKey, ) -> Result { @@ -118,7 +118,7 @@ impl Json { /// /// [spec]: https://tc39.es/ecma262/#sec-json.stringify /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify - pub(crate) fn stringify(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn stringify(_: &Value, args: &[Value], ctx: &mut Context) -> Result { let object = match args.get(0) { Some(obj) if obj.is_symbol() || obj.is_function() || obj.is_undefined() => { return Ok(Value::undefined()) @@ -182,8 +182,8 @@ impl Json { /// Initialise the `JSON` object on the global object. #[inline] - pub(crate) fn init(interpreter: &mut Interpreter) -> (&'static str, Value) { - let global = interpreter.global(); + pub(crate) fn init(interpreter: &mut Context) -> (&'static str, Value) { + let global = interpreter.global_object(); let _timer = BoaProfiler::global().start_event(Self::NAME, "init"); let json = Value::new_object(Some(global)); diff --git a/boa/src/builtins/json/tests.rs b/boa/src/builtins/json/tests.rs index 02c661c594b..7bea94f67b4 100644 --- a/boa/src/builtins/json/tests.rs +++ b/boa/src/builtins/json/tests.rs @@ -1,14 +1,12 @@ use crate::{ builtins::{object::PROTOTYPE, value::same_value}, - exec::Interpreter, + context::Context, forward, forward_val, - realm::Realm, }; #[test] fn json_sanity() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!( forward(&mut engine, r#"JSON.parse('{"aaa":"bbb"}').aaa == 'bbb'"#), "true" @@ -24,8 +22,7 @@ fn json_sanity() { #[test] fn json_stringify_remove_undefined_values_from_objects() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward( &mut engine, @@ -38,8 +35,7 @@ fn json_stringify_remove_undefined_values_from_objects() { #[test] fn json_stringify_remove_function_values_from_objects() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward( &mut engine, @@ -52,8 +48,7 @@ fn json_stringify_remove_function_values_from_objects() { #[test] fn json_stringify_remove_symbols_from_objects() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward( &mut engine, @@ -66,8 +61,7 @@ fn json_stringify_remove_symbols_from_objects() { #[test] fn json_stringify_replacer_array_strings() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward( &mut engine, r#"JSON.stringify({aaa: 'bbb', bbb: 'ccc', ccc: 'ddd'}, ['aaa', 'bbb'])"#, @@ -78,8 +72,7 @@ fn json_stringify_replacer_array_strings() { #[test] fn json_stringify_replacer_array_numbers() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward( &mut engine, r#"JSON.stringify({ 0: 'aaa', 1: 'bbb', 2: 'ccc'}, [1, 2])"#, @@ -90,8 +83,7 @@ fn json_stringify_replacer_array_numbers() { #[test] fn json_stringify_replacer_function() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward( &mut engine, r#"JSON.stringify({ aaa: 1, bbb: 2}, (key, value) => { @@ -108,8 +100,7 @@ fn json_stringify_replacer_function() { #[test] fn json_stringify_arrays() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward(&mut engine, r#"JSON.stringify(['a', 'b'])"#); let expected = forward(&mut engine, r#"'["a","b"]'"#); @@ -118,8 +109,7 @@ fn json_stringify_arrays() { #[test] fn json_stringify_object_array() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward(&mut engine, r#"JSON.stringify([{a: 'b'}, {b: 'c'}])"#); let expected = forward(&mut engine, r#"'[{"a":"b"},{"b":"c"}]'"#); @@ -128,8 +118,7 @@ fn json_stringify_object_array() { #[test] fn json_stringify_array_converts_undefined_to_null() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward(&mut engine, r#"JSON.stringify([undefined])"#); let expected = forward(&mut engine, r#"'[null]'"#); @@ -138,8 +127,7 @@ fn json_stringify_array_converts_undefined_to_null() { #[test] fn json_stringify_array_converts_function_to_null() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward(&mut engine, r#"JSON.stringify([() => {}])"#); let expected = forward(&mut engine, r#"'[null]'"#); @@ -148,8 +136,7 @@ fn json_stringify_array_converts_function_to_null() { #[test] fn json_stringify_array_converts_symbol_to_null() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward(&mut engine, r#"JSON.stringify([Symbol()])"#); let expected = forward(&mut engine, r#"'[null]'"#); @@ -157,8 +144,7 @@ fn json_stringify_array_converts_symbol_to_null() { } #[test] fn json_stringify_function_replacer_propogate_error() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual = forward( &mut engine, @@ -179,8 +165,7 @@ fn json_stringify_function_replacer_propogate_error() { #[test] fn json_stringify_function() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual_function = forward(&mut engine, r#"JSON.stringify(() => {})"#); let expected = forward(&mut engine, r#"undefined"#); @@ -190,8 +175,7 @@ fn json_stringify_function() { #[test] fn json_stringify_undefined() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual_undefined = forward(&mut engine, r#"JSON.stringify(undefined)"#); let expected = forward(&mut engine, r#"undefined"#); @@ -200,8 +184,7 @@ fn json_stringify_undefined() { #[test] fn json_stringify_symbol() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual_symbol = forward(&mut engine, r#"JSON.stringify(Symbol())"#); let expected = forward(&mut engine, r#"undefined"#); @@ -211,8 +194,7 @@ fn json_stringify_symbol() { #[test] fn json_stringify_no_args() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual_no_args = forward(&mut engine, r#"JSON.stringify()"#); let expected = forward(&mut engine, r#"undefined"#); @@ -222,8 +204,7 @@ fn json_stringify_no_args() { #[test] fn json_parse_array_with_reviver() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let result = forward_val( &mut engine, r#"JSON.parse('[1,2,3,4]', function(k, v){ @@ -254,8 +235,7 @@ fn json_parse_array_with_reviver() { #[test] fn json_parse_object_with_reviver() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let result = forward( &mut engine, r#" @@ -281,8 +261,7 @@ fn json_parse_object_with_reviver() { #[test] fn json_parse_sets_prototypes() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" const jsonString = "{ \"ob\":{\"ject\":1}, @@ -304,13 +283,11 @@ fn json_parse_sets_prototypes() { .prototype() .clone(); let global_object_prototype = engine - .realm - .global_obj + .global_object() .get_field("Object") .get_field(PROTOTYPE); let global_array_prototype = engine - .realm - .global_obj + .global_object() .get_field("Array") .get_field(PROTOTYPE); assert_eq!( @@ -322,8 +299,7 @@ fn json_parse_sets_prototypes() { #[test] fn json_fields_should_be_enumerable() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let actual_object = forward( &mut engine, r#" diff --git a/boa/src/builtins/map/mod.rs b/boa/src/builtins/map/mod.rs index 46aa475ba47..f5ea371d4f8 100644 --- a/boa/src/builtins/map/mod.rs +++ b/boa/src/builtins/map/mod.rs @@ -7,7 +7,7 @@ use crate::{ property::{Attribute, Property}, value::Value, }, - exec::Interpreter, + context::Context, BoaProfiler, Result, }; use ordered_map::OrderedMap; @@ -44,7 +44,7 @@ impl Map { /// /// [spec]: https://tc39.es/ecma262/#sec-map.prototype.set /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/set - pub(crate) fn set(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn set(this: &Value, args: &[Value], ctx: &mut Context) -> Result { let (key, value) = match args.len() { 0 => (Value::Undefined, Value::Undefined), 1 => (args[0].clone(), Value::Undefined), @@ -77,7 +77,7 @@ impl Map { /// /// [spec]: https://tc39.es/ecma262/#sec-map.prototype.delete /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/delete - pub(crate) fn delete(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn delete(this: &Value, args: &[Value], ctx: &mut Context) -> Result { let undefined = Value::Undefined; let key = match args.len() { 0 => &undefined, @@ -109,7 +109,7 @@ impl Map { /// /// [spec]: https://tc39.es/ecma262/#sec-map.prototype.get /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get - pub(crate) fn get(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn get(this: &Value, args: &[Value], ctx: &mut Context) -> Result { let undefined = Value::Undefined; let key = match args.len() { 0 => &undefined, @@ -140,7 +140,7 @@ impl Map { /// /// [spec]: https://tc39.es/ecma262/#sec-map.prototype.clear /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/clear - pub(crate) fn clear(this: &Value, _: &[Value], _: &mut Interpreter) -> Result { + pub(crate) fn clear(this: &Value, _: &[Value], _: &mut Context) -> Result { this.set_data(ObjectData::Map(OrderedMap::new())); Self::set_size(this, 0); @@ -158,7 +158,7 @@ impl Map { /// /// [spec]: https://tc39.es/ecma262/#sec-map.prototype.has /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/has - pub(crate) fn has(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn has(this: &Value, args: &[Value], ctx: &mut Context) -> Result { let undefined = Value::Undefined; let key = match args.len() { 0 => &undefined, @@ -188,7 +188,7 @@ impl Map { pub(crate) fn for_each( this: &Value, args: &[Value], - interpreter: &mut Interpreter, + interpreter: &mut Context, ) -> Result { if args.is_empty() { return Err(Value::from("Missing argument for Map.prototype.forEach")); @@ -227,12 +227,12 @@ impl Map { } /// Create a new map - pub(crate) fn make_map(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn make_map(this: &Value, args: &[Value], ctx: &mut Context) -> Result { // Make a new Object which will internally represent the Array (mapping // between indices and values): this creates an Object with no prototype // Set Prototype - let prototype = ctx.realm.global_obj.get_field("Map").get_field(PROTOTYPE); + let prototype = ctx.global_object().get_field("Map").get_field(PROTOTYPE); this.as_object_mut() .expect("this is array object") @@ -284,8 +284,8 @@ impl Map { } /// Initialise the `Map` object on the global object. - pub(crate) fn init(interpreter: &mut Interpreter) -> (&'static str, Value) { - let global = interpreter.global(); + pub(crate) fn init(interpreter: &mut Context) -> (&'static str, Value) { + let global = interpreter.global_object(); let _timer = BoaProfiler::global().start_event(Self::NAME, "init"); // Create prototype diff --git a/boa/src/builtins/map/tests.rs b/boa/src/builtins/map/tests.rs index a0d7b622944..39c934280ce 100644 --- a/boa/src/builtins/map/tests.rs +++ b/boa/src/builtins/map/tests.rs @@ -1,9 +1,8 @@ -use crate::{exec::Interpreter, forward, realm::Realm}; +use crate::{context::Context, forward}; #[test] fn construct_empty() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var empty = new Map(); "#; @@ -14,8 +13,7 @@ fn construct_empty() { #[test] fn construct_from_array() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" let map = new Map([["1", "one"], ["2", "two"]]); "#; @@ -26,8 +24,7 @@ fn construct_from_array() { #[test] fn clone() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" let original = new Map([["1", "one"], ["2", "two"]]); let clone = new Map(original); @@ -48,8 +45,7 @@ fn clone() { #[test] fn merge() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" let first = new Map([["1", "one"], ["2", "two"]]); let second = new Map([["2", "second two"], ["3", "three"]]); @@ -68,8 +64,7 @@ fn merge() { #[test] fn get() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" let map = new Map([["1", "one"], ["2", "two"]]); "#; @@ -86,8 +81,7 @@ fn get() { #[test] fn set() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" let map = new Map(); "#; @@ -105,8 +99,7 @@ fn set() { #[test] fn clear() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" let map = new Map([["1", "one"], ["2", "two"]]); map.clear(); @@ -118,8 +111,7 @@ fn clear() { #[test] fn delete() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" let map = new Map([["1", "one"], ["2", "two"]]); "#; @@ -134,8 +126,7 @@ fn delete() { #[test] fn has() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" let map = new Map([["1", "one"]]); "#; @@ -150,8 +141,7 @@ fn has() { #[test] fn for_each() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" let map = new Map([[1, 5], [2, 10], [3, 15]]); let valueSum = 0; @@ -172,8 +162,7 @@ fn for_each() { #[test] fn modify_key() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" let obj = new Object(); let map = new Map([[obj, "one"]]); @@ -186,8 +175,7 @@ fn modify_key() { #[test] fn order() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" let map = new Map([[1, "one"]]); map.set(2, "two"); @@ -213,8 +201,7 @@ fn order() { #[test] fn recursive_display() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" let map = new Map(); let array = new Array([map]); @@ -229,8 +216,7 @@ fn recursive_display() { #[test] fn not_a_function() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r" try { let map = Map() diff --git a/boa/src/builtins/math/mod.rs b/boa/src/builtins/math/mod.rs index be769e61f10..8c2a48e1fec 100644 --- a/boa/src/builtins/math/mod.rs +++ b/boa/src/builtins/math/mod.rs @@ -13,7 +13,7 @@ use crate::{ builtins::{function::make_builtin_fn, value::Value}, - exec::Interpreter, + context::Context, BoaProfiler, Result, }; use std::f64; @@ -37,7 +37,7 @@ impl Math { /// /// [spec]: https://tc39.es/ecma262/#sec-math.abs /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/abs - pub(crate) fn abs(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn abs(_: &Value, args: &[Value], ctx: &mut Context) -> Result { Ok(args .get(0) .map(|x| x.to_number(ctx)) @@ -54,7 +54,7 @@ impl Math { /// /// [spec]: https://tc39.es/ecma262/#sec-math.acos /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/acos - pub(crate) fn acos(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn acos(_: &Value, args: &[Value], ctx: &mut Context) -> Result { Ok(args .get(0) .map(|x| x.to_number(ctx)) @@ -71,7 +71,7 @@ impl Math { /// /// [spec]: https://tc39.es/ecma262/#sec-math.acosh /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/acosh - pub(crate) fn acosh(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn acosh(_: &Value, args: &[Value], ctx: &mut Context) -> Result { Ok(args .get(0) .map(|x| x.to_number(ctx)) @@ -88,7 +88,7 @@ impl Math { /// /// [spec]: https://tc39.es/ecma262/#sec-math.asin /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/asin - pub(crate) fn asin(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn asin(_: &Value, args: &[Value], ctx: &mut Context) -> Result { Ok(args .get(0) .map(|x| x.to_number(ctx)) @@ -105,7 +105,7 @@ impl Math { /// /// [spec]: https://tc39.es/ecma262/#sec-math.asinh /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/asinh - pub(crate) fn asinh(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn asinh(_: &Value, args: &[Value], ctx: &mut Context) -> Result { Ok(args .get(0) .map(|x| x.to_number(ctx)) @@ -122,7 +122,7 @@ impl Math { /// /// [spec]: https://tc39.es/ecma262/#sec-math.atan /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/atan - pub(crate) fn atan(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn atan(_: &Value, args: &[Value], ctx: &mut Context) -> Result { Ok(args .get(0) .map(|x| x.to_number(ctx)) @@ -139,7 +139,7 @@ impl Math { /// /// [spec]: https://tc39.es/ecma262/#sec-math.atanh /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/atanh - pub(crate) fn atanh(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn atanh(_: &Value, args: &[Value], ctx: &mut Context) -> Result { Ok(args .get(0) .map(|x| x.to_number(ctx)) @@ -156,7 +156,7 @@ impl Math { /// /// [spec]: https://tc39.es/ecma262/#sec-math.atan2 /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/atan2 - pub(crate) fn atan2(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn atan2(_: &Value, args: &[Value], ctx: &mut Context) -> Result { Ok(match ( args.get(0).map(|x| x.to_number(ctx)).transpose()?, args.get(1).map(|x| x.to_number(ctx)).transpose()?, @@ -175,7 +175,7 @@ impl Math { /// /// [spec]: https://tc39.es/ecma262/#sec-math.cbrt /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/cbrt - pub(crate) fn cbrt(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn cbrt(_: &Value, args: &[Value], ctx: &mut Context) -> Result { Ok(args .get(0) .map(|x| x.to_number(ctx)) @@ -192,7 +192,7 @@ impl Math { /// /// [spec]: https://tc39.es/ecma262/#sec-math.ceil /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/ceil - pub(crate) fn ceil(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn ceil(_: &Value, args: &[Value], ctx: &mut Context) -> Result { Ok(args .get(0) .map(|x| x.to_number(ctx)) @@ -209,7 +209,7 @@ impl Math { /// /// [spec]: https://tc39.es/ecma262/#sec-math.clz32 /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/clz32 - pub(crate) fn clz32(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn clz32(_: &Value, args: &[Value], ctx: &mut Context) -> Result { Ok(args .get(0) .map(|x| x.to_u32(ctx)) @@ -227,7 +227,7 @@ impl Math { /// /// [spec]: https://tc39.es/ecma262/#sec-math.cos /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/cos - pub(crate) fn cos(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn cos(_: &Value, args: &[Value], ctx: &mut Context) -> Result { Ok(args .get(0) .map(|x| x.to_number(ctx)) @@ -244,7 +244,7 @@ impl Math { /// /// [spec]: https://tc39.es/ecma262/#sec-math.cosh /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/cosh - pub(crate) fn cosh(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn cosh(_: &Value, args: &[Value], ctx: &mut Context) -> Result { Ok(args .get(0) .map(|x| x.to_number(ctx)) @@ -261,7 +261,7 @@ impl Math { /// /// [spec]: https://tc39.es/ecma262/#sec-math.exp /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/exp - pub(crate) fn exp(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn exp(_: &Value, args: &[Value], ctx: &mut Context) -> Result { Ok(args .get(0) .map(|x| x.to_number(ctx)) @@ -280,7 +280,7 @@ impl Math { /// /// [spec]: https://tc39.es/ecma262/#sec-math.expm1 /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/expm1 - pub(crate) fn expm1(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn expm1(_: &Value, args: &[Value], ctx: &mut Context) -> Result { Ok(args .get(0) .map(|x| x.to_number(ctx)) @@ -297,7 +297,7 @@ impl Math { /// /// [spec]: https://tc39.es/ecma262/#sec-math.floor /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor - pub(crate) fn floor(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn floor(_: &Value, args: &[Value], ctx: &mut Context) -> Result { Ok(args .get(0) .map(|x| x.to_number(ctx)) @@ -314,7 +314,7 @@ impl Math { /// /// [spec]: https://tc39.es/ecma262/#sec-math.fround /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/fround - pub(crate) fn fround(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn fround(_: &Value, args: &[Value], ctx: &mut Context) -> Result { Ok(args .get(0) .map(|x| x.to_number(ctx)) @@ -331,7 +331,7 @@ impl Math { /// /// [spec]: https://tc39.es/ecma262/#sec-math.hypot /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/hypot - pub(crate) fn hypot(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn hypot(_: &Value, args: &[Value], ctx: &mut Context) -> Result { let mut result = 0f64; for arg in args { let x = arg.to_number(ctx)?; @@ -348,7 +348,7 @@ impl Math { /// /// [spec]: https://tc39.es/ecma262/#sec-math.imul /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/imul - pub(crate) fn imul(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn imul(_: &Value, args: &[Value], ctx: &mut Context) -> Result { Ok(match ( args.get(0).map(|x| x.to_u32(ctx)).transpose()?, args.get(1).map(|x| x.to_u32(ctx)).transpose()?, @@ -367,7 +367,7 @@ impl Math { /// /// [spec]: https://tc39.es/ecma262/#sec-math.log /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log - pub(crate) fn log(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn log(_: &Value, args: &[Value], ctx: &mut Context) -> Result { Ok(args .get(0) .map(|x| x.to_number(ctx)) @@ -384,7 +384,7 @@ impl Math { /// /// [spec]: https://tc39.es/ecma262/#sec-math.log1p /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log1p - pub(crate) fn log1p(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn log1p(_: &Value, args: &[Value], ctx: &mut Context) -> Result { Ok(args .get(0) .map(|x| x.to_number(ctx)) @@ -401,7 +401,7 @@ impl Math { /// /// [spec]: https://tc39.es/ecma262/#sec-math.log10 /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log10 - pub(crate) fn log10(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn log10(_: &Value, args: &[Value], ctx: &mut Context) -> Result { Ok(args .get(0) .map(|x| x.to_number(ctx)) @@ -418,7 +418,7 @@ impl Math { /// /// [spec]: https://tc39.es/ecma262/#sec-math.log2 /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log2 - pub(crate) fn log2(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn log2(_: &Value, args: &[Value], ctx: &mut Context) -> Result { Ok(args .get(0) .map(|x| x.to_number(ctx)) @@ -435,7 +435,7 @@ impl Math { /// /// [spec]: https://tc39.es/ecma262/#sec-math.max /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max - pub(crate) fn max(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn max(_: &Value, args: &[Value], ctx: &mut Context) -> Result { let mut max = f64::NEG_INFINITY; for arg in args { let num = arg.to_number(ctx)?; @@ -452,7 +452,7 @@ impl Math { /// /// [spec]: https://tc39.es/ecma262/#sec-math.min /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/min - pub(crate) fn min(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn min(_: &Value, args: &[Value], ctx: &mut Context) -> Result { let mut min = f64::INFINITY; for arg in args { let num = arg.to_number(ctx)?; @@ -469,7 +469,7 @@ impl Math { /// /// [spec]: https://tc39.es/ecma262/#sec-math.pow /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/pow - pub(crate) fn pow(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn pow(_: &Value, args: &[Value], ctx: &mut Context) -> Result { Ok(match ( args.get(0).map(|x| x.to_number(ctx)).transpose()?, args.get(1).map(|x| x.to_number(ctx)).transpose()?, @@ -488,7 +488,7 @@ impl Math { /// /// [spec]: https://tc39.es/ecma262/#sec-math.random /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random - pub(crate) fn random(_: &Value, _: &[Value], _: &mut Interpreter) -> Result { + pub(crate) fn random(_: &Value, _: &[Value], _: &mut Context) -> Result { Ok(rand::random::().into()) } @@ -500,7 +500,7 @@ impl Math { /// /// [spec]: https://tc39.es/ecma262/#sec-math.round /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round - pub(crate) fn round(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn round(_: &Value, args: &[Value], ctx: &mut Context) -> Result { Ok(args .get(0) .map(|x| x.to_number(ctx)) @@ -517,7 +517,7 @@ impl Math { /// /// [spec]: https://tc39.es/ecma262/#sec-math.sign /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sign - pub(crate) fn sign(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn sign(_: &Value, args: &[Value], ctx: &mut Context) -> Result { Ok(args .get(0) .map(|x| x.to_number(ctx)) @@ -543,7 +543,7 @@ impl Math { /// /// [spec]: https://tc39.es/ecma262/#sec-math.sin /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sin - pub(crate) fn sin(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn sin(_: &Value, args: &[Value], ctx: &mut Context) -> Result { Ok(args .get(0) .map(|x| x.to_number(ctx)) @@ -560,7 +560,7 @@ impl Math { /// /// [spec]: https://tc39.es/ecma262/#sec-math.sinh /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sinh - pub(crate) fn sinh(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn sinh(_: &Value, args: &[Value], ctx: &mut Context) -> Result { Ok(args .get(0) .map(|x| x.to_number(ctx)) @@ -577,7 +577,7 @@ impl Math { /// /// [spec]: https://tc39.es/ecma262/#sec-math.sqrt /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sqrt - pub(crate) fn sqrt(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn sqrt(_: &Value, args: &[Value], ctx: &mut Context) -> Result { Ok(args .get(0) .map(|x| x.to_number(ctx)) @@ -594,7 +594,7 @@ impl Math { /// /// [spec]: https://tc39.es/ecma262/#sec-math.tan /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/tan - pub(crate) fn tan(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn tan(_: &Value, args: &[Value], ctx: &mut Context) -> Result { Ok(args .get(0) .map(|x| x.to_number(ctx)) @@ -611,7 +611,7 @@ impl Math { /// /// [spec]: https://tc39.es/ecma262/#sec-math.tanh /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/tanh - pub(crate) fn tanh(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn tanh(_: &Value, args: &[Value], ctx: &mut Context) -> Result { Ok(args .get(0) .map(|x| x.to_number(ctx)) @@ -628,7 +628,7 @@ impl Math { /// /// [spec]: https://tc39.es/ecma262/#sec-math.trunc /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/trunc - pub(crate) fn trunc(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn trunc(_: &Value, args: &[Value], ctx: &mut Context) -> Result { Ok(args .get(0) .map(|x| x.to_number(ctx)) @@ -638,8 +638,8 @@ impl Math { } /// Create a new `Math` object - pub(crate) fn create(interpreter: &mut Interpreter) -> Value { - let global = interpreter.global(); + pub(crate) fn create(interpreter: &mut Context) -> Value { + let global = interpreter.global_object(); let _timer = BoaProfiler::global().start_event("math:create", "init"); let math = Value::new_object(Some(global)); @@ -696,7 +696,7 @@ impl Math { /// Initialise the `Math` object on the global object. #[inline] - pub(crate) fn init(interpreter: &mut Interpreter) -> (&'static str, Value) { + pub(crate) fn init(interpreter: &mut Context) -> (&'static str, Value) { let _timer = BoaProfiler::global().start_event(Self::NAME, "init"); (Self::NAME, Self::create(interpreter)) diff --git a/boa/src/builtins/math/tests.rs b/boa/src/builtins/math/tests.rs index 506528c6642..d25ef3864c7 100644 --- a/boa/src/builtins/math/tests.rs +++ b/boa/src/builtins/math/tests.rs @@ -1,12 +1,11 @@ #![allow(clippy::float_cmp)] -use crate::{exec::Interpreter, forward, forward_val, realm::Realm}; +use crate::{context::Context, forward, forward_val}; use std::f64; #[test] fn abs() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var a = Math.abs(3 - 5); var b = Math.abs(1.23456 - 7.89012); @@ -23,8 +22,7 @@ fn abs() { #[test] fn acos() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var a = Math.acos(8 / 10); var b = Math.acos(5 / 3); @@ -47,8 +45,7 @@ fn acos() { #[test] fn acosh() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var a = Math.acosh(2); var b = Math.acosh(-1); @@ -68,8 +65,7 @@ fn acosh() { #[test] fn asin() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var a = Math.asin(6 / 10); var b = Math.asin(5 / 3); @@ -86,8 +82,7 @@ fn asin() { #[test] fn asinh() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var a = Math.asinh(1); var b = Math.asinh(0); @@ -104,8 +99,7 @@ fn asinh() { #[test] fn atan() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var a = Math.atan(1); var b = Math.atan(0); @@ -125,8 +119,7 @@ fn atan() { #[test] fn atan2() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var a = Math.atan2(90, 15); var b = Math.atan2(15, 90); @@ -143,8 +136,7 @@ fn atan2() { #[test] fn cbrt() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var a = Math.cbrt(64); var b = Math.cbrt(-1); @@ -164,8 +156,7 @@ fn cbrt() { #[test] fn ceil() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var a = Math.ceil(1.95); var b = Math.ceil(4); @@ -186,8 +177,7 @@ fn ceil() { #[test] #[allow(clippy::many_single_char_names)] fn clz32() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var a = Math.clz32(); var b = Math.clz32({}); @@ -222,8 +212,7 @@ fn clz32() { #[test] fn cos() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var a = Math.cos(0); var b = Math.cos(1); @@ -240,8 +229,7 @@ fn cos() { #[test] fn cosh() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var a = Math.cosh(0); var b = Math.cosh(1); @@ -261,8 +249,7 @@ fn cosh() { #[test] fn exp() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var a = Math.exp(0); var b = Math.exp(-1); @@ -283,8 +270,7 @@ fn exp() { #[test] #[allow(clippy::many_single_char_names)] fn expm1() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var a = Math.expm1(); var b = Math.expm1({}); @@ -329,8 +315,7 @@ fn expm1() { #[test] fn floor() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var a = Math.floor(1.95); var b = Math.floor(-3.01); @@ -351,8 +336,7 @@ fn floor() { #[test] #[allow(clippy::many_single_char_names)] fn fround() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var a = Math.fround(NaN); var b = Math.fround(Infinity); @@ -385,8 +369,7 @@ fn fround() { #[test] #[allow(clippy::many_single_char_names)] fn hypot() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var a = Math.hypot(); var b = Math.hypot(3, 4); @@ -419,8 +402,7 @@ fn hypot() { #[test] #[allow(clippy::many_single_char_names)] fn imul() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var a = Math.imul(3, 4); var b = Math.imul(-5, 12); @@ -449,8 +431,7 @@ fn imul() { #[test] fn log() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var a = Math.log(1); var b = Math.log(10); @@ -471,8 +452,7 @@ fn log() { #[test] #[allow(clippy::many_single_char_names)] fn log1p() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var a = Math.log1p(1); var b = Math.log1p(0); @@ -504,8 +484,7 @@ fn log1p() { #[test] fn log10() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var a = Math.log10(2); var b = Math.log10(1); @@ -525,8 +504,7 @@ fn log10() { #[test] fn log2() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var a = Math.log2(3); var b = Math.log2(1); @@ -546,8 +524,7 @@ fn log2() { #[test] fn max() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var a = Math.max(10, 20); var b = Math.max(-10, -20); @@ -567,8 +544,7 @@ fn max() { #[test] fn min() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var a = Math.min(10, 20); var b = Math.min(-10, -20); @@ -588,8 +564,7 @@ fn min() { #[test] fn pow() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var a = Math.pow(2, 10); var b = Math.pow(-7, 2); @@ -612,8 +587,7 @@ fn pow() { #[test] fn round() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var a = Math.round(20.5); var b = Math.round(-20.3); @@ -630,8 +604,7 @@ fn round() { #[test] fn sign() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var a = Math.sign(3); var b = Math.sign(-3); @@ -651,8 +624,7 @@ fn sign() { #[test] fn sin() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var a = Math.sin(0); var b = Math.sin(1); @@ -669,8 +641,7 @@ fn sin() { #[test] fn sinh() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var a = Math.sinh(0); var b = Math.sinh(1); @@ -687,8 +658,7 @@ fn sinh() { #[test] fn sqrt() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var a = Math.sqrt(0); var b = Math.sqrt(2); @@ -708,8 +678,7 @@ fn sqrt() { #[test] fn tan() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var a = Math.tan(1.1); "#; @@ -727,8 +696,7 @@ fn tan() { #[test] fn tanh() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var a = Math.tanh(1); var b = Math.tanh(0); @@ -745,8 +713,7 @@ fn tanh() { #[test] fn trunc() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var a = Math.trunc(13.37); var b = Math.trunc(0.123); diff --git a/boa/src/builtins/mod.rs b/boa/src/builtins/mod.rs index 29d1628c33b..e64c518653c 100644 --- a/boa/src/builtins/mod.rs +++ b/boa/src/builtins/mod.rs @@ -42,11 +42,11 @@ pub(crate) use self::{ undefined::Undefined, value::Value, }; -use crate::Interpreter; +use crate::Context; /// Initializes builtin objects and functions #[inline] -pub fn init(interpreter: &mut Interpreter) { +pub fn init(interpreter: &mut Context) { let globals = [ // The `Function` global must be initialized before other types. function::init, @@ -78,7 +78,7 @@ pub fn init(interpreter: &mut Interpreter) { for init in &globals { let (name, value) = init(interpreter); - let global = interpreter.global(); + let global = interpreter.global_object(); match global { Value::Object(ref global_object) => { global_object.borrow_mut().insert_field(name, value); diff --git a/boa/src/builtins/nan/mod.rs b/boa/src/builtins/nan/mod.rs index eeb29c039c9..043b7cb2993 100644 --- a/boa/src/builtins/nan/mod.rs +++ b/boa/src/builtins/nan/mod.rs @@ -13,7 +13,7 @@ #[cfg(test)] mod tests; -use crate::{builtins::value::Value, BoaProfiler, Interpreter}; +use crate::{builtins::value::Value, BoaProfiler, Context}; /// JavaScript global `NaN` property. #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] @@ -25,7 +25,7 @@ impl NaN { /// Initialize the `NaN` property on the global object. #[inline] - pub(crate) fn init(_interpreter: &mut Interpreter) -> (&'static str, Value) { + pub(crate) fn init(_interpreter: &mut Context) -> (&'static str, Value) { let _timer = BoaProfiler::global().start_event(Self::NAME, "init"); (Self::NAME, Value::from(f64::NAN)) diff --git a/boa/src/builtins/number/mod.rs b/boa/src/builtins/number/mod.rs index 28dd2c7468b..70f8498b3ad 100644 --- a/boa/src/builtins/number/mod.rs +++ b/boa/src/builtins/number/mod.rs @@ -18,7 +18,7 @@ use super::{ object::ObjectData, value::AbstractRelation, }; -use crate::{builtins::value::Value, exec::Interpreter, BoaProfiler, Result}; +use crate::{builtins::value::Value, context::Context, BoaProfiler, Result}; use num_traits::float::FloatCore; mod conversions; @@ -102,7 +102,7 @@ impl Number { /// - [ECMAScript reference][spec] /// /// [spec]: https://tc39.es/ecma262/#sec-thisnumbervalue - fn this_number_value(value: &Value, ctx: &mut Interpreter) -> Result { + fn this_number_value(value: &Value, ctx: &mut Context) -> Result { match *value { Value::Integer(integer) => return Ok(f64::from(integer)), Value::Rational(rational) => return Ok(rational), @@ -129,11 +129,7 @@ impl Number { /// `[[Construct]]` - Creates a Number instance /// /// `[[Call]]` - Creates a number primitive - pub(crate) fn make_number( - this: &Value, - args: &[Value], - ctx: &mut Interpreter, - ) -> Result { + pub(crate) fn make_number(this: &Value, args: &[Value], ctx: &mut Context) -> Result { let data = match args.get(0) { Some(ref value) => value.to_numeric_number(ctx)?, None => 0.0, @@ -157,7 +153,7 @@ impl Number { pub(crate) fn to_exponential( this: &Value, _args: &[Value], - ctx: &mut Interpreter, + ctx: &mut Context, ) -> Result { let this_num = Self::this_number_value(this, ctx)?; let this_str_num = Self::num_to_exponential(this_num); @@ -175,7 +171,7 @@ impl Number { /// [spec]: https://tc39.es/ecma262/#sec-number.prototype.tofixed /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed #[allow(clippy::wrong_self_convention)] - pub(crate) fn to_fixed(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn to_fixed(this: &Value, args: &[Value], ctx: &mut Context) -> Result { let this_num = Self::this_number_value(this, ctx)?; let precision = match args.get(0) { Some(n) => match n.to_integer(ctx)? as i32 { @@ -205,7 +201,7 @@ impl Number { pub(crate) fn to_locale_string( this: &Value, _args: &[Value], - ctx: &mut Interpreter, + ctx: &mut Context, ) -> Result { let this_num = Self::this_number_value(this, ctx)?; let this_str_num = format!("{}", this_num); @@ -223,11 +219,7 @@ impl Number { /// [spec]: https://tc39.es/ecma262/#sec-number.prototype.toexponential /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toPrecision #[allow(clippy::wrong_self_convention)] - pub(crate) fn to_precision( - this: &Value, - args: &[Value], - ctx: &mut Interpreter, - ) -> Result { + pub(crate) fn to_precision(this: &Value, args: &[Value], ctx: &mut Context) -> Result { let this_num = Self::this_number_value(this, ctx)?; let _num_str_len = format!("{}", this_num).len(); let _precision = match args.get(0) { @@ -381,7 +373,7 @@ impl Number { /// [spec]: https://tc39.es/ecma262/#sec-number.prototype.tostring /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString #[allow(clippy::wrong_self_convention)] - pub(crate) fn to_string(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn to_string(this: &Value, args: &[Value], ctx: &mut Context) -> Result { // 1. Let x be ? thisNumberValue(this value). let x = Self::this_number_value(this, ctx)?; @@ -436,7 +428,7 @@ impl Number { /// /// [spec]: https://tc39.es/ecma262/#sec-number.prototype.valueof /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/valueOf - pub(crate) fn value_of(this: &Value, _args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn value_of(this: &Value, _args: &[Value], ctx: &mut Context) -> Result { Ok(Value::from(Self::this_number_value(this, ctx)?)) } @@ -454,11 +446,7 @@ impl Number { /// /// [spec]: https://tc39.es/ecma262/#sec-parseint-string-radix /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt - pub(crate) fn parse_int( - _this: &Value, - args: &[Value], - _ctx: &mut Interpreter, - ) -> Result { + pub(crate) fn parse_int(_this: &Value, args: &[Value], _ctx: &mut Context) -> Result { if let (Some(val), r) = (args.get(0), args.get(1)) { let mut radix = if let Some(rx) = r { if let Value::Integer(i) = rx { @@ -524,11 +512,7 @@ impl Number { /// /// [spec]: https://tc39.es/ecma262/#sec-parsefloat-string /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat - pub(crate) fn parse_float( - _this: &Value, - args: &[Value], - _ctx: &mut Interpreter, - ) -> Result { + pub(crate) fn parse_float(_this: &Value, args: &[Value], _ctx: &mut Context) -> Result { if let Some(val) = args.get(0) { match val { Value::String(s) => { @@ -573,7 +557,7 @@ impl Number { pub(crate) fn global_is_finite( _this: &Value, args: &[Value], - ctx: &mut Interpreter, + ctx: &mut Context, ) -> Result { if let Some(value) = args.get(0) { let number = value.to_number(ctx)?; @@ -597,11 +581,7 @@ impl Number { /// /// [spec]: https://tc39.es/ecma262/#sec-isnan-number /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN - pub(crate) fn global_is_nan( - _this: &Value, - args: &[Value], - ctx: &mut Interpreter, - ) -> Result { + pub(crate) fn global_is_nan(_this: &Value, args: &[Value], ctx: &mut Context) -> Result { if let Some(value) = args.get(0) { let number = value.to_number(ctx)?; Ok(number.is_nan().into()) @@ -627,7 +607,7 @@ impl Number { pub(crate) fn number_is_finite( _this: &Value, args: &[Value], - _ctx: &mut Interpreter, + _ctx: &mut Context, ) -> Result { Ok(Value::from(if let Some(val) = args.get(0) { match val { @@ -653,7 +633,7 @@ impl Number { pub(crate) fn number_is_integer( _this: &Value, args: &[Value], - _ctx: &mut Interpreter, + _ctx: &mut Context, ) -> Result { Ok(args.get(0).map_or(false, Self::is_integer).into()) } @@ -675,7 +655,7 @@ impl Number { pub(crate) fn number_is_nan( _this: &Value, args: &[Value], - _ctx: &mut Interpreter, + _ctx: &mut Context, ) -> Result { Ok(Value::from(if let Some(val) = args.get(0) { match val { @@ -705,7 +685,7 @@ impl Number { pub(crate) fn is_safe_integer( _this: &Value, args: &[Value], - _ctx: &mut Interpreter, + _ctx: &mut Context, ) -> Result { Ok(Value::from(match args.get(0) { Some(Value::Integer(_)) => true, @@ -740,8 +720,8 @@ impl Number { /// Initialise the `Number` object on the global object. #[inline] - pub(crate) fn init(interpreter: &mut Interpreter) -> (&'static str, Value) { - let global = interpreter.global(); + pub(crate) fn init(interpreter: &mut Context) -> (&'static str, Value) { + let global = interpreter.global_object(); let _timer = BoaProfiler::global().start_event(Self::NAME, "init"); let prototype = Value::new_object(Some(global)); diff --git a/boa/src/builtins/number/tests.rs b/boa/src/builtins/number/tests.rs index 2f95a928e7d..65eceade905 100644 --- a/boa/src/builtins/number/tests.rs +++ b/boa/src/builtins/number/tests.rs @@ -1,11 +1,10 @@ #![allow(clippy::float_cmp)] -use crate::{builtins::Number, exec::Interpreter, forward, forward_val, realm::Realm}; +use crate::{builtins::Number, context::Context, forward, forward_val}; #[test] fn integer_number_primitive_to_number_object() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let scenario = r#" (100).toString() === "100" @@ -16,8 +15,7 @@ fn integer_number_primitive_to_number_object() { #[test] fn call_number() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var default_zero = Number(); var int_one = Number(1); @@ -51,8 +49,7 @@ fn call_number() { #[test] fn to_exponential() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var default_exp = Number().toExponential(); var int_exp = Number(5).toExponential(); @@ -80,8 +77,7 @@ fn to_exponential() { #[test] fn to_fixed() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var default_fixed = Number().toFixed(); var pos_fixed = Number("3.456e+4").toFixed(); @@ -106,8 +102,7 @@ fn to_fixed() { #[test] fn to_locale_string() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var default_locale = Number().toLocaleString(); var small_locale = Number(5).toLocaleString(); @@ -133,8 +128,7 @@ fn to_locale_string() { #[test] #[ignore] fn to_precision() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var default_precision = Number().toPrecision(); var low_precision = Number(123456789).toPrecision(1); @@ -165,8 +159,7 @@ fn to_precision() { #[test] fn to_string() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!("\"NaN\"", &forward(&mut engine, "Number(NaN).toString()")); assert_eq!( @@ -338,8 +331,7 @@ fn to_string() { #[test] fn num_to_string_exponential() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!("\"0\"", forward(&mut engine, "(0).toString()")); assert_eq!("\"0\"", forward(&mut engine, "(-0).toString()")); @@ -377,8 +369,7 @@ fn num_to_string_exponential() { #[test] fn value_of() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); // TODO: In addition to parsing numbers from strings, parse them bare As of October 2019 // the parser does not understand scientific e.g., Xe+Y or -Xe-Y notation. let init = r#" @@ -436,8 +427,7 @@ fn same_value_zero() { #[test] fn from_bigint() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(&forward(&mut engine, "Number(0n)"), "0",); assert_eq!(&forward(&mut engine, "Number(100000n)"), "100000",); @@ -447,8 +437,7 @@ fn from_bigint() { #[test] fn number_constants() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert!(!forward_val(&mut engine, "Number.EPSILON") .unwrap() @@ -475,48 +464,42 @@ fn number_constants() { #[test] fn parse_int_simple() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(&forward(&mut engine, "parseInt(\"6\")"), "6"); } #[test] fn parse_int_negative() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(&forward(&mut engine, "parseInt(\"-9\")"), "-9"); } #[test] fn parse_int_already_int() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(&forward(&mut engine, "parseInt(100)"), "100"); } #[test] fn parse_int_float() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(&forward(&mut engine, "parseInt(100.5)"), "100"); } #[test] fn parse_int_float_str() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(&forward(&mut engine, "parseInt(\"100.5\")"), "NaN"); } #[test] fn parse_int_inferred_hex() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(&forward(&mut engine, "parseInt(\"0xA\")"), "10"); } @@ -525,16 +508,14 @@ fn parse_int_inferred_hex() { /// a radix 10 if no radix is specified. Some alternative implementations default to a radix of 8. #[test] fn parse_int_zero_start() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(&forward(&mut engine, "parseInt(\"018\")"), "18"); } #[test] fn parse_int_varying_radix() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let base_str = "1000"; @@ -553,8 +534,7 @@ fn parse_int_varying_radix() { #[test] fn parse_int_negative_varying_radix() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let base_str = "-1000"; @@ -573,16 +553,14 @@ fn parse_int_negative_varying_radix() { #[test] fn parse_int_malformed_str() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(&forward(&mut engine, "parseInt(\"hello\")"), "NaN"); } #[test] fn parse_int_undefined() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(&forward(&mut engine, "parseInt(undefined)"), "NaN"); } @@ -591,8 +569,7 @@ fn parse_int_undefined() { /// passed as the first argument. #[test] fn parse_int_no_args() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(&forward(&mut engine, "parseInt()"), "NaN"); } @@ -600,64 +577,56 @@ fn parse_int_no_args() { /// Shows that extra arguments to parseInt are ignored. #[test] fn parse_int_too_many_args() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(&forward(&mut engine, "parseInt(\"100\", 10, 10)"), "100"); } #[test] fn parse_float_simple() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(&forward(&mut engine, "parseFloat(\"6.5\")"), "6.5"); } #[test] fn parse_float_int() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(&forward(&mut engine, "parseFloat(10)"), "10"); } #[test] fn parse_float_int_str() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(&forward(&mut engine, "parseFloat(\"8\")"), "8"); } #[test] fn parse_float_already_float() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(&forward(&mut engine, "parseFloat(17.5)"), "17.5"); } #[test] fn parse_float_negative() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(&forward(&mut engine, "parseFloat(\"-99.7\")"), "-99.7"); } #[test] fn parse_float_malformed_str() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(&forward(&mut engine, "parseFloat(\"hello\")"), "NaN"); } #[test] fn parse_float_undefined() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(&forward(&mut engine, "parseFloat(undefined)"), "NaN"); } @@ -665,8 +634,7 @@ fn parse_float_undefined() { /// No arguments to parseFloat is treated the same as passing undefined as the first argument. #[test] fn parse_float_no_args() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(&forward(&mut engine, "parseFloat()"), "NaN"); } @@ -674,16 +642,14 @@ fn parse_float_no_args() { /// Shows that the parseFloat function ignores extra arguments. #[test] fn parse_float_too_many_args() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(&forward(&mut engine, "parseFloat(\"100.5\", 10)"), "100.5"); } #[test] fn global_is_finite() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!("false", &forward(&mut engine, "isFinite(Infinity)")); assert_eq!("false", &forward(&mut engine, "isFinite(NaN)")); @@ -698,8 +664,7 @@ fn global_is_finite() { #[test] fn global_is_nan() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!("true", &forward(&mut engine, "isNaN(NaN)")); assert_eq!("true", &forward(&mut engine, "isNaN('NaN')")); @@ -720,8 +685,7 @@ fn global_is_nan() { #[test] fn number_is_finite() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!("false", &forward(&mut engine, "Number.isFinite(Infinity)")); assert_eq!("false", &forward(&mut engine, "Number.isFinite(NaN)")); @@ -747,8 +711,7 @@ fn number_is_finite() { #[test] fn number_is_integer() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!("true", &forward(&mut engine, "Number.isInteger(0)")); assert_eq!("true", &forward(&mut engine, "Number.isInteger(1)")); @@ -795,8 +758,7 @@ fn number_is_integer() { #[test] fn number_is_nan() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!("true", &forward(&mut engine, "Number.isNaN(NaN)")); assert_eq!("true", &forward(&mut engine, "Number.isNaN(Number.NaN)")); @@ -829,8 +791,7 @@ fn number_is_nan() { #[test] fn number_is_safe_integer() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!("true", &forward(&mut engine, "Number.isSafeInteger(3)")); assert_eq!( diff --git a/boa/src/builtins/object/gcobject.rs b/boa/src/builtins/object/gcobject.rs index b13e4c90902..b471d2e3893 100644 --- a/boa/src/builtins/object/gcobject.rs +++ b/boa/src/builtins/object/gcobject.rs @@ -12,7 +12,7 @@ use crate::{ function_environment_record::BindingStatus, lexical_environment::new_function_environment, }, syntax::ast::node::statement_list::RcStatementList, - Executable, Interpreter, Result, + Context, Executable, Result, }; use gc::{Finalize, Gc, GcCell, GcCellRef, GcCellRefMut, Trace}; use std::{ @@ -108,7 +108,7 @@ impl GcObject { // // #[track_caller] - pub fn call(&self, this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub fn call(&self, this: &Value, args: &[Value], ctx: &mut Context) -> Result { let this_function_object = self.clone(); let f_body = if let Some(function) = self.borrow().as_function() { if function.is_callable() { @@ -161,7 +161,7 @@ impl GcObject { .borrow_mut() .initialize_binding("arguments", arguments_obj); - ctx.realm.environment.push(local_env); + ctx.realm_mut().environment.push(local_env); FunctionBody::Ordinary(body.clone()) } @@ -177,7 +177,7 @@ impl GcObject { FunctionBody::BuiltIn(func) => func(this, args, ctx), FunctionBody::Ordinary(body) => { let result = body.run(ctx); - ctx.realm.environment.pop(); + ctx.realm_mut().environment.pop(); result } @@ -190,7 +190,7 @@ impl GcObject { /// Panics if the object is currently mutably borrowed. // #[track_caller] - pub fn construct(&self, args: &[Value], ctx: &mut Interpreter) -> Result { + pub fn construct(&self, args: &[Value], ctx: &mut Context) -> Result { let this = Object::create(self.borrow().get(&PROTOTYPE.into())).into(); let this_function_object = self.clone(); @@ -243,13 +243,13 @@ impl GcObject { .borrow_mut() .initialize_binding("arguments", arguments_obj); - ctx.realm.environment.push(local_env); + ctx.realm_mut().environment.push(local_env); // Call body should be set before reaching here let _ = body.run(ctx); // local_env gets dropped here, its no longer needed - let binding = ctx.realm.environment.get_this_binding(); + let binding = ctx.realm_mut().environment.get_this_binding(); Ok(binding) } } diff --git a/boa/src/builtins/object/mod.rs b/boa/src/builtins/object/mod.rs index 33dd2815ccf..3e7632a8dd5 100644 --- a/boa/src/builtins/object/mod.rs +++ b/boa/src/builtins/object/mod.rs @@ -21,7 +21,7 @@ use crate::{ value::{RcBigInt, RcString, RcSymbol, Value}, BigInt, Date, RegExp, }, - exec::Interpreter, + context::Context, BoaProfiler, Result, }; use gc::{Finalize, Trace}; @@ -79,7 +79,7 @@ pub trait Class: NativeObject + Sized { const ATTRIBUTE: Attribute = Attribute::all(); /// The constructor of the class. - fn constructor(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result; + fn constructor(this: &Value, args: &[Value], ctx: &mut Context) -> Result; /// Initializes the internals and the methods of the class. fn init(class: &mut ClassBuilder<'_>) -> Result<()>; @@ -89,13 +89,13 @@ pub trait Class: NativeObject + Sized { /// /// This is automatically implemented, when a type implements `Class`. pub trait ClassConstructor: Class { - fn raw_constructor(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result + fn raw_constructor(this: &Value, args: &[Value], ctx: &mut Context) -> Result where Self: Sized; } impl ClassConstructor for T { - fn raw_constructor(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result + fn raw_constructor(this: &Value, args: &[Value], ctx: &mut Context) -> Result where Self: Sized, { @@ -108,17 +108,17 @@ impl ClassConstructor for T { /// Class builder which allows adding methods and static methods to the class. #[derive(Debug)] pub struct ClassBuilder<'context> { - context: &'context mut Interpreter, + context: &'context mut Context, object: GcObject, prototype: GcObject, } impl<'context> ClassBuilder<'context> { - pub(crate) fn new(context: &'context mut Interpreter) -> Self + pub(crate) fn new(context: &'context mut Context) -> Self where T: ClassConstructor, { - let global = context.global(); + let global = context.global_object(); let prototype = { let object_prototype = global.get_field("Object").get_field(PROTOTYPE); @@ -181,7 +181,7 @@ impl<'context> ClassBuilder<'context> { let mut function = Object::function( Function::BuiltIn(function.into(), FunctionFlags::CALLABLE), self.context - .global() + .global_object() .get_field("Function") .get_field("prototype"), ); @@ -205,7 +205,7 @@ impl<'context> ClassBuilder<'context> { let mut function = Object::function( Function::BuiltIn(function.into(), FunctionFlags::CALLABLE), self.context - .global() + .global_object() .get_field("Function") .get_field("prototype"), ); @@ -252,7 +252,7 @@ impl<'context> ClassBuilder<'context> { .insert_property(key.into(), property); } - pub fn context(&mut self) -> &'_ mut Interpreter { + pub fn context(&mut self) -> &'_ mut Context { self.context } } @@ -663,14 +663,15 @@ impl Object { } /// Create a new object. -pub fn make_object(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { +pub fn make_object(_: &Value, args: &[Value], ctx: &mut Context) -> Result { if let Some(arg) = args.get(0) { if !arg.is_null_or_undefined() { return arg.to_object(ctx); } } + let global = ctx.global_object(); - Ok(Value::new_object(Some(ctx.global()))) + Ok(Value::new_object(Some(global))) } /// `Object.create( proto, [propertiesObject] )` @@ -683,7 +684,7 @@ pub fn make_object(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result Result { +pub fn create(_: &Value, args: &[Value], interpreter: &mut Context) -> Result { let prototype = args.get(0).cloned().unwrap_or_else(Value::undefined); let properties = args.get(1).cloned().unwrap_or_else(Value::undefined); @@ -704,7 +705,7 @@ pub fn create(_: &Value, args: &[Value], interpreter: &mut Interpreter) -> Resul } /// Uses the SameValue algorithm to check equality of objects -pub fn is(_: &Value, args: &[Value], _: &mut Interpreter) -> Result { +pub fn is(_: &Value, args: &[Value], _: &mut Context) -> Result { let x = args.get(0).cloned().unwrap_or_else(Value::undefined); let y = args.get(1).cloned().unwrap_or_else(Value::undefined); @@ -712,7 +713,7 @@ pub fn is(_: &Value, args: &[Value], _: &mut Interpreter) -> Result { } /// Get the `prototype` of an object. -pub fn get_prototype_of(_: &Value, args: &[Value], _: &mut Interpreter) -> Result { +pub fn get_prototype_of(_: &Value, args: &[Value], _: &mut Context) -> Result { let obj = args.get(0).expect("Cannot get object"); Ok(obj .as_object() @@ -720,7 +721,7 @@ pub fn get_prototype_of(_: &Value, args: &[Value], _: &mut Interpreter) -> Resul } /// Set the `prototype` of an object. -pub fn set_prototype_of(_: &Value, args: &[Value], _: &mut Interpreter) -> Result { +pub fn set_prototype_of(_: &Value, args: &[Value], _: &mut Context) -> Result { let obj = args.get(0).expect("Cannot get object").clone(); let proto = args.get(1).expect("Cannot get object").clone(); obj.as_object_mut().unwrap().prototype = proto; @@ -728,7 +729,7 @@ pub fn set_prototype_of(_: &Value, args: &[Value], _: &mut Interpreter) -> Resul } /// Define a property in an object -pub fn define_property(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { +pub fn define_property(_: &Value, args: &[Value], ctx: &mut Context) -> Result { let obj = args.get(0).expect("Cannot get object"); let prop = args.get(1).expect("Cannot get object").to_string(ctx)?; let desc = Property::from(args.get(2).expect("Cannot get object")); @@ -746,7 +747,7 @@ pub fn define_property(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Resu /// /// [spec]: https://tc39.es/ecma262/#sec-object.prototype.tostring /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString -pub fn to_string(this: &Value, _: &[Value], _: &mut Interpreter) -> Result { +pub fn to_string(this: &Value, _: &[Value], _: &mut Context) -> Result { // FIXME: it should not display the object. Ok(this.display().to_string().into()) } @@ -762,7 +763,7 @@ pub fn to_string(this: &Value, _: &[Value], _: &mut Interpreter) -> Result Result { +pub fn has_own_property(this: &Value, args: &[Value], ctx: &mut Context) -> Result { let prop = if args.is_empty() { None } else { @@ -780,11 +781,7 @@ pub fn has_own_property(this: &Value, args: &[Value], ctx: &mut Interpreter) -> } } -pub fn property_is_enumerable( - this: &Value, - args: &[Value], - ctx: &mut Interpreter, -) -> Result { +pub fn property_is_enumerable(this: &Value, args: &[Value], ctx: &mut Context) -> Result { let key = match args.get(0) { None => return Ok(Value::from(false)), Some(key) => key, @@ -804,8 +801,8 @@ pub fn property_is_enumerable( /// Initialise the `Object` object on the global object. #[inline] -pub fn init(interpreter: &mut Interpreter) -> (&'static str, Value) { - let global = interpreter.global(); +pub fn init(interpreter: &mut Context) -> (&'static str, Value) { + let global = interpreter.global_object(); let _timer = BoaProfiler::global().start_event("object", "init"); let prototype = Value::new_object(None); diff --git a/boa/src/builtins/object/tests.rs b/boa/src/builtins/object/tests.rs index ae405a67e7b..0c54fc76e77 100644 --- a/boa/src/builtins/object/tests.rs +++ b/boa/src/builtins/object/tests.rs @@ -1,9 +1,8 @@ -use crate::{exec::Interpreter, forward, realm::Realm}; +use crate::{context::Context, forward}; #[test] fn object_create_with_regular_object() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" const foo = { a: 5 }; @@ -18,8 +17,7 @@ fn object_create_with_regular_object() { #[test] fn object_create_with_undefined() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" try { @@ -38,8 +36,7 @@ fn object_create_with_undefined() { #[test] fn object_create_with_number() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" try { @@ -60,8 +57,7 @@ fn object_create_with_number() { #[ignore] // to test on __proto__ somehow. __proto__ getter is not working as expected currently fn object_create_with_function() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" const x = function (){}; @@ -75,8 +71,7 @@ fn object_create_with_function() { #[test] fn object_is() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var foo = { a: 1}; @@ -96,13 +91,12 @@ fn object_is() { assert_eq!(forward(&mut engine, "Object.is(NaN, 0/0)"), "true"); assert_eq!(forward(&mut engine, "Object.is()"), "true"); assert_eq!(forward(&mut engine, "Object.is(undefined)"), "true"); - assert!(engine.realm.global_obj.is_global()); - assert!(!engine.realm.global_obj.get_field("Object").is_global()); + assert!(engine.global_object().is_global()); + assert!(!engine.global_object().get_field("Object").is_global()); } #[test] fn object_has_own_property() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" let x = { someProp: 1, undefinedProp: undefined, nullProp: null }; "#; @@ -122,8 +116,7 @@ fn object_has_own_property() { #[test] fn object_property_is_enumerable() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" let x = { enumerableProp: 'yes' }; "#; diff --git a/boa/src/builtins/regexp/mod.rs b/boa/src/builtins/regexp/mod.rs index 214b2807ca3..a3fb6bd254d 100644 --- a/boa/src/builtins/regexp/mod.rs +++ b/boa/src/builtins/regexp/mod.rs @@ -18,7 +18,7 @@ use crate::{ property::Property, value::{RcString, Value}, }, - exec::Interpreter, + context::Context, BoaProfiler, Result, }; use gc::{unsafe_empty_trace, Finalize, Trace}; @@ -72,7 +72,7 @@ impl RegExp { pub(crate) const LENGTH: usize = 2; /// Create a new `RegExp` - pub(crate) fn make_regexp(this: &Value, args: &[Value], _: &mut Interpreter) -> Result { + pub(crate) fn make_regexp(this: &Value, args: &[Value], _: &mut Context) -> Result { let arg = args.get(0).ok_or_else(Value::undefined)?; let mut regex_body = String::new(); let mut regex_flags = String::new(); @@ -169,7 +169,7 @@ impl RegExp { // /// // /// [spec]: https://tc39.es/ecma262/#sec-get-regexp.prototype.dotAll // /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/dotAll - // fn get_dot_all(this: &Value, _: &[Value], _: &mut Interpreter) -> Result { + // fn get_dot_all(this: &Value, _: &[Value], _: &mut Context) -> Result { // this.with_internal_state_ref(|regex: &RegExp| Ok(Value::from(regex.dot_all))) // } @@ -184,7 +184,7 @@ impl RegExp { // /// [spec]: https://tc39.es/ecma262/#sec-get-regexp.prototype.flags // /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/flags // /// [flags]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Advanced_searching_with_flags_2 - // fn get_flags(this: &Value, _: &[Value], _: &mut Interpreter) -> Result { + // fn get_flags(this: &Value, _: &[Value], _: &mut Context) -> Result { // this.with_internal_state_ref(|regex: &RegExp| Ok(Value::from(regex.flags.clone()))) // } @@ -198,7 +198,7 @@ impl RegExp { // /// // /// [spec]: https://tc39.es/ecma262/#sec-get-regexp.prototype.global // /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/global - // fn get_global(this: &Value, _: &[Value], _: &mut Interpreter) -> Result { + // fn get_global(this: &Value, _: &[Value], _: &mut Context) -> Result { // this.with_internal_state_ref(|regex: &RegExp| Ok(Value::from(regex.global))) // } @@ -212,7 +212,7 @@ impl RegExp { // /// // /// [spec]: https://tc39.es/ecma262/#sec-get-regexp.prototype.ignorecase // /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/ignoreCase - // fn get_ignore_case(this: &Value, _: &[Value], _: &mut Interpreter) -> Result { + // fn get_ignore_case(this: &Value, _: &[Value], _: &mut Context) -> Result { // this.with_internal_state_ref(|regex: &RegExp| Ok(Value::from(regex.ignore_case))) // } @@ -226,7 +226,7 @@ impl RegExp { // /// // /// [spec]: https://tc39.es/ecma262/#sec-get-regexp.prototype.multiline // /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/multiline - // fn get_multiline(this: &Value, _: &[Value], _: &mut Interpreter) -> Result { + // fn get_multiline(this: &Value, _: &[Value], _: &mut Context) -> Result { // this.with_internal_state_ref(|regex: &RegExp| Ok(Value::from(regex.multiline))) // } @@ -241,7 +241,7 @@ impl RegExp { // /// // /// [spec]: https://tc39.es/ecma262/#sec-get-regexp.prototype.source // /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/source - // fn get_source(this: &Value, _: &[Value], _: &mut Interpreter) -> Result { + // fn get_source(this: &Value, _: &[Value], _: &mut Context) -> Result { // Ok(this.get_internal_slot("OriginalSource")) // } @@ -255,7 +255,7 @@ impl RegExp { // /// // /// [spec]: https://tc39.es/ecma262/#sec-get-regexp.prototype.sticky // /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/sticky - // fn get_sticky(this: &Value, _: &[Value], _: &mut Interpreter) -> Result { + // fn get_sticky(this: &Value, _: &[Value], _: &mut Context) -> Result { // this.with_internal_state_ref(|regex: &RegExp| Ok(Value::from(regex.sticky))) // } @@ -270,7 +270,7 @@ impl RegExp { // /// // /// [spec]: https://tc39.es/ecma262/#sec-get-regexp.prototype.unicode // /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicode - // fn get_unicode(this: &Value, _: &[Value], _: &mut Interpreter) -> Result { + // fn get_unicode(this: &Value, _: &[Value], _: &mut Context) -> Result { // this.with_internal_state_ref(|regex: &RegExp| Ok(Value::from(regex.unicode))) // } @@ -286,7 +286,7 @@ impl RegExp { /// /// [spec]: https://tc39.es/ecma262/#sec-regexp.prototype.test /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test - pub(crate) fn test(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn test(this: &Value, args: &[Value], ctx: &mut Context) -> Result { let arg_str = args .get(0) .expect("could not get argument") @@ -325,7 +325,7 @@ impl RegExp { /// /// [spec]: https://tc39.es/ecma262/#sec-regexp.prototype.exec /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec - pub(crate) fn exec(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn exec(this: &Value, args: &[Value], ctx: &mut Context) -> Result { let arg_str = args .get(0) .expect("could not get argument") @@ -381,7 +381,7 @@ impl RegExp { /// /// [spec]: https://tc39.es/ecma262/#sec-regexp.prototype-@@match /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/@@match - pub(crate) fn r#match(this: &Value, arg: RcString, ctx: &mut Interpreter) -> Result { + pub(crate) fn r#match(this: &Value, arg: RcString, ctx: &mut Context) -> Result { let (matcher, flags) = if let Some(object) = this.as_object() { let regex = object.as_regexp().unwrap(); (regex.matcher.clone(), regex.flags.clone()) @@ -413,7 +413,7 @@ impl RegExp { /// [spec]: https://tc39.es/ecma262/#sec-regexp.prototype.tostring /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/toString #[allow(clippy::wrong_self_convention)] - pub(crate) fn to_string(this: &Value, _: &[Value], _: &mut Interpreter) -> Result { + pub(crate) fn to_string(this: &Value, _: &[Value], _: &mut Context) -> Result { let (body, flags) = if let Some(object) = this.as_object() { let regex = object.as_regexp().unwrap(); (regex.original_source.clone(), regex.flags.clone()) @@ -480,9 +480,9 @@ impl RegExp { /// Initialise the `RegExp` object on the global object. #[inline] - pub(crate) fn init(interpreter: &mut Interpreter) -> (&'static str, Value) { + pub(crate) fn init(interpreter: &mut Context) -> (&'static str, Value) { let _timer = BoaProfiler::global().start_event(Self::NAME, "init"); - let global = interpreter.global(); + let global = interpreter.global_object(); // Create prototype let prototype = Value::new_object(Some(global)); diff --git a/boa/src/builtins/regexp/tests.rs b/boa/src/builtins/regexp/tests.rs index b407a30fa5d..282f318a46c 100644 --- a/boa/src/builtins/regexp/tests.rs +++ b/boa/src/builtins/regexp/tests.rs @@ -1,9 +1,8 @@ -use crate::{exec::Interpreter, forward, realm::Realm}; +use crate::{context::Context, forward}; #[test] fn constructors() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var constructed = new RegExp("[0-9]+(\\.[0-9]+)?"); var literal = /[0-9]+(\.[0-9]+)?/; @@ -20,7 +19,7 @@ fn constructors() { // #[test] // fn flags() { -// let mut engine = Interpreter::new(); +// let mut engine = Context::new(); // let init = r#" // var re_gi = /test/gi; // var re_sm = /test/sm; @@ -46,8 +45,7 @@ fn constructors() { #[test] fn last_index() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var regex = /[0-9]+(\.[0-9]+)?/g; "#; @@ -62,8 +60,7 @@ fn last_index() { #[test] fn exec() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var re = /quick\s(brown).+?(jumps)/ig; var result = re.exec('The Quick Brown Fox Jumps Over The Lazy Dog'); @@ -85,8 +82,7 @@ fn exec() { #[test] fn to_string() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!( forward(&mut engine, "(new RegExp('a+b+c')).toString()"), diff --git a/boa/src/builtins/string/mod.rs b/boa/src/builtins/string/mod.rs index e5a613702d0..d84aa71ab34 100644 --- a/boa/src/builtins/string/mod.rs +++ b/boa/src/builtins/string/mod.rs @@ -20,7 +20,7 @@ use crate::{ value::{RcString, Value}, RegExp, }, - exec::Interpreter, + context::Context, BoaProfiler, Result, }; use regex::Regex; @@ -48,7 +48,7 @@ impl String { /// which can differ in JavaScript engines. In Boa it is `2^32 - 1` pub(crate) const MAX_STRING_LENGTH: f64 = u32::MAX as f64; - fn this_string_value(this: &Value, ctx: &mut Interpreter) -> Result { + fn this_string_value(this: &Value, ctx: &mut Context) -> Result { match this { Value::String(ref string) => return Ok(string.clone()), Value::Object(ref object) => { @@ -67,11 +67,7 @@ impl String { /// /// [[Call]] - Returns a new native `string` /// - pub(crate) fn make_string( - this: &Value, - args: &[Value], - ctx: &mut Interpreter, - ) -> Result { + pub(crate) fn make_string(this: &Value, args: &[Value], ctx: &mut Context) -> Result { // This value is used by console.log and other routines to match Obexpecty"failed to parse argument for String method"pe // to its Javascript Identifier (global constructor method name) let string = match args.get(0) { @@ -91,7 +87,7 @@ impl String { /// Get the string value to a primitive string #[allow(clippy::wrong_self_convention)] #[inline] - pub(crate) fn to_string(this: &Value, _: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn to_string(this: &Value, _: &[Value], ctx: &mut Context) -> Result { // Get String from String Object and send it back as a new value Ok(Value::from(Self::this_string_value(this, ctx)?)) } @@ -112,7 +108,7 @@ impl String { /// /// [spec]: https://tc39.es/ecma262/#sec-string.prototype.charat /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt - pub(crate) fn char_at(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn char_at(this: &Value, args: &[Value], ctx: &mut Context) -> Result { // First we get it the actual string a private field stored on the object only the engine has access to. // Then we convert it into a Rust String by wrapping it in from_value let primitive_val = this.to_string(ctx)?; @@ -154,11 +150,7 @@ impl String { /// /// [spec]: https://tc39.es/ecma262/#sec-string.prototype.charcodeat /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt - pub(crate) fn char_code_at( - this: &Value, - args: &[Value], - ctx: &mut Interpreter, - ) -> Result { + pub(crate) fn char_code_at(this: &Value, args: &[Value], ctx: &mut Context) -> Result { // First we get it the actual string a private field stored on the object only the engine has access to. // Then we convert it into a Rust String by wrapping it in from_value let primitive_val = this.to_string(ctx)?; @@ -198,7 +190,7 @@ impl String { /// /// [spec]: https://tc39.es/ecma262/#sec-string.prototype.concat /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/concat - pub(crate) fn concat(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn concat(this: &Value, args: &[Value], ctx: &mut Context) -> Result { let object = this.require_object_coercible(ctx)?; let mut string = object.to_string(ctx)?.to_string(); @@ -220,7 +212,7 @@ impl String { /// /// [spec]: https://tc39.es/ecma262/#sec-string.prototype.repeat /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat - pub(crate) fn repeat(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn repeat(this: &Value, args: &[Value], ctx: &mut Context) -> Result { let object = this.require_object_coercible(ctx)?; let string = object.to_string(ctx)?; @@ -254,7 +246,7 @@ impl String { /// /// [spec]: https://tc39.es/ecma262/#sec-string.prototype.slice /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice - pub(crate) fn slice(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn slice(this: &Value, args: &[Value], ctx: &mut Context) -> Result { // First we get it the actual string a private field stored on the object only the engine has access to. // Then we convert it into a Rust String by wrapping it in from_value let primitive_val = this.to_string(ctx)?; @@ -304,11 +296,7 @@ impl String { /// /// [spec]: https://tc39.es/ecma262/#sec-string.prototype.startswith /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith - pub(crate) fn starts_with( - this: &Value, - args: &[Value], - ctx: &mut Interpreter, - ) -> Result { + pub(crate) fn starts_with(this: &Value, args: &[Value], ctx: &mut Context) -> Result { // First we get it the actual string a private field stored on the object only the engine has access to. // Then we convert it into a Rust String by wrapping it in from_value let primitive_val = this.to_string(ctx)?; @@ -351,7 +339,7 @@ impl String { /// /// [spec]: https://tc39.es/ecma262/#sec-string.prototype.endswith /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith - pub(crate) fn ends_with(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn ends_with(this: &Value, args: &[Value], ctx: &mut Context) -> Result { // First we get it the actual string a private field stored on the object only the engine has access to. // Then we convert it into a Rust String by wrapping it in from_value let primitive_val = this.to_string(ctx)?; @@ -397,7 +385,7 @@ impl String { /// /// [spec]: https://tc39.es/ecma262/#sec-string.prototype.includes /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes - pub(crate) fn includes(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn includes(this: &Value, args: &[Value], ctx: &mut Context) -> Result { // First we get it the actual string a private field stored on the object only the engine has access to. // Then we convert it into a Rust String by wrapping it in from_value let primitive_val = this.to_string(ctx)?; @@ -459,7 +447,7 @@ impl String { /// /// [spec]: https://tc39.es/ecma262/#sec-string.prototype.replace /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace - pub(crate) fn replace(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn replace(this: &Value, args: &[Value], ctx: &mut Context) -> Result { // TODO: Support Symbol replacer let primitive_val = this.to_string(ctx)?; if args.is_empty() { @@ -617,7 +605,7 @@ impl String { /// /// [spec]: https://tc39.es/ecma262/#sec-string.prototype.indexof /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf - pub(crate) fn index_of(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn index_of(this: &Value, args: &[Value], ctx: &mut Context) -> Result { let this = this.require_object_coercible(ctx)?; let string = this.to_string(ctx)?; @@ -660,11 +648,7 @@ impl String { /// /// [spec]: https://tc39.es/ecma262/#sec-string.prototype.lastindexof /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf - pub(crate) fn last_index_of( - this: &Value, - args: &[Value], - ctx: &mut Interpreter, - ) -> Result { + pub(crate) fn last_index_of(this: &Value, args: &[Value], ctx: &mut Context) -> Result { let this = this.require_object_coercible(ctx)?; let string = this.to_string(ctx)?; @@ -705,7 +689,7 @@ impl String { /// [spec]: https://tc39.es/ecma262/#sec-string.prototype.match /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match /// [regex]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions - pub(crate) fn r#match(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn r#match(this: &Value, args: &[Value], ctx: &mut Context) -> Result { let re = RegExp::make_regexp(&Value::from(Object::default()), &[args[0].clone()], ctx)?; RegExp::r#match(&re, this.to_string(ctx)?, ctx) } @@ -756,7 +740,7 @@ impl String { /// /// [spec]: https://tc39.es/ecma262/#sec-string.prototype.padend /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padEnd - pub(crate) fn pad_end(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn pad_end(this: &Value, args: &[Value], ctx: &mut Context) -> Result { let primitive = this.to_string(ctx)?; if args.is_empty() { return Err(Value::from("padEnd requires maxLength argument")); @@ -783,7 +767,7 @@ impl String { /// /// [spec]: https://tc39.es/ecma262/#sec-string.prototype.padstart /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart - pub(crate) fn pad_start(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn pad_start(this: &Value, args: &[Value], ctx: &mut Context) -> Result { let primitive = this.to_string(ctx)?; if args.is_empty() { return Err(Value::from("padStart requires maxLength argument")); @@ -808,11 +792,11 @@ impl String { // And does not include: // '\u{FEFF}' (zero width non-breaking space) match c { - // Explicit whitespace: https://tc39.es/ecma262/#sec-white-space + // Explicit whitespace: https://tc39.es/ecma262/#sec-white-space '\u{0009}' | '\u{000B}' | '\u{000C}' | '\u{0020}' | '\u{00A0}' | '\u{FEFF}' | - // Unicode Space_Seperator category + // Unicode Space_Seperator category '\u{1680}' | '\u{2000}'..='\u{200A}' | '\u{202F}' | '\u{205F}' | '\u{3000}' | - // Line terminators: https://tc39.es/ecma262/#sec-line-terminators + // Line terminators: https://tc39.es/ecma262/#sec-line-terminators '\u{000A}' | '\u{000D}' | '\u{2028}' | '\u{2029}' => true, _ => false, } @@ -830,7 +814,7 @@ impl String { /// /// [spec]: https://tc39.es/ecma262/#sec-string.prototype.trim /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim - pub(crate) fn trim(this: &Value, _: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn trim(this: &Value, _: &[Value], ctx: &mut Context) -> Result { let this = this.require_object_coercible(ctx)?; let string = this.to_string(ctx)?; Ok(Value::from( @@ -850,8 +834,7 @@ impl String { /// /// [spec]: https://tc39.es/ecma262/#sec-string.prototype.trimstart /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimStart - pub(crate) fn trim_start(this: &Value, _: &[Value], ctx: &mut Interpreter) -> Result { - let this = this.require_object_coercible(ctx)?; + pub(crate) fn trim_start(this: &Value, _: &[Value], ctx: &mut Context) -> Result { let string = this.to_string(ctx)?; Ok(Value::from( string.trim_start_matches(Self::is_trimmable_whitespace), @@ -870,7 +853,7 @@ impl String { /// /// [spec]: https://tc39.es/ecma262/#sec-string.prototype.trimend /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimEnd - pub(crate) fn trim_end(this: &Value, _: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn trim_end(this: &Value, _: &[Value], ctx: &mut Context) -> Result { let this = this.require_object_coercible(ctx)?; let string = this.to_string(ctx)?; Ok(Value::from( @@ -889,7 +872,7 @@ impl String { /// [spec]: https://tc39.es/ecma262/#sec-string.prototype.tolowercase /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase #[allow(clippy::wrong_self_convention)] - pub(crate) fn to_lowercase(this: &Value, _: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn to_lowercase(this: &Value, _: &[Value], ctx: &mut Context) -> Result { // First we get it the actual string a private field stored on the object only the engine has access to. // Then we convert it into a Rust String by wrapping it in from_value let this_str = this.to_string(ctx)?; @@ -911,7 +894,7 @@ impl String { /// [spec]: https://tc39.es/ecma262/#sec-string.prototype.toUppercase /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase #[allow(clippy::wrong_self_convention)] - pub(crate) fn to_uppercase(this: &Value, _: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn to_uppercase(this: &Value, _: &[Value], ctx: &mut Context) -> Result { // First we get it the actual string a private field stored on the object only the engine has access to. // Then we convert it into a Rust String by wrapping it in from_value let this_str = this.to_string(ctx)?; @@ -930,7 +913,7 @@ impl String { /// /// [spec]: https://tc39.es/ecma262/#sec-string.prototype.substring /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring - pub(crate) fn substring(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn substring(this: &Value, args: &[Value], ctx: &mut Context) -> Result { // First we get it the actual string a private field stored on the object only the engine has access to. // Then we convert it into a Rust String by wrapping it in from_value let primitive_val = this.to_string(ctx)?; @@ -979,7 +962,7 @@ impl String { /// [spec]: https://tc39.es/ecma262/#sec-string.prototype.substr /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr /// - pub(crate) fn substr(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn substr(this: &Value, args: &[Value], ctx: &mut Context) -> Result { // First we get it the actual string a private field stored on the object only the engine has access to. // Then we convert it into a Rust String by wrapping it in from_value let primitive_val = this.to_string(ctx)?; @@ -1035,7 +1018,7 @@ impl String { /// /// [spec]: https://tc39.es/ecma262/#sec-string.prototype.value_of /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/valueOf - pub(crate) fn value_of(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn value_of(this: &Value, args: &[Value], ctx: &mut Context) -> Result { // Use the to_string method because it is specified to do the same thing in this case Self::to_string(this, args, ctx) } @@ -1053,7 +1036,7 @@ impl String { /// [regex]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions /// [cg]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Groups_and_Ranges // TODO: update this method to return iterator - pub(crate) fn match_all(this: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn match_all(this: &Value, args: &[Value], ctx: &mut Context) -> Result { let re: Value = match args.get(0) { Some(arg) => { if arg.is_null() { @@ -1084,12 +1067,12 @@ impl String { /// Initialise the `String` object on the global object. #[inline] - pub(crate) fn init(interpreter: &mut Interpreter) -> (&'static str, Value) { + pub(crate) fn init(interpreter: &mut Context) -> (&'static str, Value) { let _timer = BoaProfiler::global().start_event(Self::NAME, "init"); // Create `String` `prototype` - let global = interpreter.global(); + let global = interpreter.global_object(); let prototype = Value::new_object(Some(global)); let length = Property::default().value(Value::from(0)); diff --git a/boa/src/builtins/string/tests.rs b/boa/src/builtins/string/tests.rs index 3666f4a1cbc..f084d886d7d 100644 --- a/boa/src/builtins/string/tests.rs +++ b/boa/src/builtins/string/tests.rs @@ -1,12 +1,11 @@ -use crate::{exec::Interpreter, forward, forward_val, realm::Realm}; +use crate::{context::Context, forward, forward_val}; ///TODO: re-enable when getProperty() is finished; #[test] #[ignore] fn length() { //TEST262: https://github.com/tc39/test262/blob/master/test/built-ins/String/length.js - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" const a = new String(' '); const b = new String('\ud834\udf06'); @@ -30,8 +29,7 @@ fn length() { #[test] fn new_string_has_length() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" let a = new String("1234"); a @@ -43,8 +41,7 @@ fn new_string_has_length() { #[test] fn new_utf8_string_has_length() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" let a = new String("中文"); a @@ -56,8 +53,7 @@ fn new_utf8_string_has_length() { #[test] fn concat() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var hello = new String('Hello, '); var world = new String('world! '); @@ -74,8 +70,7 @@ fn concat() { #[test] fn generic_concat() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" Number.prototype.concat = String.prototype.concat; let number = new Number(100); @@ -90,8 +85,7 @@ fn generic_concat() { #[test] /// Test the correct type is returned from call and construct fn construct_and_call() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var hello = new String('Hello'); var world = String('world'); @@ -108,8 +102,7 @@ fn construct_and_call() { #[test] fn repeat() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var empty = new String(''); var en = new String('english'); @@ -130,8 +123,7 @@ fn repeat() { #[test] fn repeat_throws_when_count_is_negative() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!( forward( @@ -150,8 +142,7 @@ fn repeat_throws_when_count_is_negative() { #[test] fn repeat_throws_when_count_is_infinity() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!( forward( @@ -170,8 +161,7 @@ fn repeat_throws_when_count_is_infinity() { #[test] fn repeat_throws_when_count_overflows_max_length() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!( forward( @@ -190,8 +180,7 @@ fn repeat_throws_when_count_overflows_max_length() { #[test] fn repeat_generic() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = "Number.prototype.repeat = String.prototype.repeat;"; forward(&mut engine, init); @@ -205,8 +194,7 @@ fn repeat_generic() { #[test] fn replace() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var a = "abc"; a = a.replace("a", "2"); @@ -220,8 +208,7 @@ fn replace() { #[test] fn replace_no_match() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var a = "abc"; a = a.replace(/d/, "$&$&"); @@ -234,8 +221,7 @@ fn replace_no_match() { #[test] fn replace_with_capture_groups() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var re = /(\w+)\s(\w+)/; var a = "John Smith"; @@ -250,8 +236,7 @@ fn replace_with_capture_groups() { #[test] fn replace_with_tenth_capture_group() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var re = /(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)/; var a = "0123456789"; @@ -265,8 +250,7 @@ fn replace_with_tenth_capture_group() { #[test] fn replace_substitutions() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var re = / two /; var a = "one two three"; @@ -288,8 +272,7 @@ fn replace_substitutions() { #[test] fn replace_with_function() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var a = "ecmascript is cool"; var p1, p2, p3; @@ -315,8 +298,7 @@ fn replace_with_function() { #[test] fn starts_with() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var empty = new String(''); var en = new String('english'); @@ -340,8 +322,7 @@ fn starts_with() { #[test] fn ends_with() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var empty = new String(''); var en = new String('english'); @@ -365,8 +346,7 @@ fn ends_with() { #[test] fn match_all() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(forward(&mut engine, "'aa'.matchAll(null).length"), "0"); assert_eq!(forward(&mut engine, "'aa'.matchAll(/b/).length"), "0"); @@ -408,8 +388,7 @@ fn match_all() { #[test] fn test_match() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var str = new String('The Quick Brown Fox Jumps Over The Lazy Dog'); var result1 = str.match(/quick\s(brown).+?(jumps)/i); @@ -453,8 +432,7 @@ fn test_match() { #[test] fn trim() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(forward(&mut engine, "'Hello'.trim()"), "\"Hello\""); assert_eq!(forward(&mut engine, "' \nHello'.trim()"), "\"Hello\""); assert_eq!(forward(&mut engine, "'Hello \n\r'.trim()"), "\"Hello\""); @@ -463,8 +441,7 @@ fn trim() { #[test] fn trim_start() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(forward(&mut engine, "'Hello'.trimStart()"), "\"Hello\""); assert_eq!(forward(&mut engine, "' \nHello'.trimStart()"), "\"Hello\""); assert_eq!( @@ -476,8 +453,7 @@ fn trim_start() { #[test] fn trim_end() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(forward(&mut engine, "'Hello'.trimEnd()"), "\"Hello\""); assert_eq!(forward(&mut engine, "' \nHello'.trimEnd()"), "\" \nHello\""); assert_eq!(forward(&mut engine, "'Hello \n'.trimEnd()"), "\"Hello\""); @@ -486,8 +462,7 @@ fn trim_end() { #[test] fn index_of_with_no_arguments() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(forward(&mut engine, "''.indexOf()"), "-1"); assert_eq!(forward(&mut engine, "'undefined'.indexOf()"), "0"); assert_eq!(forward(&mut engine, "'a1undefined'.indexOf()"), "2"); @@ -498,8 +473,7 @@ fn index_of_with_no_arguments() { #[test] fn index_of_with_string_search_string_argument() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(forward(&mut engine, "''.indexOf('hello')"), "-1"); assert_eq!( forward(&mut engine, "'undefined'.indexOf('undefined')"), @@ -525,8 +499,7 @@ fn index_of_with_string_search_string_argument() { #[test] fn index_of_with_non_string_search_string_argument() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(forward(&mut engine, "''.indexOf(1)"), "-1"); assert_eq!(forward(&mut engine, "'1'.indexOf(1)"), "0"); assert_eq!(forward(&mut engine, "'true'.indexOf(true)"), "0"); @@ -537,8 +510,7 @@ fn index_of_with_non_string_search_string_argument() { #[test] fn index_of_with_from_index_argument() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(forward(&mut engine, "''.indexOf('x', 2)"), "-1"); assert_eq!(forward(&mut engine, "'x'.indexOf('x', 2)"), "-1"); assert_eq!(forward(&mut engine, "'abcx'.indexOf('x', 2)"), "3"); @@ -553,8 +525,7 @@ fn index_of_with_from_index_argument() { #[test] fn generic_index_of() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); forward_val( &mut engine, "Number.prototype.indexOf = String.prototype.indexOf", @@ -568,8 +539,7 @@ fn generic_index_of() { #[test] fn index_of_empty_search_string() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(forward(&mut engine, "''.indexOf('')"), "0"); assert_eq!(forward(&mut engine, "''.indexOf('', 10)"), "0"); @@ -580,8 +550,7 @@ fn index_of_empty_search_string() { #[test] fn last_index_of_with_no_arguments() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(forward(&mut engine, "''.lastIndexOf()"), "-1"); assert_eq!(forward(&mut engine, "'undefined'.lastIndexOf()"), "0"); assert_eq!(forward(&mut engine, "'a1undefined'.lastIndexOf()"), "2"); @@ -601,8 +570,7 @@ fn last_index_of_with_no_arguments() { #[test] fn last_index_of_with_string_search_string_argument() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(forward(&mut engine, "''.lastIndexOf('hello')"), "-1"); assert_eq!( forward(&mut engine, "'undefined'.lastIndexOf('undefined')"), @@ -637,8 +605,7 @@ fn last_index_of_with_string_search_string_argument() { #[test] fn last_index_of_with_non_string_search_string_argument() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(forward(&mut engine, "''.lastIndexOf(1)"), "-1"); assert_eq!(forward(&mut engine, "'1'.lastIndexOf(1)"), "0"); assert_eq!(forward(&mut engine, "'11'.lastIndexOf(1)"), "1"); @@ -653,8 +620,7 @@ fn last_index_of_with_non_string_search_string_argument() { #[test] fn last_index_of_with_from_index_argument() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(forward(&mut engine, "''.lastIndexOf('x', 2)"), "-1"); assert_eq!(forward(&mut engine, "'x'.lastIndexOf('x', 2)"), "-1"); assert_eq!(forward(&mut engine, "'abcxx'.lastIndexOf('x', 2)"), "4"); @@ -669,8 +635,7 @@ fn last_index_of_with_from_index_argument() { #[test] fn last_index_with_empty_search_string() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(forward(&mut engine, "''.lastIndexOf('')"), "0"); assert_eq!(forward(&mut engine, "'x'.lastIndexOf('', 2)"), "1"); assert_eq!(forward(&mut engine, "'abcxx'.lastIndexOf('', 4)"), "4"); @@ -681,8 +646,7 @@ fn last_index_with_empty_search_string() { #[test] fn generic_last_index_of() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); forward_val( &mut engine, "Number.prototype.lastIndexOf = String.prototype.lastIndexOf", @@ -696,8 +660,7 @@ fn generic_last_index_of() { #[test] fn last_index_non_integer_position_argument() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!( forward(&mut engine, "''.lastIndexOf('x', new Number(4))"), "-1" diff --git a/boa/src/builtins/symbol/mod.rs b/boa/src/builtins/symbol/mod.rs index 25ccf148f8c..295dbf273b1 100644 --- a/boa/src/builtins/symbol/mod.rs +++ b/boa/src/builtins/symbol/mod.rs @@ -24,7 +24,7 @@ use crate::{ property::{Attribute, Property}, value::{RcString, RcSymbol, Value}, }, - exec::Interpreter, + context::Context, BoaProfiler, Result, }; use gc::{Finalize, Trace}; @@ -58,7 +58,7 @@ impl Symbol { self.hash } - fn this_symbol_value(value: &Value, ctx: &mut Interpreter) -> Result { + fn this_symbol_value(value: &Value, ctx: &mut Context) -> Result { match value { Value::Symbol(ref symbol) => return Ok(symbol.clone()), Value::Object(ref object) => { @@ -84,7 +84,7 @@ impl Symbol { /// /// [spec]: https://tc39.es/ecma262/#sec-symbol-description /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/Symbol - pub(crate) fn call(_: &Value, args: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn call(_: &Value, args: &[Value], ctx: &mut Context) -> Result { let description = match args.get(0) { Some(ref value) if !value.is_undefined() => Some(value.to_string(ctx)?), _ => None, @@ -104,7 +104,7 @@ impl Symbol { /// [spec]: https://tc39.es/ecma262/#sec-symbol.prototype.tostring /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toString #[allow(clippy::wrong_self_convention)] - pub(crate) fn to_string(this: &Value, _: &[Value], ctx: &mut Interpreter) -> Result { + pub(crate) fn to_string(this: &Value, _: &[Value], ctx: &mut Context) -> Result { let symbol = Self::this_symbol_value(this, ctx)?; let description = symbol.description().unwrap_or(""); Ok(Value::from(format!("Symbol({})", description))) @@ -112,7 +112,7 @@ impl Symbol { /// Initialise the `Symbol` object on the global object. #[inline] - pub fn init(interpreter: &mut Interpreter) -> (&'static str, Value) { + pub fn init(interpreter: &mut Context) -> (&'static str, Value) { // Define the Well-Known Symbols // https://tc39.es/ecma262/#sec-well-known-symbols let symbol_async_iterator = @@ -131,7 +131,7 @@ impl Symbol { let symbol_to_string_tag = interpreter.construct_symbol(Some("Symbol.toStringTag".into())); let symbol_unscopables = interpreter.construct_symbol(Some("Symbol.unscopables".into())); - let global = interpreter.global(); + let global = interpreter.global_object(); let _timer = BoaProfiler::global().start_event(Self::NAME, "init"); // Create prototype object diff --git a/boa/src/builtins/symbol/tests.rs b/boa/src/builtins/symbol/tests.rs index 3e7f293ec90..207aacf05b4 100644 --- a/boa/src/builtins/symbol/tests.rs +++ b/boa/src/builtins/symbol/tests.rs @@ -1,9 +1,8 @@ -use crate::{exec::Interpreter, forward, forward_val, realm::Realm}; +use crate::{context::Context, forward, forward_val}; #[test] fn call_symbol_and_check_return_type() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var sym = Symbol(); "#; @@ -14,8 +13,7 @@ fn call_symbol_and_check_return_type() { #[test] fn print_symbol_expect_description() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var sym = Symbol("Hello"); "#; @@ -26,8 +24,7 @@ fn print_symbol_expect_description() { #[test] fn symbol_access() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var x = {}; var sym1 = Symbol("Hello"); diff --git a/boa/src/builtins/undefined/mod.rs b/boa/src/builtins/undefined/mod.rs index ae21964a3ae..f9c86e8efac 100644 --- a/boa/src/builtins/undefined/mod.rs +++ b/boa/src/builtins/undefined/mod.rs @@ -12,7 +12,7 @@ #[cfg(test)] mod tests; -use crate::{builtins::value::Value, BoaProfiler, Interpreter}; +use crate::{builtins::value::Value, BoaProfiler, Context}; /// JavaScript global `undefined` property. #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] @@ -24,7 +24,7 @@ impl Undefined { /// Initialize the `undefined` property on the global object. #[inline] - pub(crate) fn init(_interpreter: &mut Interpreter) -> (&'static str, Value) { + pub(crate) fn init(_interpreter: &mut Context) -> (&'static str, Value) { let _timer = BoaProfiler::global().start_event(Self::NAME, "init"); (Self::NAME, Value::undefined()) diff --git a/boa/src/builtins/value/equality.rs b/boa/src/builtins/value/equality.rs index 93ffc3bd7b8..7cb09bf5e7f 100644 --- a/boa/src/builtins/value/equality.rs +++ b/boa/src/builtins/value/equality.rs @@ -1,5 +1,5 @@ use super::*; -use crate::{builtins::Number, Interpreter}; +use crate::{builtins::Number, Context}; impl Value { /// Strict equality comparison. @@ -37,7 +37,7 @@ impl Value { /// This method is executed when doing abstract equality comparisons with the `==` operator. /// For more information, check #[allow(clippy::float_cmp)] - pub fn equals(&self, other: &Self, interpreter: &mut Interpreter) -> Result { + pub fn equals(&self, other: &Self, interpreter: &mut Context) -> Result { // 1. If Type(x) is the same as Type(y), then // a. Return the result of performing Strict Equality Comparison x === y. if self.get_type() == other.get_type() { diff --git a/boa/src/builtins/value/mod.rs b/boa/src/builtins/value/mod.rs index 145c15e619a..d466afbf67f 100644 --- a/boa/src/builtins/value/mod.rs +++ b/boa/src/builtins/value/mod.rs @@ -11,7 +11,7 @@ use crate::builtins::{ property::{Attribute, Property, PropertyKey}, BigInt, Number, }; -use crate::exec::Interpreter; +use crate::context::Context; use crate::{BoaProfiler, Result}; use gc::{Finalize, GcCellRef, GcCellRefMut, Trace}; use serde_json::{map::Map, Number as JSONNumber, Value as JSONValue}; @@ -172,7 +172,7 @@ impl Value { } /// Convert from a JSON value to a JS value - pub fn from_json(json: JSONValue, interpreter: &mut Interpreter) -> Self { + pub fn from_json(json: JSONValue, interpreter: &mut Context) -> Self { match json { JSONValue::Number(v) => { if let Some(Ok(integer_32)) = v.as_i64().map(i32::try_from) { @@ -185,8 +185,7 @@ impl Value { JSONValue::Bool(v) => Self::boolean(v), JSONValue::Array(vs) => { let global_array_prototype = interpreter - .realm - .global_obj + .global_object() .get_field("Array") .get_field(PROTOTYPE); let new_obj = @@ -208,7 +207,7 @@ impl Value { new_obj } JSONValue::Object(obj) => { - let new_obj = Value::new_object(Some(interpreter.global())); + let new_obj = Value::new_object(Some(interpreter.global_object())); for (key, json) in obj.into_iter() { let value = Self::from_json(json, interpreter); new_obj.set_property( @@ -226,7 +225,7 @@ impl Value { } /// Converts the `Value` to `JSON`. - pub fn to_json(&self, interpreter: &mut Interpreter) -> Result { + pub fn to_json(&self, interpreter: &mut Context) -> Result { let to_json = self.get_field("toJSON"); if to_json.is_function() { let json_value = interpreter.call(&to_json, self, &[])?; @@ -580,11 +579,7 @@ impl Value { /// The abstract operation ToPrimitive takes an input argument and an optional argument PreferredType. /// /// - pub fn to_primitive( - &self, - ctx: &mut Interpreter, - preferred_type: PreferredType, - ) -> Result { + pub fn to_primitive(&self, ctx: &mut Context, preferred_type: PreferredType) -> Result { // 1. Assert: input is an ECMAScript language value. (always a value not need to check) // 2. If Type(input) is Object, then if let Value::Object(_) = self { @@ -608,7 +603,7 @@ impl Value { /// Converts the value to a `BigInt`. /// /// This function is equivelent to `BigInt(value)` in JavaScript. - pub fn to_bigint(&self, ctx: &mut Interpreter) -> Result { + pub fn to_bigint(&self, ctx: &mut Context) -> Result { match self { Value::Null => Err(ctx.construct_type_error("cannot convert null to a BigInt")), Value::Undefined => { @@ -655,7 +650,7 @@ impl Value { /// Converts the value to a string. /// /// This function is equivalent to `String(value)` in JavaScript. - pub fn to_string(&self, ctx: &mut Interpreter) -> Result { + pub fn to_string(&self, ctx: &mut Context) -> Result { match self { Value::Null => Ok("null".into()), Value::Undefined => Ok("undefined".into()), @@ -677,14 +672,14 @@ impl Value { /// This function is equivalent to `Object(value)` in JavaScript /// /// See: - pub fn to_object(&self, ctx: &mut Interpreter) -> Result { + pub fn to_object(&self, ctx: &mut Context) -> Result { match self { Value::Undefined | Value::Null => { ctx.throw_type_error("cannot convert 'null' or 'undefined' to object") } Value::Boolean(boolean) => { let proto = ctx - .realm + .realm() .environment .get_binding_value("Boolean") .expect("Boolean was not initialized") @@ -697,7 +692,7 @@ impl Value { } Value::Integer(integer) => { let proto = ctx - .realm + .realm() .environment .get_binding_value("Number") .expect("Number was not initialized") @@ -709,7 +704,7 @@ impl Value { } Value::Rational(rational) => { let proto = ctx - .realm + .realm() .environment .get_binding_value("Number") .expect("Number was not initialized") @@ -722,7 +717,7 @@ impl Value { } Value::String(ref string) => { let proto = ctx - .realm + .realm() .environment .get_binding_value("String") .expect("String was not initialized") @@ -735,7 +730,7 @@ impl Value { } Value::Symbol(ref symbol) => { let proto = ctx - .realm + .realm() .environment .get_binding_value("Symbol") .expect("Symbol was not initialized") @@ -748,7 +743,7 @@ impl Value { } Value::BigInt(ref bigint) => { let proto = ctx - .realm + .realm() .environment .get_binding_value("BigInt") .expect("BigInt was not initialized") @@ -764,7 +759,7 @@ impl Value { /// Converts the value to a `PropertyKey`, that can be used as a key for properties. /// /// See - pub fn to_property_key(&self, ctx: &mut Interpreter) -> Result { + pub fn to_property_key(&self, ctx: &mut Context) -> Result { Ok(match self { // Fast path: Value::String(string) => string.clone().into(), @@ -781,7 +776,7 @@ impl Value { /// It returns value converted to a numeric value of type `Number` or `BigInt`. /// /// See: - pub fn to_numeric(&self, ctx: &mut Interpreter) -> Result { + pub fn to_numeric(&self, ctx: &mut Context) -> Result { let primitive = self.to_primitive(ctx, PreferredType::Number)?; if let Some(bigint) = primitive.as_bigint() { return Ok(bigint.clone().into()); @@ -794,7 +789,7 @@ impl Value { /// This function is equivalent to `value | 0` in JavaScript /// /// See: - pub fn to_u32(&self, ctx: &mut Interpreter) -> Result { + pub fn to_u32(&self, ctx: &mut Context) -> Result { // This is the fast path, if the value is Integer we can just return it. if let Value::Integer(number) = *self { return Ok(number as u32); @@ -807,7 +802,7 @@ impl Value { /// Converts a value to an integral 32 bit signed integer. /// /// See: - pub fn to_i32(&self, ctx: &mut Interpreter) -> Result { + pub fn to_i32(&self, ctx: &mut Context) -> Result { // This is the fast path, if the value is Integer we can just return it. if let Value::Integer(number) = *self { return Ok(number); @@ -820,7 +815,7 @@ impl Value { /// Converts a value to a non-negative integer if it is a valid integer index value. /// /// See: - pub fn to_index(&self, ctx: &mut Interpreter) -> Result { + pub fn to_index(&self, ctx: &mut Context) -> Result { if self.is_undefined() { return Ok(0); } @@ -841,7 +836,7 @@ impl Value { /// Converts argument to an integer suitable for use as the length of an array-like object. /// /// See: - pub fn to_length(&self, ctx: &mut Interpreter) -> Result { + pub fn to_length(&self, ctx: &mut Context) -> Result { // 1. Let len be ? ToInteger(argument). let len = self.to_integer(ctx)?; @@ -857,7 +852,7 @@ impl Value { /// Converts a value to an integral Number value. /// /// See: - pub fn to_integer(&self, ctx: &mut Interpreter) -> Result { + pub fn to_integer(&self, ctx: &mut Context) -> Result { // 1. Let number be ? ToNumber(argument). let number = self.to_number(ctx)?; @@ -881,7 +876,7 @@ impl Value { /// This function is equivalent to the unary `+` operator (`+value`) in JavaScript /// /// See: https://tc39.es/ecma262/#sec-tonumber - pub fn to_number(&self, ctx: &mut Interpreter) -> Result { + pub fn to_number(&self, ctx: &mut Context) -> Result { match *self { Value::Null => Ok(0.0), Value::Undefined => Ok(f64::NAN), @@ -909,7 +904,7 @@ impl Value { /// This function is equivalent to `Number(value)` in JavaScript /// /// See: - pub fn to_numeric_number(&self, ctx: &mut Interpreter) -> Result { + pub fn to_numeric_number(&self, ctx: &mut Context) -> Result { let primitive = self.to_primitive(ctx, PreferredType::Number)?; if let Some(ref bigint) = primitive.as_bigint() { return Ok(bigint.to_f64()); @@ -929,7 +924,7 @@ impl Value { /// [table]: https://tc39.es/ecma262/#table-14 /// [spec]: https://tc39.es/ecma262/#sec-requireobjectcoercible #[inline] - pub fn require_object_coercible<'a>(&'a self, ctx: &mut Interpreter) -> Result<&'a Value> { + pub fn require_object_coercible<'a>(&'a self, ctx: &mut Context) -> Result<&'a Value> { if self.is_null_or_undefined() { Err(ctx.construct_type_error("cannot convert null or undefined to Object")) } else { diff --git a/boa/src/builtins/value/operations.rs b/boa/src/builtins/value/operations.rs index 8afea841ca9..7b6e1596d58 100644 --- a/boa/src/builtins/value/operations.rs +++ b/boa/src/builtins/value/operations.rs @@ -6,7 +6,7 @@ use crate::builtins::{ impl Value { #[inline] - pub fn add(&self, other: &Self, ctx: &mut Interpreter) -> Result { + pub fn add(&self, other: &Self, ctx: &mut Context) -> Result { Ok(match (self, other) { // Fast path: (Self::Integer(x), Self::Integer(y)) => Self::rational(f64::from(*x) + f64::from(*y)), @@ -43,7 +43,7 @@ impl Value { } #[inline] - pub fn sub(&self, other: &Self, ctx: &mut Interpreter) -> Result { + pub fn sub(&self, other: &Self, ctx: &mut Context) -> Result { Ok(match (self, other) { // Fast path: (Self::Integer(x), Self::Integer(y)) => Self::rational(f64::from(*x) - f64::from(*y)), @@ -71,7 +71,7 @@ impl Value { } #[inline] - pub fn mul(&self, other: &Self, ctx: &mut Interpreter) -> Result { + pub fn mul(&self, other: &Self, ctx: &mut Context) -> Result { Ok(match (self, other) { // Fast path: (Self::Integer(x), Self::Integer(y)) => Self::rational(f64::from(*x) * f64::from(*y)), @@ -99,7 +99,7 @@ impl Value { } #[inline] - pub fn div(&self, other: &Self, ctx: &mut Interpreter) -> Result { + pub fn div(&self, other: &Self, ctx: &mut Context) -> Result { Ok(match (self, other) { // Fast path: (Self::Integer(x), Self::Integer(y)) => Self::rational(f64::from(*x) / f64::from(*y)), @@ -127,7 +127,7 @@ impl Value { } #[inline] - pub fn rem(&self, other: &Self, ctx: &mut Interpreter) -> Result { + pub fn rem(&self, other: &Self, ctx: &mut Context) -> Result { Ok(match (self, other) { // Fast path: (Self::Integer(x), Self::Integer(y)) => Self::integer(x % *y), @@ -155,7 +155,7 @@ impl Value { } #[inline] - pub fn pow(&self, other: &Self, ctx: &mut Interpreter) -> Result { + pub fn pow(&self, other: &Self, ctx: &mut Context) -> Result { Ok(match (self, other) { // Fast path: (Self::Integer(x), Self::Integer(y)) => Self::rational(f64::from(*x).powi(*y)), @@ -181,7 +181,7 @@ impl Value { } #[inline] - pub fn bitand(&self, other: &Self, ctx: &mut Interpreter) -> Result { + pub fn bitand(&self, other: &Self, ctx: &mut Context) -> Result { Ok(match (self, other) { // Fast path: (Self::Integer(x), Self::Integer(y)) => Self::integer(x & y), @@ -213,7 +213,7 @@ impl Value { } #[inline] - pub fn bitor(&self, other: &Self, ctx: &mut Interpreter) -> Result { + pub fn bitor(&self, other: &Self, ctx: &mut Context) -> Result { Ok(match (self, other) { // Fast path: (Self::Integer(x), Self::Integer(y)) => Self::integer(x | y), @@ -245,7 +245,7 @@ impl Value { } #[inline] - pub fn bitxor(&self, other: &Self, ctx: &mut Interpreter) -> Result { + pub fn bitxor(&self, other: &Self, ctx: &mut Context) -> Result { Ok(match (self, other) { // Fast path: (Self::Integer(x), Self::Integer(y)) => Self::integer(x ^ y), @@ -277,7 +277,7 @@ impl Value { } #[inline] - pub fn shl(&self, other: &Self, ctx: &mut Interpreter) -> Result { + pub fn shl(&self, other: &Self, ctx: &mut Context) -> Result { Ok(match (self, other) { // Fast path: (Self::Integer(x), Self::Integer(y)) => Self::integer(x.wrapping_shl(*y as u32)), @@ -313,7 +313,7 @@ impl Value { } #[inline] - pub fn shr(&self, other: &Self, ctx: &mut Interpreter) -> Result { + pub fn shr(&self, other: &Self, ctx: &mut Context) -> Result { Ok(match (self, other) { // Fast path: (Self::Integer(x), Self::Integer(y)) => Self::integer(x.wrapping_shr(*y as u32)), @@ -349,7 +349,7 @@ impl Value { } #[inline] - pub fn ushr(&self, other: &Self, ctx: &mut Interpreter) -> Result { + pub fn ushr(&self, other: &Self, ctx: &mut Context) -> Result { Ok(match (self, other) { // Fast path: (Self::Integer(x), Self::Integer(y)) => { @@ -384,7 +384,7 @@ impl Value { } #[inline] - pub fn neg(&self, interpreter: &mut Interpreter) -> Result { + pub fn neg(&self, interpreter: &mut Context) -> Result { Ok(match *self { Self::Symbol(_) | Self::Undefined => Self::rational(NAN), Self::Object(_) => Self::rational(match self.to_numeric_number(interpreter) { @@ -404,7 +404,7 @@ impl Value { } #[inline] - pub fn not(&self, _: &mut Interpreter) -> Result { + pub fn not(&self, _: &mut Context) -> Result { Ok(!self.to_boolean()) } @@ -429,7 +429,7 @@ impl Value { &self, other: &Self, left_first: bool, - ctx: &mut Interpreter, + ctx: &mut Context, ) -> Result { Ok(match (self, other) { // Fast path (for some common operations): @@ -528,7 +528,7 @@ impl Value { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Less_than /// [spec]: https://tc39.es/ecma262/#sec-relational-operators-runtime-semantics-evaluation #[inline] - pub fn lt(&self, other: &Self, ctx: &mut Interpreter) -> Result { + pub fn lt(&self, other: &Self, ctx: &mut Context) -> Result { match self.abstract_relation(other, true, ctx)? { AbstractRelation::True => Ok(true), AbstractRelation::False | AbstractRelation::Undefined => Ok(false), @@ -545,7 +545,7 @@ impl Value { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Less_than_or_equal /// [spec]: https://tc39.es/ecma262/#sec-relational-operators-runtime-semantics-evaluation #[inline] - pub fn le(&self, other: &Self, ctx: &mut Interpreter) -> Result { + pub fn le(&self, other: &Self, ctx: &mut Context) -> Result { match other.abstract_relation(self, false, ctx)? { AbstractRelation::False => Ok(true), AbstractRelation::True | AbstractRelation::Undefined => Ok(false), @@ -562,7 +562,7 @@ impl Value { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Greater_than /// [spec]: https://tc39.es/ecma262/#sec-relational-operators-runtime-semantics-evaluation #[inline] - pub fn gt(&self, other: &Self, ctx: &mut Interpreter) -> Result { + pub fn gt(&self, other: &Self, ctx: &mut Context) -> Result { match other.abstract_relation(self, false, ctx)? { AbstractRelation::True => Ok(true), AbstractRelation::False | AbstractRelation::Undefined => Ok(false), @@ -579,7 +579,7 @@ impl Value { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Greater_than_or_equal /// [spec]: https://tc39.es/ecma262/#sec-relational-operators-runtime-semantics-evaluation #[inline] - pub fn ge(&self, other: &Self, ctx: &mut Interpreter) -> Result { + pub fn ge(&self, other: &Self, ctx: &mut Context) -> Result { match self.abstract_relation(other, true, ctx)? { AbstractRelation::False => Ok(true), AbstractRelation::True | AbstractRelation::Undefined => Ok(false), diff --git a/boa/src/builtins/value/tests.rs b/boa/src/builtins/value/tests.rs index c797c46585e..e25c26976d2 100644 --- a/boa/src/builtins/value/tests.rs +++ b/boa/src/builtins/value/tests.rs @@ -1,5 +1,5 @@ use super::*; -use crate::{forward, forward_val, Interpreter, Realm}; +use crate::{context::Context, forward, forward_val}; use std::collections::hash_map::DefaultHasher; use std::hash::{Hash, Hasher}; @@ -54,8 +54,7 @@ fn number_is_true() { // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness #[test] fn abstract_equality_comparison() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(forward(&mut engine, "undefined == undefined"), "true"); assert_eq!(forward(&mut engine, "null == null"), "true"); @@ -159,8 +158,7 @@ fn hash_object() { #[test] fn get_types() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!( forward_val(&mut engine, "undefined").unwrap().get_type(), @@ -250,8 +248,7 @@ fn to_string() { #[test] fn add_number_and_number() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let value = forward_val(&mut engine, "1 + 2").unwrap(); let value = value.to_i32(&mut engine).unwrap(); @@ -260,8 +257,7 @@ fn add_number_and_number() { #[test] fn add_number_and_string() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let value = forward_val(&mut engine, "1 + \" + 2 = 3\"").unwrap(); let value = value.to_string(&mut engine).unwrap(); @@ -270,8 +266,7 @@ fn add_number_and_string() { #[test] fn add_string_and_string() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let value = forward_val(&mut engine, "\"Hello\" + \", world\"").unwrap(); let value = value.to_string(&mut engine).unwrap(); @@ -280,8 +275,7 @@ fn add_string_and_string() { #[test] fn add_number_object_and_number() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let value = forward_val(&mut engine, "new Number(10) + 6").unwrap(); let value = value.to_i32(&mut engine).unwrap(); @@ -290,8 +284,7 @@ fn add_number_object_and_number() { #[test] fn add_number_object_and_string_object() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let value = forward_val(&mut engine, "new Number(10) + new String(\"0\")").unwrap(); let value = value.to_string(&mut engine).unwrap(); @@ -300,8 +293,7 @@ fn add_number_object_and_string_object() { #[test] fn sub_number_and_number() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let value = forward_val(&mut engine, "1 - 999").unwrap(); let value = value.to_i32(&mut engine).unwrap(); @@ -310,8 +302,7 @@ fn sub_number_and_number() { #[test] fn sub_number_object_and_number_object() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let value = forward_val(&mut engine, "new Number(1) - new Number(999)").unwrap(); let value = value.to_i32(&mut engine).unwrap(); @@ -320,8 +311,7 @@ fn sub_number_object_and_number_object() { #[test] fn sub_string_and_number_object() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let value = forward_val(&mut engine, "'Hello' - new Number(999)").unwrap(); let value = value.to_number(&mut engine).unwrap(); @@ -330,8 +320,7 @@ fn sub_string_and_number_object() { #[test] fn bitand_integer_and_integer() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let value = forward_val(&mut engine, "0xFFFF & 0xFF").unwrap(); let value = value.to_i32(&mut engine).unwrap(); @@ -340,8 +329,7 @@ fn bitand_integer_and_integer() { #[test] fn bitand_integer_and_rational() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let value = forward_val(&mut engine, "0xFFFF & 255.5").unwrap(); let value = value.to_i32(&mut engine).unwrap(); @@ -350,8 +338,7 @@ fn bitand_integer_and_rational() { #[test] fn bitand_rational_and_rational() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let value = forward_val(&mut engine, "255.772 & 255.5").unwrap(); let value = value.to_i32(&mut engine).unwrap(); @@ -361,8 +348,7 @@ fn bitand_rational_and_rational() { #[test] #[allow(clippy::float_cmp)] fn pow_number_and_number() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let value = forward_val(&mut engine, "3 ** 3").unwrap(); let value = value.to_number(&mut engine).unwrap(); @@ -371,8 +357,7 @@ fn pow_number_and_number() { #[test] fn pow_number_and_string() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let value = forward_val(&mut engine, "3 ** 'Hello'").unwrap(); let value = value.to_number(&mut engine).unwrap(); @@ -381,8 +366,7 @@ fn pow_number_and_string() { #[test] fn assign_pow_number_and_string() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let value = forward_val( &mut engine, @@ -406,8 +390,7 @@ fn display_string() { #[test] fn display_array_string() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let value = forward_val(&mut engine, "[\"Hello\"]").unwrap(); assert_eq!(value.display().to_string(), "[ \"Hello\" ]"); @@ -415,8 +398,7 @@ fn display_array_string() { #[test] fn display_boolean_object() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let d_obj = r#" let bool = new Boolean(0); bool @@ -427,8 +409,7 @@ fn display_boolean_object() { #[test] fn display_number_object() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let d_obj = r#" let num = new Number(3.14); num @@ -439,8 +420,7 @@ fn display_number_object() { #[test] fn display_negative_zero_object() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let d_obj = r#" let num = new Number(-0); num @@ -451,8 +431,7 @@ fn display_negative_zero_object() { #[test] fn debug_object() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let value = forward_val(&mut engine, "new Array([new Date()])").unwrap(); // We don't care about the contents of the debug display (it is *debug* after all). In the commit that this test was @@ -466,8 +445,7 @@ fn debug_object() { #[test] #[ignore] // TODO: Once objects are printed in a simpler way this test can be simplified and used fn display_object() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let d_obj = r#" let o = {a: 'a'}; o @@ -531,8 +509,7 @@ mod abstract_relational_comparison { #[test] fn number_less_than_number() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "1 < 2" => true); check_comparison!(engine, "2 < 2" => false); check_comparison!(engine, "3 < 2" => false); @@ -542,8 +519,7 @@ mod abstract_relational_comparison { #[test] fn string_less_than_number() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "'1' < 2" => true); check_comparison!(engine, "'2' < 2" => false); check_comparison!(engine, "'3' < 2" => false); @@ -553,8 +529,7 @@ mod abstract_relational_comparison { #[test] fn number_less_than_string() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "1 < '2'" => true); check_comparison!(engine, "2 < '2'" => false); check_comparison!(engine, "3 < '2'" => false); @@ -564,8 +539,7 @@ mod abstract_relational_comparison { #[test] fn number_object_less_than_number() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "new Number(1) < '2'" => true); check_comparison!(engine, "new Number(2) < '2'" => false); check_comparison!(engine, "new Number(3) < '2'" => false); @@ -575,8 +549,7 @@ mod abstract_relational_comparison { #[test] fn number_object_less_than_number_object() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "new Number(1) < new Number(2)" => true); check_comparison!(engine, "new Number(2) < new Number(2)" => false); check_comparison!(engine, "new Number(3) < new Number(2)" => false); @@ -586,8 +559,7 @@ mod abstract_relational_comparison { #[test] fn string_less_than_string() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "'hello' < 'hello'" => false); check_comparison!(engine, "'hell' < 'hello'" => true); check_comparison!(engine, "'hello, world' < 'world'" => true); @@ -596,8 +568,7 @@ mod abstract_relational_comparison { #[test] fn string_object_less_than_string() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "new String('hello') < 'hello'" => false); check_comparison!(engine, "new String('hell') < 'hello'" => true); check_comparison!(engine, "new String('hello, world') < 'world'" => true); @@ -606,8 +577,7 @@ mod abstract_relational_comparison { #[test] fn string_object_less_than_string_object() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "new String('hello') < new String('hello')" => false); check_comparison!(engine, "new String('hell') < new String('hello')" => true); check_comparison!(engine, "new String('hello, world') < new String('world')" => true); @@ -616,8 +586,7 @@ mod abstract_relational_comparison { #[test] fn bigint_less_than_number() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "1n < 10" => true); check_comparison!(engine, "10n < 10" => false); check_comparison!(engine, "100n < 10" => false); @@ -626,8 +595,7 @@ mod abstract_relational_comparison { #[test] fn number_less_than_bigint() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "10 < 1n" => false); check_comparison!(engine, "1 < 1n" => false); check_comparison!(engine, "-1 < -1n" => false); @@ -636,40 +604,35 @@ mod abstract_relational_comparison { #[test] fn negative_infnity_less_than_bigint() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "-Infinity < -10000000000n" => true); check_comparison!(engine, "-Infinity < (-1n << 100n)" => true); } #[test] fn bigint_less_than_infinity() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "1000n < NaN" => false); check_comparison!(engine, "(1n << 100n) < NaN" => false); } #[test] fn nan_less_than_bigint() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "NaN < -10000000000n" => false); check_comparison!(engine, "NaN < (-1n << 100n)" => false); } #[test] fn bigint_less_than_nan() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "1000n < Infinity" => true); check_comparison!(engine, "(1n << 100n) < Infinity" => true); } #[test] fn bigint_less_than_string() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "1000n < '1000'" => false); check_comparison!(engine, "1000n < '2000'" => true); check_comparison!(engine, "1n < '-1'" => false); @@ -679,8 +642,7 @@ mod abstract_relational_comparison { #[test] fn string_less_than_bigint() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "'1000' < 1000n" => false); check_comparison!(engine, "'2000' < 1000n" => false); check_comparison!(engine, "'500' < 1000n" => true); @@ -693,8 +655,7 @@ mod abstract_relational_comparison { #[test] fn number_less_than_or_equal_number() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "1 <= 2" => true); check_comparison!(engine, "2 <= 2" => true); check_comparison!(engine, "3 <= 2" => false); @@ -704,8 +665,7 @@ mod abstract_relational_comparison { #[test] fn string_less_than_or_equal_number() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "'1' <= 2" => true); check_comparison!(engine, "'2' <= 2" => true); check_comparison!(engine, "'3' <= 2" => false); @@ -715,8 +675,7 @@ mod abstract_relational_comparison { #[test] fn number_less_than_or_equal_string() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "1 <= '2'" => true); check_comparison!(engine, "2 <= '2'" => true); check_comparison!(engine, "3 <= '2'" => false); @@ -726,8 +685,7 @@ mod abstract_relational_comparison { #[test] fn number_object_less_than_or_equal_number() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "new Number(1) <= '2'" => true); check_comparison!(engine, "new Number(2) <= '2'" => true); check_comparison!(engine, "new Number(3) <= '2'" => false); @@ -737,8 +695,7 @@ mod abstract_relational_comparison { #[test] fn number_object_less_than_number_or_equal_object() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "new Number(1) <= new Number(2)" => true); check_comparison!(engine, "new Number(2) <= new Number(2)" => true); check_comparison!(engine, "new Number(3) <= new Number(2)" => false); @@ -748,8 +705,7 @@ mod abstract_relational_comparison { #[test] fn string_less_than_or_equal_string() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "'hello' <= 'hello'" => true); check_comparison!(engine, "'hell' <= 'hello'" => true); check_comparison!(engine, "'hello, world' <= 'world'" => true); @@ -758,8 +714,7 @@ mod abstract_relational_comparison { #[test] fn string_object_less_than_or_equal_string() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "new String('hello') <= 'hello'" => true); check_comparison!(engine, "new String('hell') <= 'hello'" => true); check_comparison!(engine, "new String('hello, world') <= 'world'" => true); @@ -768,8 +723,7 @@ mod abstract_relational_comparison { #[test] fn string_object_less_than_string_or_equal_object() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "new String('hello') <= new String('hello')" => true); check_comparison!(engine, "new String('hell') <= new String('hello')" => true); check_comparison!(engine, "new String('hello, world') <= new String('world')" => true); @@ -778,8 +732,7 @@ mod abstract_relational_comparison { #[test] fn bigint_less_than_or_equal_number() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "1n <= 10" => true); check_comparison!(engine, "10n <= 10" => true); check_comparison!(engine, "100n <= 10" => false); @@ -788,8 +741,7 @@ mod abstract_relational_comparison { #[test] fn number_less_than_or_equal_bigint() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "10 <= 1n" => false); check_comparison!(engine, "1 <= 1n" => true); check_comparison!(engine, "-1 <= -1n" => true); @@ -798,40 +750,35 @@ mod abstract_relational_comparison { #[test] fn negative_infnity_less_than_or_equal_bigint() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "-Infinity <= -10000000000n" => true); check_comparison!(engine, "-Infinity <= (-1n << 100n)" => true); } #[test] fn bigint_less_than_or_equal_infinity() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "1000n <= NaN" => false); check_comparison!(engine, "(1n << 100n) <= NaN" => false); } #[test] fn nan_less_than_or_equal_bigint() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "NaN <= -10000000000n" => false); check_comparison!(engine, "NaN <= (-1n << 100n)" => false); } #[test] fn bigint_less_than_or_equal_nan() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "1000n <= Infinity" => true); check_comparison!(engine, "(1n << 100n) <= Infinity" => true); } #[test] fn bigint_less_than_or_equal_string() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "1000n <= '1000'" => true); check_comparison!(engine, "1000n <= '2000'" => true); check_comparison!(engine, "1n <= '-1'" => false); @@ -841,8 +788,7 @@ mod abstract_relational_comparison { #[test] fn string_less_than_or_equal_bigint() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "'1000' <= 1000n" => true); check_comparison!(engine, "'2000' <= 1000n" => false); check_comparison!(engine, "'500' <= 1000n" => true); @@ -855,8 +801,7 @@ mod abstract_relational_comparison { #[test] fn number_greater_than_number() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "1 > 2" => false); check_comparison!(engine, "2 > 2" => false); check_comparison!(engine, "3 > 2" => true); @@ -866,8 +811,7 @@ mod abstract_relational_comparison { #[test] fn string_greater_than_number() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "'1' > 2" => false); check_comparison!(engine, "'2' > 2" => false); check_comparison!(engine, "'3' > 2" => true); @@ -877,8 +821,7 @@ mod abstract_relational_comparison { #[test] fn number_less_greater_string() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "1 > '2'" => false); check_comparison!(engine, "2 > '2'" => false); check_comparison!(engine, "3 > '2'" => true); @@ -888,8 +831,7 @@ mod abstract_relational_comparison { #[test] fn number_object_greater_than_number() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "new Number(1) > '2'" => false); check_comparison!(engine, "new Number(2) > '2'" => false); check_comparison!(engine, "new Number(3) > '2'" => true); @@ -899,8 +841,7 @@ mod abstract_relational_comparison { #[test] fn number_object_greater_than_number_object() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "new Number(1) > new Number(2)" => false); check_comparison!(engine, "new Number(2) > new Number(2)" => false); check_comparison!(engine, "new Number(3) > new Number(2)" => true); @@ -910,8 +851,7 @@ mod abstract_relational_comparison { #[test] fn string_greater_than_string() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "'hello' > 'hello'" => false); check_comparison!(engine, "'hell' > 'hello'" => false); check_comparison!(engine, "'hello, world' > 'world'" => false); @@ -921,8 +861,7 @@ mod abstract_relational_comparison { #[test] fn string_object_greater_than_string() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "new String('hello') > 'hello'" => false); check_comparison!(engine, "new String('hell') > 'hello'" => false); check_comparison!(engine, "new String('hello, world') > 'world'" => false); @@ -932,8 +871,7 @@ mod abstract_relational_comparison { #[test] fn string_object_greater_than_string_object() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "new String('hello') > new String('hello')" => false); check_comparison!(engine, "new String('hell') > new String('hello')" => false); check_comparison!(engine, "new String('hello, world') > new String('world')" => false); @@ -943,8 +881,7 @@ mod abstract_relational_comparison { #[test] fn bigint_greater_than_number() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "1n > 10" => false); check_comparison!(engine, "10n > 10" => false); check_comparison!(engine, "100n > 10" => true); @@ -953,8 +890,7 @@ mod abstract_relational_comparison { #[test] fn number_greater_than_bigint() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "10 > 1n" => true); check_comparison!(engine, "1 > 1n" => false); check_comparison!(engine, "-1 > -1n" => false); @@ -963,40 +899,35 @@ mod abstract_relational_comparison { #[test] fn negative_infnity_greater_than_bigint() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "-Infinity > -10000000000n" => false); check_comparison!(engine, "-Infinity > (-1n << 100n)" => false); } #[test] fn bigint_greater_than_infinity() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "1000n > NaN" => false); check_comparison!(engine, "(1n << 100n) > NaN" => false); } #[test] fn nan_greater_than_bigint() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "NaN > -10000000000n" => false); check_comparison!(engine, "NaN > (-1n << 100n)" => false); } #[test] fn bigint_greater_than_nan() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "1000n > Infinity" => false); check_comparison!(engine, "(1n << 100n) > Infinity" => false); } #[test] fn bigint_greater_than_string() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "1000n > '1000'" => false); check_comparison!(engine, "1000n > '2000'" => false); check_comparison!(engine, "1n > '-1'" => true); @@ -1006,8 +937,7 @@ mod abstract_relational_comparison { #[test] fn string_greater_than_bigint() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "'1000' > 1000n" => false); check_comparison!(engine, "'2000' > 1000n" => true); check_comparison!(engine, "'500' > 1000n" => false); @@ -1020,8 +950,7 @@ mod abstract_relational_comparison { #[test] fn number_greater_than_or_equal_number() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "1 >= 2" => false); check_comparison!(engine, "2 >= 2" => true); check_comparison!(engine, "3 >= 2" => true); @@ -1031,8 +960,7 @@ mod abstract_relational_comparison { #[test] fn string_greater_than_or_equal_number() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "'1' >= 2" => false); check_comparison!(engine, "'2' >= 2" => true); check_comparison!(engine, "'3' >= 2" => true); @@ -1042,8 +970,7 @@ mod abstract_relational_comparison { #[test] fn number_less_greater_or_equal_string() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "1 >= '2'" => false); check_comparison!(engine, "2 >= '2'" => true); check_comparison!(engine, "3 >= '2'" => true); @@ -1053,8 +980,7 @@ mod abstract_relational_comparison { #[test] fn number_object_greater_than_or_equal_number() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "new Number(1) >= '2'" => false); check_comparison!(engine, "new Number(2) >= '2'" => true); check_comparison!(engine, "new Number(3) >= '2'" => true); @@ -1064,8 +990,7 @@ mod abstract_relational_comparison { #[test] fn number_object_greater_than_or_equal_number_object() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "new Number(1) >= new Number(2)" => false); check_comparison!(engine, "new Number(2) >= new Number(2)" => true); check_comparison!(engine, "new Number(3) >= new Number(2)" => true); @@ -1075,8 +1000,7 @@ mod abstract_relational_comparison { #[test] fn string_greater_than_or_equal_string() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "'hello' >= 'hello'" => true); check_comparison!(engine, "'hell' >= 'hello'" => false); check_comparison!(engine, "'hello, world' >= 'world'" => false); @@ -1086,8 +1010,7 @@ mod abstract_relational_comparison { #[test] fn string_object_greater_or_equal_than_string() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "new String('hello') >= 'hello'" => true); check_comparison!(engine, "new String('hell') >= 'hello'" => false); check_comparison!(engine, "new String('hello, world') >= 'world'" => false); @@ -1097,8 +1020,7 @@ mod abstract_relational_comparison { #[test] fn string_object_greater_than_or_equal_string_object() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "new String('hello') >= new String('hello')" => true); check_comparison!(engine, "new String('hell') >= new String('hello')" => false); check_comparison!(engine, "new String('hello, world') >= new String('world')" => false); @@ -1108,8 +1030,7 @@ mod abstract_relational_comparison { #[test] fn bigint_greater_than_or_equal_number() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "1n >= 10" => false); check_comparison!(engine, "10n >= 10" => true); check_comparison!(engine, "100n >= 10" => true); @@ -1118,8 +1039,7 @@ mod abstract_relational_comparison { #[test] fn number_greater_than_or_equal_bigint() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "10 >= 1n" => true); check_comparison!(engine, "1 >= 1n" => true); check_comparison!(engine, "-1 >= -1n" => true); @@ -1128,40 +1048,35 @@ mod abstract_relational_comparison { #[test] fn negative_infnity_greater_or_equal_than_bigint() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "-Infinity >= -10000000000n" => false); check_comparison!(engine, "-Infinity >= (-1n << 100n)" => false); } #[test] fn bigint_greater_than_or_equal_infinity() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "1000n >= NaN" => false); check_comparison!(engine, "(1n << 100n) >= NaN" => false); } #[test] fn nan_greater_than_or_equal_bigint() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "NaN >= -10000000000n" => false); check_comparison!(engine, "NaN >= (-1n << 100n)" => false); } #[test] fn bigint_greater_than_or_equal_nan() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "1000n >= Infinity" => false); check_comparison!(engine, "(1n << 100n) >= Infinity" => false); } #[test] fn bigint_greater_than_or_equal_string() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "1000n >= '1000'" => true); check_comparison!(engine, "1000n >= '2000'" => false); check_comparison!(engine, "1n >= '-1'" => true); @@ -1171,8 +1086,7 @@ mod abstract_relational_comparison { #[test] fn string_greater_than_or_equal_bigint() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); check_comparison!(engine, "'1000' >= 1000n" => true); check_comparison!(engine, "'2000' >= 1000n" => true); check_comparison!(engine, "'500' >= 1000n" => false); diff --git a/boa/src/context.rs b/boa/src/context.rs new file mode 100644 index 00000000000..d42e9b3e5aa --- /dev/null +++ b/boa/src/context.rs @@ -0,0 +1,496 @@ +//! Javascript context. + +use crate::{ + builtins::{ + self, + function::{Function, FunctionFlags, NativeFunction}, + object::ObjectData, + object::{Class, ClassBuilder, GcObject, Object, PROTOTYPE}, + property::{Property, PropertyKey}, + value::{PreferredType, RcSymbol, Type}, + value::{RcString, Value}, + Console, Symbol, + }, + exec::Interpreter, + realm::Realm, + syntax::{ + ast::{ + node::{ + statement_list::RcStatementList, Call, FormalParameter, Identifier, New, + StatementList, + }, + Const, Node, + }, + Parser, + }, + BoaProfiler, Executable, Result, +}; +use std::result::Result as StdResult; + +/// Javascript context. It is the primary way to interact with the runtime. +/// +/// For each `Context` instance a new instance of runtime is created. +/// It means that it is safe to use different contexts in different threads, +/// but each `Context` instance must be used only from a single thread. +#[derive(Debug)] +pub struct Context { + /// realm holds both the global object and the environment + realm: Realm, + + /// The current executor. + executor: Interpreter, + + /// Symbol hash. + /// + /// For now this is an incremental u32 number. + symbol_count: u32, + + /// console object state. + console: Console, +} + +impl Default for Context { + fn default() -> Self { + let realm = Realm::create(); + let executor = Interpreter::new(); + let mut context = Self { + realm, + executor, + symbol_count: 0, + console: Console::default(), + }; + + // Add new builtIns to Context Realm + // At a later date this can be removed from here and called explicitly, + // but for now we almost always want these default builtins + context.create_intrinsics(); + + context + } +} + +impl Context { + /// Create a new `Context`. + pub fn new() -> Self { + Default::default() + } + + pub fn realm(&self) -> &Realm { + &self.realm + } + + pub fn realm_mut(&mut self) -> &mut Realm { + &mut self.realm + } + + pub fn executor(&mut self) -> &mut Interpreter { + &mut self.executor + } + + /// A helper function for getting a immutable reference to the `console` object. + pub(crate) fn console(&self) -> &Console { + &self.console + } + + /// A helper function for getting a mutable reference to the `console` object. + pub(crate) fn console_mut(&mut self) -> &mut Console { + &mut self.console + } + + /// Sets up the default global objects within Global + fn create_intrinsics(&mut self) { + let _timer = BoaProfiler::global().start_event("create_intrinsics", "interpreter"); + // Create intrinsics, add global objects here + builtins::init(self); + } + + /// Generates a new `Symbol` internal hash. + /// + /// This currently is an incremented value. + #[inline] + fn generate_hash(&mut self) -> u32 { + let hash = self.symbol_count; + self.symbol_count += 1; + hash + } + + /// Construct a new `Symbol` with an optional description. + #[inline] + pub fn construct_symbol(&mut self, description: Option) -> RcSymbol { + RcSymbol::from(Symbol::new(self.generate_hash(), description)) + } + + /// Construct an empty object. + #[inline] + pub fn construct_object(&self) -> GcObject { + let object_prototype = self + .global_object() + .get_field("Object") + .get_field(PROTOTYPE); + GcObject::new(Object::create(object_prototype)) + } + + /// + pub(crate) fn call(&mut self, f: &Value, this: &Value, args: &[Value]) -> Result { + match *f { + Value::Object(ref object) => object.call(this, args, self), + _ => self.throw_type_error("not a function"), + } + } + + /// Retrun the global object. + pub(crate) fn global_object(&self) -> &Value { + &self.realm.global_obj + } + + /// Constructs a `RangeError` with the specified message. + pub fn construct_range_error(&mut self, message: M) -> Value + where + M: Into, + { + // Runs a `new RangeError(message)`. + New::from(Call::new( + Identifier::from("RangeError"), + vec![Const::from(message.into()).into()], + )) + .run(self) + .expect_err("RangeError should always throw") + } + + /// Throws a `RangeError` with the specified message. + pub fn throw_range_error(&mut self, message: M) -> Result + where + M: Into, + { + Err(self.construct_range_error(message)) + } + + /// Constructs a `TypeError` with the specified message. + pub fn construct_type_error(&mut self, message: M) -> Value + where + M: Into, + { + // Runs a `new TypeError(message)`. + New::from(Call::new( + Identifier::from("TypeError"), + vec![Const::from(message.into()).into()], + )) + .run(self) + .expect_err("TypeError should always throw") + } + + /// Throws a `TypeError` with the specified message. + pub fn throw_type_error(&mut self, message: M) -> Result + where + M: Into, + { + Err(self.construct_type_error(message)) + } + + /// Constructs a `ReferenceError` with the specified message. + pub fn construct_reference_error(&mut self, message: M) -> Value + where + M: Into, + { + New::from(Call::new( + Identifier::from("ReferenceError"), + vec![Const::from(message.into() + " is not defined").into()], + )) + .run(self) + .expect_err("ReferenceError should always throw") + } + + /// Throws a `ReferenceError` with the specified message. + pub fn throw_reference_error(&mut self, message: M) -> Result + where + M: Into, + { + Err(self.construct_reference_error(message)) + } + + /// Constructs a `SyntaxError` with the specified message. + pub fn construct_syntax_error(&mut self, message: M) -> Value + where + M: Into, + { + New::from(Call::new( + Identifier::from("SyntaxError"), + vec![Const::from(message.into()).into()], + )) + .run(self) + .expect_err("SyntaxError should always throw") + } + + /// Throws a `SyntaxError` with the specified message. + pub fn throw_syntax_error(&mut self, message: M) -> Result + where + M: Into, + { + Err(self.construct_syntax_error(message)) + } + + /// Utility to create a function Value for Function Declarations, Arrow Functions or Function Expressions + pub(crate) fn create_function( + &mut self, + params: P, + body: B, + flags: FunctionFlags, + ) -> Value + where + P: Into>, + B: Into, + { + let function_prototype = self + .global_object() + .get_field("Function") + .get_field(PROTOTYPE); + + // Every new function has a prototype property pre-made + let proto = Value::new_object(Some(self.global_object())); + + let params = params.into(); + let params_len = params.len(); + let func = Function::Ordinary { + flags, + body: RcStatementList::from(body.into()), + params, + environment: self.realm.environment.get_current_environment().clone(), + }; + + let new_func = Object::function(func, function_prototype); + + let val = Value::from(new_func); + + // Set constructor field to the newly created Value (function object) + proto.set_field("constructor", val.clone()); + + val.set_field(PROTOTYPE, proto); + val.set_field("length", Value::from(params_len)); + + val + } + + /// Create a new builin function. + pub fn create_builtin_function( + &mut self, + name: &str, + length: usize, + body: NativeFunction, + ) -> Result { + let function_prototype = self + .global_object() + .get_field("Function") + .get_field(PROTOTYPE); + + // Every new function has a prototype property pre-made + let proto = Value::new_object(Some(self.global_object())); + let mut function = Object::function( + Function::BuiltIn(body.into(), FunctionFlags::CALLABLE), + function_prototype, + ); + function.set(PROTOTYPE.into(), proto); + function.set("length".into(), length.into()); + function.set("name".into(), name.into()); + + Ok(GcObject::new(function)) + } + + /// Register a global function. + pub fn register_global_function( + &mut self, + name: &str, + length: usize, + body: NativeFunction, + ) -> Result<()> { + let function = self.create_builtin_function(name, length, body)?; + self.global_object().set_field(name, function); + Ok(()) + } + + /// Converts an array object into a rust vector of values. + /// + /// This is useful for the spread operator, for any other object an `Err` is returned + pub(crate) fn extract_array_properties(&mut self, value: &Value) -> StdResult, ()> { + if let Value::Object(ref x) = value { + // Check if object is array + if let ObjectData::Array = x.borrow().data { + let length = value.get_field("length").as_number().unwrap() as i32; + let values = (0..length) + .map(|idx| value.get_field(idx.to_string())) + .collect(); + return Ok(values); + } + // Check if object is a Map + else if let ObjectData::Map(ref map) = x.borrow().data { + let values = map + .iter() + .map(|(key, value)| { + // Construct a new array containing the key-value pair + let array = Value::new_object(Some( + &self + .realm() + .environment + .get_global_object() + .expect("Could not get global object"), + )); + array.set_data(ObjectData::Array); + array.as_object_mut().expect("object").set_prototype( + self.realm() + .environment + .get_binding_value("Array") + .expect("Array was not initialized") + .get_field(PROTOTYPE), + ); + array.set_field("0", key); + array.set_field("1", value); + array.set_field("length", Value::from(2)); + array + }) + .collect(); + return Ok(values); + } + + return Err(()); + } + + Err(()) + } + + /// Converts an object to a primitive. + /// + /// More information: + /// - [ECMAScript][spec] + /// + /// [spec]: https://tc39.es/ecma262/#sec-ordinarytoprimitive + pub(crate) fn ordinary_to_primitive( + &mut self, + o: &Value, + hint: PreferredType, + ) -> Result { + // 1. Assert: Type(O) is Object. + debug_assert!(o.get_type() == Type::Object); + // 2. Assert: Type(hint) is String and its value is either "string" or "number". + debug_assert!(hint == PreferredType::String || hint == PreferredType::Number); + + // 3. If hint is "string", then + // a. Let methodNames be « "toString", "valueOf" ». + // 4. Else, + // a. Let methodNames be « "valueOf", "toString" ». + let method_names = if hint == PreferredType::String { + ["toString", "valueOf"] + } else { + ["valueOf", "toString"] + }; + + // 5. For each name in methodNames in List order, do + for name in &method_names { + // a. Let method be ? Get(O, name). + let method: Value = o.get_field(*name); + // b. If IsCallable(method) is true, then + if method.is_function() { + // i. Let result be ? Call(method, O). + let result = self.call(&method, &o, &[])?; + // ii. If Type(result) is not Object, return result. + if !result.is_object() { + return Ok(result); + } + } + } + + // 6. Throw a TypeError exception. + self.throw_type_error("cannot convert object to primitive value") + } + + /// https://tc39.es/ecma262/#sec-hasproperty + pub(crate) fn has_property(&self, obj: &Value, key: &PropertyKey) -> bool { + if let Some(obj) = obj.as_object() { + obj.has_property(key) + } else { + false + } + } + + pub(crate) fn set_value(&mut self, node: &Node, value: Value) -> Result { + match node { + Node::Identifier(ref name) => { + self.realm + .environment + .set_mutable_binding(name.as_ref(), value.clone(), true); + Ok(value) + } + Node::GetConstField(ref get_const_field_node) => Ok(get_const_field_node + .obj() + .run(self)? + .set_field(get_const_field_node.field(), value)), + Node::GetField(ref get_field) => { + let field = get_field.field().run(self)?; + let key = field.to_property_key(self)?; + Ok(get_field.obj().run(self)?.set_field(key, value)) + } + _ => panic!("TypeError: invalid assignment to {}", node), + } + } + + /// Register a global class of type `T`, where `T` implemets `Class`. + /// + /// # Example + /// ```ignore + /// #[derive(Debug, Trace, Finalize)] + /// struct MyClass; + /// + /// impl Class for MyClass { + /// // ... + /// } + /// + /// context.register_global_class::(); + /// ``` + pub fn register_global_class(&mut self) -> Result<()> + where + T: Class, + { + let mut class_builder = ClassBuilder::new::(self); + T::init(&mut class_builder)?; + + let class = class_builder.build(); + let property = Property::data_descriptor(class.into(), T::ATTRIBUTE); + self.global_object() + .as_object_mut() + .unwrap() + .insert_property(T::NAME, property); + Ok(()) + } + + fn parser_expr(src: &str) -> StdResult { + Parser::new(src.as_bytes()) + .parse_all() + .map_err(|e| e.to_string()) + } + + /// Evaluates the given code. + /// + /// # Examples + /// ``` + ///# use boa::Context; + /// let mut context = Context::new(); + /// + /// let value = context.eval("1 + 3").unwrap(); + /// + /// assert!(value.is_number()); + /// assert_eq!(value.as_number().unwrap(), 4.0); + /// ``` + #[allow(clippy::unit_arg, clippy::drop_copy)] + pub fn eval(&mut self, src: &str) -> Result { + let main_timer = BoaProfiler::global().start_event("Main", "Main"); + + let result = match Self::parser_expr(src) { + Ok(expr) => expr.run(self), + Err(e) => self.throw_type_error(e), + }; + + // The main_timer needs to be dropped before the BoaProfiler is. + drop(main_timer); + BoaProfiler::global().drop(); + + result + } +} diff --git a/boa/src/exec/array/mod.rs b/boa/src/exec/array/mod.rs index d61788395d5..5eff93f2361 100644 --- a/boa/src/exec/array/mod.rs +++ b/boa/src/exec/array/mod.rs @@ -1,6 +1,6 @@ //! Array declaration execution. -use super::{Executable, Interpreter}; +use super::{Context, Executable}; use crate::{ builtins::{Array, Value}, syntax::ast::node::{ArrayDecl, Node}, @@ -8,7 +8,7 @@ use crate::{ }; impl Executable for ArrayDecl { - fn run(&self, interpreter: &mut Interpreter) -> Result { + fn run(&self, interpreter: &mut Context) -> Result { let _timer = BoaProfiler::global().start_event("ArrayDecl", "exec"); let array = Array::new_array(interpreter)?; let mut elements = Vec::new(); diff --git a/boa/src/exec/block/mod.rs b/boa/src/exec/block/mod.rs index 4835022ed2b..803696c9ade 100644 --- a/boa/src/exec/block/mod.rs +++ b/boa/src/exec/block/mod.rs @@ -1,13 +1,13 @@ //! Block statement execution. -use super::{Executable, Interpreter, InterpreterState}; +use super::{Context, Executable, InterpreterState}; use crate::{ builtins::value::Value, environment::lexical_environment::new_declarative_environment, syntax::ast::node::Block, BoaProfiler, Result, }; impl Executable for Block { - fn run(&self, interpreter: &mut Interpreter) -> Result { + fn run(&self, interpreter: &mut Context) -> Result { let _timer = BoaProfiler::global().start_event("Block", "exec"); { let env = &mut interpreter.realm_mut().environment; @@ -22,7 +22,7 @@ impl Executable for Block { for statement in self.statements() { obj = statement.run(interpreter)?; - match interpreter.get_current_state() { + match interpreter.executor().get_current_state() { InterpreterState::Return => { // Early return. break; diff --git a/boa/src/exec/break_node/mod.rs b/boa/src/exec/break_node/mod.rs index 76acb67db9b..458a6ec4b4a 100644 --- a/boa/src/exec/break_node/mod.rs +++ b/boa/src/exec/break_node/mod.rs @@ -1,4 +1,4 @@ -use super::{Executable, Interpreter, InterpreterState}; +use super::{Context, Executable, InterpreterState}; use crate::{ builtins::value::Value, syntax::ast::node::{Break, Continue}, @@ -9,16 +9,20 @@ use crate::{ mod tests; impl Executable for Break { - fn run(&self, interpreter: &mut Interpreter) -> Result { - interpreter.set_current_state(InterpreterState::Break(self.label().map(String::from))); + fn run(&self, interpreter: &mut Context) -> Result { + interpreter + .executor() + .set_current_state(InterpreterState::Break(self.label().map(String::from))); Ok(Value::undefined()) } } impl Executable for Continue { - fn run(&self, interpreter: &mut Interpreter) -> Result { - interpreter.set_current_state(InterpreterState::Continue(self.label().map(String::from))); + fn run(&self, interpreter: &mut Context) -> Result { + interpreter + .executor() + .set_current_state(InterpreterState::Continue(self.label().map(String::from))); Ok(Value::undefined()) } diff --git a/boa/src/exec/break_node/tests.rs b/boa/src/exec/break_node/tests.rs index 2cbb2db0324..02d2557e9e4 100644 --- a/boa/src/exec/break_node/tests.rs +++ b/boa/src/exec/break_node/tests.rs @@ -1,17 +1,16 @@ -use super::{Interpreter, InterpreterState}; -use crate::{exec::Executable, syntax::ast::node::Break, Realm}; +use super::{Context, InterpreterState}; +use crate::{exec::Executable, syntax::ast::node::Break}; #[test] fn check_post_state() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let brk: Break = Break::new("label"); brk.run(&mut engine).unwrap(); assert_eq!( - engine.get_current_state(), + engine.executor().get_current_state(), &InterpreterState::Break(Some("label".to_string())) ); } diff --git a/boa/src/exec/call/mod.rs b/boa/src/exec/call/mod.rs index ad4d23f1557..7c61e4c1d3b 100644 --- a/boa/src/exec/call/mod.rs +++ b/boa/src/exec/call/mod.rs @@ -1,4 +1,4 @@ -use super::{Executable, Interpreter, InterpreterState}; +use super::{Context, Executable, InterpreterState}; use crate::{ builtins::value::{Type, Value}, syntax::ast::node::{Call, Node}, @@ -6,7 +6,7 @@ use crate::{ }; impl Executable for Call { - fn run(&self, interpreter: &mut Interpreter) -> Result { + fn run(&self, interpreter: &mut Context) -> Result { let _timer = BoaProfiler::global().start_event("Call", "exec"); let (this, func) = match self.expr() { Node::GetConstField(ref get_const_field) => { @@ -44,7 +44,9 @@ impl Executable for Call { let fnct_result = interpreter.call(&func, &this, &v_args); // unset the early return flag - interpreter.set_current_state(InterpreterState::Executing); + interpreter + .executor() + .set_current_state(InterpreterState::Executing); fnct_result } diff --git a/boa/src/exec/conditional/mod.rs b/boa/src/exec/conditional/mod.rs index 17409d66ad1..d0501897513 100644 --- a/boa/src/exec/conditional/mod.rs +++ b/boa/src/exec/conditional/mod.rs @@ -1,14 +1,13 @@ -use super::{Executable, Interpreter}; +use super::{Context, Executable}; use crate::{ builtins::Value, syntax::ast::node::{ConditionalOp, If}, Result, }; -use std::borrow::Borrow; impl Executable for If { - fn run(&self, interpreter: &mut Interpreter) -> Result { - Ok(if self.cond().run(interpreter)?.borrow().to_boolean() { + fn run(&self, interpreter: &mut Context) -> Result { + Ok(if self.cond().run(interpreter)?.to_boolean() { self.body().run(interpreter)? } else if let Some(ref else_e) = self.else_node() { else_e.run(interpreter)? @@ -19,8 +18,8 @@ impl Executable for If { } impl Executable for ConditionalOp { - fn run(&self, interpreter: &mut Interpreter) -> Result { - Ok(if self.cond().run(interpreter)?.borrow().to_boolean() { + fn run(&self, interpreter: &mut Context) -> Result { + Ok(if self.cond().run(interpreter)?.to_boolean() { self.if_true().run(interpreter)? } else { self.if_false().run(interpreter)? diff --git a/boa/src/exec/declaration/mod.rs b/boa/src/exec/declaration/mod.rs index 5b5678de107..377f0baa002 100644 --- a/boa/src/exec/declaration/mod.rs +++ b/boa/src/exec/declaration/mod.rs @@ -1,6 +1,6 @@ //! Declaration execution. -use super::{Executable, Interpreter}; +use super::{Context, Executable}; use crate::{ builtins::{function::FunctionFlags, Value}, environment::lexical_environment::VariableScope, @@ -11,7 +11,7 @@ use crate::{ }; impl Executable for FunctionDecl { - fn run(&self, interpreter: &mut Interpreter) -> Result { + fn run(&self, interpreter: &mut Context) -> Result { let _timer = BoaProfiler::global().start_event("FunctionDecl", "exec"); let val = interpreter.create_function( self.parameters().to_vec(), @@ -37,7 +37,7 @@ impl Executable for FunctionDecl { } impl Executable for FunctionExpr { - fn run(&self, interpreter: &mut Interpreter) -> Result { + fn run(&self, interpreter: &mut Context) -> Result { let val = interpreter.create_function( self.parameters().to_vec(), self.body().to_vec(), @@ -53,7 +53,7 @@ impl Executable for FunctionExpr { } impl Executable for VarDeclList { - fn run(&self, interpreter: &mut Interpreter) -> Result { + fn run(&self, interpreter: &mut Context) -> Result { for var in self.as_ref() { let val = match var.init() { Some(v) => v.run(interpreter)?, @@ -79,7 +79,7 @@ impl Executable for VarDeclList { } impl Executable for ConstDeclList { - fn run(&self, interpreter: &mut Interpreter) -> Result { + fn run(&self, interpreter: &mut Context) -> Result { for decl in self.as_ref() { let val = decl.init().run(interpreter)?; @@ -98,7 +98,7 @@ impl Executable for ConstDeclList { } impl Executable for LetDeclList { - fn run(&self, interpreter: &mut Interpreter) -> Result { + fn run(&self, interpreter: &mut Context) -> Result { for var in self.as_ref() { let val = match var.init() { Some(v) => v.run(interpreter)?, @@ -119,7 +119,7 @@ impl Executable for LetDeclList { } impl Executable for ArrowFunctionDecl { - fn run(&self, interpreter: &mut Interpreter) -> Result { + fn run(&self, interpreter: &mut Context) -> Result { Ok(interpreter.create_function( self.params().to_vec(), self.body().to_vec(), diff --git a/boa/src/exec/exception.rs b/boa/src/exec/exception.rs deleted file mode 100644 index 06ea6e8830c..00000000000 --- a/boa/src/exec/exception.rs +++ /dev/null @@ -1,96 +0,0 @@ -use super::*; -use crate::{ - exec::Executable, - syntax::ast::{ - node::{Call, Identifier, New}, - Const, - }, -}; - -impl Interpreter { - /// Constructs a `RangeError` with the specified message. - pub fn construct_range_error(&mut self, message: M) -> Value - where - M: Into, - { - // Runs a `new RangeError(message)`. - New::from(Call::new( - Identifier::from("RangeError"), - vec![Const::from(message.into()).into()], - )) - .run(self) - .expect_err("RangeError should always throw") - } - - /// Throws a `RangeError` with the specified message. - pub fn throw_range_error(&mut self, message: M) -> Result - where - M: Into, - { - Err(self.construct_range_error(message)) - } - - /// Constructs a `TypeError` with the specified message. - pub fn construct_type_error(&mut self, message: M) -> Value - where - M: Into, - { - // Runs a `new TypeError(message)`. - New::from(Call::new( - Identifier::from("TypeError"), - vec![Const::from(message.into()).into()], - )) - .run(self) - .expect_err("TypeError should always throw") - } - - /// Throws a `TypeError` with the specified message. - pub fn throw_type_error(&mut self, message: M) -> Result - where - M: Into, - { - Err(self.construct_type_error(message)) - } - - /// Constructs a `ReferenceError` with the specified message. - pub fn construct_reference_error(&mut self, message: M) -> Value - where - M: Into, - { - New::from(Call::new( - Identifier::from("ReferenceError"), - vec![Const::from(message.into() + " is not defined").into()], - )) - .run(self) - .expect_err("ReferenceError should always throw") - } - - /// Throws a `ReferenceError` with the specified message. - pub fn throw_reference_error(&mut self, message: M) -> Result - where - M: Into, - { - Err(self.construct_reference_error(message)) - } - - /// Constructs a `SyntaxError` with the specified message. - pub fn construct_syntax_error(&mut self, message: M) -> Value - where - M: Into, - { - New::from(Call::new( - Identifier::from("SyntaxError"), - vec![Const::from(message.into()).into()], - )) - .run(self) - .expect_err("SyntaxError should always throw") - } - - /// Throws a `SyntaxError` with the specified message. - pub fn throw_syntax_error(&mut self, message: M) -> Result - where - M: Into, - { - Err(self.construct_syntax_error(message)) - } -} diff --git a/boa/src/exec/field/mod.rs b/boa/src/exec/field/mod.rs index 7b854a789bb..e883d47c75e 100644 --- a/boa/src/exec/field/mod.rs +++ b/boa/src/exec/field/mod.rs @@ -1,4 +1,4 @@ -use super::{Executable, Interpreter}; +use super::{Context, Executable}; use crate::{ builtins::value::{Type, Value}, syntax::ast::node::{GetConstField, GetField}, @@ -6,7 +6,7 @@ use crate::{ }; impl Executable for GetConstField { - fn run(&self, interpreter: &mut Interpreter) -> Result { + fn run(&self, interpreter: &mut Context) -> Result { let mut obj = self.obj().run(interpreter)?; if obj.get_type() != Type::Object { obj = obj.to_object(interpreter)?; @@ -17,7 +17,7 @@ impl Executable for GetConstField { } impl Executable for GetField { - fn run(&self, interpreter: &mut Interpreter) -> Result { + fn run(&self, interpreter: &mut Context) -> Result { let mut obj = self.obj().run(interpreter)?; if obj.get_type() != Type::Object { obj = obj.to_object(interpreter)?; diff --git a/boa/src/exec/identifier/mod.rs b/boa/src/exec/identifier/mod.rs index 0817642c791..28981c8f08b 100644 --- a/boa/src/exec/identifier/mod.rs +++ b/boa/src/exec/identifier/mod.rs @@ -1,8 +1,8 @@ -use super::{Executable, Interpreter}; +use super::{Context, Executable}; use crate::{builtins::value::Value, syntax::ast::node::identifier::Identifier, Result}; impl Executable for Identifier { - fn run(&self, interpreter: &mut Interpreter) -> Result { + fn run(&self, interpreter: &mut Context) -> Result { interpreter .realm() .environment diff --git a/boa/src/exec/iteration/mod.rs b/boa/src/exec/iteration/mod.rs index 5bd86ebfb84..49e23efbe94 100644 --- a/boa/src/exec/iteration/mod.rs +++ b/boa/src/exec/iteration/mod.rs @@ -1,19 +1,18 @@ //! Iteration node execution. -use super::{Executable, Interpreter, InterpreterState}; +use super::{Context, Executable, InterpreterState}; use crate::{ builtins::value::Value, environment::lexical_environment::new_declarative_environment, syntax::ast::node::{DoWhileLoop, ForLoop, WhileLoop}, BoaProfiler, Result, }; -use std::borrow::Borrow; #[cfg(test)] mod tests; impl Executable for ForLoop { - fn run(&self, interpreter: &mut Interpreter) -> Result { + fn run(&self, interpreter: &mut Context) -> Result { // Create the block environment. let _timer = BoaProfiler::global().start_event("ForLoop", "exec"); { @@ -35,17 +34,21 @@ impl Executable for ForLoop { { let result = self.body().run(interpreter)?; - match interpreter.get_current_state() { + match interpreter.executor().get_current_state() { InterpreterState::Break(_label) => { // TODO break to label. // Loops 'consume' breaks. - interpreter.set_current_state(InterpreterState::Executing); + interpreter + .executor() + .set_current_state(InterpreterState::Executing); break; } InterpreterState::Continue(_label) => { // TODO continue to label. - interpreter.set_current_state(InterpreterState::Executing); + interpreter + .executor() + .set_current_state(InterpreterState::Executing); // after breaking out of the block, continue execution of the loop } InterpreterState::Return => { @@ -69,21 +72,25 @@ impl Executable for ForLoop { } impl Executable for WhileLoop { - fn run(&self, interpreter: &mut Interpreter) -> Result { + fn run(&self, interpreter: &mut Context) -> Result { let mut result = Value::undefined(); - while self.cond().run(interpreter)?.borrow().to_boolean() { + while self.cond().run(interpreter)?.to_boolean() { result = self.expr().run(interpreter)?; - match interpreter.get_current_state() { + match interpreter.executor().get_current_state() { InterpreterState::Break(_label) => { // TODO break to label. // Loops 'consume' breaks. - interpreter.set_current_state(InterpreterState::Executing); + interpreter + .executor() + .set_current_state(InterpreterState::Executing); break; } InterpreterState::Continue(_label) => { // TODO continue to label. - interpreter.set_current_state(InterpreterState::Executing); + interpreter + .executor() + .set_current_state(InterpreterState::Executing); // after breaking out of the block, continue execution of the loop } InterpreterState::Return => { @@ -99,19 +106,23 @@ impl Executable for WhileLoop { } impl Executable for DoWhileLoop { - fn run(&self, interpreter: &mut Interpreter) -> Result { + fn run(&self, interpreter: &mut Context) -> Result { let mut result = self.body().run(interpreter)?; - match interpreter.get_current_state() { + match interpreter.executor().get_current_state() { InterpreterState::Break(_label) => { // TODO break to label. // Loops 'consume' breaks. - interpreter.set_current_state(InterpreterState::Executing); + interpreter + .executor() + .set_current_state(InterpreterState::Executing); return Ok(result); } InterpreterState::Continue(_label) => { // TODO continue to label; - interpreter.set_current_state(InterpreterState::Executing); + interpreter + .executor() + .set_current_state(InterpreterState::Executing); // after breaking out of the block, continue execution of the loop } InterpreterState::Return => { @@ -122,19 +133,23 @@ impl Executable for DoWhileLoop { } } - while self.cond().run(interpreter)?.borrow().to_boolean() { + while self.cond().run(interpreter)?.to_boolean() { result = self.body().run(interpreter)?; - match interpreter.get_current_state() { + match interpreter.executor().get_current_state() { InterpreterState::Break(_label) => { // TODO break to label. // Loops 'consume' breaks. - interpreter.set_current_state(InterpreterState::Executing); + interpreter + .executor() + .set_current_state(InterpreterState::Executing); break; } InterpreterState::Continue(_label) => { // TODO continue to label. - interpreter.set_current_state(InterpreterState::Executing); + interpreter + .executor() + .set_current_state(InterpreterState::Executing); // after breaking out of the block, continue execution of the loop } InterpreterState::Return => { diff --git a/boa/src/exec/mod.rs b/boa/src/exec/mod.rs index 735c58b1af2..5df73251e98 100644 --- a/boa/src/exec/mod.rs +++ b/boa/src/exec/mod.rs @@ -6,7 +6,6 @@ mod break_node; mod call; mod conditional; mod declaration; -mod exception; mod field; mod identifier; mod iteration; @@ -17,32 +16,22 @@ mod return_smt; mod spread; mod statement_list; mod switch; -#[cfg(test)] -mod tests; mod throw; mod try_node; +#[cfg(test)] +mod tests; + use crate::{ - builtins, - builtins::{ - function::{Function, FunctionFlags, NativeFunction}, - object::{Class, ClassBuilder, GcObject, Object, ObjectData, PROTOTYPE}, - property::{Property, PropertyKey}, - value::{PreferredType, RcString, RcSymbol, Type, Value}, - Console, Symbol, - }, - realm::Realm, - syntax::ast::{ - constant::Const, - node::{FormalParameter, Node, RcStatementList, StatementList}, - }, + builtins::value::Value, + context::Context, + syntax::ast::{constant::Const, node::Node}, BoaProfiler, Result, }; -use std::result::Result as StdResult; pub trait Executable { - /// Runs this executable in the given executor. - fn run(&self, interpreter: &mut Interpreter) -> Result; + /// Runs this executable in the given context. + fn run(&self, interpreter: &mut Context) -> Result; } #[derive(Debug, Eq, PartialEq)] @@ -58,269 +47,19 @@ pub(crate) enum InterpreterState { pub struct Interpreter { /// the current state of the interpreter. state: InterpreterState, +} - /// realm holds both the global object and the environment - pub realm: Realm, - - /// This is for generating an unique internal `Symbol` hash. - symbol_count: u32, - - /// console object state. - console: Console, +impl Default for Interpreter { + fn default() -> Self { + Self::new() + } } impl Interpreter { /// Creates a new interpreter. - pub fn new(realm: Realm) -> Self { - let mut interpreter = Self { + pub fn new() -> Self { + Self { state: InterpreterState::Executing, - realm, - symbol_count: 0, - console: Console::default(), - }; - - // Add new builtIns to Interpreter Realm - // At a later date this can be removed from here and called explicitly, but for now we almost always want these default builtins - interpreter.create_intrinsics(); - - interpreter - } - - /// Sets up the default global objects within Global - fn create_intrinsics(&mut self) { - let _timer = BoaProfiler::global().start_event("create_intrinsics", "interpreter"); - // Create intrinsics, add global objects here - builtins::init(self); - } - - /// Retrieves the `Realm` of this executor. - #[inline] - pub(crate) fn realm(&self) -> &Realm { - &self.realm - } - - /// Retrieves the `Realm` of this executor as a mutable reference. - #[inline] - pub(crate) fn realm_mut(&mut self) -> &mut Realm { - &mut self.realm - } - - /// Retrieves the global object of the `Realm` of this executor. - #[inline] - pub fn global(&self) -> &Value { - &self.realm.global_obj - } - - /// Generates a new `Symbol` internal hash. - /// - /// This currently is an incremented value. - #[inline] - pub(crate) fn generate_hash(&mut self) -> u32 { - let hash = self.symbol_count; - self.symbol_count += 1; - hash - } - - /// Utility to create a function Value for Function Declarations, Arrow Functions or Function Expressions - pub(crate) fn create_function( - &mut self, - params: P, - body: B, - flags: FunctionFlags, - ) -> Value - where - P: Into>, - B: Into, - { - let function_prototype = self.global().get_field("Function").get_field(PROTOTYPE); - - // Every new function has a prototype property pre-made - let proto = Value::new_object(Some(self.global())); - - let params = params.into(); - let params_len = params.len(); - let func = Function::Ordinary { - flags, - body: RcStatementList::from(body.into()), - params, - environment: self.realm.environment.get_current_environment().clone(), - }; - - let new_func = Object::function(func, function_prototype); - - let val = Value::from(new_func); - - // Set constructor field to the newly created Value (function object) - proto.set_field("constructor", val.clone()); - - val.set_field(PROTOTYPE, proto); - val.set_field("length", Value::from(params_len)); - - val - } - - /// Utility to create a function Value for Function Declarations, Arrow Functions or Function Expressions - pub fn create_builtin_function( - &mut self, - name: &str, - length: usize, - body: NativeFunction, - ) -> Result { - let function_prototype = self.global().get_field("Function").get_field(PROTOTYPE); - - // Every new function has a prototype property pre-made - let proto = Value::new_object(Some(self.global())); - let mut function = Object::function( - Function::BuiltIn(body.into(), FunctionFlags::CALLABLE), - function_prototype, - ); - function.set(PROTOTYPE.into(), proto); - function.set("length".into(), length.into()); - function.set("name".into(), name.into()); - - Ok(GcObject::new(function)) - } - - pub fn register_global_function( - &mut self, - name: &str, - length: usize, - body: NativeFunction, - ) -> Result<()> { - let function = self.create_builtin_function(name, length, body)?; - self.global().set_field(name, function); - Ok(()) - } - - /// - pub(crate) fn call(&mut self, f: &Value, this: &Value, args: &[Value]) -> Result { - match *f { - Value::Object(ref object) => object.call(this, args, self), - _ => self.throw_type_error("not a function"), - } - } - - /// Converts an array object into a rust vector of values. - /// - /// This is useful for the spread operator, for any other object an `Err` is returned - pub(crate) fn extract_array_properties(&mut self, value: &Value) -> StdResult, ()> { - if let Value::Object(ref x) = value { - // Check if object is array - if let ObjectData::Array = x.borrow().data { - let length = value.get_field("length").as_number().unwrap() as i32; - let values = (0..length) - .map(|idx| value.get_field(idx.to_string())) - .collect(); - return Ok(values); - } - // Check if object is a Map - else if let ObjectData::Map(ref map) = x.borrow().data { - let values = map - .iter() - .map(|(key, value)| { - // Construct a new array containing the key-value pair - let array = Value::new_object(Some( - &self - .realm() - .environment - .get_global_object() - .expect("Could not get global object"), - )); - array.set_data(ObjectData::Array); - array.as_object_mut().expect("object").set_prototype( - self.realm() - .environment - .get_binding_value("Array") - .expect("Array was not initialized") - .get_field(PROTOTYPE), - ); - array.set_field("0", key); - array.set_field("1", value); - array.set_field("length", Value::from(2)); - array - }) - .collect(); - return Ok(values); - } - - return Err(()); - } - - Err(()) - } - - /// Converts an object to a primitive. - /// - /// More information: - /// - [ECMAScript][spec] - /// - /// [spec]: https://tc39.es/ecma262/#sec-ordinarytoprimitive - pub(crate) fn ordinary_to_primitive( - &mut self, - o: &Value, - hint: PreferredType, - ) -> Result { - // 1. Assert: Type(O) is Object. - debug_assert!(o.get_type() == Type::Object); - // 2. Assert: Type(hint) is String and its value is either "string" or "number". - debug_assert!(hint == PreferredType::String || hint == PreferredType::Number); - - // 3. If hint is "string", then - // a. Let methodNames be « "toString", "valueOf" ». - // 4. Else, - // a. Let methodNames be « "valueOf", "toString" ». - let method_names = if hint == PreferredType::String { - ["toString", "valueOf"] - } else { - ["valueOf", "toString"] - }; - - // 5. For each name in methodNames in List order, do - for name in &method_names { - // a. Let method be ? Get(O, name). - let method: Value = o.get_field(*name); - // b. If IsCallable(method) is true, then - if method.is_function() { - // i. Let result be ? Call(method, O). - let result = self.call(&method, &o, &[])?; - // ii. If Type(result) is not Object, return result. - if !result.is_object() { - return Ok(result); - } - } - } - - // 6. Throw a TypeError exception. - self.throw_type_error("cannot convert object to primitive value") - } - - /// https://tc39.es/ecma262/#sec-hasproperty - pub(crate) fn has_property(&self, obj: &Value, key: &PropertyKey) -> bool { - if let Some(obj) = obj.as_object() { - obj.has_property(key) - } else { - false - } - } - - fn set_value(&mut self, node: &Node, value: Value) -> Result { - match node { - Node::Identifier(ref name) => { - self.realm - .environment - .set_mutable_binding(name.as_ref(), value.clone(), true); - Ok(value) - } - Node::GetConstField(ref get_const_field_node) => Ok(get_const_field_node - .obj() - .run(self)? - .set_field(get_const_field_node.field(), value)), - Node::GetField(ref get_field) => { - let field = get_field.field().run(self)?; - let key = field.to_property_key(self)?; - Ok(get_field.obj().run(self)?.set_field(key, value)) - } - _ => panic!("TypeError: invalid assignment to {}", node), } } @@ -333,62 +72,10 @@ impl Interpreter { pub(crate) fn get_current_state(&self) -> &InterpreterState { &self.state } - - /// A helper function for getting a immutable reference to the `console` object. - pub(crate) fn console(&self) -> &Console { - &self.console - } - - /// A helper function for getting a mutable reference to the `console` object. - pub(crate) fn console_mut(&mut self) -> &mut Console { - &mut self.console - } - - /// Construct a new `Symbol` with an optional description. - #[inline] - pub fn construct_symbol(&mut self, description: Option) -> RcSymbol { - RcSymbol::from(Symbol::new(self.generate_hash(), description)) - } - - /// Construct an empty object. - #[inline] - pub fn construct_object(&self) -> GcObject { - let object_prototype = self.global().get_field("Object").get_field(PROTOTYPE); - GcObject::new(Object::create(object_prototype)) - } - - /// Register a global class of type `T`, where `T` implemets `Class`. - /// - /// # Example - /// ```ignore - /// #[derive(Debug, Trace, Finalize)] - /// struct MyClass; - /// - /// impl Class for MyClass { - /// // ... - /// } - /// - /// context.register_global_class::(); - /// ``` - pub fn register_global_class(&mut self) -> Result<()> - where - T: Class, - { - let mut class_builder = ClassBuilder::new::(self); - T::init(&mut class_builder)?; - - let class = class_builder.build(); - let property = Property::data_descriptor(class.into(), T::ATTRIBUTE); - self.global() - .as_object_mut() - .unwrap() - .insert_property(T::NAME, property); - Ok(()) - } } impl Executable for Node { - fn run(&self, interpreter: &mut Interpreter) -> Result { + fn run(&self, interpreter: &mut Context) -> Result { let _timer = BoaProfiler::global().start_event("Executable", "exec"); match *self { Node::Const(Const::Null) => Ok(Value::null()), diff --git a/boa/src/exec/new/mod.rs b/boa/src/exec/new/mod.rs index eb038481576..3ccf9483125 100644 --- a/boa/src/exec/new/mod.rs +++ b/boa/src/exec/new/mod.rs @@ -1,8 +1,8 @@ -use super::{Executable, Interpreter}; +use super::{Context, Executable}; use crate::{builtins::Value, syntax::ast::node::New, BoaProfiler, Result}; impl Executable for New { - fn run(&self, interpreter: &mut Interpreter) -> Result { + fn run(&self, interpreter: &mut Context) -> Result { let _timer = BoaProfiler::global().start_event("New", "exec"); let func_object = self.expr().run(interpreter)?; diff --git a/boa/src/exec/object/mod.rs b/boa/src/exec/object/mod.rs index ee0649b8755..8ac33b9549b 100644 --- a/boa/src/exec/object/mod.rs +++ b/boa/src/exec/object/mod.rs @@ -1,6 +1,6 @@ //! Object execution. -use super::{Executable, Interpreter}; +use super::{Context, Executable}; use crate::{ builtins::value::Value, syntax::ast::node::MethodDefinitionKind, @@ -8,10 +8,8 @@ use crate::{ Result, }; -use std::borrow::Borrow; - impl Executable for Object { - fn run(&self, interpreter: &mut Interpreter) -> Result { + fn run(&self, interpreter: &mut Context) -> Result { let global_val = &interpreter .realm() .environment @@ -23,11 +21,11 @@ impl Executable for Object { for property in self.properties().iter() { match property { PropertyDefinition::Property(key, value) => { - obj.borrow().set_field(key.clone(), value.run(interpreter)?); + obj.set_field(key.clone(), value.run(interpreter)?); } PropertyDefinition::MethodDefinition(kind, name, func) => { if let MethodDefinitionKind::Ordinary = kind { - obj.borrow().set_field(name.clone(), func.run(interpreter)?); + obj.set_field(name.clone(), func.run(interpreter)?); } else { // TODO: Implement other types of MethodDefinitionKinds. unimplemented!("other types of property method definitions."); diff --git a/boa/src/exec/operator/mod.rs b/boa/src/exec/operator/mod.rs index 1a33b69b995..2bfd9934a6e 100644 --- a/boa/src/exec/operator/mod.rs +++ b/boa/src/exec/operator/mod.rs @@ -2,7 +2,7 @@ #[cfg(test)] mod tests; -use super::{Executable, Interpreter}; +use super::{Context, Executable}; use crate::{ builtins::value::Value, environment::lexical_environment::VariableScope, @@ -14,7 +14,7 @@ use crate::{ }; impl Executable for Assign { - fn run(&self, interpreter: &mut Interpreter) -> Result { + fn run(&self, interpreter: &mut Context) -> Result { let _timer = BoaProfiler::global().start_event("Assign", "exec"); let val = self.rhs().run(interpreter)?; match self.lhs() { @@ -50,7 +50,7 @@ impl Executable for Assign { } impl Executable for BinOp { - fn run(&self, interpreter: &mut Interpreter) -> Result { + fn run(&self, interpreter: &mut Context) -> Result { match self.op() { op::BinOp::Num(op) => { let x = self.lhs().run(interpreter)?; @@ -134,7 +134,7 @@ impl Executable for BinOp { .ok_or_else(|| interpreter.construct_reference_error(name.as_ref()))?; let v_b = self.rhs().run(interpreter)?; let value = Self::run_assign(op, v_a, v_b, interpreter)?; - interpreter.realm.environment.set_mutable_binding( + interpreter.realm_mut().environment.set_mutable_binding( name.as_ref(), value.clone(), true, @@ -161,12 +161,7 @@ impl Executable for BinOp { impl BinOp { /// Runs the assignment operators. - fn run_assign( - op: AssignOp, - x: Value, - y: Value, - interpreter: &mut Interpreter, - ) -> Result { + fn run_assign(op: AssignOp, x: Value, y: Value, interpreter: &mut Context) -> Result { match op { AssignOp::Add => x.add(&y, interpreter), AssignOp::Sub => x.sub(&y, interpreter), @@ -184,7 +179,7 @@ impl BinOp { } impl Executable for UnaryOp { - fn run(&self, interpreter: &mut Interpreter) -> Result { + fn run(&self, interpreter: &mut Context) -> Result { let x = self.target().run(interpreter)?; Ok(match self.op() { diff --git a/boa/src/exec/return_smt/mod.rs b/boa/src/exec/return_smt/mod.rs index e77a072dd34..6bb944d3fd9 100644 --- a/boa/src/exec/return_smt/mod.rs +++ b/boa/src/exec/return_smt/mod.rs @@ -1,14 +1,16 @@ -use super::{Executable, Interpreter, InterpreterState}; +use super::{Context, Executable, InterpreterState}; use crate::{builtins::value::Value, syntax::ast::node::Return, Result}; impl Executable for Return { - fn run(&self, interpreter: &mut Interpreter) -> Result { + fn run(&self, interpreter: &mut Context) -> Result { let result = match self.expr() { Some(ref v) => v.run(interpreter), None => Ok(Value::undefined()), }; // Set flag for return - interpreter.set_current_state(InterpreterState::Return); + interpreter + .executor() + .set_current_state(InterpreterState::Return); result } } diff --git a/boa/src/exec/spread/mod.rs b/boa/src/exec/spread/mod.rs index 20e0f9393c0..fb644020d32 100644 --- a/boa/src/exec/spread/mod.rs +++ b/boa/src/exec/spread/mod.rs @@ -1,8 +1,8 @@ -use super::{Executable, Interpreter}; +use super::{Context, Executable}; use crate::{builtins::value::Value, syntax::ast::node::Spread, Result}; impl Executable for Spread { - fn run(&self, interpreter: &mut Interpreter) -> Result { + fn run(&self, interpreter: &mut Context) -> Result { // TODO: for now we can do nothing but return the value as-is self.val().run(interpreter) } diff --git a/boa/src/exec/statement_list.rs b/boa/src/exec/statement_list.rs index df42d6e71fd..c04cc6e3ccc 100644 --- a/boa/src/exec/statement_list.rs +++ b/boa/src/exec/statement_list.rs @@ -1,19 +1,21 @@ //! Statement list execution. -use super::{Executable, Interpreter, InterpreterState}; +use super::{Context, Executable, InterpreterState}; use crate::{builtins::value::Value, syntax::ast::node::StatementList, BoaProfiler, Result}; impl Executable for StatementList { - fn run(&self, interpreter: &mut Interpreter) -> Result { + fn run(&self, interpreter: &mut Context) -> Result { let _timer = BoaProfiler::global().start_event("StatementList", "exec"); // https://tc39.es/ecma262/#sec-block-runtime-semantics-evaluation // The return value is uninitialized, which means it defaults to Value::Undefined let mut obj = Value::default(); - interpreter.set_current_state(InterpreterState::Executing); + interpreter + .executor() + .set_current_state(InterpreterState::Executing); for (i, item) in self.statements().iter().enumerate() { let val = item.run(interpreter)?; - match interpreter.get_current_state() { + match interpreter.executor().get_current_state() { InterpreterState::Return => { // Early return. obj = val; diff --git a/boa/src/exec/switch/mod.rs b/boa/src/exec/switch/mod.rs index 7c5a66f358b..15018dcdbf0 100644 --- a/boa/src/exec/switch/mod.rs +++ b/boa/src/exec/switch/mod.rs @@ -1,15 +1,17 @@ -use super::{Executable, Interpreter, InterpreterState}; +use super::{Context, Executable, InterpreterState}; use crate::{builtins::value::Value, syntax::ast::node::Switch, Result}; #[cfg(test)] mod tests; impl Executable for Switch { - fn run(&self, interpreter: &mut Interpreter) -> Result { + fn run(&self, interpreter: &mut Context) -> Result { let val = self.val().run(interpreter)?; let mut result = Value::null(); let mut matched = false; - interpreter.set_current_state(InterpreterState::Executing); + interpreter + .executor() + .set_current_state(InterpreterState::Executing); // If a case block does not end with a break statement then subsequent cases will be run without // checking their conditions until a break is encountered. @@ -21,7 +23,7 @@ impl Executable for Switch { if fall_through || val.strict_equals(&cond.run(interpreter)?) { matched = true; let result = block.run(interpreter)?; - match interpreter.get_current_state() { + match interpreter.executor().get_current_state() { InterpreterState::Return => { // Early return. return Ok(result); @@ -29,7 +31,9 @@ impl Executable for Switch { InterpreterState::Break(_label) => { // TODO, break to a label. // Break statement encountered so therefore end switch statement. - interpreter.set_current_state(InterpreterState::Executing); + interpreter + .executor() + .set_current_state(InterpreterState::Executing); break; } InterpreterState::Continue(_label) => { @@ -46,10 +50,12 @@ impl Executable for Switch { if !matched { if let Some(default) = self.default() { - interpreter.set_current_state(InterpreterState::Executing); + interpreter + .executor() + .set_current_state(InterpreterState::Executing); for (i, item) in default.iter().enumerate() { let val = item.run(interpreter)?; - match interpreter.get_current_state() { + match interpreter.executor().get_current_state() { InterpreterState::Return => { // Early return. result = val; diff --git a/boa/src/exec/tests.rs b/boa/src/exec/tests.rs index d1b3feada82..d94243af3fa 100644 --- a/boa/src/exec/tests.rs +++ b/boa/src/exec/tests.rs @@ -1,9 +1,7 @@ use crate::{ builtins::{Number, Value}, - exec, - exec::Interpreter, - forward, forward_val, - realm::Realm, + context::Context, + exec, forward, forward_val, }; #[test] @@ -115,8 +113,7 @@ fn object_field_set() { #[test] fn spread_with_arguments() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let scenario = r#" const a = [1, "test", 3, 4]; @@ -142,8 +139,7 @@ fn spread_with_arguments() { #[test] fn array_rest_with_arguments() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let scenario = r#" var b = [4, 5, 6] @@ -721,8 +717,7 @@ mod in_operator { #[test] fn should_type_error_when_rhs_not_object() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let scenario = r#" var x = false; @@ -739,8 +734,7 @@ mod in_operator { #[test] fn should_set_this_value() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let scenario = r#" function Foo() { @@ -758,8 +752,7 @@ mod in_operator { #[test] fn new_instance_should_point_to_prototype() { // A new instance should point to a prototype object created with the constructor function - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let scenario = r#" function Foo() {} @@ -886,8 +879,7 @@ fn function_decl_hoisting() { #[test] fn to_bigint() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert!(Value::null().to_bigint(&mut engine).is_err()); assert!(Value::undefined().to_bigint(&mut engine).is_err()); @@ -898,8 +890,7 @@ fn to_bigint() { #[test] fn to_index() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(Value::undefined().to_index(&mut engine).unwrap(), 0); assert!(Value::integer(-1).to_index(&mut engine).is_err()); @@ -907,8 +898,7 @@ fn to_index() { #[test] fn to_integer() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert!(Number::equal( Value::number(f64::NAN).to_integer(&mut engine).unwrap(), @@ -945,8 +935,7 @@ fn to_integer() { #[test] fn to_length() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(Value::number(f64::NAN).to_length(&mut engine).unwrap(), 0); assert_eq!( @@ -977,8 +966,7 @@ fn to_length() { #[test] fn to_int32() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); macro_rules! check_to_int32 { ($from:expr => $to:expr) => { @@ -1091,8 +1079,7 @@ fn to_int32() { #[test] fn to_string() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert_eq!(Value::null().to_string(&mut engine).unwrap(), "null"); assert_eq!( @@ -1109,8 +1096,7 @@ fn to_string() { #[test] fn calling_function_with_unspecified_arguments() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let scenario = r#" function test(a, b) { return b; @@ -1124,8 +1110,7 @@ fn calling_function_with_unspecified_arguments() { #[test] fn to_object() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert!(Value::undefined() .to_object(&mut engine) @@ -1139,8 +1124,7 @@ fn to_object() { #[test] fn check_this_binding_in_object_literal() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" var foo = { a: 3, @@ -1155,8 +1139,7 @@ fn check_this_binding_in_object_literal() { #[test] fn array_creation_benchmark() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" (function(){ let testArr = []; @@ -1173,8 +1156,7 @@ fn array_creation_benchmark() { #[test] fn array_pop_benchmark() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" (function(){ let testArray = [83, 93, 27, 29, 2828, 234, 23, 56, 32, 56, 67, 77, 32, @@ -1207,8 +1189,7 @@ fn array_pop_benchmark() { #[test] fn number_object_access_benchmark() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" new Number( new Number( @@ -1224,8 +1205,7 @@ fn number_object_access_benchmark() { #[test] fn not_a_function() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let init = r#" let a = {}; let b = true; @@ -1289,8 +1269,7 @@ fn comma_operator() { fn assignment_to_non_assignable() { // Relates to the behaviour described at // https://tc39.es/ecma262/#sec-assignment-operators-static-semantics-early-errors - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); // Tests all assignment operators as per [spec] and [mdn] // @@ -1312,8 +1291,7 @@ fn assignment_to_non_assignable() { fn multicharacter_assignment_to_non_assignable() { // Relates to the behaviour described at // https://tc39.es/ecma262/#sec-assignment-operators-static-semantics-early-errors - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); let test_cases = ["3 **= 5", "3 <<= 5", "3 >>= 5"]; @@ -1328,8 +1306,7 @@ fn multicharacter_assignment_to_non_assignable() { #[test] #[ignore] fn multicharacter_bitwise_assignment_to_non_assignable() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); // Disabled - awaiting implementation. let test_cases = ["3 >>>= 5", "3 &&= 5", "3 ||= 5", "3 ??= 5"]; @@ -1344,8 +1321,7 @@ fn multicharacter_bitwise_assignment_to_non_assignable() { #[test] fn assign_to_array_decl() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); assert!(forward(&mut engine, "[1] = [2]").starts_with("Syntax Error: ")); assert!(forward(&mut engine, "[3, 5] = [7, 8]").starts_with("Syntax Error: ")); @@ -1355,8 +1331,7 @@ fn assign_to_array_decl() { #[test] fn assign_to_object_decl() { - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); const ERR_MSG: &str = "expected token \';\', got \':\' in expression statement at line 1, col 3"; diff --git a/boa/src/exec/throw/mod.rs b/boa/src/exec/throw/mod.rs index ae66c9560d0..85a4dbda574 100644 --- a/boa/src/exec/throw/mod.rs +++ b/boa/src/exec/throw/mod.rs @@ -1,9 +1,9 @@ -use super::{Executable, Interpreter}; +use super::{Context, Executable}; use crate::{builtins::value::Value, syntax::ast::node::Throw, Result}; impl Executable for Throw { #[inline] - fn run(&self, interpreter: &mut Interpreter) -> Result { + fn run(&self, interpreter: &mut Context) -> Result { Err(self.expr().run(interpreter)?) } } diff --git a/boa/src/exec/try_node/mod.rs b/boa/src/exec/try_node/mod.rs index fb909e2e39e..e9352b748b0 100644 --- a/boa/src/exec/try_node/mod.rs +++ b/boa/src/exec/try_node/mod.rs @@ -1,6 +1,6 @@ //! Try..catch node execution. -use super::{Executable, Interpreter}; +use super::{Context, Executable}; use crate::{ builtins::value::Value, environment::lexical_environment::{new_declarative_environment, VariableScope}, @@ -12,7 +12,7 @@ use crate::{ mod tests; impl Executable for Try { - fn run(&self, interpreter: &mut Interpreter) -> Result { + fn run(&self, interpreter: &mut Context) -> Result { let _timer = BoaProfiler::global().start_event("Try", "exec"); let res = self.block().run(interpreter).map_or_else( |err| { diff --git a/boa/src/lib.rs b/boa/src/lib.rs index 209d88725d2..ada0b7d1faf 100644 --- a/boa/src/lib.rs +++ b/boa/src/lib.rs @@ -33,6 +33,11 @@ clippy::let_unit_value, missing_doc_code_examples )] +// Remove me when we remove `forward_val`, `froward` and `exec` +// +// This is added because it generates to many warnings, +// beause these functions are used in almost every test. +#![allow(deprecated)] pub mod builtins; pub mod environment; @@ -41,20 +46,20 @@ pub mod profiler; pub mod realm; pub mod syntax; -use crate::{builtins::value::Value, syntax::ast::node::StatementList}; -pub use crate::{ - exec::{Executable, Interpreter}, - profiler::BoaProfiler, - realm::Realm, - syntax::{ - lexer::Lexer, - parser::{ParseError, Parser}, - }, -}; +mod context; + use std::result::Result as StdResult; +pub(crate) use crate::{ + exec::Executable, + profiler::BoaProfiler, + syntax::{ast::node::StatementList, Parser}, +}; pub use gc::{custom_trace, unsafe_empty_trace, Finalize, Trace}; +// Export things to root level +pub use crate::{builtins::value::Value, context::Context}; + /// The result of a Javascript expression is represented like this so it can succeed (`Ok`) or fail (`Err`) #[must_use] pub type Result = StdResult; @@ -65,9 +70,10 @@ fn parser_expr(src: &str) -> StdResult { .map_err(|e| e.to_string()) } -/// Execute the code using an existing Interpreter -/// The str is consumed and the state of the Interpreter is changed -pub fn forward(engine: &mut Interpreter, src: &str) -> String { +/// Execute the code using an existing Context +/// The str is consumed and the state of the Context is changed +#[deprecated(note = "Please use Context::eval() instead")] +pub fn forward(engine: &mut Context, src: &str) -> String { // Setup executor let expr = match parser_expr(src) { Ok(res) => res, @@ -79,12 +85,13 @@ pub fn forward(engine: &mut Interpreter, src: &str) -> String { ) } -/// Execute the code using an existing Interpreter. -/// The str is consumed and the state of the Interpreter is changed +/// Execute the code using an existing Context. +/// The str is consumed and the state of the Context is changed /// Similar to `forward`, except the current value is returned instad of the string /// If the interpreter fails parsing an error value is returned instead (error object) #[allow(clippy::unit_arg, clippy::drop_copy)] -pub fn forward_val(engine: &mut Interpreter, src: &str) -> Result { +#[deprecated(note = "Please use Context::eval() instead")] +pub fn forward_val(engine: &mut Context, src: &str) -> Result { let main_timer = BoaProfiler::global().start_event("Main", "Main"); // Setup executor let result = match parser_expr(src) { @@ -102,10 +109,11 @@ pub fn forward_val(engine: &mut Interpreter, src: &str) -> Result { result } -/// Create a clean Interpreter and execute the code +/// Create a clean Context and execute the code +#[deprecated(note = "Please use Context::eval() instead")] pub fn exec(src: &str) -> String { - // Create new Realm - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); - forward(&mut engine, src) + match Context::new().eval(src) { + Ok(value) => value.display().to_string(), + Err(error) => error.display().to_string(), + } } diff --git a/boa/src/syntax/mod.rs b/boa/src/syntax/mod.rs index 6605bf23af7..802f7a1d195 100644 --- a/boa/src/syntax/mod.rs +++ b/boa/src/syntax/mod.rs @@ -3,3 +3,6 @@ pub mod ast; pub mod lexer; pub mod parser; + +pub use lexer::Lexer; +pub use parser::Parser; diff --git a/boa_cli/src/main.rs b/boa_cli/src/main.rs index 03b6efcec58..ce69719f1e9 100644 --- a/boa_cli/src/main.rs +++ b/boa_cli/src/main.rs @@ -25,7 +25,7 @@ clippy::as_conversions )] -use boa::{exec::Interpreter, forward_val, realm::Realm, syntax::ast::node::StatementList}; +use boa::{syntax::ast::node::StatementList, Context}; use colored::*; use rustyline::{config::Config, error::ReadlineError, EditMode, Editor}; use std::{fs::read_to_string, path::PathBuf}; @@ -139,9 +139,7 @@ fn dump(src: &str, args: &Opt) -> Result<(), String> { pub fn main() -> Result<(), std::io::Error> { let args = Opt::from_args(); - let realm = Realm::create(); - - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); for file in &args.files { let buffer = read_to_string(file)?; @@ -151,7 +149,7 @@ pub fn main() -> Result<(), std::io::Error> { eprintln!("{}", e); } } else { - match forward_val(&mut engine, &buffer) { + match engine.eval(&buffer) { Ok(v) => print!("{}", v.display()), Err(v) => eprint!("{}", v.display()), } @@ -187,7 +185,7 @@ pub fn main() -> Result<(), std::io::Error> { eprintln!("{}", e); } } else { - match forward_val(&mut engine, line.trim_end()) { + match engine.eval(line.trim_end()) { Ok(v) => println!("{}", v.display()), Err(v) => { eprintln!("{}: {}", "Uncaught".red(), v.display().to_string().red()) diff --git a/boa_wasm/src/lib.rs b/boa_wasm/src/lib.rs index 158892831dd..a454dd5ed2e 100644 --- a/boa_wasm/src/lib.rs +++ b/boa_wasm/src/lib.rs @@ -1,4 +1,4 @@ -use boa::{Executable, Interpreter, Parser, Realm}; +use boa::{exec::Executable, syntax::Parser, Context}; use wasm_bindgen::prelude::*; #[wasm_bindgen] @@ -8,8 +8,7 @@ pub fn evaluate(src: &str) -> Result { .map_err(|e| JsValue::from(format!("Parsing Error: {}", e)))?; // Setup executor - let realm = Realm::create(); - let mut engine = Interpreter::new(realm); + let mut engine = Context::new(); // Setup executor expr.run(&mut engine) From 98b6d520b99a867edf382534f9a290079087b027 Mon Sep 17 00:00:00 2001 From: HalidOdat Date: Wed, 2 Sep 2020 19:31:19 +0200 Subject: [PATCH 2/4] fix benchmarks --- boa/benches/exec.rs | 7 +------ boa/src/context.rs | 6 +++--- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/boa/benches/exec.rs b/boa/benches/exec.rs index 1397e0304be..3730634ac65 100644 --- a/boa/benches/exec.rs +++ b/boa/benches/exec.rs @@ -1,11 +1,6 @@ //! Benchmarks of the whole execution engine in Boa. -use boa::{ - context::Context, - exec::Executable, - realm::Realm, - syntax::{Lexer, Parser}, -}; +use boa::{exec::Executable, realm::Realm, syntax::Parser, Context}; use criterion::{black_box, criterion_group, criterion_main, Criterion}; #[cfg(all(target_arch = "x86_64", target_os = "linux", target_env = "gnu"))] diff --git a/boa/src/context.rs b/boa/src/context.rs index d42e9b3e5aa..6326b5a526d 100644 --- a/boa/src/context.rs +++ b/boa/src/context.rs @@ -42,7 +42,7 @@ pub struct Context { /// Symbol hash. /// - /// For now this is an incremental u32 number. + /// For now this is an incremented u32 number. symbol_count: u32, /// console object state. @@ -138,8 +138,8 @@ impl Context { } } - /// Retrun the global object. - pub(crate) fn global_object(&self) -> &Value { + /// Return the global object. + pub fn global_object(&self) -> &Value { &self.realm.global_obj } From 7bf48784c96eaddf0efc0f2bce61d902dbe08734 Mon Sep 17 00:00:00 2001 From: HalidOdat Date: Wed, 2 Sep 2020 19:39:46 +0200 Subject: [PATCH 3/4] allow deprecated only for tests --- boa/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boa/src/lib.rs b/boa/src/lib.rs index ada0b7d1faf..134f2bd3428 100644 --- a/boa/src/lib.rs +++ b/boa/src/lib.rs @@ -37,7 +37,7 @@ // // This is added because it generates to many warnings, // beause these functions are used in almost every test. -#![allow(deprecated)] +#![cfg_attr(any(test), allow(deprecated))] pub mod builtins; pub mod environment; From 3c89202df46dd1e3500dcddae382bccbaac4a4e0 Mon Sep 17 00:00:00 2001 From: HalidOdat Date: Wed, 2 Sep 2020 19:56:59 +0200 Subject: [PATCH 4/4] Fix use of deprecated functions in benchmarks --- boa/benches/full.rs | 48 +++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/boa/benches/full.rs b/boa/benches/full.rs index d979c5ea68f..a46682db38c 100644 --- a/boa/benches/full.rs +++ b/boa/benches/full.rs @@ -1,6 +1,6 @@ //! Benchmarks of whole program execution in Boa. -use boa::exec; +use boa::Context; use criterion::{black_box, criterion_group, criterion_main, Criterion}; #[cfg(all(target_arch = "x86_64", target_os = "linux", target_env = "gnu"))] @@ -15,7 +15,7 @@ static SYMBOL_CREATION: &str = include_str!("bench_scripts/symbol_creation.js"); fn symbol_creation(c: &mut Criterion) { // Execute the code by taking into account realm creation, lexing and parsing c.bench_function("Symbols (Full)", move |b| { - b.iter(|| exec(black_box(SYMBOL_CREATION))) + b.iter(|| Context::new().eval(black_box(SYMBOL_CREATION))) }); } @@ -24,7 +24,7 @@ static FOR_LOOP: &str = include_str!("bench_scripts/for_loop.js"); fn for_loop(c: &mut Criterion) { // Execute the code by taking into account realm creation, lexing and parsing c.bench_function("For loop (Full)", move |b| { - b.iter(|| exec(black_box(FOR_LOOP))) + b.iter(|| Context::new().eval(black_box(FOR_LOOP))) }); } @@ -33,7 +33,7 @@ static FIBONACCI: &str = include_str!("bench_scripts/fibonacci.js"); fn fibonacci(c: &mut Criterion) { // Execute the code by taking into account realm creation, lexing and parsing c.bench_function("Fibonacci (Full)", move |b| { - b.iter(|| exec(black_box(FIBONACCI))) + b.iter(|| Context::new().eval(black_box(FIBONACCI))) }); } @@ -42,7 +42,7 @@ static OBJECT_CREATION: &str = include_str!("bench_scripts/object_creation.js"); fn object_creation(c: &mut Criterion) { // Execute the code by taking into account realm creation, lexing and parsing c.bench_function("Object Creation (Full)", move |b| { - b.iter(|| exec(black_box(OBJECT_CREATION))) + b.iter(|| Context::new().eval(black_box(OBJECT_CREATION))) }); } @@ -51,7 +51,7 @@ static OBJECT_PROP_ACCESS_CONST: &str = include_str!("bench_scripts/object_prop_ fn object_prop_access_const(c: &mut Criterion) { // Execute the code by taking into account realm creation, lexing and parsing c.bench_function("Static Object Property Access (Full)", move |b| { - b.iter(|| exec(black_box(OBJECT_PROP_ACCESS_CONST))) + b.iter(|| Context::new().eval(black_box(OBJECT_PROP_ACCESS_CONST))) }); } @@ -60,7 +60,7 @@ static OBJECT_PROP_ACCESS_DYN: &str = include_str!("bench_scripts/object_prop_ac fn object_prop_access_dyn(c: &mut Criterion) { // Execute the code by taking into account realm creation, lexing and parsing c.bench_function("Dynamic Object Property Access (Full)", move |b| { - b.iter(|| exec(black_box(OBJECT_PROP_ACCESS_DYN))) + b.iter(|| Context::new().eval(black_box(OBJECT_PROP_ACCESS_DYN))) }); } @@ -69,7 +69,7 @@ static REGEXP_LITERAL_CREATION: &str = include_str!("bench_scripts/regexp_litera fn regexp_literal_creation(c: &mut Criterion) { // Execute the code by taking into account realm creation, lexing and parsing c.bench_function("RegExp Literal Creation (Full)", move |b| { - b.iter(|| exec(black_box(REGEXP_LITERAL_CREATION))) + b.iter(|| Context::new().eval(black_box(REGEXP_LITERAL_CREATION))) }); } @@ -78,7 +78,7 @@ static REGEXP_CREATION: &str = include_str!("bench_scripts/regexp_creation.js"); fn regexp_creation(c: &mut Criterion) { // Execute the code by taking into account realm creation, lexing and parsing c.bench_function("RegExp (Full)", move |b| { - b.iter(|| exec(black_box(REGEXP_CREATION))) + b.iter(|| Context::new().eval(black_box(REGEXP_CREATION))) }); } @@ -87,7 +87,7 @@ static REGEXP_LITERAL: &str = include_str!("bench_scripts/regexp_literal.js"); fn regexp_literal(c: &mut Criterion) { // Execute the code by taking into account realm creation, lexing and parsing c.bench_function("RegExp Literal (Full)", move |b| { - b.iter(|| exec(black_box(REGEXP_LITERAL))) + b.iter(|| Context::new().eval(black_box(REGEXP_LITERAL))) }); } @@ -95,14 +95,16 @@ static REGEXP: &str = include_str!("bench_scripts/regexp.js"); fn regexp(c: &mut Criterion) { // Execute the code by taking into account realm creation, lexing and parsing - c.bench_function("RegExp (Full)", move |b| b.iter(|| exec(black_box(REGEXP)))); + c.bench_function("RegExp (Full)", move |b| { + b.iter(|| Context::new().eval(black_box(REGEXP))) + }); } static ARRAY_ACCESS: &str = include_str!("bench_scripts/array_access.js"); fn array_access(c: &mut Criterion) { c.bench_function("Array access (Full)", move |b| { - b.iter(|| exec(black_box(ARRAY_ACCESS))) + b.iter(|| Context::new().eval(black_box(ARRAY_ACCESS))) }); } @@ -110,7 +112,7 @@ static ARRAY_CREATE: &str = include_str!("bench_scripts/array_create.js"); fn array_creation(c: &mut Criterion) { c.bench_function("Array creation (Full)", move |b| { - b.iter(|| exec(black_box(ARRAY_CREATE))) + b.iter(|| Context::new().eval(black_box(ARRAY_CREATE))) }); } @@ -118,7 +120,7 @@ static ARRAY_POP: &str = include_str!("bench_scripts/array_pop.js"); fn array_pop(c: &mut Criterion) { c.bench_function("Array pop (Full)", move |b| { - b.iter(|| exec(black_box(ARRAY_POP))) + b.iter(|| Context::new().eval(black_box(ARRAY_POP))) }); } @@ -126,7 +128,7 @@ static STRING_CONCAT: &str = include_str!("bench_scripts/string_concat.js"); fn string_concat(c: &mut Criterion) { c.bench_function("String concatenation (Full)", move |b| { - b.iter(|| exec(black_box(STRING_CONCAT))) + b.iter(|| Context::new().eval(black_box(STRING_CONCAT))) }); } @@ -134,7 +136,7 @@ static STRING_COMPARE: &str = include_str!("bench_scripts/string_compare.js"); fn string_compare(c: &mut Criterion) { c.bench_function("String comparison (Full)", move |b| { - b.iter(|| exec(black_box(STRING_COMPARE))) + b.iter(|| Context::new().eval(black_box(STRING_COMPARE))) }); } @@ -142,7 +144,7 @@ static STRING_COPY: &str = include_str!("bench_scripts/string_copy.js"); fn string_copy(c: &mut Criterion) { c.bench_function("String copy (Full)", move |b| { - b.iter(|| exec(black_box(STRING_COPY))) + b.iter(|| Context::new().eval(black_box(STRING_COPY))) }); } @@ -150,7 +152,7 @@ static NUMBER_OBJECT_ACCESS: &str = include_str!("bench_scripts/number_object_ac fn number_object_access(c: &mut Criterion) { c.bench_function("Number Object Access (Full)", move |b| { - b.iter(|| exec(black_box(NUMBER_OBJECT_ACCESS))) + b.iter(|| Context::new().eval(black_box(NUMBER_OBJECT_ACCESS))) }); } @@ -158,7 +160,7 @@ static BOOLEAN_OBJECT_ACCESS: &str = include_str!("bench_scripts/boolean_object_ fn boolean_object_access(c: &mut Criterion) { c.bench_function("Boolean Object Access (Full)", move |b| { - b.iter(|| exec(black_box(BOOLEAN_OBJECT_ACCESS))) + b.iter(|| Context::new().eval(black_box(BOOLEAN_OBJECT_ACCESS))) }); } @@ -166,7 +168,7 @@ static STRING_OBJECT_ACCESS: &str = include_str!("bench_scripts/string_object_ac fn string_object_access(c: &mut Criterion) { c.bench_function("String Object Access (Full)", move |b| { - b.iter(|| exec(black_box(STRING_OBJECT_ACCESS))) + b.iter(|| Context::new().eval(black_box(STRING_OBJECT_ACCESS))) }); } @@ -174,7 +176,7 @@ static ARITHMETIC_OPERATIONS: &str = include_str!("bench_scripts/arithmetic_oper fn arithmetic_operations(c: &mut Criterion) { c.bench_function("Arithmetic operations (Full)", move |b| { - b.iter(|| exec(black_box(ARITHMETIC_OPERATIONS))) + b.iter(|| Context::new().eval(black_box(ARITHMETIC_OPERATIONS))) }); } @@ -182,7 +184,7 @@ static CLEAN_JS: &str = include_str!("bench_scripts/clean_js.js"); fn clean_js(c: &mut Criterion) { c.bench_function("Clean js (Full)", move |b| { - b.iter(|| exec(black_box(CLEAN_JS))) + b.iter(|| Context::new().eval(black_box(CLEAN_JS))) }); } @@ -190,7 +192,7 @@ static MINI_JS: &str = include_str!("bench_scripts/mini_js.js"); fn mini_js(c: &mut Criterion) { c.bench_function("Mini js (Full)", move |b| { - b.iter(|| exec(black_box(MINI_JS))) + b.iter(|| Context::new().eval(black_box(MINI_JS))) }); }