JFScript. Scripts you can trust.
A small scripting language for apps built with JFLib. Scripts compile to bytecode and run in a sandbox with hard limits. They give the same result to the last bit on every machine, and they can pause anywhere and go on later. Today JFScript runs the simulated servers in JFNetworker.
JFScript is still in development. The language, its library and the Rust API can change before it is released on its own.
Familiar at first sight.
If you have written JavaScript, Lua or Python, you can read JFScript. It has functions and closures, lists and maps, try and catch, and modules through import. Maps keep their order, integers stay integers, and text counts characters, not bytes.
Types are optional. Where you write them, the checker and the editor find mistakes before the script runs.
A standard library comes with every script: text, lists, maps, bytes, math and JSON.
// the parts of a request path let parts = "/users/42/posts".split("/") .filter(fn(p) { return len(p) > 0 }) let id = int.parse(parts[1]) ?? 0 // 42 // records, sorted stably let jobs = [{n: "b", prio: 2}, {n: "a", prio: 1}] jobs.sort(fn(x, y) { return x.prio - y.prio }) print(jobs.map(fn(j) { return j.n }).join(", ")) // JSON keeps the order of maps let t = json.encode({id: 7, load: 0.5}) print("{:>4}|{:.2}".format(id, 0.5)) // 42|0.50 // the same bits on every machine let load = 100 + 50 * math.sin(2 * math.pi * 3 / 24) let shard = "user:42".hash() % 8
What makes it different.
Deterministic
No clock, no randomness, and floating point that rounds the same everywhere. A script gives the same bits on Windows, Linux, Android and in the browser.
Sandboxed
Every instruction costs fuel. Memory, stack and call depth have hard limits, so an endless loop or a huge list simply ends the run.
Pausable
Run a script in slices of any size and get the same result as in one go. A whole VM can be cloned in the middle of a run.
Your names only
A script reaches only the functions the host gives it. Give the compiler your list of names, and a misspelt one is an error before the script runs, with a suggestion.
Exact math
The math functions are fdlibm, bit for bit the same on every machine: errors below one ulp, and exact wherever the result can be represented exactly.
Editor support built in
Completion, hover, signatures, go to definition, references, rename, folding, an outline and a formatter, also on half-typed code.
Embedded in a few lines of Rust.
Compile the source, create a VM with its limits and run it with as much fuel as you like per slice. Your host answers the functions the script calls by name. Values that live longer than a run are pinned, and a stale value gives an error instead of a crash.
JFNetworker turns fuel into simulated CPU time: a server script in a lab costs time like real code on a real machine.
let prog = script::compile_named("server.jfs", src)?; let mut vm = Vm::new(&prog, Limits::default()); loop { match vm.run(&mut host, 10_000) { // fuel per slice Status::Done(_) => break, Status::Yielded => continue, Status::Error(e) => { log(&e.to_string()); break } } } let req = vm.new_str("GET /")?; vm.call("on_request", &[req])?;
A runner for the command line.
jfs runs a script the way python file.py runs a Python file. It checks scripts without running them, runs the test_* functions of test files, steps through code in a console debugger, and runs timers on a virtual clock with a fixed random seed.
A script that uses only the standard library, print, log and timers runs the same in jfs and in JFNetworker.
jfs hello.jfs Anna Ben
jfs -e "print([3, 1, 2].sort())"
jfs --check hello.jfs
jfs --test tests/*.jfs
jfs --debug app.jfs
jfs --virtual-time --seed 7 sim.jfs
Modules without magic.
import is a plain function. It returns the top-level names of another file as a map, leaving out the private ones that start with an underscore. Each file runs once per VM, and its functions keep seeing their own globals.
// lib/util.jfs let _calls = 0 // private fn shout(s) { _calls += 1; return s.upper() + "!" } fn calls() { return _calls } // main.jfs let util = import("lib/util.jfs") print(util.shout("hello"), util.calls()) // HELLO! 1