WASM Runtime

The WASM runtime uses the same capability protocol. The differences are the exported entry function and the host bridge: networking, HostGateway calls, and response reads go through ting_env imports.

Runtime Entry

Compile to wasm32-unknown-unknown. The runtime calls the exported invoke method using the method name from the capability invoke field and passes JSON parameters.

#[no_mangle]
pub extern "C" fn invoke(method: *const i8, params: *const i8) -> *mut i8 {
    let method = read_c_string(method);
    let params = read_c_string(params);

    match method.as_str() {
        "documentInvoke" => document_invoke(&params),
        "searchMetadata" => search_metadata(&params),
        _ => error_json("unknown method"),
    }
}

Host Functions

http_request performs permission-controlled network access; host_invoke calls HostGateway; host_response_size and host_read_body read the JSON response returned by the host.

#[link(wasm_import_module = "ting_env")]
extern "C" {
    fn http_request(
        method_ptr: *const u8,
        method_len: i32,
        url_ptr: *const u8,
        url_len: i32,
        body_ptr: *const u8,
        body_len: i32,
    ) -> i32;

    fn host_invoke(
        method_ptr: *const u8,
        method_len: i32,
        params_ptr: *const u8,
        params_len: i32,
    ) -> i32;

    fn host_response_size(handle: i32) -> i32;
    fn host_read_body(handle: i32, ptr: *mut u8, len: i32) -> i32;
}

Capability Example

WASM works well for content_processor, metadata_provider, and compute-heavy tool_provider capabilities. The following manifest declares a document processor.

runtime: wasm
entry_point: document_reader.wasm
capabilities:
  - id: document.reader
    kind: content_processor
    invoke: documentInvoke
    matches:
      extensions: [txt, pdf]
    operations:
      - probe
      - extract_metadata
      - list_sections
      - read_chunk
      - render_page
permissions:
  - type: media_read
  - type: cache_write

Packaging

Place plugin.yml and the compiled .wasm file in the package. If the WASM module needs network or HostGateway access, the manifest still needs the matching permissions.

trpack validate my-plugin
trpack build my-plugin --output dist/my-plugin.tr
trpack verify dist/my-plugin.tr

Good WASM Use Cases

  • Cross-platform parsing for text, PDF, or custom document formats.
  • Content processing that benefits from Rust libraries without depending on system dynamic libraries.
  • Heavier compute work such as sectioning, indexing, compression, or format probing.