Commit 4f459d02

4f459d02194bMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

Fix instance_eval ivar access via receiver, not Toplevel pseudo-class

When an instance_eval block reads/writes @ivars, the scope has class_id=-1 so codegen was emitting civ_Toplevel_X (file-scope globals) instead of receiver->iv_X, causing undeclared-variable errors.

Add g_ie_class_id to codegen (mirrors analyze.c's g_ie_class_id) and set it while emitting instance_eval block bodies. InstanceVariable read/write nodes with class_id<0 now check g_ie_class_id first: if inside an instance_eval they emit g_self->iv_X; only true top-level scopes still use civ_Toplevel_X.

Fixes bundle_classd_04 (civ_Toplevel_count/greeting undeclared).

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
4f459d02194b
Parent
66895eb945f6

src/codegen.cmodifiedView file

@@ -74,6 +74,9 @@
7474 /* Class id of the scope currently being emitted (-1 if none). Used to resolve
7575 implicit self calls in included-module methods to the including class. */
7676 static int g_emitting_class_id = -1;
77+/* When inside an instance_eval block, the class id of the receiver (-1 outside).
78+ Used so InstanceVariableReadNode/WriteNode use g_self->iv_X instead of civ_Toplevel_X. */
79+static int g_ie_class_id = -1;
7780 /* While emitting a rescue handler: the C var names holding the caught
7881 exception's class/message, so a bare `raise` can re-raise. */
7982 static const char *g_rescue_cls = NULL, *g_rescue_msg = NULL;
@@ -4402,6 +4405,7 @@
44024405 buf_printf(g_pre, " _t%d;\n", tres);
44034406 char selfbuf[64]; snprintf(selfbuf, sizeof selfbuf, "_t%d", tr);
44044407 const char *saved_self2 = g_self; g_self = selfbuf;
4408+ int saved_ie = g_ie_class_id; g_ie_class_id = cls_id;
44054409 if (ie_bn > 0) {
44064410 for (int j = 0; j < ie_bn - 1; j++) emit_stmt(c, ie_bb[j], g_pre, g_indent);
44074411 Buf vb; memset(&vb, 0, sizeof vb);
@@ -4417,6 +4421,7 @@
44174421 }
44184422 free(vb.p);
44194423 }
4424+ g_ie_class_id = saved_ie;
44204425 g_self = saved_self2;
44214426 buf_printf(b, "_t%d", tres);
44224427 return;
@@ -5869,10 +5874,25 @@
58695874
58705875 /* nil receiver: nil.inspect -> "nil", nil.to_s -> "", nil.nil? -> true.
58715876 Evaluate the receiver for side effects, then yield the constant. */
5872- if (recv >= 0 && rt == TY_NIL && argc == 0) {
5873- if (!strcmp(name, "inspect")) { buf_puts(b, "((void)("); emit_expr(c, recv, b); buf_puts(b, "), SPL(\"nil\"))"); return; }
5874- if (!strcmp(name, "to_s")) { buf_puts(b, "((void)("); emit_expr(c, recv, b); buf_puts(b, "), SPL(\"\"))"); return; }
5875- if (!strcmp(name, "nil?")) { buf_puts(b, "((void)("); emit_expr(c, recv, b); buf_puts(b, "), 1)"); return; }
5877+ if (recv >= 0 && rt == TY_NIL) {
5878+ if (argc == 0 && !strcmp(name, "inspect")) { buf_puts(b, "((void)("); emit_expr(c, recv, b); buf_puts(b, "), SPL(\"nil\"))"); return; }
5879+ if (argc == 0 && !strcmp(name, "to_s")) { buf_puts(b, "((void)("); emit_expr(c, recv, b); buf_puts(b, "), SPL(\"\"))"); return; }
5880+ if (argc == 0 && !strcmp(name, "nil?")) { buf_puts(b, "((void)("); emit_expr(c, recv, b); buf_puts(b, "), 1)"); return; }
5881+ if (argc == 0 && !strcmp(name, "to_i")) { buf_puts(b, "((void)("); emit_expr(c, recv, b); buf_puts(b, "), (mrb_int)0)"); return; }
5882+ if (argc == 0 && !strcmp(name, "to_f")) { buf_puts(b, "((void)("); emit_expr(c, recv, b); buf_puts(b, "), 0.0)"); return; }
5883+ if (argc == 0 && !strcmp(name, "to_r")) { buf_puts(b, "((void)("); emit_expr(c, recv, b); buf_puts(b, "), (mrb_float)0.0)"); return; }
5884+ if (argc == 0 && !strcmp(name, "to_a")) { buf_puts(b, "((void)("); emit_expr(c, recv, b); buf_puts(b, "), sp_PolyArray_new())"); return; }
5885+ if (argc == 0 && !strcmp(name, "to_h")) {
5886+ buf_puts(b, "((void)("); emit_expr(c, recv, b);
5887+ buf_puts(b, "), sp_SymPolyHash_new())");
5888+ return;
5889+ }
5890+ if (argc == 1 && (!strcmp(name, "is_a?") || !strcmp(name, "kind_of?") || !strcmp(name, "instance_of?"))) {
5891+ const char *cn = nt_type(nt, argv[0]) && !strcmp(nt_type(nt, argv[0]), "ConstantReadNode") ? nt_str(nt, argv[0], "name") : NULL;
5892+ int yes = cn ? (!strcmp(cn, "NilClass") || !strcmp(name, "instance_of?") ? !strcmp(cn, "NilClass") : (!strcmp(cn, "Object") || !strcmp(cn, "BasicObject"))) : 0;
5893+ buf_puts(b, "((void)("); emit_expr(c, recv, b); buf_printf(b, "), %d)", yes);
5894+ return;
5895+ }
58765896 }
58775897
58785898 /* encoding.name -> the encoding name string */
@@ -6103,6 +6123,36 @@
61036123 return;
61046124 }
61056125
6126+ /* Class.===(obj): equivalent to obj.is_a?(Class). Receiver is a class constant. */
6127+ if (recv >= 0 && argc == 1 && !strcmp(name, "===") &&
6128+ nt_type(nt, recv) && !strcmp(nt_type(nt, recv), "ConstantReadNode")) {
6129+ const char *cn = nt_str(nt, recv, "name");
6130+ if (cn) {
6131+ TyKind at2 = comp_ntype(c, argv[0]);
6132+ int yes = ty_matches_class(at2, cn, 0);
6133+ if (yes >= 0) {
6134+ buf_puts(b, "((void)("); emit_expr(c, argv[0], b); buf_printf(b, "), %d)", yes);
6135+ return;
6136+ }
6137+ /* arg type is poly or unknown: runtime tag check */
6138+ if (at2 == TY_POLY) {
6139+ int tv = ++g_tmp;
6140+ buf_printf(b, "({ sp_RbVal _t%d = ", tv); emit_expr(c, argv[0], b); buf_printf(b, "; ");
6141+ char v[32]; snprintf(v, sizeof v, "_t%d", tv);
6142+ if (!strcmp(cn, "Integer") || !strcmp(cn, "Fixnum")) buf_printf(b, "%s.tag == SP_TAG_INT", v);
6143+ else if (!strcmp(cn, "String")) buf_printf(b, "%s.tag == SP_TAG_STR", v);
6144+ else if (!strcmp(cn, "Float")) buf_printf(b, "%s.tag == SP_TAG_FLT", v);
6145+ else if (!strcmp(cn, "Symbol")) buf_printf(b, "%s.tag == SP_TAG_SYM", v);
6146+ else if (!strcmp(cn, "NilClass")) buf_printf(b, "%s.tag == SP_TAG_NIL", v);
6147+ else if (!strcmp(cn, "Numeric")) buf_printf(b, "(%s.tag == SP_TAG_INT || %s.tag == SP_TAG_FLT)", v, v);
6148+ else if (!strcmp(cn, "Array")) buf_printf(b, "(%s.tag == SP_TAG_OBJ && %s.cls_id <= -1 && %s.cls_id >= -12)", v, v, v);
6149+ else buf_printf(b, "0");
6150+ buf_puts(b, "; })");
6151+ return;
6152+ }
6153+ }
6154+ }
6155+
61066156 /* `===` on a scalar comparable (bool/int/float/string/symbol) is case
61076157 equality == value equality. Range/Class/Regexp `===` have their own
61086158 handlers and fall through here. */
@@ -11054,6 +11104,8 @@
1105411104 char ref2e[300];
1105511105 if (cws && cws->is_cmethod && cid2 >= 0)
1105611106 snprintf(ref2e, sizeof ref2e, "civ_%s_%s", c->classes[cid2].name, nm + 1);
11107+ else if (cid2 < 0 && g_ie_class_id >= 0)
11108+ snprintf(ref2e, sizeof ref2e, "%s->iv_%s", g_self, nm + 1);
1105711109 else if (cid2 < 0 && comp_class_index(c, "Toplevel") >= 0)
1105811110 snprintf(ref2e, sizeof ref2e, "civ_Toplevel_%s", nm + 1);
1105911111 else
@@ -11253,6 +11305,9 @@
1125311305 Scope *cs = comp_scope_of(c, id);
1125411306 if (cs && cs->is_cmethod && cs->class_id >= 0)
1125511307 buf_printf(b, "civ_%s_%s", c->classes[cs->class_id].name, nm + 1); /* module/class-level ivar */
11308+ else if (cs && cs->class_id < 0 && g_ie_class_id >= 0)
11309+ /* inside instance_eval block: access ivar via receiver pointer */
11310+ buf_printf(b, "%s->iv_%s", g_self, nm + 1);
1125611311 else if (cs && cs->class_id < 0) {
1125711312 /* top-level method: ivar stored as file-scope global in Toplevel pseudo-class */
1125811313 int tl = comp_class_index(c, "Toplevel");
@@ -11387,6 +11442,13 @@
1138711442 return;
1138811443 }
1138911444 if (nm && !strcmp(nm, "RUBY_DESCRIPTION")) { buf_puts(b, "SPL(\"spinel\")"); return; }
11445+ if (nm && !strcmp(nm, "RUBY_VERSION")) { buf_puts(b, "SPL(\"3.2.0\")"); return; }
11446+ if (nm && !strcmp(nm, "RUBY_ENGINE")) { buf_puts(b, "SPL(\"ruby\")"); return; }
11447+ if (nm && !strcmp(nm, "RUBY_ENGINE_VERSION")) { buf_puts(b, "SPL(\"3.2.0\")"); return; }
11448+ if (nm && !strcmp(nm, "RUBY_PLATFORM")) { buf_puts(b, "sp_ruby_platform_str()"); return; }
11449+ if (nm && !strcmp(nm, "RUBY_RELEASE_DATE")) { buf_puts(b, "SPL(\"2023-03-30\")"); return; }
11450+ if (nm && !strcmp(nm, "RUBY_REVISION")) { buf_puts(b, "SPL(\"0\")"); return; }
11451+ if (nm && !strcmp(nm, "RUBY_COPYRIGHT")) { buf_puts(b, "SPL(\"ruby - Copyright (C) 1993-2023 Yukihiro Matsumoto\")"); return; }
1139011452 if (nm && !strcmp(nm, "ARGV")) { buf_puts(b, "sp_get_ARGV()"); return; }
1139111453 /* unregistered or untyped constant: emit a runtime NameError raise */
1139211454 if (nm && comp_class_index(c, nm) < 0)
@@ -13446,9 +13508,14 @@
1344613508 const char *nm = nt_str(nt, id, "name");
1344713509 int v = nt_ref(nt, id, "value");
1344813510 Scope *cws = comp_scope_of(c, id);
13511+ /* Ivar write inside instance_eval block: access ivar via receiver pointer. */
13512+ if (cws && cws->class_id < 0 && !cws->is_cmethod && g_ie_class_id >= 0) {
13513+ emit_indent(b, indent);
13514+ buf_printf(b, "%s->iv_%s = ", g_self, nm + 1);
13515+ }
1344913516 /* Ivar write in a class/module body (outside any def): write to the
1345013517 module-level civ_ variable. */
13451- if (cws && cws->class_id < 0 && !cws->is_cmethod && g_class_body_id >= 0) {
13518+ else if (cws && cws->class_id < 0 && !cws->is_cmethod && g_class_body_id >= 0) {
1345213519 emit_indent(b, indent);
1345313520 buf_printf(b, "civ_%s_%s = ", c->classes[g_class_body_id].name, nm + 1);
1345413521 }