BREP_script_core 0.2.0

The next.BREP.io script interpreter core (Rust B-rep CAD): evaluate one JavaScript file on Boa, call one global function with one JSON argument, and return its JSON result or its refusal. Shared by the PLM server's hooks and the CAD app, native and wasm.
Documentation
# BREP_script_core โ€” a small, bounded JavaScript call on Boa

Part of the [next.BREP.io](https://next.brep.io) Rust B-rep kernel: `cargo add BREP_script_core` ยท [licence summary](#licence)

`BREP_script_core` runs one JavaScript file on the pure-Rust
[Boa](https://crates.io/crates/boa_engine) 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

```rust
use brep_script_core::{call, Failure};
use serde_json::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!({"number": "P-100", "revision": 1, "description": "Bracket"});
match call(source, "hooks.js", "onRelease", &input, &|_, _| Ok(())) {
    Ok(done) => println!("{} (log: {:?})", done.value, done.logs),
    Err(Failure { message, logs }) => eprintln!("refused: {message} (log: {logs:?})"),
}
```

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 `call` builds a new Boa `Context`,
  evaluates the file, calls the function and drops the context: no state leaks
  from one call into the next, and a `Context` (which is not `Send`) never
  crosses a thread.
- **Host bindings.** `install(&mut Context, &Logs)` runs after `console` is
  defined and before the file is evaluated; add your own globals there.
  `brep_script_core::boa_engine` re-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 }`; for
  `throw new Error("x")` and `throw "x"` the message is `x`.
- **`console`.** `console.log`, `info`, `warn` and `error` write into the
  call's log (`warn` and `error` lines are prefixed `warn: ` and `error: `);
  `Success::logs` and `Failure::logs` return 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 most `RECURSION_LIMIT` (512),
  so `while (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.** `describe` turns a Boa `JsError` into the message a person
  reads, and `text_of` formats one value as a log line shows it.

## ES modules and the worker

- `modules::with_module` evaluates 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::synchronous` rejects a Promise result from a
  callback that must answer synchronously.
- `worker` is a restricted execution protocol, one JSON message per line: a
  program (a plain script body with top-level `await` and `return`) that
  imports nothing and reaches its host only through the functions of a global
  `sdk`, built from the `<namespace>.<action>` names it is started with; each
  call is a message the host answers. `worker::Realm` is the event-driven core (`begin`, `answer`,
  `abandon`) a host drives with messages, for example from a browser Web
  Worker; on native targets `worker::main` runs 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

In plain words: the crates are free to use as they are, including in a commercial project. If you
fix or change something in the kernel, send the change back as a pull request to
[github.com/mmiscool/next.BREP.io_RUST_BREP_KERNEL](https://github.com/mmiscool/next.BREP.io_RUST_BREP_KERNEL). If you do not want to share your
changes, buy a commercial licence from [Autodrop3d LLC](https://autodrop3d.com); with it you never
have to share your code back.

The exact terms are in `LICENSE.md`, shipped in the crate (`license-file` in the manifest) and at
[LICENSE.md](https://github.com/mmiscool/next.BREP.io_RUST_BREP_KERNEL/blob/master/LICENSE.md); if this summary and the file ever differ, the file wins.