TypeScriptFunction, more exports

This commit is contained in:
Chris Wanstrath 2025-10-25 07:55:58 -07:00
parent e1e7cdf1ef
commit 46829df28b
3 changed files with 12 additions and 10 deletions

View File

@ -1,14 +1,16 @@
import type { Bytecode } from "./bytecode" import type { Bytecode } from "./bytecode"
import { type Value } from "./value" import type { Value, TypeScriptFunction } from "./value"
import { VM } from "./vm" import { VM } from "./vm"
export async function run(bytecode: Bytecode, functions?: Record<string, Function>): Promise<Value> { export async function run(bytecode: Bytecode, functions?: Record<string, TypeScriptFunction>): Promise<Value> {
const vm = new VM(bytecode, functions) const vm = new VM(bytecode, functions)
return await vm.run() return await vm.run()
} }
export { type Bytecode, toBytecode, type ProgramItem } from "./bytecode" export { type Bytecode, toBytecode, type ProgramItem } from "./bytecode"
export { bytecodeToString } from "./format"
export { wrapNative } from "./function" export { wrapNative } from "./function"
export { type Value, toValue, toString, toNumber, fromValue, toNull } from "./value" export { Scope } from "./scope"
export { VM } from "./vm" export type { Value, TypeScriptFunction, NativeFunction } from "./value"
export { bytecodeToString } from "./format" export { toValue, toString, toNumber, fromValue, toNull } from "./value"
export { VM } from "./vm"

View File

@ -3,6 +3,7 @@ import { Scope } from "./scope"
import { VM } from "./vm" import { VM } from "./vm"
export type NativeFunction = (...args: Value[]) => Promise<Value> | Value export type NativeFunction = (...args: Value[]) => Promise<Value> | Value
export type TypeScriptFunction = (this: VM, ...args: any[]) => any
export type Value = export type Value =
| { type: 'null', value: null } | { type: 'null', value: null }

View File

@ -3,11 +3,10 @@ import type { ExceptionHandler } from "./exception"
import { type Frame } from "./frame" import { type Frame } from "./frame"
import { OpCode } from "./opcode" import { OpCode } from "./opcode"
import { Scope } from "./scope" 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" import { extractParamInfo, wrapNative, isWrapped, getOriginalFunction } from "./function"
type Fn = (this: VM, ...args: any[]) => any
export class VM { export class VM {
pc = 0 pc = 0
stopped = false stopped = false
@ -20,7 +19,7 @@ export class VM {
labels: Map<number, string> = new Map() labels: Map<number, string> = new Map()
nativeFunctions: Map<string, NativeFunction> = new Map() nativeFunctions: Map<string, NativeFunction> = new Map()
constructor(bytecode: Bytecode, functions?: Record<string, Fn>) { constructor(bytecode: Bytecode, functions?: Record<string, TypeScriptFunction>) {
this.instructions = bytecode.instructions this.instructions = bytecode.instructions
this.constants = bytecode.constants this.constants = bytecode.constants
this.labels = bytecode.labels || new Map() 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) const wrapped = isWrapped(fn) ? fn as NativeFunction : wrapNative(this, fn)
this.scope.set(name, { type: 'native', fn: wrapped, value: '<function>' }) this.scope.set(name, { type: 'native', fn: wrapped, value: '<function>' })
} }