Skip to content

Commit 8df335a

Browse files
authoredNov 26, 2023
Expose class name to static initializers (#139)
Fixes: #138
1 parent d4c1244 commit 8df335a

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed
 

‎quickjs.c

+8
Original file line numberDiff line numberDiff line change
@@ -20972,6 +20972,14 @@ static __exception int js_parse_class(JSParseState *s, BOOL is_class_expr,
2097220972
emit_atom(s, JS_ATOM_this);
2097320973
emit_u16(s, 0);
2097420974

20975+
// expose class name to static initializers
20976+
if (is_static && class_name != JS_ATOM_NULL) {
20977+
emit_op(s, OP_dup);
20978+
emit_op(s, OP_scope_put_var_init);
20979+
emit_atom(s, class_name);
20980+
emit_u16(s, s->cur_func->scope_level);
20981+
}
20982+
2097520983
if (name == JS_ATOM_NULL) {
2097620984
emit_op(s, OP_scope_get_var);
2097720985
emit_atom(s, field_var_name);

‎tests/test_language.js

+9
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,15 @@ function test_class()
325325
/* test class name scope */
326326
var E1 = class E { static F() { return E; } };
327327
assert(E1 === E1.F());
328+
329+
class S {
330+
static x = 42;
331+
static y = S.x;
332+
static z = this.x;
333+
}
334+
assert(S.x === 42);
335+
assert(S.y === 42);
336+
assert(S.z === 42);
328337
};
329338

330339
function test_template()

0 commit comments

Comments
 (0)