Commit e2ea3cf3
e2ea3cf3574eMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
Merge pull request #362 from mame/pr/poly-shl-array-push
fix(runtime): sp_poly_shl dispatches Array#<< push by recv cls_id
Details
- Commit
- e2ea3cf3574e
- Parent
- 1073bb9e7314
- Merge parent
- 678580f9611c
lib/sp_runtime.hmodifiedView file
@@ -919,7 +919,10 @@
919919
static sp_RbVal sp_poly_div(sp_RbVal a, sp_RbVal b) { if (a.tag == SP_TAG_FLT || b.tag == SP_TAG_FLT) return sp_box_float(sp_poly_to_f(a) / sp_poly_to_f(b)); return sp_box_int(sp_idiv(sp_poly_to_i(a), sp_poly_to_i(b))); }920920
static sp_RbVal sp_poly_mod(sp_RbVal a, sp_RbVal b) { if (a.tag == SP_TAG_FLT || b.tag == SP_TAG_FLT) return sp_box_float(fmod(sp_poly_to_f(a), sp_poly_to_f(b))); return sp_box_int(sp_imod(sp_poly_to_i(a), sp_poly_to_i(b))); }921921
static sp_RbVal sp_poly_pow(sp_RbVal a, sp_RbVal b) { double r = pow((double)sp_poly_to_f(a), (double)sp_poly_to_f(b)); if (a.tag == SP_TAG_INT && b.tag == SP_TAG_INT && b.v.i >= 0) return sp_box_int((mrb_int)r); return sp_box_float((mrb_float)r); }922-
static sp_RbVal sp_poly_shl(sp_RbVal a, sp_RbVal b) { return sp_box_int(sp_poly_to_i(a) << sp_poly_to_i(b)); }922+
/* sp_poly_shl is defined after sp_PolyArray_push (below) so the923+
push-dispatch path can call it directly. The Integer-bit-shift924+
semantics fall through when the recv isn't an array. */925+
static sp_RbVal sp_poly_shl(sp_RbVal a, sp_RbVal b);923926
static sp_RbVal sp_poly_shr(sp_RbVal a, sp_RbVal b) { return sp_box_int(sp_poly_to_i(a) >> sp_poly_to_i(b)); }924927
static sp_RbVal sp_poly_band(sp_RbVal a, sp_RbVal b) { return sp_box_int(sp_poly_to_i(a) & sp_poly_to_i(b)); }925928
static sp_RbVal sp_poly_bor(sp_RbVal a, sp_RbVal b) { return sp_box_int(sp_poly_to_i(a) | sp_poly_to_i(b)); }@@ -933,6 +936,35 @@
933936
static void sp_PolyArray_fin(void *p) { sp_PolyArray *a = (sp_PolyArray *)p; sp_gc_hdr *h = (sp_gc_hdr *)((char *)a - sizeof(sp_gc_hdr)); sp_gc_bytes -= sizeof(sp_RbVal) * a->cap; h->size -= sizeof(sp_RbVal) * a->cap; free(a->data); }934937
static sp_PolyArray *sp_PolyArray_new(void) { sp_PolyArray *a = (sp_PolyArray *)sp_gc_alloc(sizeof(sp_PolyArray), sp_PolyArray_fin, sp_PolyArray_scan); a->cap = 16; a->data = (sp_RbVal *)malloc(sizeof(sp_RbVal) * a->cap); a->len = 0; { sp_gc_hdr *h = (sp_gc_hdr *)((char *)a - sizeof(sp_gc_hdr)); h->size += sizeof(sp_RbVal) * a->cap; sp_gc_bytes += sizeof(sp_RbVal) * a->cap; } return a; }935938
static void sp_PolyArray_push(sp_PolyArray *a, sp_RbVal v) { if (a->len >= a->cap) { sp_gc_hdr *h = (sp_gc_hdr *)((char *)a - sizeof(sp_gc_hdr)); sp_gc_bytes -= sizeof(sp_RbVal) * a->cap; h->size -= sizeof(sp_RbVal) * a->cap; a->cap = a->cap * 2 + 1; a->data = (sp_RbVal *)realloc(a->data, sizeof(sp_RbVal) * a->cap); h->size += sizeof(sp_RbVal) * a->cap; sp_gc_bytes += sizeof(sp_RbVal) * a->cap; } a->data[a->len++] = v; }939+
static sp_RbVal sp_poly_shl(sp_RbVal a, sp_RbVal b) {940+
/* Dispatch by recv cls_id: an IntArray / PolyArray / etc. boxed941+
into a poly slot still wants Array#<< (push), not Integer#<<942+
(bit-shift). Falls through to bit-shift only when the recv is943+
a non-array. Returns the recv (matching `<<`s chainability). */944+
if (a.tag == SP_TAG_OBJ) {945+
if (a.cls_id == SP_BUILTIN_INT_ARRAY) {946+
sp_IntArray_push((sp_IntArray *)a.v.p, b.tag == SP_TAG_INT ? b.v.i : sp_poly_to_i(b));947+
return a;948+
}949+
if (a.cls_id == SP_BUILTIN_POLY_ARRAY) {950+
sp_PolyArray_push((sp_PolyArray *)a.v.p, b);951+
return a;952+
}953+
if (a.cls_id == SP_BUILTIN_PTR_ARRAY) {954+
sp_PtrArray_push((sp_PtrArray *)a.v.p, b.v.p);955+
return a;956+
}957+
if (a.cls_id == SP_BUILTIN_FLT_ARRAY) {958+
sp_FloatArray_push((sp_FloatArray *)a.v.p, b.tag == SP_TAG_FLT ? b.v.f : (mrb_float)sp_poly_to_i(b));959+
return a;960+
}961+
if (a.cls_id == SP_BUILTIN_STR_ARRAY) {962+
sp_StrArray_push((sp_StrArray *)a.v.p, b.tag == SP_TAG_STR ? (const char *)b.v.p : "");963+
return a;964+
}965+
}966+
return sp_box_int(sp_poly_to_i(a) << sp_poly_to_i(b));967+
}936968
static mrb_int sp_PolyArray_length(sp_PolyArray *a) { return a->len; }937969
static sp_RbVal sp_PolyArray_get(sp_PolyArray *a, mrb_int i) { if (i < 0) i += a->len; return a->data[i]; }938970
static void sp_PolyArray_set(sp_PolyArray *a, mrb_int i, sp_RbVal v) { if (i < 0) i += a->len; a->data[i] = v; }test/poly_shl_array_push_dispatch.rbaddedView file
Loading diff…
test/poly_shl_array_push_dispatch.rb.expectedaddedView file
Loading diff…