Skip to content

Commit 166d5ac

Browse files
caiolimaleobalter
authored andcommitted
Adding tests to validate HomeObject setup on private methods and accessors (#2214)
1 parent 14b6bec commit 166d5ac

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (C) 2019 Caio Lima (Igalia SL). All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
description: Private getter contains proper HomeObject
6+
esid: sec-method-definitions-runtime-semantics-classelementevaluation
7+
info: |
8+
MethodDefinition : get ClassElementName () { FunctionBody }
9+
1. Let key be the result of evaluating ClassElementName.
10+
2. ReturnIfAbrupt(key).
11+
3. If the function code for this MethodDefinition is strict mode code, let strict be true. Otherwise let strict be false.
12+
4. Let scope be the running execution context's LexicalEnvironment.
13+
5. Let formalParameterList be an instance of the production FormalParameters:[empty] .
14+
6. Let closure be FunctionCreate(Method, formalParameterList, FunctionBody, scope, strict).
15+
7. Perform MakeMethod(closure, homeObject).
16+
8. Perform SetFunctionName(closure, key, "get").
17+
9. If key is a Private Name,
18+
a. If key has a [[Kind]] field,
19+
i. Assert: key.[[Kind]] is "accessor".
20+
ii. Assert: key.[[Brand]] is homeObject.
21+
iii. Assert: key does not have a [[Get]] field.
22+
iv. Set key.[[Get]] to closure.
23+
b. Otherwise,
24+
i. Set key.[[Kind]] to "accessor".
25+
ii. Set key.[[Brand]] to homeObject.
26+
iii. Set key.[[Get]] to closure.
27+
10. Else,
28+
a. Let desc be the PropertyDescriptor{[[Get]]: closure, [[Enumerable]]: enumerable, [[Configurable]]: true}.
29+
b. Perform ? DefinePropertyOrThrow(homeObject, key, desc).
30+
features: [class-methods-private, class]
31+
---*/
32+
33+
class A {
34+
method() {
35+
return "Test262";
36+
}
37+
}
38+
39+
class C extends A {
40+
get #m() {
41+
return super.method();
42+
}
43+
44+
access() {
45+
return this.#m;
46+
}
47+
}
48+
49+
let c = new C();
50+
assert.sameValue(c.access(), "Test262");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (C) 2019 Caio Lima (Igalia SL). All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
description: Private method contains proper HomeObject
6+
esid: sec-method-definitions-runtime-semantics-classelementevaluation
7+
info: |
8+
MethodDefinition : ClassElementName ( UniqueFormalParameters ) { FunctionBody }
9+
1. Let methodDef be DefineMethod of MethodDefinition with argument homeObject.
10+
2. ReturnIfAbrupt(methodDef).
11+
3. Perform ? DefineOrdinaryMethod(methodDef.[[Key]], homeObject, methodDef.[[Closure]], _enumerable).
12+
13+
MethodDefinition : PropertyName ( UniqueFormalParameters ) { FunctionBody }
14+
1. Let propKey be the result of evaluating PropertyName.
15+
2. ReturnIfAbrupt(propKey).
16+
3. Let scope be the running execution context's LexicalEnvironment.
17+
4. If functionPrototype is present as a parameter, then
18+
a. Let kind be Normal.
19+
b. Let prototype be functionPrototype.
20+
5. Else,
21+
a. Let kind be Method.
22+
b. Let prototype be the intrinsic object %FunctionPrototype%.
23+
6. Let closure be FunctionCreate(kind, UniqueFormalParameters, FunctionBody, scope, prototype).
24+
7. Perform MakeMethod(closure, object).
25+
8. Set closure.[[SourceText]] to the source text matched by MethodDefinition.
26+
9. Return the Record { [[Key]]: propKey, [[Closure]]: closure }.
27+
features: [class-methods-private, class]
28+
---*/
29+
30+
class A {
31+
method() {
32+
return "Test262";
33+
}
34+
}
35+
36+
class C extends A {
37+
#m() {
38+
return super.method();
39+
}
40+
41+
access(o) {
42+
return this.#m.call(o);
43+
}
44+
}
45+
46+
let c = new C();
47+
assert.sameValue(c.access(c), "Test262");
48+
49+
let o = {};
50+
assert.sameValue(c.access(o), "Test262");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (C) 2019 Caio Lima (Igalia SL). All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
description: Private setter contains proper HomeObject
6+
esid: sec-method-definitions-runtime-semantics-classelementevaluation
7+
info: |
8+
MethodDefinition : set ClassElementName ( PropertySetParameterList ) { FunctionBody }
9+
1. Let key be the result of evaluating ClassElementName.
10+
2. ReturnIfAbrupt(key).
11+
3. If the function code for this MethodDefinition is strict mode code, let strict be true. Otherwise let strict be false.
12+
4. Let scope be the running execution context's LexicalEnvironment.
13+
5. Let closure be FunctionCreate(Method, PropertySetParameterList, FunctionBody, scope, strict).
14+
6. Perform MakeMethod(closure, homeObject).
15+
7. Perform SetFunctionName(closure, key, "set").
16+
8. If key is a Private Name,
17+
a. If key has a [[Kind]] field,
18+
i. Assert: key.[[Kind]] is "accessor".
19+
ii. Assert: key.[[Brand]] is homeObject.
20+
iii. Assert: key does not have a [[Set]] field.
21+
iv. Set key.[[Set]] to closure.
22+
b. Otherwise,
23+
i. Set key.[[Kind]] to "accessor".
24+
ii. Set key.[[Brand]] to homeObject.
25+
iii. Set key.[[Set]] to closure.
26+
9. Else,
27+
a. Let desc be the PropertyDescriptor{[[Set]]: closure, [[Enumerable]]: enumerable, [[Configurable]]: true}.
28+
b. Perform ? DefinePropertyOrThrow(homeObject, key, desc).
29+
features: [class-methods-private, class]
30+
---*/
31+
32+
class A {
33+
method(v) {
34+
return v;
35+
}
36+
}
37+
38+
class C extends A {
39+
set #m(v) {
40+
this._v = super.method(v);
41+
}
42+
43+
access() {
44+
return this.#m = "Test262";
45+
}
46+
}
47+
48+
let c = new C();
49+
c.access();
50+
assert.sameValue(c._v, "Test262");

0 commit comments

Comments
 (0)