Virtual Filesystem
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_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.
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