Commit 238b0976
238b097627fdMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified
Description
Call a bound Method through the two-arg poly index (store dispatch)
optcarrot's CPU memory write is @store[addr][addr, value]: @store[addr] is a bound Method (poke handler) and [addr, value] is Method#[] with two args. codegen lowers poly[a, b] to sp_poly_slice, which ignored SP_BUILTIN_METHOD and returned nil, dropping every register write. Add a Method arm to sp_poly_slice (additive; mirrors the one-arg sp_poly_arr_get_hash path) and coerce both index operands to int at the call site. Move the sp_BoundMethod typedef above sp_poly_slice so it is in scope (pure reordering).
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 238b097627fd
- Parent
- 317c657bd756
lib/sp_runtime.hmodifiedView file
@@ -3377,9 +3377,16 @@
33773377
static void sp_PolyArray_set(sp_PolyArray *a, mrb_int i, sp_RbVal v) { if (!a) return; if (a->frozen) { sp_raise_frozen_array(); return; } mrb_int orig=i; if (i < 0) i += a->len; if (i < 0) sp_raise_cls("IndexError", sp_sprintf("index %lld too small for array; minimum: %lld",(long long)orig,(long long)-a->len)); if (i >= a->len) return; a->data[i] = v; }33783378
static sp_PolyArray *sp_PolyArray_slice(sp_PolyArray *a, mrb_int start, mrb_int len) { if (start < 0) start += a->len; if (start < 0) start = 0; sp_PolyArray *b = sp_PolyArray_new(); if (start >= a->len || len <= 0) return b; if (start + len > a->len) len = a->len - start; for (mrb_int i = 0; i < len; i++) sp_PolyArray_push(b, a->data[start + i]); return b; }33793379
/* 2-arg slice on a poly receiver: dispatch to the typed slice functions. */3380+
typedef struct sp_BoundMethod { void *self; mrb_int fn; const char *name; } sp_BoundMethod;33803381
static sp_RbVal sp_poly_slice(sp_RbVal a, mrb_int start, mrb_int len) {33813382
if (a.tag == SP_TAG_STR) return sp_box_str(sp_str_sub_range(a.v.s ? a.v.s : "", start, len));33823383
if (a.tag != SP_TAG_OBJ) return sp_box_nil();3384+
/* bm[a, b]: a boxed bound Method called with two int arguments (optcarrot's3385+
store dispatch table: `@store[addr][addr, value]`). */3386+
if (a.cls_id == SP_BUILTIN_METHOD) {3387+
sp_BoundMethod *m = (sp_BoundMethod *)a.v.p;3388+
return sp_box_int(((mrb_int (*)(void *, mrb_int, mrb_int))(uintptr_t)m->fn)((void *)m->self, start, len));3389+
}33833390
switch (a.cls_id) {33843391
case SP_BUILTIN_INT_ARRAY: return sp_box_int_array(sp_IntArray_slice((sp_IntArray*)a.v.p, start, len));33853392
case SP_BUILTIN_FLT_ARRAY: return sp_box_float_array(sp_FloatArray_slice((sp_FloatArray*)a.v.p, start, len));@@ -3971,7 +3978,6 @@
39713978
typedef mrb_bool (*sp_obj_eql_fn)(int cls_id, void *a, void *b);39723979
static sp_obj_hash_fn sp_obj_hash_hook = NULL;39733980
static sp_obj_eql_fn sp_obj_eql_hook = NULL;3974-
typedef struct sp_BoundMethod { void *self; mrb_int fn; const char *name; } sp_BoundMethod;39753981
static mrb_int sp_rbval_hash_key(sp_RbVal v) {39763982
switch (v.tag) {39773983
case SP_TAG_INT: case SP_TAG_BOOL: case SP_TAG_NIL: case SP_TAG_SYM:src/codegen_call.cmodifiedView file
Loading diff…