Skip to content

Commit

Permalink
Context feature
Browse files Browse the repository at this point in the history
 - 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()
  • Loading branch information
HalidOdat committed Sep 2, 2020
1 parent 460b4a2 commit 7e1bb78
Show file tree
Hide file tree
Showing 73 changed files with 1,428 additions and 1,835 deletions.
73 changes: 28 additions & 45 deletions boa/benches/exec.rs
Original file line number Diff line number Diff line change
@@ -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"))]
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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()
Expand 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()
Expand 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()
Expand 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()
Expand 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())
Expand All @@ -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())
Expand Down
22 changes: 9 additions & 13 deletions boa/examples/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ use boa::{
property::Attribute,
value::Value,
},
exec::Interpreter,
forward_val,
realm::Realm,
Result,
Context, Result,
};

use gc::{Finalize, Trace};
Expand All @@ -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<Value> {
fn say_hello(this: &Value, _: &[Value], ctx: &mut Context) -> Result<Value> {
// We check if this is an object.
if let Some(object) = this.as_object() {
// If it is we downcast the type to type `Person`.
Expand Down Expand Up @@ -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<Self> {
fn constructor(_this: &Value, args: &[Value], ctx: &mut Context) -> Result<Self> {
// we get the first arguemnt of undefined if the first one is unavalable and call `to_string`.
//
// This is equivalent to `String(arg)`.
Expand Down Expand Up @@ -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::<Person>().unwrap();

forward_val(
&mut context,
r"
context
.eval(
r"
let person = new Person('John', 19);
person.sayHello();
Expand All @@ -145,6 +141,6 @@ fn main() {
console.log(person.inheritedProperty);
console.log(Person.prototype.inheritedProperty === person.inheritedProperty);
",
)
.unwrap();
)
.unwrap();
}
Loading

0 comments on commit 7e1bb78

Please sign in to comment.