Skip to content

Commit

Permalink
Fix sending this value to function environments (#526)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonwilliams authored Jun 25, 2020
1 parent 3fe8942 commit c4a652a
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 27 deletions.
6 changes: 6 additions & 0 deletions .editorConfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ root = true
[{Makefile,**.mk}]
# Use tabs for indentation (Makefiles require tabs)
indent_style = tab

[*]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
60 changes: 40 additions & 20 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@
"type": "process",
"label": "Cargo Run",
"command": "cargo",
"args": ["run", "--bin", "boa", "./tests/js/test.js"],
"problemMatcher": ["$rustc"],
"args": [
"run",
"--bin",
"boa",
"./tests/js/test.js"
],
"group": {
"kind": "build",
"isDefault": true
},
"options": {
"env": { "RUST_BACKTRACE": "full" }
"env": {
"RUST_BACKTRACE": "full"
}
},
"presentation": {
"clear": true
Expand All @@ -24,14 +30,17 @@
"type": "process",
"label": "Cargo Run (Profiler)",
"command": "cargo",
"args": ["run", "--features", "Boa/profiler", "../tests/js/test.js"],
"problemMatcher": ["$rustc"],
"group": {
"kind": "build",
"isDefault": true
},
"args": [
"run",
"--features",
"Boa/profiler",
"../tests/js/test.js"
],
"group": "build",
"options": {
"env": { "RUST_BACKTRACE": "full" },
"env": {
"RUST_BACKTRACE": "full"
},
"cwd": "${workspaceFolder}/boa_cli"
},
"presentation": {
Expand All @@ -42,8 +51,12 @@
"type": "process",
"label": "Get Tokens",
"command": "cargo",
"args": ["run", "--", "-t=Debug", "./tests/js/test.js"],
"problemMatcher": ["$rustc"],
"args": [
"run",
"--",
"-t=Debug",
"./tests/js/test.js"
],
"group": "build",
"presentation": {
"clear": true
Expand All @@ -53,19 +66,24 @@
"type": "process",
"label": "Get AST",
"command": "cargo",
"args": ["run", "--", "-a=Debug", "./tests/js/test.js"],
"problemMatcher": ["$rustc"],
"args": [
"run",
"--",
"-a=Debug",
"./tests/js/test.js"
],
"group": "build",
"presentation": {
"clear": true
}
},
},
{
"type": "process",
"label": "Cargo Test",
"command": "cargo",
"args": ["test"],
"problemMatcher": ["$rustc"],
"args": [
"test"
],
"group": {
"kind": "test",
"isDefault": true
Expand All @@ -78,9 +96,11 @@
"type": "process",
"label": "Cargo Test Build",
"command": "cargo",
"args": ["test", "--no-run"],
"problemMatcher": ["$rustc"],
"args": [
"test",
"--no-run"
],
"group": "build"
}
]
}
}
20 changes: 17 additions & 3 deletions boa/src/builtins/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,18 @@ impl Function {
// <https://tc39.es/ecma262/#sec-prepareforordinarycall>
let local_env = new_function_environment(
function,
None,
if let ThisMode::Lexical = self.this_mode {
None
} else {
Some(this.clone())
},
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
Expand Down Expand Up @@ -253,7 +262,12 @@ impl Function {
function,
Some(this.clone()),
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
Expand Down
2 changes: 1 addition & 1 deletion boa/src/environment/function_environment_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl EnvironmentRecordTrait for FunctionEnvironmentRecord {
}
BindingStatus::Uninitialized => {
// 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(),
Expand Down
11 changes: 8 additions & 3 deletions boa/src/environment/lexical_environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,20 @@ pub fn new_function_environment(
outer: Option<Environment>,
binding_status: BindingStatus,
) -> Environment {
Gc::new(GcCell::new(Box::new(FunctionEnvironmentRecord {
let mut func_env = FunctionEnvironmentRecord {
env_rec: FxHashMap::default(),
function: f,
this_binding_status: binding_status,
home_object: 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
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 {
Expand Down
16 changes: 16 additions & 0 deletions boa/src/exec/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -939,3 +939,19 @@ fn to_object() {
.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");
}

0 comments on commit c4a652a

Please sign in to comment.