Commit 48144b27

48144b27853bMerged

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

Description

Object#class on a poly receiver

Type x.class as String when x is poly (analyze), and emit a sp_poly_class_name runtime helper that maps the boxed value's tag to its Ruby class name (Integer/String/Float/Symbol/NilClass/ TrueClass/FalseClass/Encoding/Class, builtin Array/Range/Time, and user objects via the class-names table).

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
48144b27853b
Parent
3ef0d029067c

lib/sp_runtime.hmodifiedView file

@@ -2771,6 +2771,30 @@
27712771 default: return sp_str_empty;
27722772 }
27732773 }
2774+/* Class name of a boxed value, for `x.class` where x is poly. Returns a
2775+ .rodata or names-table string (never GC-managed). */
2776+static const char *sp_poly_class_name(sp_RbVal v) {
2777+ switch (v.tag) {
2778+ case SP_TAG_INT: return SPL("Integer");
2779+ case SP_TAG_STR: return SPL("String");
2780+ case SP_TAG_FLT: return SPL("Float");
2781+ case SP_TAG_BOOL: return v.v.b ? SPL("TrueClass") : SPL("FalseClass");
2782+ case SP_TAG_NIL: return SPL("NilClass");
2783+ case SP_TAG_SYM: return SPL("Symbol");
2784+ case SP_TAG_ENCODING: return SPL("Encoding");
2785+ case SP_TAG_CLASS: return SPL("Class");
2786+ case SP_TAG_OBJ:
2787+ switch (v.cls_id) {
2788+ case SP_BUILTIN_INT_ARRAY: case SP_BUILTIN_FLT_ARRAY:
2789+ case SP_BUILTIN_STR_ARRAY: case SP_BUILTIN_SYM_ARRAY:
2790+ case SP_BUILTIN_PTR_ARRAY: return SPL("Array");
2791+ case SP_BUILTIN_RANGE: return SPL("Range");
2792+ case SP_BUILTIN_TIME: return SPL("Time");
2793+ default: { sp_Class c = {v.cls_id}; return sp_class_to_s(c); }
2794+ }
2795+ default: return SPL("Object");
2796+ }
2797+}
27742798 static sp_RbVal sp_poly_add(sp_RbVal a, sp_RbVal b) { if (a.tag == SP_TAG_INT && b.tag == SP_TAG_INT) return sp_box_int(a.v.i + b.v.i); if (a.tag == SP_TAG_FLT && b.tag == SP_TAG_FLT) return sp_box_float(a.v.f + b.v.f); if (a.tag == SP_TAG_INT && b.tag == SP_TAG_FLT) return sp_box_float((mrb_float)a.v.i + b.v.f); if (a.tag == SP_TAG_FLT && b.tag == SP_TAG_INT) return sp_box_float(a.v.f + (mrb_float)b.v.i); if (a.tag == SP_TAG_STR && b.tag == SP_TAG_STR) return sp_box_str(sp_str_concat(a.v.s, b.v.s)); return sp_box_int(0); }
27752799 static sp_RbVal sp_poly_sub(sp_RbVal a, sp_RbVal b) { if (a.tag == SP_TAG_INT && b.tag == SP_TAG_INT) return sp_box_int(a.v.i - b.v.i); if (a.tag == SP_TAG_FLT && b.tag == SP_TAG_FLT) return sp_box_float(a.v.f - b.v.f); return sp_box_int(0); }
27762800 static sp_RbVal sp_poly_mul(sp_RbVal a, sp_RbVal b) { if (a.tag == SP_TAG_INT && b.tag == SP_TAG_INT) return sp_box_int(a.v.i * b.v.i); if (a.tag == SP_TAG_FLT && b.tag == SP_TAG_FLT) return sp_box_float(a.v.f * b.v.f); if (a.tag == SP_TAG_INT && b.tag == SP_TAG_FLT) return sp_box_float((mrb_float)a.v.i * b.v.f); if (a.tag == SP_TAG_FLT && b.tag == SP_TAG_INT) return sp_box_float(a.v.f * (mrb_float)b.v.i); return sp_box_int(0); }