Virtual Filesystem

For new readers

A "virtual" filesystem here means an in-memory stand-in for real files — reads/writes work the same way, but nothing touches your actual disk, which is why this demo can run entirely inside a browser sandbox (try the "Compile and run in this browser" button). Useful anywhere you want file-like storage without real disk access, including in contexts (like WASM) where real disk access isn't available at all.

An in-memory filesystem available on every target including WASM, run live below — see the Standard Library reference for the full vfs_* function list and the native-only, security-gated vfs_flush_to_disk.

vfs_read/vfs_write/vfs_append/vfs_exists/vfs_list/vfs_delete are an in-memory filesystem available on every target — including WASM, unlike read_file/write_file, which are real (native-only) disk I/O. Try the Compile and run in this browser button below: this demo runs entirely inside the WASM sandbox with no real filesystem access at all. A separate, explicit, native-only vfs_flush_to_disk (not exercised here) write-throughs to real files, gated by an allowed-root check.

vfs_append is the newest addition — the VFS itself is just a plain in-memory map from path to content string, and until this was added, the ONLY way to add anything to an existing file was vfs_write: read the whole thing, build the new content, write the whole thing back. Fine for occasional whole-file rewrites; a real problem for anything appending piece by piece (a growing log, a database table gaining rows one at a time), where it turns N appends into O(N²) total work. Found and fixed stress-testing the SQL console's INSERT path against a few thousand rows for a real external project (fantpop-patlang) — vfs_append pushes onto the existing stored string directly, true O(new content) amortized, not a read-modify-write of everything already there.

PatLang source
# The virtual filesystem: an in-memory vfs_* layer available on every
# target including WASM (unlike read_file/write_file/etc, which are real
# std::fs and native-only), with an explicit, native-only, security-gated
# write-through to the real filesystem.

vfs_write("notes/todo.txt", "write the vfs demo")
vfs_write("notes/done.txt", "wired up rule_add")
print("exists notes/todo.txt: " + vfs_exists("notes/todo.txt"))
print("exists notes/missing.txt: " + vfs_exists("notes/missing.txt"))
print("content: " + vfs_read("notes/todo.txt"))

let listed = vfs_list("notes/")
print("files under notes/: " + list_len(listed))

let removed = vfs_delete("notes/todo.txt")
print("deleted todo.txt: " + removed)
print("exists after delete: " + vfs_exists("notes/todo.txt"))

# vfs_flush_to_disk is native-only and security-gated by PATLANG_VFS_ALLOWED_ROOT
# (defaults to the current working directory) -- not run here since this
# demo is also compiled and run under WASM, where it must correctly error
# rather than silently doing nothing or crashing the whole program.

print("DONE")

(not run yet)

Native run on the build machine:

exists notes/todo.txt: 1
exists notes/missing.txt: 0
content: write the vfs demo
files under notes/: 2
deleted todo.txt: true
exists after delete: 0
DONE