5. Speak the host API

The module surface scripts import with use — and the traps around Results and unregistered modules.

After this lesson you can

- Import host modules and handle their Result returns with ? - Know which wscript-std modules are NOT registered - Find the authoritative surface in weave.wscripti

Before you start: Write your own package

Everything a script may touch comes from host modules — use fs, use shell, use template, use path, use log, and the rest. All fallible functions return Result[…, string]; propagate with ? and let the engine report the error against the step rather than swallowing it. Modules are registered on both platforms — a foreign-platform call (say registry on Linux) errors at runtime rather than silently no-opping.

Two traps: config-weave's fs replaces wscript-std's standalone fs (which is not registered — same for math, process and xml), and the surface is exactly what config-weave wscripti emits. If a function isn't in weave.wscripti, it does not exist, whatever the wscript-std docs say.

§ 1Exercise: Render a config from a template

Extend your resource (or add one) to render a file with template::render from a params-supplied string, using shell::run to verify the result out-of-band.

rust
use value
use fs
use template
use shell

fn apply(params: Value) -> Result[ApplyResult, string] {
    let out = template::render(params.get("tpl")?.as_string()?, params)?
    fs::write(params.get("path")?.as_string()?, out)?
    let r = shell::run("cat " + params.get("path")?.as_string()?)?
    if r.code != 0 { return Err("verify failed") }
    Ok(ApplyResult::Success)
}

Expected result

validate compiles the script; apply writes the rendered file and the step converges on re-check.

Hint

Check weave.wscripti for template::render's exact signature before guessing — inventing host functions is the classic failure.