Skip to content

Embedded Method

The .embedded() method executes the program currently loaded in the VM and returns its defined value, current execution outputs, and VM halted state. An undefined result or halted VM supplies null for value.

Using TypeScript

For TypeScript, create the VM with Control and Observe, load a raw bytecode array, and call .embedded().

ts
const raw = [
  ['push', 42],
  ['stop'],
];
const optimized = tools.optimizeBytecode(raw);
vm.load(optimized)
const result = vm.embedded();
console.log(result);

Using Rust

In Rust, you can load a serialized bytecode string or a serde_json value before calling .embedded().

rust
let raw = r#"[
  ["push", 42],
  ["stop"]
]"#;
let optimized = tools.optimize_bytecode(raw);
vm.load(optimized.clone())
let result = vm.embedded();
println!("value: {}", result["value"]);
println!("outputs: {}", result["outputs"]);
println!("halted: {}", result["halted"]);
rust
let raw = serde_json::json!([
  ["push", 42],
  ["stop"]
]);
let optimized = tools.optimize_bytecode(raw);
vm.load(optimized.clone())
let result = vm.embedded();
println!("value: {}", result["value"]);
println!("outputs: {}", result["outputs"]);
println!("halted: {}", result["halted"]);

INFO

Capabilities Required: Control executes the program, and Observe retrieves its outputs.

TIP

Before each embedded execution, .embedded() clears outputs left by the prior embedded execution. The returned outputs belong to the current execution, value contains a defined result or null for an undefined result or halted VM, and halted reports the VM halt state. Native execution failures return { status: "error", message }, while N-API and WebAssembly propagate failures through their binding error mechanisms.