From 8df335a7b9a328cb495fc8c1376fff0182e5ff70 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Sun, 26 Nov 2023 01:09:18 +0100 Subject: [PATCH] Expose class name to static initializers (#139) Fixes: https://github.com/quickjs-ng/quickjs/issues/138 --- quickjs.c | 8 ++++++++ tests/test_language.js | 9 +++++++++ 2 files changed, 17 insertions(+) diff --git a/quickjs.c b/quickjs.c index 9e76e90..65a9026 100644 --- a/quickjs.c +++ b/quickjs.c @@ -20972,6 +20972,14 @@ static __exception int js_parse_class(JSParseState *s, BOOL is_class_expr, emit_atom(s, JS_ATOM_this); emit_u16(s, 0); + // expose class name to static initializers + if (is_static && class_name != JS_ATOM_NULL) { + 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); + } + if (name == JS_ATOM_NULL) { emit_op(s, OP_scope_get_var); emit_atom(s, field_var_name); diff --git a/tests/test_language.js b/tests/test_language.js index d19dc40..746fa0e 100644 --- a/tests/test_language.js +++ b/tests/test_language.js @@ -325,6 +325,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()