mirror of https://github.com/boa-dev/boa.git
Browse Source
* replacing ExecutorBuilder with Realm * fixing global_env * adding benchmark for realm * instrinsics was being called twice * update changelogpull/125/head
Jason Williams
5 years ago
committed by
GitHub
12 changed files with 223 additions and 103 deletions
@ -0,0 +1,12 @@
|
||||
#[macro_use] |
||||
extern crate criterion; |
||||
|
||||
use boa::realm::Realm; |
||||
use criterion::Criterion; |
||||
|
||||
fn create_realm(c: &mut Criterion) { |
||||
c.bench_function("Create Realm", move |b| b.iter(|| Realm::create())); |
||||
} |
||||
|
||||
criterion_group!(benches, create_realm); |
||||
criterion_main!(benches); |
@ -0,0 +1,95 @@
|
||||
//! Conceptually, a realm consists of a set of intrinsic objects, an ECMAScript global environment,
|
||||
//! all of the ECMAScript code that is loaded within the scope of that global environment,
|
||||
//! and other associated state and resources.
|
||||
//!
|
||||
//!A realm is represented in this implementation as a Realm struct with the fields specified from the spec
|
||||
use crate::{ |
||||
environment::{ |
||||
declarative_environment_record::DeclarativeEnvironmentRecord, |
||||
global_environment_record::GlobalEnvironmentRecord, |
||||
lexical_environment::LexicalEnvironment, |
||||
object_environment_record::ObjectEnvironmentRecord, |
||||
}, |
||||
js::{ |
||||
array, boolean, console, function, json, math, object, regexp, string, |
||||
value::{Value, ValueData}, |
||||
}, |
||||
}; |
||||
use gc::{Gc, GcCell}; |
||||
use std::collections::{hash_map::HashMap, hash_set::HashSet}; |
||||
|
||||
/// Representation of a Realm.
|
||||
/// In the specification these are called Realm Records.
|
||||
#[derive(Debug)] |
||||
pub struct Realm { |
||||
pub global_obj: Value, |
||||
pub global_env: Gc<GcCell<Box<GlobalEnvironmentRecord>>>, |
||||
pub environment: LexicalEnvironment, |
||||
} |
||||
|
||||
impl Realm { |
||||
pub fn create() -> Realm { |
||||
// Create brand new global object
|
||||
// Global has no prototype to pass None to new_obj
|
||||
let global = ValueData::new_obj(None); |
||||
// We need to clone the global here because its referenced from separate places (only pointer is cloned)
|
||||
let global_env = new_global_environment(global.clone(), global.clone()); |
||||
|
||||
let new_realm = Realm { |
||||
global_obj: global.clone(), |
||||
global_env, |
||||
environment: LexicalEnvironment::new(global), |
||||
}; |
||||
|
||||
// Add new builtIns to Realm
|
||||
// At a later date this can be removed from here and called explicity, but for now we almost always want these default builtins
|
||||
new_realm.create_instrinsics(); |
||||
|
||||
new_realm |
||||
} |
||||
|
||||
// Sets up the default global objects within Global
|
||||
fn create_instrinsics(&self) { |
||||
let global = &self.global_obj; |
||||
// Create intrinsics, add global objects here
|
||||
object::init(global); |
||||
console::init(global); |
||||
math::init(global); |
||||
function::init(global); |
||||
json::init(global); |
||||
|
||||
global.set_field_slice("String", string::create_constructor(global)); |
||||
global.set_field_slice("RegExp", regexp::create_constructor(global)); |
||||
global.set_field_slice("Array", array::create_constructor(global)); |
||||
global.set_field_slice("Boolean", boolean::create_constructor(global)); |
||||
} |
||||
} |
||||
|
||||
// Similar to new_global_environment in lexical_environment, except we need to return a GlobalEnvirionment
|
||||
fn new_global_environment( |
||||
global: Value, |
||||
this_value: Value, |
||||
) -> Gc<GcCell<Box<GlobalEnvironmentRecord>>> { |
||||
let obj_rec = Box::new(ObjectEnvironmentRecord { |
||||
bindings: global, |
||||
outer_env: None, |
||||
/// Object Environment Records created for with statements (13.11)
|
||||
/// can provide their binding object as an implicit this value for use in function calls.
|
||||
/// The capability is controlled by a withEnvironment Boolean value that is associated
|
||||
/// with each object Environment Record. By default, the value of withEnvironment is false
|
||||
/// for any object Environment Record.
|
||||
with_environment: false, |
||||
}); |
||||
|
||||
let dcl_rec = Box::new(DeclarativeEnvironmentRecord { |
||||
env_rec: HashMap::new(), |
||||
outer_env: None, |
||||
}); |
||||
|
||||
Gc::new(GcCell::new(Box::new(GlobalEnvironmentRecord { |
||||
object_record: obj_rec, |
||||
global_this_binding: this_value, |
||||
declarative_record: dcl_rec, |
||||
var_names: HashSet::new(), |
||||
}))) |
||||
} |
Loading…
Reference in new issue