From 000eb7ad92499c4696556304485bae4b3531bd92 Mon Sep 17 00:00:00 2001 From: Chris Wanstrath Date: Mon, 6 Oct 2025 09:07:58 -0700 Subject: [PATCH] native example --- examples/native.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 examples/native.ts diff --git a/examples/native.ts b/examples/native.ts new file mode 100644 index 0000000..c4b6344 --- /dev/null +++ b/examples/native.ts @@ -0,0 +1,21 @@ +import { toBytecode } from "#bytecode" +import { VM } from "#vm" +import { type Value, toString, toValue } from "#value" + +const bytecode = toBytecode(` + PUSH 5 + PUSH 10 + ADD + CALL_NATIVE print +`) + +const vm = new VM(bytecode) + +vm.registerFunction('print', (...args: Value[]): Value => { + console.log(...args.map(toString)) + return toValue(null) +}) + +console.write('5 + 10 = ') + +await vm.run() \ No newline at end of file