Commit 7260fbba

7260fbba568cMerged

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

Description

Bound Method objects (TY_METHOD): method(:sym), .call, .name, .class

Add a first-class bound-Method type backed by a runtime sp_BoundMethod {self, fn, name}. method(:sym) / obj.method(:sym) / self method(:sym) create one; .call/[] invokes it (top-level refs call the function directly, object-bound methods cast fn through the (void*self,mrb_int...) ABI with single receiver eval); .name reads the name; .class is Method.

Methods reached only via method(:sym) are kept reachable (their symbol counts as a use) and, when the bound ABI is their only caller, their untyped params default to the int ABI instead of being dropped. .call arg types bind to the target method's params.

Fixes method, i960, bound_method_single_eval, bound_method_gc. The poly-array Method dispatch and builtin-array adapters (obj_recv case c, method_dispatch_poly_array, builtin_array_method_capture) remain.

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

Details

Commit
7260fbba568c
Parent
7d48dd3bf979

lib/sp_runtime.hmodifiedView file

@@ -5031,6 +5031,14 @@
50315031 static void sp_Proc_scan(void *p) { sp_Proc *pr = (sp_Proc *)p; if (pr->cap && pr->cap_scan) pr->cap_scan(pr->cap); }
50325032 static sp_Proc *sp_proc_new_meta(void *fn, void *cap, void (*cap_scan)(void *), mrb_int arity, mrb_bool lambda_p, mrb_int param_count, const sp_sym *param_kinds, const sp_sym *param_names) { sp_Proc *p = (sp_Proc *)sp_gc_alloc(sizeof(sp_Proc), NULL, sp_Proc_scan); p->fn = fn; p->cap = cap; p->cap_scan = cap_scan; p->arity = arity; p->lambda_p = lambda_p; p->param_count = param_count; p->param_kinds = param_kinds; p->param_names = param_names; return p; }
50335033 static sp_Proc *sp_proc_new(void *fn, void *cap, void (*cap_scan)(void *)) { return sp_proc_new_meta(fn, cap, cap_scan, 0, FALSE, 0, NULL, NULL); }
5034+
5035+/* Bound Method object: `obj.method(:foo)` / `method(:foo)`. `self` is the
5036+ bound receiver (NULL for a top-level method), `fn` the function address
5037+ (cast to the right signature at the call site), `name` the method name
5038+ (a string literal). Only `self` is GC-managed. */
5039+typedef struct sp_BoundMethod { void *self; mrb_int fn; const char *name; } sp_BoundMethod;
5040+static void sp_BoundMethod_scan(void *p) { sp_BoundMethod *m = (sp_BoundMethod *)p; if (m->self) sp_gc_mark(m->self); }
5041+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; }
50345042 static mrb_int sp_proc_arity(sp_Proc *p) { return p ? p->arity : 0; }
50355043 static mrb_bool sp_proc_lambda_p(sp_Proc *p) { return p ? p->lambda_p : FALSE; }
50365044 static mrb_int sp_proc_call(sp_Proc *p, mrb_int *args) { if (!p || !p->fn) return 0; if (!args) { mrb_int noargs[16] = {0}; return ((mrb_int (*)(void *, mrb_int *))p->fn)(p->cap, noargs); } return ((mrb_int (*)(void *, mrb_int *))p->fn)(p->cap, args); }