Class WasmRuntime
Thread-safety: chicory Instance is NOT thread-safe, so a fresh
Instance is created for each invocation. The parsed WasmModule,
by contrast, is immutable and freely reusable across threads, so it is
cached (see MODULE_CACHE) keyed by a content hash of
the module bytes — parsing/validating the binary is chicory's most expensive
step and is pure given the bytes, so it is done at most once per distinct module.
ABI. Two export shapes are supported, both returning non-zero for a match:
- Legacy body-only —
match(i32 ptr, i32 len) -> i32. The request body is written into linear memory at offset 0 and the function is called with(0, bodyLength). - Richer request envelope —
match_request(i32 ptr, i32 len) -> i32. A JSON envelope{"method","path","headers","body"}is written into linear memory at offset 0 and the function is called with(0, jsonLength). This lets a module read the method, path and headers in addition to the body.
match_request it is preferred; otherwise the runtime
falls back to match with the body only, so existing body-only modules keep
working unchanged.
This class fails closed: any error returns false.
-
Constructor Summary
ConstructorsConstructorDescriptionWasmRuntime(byte[] wasmBytes) Create a runtime with the default memory page limit fromConfigurationProperties.wasmMaxMemoryPages().WasmRuntime(byte[] wasmBytes, int maxMemoryPages) Create a runtime with an explicit memory page limit. -
Method Summary
Modifier and TypeMethodDescriptionbooleanCall the WASM module with just the request body (legacy body-only ABI).booleancallMatch(WasmRequest request) Call the WASM module with the full request envelope (method, path, headers, body).static voidinvalidate(byte[] wasmBytes) Drop the cached parsed module for the given bytes, if present.static voidClear all cached parsed modules.
-
Constructor Details
-
WasmRuntime
public WasmRuntime(byte[] wasmBytes) Create a runtime with the default memory page limit fromConfigurationProperties.wasmMaxMemoryPages(). -
WasmRuntime
public WasmRuntime(byte[] wasmBytes, int maxMemoryPages) Create a runtime with an explicit memory page limit.- Parameters:
wasmBytes- the compiled WASM binarymaxMemoryPages- maximum number of WASM linear memory pages (each page is 64 KiB)
-
-
Method Details
-
callMatch
Call the WASM module with just the request body (legacy body-only ABI).Retained for back-compat; equivalent to
callMatch(WasmRequest.ofBody(requestBody)).- Parameters:
requestBody- the HTTP request body (may be null)- Returns:
trueif the module reports a match
-
callMatch
Call the WASM module with the full request envelope (method, path, headers, body).If the module exports
MATCH_REQUESTthe JSON envelope is passed and that function is invoked; otherwise the runtime falls back to the legacyMATCHexport with only the body, preserving back-compat for body-only modules.- Parameters:
request- the request parts to expose to the module (must not be null)- Returns:
trueif the module reports a match
-
invalidate
public static void invalidate(byte[] wasmBytes) Drop the cached parsed module for the given bytes, if present. Called when a module is unloaded so its parsed form is released promptly. A no-op when the bytes were never cached. Correctness never depends on this (the cache is content-keyed); it only bounds memory. -
invalidateAll
public static void invalidateAll()Clear all cached parsed modules. Called on a full WASM store reset.
-