Commit 9db1a30d

9db1a30d1619Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(codegen): box mutable_str into poly as SP_TAG_STR not SP_TAG_OBJ/0

Sam Ruby's #541 (roundhouse real-blog returning HTTP 200 with empty body). View functions returning sp_String * flowed into a body slot that had been widened to poly across unrelated classes (Article.body holds a String, Comment.body holds a String, ArticleParams.body holds something else, etc.), so the analyzer pinned the slot's effective type as poly. At the call site the box went through box_non_nullable_value_to_poly("mutable_str", val), which fell into the catch-all type_is_pointer arm:

return "sp_box_obj((void *)(val), 0)"

Generic SP_TAG_OBJ box with cls_id 0 (BasicObject). Downstream dispatch (Tep::Server#write_response in roundhouse, computing Content-Length and writing the body to the wire) had no arm matching SP_TAG_OBJ + cls_id 0, so the body was silently dropped -- 200 OK with Content-Length: 0.

Add a mutable_str arm in box_non_nullable_value_to_poly before the type_is_pointer catch-all. Box through sp_box_str(s->data) (SP_TAG_STR with the underlying char* buffer); consumers reading v.v.s on the tag find the string content. The statement-expression temp holds the recv so side effects fire exactly once; the ? : sp_box_nil() arm handles the NULL recv (shouldn't happen for mutable_str but matches the sp_box_nullable_str shape).

Sam noted in #541 he tried 5 minimal shapes that didn't reproduce; my test/mutable_str_into_poly_slot.rb does reproduce by widening body across two unrelated classes, mirroring the real-blog dataset's per-class same-named slot collision. The fix-on-master output matches CRuby: "<h1>title</h1><p>body</p>" instead of 0 / empty.

Closes #541

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

Details

Commit
9db1a30d1619
Parent
35122ae5b78d

spinel_codegen.rbmodifiedView file

@@ -20106,8 +20106,19 @@
2010620106 if hcid != ""
2010720107 return "sp_box_obj((void *)(" + val + "), " + hcid + ")"
2010820108 end
20109- # Other pointer types (mutable strings, etc.) — box with a
20110- # neutral cls_id of 0 rather than truncating the pointer to int.
20109+ # A `sp_String *` (mutable_str) is a heap buffer with a `data`
20110+ # field of const char *. Box it as a string (SP_TAG_STR) so
20111+ # consumers that dispatch on the tag (sp_poly_to_s, write_response
20112+ # in roundhouse's Tep::Server, etc.) read the string content
20113+ # instead of falling through to the SP_TAG_OBJ cls_id=0 case
20114+ # which matches no arm. Issue #541. Spill the recv to a temp via
20115+ # a statement-expression so side effects fire exactly once.
20116+ if at == "mutable_str"
20117+ mtmp = new_temp
20118+ return "({ sp_String *" + mtmp + " = " + val + "; " + mtmp + " ? sp_box_str(" + mtmp + "->data) : sp_box_nil(); })"
20119+ end
20120+ # Other pointer types — box with a neutral cls_id of 0 rather
20121+ # than truncating the pointer to int.
2011120122 if type_is_pointer(at) == 1
2011220123 return "sp_box_obj((void *)(" + val + "), 0)"
2011320124 end