Commit 221be1a8

221be1a8b413Merged

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

Description

Add nil? on object types and include?/member? on poly receivers

  • nil? on user-defined object types always returns false (objects are never nil)
  • include?/member? on poly receiver: dispatch arms for IntArray/Range (int arg), StrArray/str-keyed hashes (str arg), SymArray/SymPolyHash (sym arg), and PolyArray (boxed fallback); TAG_STR pre-switch for string receiver with string arg

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

Details

Commit
221be1a8b413
Parent
d14c0ac1c7b4

src/codegen.cmodifiedView file

@@ -5031,13 +5031,14 @@
50315031 if (recv >= 0 && rt == TY_POLY && argc > 0) {
50325032 /* the builtin-array `[]` arm only applies to an integer index */
50335033 int is_index = !strcmp(name, "[]") && argc == 1 && comp_ntype(c, argv[0]) == TY_INT;
5034+ int is_include = (!strcmp(name, "include?") || !strcmp(name, "member?")) && argc == 1;
50345035 int ncand = 0;
50355036 for (int k = 0; k < c->nclasses; k++) {
50365037 int mi = comp_method_in_chain(c, k, name, NULL);
50375038 /* Include if call supplies all required params (pad defaults / truncate extras) */
50385039 if (mi >= 0 && argc >= c->scopes[mi].nrequired) ncand++;
50395040 }
5040- if (ncand > 0 || is_index) {
5041+ if (ncand > 0 || is_index || is_include) {
50415042 TyKind ret = comp_ntype(c, id);
50425043 int tv = ++g_tmp, tr = ++g_tmp;
50435044 int *atmp = malloc(sizeof(int) * argc);
@@ -5049,6 +5050,9 @@
50495050 }
50505051 emit_ctype(c, is_scalar_ret(ret) ? ret : TY_INT, b);
50515052 buf_printf(b, " _t%d = %s; ", tr, is_scalar_ret(ret) ? default_value(ret) : "0");
5053+ /* include? on a TAG_STR receiver: check tag before entering cls_id switch */
5054+ if (is_include && infer_type(c, argv[0]) == TY_STRING)
5055+ buf_printf(b, "if (_t%d.tag == SP_TAG_STR) { _t%d = sp_str_include(_t%d.v.s, _t%d); } else ", tv, tr, tv, atmp[0]);
50525056 buf_printf(b, "switch (_t%d.cls_id) {", tv);
50535057 for (int k = 0; k < c->nclasses; k++) {
50545058 int defcls = -1;
@@ -5103,6 +5107,32 @@
51035107 buf_printf(b, " case SP_BUILTIN_INT_ARRAY: _t%d = sp_IntArray_get((sp_IntArray *)_t%d.v.p, _t%d); break;", tr, tv, atmp[0]);
51045108 }
51055109 }
5110+ if (is_include) {
5111+ TyKind at = infer_type(c, argv[0]);
5112+ if (at == TY_INT) {
5113+ buf_printf(b, " case SP_BUILTIN_INT_ARRAY: _t%d = sp_IntArray_include((sp_IntArray *)_t%d.v.p, _t%d); break;", tr, tv, atmp[0]);
5114+ buf_printf(b, " case SP_BUILTIN_RANGE: _t%d = sp_range_include((sp_Range *)_t%d.v.p, _t%d); break;", tr, tv, atmp[0]);
5115+ }
5116+ else if (at == TY_STRING) {
5117+ buf_printf(b, " case SP_BUILTIN_STR_ARRAY: _t%d = sp_StrArray_include((sp_StrArray *)_t%d.v.p, _t%d); break;", tr, tv, atmp[0]);
5118+ buf_printf(b, " case SP_BUILTIN_STR_INT_HASH: _t%d = sp_StrIntHash_has_key((sp_StrIntHash *)_t%d.v.p, _t%d); break;", tr, tv, atmp[0]);
5119+ buf_printf(b, " case SP_BUILTIN_STR_STR_HASH: _t%d = sp_StrStrHash_has_key((sp_StrStrHash *)_t%d.v.p, _t%d); break;", tr, tv, atmp[0]);
5120+ buf_printf(b, " case SP_BUILTIN_STR_POLY_HASH: _t%d = sp_StrPolyHash_has_key((sp_StrPolyHash *)_t%d.v.p, _t%d); break;", tr, tv, atmp[0]);
5121+ }
5122+ else if (at == TY_SYMBOL) {
5123+ /* sym array is stored as IntArray (sp_sym == mrb_int) */
5124+ buf_printf(b, " case SP_BUILTIN_SYM_ARRAY: _t%d = sp_IntArray_include((sp_IntArray *)_t%d.v.p, _t%d); break;", tr, tv, atmp[0]);
5125+ buf_printf(b, " case SP_BUILTIN_SYM_POLY_HASH: _t%d = sp_SymPolyHash_has_key((sp_SymPolyHash *)_t%d.v.p, _t%d); break;", tr, tv, atmp[0]);
5126+ }
5127+ /* PolyArray: box the arg for runtime comparison */
5128+ {
5129+ int tbox = ++g_tmp;
5130+ buf_printf(b, " case SP_BUILTIN_POLY_ARRAY: { sp_RbVal _t%d = ", tbox);
5131+ char tn[32]; snprintf(tn, sizeof tn, "_t%d", atmp[0]);
5132+ emit_boxed_text(c, at, tn, b);
5133+ buf_printf(b, "; _t%d = sp_PolyArray_include((sp_PolyArray *)_t%d.v.p, _t%d); break; }", tr, tv, tbox);
5134+ }
5135+ }
51065136 /* the poly value may actually be a string-keyed hash: dispatch `[]` /
51075137 `fetch` to the matching hash storage, boxing the value into the poly
51085138 result. */
@@ -7072,6 +7102,11 @@
70727102 if (recv >= 0 && argc == 0 && !strcmp(name, "to_s")) {
70737103 buf_puts(b, "\"\""); return;
70747104 }
7105+ /* nil? on an object type: objects are never nil */
7106+ if (recv >= 0 && argc == 0 && !strcmp(name, "nil?") && ty_is_object(rt)) {
7107+ buf_puts(b, "((void)("); emit_expr(c, recv, b); buf_puts(b, "), 0)");
7108+ return;
7109+ }
70757110
70767111 unsupported(c, id, "call");
70777112 }