Compare commits

..

No commits in common. "42c0e62597a3259a48b4c43bd2f7291e94a60912" and "e1e7cdf1ef8027bba91237187b2306d10777dfb9" have entirely different histories.

5 changed files with 10 additions and 16 deletions

View File

@ -104,8 +104,6 @@ function formatValue(value: Value): string {
} else if (value.type === 'function') {
const params = value.params.join(', ')
return `${colors.dim}<fn(${params})>${colors.reset}`
} else if (value.type === 'native') {
return `${colors.dim}<native>${colors.reset}`
}
return String(value)
}

View File

@ -38,8 +38,6 @@ function formatValue(value: Value): string {
} else if (value.type === 'function') {
const params = value.params.join(', ')
return `${colors.dim}<fn(${params})>${colors.reset}`
} else if (value.type === 'native') {
return `${colors.dim}<native>${colors.reset}`
}
return String(value)
}

View File

@ -1,16 +1,14 @@
import type { Bytecode } from "./bytecode"
import type { Value, TypeScriptFunction } from "./value"
import { type Value } from "./value"
import { VM } from "./vm"
export async function run(bytecode: Bytecode, functions?: Record<string, TypeScriptFunction>): Promise<Value> {
export async function run(bytecode: Bytecode, functions?: Record<string, Function>): Promise<Value> {
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 { Scope } from "./scope"
export type { Value, TypeScriptFunction, NativeFunction } from "./value"
export { toValue, toString, toNumber, fromValue, toNull } from "./value"
export { VM } from "./vm"
export { type Value, toValue, toString, toNumber, fromValue, toNull } from "./value"
export { VM } from "./vm"
export { bytecodeToString } from "./format"

View File

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

View File

@ -3,10 +3,11 @@ import type { ExceptionHandler } from "./exception"
import { type Frame } from "./frame"
import { OpCode } from "./opcode"
import { Scope } from "./scope"
import type { Value, NativeFunction, TypeScriptFunction } from "./value"
import { toValue, toNumber, isTrue, isEqual, toString, fromValue, fnFromValue } from "./value"
import { type Value, type NativeFunction, 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
@ -19,7 +20,7 @@ export class VM {
labels: Map<number, string> = new Map()
nativeFunctions: Map<string, NativeFunction> = new Map()
constructor(bytecode: Bytecode, functions?: Record<string, TypeScriptFunction>) {
constructor(bytecode: Bytecode, functions?: Record<string, Fn>) {
this.instructions = bytecode.instructions
this.constants = bytecode.constants
this.labels = bytecode.labels || new Map()
@ -44,7 +45,7 @@ export class VM {
}
}
registerFunction(name: string, fn: TypeScriptFunction) {
registerFunction(name: string, fn: Fn) {
const wrapped = isWrapped(fn) ? fn as NativeFunction : wrapNative(this, fn)
this.scope.set(name, { type: 'native', fn: wrapped, value: '<function>' })
}