Skip to content

stdlib: ファイル I/O と fd 汎用 read/write/close (#33)#116

Merged
ryuichi1208 merged 10 commits into
mainfrom
feat/33-file-io
Jun 1, 2026
Merged

stdlib: ファイル I/O と fd 汎用 read/write/close (#33)#116
ryuichi1208 merged 10 commits into
mainfrom
feat/33-file-io

Conversation

@ryuichi1208

@ryuichi1208 ryuichi1208 commented Jun 1, 2026

Copy link
Copy Markdown
Member

概要

  • TCP 専用だった tcp_read / tcp_write / tcp_close を、fd 汎用の read / write / close(POSIX read(2)/write(2)/close(2) ベース)に統合
  • ファイルを開く file_open(path: string, mode: string) -> int"r"/"w"/"a"、失敗時は負の fd)を追加
  • 書く側は「開く操作はソース別、読み書き閉じる操作は共通」という Unix の意味論で、TCP もファイルも同じ read/write/close で書ける

Closes #33

なぜ

tcp_read/tcp_write/tcp_close は実はソケット専用の操作ではなく、read(2)/write(2)/close(2) は任意の fd に使える汎用 syscall。これを汎用化したうえで file_open を足すだけで、ファイル I/O が最小コードで手に入る。

設計の核心

runtime を recv/send から read(2)/write(2) に変え、EAGAIN のとき fiber 上なら netpoller に park する。

  • ノンブロッキングソケット → EAGAIN を返すので park(既存の netpoller 連携)
  • 正規ファイル → read(2)EAGAIN を返さないのでそのまま同期 read

これにより fd 種別を判定する分岐コードなしで、ソケットとファイルが同一コードパスで両対応する。tcp_listen/tcp_accept はソケット固有なのでそのまま残す。

組み込み関数

関数 シグネチャ
file_open (path: string, mode: string) -> int
read (fd: int, max: int) -> Bytes
write (fd: int, b: Bytes) -> int
close (fd: int) -> int

ryuichi1208 and others added 10 commits June 1, 2026 17:17
Design for issue #33. Replace socket-only tcp_read/tcp_write/tcp_close
with fd-generic read/write/close backed by read(2)/write(2)/close(2),
and add file_open(path, mode). The EAGAIN+park path naturally handles
sockets (nonblocking) and files (no EAGAIN) without fd-type branching.
tcp_listen/tcp_accept stay as socket-specific open operations.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Lift the EAGAIN+park structure from the old tcp_read/tcp_write into
io.c using read(2)/write(2), add close(2) and file_open. Remove
rw_tcp_read/rw_tcp_write/rw_tcp_close (superseded). tcp_listen/accept
stay in tcp.c.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…ile_open

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@ryuichi1208 ryuichi1208 merged commit 776103e into main Jun 1, 2026
2 checks passed
@github-actions github-actions Bot mentioned this pull request Jun 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

stdlib: ファイル I/O (open / read / write / close)

1 participant