Skip to content

Commit

Permalink
Merge pull request #1 from creampnx-x/fix/new-super-access
Browse files Browse the repository at this point in the history
fix: get this in object method
  • Loading branch information
creampnx-x committed Sep 11, 2022
2 parents 0f58b1d + ee8f98c commit c59b652
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
9 changes: 4 additions & 5 deletions boa_engine/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1524,14 +1524,13 @@ impl Context {
let mut home_object = function.get_home_object().cloned();

if home_object == None {
home_object = self
.vm
.stack
.last()
.expect("stack is empty")
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
41 changes: 30 additions & 11 deletions boa_engine/src/vm/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,20 +141,13 @@ fn finally_block_binding_env() {
#[test]
fn run_super_method_in_object() {
let source = r#"
let obj = {
v() {
return super.m();
}
};
let proto = {
m() {
return "super";
}
m() { return "super"; }
};
let obj = {
v() { return super.m(); }
};
Object.setPrototypeOf(obj, proto);
obj.v();
"#;

Expand All @@ -163,3 +156,29 @@ fn run_super_method_in_object() {
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"))
)
}

0 comments on commit c59b652

Please sign in to comment.