BREP_script_core — a small, bounded JavaScript call on Boa
BREP_script_core runs one JavaScript file on the pure-Rust
Boa engine, calls one global function
with one JSON argument, and hands back the function's JSON result or a
message a person can read. It is the interpreter behind the BREP PLM server's
administrator hooks and the BREP CAD application's scripts, natively and in
the browser (wasm32-unknown-unknown).
It is for a host that wants user-written rules — "what number does this part
get", "may this revision be released" — without giving the script anything
the host did not hand it. The crate depends on Boa and serde_json only: no
file system, no network, no clock. Every binding that reaches outside the
interpreter is installed by the host.
Example
use ;
use json;
let source = r#"
function onRelease(part) {
console.log("checking " + part.number);
if (!part.description) throw new Error("a released part needs a description");
return { ok: true, revision: part.revision + 1 };
}
"#;
// No host bindings beyond `console`: pass an `install` that adds nothing.
let input = json!;
match call
This prints {"ok":true,"revision":2} (log: ["checking P-100"]). Without a
description the call returns Failure with the message
a released part needs a description.
How it behaves
- A fresh interpreter per call. Each
callbuilds a new BoaContext, evaluates the file, calls the function and drops the context: no state leaks from one call into the next, and aContext(which is notSend) never crosses a thread. - Host bindings.
install(&mut Context, &Logs)runs afterconsoleis defined and before the file is evaluated; add your own globals there.brep_script_core::boa_enginere-exports Boa so your bindings name the same engine version. - Failures are sentences. A throw, a parse error, or a function the file
does not define all return
Failure { message, logs }; forthrow new Error("x")andthrow "x"the message isx. console.console.log,info,warnanderrorwrite into the call's log (warnanderrorlines are prefixedwarn:anderror:);Success::logsandFailure::logsreturn it.- Bounded, honestly. Each call frame may run at most
LOOP_ITERATION_LIMIT(10,000,000) loop iterations in total (Boa counts per frame, not per loop) and the call depth is at mostRECURSION_LIMIT(512), sowhile (true) {}and runaway recursion throw instead of hanging. A loop that calls a function whose own loops stay under the limit is not bounded in total. There is no wall-clock limit: Boa cannot pre-empt a running script, so many individually bounded loops can still run long, and a script blocked in a host function is bounded only by that function's own timeout. - Helpers.
describeturns a BoaJsErrorinto the message a person reads, andtext_offormats one value as a log line shows it.
ES modules and the worker
modules::with_moduleevaluates a graph of ES modules held in memory (a map of package-local paths to sources; only./and../imports resolve) in a fresh, equally bounded context, then hands the evaluated entry module to a callback.modules::synchronousrejects a Promise result from a callback that must answer synchronously.workeris a restricted execution protocol, one JSON message per line: a program (an ES module with a defaultmain) that can import only the SDK module it is started with and reaches its host only through calls the host answers.worker::Realmis the event-driven core (begin,answer,abandon) a host drives with messages, for example from a browser Web Worker; on native targetsworker::mainruns it over stdin and stdout, for a host that starts it as a separate process and enforces CPU, memory and time limits by killing that process.
Feature flags
None. On wasm32 the crate turns on Boa's js feature by itself (a
getrandom backend and a browser clock), so a host never has to.
Licence
The Autodrop3d licence in LICENSE.md, shipped in the crate (license-file
in the manifest). It is not an SPDX licence: read it before you modify or
redistribute the crate.