Skip to content

Penggunaan Cepat

Inisialisasi LightVM sangat fleksibel dan memungkinkan Anda untuk mengkonfigurasi kapabilitas dan fitur debugging sesuai dengan kebutuhan aplikasi Anda.

Menggunakan TypeScript

Untuk proyek berbasis TypeScript, Anda dapat mengkonfigurasi instance VM dengan pola builder yang intuitif sebelum mengakses antarmuka tools utama.

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

const vm = new LightVM({ caps: [Capability.Observe, Capability.Control] })
  .setMaxIo(100) // Maximum number of I/O operations allowed (default: 100)
  .setMaxImport(3) // Maximum number of allowed module imports (default: 3)
  .setMaxAlloc(50) // Maximum number of memory allocations allowed (default: 50)
  .setMaxCall(200) // Maximum number of nested function calls allowed (default: 200)
  .setMaxJump(100) // Maximum number of control flow jumps allowed (default: 100)
  .setMaxTicks(1_000_000) // Maximum number of execution ticks before stopping (default: 1,000,000)
  .setMaxStackSize(128) // Maximum number of items the stack can hold (default: 128)
  .setAllowedImports(['math', 'time', 'utils']) // Whitelist of modules that can be imported
  .setTimeBudget(TimeBudget.Cheap) // Sets the execution time budget limit to prevent infinite loops (Default: Cheap)
  .withUnsafeMode(false) // Enable or disable system-level unsafe operations (default: false)
  .withNightly(false) // Allow nightly features (default: false)
  .withBacktrace(false) // Display backtrace details in error messages (default: false)
  .withExplain(false) // Display a more detailed hint in the error message (default: false)
  .withHint(true); // Display a hint on error messages (default: true)

const tools = vm.tools();
ts
import { LightVM, Capability, TimeBudget } from 'lightvm';

const vm = new LightVM({
  caps: [Capability.Observe, Capability.Control],
  runtimeConfig: {
    nightly: false, // Allow nightly features (default: false)
  },
  errorOptions: {
    backtrace: false, // Display backtrace details in error messages (default: false)
    explain: false, // Display a more detailed hint in the error message (default: false)
    hint: true, // Display a hint on error messages (default: true)
  },
  securityConfig: {
    maxIo: 100, // Maximum number of I/O operations allowed (default: 100)
    maxImport: 3, // Maximum number of allowed module imports (default: 3)
    maxAlloc: 50, // Maximum number of memory allocations allowed (default: 50)
    maxCall: 200, // Maximum number of nested function calls allowed (default: 200)
    maxJump: 100, // Maximum number of control flow jumps allowed (default: 100)
    maxTicks: 1_000_000, // Maximum number of execution ticks before stopping (default: 1,000,000)
    maxStackSize: 128, // Maximum number of items the stack can hold (default: 128)
    allowedImports: ['math', 'time', 'utils'], // Whitelist of modules that can be imported
    timeBudget: TimeBudget.Cheap, // Sets the execution time budget limit to prevent infinite loops (Default: Cheap)
    unsafeMode: false, // Enable or disable system-level unsafe operations (default: false)
  },
});

const tools = vm.tools();

Menggunakan Rust

Bagi pengguna Rust, konfigurasi dilakukan melalui VmConfig. Anda dapat mengatur kemampuan VM secara deklaratif sebelum mengeksekusi bytecode.

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

fn main() {
  let mut vm = LightVM::new(VmConfig {
    caps: vec![Capability::Control, Capability::Observe],
    ..Default::default()
  })
  .set_max_io(100) // Maximum number of I/O operations allowed (default: 100)
  .set_max_import(3) // Maximum number of allowed module imports (default: 3)
  .set_max_alloc(50) // Maximum number of memory allocations allowed (default: 50)
  .set_max_call(200) // Maximum number of nested function calls allowed (default: 200)
  .set_max_jump(100) // Maximum number of control flow jumps allowed (default: 100)
  .set_max_ticks(1_000_000) // Maximum number of execution ticks before stopping (default: 1,000,000)
  .set_max_stack_size(128) // Maximum number of items the stack can hold (default: 128)
  .set_allowed_imports(vec!["math".into(), "time".into(), "utils".into()]) // Whitelist of modules that can be imported
  .set_time_budget(TimeBudget::Cheap) // Sets the execution time budget limit to prevent infinite loops (Default: Cheap)
  .with_unsafe_mode(false) // Enable or disable system-level unsafe operations (default: false)
  .with_nightly(false) // Allow nightly features (default: false)
  .with_backtrace(false) // Display backtrace details in error messages (default: false)
  .with_explain(false) // Display a more detailed hint in the error message (default: false)
  .with_hint(true); // Display a hint on error messages (default: true)
  
  let tools = vm.tools();
}
rust
use lightvm::LightVM;
use lightvm::types::{
  vmconfig::VmConfig,
  runtime_config::RuntimeConfig,
  error_options::ErrorOptions,
  security_config::SecurityConfig,
  capability::Capability,
  time_budget::TimeBudget
};

fn main() {
  let mut vm = LightVM::new(VmConfig {
    caps: vec![Capability::Control, Capability::Observe],
    runtime_config: Some(RuntimeConfig {
      nightly: false // Allow nightly features (default: false)
    }),
    error_options: Some(ErrorOptions {
      backtrace: false, // Display backtrace details in error messages (default: false)
      explain: false, // Display a more detailed hint in the error message (default: false)
      hint: true // Display a hint on error messages (default: true)
    }),
    security_config: Some(SecurityConfig {
      max_io: 100, // Maximum number of I/O operations allowed (default: 100)
      max_import: 3, // Maximum number of allowed module imports (default: 3)
      max_alloc: 50, // Maximum number of memory allocations allowed (default: 50)
      max_call: 200, // Maximum number of nested function calls allowed (default: 200)
      max_jump: 100, // Maximum number of control flow jumps allowed (default: 100)
      max_ticks: 1_000_000, // Maximum number of execution ticks before stopping (default: 1,000,000)
      max_stack_size: 128, // Maximum number of items the stack can hold (default: 128)
      allowed_imports: vec!["math".into(), "time".into(), "utils".into()], // Whitelist of modules that can be imported
      time_budget: TimeBudget::Cheap, // Sets the execution time budget limit to prevent infinite loops (Default: Cheap)
      unsafe_mode: false // Enable or disable system-level unsafe operations (default: false)
    })
  });
  
  let tools = vm.tools();
}

TIP

Dapatkan antarmuka alat. Simpan ini sebagai konstanta agar dapat digunakan kembali untuk semua tugas yang akan datang.

Referensi API Terkait

Untuk mengatur izin akses atau batas eksekusi, lihat panduan lengkapnya di halaman Kapabilitas dan Batas Waktu Eksekusi.