Browse Source

Fix sending `this` value to function environments (#526)

pull/529/head
Jason Williams 4 years ago committed by GitHub
parent
commit
c4a652a517
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      .editorConfig
  2. 60
      .vscode/tasks.json
  3. 20
      boa/src/builtins/function/mod.rs
  4. 2
      boa/src/environment/function_environment_record.rs
  5. 11
      boa/src/environment/lexical_environment.rs
  6. 16
      boa/src/exec/tests.rs

6
.editorConfig

@ -3,3 +3,9 @@ root = true
[{Makefile,**.mk}] [{Makefile,**.mk}]
# Use tabs for indentation (Makefiles require tabs) # Use tabs for indentation (Makefiles require tabs)
indent_style = tab indent_style = tab
[*]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

60
.vscode/tasks.json vendored

@ -7,14 +7,20 @@
"type": "process", "type": "process",
"label": "Cargo Run", "label": "Cargo Run",
"command": "cargo", "command": "cargo",
"args": ["run", "--bin", "boa", "./tests/js/test.js"], "args": [
"problemMatcher": ["$rustc"], "run",
"--bin",
"boa",
"./tests/js/test.js"
],
"group": { "group": {
"kind": "build", "kind": "build",
"isDefault": true "isDefault": true
}, },
"options": { "options": {
"env": { "RUST_BACKTRACE": "full" } "env": {
"RUST_BACKTRACE": "full"
}
}, },
"presentation": { "presentation": {
"clear": true "clear": true
@ -24,14 +30,17 @@
"type": "process", "type": "process",
"label": "Cargo Run (Profiler)", "label": "Cargo Run (Profiler)",
"command": "cargo", "command": "cargo",
"args": ["run", "--features", "Boa/profiler", "../tests/js/test.js"], "args": [
"problemMatcher": ["$rustc"], "run",
"group": { "--features",
"kind": "build", "Boa/profiler",
"isDefault": true "../tests/js/test.js"
}, ],
"group": "build",
"options": { "options": {
"env": { "RUST_BACKTRACE": "full" }, "env": {
"RUST_BACKTRACE": "full"
},
"cwd": "${workspaceFolder}/boa_cli" "cwd": "${workspaceFolder}/boa_cli"
}, },
"presentation": { "presentation": {
@ -42,8 +51,12 @@
"type": "process", "type": "process",
"label": "Get Tokens", "label": "Get Tokens",
"command": "cargo", "command": "cargo",
"args": ["run", "--", "-t=Debug", "./tests/js/test.js"], "args": [
"problemMatcher": ["$rustc"], "run",
"--",
"-t=Debug",
"./tests/js/test.js"
],
"group": "build", "group": "build",
"presentation": { "presentation": {
"clear": true "clear": true
@ -53,19 +66,24 @@
"type": "process", "type": "process",
"label": "Get AST", "label": "Get AST",
"command": "cargo", "command": "cargo",
"args": ["run", "--", "-a=Debug", "./tests/js/test.js"], "args": [
"problemMatcher": ["$rustc"], "run",
"--",
"-a=Debug",
"./tests/js/test.js"
],
"group": "build", "group": "build",
"presentation": { "presentation": {
"clear": true "clear": true
} },
}, },
{ {
"type": "process", "type": "process",
"label": "Cargo Test", "label": "Cargo Test",
"command": "cargo", "command": "cargo",
"args": ["test"], "args": [
"problemMatcher": ["$rustc"], "test"
],
"group": { "group": {
"kind": "test", "kind": "test",
"isDefault": true "isDefault": true
@ -78,9 +96,11 @@
"type": "process", "type": "process",
"label": "Cargo Test Build", "label": "Cargo Test Build",
"command": "cargo", "command": "cargo",
"args": ["test", "--no-run"], "args": [
"problemMatcher": ["$rustc"], "test",
"--no-run"
],
"group": "build" "group": "build"
} }
] ]
} }

20
boa/src/builtins/function/mod.rs

@ -191,9 +191,18 @@ impl Function {
// <https://tc39.es/ecma262/#sec-prepareforordinarycall> // <https://tc39.es/ecma262/#sec-prepareforordinarycall>
let local_env = new_function_environment( let local_env = new_function_environment(
function, function,
None, if let ThisMode::Lexical = self.this_mode {
None
} else {
Some(this.clone())
},
self.environment.as_ref().cloned(), self.environment.as_ref().cloned(),
BindingStatus::Uninitialized, // Arrow functions do not have a this binding https://tc39.es/ecma262/#sec-function-environment-records
if let ThisMode::Lexical = self.this_mode {
BindingStatus::Lexical
} else {
BindingStatus::Uninitialized
},
); );
// Add argument bindings to the function environment // Add argument bindings to the function environment
@ -253,7 +262,12 @@ impl Function {
function, function,
Some(this.clone()), Some(this.clone()),
self.environment.as_ref().cloned(), self.environment.as_ref().cloned(),
BindingStatus::Initialized, // Arrow functions do not have a this binding https://tc39.es/ecma262/#sec-function-environment-records
if let ThisMode::Lexical = self.this_mode {
BindingStatus::Lexical
} else {
BindingStatus::Uninitialized
},
); );
// Add argument bindings to the function environment // Add argument bindings to the function environment

2
boa/src/environment/function_environment_record.rs

@ -112,7 +112,7 @@ impl EnvironmentRecordTrait for FunctionEnvironmentRecord {
} }
BindingStatus::Uninitialized => { BindingStatus::Uninitialized => {
// TODO: change this when error handling comes into play // TODO: change this when error handling comes into play
panic!("Reference Error: Unitialised binding for this function"); panic!("Reference Error: Uninitialised binding for this function");
} }
BindingStatus::Initialized => self.this_value.clone(), BindingStatus::Initialized => self.this_value.clone(),

11
boa/src/environment/lexical_environment.rs

@ -235,15 +235,20 @@ pub fn new_function_environment(
outer: Option<Environment>, outer: Option<Environment>,
binding_status: BindingStatus, binding_status: BindingStatus,
) -> Environment { ) -> Environment {
Gc::new(GcCell::new(Box::new(FunctionEnvironmentRecord { let mut func_env = FunctionEnvironmentRecord {
env_rec: FxHashMap::default(), env_rec: FxHashMap::default(),
function: f, function: f,
this_binding_status: binding_status, this_binding_status: binding_status,
home_object: Value::undefined(), home_object: Value::undefined(),
new_target: Value::undefined(), new_target: Value::undefined(),
outer_env: outer, // this will come from Environment set as a private property of F - https://tc39.es/ecma262/#sec-ecmascript-function-objects outer_env: outer, // this will come from Environment set as a private property of F - https://tc39.es/ecma262/#sec-ecmascript-function-objects
this_value: this.unwrap_or_else(Value::undefined), this_value: Value::undefined(),
}))) };
// If a `this` value has been passed, bind it to the environment
if let Some(v) = this {
func_env.bind_this_value(v);
}
Gc::new(GcCell::new(Box::new(func_env)))
} }
pub fn new_object_environment(object: Value, environment: Option<Environment>) -> Environment { pub fn new_object_environment(object: Value, environment: Option<Environment>) -> Environment {

16
boa/src/exec/tests.rs

@ -939,3 +939,19 @@ fn to_object() {
.is_object()); .is_object());
assert!(engine.to_object(&Value::null()).unwrap_err().is_object()); assert!(engine.to_object(&Value::null()).unwrap_err().is_object());
} }
#[test]
fn check_this_binding_in_object_literal() {
let realm = Realm::create();
let mut engine = Interpreter::new(realm);
let init = r#"
var foo = {
a: 3,
bar: function () { return this.a + 5 }
};
foo.bar()
"#;
assert_eq!(forward(&mut engine, init), "8");
}

Loading…
Cancel
Save