Commit badd0ef6

badd0ef6695aMerged

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

Description

Boxed bound Methods in poly containers: box tag, bm[arg], poly bm.call

Add SP_BUILTIN_METHOD + sp_box_method so a Method stored in a poly array/ slot keeps its identity (was boxed to nil). sp_poly_arr_get_hash calls a boxed Method on bm[arg]; the poly .call dispatch branches Method vs Proc ABI at runtime.

Fixes method_dispatch_poly_array.

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

Details

Commit
badd0ef6695a
Parent
2631d800a7d8

lib/sp_runtime.hmodifiedView file

@@ -2740,6 +2740,7 @@
27402740 #define SP_BUILTIN_OBJECT (-21) /* Object.new identity sentinel */
27412741 #define SP_BUILTIN_FIBER (-22) /* sp_Fiber * boxed into poly slot */
27422742 #define SP_BUILTIN_IO (-23) /* sp_File * (File/IO handle) boxed into poly slot */
2743+#define SP_BUILTIN_METHOD (-24) /* sp_BoundMethod * boxed into poly slot */
27432744 /* sp_RbVal is defined in sp_gc.h (the mark helpers dispatch on its tag). */
27442745 static sp_RbVal sp_box_int(mrb_int v) { sp_RbVal r; r.tag = SP_TAG_INT; r.cls_id = 0; r.v.i = v; return r; }
27452746 static sp_RbVal sp_box_str(const char *v) { sp_RbVal r; r.tag = SP_TAG_STR; r.cls_id = 0; r.v.s = v; return r; }
@@ -2850,6 +2851,7 @@
28502851 static sp_RbVal sp_box_sym_array(void *p) { return sp_box_obj(p, SP_BUILTIN_SYM_ARRAY); }
28512852 static sp_RbVal sp_box_ptr_array(void *p) { return sp_box_obj(p, SP_BUILTIN_PTR_ARRAY); }
28522853 static sp_RbVal sp_box_proc(void *p) { return sp_box_obj(p, SP_BUILTIN_PROC); }
2854+static sp_RbVal sp_box_method(void *p) { return sp_box_obj(p, SP_BUILTIN_METHOD); }
28532855
28542856 /* CRuby-compatible Array#index / #rindex / #find_index: returns
28552857 sp_RbVal (nil tag for not-found, int tag with the position when
@@ -4008,6 +4010,7 @@
40084010 }
40094011 }
40104012 /* poly_arr_get/set for PolyPolyHash with integer index key. */
4013+typedef struct sp_BoundMethod { void *self; mrb_int fn; const char *name; } sp_BoundMethod;
40114014 static sp_RbVal sp_poly_arr_get_hash(sp_RbVal a, mrb_int i) {
40124015 if (a.tag == SP_TAG_INT) return sp_box_int((a.v.i >> i) & 1);
40134016 if (a.tag == SP_TAG_OBJ && a.cls_id == SP_BUILTIN_POLY_POLY_HASH)
@@ -4016,6 +4019,11 @@
40164019 return sp_SymPolyHash_get((sp_SymPolyHash*)a.v.p, (sp_sym)i);
40174020 if (a.tag == SP_TAG_OBJ && a.cls_id == SP_BUILTIN_STR_POLY_HASH)
40184021 return sp_StrPolyHash_get((sp_StrPolyHash*)a.v.p, sp_sym_name_fn ? sp_sym_name_fn((sp_sym)i) : "");
4022+ /* bm[arg]: a boxed bound Method called with the (single) int argument. */
4023+ if (a.tag == SP_TAG_OBJ && a.cls_id == SP_BUILTIN_METHOD) {
4024+ sp_BoundMethod *m = (sp_BoundMethod *)a.v.p;
4025+ return sp_box_int(((mrb_int (*)(void *, mrb_int))(uintptr_t)m->fn)((void *)m->self, i));
4026+ }
40194027 return sp_poly_arr_get(a, i);
40204028 }
40214029 /* poly[poly_key]: dispatch on key tag at runtime. */
@@ -5038,7 +5046,6 @@
50385046 bound receiver (NULL for a top-level method), `fn` the function address
50395047 (cast to the right signature at the call site), `name` the method name
50405048 (a string literal). Only `self` is GC-managed. */
5041-typedef struct sp_BoundMethod { void *self; mrb_int fn; const char *name; } sp_BoundMethod;
50425049 static void sp_BoundMethod_scan(void *p) { sp_BoundMethod *m = (sp_BoundMethod *)p; if (m->self) sp_gc_mark(m->self); }
50435050 static sp_BoundMethod *sp_bound_method_new(void *self, mrb_int fn, const char *name) { sp_BoundMethod *m = (sp_BoundMethod *)sp_gc_alloc(sizeof(sp_BoundMethod), NULL, sp_BoundMethod_scan); m->self = self; m->fn = fn; m->name = name; return m; }
50445051 static mrb_int sp_proc_arity(sp_Proc *p) { return p ? p->arity : 0; }