virtual-machine 0.7.1 → 0.7.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/build/index.d.ts CHANGED
@@ -259,6 +259,14 @@ declare class WasmVm {
259
259
  * This allows the browser to do partial texture uploads for better performance.
260
260
  */
261
261
  get_gpu_dirty_rect(): Uint32Array | undefined;
262
+ /**
263
+ * Reset hart 0 to bare machine mode at the entry point.
264
+ *
265
+ * Benchmark workloads (see `bench_workload`) are M-mode programs; this
266
+ * undoes the S-mode boot setup that `new_with_harts` applies so they
267
+ * run exactly like under the native bench runner.
268
+ */
269
+ reset_machine_mode(): void;
262
270
  /**
263
271
  * Check if cancellation has been requested.
264
272
  */
@@ -277,10 +285,14 @@ declare class WasmVm {
277
285
  * Get a direct zero-copy view into the framebuffer in SharedArrayBuffer.
278
286
  *
279
287
  * This eliminates all memory copies by creating a Uint8Array view directly
280
- * into the SharedArrayBuffer at the framebuffer offset. The browser can
281
- * pass this directly to WebGPU's writeTexture for zero-copy rendering.
282
- *
283
- * Returns None if SharedArrayBuffer is not available (single-threaded mode).
288
+ * into guest memory at the framebuffer offset. The browser can pass this
289
+ * directly to WebGPU's writeTexture for zero-copy rendering.
290
+ *
291
+ * Works in both memory modes:
292
+ * - SMP (SharedArrayBuffer): stable view into the SAB.
293
+ * - Single-hart (linear memory): view into WASM linear memory. This view
294
+ * is invalidated whenever WASM memory grows, so consume it immediately
295
+ * and call this again each frame (do not cache it JS-side).
284
296
  */
285
297
  get_framebuffer_view(): Uint8Array | undefined;
286
298
  /**
@@ -377,12 +389,22 @@ declare class WasmVm {
377
389
  */
378
390
  is_smp(): boolean;
379
391
  /**
380
- * Execute up to N instructions in a batch.
381
- * Returns the number of instructions actually executed.
382
- * This is more efficient than calling step() N times due to reduced
383
- * JS-WASM boundary crossings.
392
+ * Execute up to N instructions by looping `step()`.
393
+ *
394
+ * DEPRECATED for hot paths: prefer `run_batch`, which hoists device
395
+ * polling and CLINT sync out of the instruction loop and returns early
396
+ * on WFI. `step_n` keeps the historical contract that a return value
397
+ * less than `count` means the VM halted (it never stops early on WFI),
398
+ * which existing consumers rely on for halt detection.
384
399
  */
385
400
  step_n(count: number): number;
401
+ /**
402
+ * Total retired guest instructions on hart 0.
403
+ *
404
+ * Unlike step counts, this reflects real instructions (superblock
405
+ * execution retires many instructions per step). Use for MIPS metrics.
406
+ */
407
+ instret(): number;
386
408
  /**
387
409
  * Get the entry PC address for workers.
388
410
  * This is the address where secondary harts should start executing.
@@ -423,6 +445,20 @@ declare class WasmVm {
423
445
  * Get the number of harts configured.
424
446
  */
425
447
  num_harts(): number;
448
+ /**
449
+ * Execute up to N instructions in a batch with device polling, CLINT
450
+ * sync, and RTC updates hoisted to chunk boundaries.
451
+ *
452
+ * This is the fast path for hart 0: the inner loop is a tight
453
+ * `cpu.step()` with no per-instruction bookkeeping. Interrupt latency
454
+ * is bounded by IRQ_SYNC_INTERVAL (shared-CLINT sync) and the CPU's own
455
+ * internal 256-instruction interrupt poll.
456
+ *
457
+ * Returns the number of dispatcher steps executed (superblocks count as
458
+ * one step; use `instret()` for true instruction counts). Returns early
459
+ * on halt or WFI so the JS caller can pace execution.
460
+ */
461
+ run_batch(max_steps: number): number;
426
462
  }
427
463
 
428
464
  declare class WorkerState {
@@ -496,6 +532,15 @@ declare enum WorkerStepResult {
496
532
  Wfi = 4,
497
533
  }
498
534
 
535
+ /**
536
+ * Build a synthetic benchmark workload binary (see `bench` module).
537
+ *
538
+ * Load the returned bytes as a raw "kernel" via `WasmVm::new_with_harts`
539
+ * and drive it with `step_n`; read `instret()` for MIPS accounting.
540
+ * Available workloads: nop, prime, memcpy, spinlock, ecall.
541
+ */
542
+ declare function bench_workload(name: string): Uint8Array;
543
+
499
544
  /**
500
545
  * Check interrupts for this hart using the shared CLINT.
501
546
  *
@@ -519,6 +564,7 @@ interface InitOutput {
519
564
  readonly memory: WebAssembly.Memory;
520
565
  readonly __wbg_wasmvm_free: (a: number, b: number) => void;
521
566
  readonly __wbg_workerstate_free: (a: number, b: number) => void;
567
+ readonly bench_workload: (a: number, b: number) => [number, number, number, number];
522
568
  readonly wasmvm_allow_workers_to_start: (a: number) => void;
523
569
  readonly wasmvm_clear_cancel: (a: number) => void;
524
570
  readonly wasmvm_connect_webtransport: (a: number, b: number, c: number, d: number, e: number) => [number, number];
@@ -553,6 +599,7 @@ interface InitOutput {
553
599
  readonly wasmvm_has_gpu_frame: (a: number) => number;
554
600
  readonly wasmvm_inject_network_packet: (a: number, b: any) => number;
555
601
  readonly wasmvm_input: (a: number, b: number) => void;
602
+ readonly wasmvm_instret: (a: number) => number;
556
603
  readonly wasmvm_is_cancel_requested: (a: number) => number;
557
604
  readonly wasmvm_is_external_network_connected: (a: number) => number;
558
605
  readonly wasmvm_is_halted: (a: number) => number;
@@ -565,6 +612,8 @@ interface InitOutput {
565
612
  readonly wasmvm_print_banner: (a: number) => void;
566
613
  readonly wasmvm_print_status: (a: number, b: number, c: number) => void;
567
614
  readonly wasmvm_request_cancel: (a: number) => void;
615
+ readonly wasmvm_reset_machine_mode: (a: number) => void;
616
+ readonly wasmvm_run_batch: (a: number, b: number) => number;
568
617
  readonly wasmvm_send_d1_char: (a: number, b: number) => number;
569
618
  readonly wasmvm_send_d1_key_event: (a: number, b: number, c: number) => number;
570
619
  readonly wasmvm_send_key_event: (a: number, b: number, c: number) => number;
@@ -579,6 +628,7 @@ interface InitOutput {
579
628
  readonly wasmvm_terminate_workers: (a: number) => void;
580
629
  readonly wasmvm_uart_output_pending: (a: number) => number;
581
630
  readonly worker_check_interrupts: (a: number, b: any) => bigint;
631
+ readonly worker_entry: (a: number, b: any, c: bigint) => void;
582
632
  readonly workerstate_get_a0: (a: number) => bigint;
583
633
  readonly workerstate_hart_id: (a: number) => number;
584
634
  readonly workerstate_is_msip_pending: (a: number) => number;
@@ -586,13 +636,12 @@ interface InitOutput {
586
636
  readonly workerstate_new: (a: number, b: any, c: bigint) => number;
587
637
  readonly workerstate_step_batch: (a: number, b: number) => number;
588
638
  readonly workerstate_step_count: (a: number) => bigint;
589
- readonly worker_entry: (a: number, b: any, c: bigint) => void;
590
- readonly wasm_bindgen__convert__closures_____invoke__haa1a35ae0470100f: (a: number, b: number, c: any) => void;
591
- readonly wasm_bindgen__closure__destroy__h34273dc4c79ee0c9: (a: number, b: number) => void;
592
- readonly wasm_bindgen__convert__closures_____invoke__h21398a37e136b0cc: (a: number, b: number) => void;
593
- readonly wasm_bindgen__convert__closures_____invoke__h260170fb96639bcf: (a: number, b: number, c: any) => void;
594
- readonly wasm_bindgen__closure__destroy__h612f8e24953b61d5: (a: number, b: number) => void;
595
- readonly wasm_bindgen__convert__closures_____invoke__h48d4812d15fc4372: (a: number, b: number) => void;
639
+ readonly wasm_bindgen__convert__closures_____invoke__h10fd13f77519f00f: (a: number, b: number, c: any) => void;
640
+ readonly wasm_bindgen__closure__destroy__h98fb6f0c0578da12: (a: number, b: number) => void;
641
+ readonly wasm_bindgen__convert__closures_____invoke__hdc1b3e8ae3d0b1f6: (a: number, b: number) => void;
642
+ readonly wasm_bindgen__closure__destroy__h166ac8bd1a99e49c: (a: number, b: number) => void;
643
+ readonly wasm_bindgen__convert__closures_____invoke__h00ee2f3d4900533a: (a: number, b: number) => void;
644
+ readonly wasm_bindgen__convert__closures_____invoke__he310f2359e2413f2: (a: number, b: number, c: any) => void;
596
645
  readonly __wbindgen_malloc: (a: number, b: number) => number;
597
646
  readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
598
647
  readonly __wbindgen_exn_store: (a: number) => void;
@@ -636,11 +685,12 @@ type __pkg_riscv_vm_WorkerState = WorkerState;
636
685
  declare const __pkg_riscv_vm_WorkerState: typeof WorkerState;
637
686
  type __pkg_riscv_vm_WorkerStepResult = WorkerStepResult;
638
687
  declare const __pkg_riscv_vm_WorkerStepResult: typeof WorkerStepResult;
688
+ declare const __pkg_riscv_vm_bench_workload: typeof bench_workload;
639
689
  declare const __pkg_riscv_vm_initSync: typeof initSync;
640
690
  declare const __pkg_riscv_vm_worker_check_interrupts: typeof worker_check_interrupts;
641
691
  declare const __pkg_riscv_vm_worker_entry: typeof worker_entry;
642
692
  declare namespace __pkg_riscv_vm {
643
- export { type __pkg_riscv_vm_InitInput as InitInput, type __pkg_riscv_vm_InitOutput as InitOutput, __pkg_riscv_vm_NetworkStatus as NetworkStatus, type __pkg_riscv_vm_SyncInitInput as SyncInitInput, __pkg_riscv_vm_WasmVm as WasmVm, __pkg_riscv_vm_WorkerState as WorkerState, __pkg_riscv_vm_WorkerStepResult as WorkerStepResult, __wbg_init as default, __pkg_riscv_vm_initSync as initSync, __pkg_riscv_vm_worker_check_interrupts as worker_check_interrupts, __pkg_riscv_vm_worker_entry as worker_entry };
693
+ export { type __pkg_riscv_vm_InitInput as InitInput, type __pkg_riscv_vm_InitOutput as InitOutput, __pkg_riscv_vm_NetworkStatus as NetworkStatus, type __pkg_riscv_vm_SyncInitInput as SyncInitInput, __pkg_riscv_vm_WasmVm as WasmVm, __pkg_riscv_vm_WorkerState as WorkerState, __pkg_riscv_vm_WorkerStepResult as WorkerStepResult, __pkg_riscv_vm_bench_workload as bench_workload, __wbg_init as default, __pkg_riscv_vm_initSync as initSync, __pkg_riscv_vm_worker_check_interrupts as worker_check_interrupts, __pkg_riscv_vm_worker_entry as worker_entry };
644
694
  }
645
695
 
646
696
  /**