In-memory sandbox primitives for MoonBit - virtual filesystem, POSIX emulation, shell
Dependencies
moon add mizchi/sandboxlet ctx = @posix.PosixContext::default()
let shell = @shell.Shell::new(ctx)
// Simple commands
let result = shell.exec_line("echo Hello, World!")
println(result.stdout) // "Hello, World!\n"
// Variables and control flow
shell.exec_line("NAME=MoonBit")
shell.exec_line("echo Hello, $NAME!")
// Conditionals
shell.exec_line("if test -f /file.txt; then echo exists; else echo not found; fi")
// Loops
shell.exec_line("for i in a b c; do echo $i; done")
// Logical operators
shell.exec_line("test -d /tmp && echo 'tmp exists'")
shell.exec_line("false || echo 'fallback'")let fs = @fs.Fs::new()
// File operations
fs.write_string("/hello.txt", "Hello, World!")
let content = fs.read_string("/hello.txt")
// Directory operations
fs.mkdir("/mydir")
fs.mkdir_p("/deep/nested/path")
let entries = fs.readdir("/")
// Symlinks
fs.symlink("/link", "/hello.txt")// Use Fs (default)
let fs = @fs.Fs::default()
let ctx = @posix.PosixContext::new(fs)
// Use S3 backend (stub - implement for real S3)
let fs = @fs.S3Backend::new("my-bucket")
let ctx = @posix.PosixContext::new(fs)
// Use SQLite backend (stub - implement for real SQLite)
let fs = @fs.SqliteBackend::new("/path/to/db.sqlite")
let ctx = @posix.PosixContext::new(fs)# Run interactive shell (native target)
moon run src/cli --target native
# Output:
# MoonBit Shell
# Backend: memory
# Type 'exit' to quit
#
# $ echo Hello
# Hello
# $ for i in 1 2 3; do echo $i; done
# 1
# 2
# 3
# $ exitsrc/
├── fs/ # Filesystem layer
│ ├── backend.mbt # FileSystem trait
│ ├── memfs.mbt # In-memory implementation
│ └── stub_backends.mbt # S3/SQLite stubs
├── posix/ # POSIX emulation layer
│ ├── posix.mbt # PosixContext (fd, env, process)
│ └── types.mbt # POSIX types (Fd, Errno, etc.)
├── shell/ # Shell interpreter
│ ├── lexer.mbt # Tokenizer
│ ├── ast.mbt # Abstract syntax tree
│ ├── parser.mbt # Recursive descent parser
│ ├── executor.mbt # AST execution engine
│ ├── variables.mbt # Variable expansion
│ └── shell.mbt # Shell interface
└── cli/ # Command-line interface
├── main_native.mbt # Interactive shell (native)
└── main_demo.mbt # Demo mode (wasm-gc)Install
Download zipIn-memory sandbox primitives for MoonBit - virtual filesystem, POSIX emulation, shell
Dependencies