← 都来码 / Dolaima← 例程索引ruby/test/file_io.rb原文

ruby/test/file_io.rb · 4775 字节 · 下载原文

# Ruby file_io.rb — 内联汇编(`asm("…")`)冒烟

# Ruby file_io.rb — an inline-assembly (`asm("…")`) smoke test

#

# 这一份测的是 Ruby 前端的**内联汇编**通路:`asm("…")` 里的文本现在原样成为一条

# This exercises the Ruby frontend's **inline assembly** path: the text inside `asm("…")` becomes one

# VML 汇编指令。此前 Ruby **没有 asm 节点**,`asm("…")` 被当成普通方法调用编成

# VML assembly instruction verbatim. Before this Ruby had **no asm node**, so `asm("…")` was compiled as an ordinary method call to

# `CALL func_asm` ⇒ 链接期「未定义的函数 'func_asm'」。

# `CALL func_asm` — a link-time "undefined function 'func_asm'".

#

# ## 判据 / The check

# ## Criterion

#

#     vmlcli Examples/ruby/file_io.rb

#

# 期望 stdout 恰好是本文件末尾「期望输出」那两行,退出码 0。

# Expect stdout to be exactly the two lines in the "expected output" block at the end, exit code 0.

#

# ## ⚠ 改动记录(2026-10-01)—— 为什么这份不再叫"文件 I/O 测试"

# ## ⚠ Change log (2026-10-01) — why this is no longer a "file I/O test"

#

# 原文是这三行:

# The original was these three lines:

#

#     asm("CALL shared_file_test")

#     asm("LOAD R0 #100")

#     asm("SYSCALL 3")

#

# 第一行引用的 **`shared_file_test` 在 `Lib/` 里根本不存在** —— `shared_` 前缀在

# The first line references **`shared_file_test`, which does not exist anywhere in `Lib/`** — the `shared_` prefix was

# v1.66.55 就已经从共享库删掉了(`GenLib/README.md` 的「命名约定」那一节写着)。

# dropped from the shared libraries back in v1.66.55 (see the "naming conventions" section of `GenLib/README.md`).

# 也就是说这一份例子**从来没能编过**,而且 Java / Swift / C# / JavaScript / ObjC / R /

# So this example has **never compiled**, and the same dead reference sits in the `file_io.*` example of

# Ruby **七门语言**的 `file_io.*` 是同一份死引用(见 `FRONTEND_DEFECTS.md`)。

# **seven** languages (Java / Swift / C# / JavaScript / ObjC / R / Ruby) — see `FRONTEND_DEFECTS.md`.

#

# 而"文件 I/O 测试"这件事~~在本平台**本来就没有可用的后端**~~ ✅ **已修好(2026-10-04 复验)**:

# And "file I/O" ~~has **no working backend on this platform at all**~~ ✅ **is fixed (re-checked 2026-10-04)**:

# `Lib/shared/file.c` 那套 `fopen`/`fwrite`/`fclose` 走的 `SYSCALL #110`~`#114`

# the `fopen`/`fwrite`/`fclose` family in `Lib/shared/file.c` goes through `SYSCALL #110`~`#114`, and

# ~~**运行时并没有实现这几个号**~~(当时)—— **现在运行时已实现**(`VMLRuntime.Syscall.cs` 的

# ~~**the runtime does not implement those numbers**~~ (at the time) -- **the runtime implements them now**

# `ExecuteFileOpen/Close/Read/Write/Control`)。当时实测(2026-10-01)`h = fopen("…", 1); fwrite(h, "…", n); fclose(h)`

# (`ExecuteFileOpen/Close/Read/Write/Control` in `VMLRuntime.Syscall.cs`). Measured then (2026-10-01): the calls compile

# 编译、运行都"成功"但**文件根本没被创建**(静默什么都不做)。

# and run "successfully" but **create no file** (they silently do nothing).

# 2026-10-04 复验:同一段代码**真的建出了文件** —— `h = fopen("rb_out.txt", 1); fwrite(h, "abc", 3); fclose(h)`

# Re-checked 2026-10-04: the same code **really creates the file** -- `h = fopen("rb_out.txt", 1); fwrite(h, "abc", 3); fclose(h)`

# 落盘一个内容逐字节 `abc` 的 `rb_out.txt`(相对路径、加 `--strict`(手机档)验证同样成立)。

# writes an `rb_out.txt` whose content is byte-for-byte `abc` (a relative path; the same holds with `--strict`, the mobile profile).

# ⇒ 这份是内联汇编冒烟(已经写好、判据明确)—— 能跑就别动;新写的代码要用文件 I/O 可以直接用 `fopen/fwrite/fclose`。

# So this is an inline-assembly smoke test (already written, with a clear criterion) -- existing code is fine as is; new code that needs file I/O may use `fopen/fwrite/fclose` directly.

#

# 另一处:颜色/输出用 `print`(不换行)+ `newline()`,与 `demo_std.rb` 同一套写法。

# Also: output uses `print` (no newline) + `newline()`, the same convention as `demo_std.rb`.


print("RUBY-ASM=")
# `SYSCALL #6` = OutputInt(R0 里的整数),与 `Lib/shared/builtins.c` 的 `print_int` 同一条路。

# `SYSCALL #6` = OutputInt (the integer in R0), the same path as `print_int` in `Lib/shared/builtins.c`.

asm("MOVE R0 #42")
asm("SYSCALL 6")
newline()

# 期望输出 / Expected output:

# Expected output (stdout must match the block below exactly):

#

#     RUBY-ASM=42