Commit 69d70c81

69d70c813157Merged

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

Description

Merge pull request #155 from ryanseys/rs-string-each-byte

test(codegen): pin String#each_byte byte-level iteration

Details

Commit
69d70c813157
Parent
4c33106cd653
Merge parent
c3c367ce9094

spinel_codegen.rbmodifiedView file

@@ -2255,6 +2255,17 @@
22552255 end
22562256 return "int"
22572257 end
2258+ # String#each_byte returns the receiver per CRuby. The block-bearing
2259+ # form is handled in compile_string_method_expr; the inference rule
2260+ # here is what makes `ret = "hi".each_byte { ... }` typed as string.
2261+ if mname == "each_byte"
2262+ if recv >= 0 && @nd_block[nid] >= 0
2263+ rt = infer_type(recv)
2264+ if rt == "string" || rt == "mutable_str"
2265+ return rt
2266+ end
2267+ end
2268+ end
22582269 if mname == "then" || mname == "yield_self"
22592270 # Return type is the block's return type. Bind the block param to
22602271 # the receiver's type so infer_type sees the inner shadow, not any
@@ -16205,6 +16216,30 @@
1620516216 end
1620616217
1620716218 def compile_string_method_expr(nid, mname, rc)
16219+ # String#each_byte returns the receiver per CRuby. The statement-level
16220+ # handler at compile_block_iteration_stmt emits the loop for `do …
16221+ # end` / `{ … }` with no captured value; the expression-level form
16222+ # (e.g. `ret = "hi".each_byte { ... }`) needs to produce the
16223+ # receiver. Same loop body, just emit-then-return-rc.
16224+ if mname == "each_byte" && @nd_block[nid] >= 0
16225+ bp = get_block_param(nid, 0)
16226+ if bp == ""
16227+ bp = "_b"
16228+ end
16229+ itmp = new_temp
16230+ src_tmp = new_temp
16231+ emit(" const char *" + src_tmp + " = " + rc + ";")
16232+ emit(" for (mrb_int " + itmp + " = 0; " + src_tmp + "[" + itmp + "]; " + itmp + "++) {")
16233+ emit(" mrb_int lv_" + bp + " = (unsigned char)" + src_tmp + "[" + itmp + "];")
16234+ @indent = @indent + 1
16235+ push_scope
16236+ declare_var(bp, "int")
16237+ compile_stmts_body(@nd_body[@nd_block[nid]])
16238+ pop_scope
16239+ @indent = @indent - 1
16240+ emit(" }")
16241+ return rc
16242+ end
1620816243 if mname == "length"
1620916244 # Only use hoisted length if the receiver matches (otherwise we'd
1621016245 # return the wrong string's length).