Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - fix computed property methods can call super methods #2274

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion boa_engine/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1521,7 +1521,17 @@ impl Context {
let function = function_object
.as_function()
.expect("must be function object");
function.get_home_object().cloned()
let mut home_object = function.get_home_object().cloned();

if home_object.is_none() {
home_object = env
.get_this_binding()
.expect("can not get `this` object")
.as_object()
.cloned();
}

home_object
} else {
return self.throw_range_error("Must call super constructor in derived class before accessing 'this' or returning from derived constructor");
};
Expand Down
45 changes: 45 additions & 0 deletions boa_engine/src/vm/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,48 @@ fn finally_block_binding_env() {
Ok(JsValue::from("Hey hey people"))
);
}

#[test]
fn run_super_method_in_object() {
let source = r#"
let proto = {
m() { return "super"; }
};
let obj = {
v() { return super.m(); }
};
Object.setPrototypeOf(obj, proto);
obj.v();
"#;

assert_eq!(
Context::default().eval(source.as_bytes()),
Ok(JsValue::from("super"))
)
}

#[test]
fn get_reference_by_super() {
let source = r#"
var fromA, fromB;
var A = { fromA: 'a', fromB: 'a' };
var B = { fromB: 'b' };
Object.setPrototypeOf(B, A);
var obj = {
fromA: 'c',
fromB: 'c',
method() {
fromA = (() => { return super.fromA; })();
fromB = (() => { return super.fromB; })();
}
};
Object.setPrototypeOf(obj, B);
obj.method();
fromA + fromB
"#;

assert_eq!(
Context::default().eval(source.as_bytes()),
Ok(JsValue::from("ab"))
)
}