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') { } else if (value.type === 'function') {
const params = value.params.join(', ') const params = value.params.join(', ')
return `${colors.dim}<fn(${params})>${colors.reset}` return `${colors.dim}<fn(${params})>${colors.reset}`
} else if (value.type === 'native') {
return `${colors.dim}<native>${colors.reset}`
} }
return String(value) return String(value)
} }

View File

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

View File

@ -1,16 +1,14 @@
import type { Bytecode } from "./bytecode" import type { Bytecode } from "./bytecode"
import type { Value, TypeScriptFunction } from "./value" import { type Value } from "./value"
import { VM } from "./vm" 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) 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 { Scope } from "./scope" export { type Value, toValue, toString, toNumber, fromValue, toNull } from "./value"
export type { Value, TypeScriptFunction, NativeFunction } from "./value" export { VM } from "./vm"
export { toValue, toString, toNumber, fromValue, toNull } from "./value" export { bytecodeToString } from "./format"
export { VM } from "./vm"

View File

@ -3,7 +3,6 @@ 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,10 +3,11 @@ 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, NativeFunction, TypeScriptFunction } from "./value" import { type Value, type NativeFunction, toValue, toNumber, isTrue, isEqual, toString, fromValue, fnFromValue } 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
@ -19,7 +20,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, TypeScriptFunction>) { constructor(bytecode: Bytecode, functions?: Record<string, Fn>) {
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()
@ -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) 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>' })
} }