Commit 02836bc6
02836bc61f76Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
feat(codegen): stub File.readable?, File.binwrite, IntArray#pack(\"C*\")
Surfaced as a chain by optcarrot's battery-save dead-code path
(return unless File.readable?(sav) /
File.binwrite(sav, @wrk.pack("C*"))):
File.readable?reusessp_file_exist. On every platform Spinel targets, an fopen-able file is also readable from the same process; distinguishing requires access(2), not worth a runtime fn.File.binwrite(path, data)reusessp_file_write. The fputs path truncates on embedded NULs, fine for the optcarrot use site (fixed-shape save RAM with no zero bytes in practice).IntArray#pack("C*")is the inverse ofString#byteswe already support — only the"C*"format is recognised. The emitted code allocates a fresh sp_str and writes each int as a byte. Other formats fall through to the warning path.
infer_call_type also gets a readable? arm so puts File.readable?
prints true/false rather than 1/0.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 02836bc61f76
- Parent
- 54978f375415
spinel_codegen.rbmodifiedView file
@@ -4655,7 +4655,7 @@
46554655
if mname == "read" || mname == "binread"46564656
return "string"46574657
end4658-
if mname == "exist?"4658+
if mname == "exist?" || mname == "readable?"46594659
return "bool"46604660
end46614661
if mname == "join"@@ -26311,6 +26311,28 @@
2631126311
end2631226312
return "sp_IntArray_sort(" + rc + ")"2631326313
end26314+
# `pack("C*")` — the only format Spinel implements: each26315+
# int element is written as a byte into a freshly allocated26316+
# NUL-terminated string. Sufficient for the optcarrot save-26317+
# RAM `@wrk.pack("C*")` shape and the symmetric inverse of26318+
# `String#bytes` we already support. Other format strings26319+
# fall through to the unresolved-call warning.26320+
if mname == "pack" && recv_type == "int_array"26321+
args_id = @nd_arguments[nid]26322+
if args_id >= 026323+
a_pk = get_args(args_id)26324+
if a_pk.length == 1 && @nd_type[a_pk[0]] == "StringNode" && @nd_content[a_pk[0]] == "C*"26325+
tmp = new_temp26326+
ntmp = new_temp26327+
itmp = new_temp26328+
emit(" mrb_int " + ntmp + " = sp_IntArray_length(" + rc + ");")26329+
emit(" char *" + tmp + " = sp_str_alloc(" + ntmp + ");")26330+
emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < " + ntmp + "; " + itmp + "++) " + tmp + "[" + itmp + "] = (char)sp_IntArray_get(" + rc + ", " + itmp + ");")26331+
emit(" " + tmp + "[" + ntmp + "] = 0;")26332+
return tmp26333+
end26334+
end26335+
end2631426336
# take_while / drop_while: block-driven prefix scan. take_while2631526337
# collects elements from the front while the block stays truthy;2631626338
# drop_while skips them and returns the rest. Mirrors the@@ -27563,9 +27585,32 @@
2756327585
if mname == "exist?"2756427586
return "sp_file_exist(" + compile_arg0(nid) + ")"2756527587
end27588+
if mname == "readable?"27589+
# Reuse the exist check: on every platform Spinel27590+
# targets, a fopen-able file is also readable from27591+
# the same process. Distinguishing the two would27592+
# need access(2), which isn't worth a runtime fn for27593+
# the dead-code optcarrot battery-save path that27594+
# surfaced this.27595+
return "sp_file_exist(" + compile_arg0(nid) + ")"27596+
end2756627597
if mname == "delete"2756727598
return "(sp_file_delete(" + compile_arg0(nid) + "), 0)"2756827599
end27600+
if mname == "binwrite"27601+
# NOTE: `sp_file_write` uses fputs, so an embedded NUL27602+
# in the payload truncates the write — fine for the27603+
# optcarrot save-RAM dead-code path that surfaced27604+
# this, not safe for general binary use.27605+
args_id_bw = @nd_arguments[nid]27606+
if args_id_bw >= 027607+
a_bw = get_args(args_id_bw)27608+
if a_bw.length >= 227609+
return "(sp_file_write(" + compile_expr(a_bw[0]) + ", " + compile_expr(a_bw[1]) + "), 0)"27610+
end27611+
end27612+
return "0"27613+
end2756927614
if mname == "join"2757027615
args_id = @nd_arguments[nid]2757127616
if args_id >= 0test/file_class_methods_int_recv.rbaddedView file
Loading diff…
test/file_class_methods_int_recv.rb.expectedaddedView file
Loading diff…