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
end22562256
return "int"22572257
end2258+
# String#each_byte returns the receiver per CRuby. The block-bearing2259+
# form is handled in compile_string_method_expr; the inference rule2260+
# here is what makes `ret = "hi".each_byte { ... }` typed as string.2261+
if mname == "each_byte"2262+
if recv >= 0 && @nd_block[nid] >= 02263+
rt = infer_type(recv)2264+
if rt == "string" || rt == "mutable_str"2265+
return rt2266+
end2267+
end2268+
end22582269
if mname == "then" || mname == "yield_self"22592270
# Return type is the block's return type. Bind the block param to22602271
# the receiver's type so infer_type sees the inner shadow, not any@@ -16205,6 +16216,30 @@
1620516216
end1620616217
1620716218
def compile_string_method_expr(nid, mname, rc)16219+
# String#each_byte returns the receiver per CRuby. The statement-level16220+
# handler at compile_block_iteration_stmt emits the loop for `do …16221+
# end` / `{ … }` with no captured value; the expression-level form16222+
# (e.g. `ret = "hi".each_byte { ... }`) needs to produce the16223+
# receiver. Same loop body, just emit-then-return-rc.16224+
if mname == "each_byte" && @nd_block[nid] >= 016225+
bp = get_block_param(nid, 0)16226+
if bp == ""16227+
bp = "_b"16228+
end16229+
itmp = new_temp16230+
src_tmp = new_temp16231+
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 + 116235+
push_scope16236+
declare_var(bp, "int")16237+
compile_stmts_body(@nd_body[@nd_block[nid]])16238+
pop_scope16239+
@indent = @indent - 116240+
emit(" }")16241+
return rc16242+
end1620816243
if mname == "length"1620916244
# Only use hoisted length if the receiver matches (otherwise we'd1621016245
# return the wrong string's length).test/string_each_byte.rbaddedView file
Loading diff…