Skip to content

Export Method Preview ​

The export method allows you to extract specific functions or logic registered within the VM's environment, exposing them to your host application. This effectively bridges the gap between the VM's isolated execution space and your primary code.

Using TypeScript ​

For TypeScript, the export method provides a seamless way to retrieve and invoke functions defined within the VM. By exporting a function, you can treat VM-side logic as native code, simplifying interaction and data processing between the two environments.

ts
import { Capability, LightVM } from 'lightvm';

const vm = new LightVM({ caps: [Capability.Control] });
const tools = vm.tools();

const raw = [
  ['jump', 7],
  ['func', 'add', 2, 2, 6, 'a', 'b'],
  ['get', 'a'],
  ['get', 'b'],
  ['add', 'int'],
  ['return'],
  ['stop'],
  ['export', 'add'],
];

const optimized = tools.optimizeBytecode(raw);
vm.load(optimized);

const addFunc = vm.export('add');

console.log(addFunc.call(5, 6));
ts
import { Capability, LightVM } from 'lightvm';

const vm = new LightVM({
  caps: [Capability.Control, Capability.Observe],
  runtimeConfig: { nightly: true },
});
const tools = vm.tools();

const raw = [
  ['val', 'score'],
  ['push', '2'],
  ['set', 'score'],
  ['export', 'score'],
];

const optimized = tools.optimizeBytecode(raw);
vm.load(optimized);

const scoreVar = vm.export('score');

console.log(scoreVar.call());

Using Rust ​

In Rust, the export method is used to map VM-side functions to Rust-callable interfaces. This mechanism allows you to leverage VM execution results directly within your main Rust application, enabling high-performance integration and modular architecture.

rust
use lightvm::LightVM;
use lightvm::types::{capability::Capability, vmconfig::VmConfig};

fn main() {
  let mut vm = LightVM::new(VmConfig {
    caps: vec![Capability::Control],
    ..Default::default()
  });
  let tools = vm.tools();

  let raw = r#"[
    ["jump", 7],
    ["func", "add", 2, 2, 6, "a", "b"],
    ["get", "a"],
    ["get", "b"],
    ["add", "int"],
    ["return"],
    ["stop"],
    ["export", "add"]
  ]"#;

  let optimized = tools.optimize_bytecode(raw);
  vm.load(optimized);

  let add_func = vm.export("add".to_string());

  println!("{:?}", add_func.call(&mut vm, vec![5.into(), 6.into()]));
}
rust
use lightvm::LightVM;
use lightvm::types::{
  capability::Capability, runtime_config::RuntimeConfig, vmconfig::VmConfig,
};

fn main() {
  let mut vm = LightVM::new(VmConfig {
    caps: vec![Capability::Control, Capability::Observe],
    runtime_config: Some(RuntimeConfig { nightly: true }),
    ..Default::default()
  });
  let tools = vm.tools();

  let raw = r#"[
    ["val", "score"],
    ["push", "2"],
    ["set", "score"],
    ["export", "score"]
  ]"#;

  let optimized = tools.optimize_bytecode(raw);
  vm.load(optimized);

  let score_variable = vm.export("score".to_string());

  println!("{:?}", score_variable.call(&mut vm, vec![]));
}

INFO

Capability Required: Control