commit 606c25d1eb9c202e9fb0a126f1db9a027d124ca0
parent 85855f4896920b53cbc724c94e2d8c744678155f
Author: Fabrice Bellard <fabrice@bellard.org>
Date: Mon, 8 Jan 2024 18:39:58 +0100
fixed class name init in static initializers
Diffstat:
2 files changed, 19 insertions(+), 17 deletions(-)
diff --git a/quickjs/quickjs.c b/quickjs/quickjs.c
@@ -23138,14 +23138,6 @@ static __exception int js_parse_class(JSParseState *s, BOOL is_class_expr,
emit_atom(s, JS_ATOM_this);
emit_u16(s, 0);
// stack is now: fclosure this
- /* XXX: should do it only once */
- if (class_name != JS_ATOM_NULL) {
- // TODO(bnoordhuis) pass as argument to init method?
- emit_op(s, OP_dup);
- emit_op(s, OP_scope_put_var_init);
- emit_atom(s, class_name);
- emit_u16(s, s->cur_func->scope_level);
- }
emit_op(s, OP_swap);
// stack is now: this fclosure
emit_op(s, OP_call_method);
@@ -23466,6 +23458,16 @@ static __exception int js_parse_class(JSParseState *s, BOOL is_class_expr,
emit_op(s, OP_add_brand);
}
+ if (class_name != JS_ATOM_NULL) {
+ /* store the class name in the scoped class name variable (it
+ is independent from the class statement variable
+ definition) */
+ emit_op(s, OP_dup);
+ emit_op(s, OP_scope_put_var_init);
+ emit_atom(s, class_name);
+ emit_u16(s, fd->scope_level);
+ }
+
/* initialize the static fields */
if (class_fields[1].fields_init_fd != NULL) {
ClassFieldsDef *cf = &class_fields[1];
@@ -23476,15 +23478,6 @@ static __exception int js_parse_class(JSParseState *s, BOOL is_class_expr,
emit_op(s, OP_drop);
}
- if (class_name != JS_ATOM_NULL) {
- /* store the class name in the scoped class name variable (it
- is independent from the class statement variable
- definition) */
- emit_op(s, OP_dup);
- emit_op(s, OP_scope_put_var_init);
- emit_atom(s, class_name);
- emit_u16(s, fd->scope_level);
- }
pop_scope(s);
pop_scope(s);
diff --git a/quickjs/tests/test_language.js b/quickjs/tests/test_language.js
@@ -326,6 +326,15 @@ function test_class()
/* test class name scope */
var E1 = class E { static F() { return E; } };
assert(E1 === E1.F());
+
+ class S {
+ static x = 42;
+ static y = S.x;
+ static z = this.x;
+ }
+ assert(S.x === 42);
+ assert(S.y === 42);
+ assert(S.z === 42);
};
function test_template()