TypeScriptFunction, more exports
This commit is contained in:
parent
e1e7cdf1ef
commit
46829df28b
12
src/index.ts
12
src/index.ts
|
|
@ -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 { wrapNative } from "./function"
|
|
||||||
export { type Value, toValue, toString, toNumber, fromValue, toNull } from "./value"
|
|
||||||
export { VM } from "./vm"
|
|
||||||
export { bytecodeToString } from "./format"
|
export { bytecodeToString } from "./format"
|
||||||
|
export { wrapNative } from "./function"
|
||||||
|
export { Scope } from "./scope"
|
||||||
|
export type { Value, TypeScriptFunction, NativeFunction } from "./value"
|
||||||
|
export { toValue, toString, toNumber, fromValue, toNull } from "./value"
|
||||||
|
export { VM } from "./vm"
|
||||||
|
|
@ -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 }
|
||||||
|
|
|
||||||
|
|
@ -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>' })
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user