From 46829df28b2eab46ed18aa326b9fb4c61cba277b Mon Sep 17 00:00:00 2001 From: Chris Wanstrath Date: Sat, 25 Oct 2025 07:55:58 -0700 Subject: [PATCH] TypeScriptFunction, more exports --- src/index.ts | 12 +++++++----- src/value.ts | 1 + src/vm.ts | 9 ++++----- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/index.ts b/src/index.ts index 0c23d00..f6f4cc3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,14 +1,16 @@ import type { Bytecode } from "./bytecode" -import { type Value } from "./value" +import type { Value, TypeScriptFunction } from "./value" import { VM } from "./vm" -export async function run(bytecode: Bytecode, functions?: Record): Promise { +export async function run(bytecode: Bytecode, functions?: Record): Promise { const vm = new VM(bytecode, functions) return await vm.run() } export { type Bytecode, toBytecode, type ProgramItem } from "./bytecode" +export { bytecodeToString } from "./format" export { wrapNative } from "./function" -export { type Value, toValue, toString, toNumber, fromValue, toNull } from "./value" -export { VM } from "./vm" -export { bytecodeToString } from "./format" \ No newline at end of file +export { Scope } from "./scope" +export type { Value, TypeScriptFunction, NativeFunction } from "./value" +export { toValue, toString, toNumber, fromValue, toNull } from "./value" +export { VM } from "./vm" \ No newline at end of file diff --git a/src/value.ts b/src/value.ts index 330fb63..47fe7f9 100644 --- a/src/value.ts +++ b/src/value.ts @@ -3,6 +3,7 @@ import { Scope } from "./scope" import { VM } from "./vm" export type NativeFunction = (...args: Value[]) => Promise | Value +export type TypeScriptFunction = (this: VM, ...args: any[]) => any export type Value = | { type: 'null', value: null } diff --git a/src/vm.ts b/src/vm.ts index be574e0..818944a 100644 --- a/src/vm.ts +++ b/src/vm.ts @@ -3,11 +3,10 @@ import type { ExceptionHandler } from "./exception" import { type Frame } from "./frame" import { OpCode } from "./opcode" import { Scope } from "./scope" -import { type Value, type NativeFunction, toValue, toNumber, isTrue, isEqual, toString, fromValue, fnFromValue } from "./value" +import type { Value, NativeFunction, TypeScriptFunction } from "./value" +import { toValue, toNumber, isTrue, isEqual, toString, fromValue, fnFromValue } from "./value" import { extractParamInfo, wrapNative, isWrapped, getOriginalFunction } from "./function" -type Fn = (this: VM, ...args: any[]) => any - export class VM { pc = 0 stopped = false @@ -20,7 +19,7 @@ export class VM { labels: Map = new Map() nativeFunctions: Map = new Map() - constructor(bytecode: Bytecode, functions?: Record) { + constructor(bytecode: Bytecode, functions?: Record) { this.instructions = bytecode.instructions this.constants = bytecode.constants this.labels = bytecode.labels || new Map() @@ -45,7 +44,7 @@ export class VM { } } - registerFunction(name: string, fn: Fn) { + registerFunction(name: string, fn: TypeScriptFunction) { const wrapped = isWrapped(fn) ? fn as NativeFunction : wrapNative(this, fn) this.scope.set(name, { type: 'native', fn: wrapped, value: '' }) }