Skip to content

Stringify Method ​

The stringify method allows you to convert complex bytecode structures into a serialized format. This is particularly useful for debugging, logging, or preparing your bytecode for storage and transmission.

Using TypeScript ​

For TypeScript, you can use the stringify method to output your bytecode arrays into a human-readable string format. This helps in verifying the structure of your instructions before they are loaded into the VM.

ts
import { LightVM } from 'lightvm';

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

const raw = [
  ['push', 5],
  ['val', 'x'],
  ['set', 'x'],
];

const stringify = tools.stringifyLTC(raw);

console.log(stringify);

Using Rust ​

In Rust, you can stringify your bytecode by working with raw string representations or using serde_json to manage complex serialization. This provides flexibility when you need to transform your bytecode into a format compatible with external tools or logging systems.

rust
use lightvm::LightVM;
use lightvm::types::{vmconfig::VmConfig};

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

  let raw = r#"[
    ["push", 5],
    ["val", "x"],
    ["set", "x"]
  ]"#;

  let stringify = tools.stringify_ltc(raw);

  println!("{:#}", stringify.clone());
}
rust
use lightvm::LightVM;
use lightvm::types::{vmconfig::VmConfig};

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

  let raw = serde_json::json!([
    ["push", 5],
    ["val", "x"],
    ["set", "x"]
  ]);

  let stringify = tools.stringify_ltc(raw);

  println!("{:#}", stringify.clone());
}

INFO

Capability Required: no specific capability