Commit 35ac35f2

35ac35f26089Merged

Yusuke Endoh <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

fix(codegen): box obj_X_ptr_array via sp_box_ptr_array in box_expr_to_poly

box_expr_to_poly checked is_obj_type before is_ptr_array_type. Both predicates fire on obj_<Class>_ptr_array (it both starts with obj_ and ends with _ptr_array), so the is_obj_type branch ran first, fed <Class>_ptr_array to find_class_idx (which returned -1 for "no such class"), and emitted

sp_box_obj(p, -1)

-1 happens to equal SP_BUILTIN_INT_ARRAY, so a PtrArray boxed into a poly slot was mis-tagged as an IntArray at runtime and subsequent dispatch fell into the wrong branch.

Reorder so the built-in pointer types (int_array, float_array, str_array, sym_array, *_ptr_array via is_ptr_array_type) are checked before the catch-all is_obj_type branch. Mirrors the ordering already used by box_value_to_poly and box_val_to_poly (they were correct from the start; only box_expr_to_poly had this oversight).

Without this, the dispatch added by the previous commit (fix(codegen): dispatch [] on poly recv that's a PtrArray) is unreachable — the runtime cls_id never lands on SP_BUILTIN_PTR_ARRAY.

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

Details

Commit
35ac35f26089
Parent
e96344d85616

spinel_codegen.rbmodifiedView file

@@ -19327,13 +19327,13 @@
1932719327 if at == "symbol"
1932819328 return "sp_box_sym(" + val + ")"
1932919329 end
19330- if is_obj_type(at) == 1
19331- cname = at[4, at.length - 4]
19332- ci = find_class_idx(cname)
19333- return "sp_box_obj(" + val + ", " + ci.to_s + ")"
19334- end
1933519330 # Built-in pointer types: route through sp_box_obj with a reserved
19336- # negative cls_id (SP_BUILTIN_*) so dispatch is uniform.
19331+ # negative cls_id (SP_BUILTIN_*) so dispatch is uniform. These
19332+ # checks run BEFORE is_obj_type because an `obj_X_ptr_array` type
19333+ # tag both starts with "obj_" and ends with "_ptr_array" — the
19334+ # is_obj_type branch would otherwise feed a "Foo_ptr_array" name
19335+ # to find_class_idx, get -1 (= SP_BUILTIN_INT_ARRAY by accident),
19336+ # and emit `sp_box_obj(p, -1)` which mis-tags the runtime value.
1933719337 if at == "int_array"
1933819338 return "sp_box_int_array(" + val + ")"
1933919339 end
@@ -19349,6 +19349,11 @@
1934919349 if is_ptr_array_type(at) == 1
1935019350 return "sp_box_ptr_array(" + val + ")"
1935119351 end
19352+ if is_obj_type(at) == 1
19353+ cname = at[4, at.length - 4]
19354+ ci = find_class_idx(cname)
19355+ return "sp_box_obj(" + val + ", " + ci.to_s + ")"
19356+ end
1935219357 if at == "proc" || at == "lambda"
1935319358 return "sp_box_proc(" + val + ")"
1935419359 end