Commit 36ad51ef

36ad51efee59Merged

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

Description

Typed-array method capture: per-(type, op) Method ABI adapters

<int_array>.method(:[]) / :[]= / :push previously produced a Method with fn=0 (no callable address), so invoking the captured Method crashed. The method(:sym) emitter now lowers these through memoized per-(kind, op) adapter functions matching the Method dispatch ABI (mrb_int fn(void *self, args...)), covering optcarrot's add_mappings(.., @ram, @ram.method(:[]=)) shape for int and string arrays.

Fixes builtin_array_method_capture.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
36ad51efee59
Parent
74d63c93e2aa

src/codegen.cmodifiedView file

@@ -4250,7 +4250,49 @@
42504250 else buf_puts(b, "NULL");
42514251 buf_puts(b, ", ");
42524252 if (mi >= 0) { buf_puts(b, "(mrb_int)(uintptr_t)&"); emit_method_cname(c, &c->scopes[mi], b); }
4253- else buf_puts(b, "(mrb_int)0"); /* builtin/Kernel method: no callable address */
4253+ else {
4254+ /* `<typed_array>.method(:op)`: lower through a per-(type, op) adapter
4255+ matching the Method dispatch ABI (optcarrot's
4256+ `add_mappings(.., @ram, @ram.method(:[]=))` shape). */
4257+ TyKind brt = recv >= 0 ? comp_ntype(c, recv) : TY_UNKNOWN;
4258+ const char *bk = ty_is_array(brt) ? array_kind(brt) : NULL;
4259+ const char *bop = NULL;
4260+ if (bk && (brt == TY_INT_ARRAY || brt == TY_STR_ARRAY)) {
4261+ if (!strcmp(sym, "[]")) bop = "get";
4262+ else if (!strcmp(sym, "[]=")) bop = "set";
4263+ else if (!strcmp(sym, "push")) bop = "push";
4264+ }
4265+ if (bop) {
4266+ /* memoized per (kind, op): emit the adapter once */
4267+ static char bam_done[2][3];
4268+ int ki = (brt == TY_INT_ARRAY) ? 0 : 1;
4269+ int oi = bop[0] == 'g' ? 0 : bop[0] == 's' ? 1 : 2;
4270+ if (!bam_done[ki][oi]) {
4271+ bam_done[ki][oi] = 1;
4272+ const char *et = (ki == 0) ? "mrb_int" : "const char *";
4273+ const char *cast = (ki == 0) ? "" : "(mrb_int)(uintptr_t)";
4274+ const char *uncast = (ki == 0) ? "" : "(const char *)(uintptr_t)";
4275+ if (oi == 0) {
4276+ buf_printf(&g_proc_protos, "static mrb_int _bam_%sArray_get(void *a, mrb_int i);\n", bk);
4277+ buf_printf(&g_procs, "static mrb_int _bam_%sArray_get(void *a, mrb_int i) {\n"
4278+ " return %ssp_%sArray_get((sp_%sArray *)a, i);\n}\n", bk, cast, bk, bk);
4279+ }
4280+ else if (oi == 1) {
4281+ buf_printf(&g_proc_protos, "static mrb_int _bam_%sArray_set(void *a, mrb_int i, mrb_int v);\n", bk);
4282+ buf_printf(&g_procs, "static mrb_int _bam_%sArray_set(void *a, mrb_int i, mrb_int v) {\n"
4283+ " sp_%sArray_set((sp_%sArray *)a, i, %sv);\n return v;\n}\n", bk, bk, bk, uncast);
4284+ }
4285+ else {
4286+ buf_printf(&g_proc_protos, "static mrb_int _bam_%sArray_push(void *a, mrb_int v);\n", bk);
4287+ buf_printf(&g_procs, "static mrb_int _bam_%sArray_push(void *a, mrb_int v) {\n"
4288+ " sp_%sArray_push((sp_%sArray *)a, %sv);\n return (mrb_int)(uintptr_t)a;\n}\n", bk, bk, bk, uncast);
4289+ }
4290+ (void)et;
4291+ }
4292+ buf_printf(b, "(mrb_int)(uintptr_t)&_bam_%sArray_%s", bk, bop);
4293+ }
4294+ else buf_puts(b, "(mrb_int)0"); /* builtin/Kernel method: no callable address */
4295+ }
42544296 buf_puts(b, ", ");
42554297 emit_str_literal(b, sym);
42564298 buf_puts(b, ")");