Commit 78396742
7839674216edMerged
Yusuke Endoh <[email protected]> · 6 months ago · 3 files · +1 added · ~2 modified
Description
feat(codegen): box built-in pointer types into poly via reserved cls_id
box_expr_to_poly only knew how to box scalars and obj_<UserClass>
values. Anything else fell through to sp_box_int(val), which
silently miscompiled when val was a pointer — passing a sp_IntArray *
where sp_box_int wants mrb_int is a C type error, so this program
didn't compile:
def kind_of(a); a.nil? ? "nil" : "something"; end puts kind_of(Object.new) puts kind_of([1, 2, 3])
Rather than introduce one new SP_TAG_* per built-in pointer type
(which doesn't scale — Array<Float>, Array<Array<int>> etc. all
need their own slot), reserve a small set of negative cls_id
constants under the existing SP_TAG_OBJ:
SP_BUILTIN_INT_ARRAY (-1) SP_BUILTIN_FLOAT_ARRAY (-2) SP_BUILTIN_STR_ARRAY (-3) SP_BUILTIN_SYM_ARRAY (-4) SP_BUILTIN_PTR_ARRAY (-5)
with thin sp_box_int_array(p) etc. wrappers that delegate to
sp_box_obj(p, SP_BUILTIN_*). box_expr_to_poly now picks the
matching wrapper for these types. Adding more built-ins is one new
constant + one wrapper.
This commit lets a poly-typed parameter accept built-in array values; dispatching methods on a poly value whose tag is a built-in is the follow-up.
Adds test/poly_box_int_array.rb as a small reproducer.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 7839674216ed
- Parent
- 3936afd5b0ed
lib/sp_runtime.hmodifiedView file
#define SP_TAG_NIL 4#define SP_TAG_OBJ 5#define SP_TAG_SYM 6/* Negative cls_id values let SP_TAG_OBJ also carry built-in pointer types (IntArray, FloatArray, ...) — avoids minting a new SP_TAG_* per type. Non-negative cls_id stays an index into the user-class table as before. The element-type tag and the array cls_id are paired by `array_cls_id = -element_tag - 1`. */#define SP_BUILTIN_ARRAY_OF(tag) (-(tag) - 1)#define SP_BUILTIN_INT_ARRAY SP_BUILTIN_ARRAY_OF(SP_TAG_INT) /* -1 */#define SP_BUILTIN_STR_ARRAY SP_BUILTIN_ARRAY_OF(SP_TAG_STR) /* -2 */#define SP_BUILTIN_FLT_ARRAY SP_BUILTIN_ARRAY_OF(SP_TAG_FLT) /* -3 */#define SP_BUILTIN_PTR_ARRAY SP_BUILTIN_ARRAY_OF(SP_TAG_OBJ) /* -6 */#define SP_BUILTIN_SYM_ARRAY SP_BUILTIN_ARRAY_OF(SP_TAG_SYM) /* -7 */typedef struct { int tag; int cls_id; union { mrb_int i; const char *s; mrb_float f; mrb_bool b; void *p; } v; } sp_RbVal;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; }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; }static sp_RbVal sp_box_nil(void) { sp_RbVal r; r.tag = SP_TAG_NIL; r.cls_id = 0; r.v.i = 0; return r; }static sp_RbVal sp_box_obj(void *p, int cls_id) { sp_RbVal r; r.tag = SP_TAG_OBJ; r.cls_id = cls_id; r.v.p = p; return r; }static sp_RbVal sp_box_sym(sp_sym v) { sp_RbVal r; r.tag = SP_TAG_SYM; r.cls_id = 0; r.v.i = (mrb_int)v; return r; }/* Built-in pointer boxes — share SP_TAG_OBJ with a reserved negative cls_id so the dispatch path is uniform. */static sp_RbVal sp_box_int_array(void *p) { return sp_box_obj(p, SP_BUILTIN_INT_ARRAY); }static sp_RbVal sp_box_float_array(void *p) { return sp_box_obj(p, SP_BUILTIN_FLT_ARRAY); }static sp_RbVal sp_box_str_array(void *p) { return sp_box_obj(p, SP_BUILTIN_STR_ARRAY); }static sp_RbVal sp_box_sym_array(void *p) { return sp_box_obj(p, SP_BUILTIN_SYM_ARRAY); }static sp_RbVal sp_box_ptr_array(void *p) { return sp_box_obj(p, SP_BUILTIN_PTR_ARRAY); }static void sp_poly_puts(sp_RbVal v) { switch (v.tag) { case SP_TAG_INT: printf("%lld\n", (long long)v.v.i); break;spinel_codegen.rbmodifiedView file
Loading diff…
test/poly_box_int_array.rbaddedView file
Loading diff…