Commit 6b49ae9f

6b49ae9ff07cMerged

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

Description

Module#const_defined? presence check

Decide Class.const_defined?(:K) at compile time. Constants live in a flat namespace, so this consults the global const and class tables for the queried name rather than the receiver's own constants.

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
6b49ae9ff07c
Parent
854c48d915f8

src/codegen.cmodifiedView file

@@ -1655,6 +1655,22 @@
16551655 }
16561656 }
16571657
1658+ /* Class.const_defined?(:K): compile-time presence check. Constants are
1659+ recorded in a flat namespace, so this consults the global const and class
1660+ tables rather than the receiver's own constants. */
1661+ if (!strcmp(name, "const_defined?") && recv >= 0 && argc >= 1 &&
1662+ nt_type(nt, recv) && !strcmp(nt_type(nt, recv), "ConstantReadNode")) {
1663+ const char *aty = nt_type(nt, argv[0]);
1664+ const char *qm = NULL;
1665+ if (aty && !strcmp(aty, "SymbolNode")) qm = nt_str(nt, argv[0], "value");
1666+ else if (aty && !strcmp(aty, "StringNode")) qm = nt_str(nt, argv[0], "content");
1667+ if (qm) {
1668+ int yes = comp_const(c, qm) != NULL || comp_class_index(c, qm) >= 0;
1669+ buf_printf(b, "%d", yes);
1670+ return;
1671+ }
1672+ }
1673+
16581674 if ((!strcmp(name, "-@") || !strcmp(name, "+@")) && recv >= 0 && argc == 0 && !ty_is_object(rt)) {
16591675 buf_puts(b, name[0] == '-' ? "(-" : "(+");
16601676 emit_expr(c, recv, b); buf_puts(b, ")");