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? reuses sp_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) reuses sp_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 of String#bytes we 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 end
4658- if mname == "exist?"
4658+ if mname == "exist?" || mname == "readable?"
46594659 return "bool"
46604660 end
46614661 if mname == "join"
@@ -26311,6 +26311,28 @@
2631126311 end
2631226312 return "sp_IntArray_sort(" + rc + ")"
2631326313 end
26314+ # `pack("C*")` — the only format Spinel implements: each
26315+ # int element is written as a byte into a freshly allocated
26316+ # NUL-terminated string. Sufficient for the optcarrot save-
26317+ # RAM `@wrk.pack("C*")` shape and the symmetric inverse of
26318+ # `String#bytes` we already support. Other format strings
26319+ # 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 >= 0
26323+ 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_temp
26326+ ntmp = new_temp
26327+ itmp = new_temp
26328+ 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 tmp
26333+ end
26334+ end
26335+ end
2631426336 # take_while / drop_while: block-driven prefix scan. take_while
2631526337 # 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 end
27588+ if mname == "readable?"
27589+ # Reuse the exist check: on every platform Spinel
27590+ # targets, a fopen-able file is also readable from
27591+ # the same process. Distinguishing the two would
27592+ # need access(2), which isn't worth a runtime fn for
27593+ # the dead-code optcarrot battery-save path that
27594+ # surfaced this.
27595+ return "sp_file_exist(" + compile_arg0(nid) + ")"
27596+ end
2756627597 if mname == "delete"
2756727598 return "(sp_file_delete(" + compile_arg0(nid) + "), 0)"
2756827599 end
27600+ if mname == "binwrite"
27601+ # NOTE: `sp_file_write` uses fputs, so an embedded NUL
27602+ # in the payload truncates the write — fine for the
27603+ # optcarrot save-RAM dead-code path that surfaced
27604+ # this, not safe for general binary use.
27605+ args_id_bw = @nd_arguments[nid]
27606+ if args_id_bw >= 0
27607+ a_bw = get_args(args_id_bw)
27608+ if a_bw.length >= 2
27609+ return "(sp_file_write(" + compile_expr(a_bw[0]) + ", " + compile_expr(a_bw[1]) + "), 0)"
27610+ end
27611+ end
27612+ return "0"
27613+ end
2756927614 if mname == "join"
2757027615 args_id = @nd_arguments[nid]
2757127616 if args_id >= 0