Skip to content

Provide Method ​

After defining your functions or logic, the provide method is used to register them into the VM's context, making them accessible during execution.

Using TypeScript ​

For TypeScript, you can provide functions or data structures directly into the VM context. Ensure that the types provided align with the expected interface of the LightVM instance.

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

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

vm.provide({
  name: 'John Doe',
  force: 2021,
});

const raw = [['get', 'name'], ['println'], ['get', 'force'], ['println']];

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

Using Rust ​

In Rust, you can provide your logic using raw string representations or by utilizing serde_json for more complex data structures. This allows the VM to seamlessly integrate Rust-defined logic into its operational scope.

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();

  vm.provide(serde_json::json!({
    "name": "John Doe",
    "force": 2021
  }));

  let raw = r#"[
    ["get", "name"],
    ["println"],
    ["get", "force"],
    ["println"]
  ]"#;

  let optimized = tools.optimize_bytecode(raw);
  vm.load(optimized)
    .run(None);
}
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();

  vm.provide(serde_json::json!({
    "name": "John Doe",
    "force": 2021
  }));

  let raw = serde_json::json!([
    ["get", "name"],
    ["println"],
    ["get", "force"],
    ["println"]
  ]);

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

INFO

Capability Required: Control