Expose wasm engine clone and add prepare API

Agent-Logs-Url: https://github.com/microsoft/regorus/sessions/58419b20-1586-4a23-85fb-5aed5f5d5961

Co-authored-by: anakrish <35780660+anakrish@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-05-13 20:16:02 +00:00
committed by GitHub
parent 839933c933
commit 72515f6d4c
5 changed files with 97 additions and 0 deletions
+3
View File
@@ -21,3 +21,6 @@ Run `cargo xtask build-wasm` to invoke wasm-pack with sensible defaults, or `car
## Usage
See [test.js](https://github.com/microsoft/regorus/blob/main/bindings/wasm/test.js) for example usage.
For best performance with large policies, call `engine.prepare()` after loading
policy/data, then use `engine.clone()` to create per-request engines.
+19
View File
@@ -138,6 +138,14 @@ impl Engine {
self.engine.set_rego_v0(enable)
}
/// Clone this engine.
///
/// Useful for creating per-request engines after loading policy/data once.
#[wasm_bindgen(js_name = "clone")]
pub fn cloneEngine(&self) -> Engine {
Clone::clone(self)
}
/// Add a policy
///
/// The policy is parsed into AST.
@@ -158,6 +166,14 @@ impl Engine {
self.engine.add_data(data).map_err(error_to_jsvalue)
}
/// Prepare the engine for evaluation.
///
/// This initializes internal evaluation structures so a cloned engine can
/// evaluate without requiring an initial "dummy" evaluation.
pub fn prepare(&mut self) -> Result<(), JsValue> {
self.engine.prepare().map_err(error_to_jsvalue)
}
/// Get the list of packages defined by loaded policies.
///
/// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.get_packages
@@ -487,6 +503,9 @@ mod tests {
)?;
assert_eq!(pkg, "data.test");
// Prepare before first evaluation.
engine.prepare()?;
let results = engine.evalQuery("data".to_string())?;
let r = regorus::Value::from_json_str(&results).map_err(error_to_jsvalue)?;
+7
View File
@@ -40,6 +40,13 @@ engine.addDataJson(`
}
`);
// Prepare internal evaluation structures once.
engine.prepare();
// Clone a prepared template engine for reuse.
var template = engine.clone();
engine = template.clone();
// Set policy input
engine.setInputJson(`
{