From 3aa40ae2c2413a60118e552b12069e694ec6a1be Mon Sep 17 00:00:00 2001 From: Chris Wanstrath Date: Fri, 7 Nov 2025 21:50:01 -0800 Subject: [PATCH] fix fromValue --- src/index.ts | 18 ++++++------------ src/tests/shrimp.test.ts | 2 +- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/index.ts b/src/index.ts index c1f4835..687e513 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ import { readFileSync } from 'fs' -import { VM, fromValue, toValue, type Bytecode } from 'reefvm' +import { VM, fromValue, toValue, isValue, type Bytecode } from 'reefvm' import { type Tree } from '@lezer/common' import { Compiler } from '#compiler/compiler' import { parser } from '#parser/shrimp' @@ -11,7 +11,7 @@ export { parser } from '#parser/shrimp' export { globals as prelude } from '#prelude' export type { Tree } from '@lezer/common' export { type Value, type Bytecode } from 'reefvm' -export { toValue, fromValue, Scope, VM, bytecodeToString } from 'reefvm' +export { toValue, fromValue, isValue, Scope, VM, bytecodeToString } from 'reefvm' export class Shrimp { vm: VM @@ -25,7 +25,7 @@ export class Shrimp { get(name: string): any { const value = this.vm.scope.get(name) - return value ? fromValue(value) : null + return value ? fromValue(value, this.vm) : null } set(name: string, value: any) { @@ -38,12 +38,7 @@ export class Shrimp { async call(name: string, ...args: any[]): Promise { const result = await this.vm.call(name, ...args) - // vm.call() returns Value for native functions, JS values for Reef functions - // Check if it's a Value object and unwrap if needed - if (result && typeof result === 'object' && 'type' in result) { - return fromValue(result) - } - return result + return isValue(result) ? fromValue(result, this.vm) : result } parse(code: string): Tree { @@ -54,7 +49,6 @@ export class Shrimp { return compileCode(code, this.globals) } - async run(code: string | Bytecode, locals?: Record): Promise { let bytecode @@ -70,7 +64,7 @@ export class Shrimp { await this.vm.continue() if (locals) this.vm.popScope() - return this.vm.stack.length ? fromValue(this.vm.stack.at(-1)!) : null + return this.vm.stack.length ? fromValue(this.vm.stack.at(-1)!, this.vm) : null } } @@ -87,7 +81,7 @@ export async function runCode(code: string, globals?: Record): Prom export async function runBytecode(bytecode: Bytecode, globals?: Record): Promise { const vm = new VM(bytecode, Object.assign({}, shrimpGlobals, globals)) await vm.run() - return vm.stack.length ? fromValue(vm.stack[vm.stack.length - 1]!) : null + return vm.stack.length ? fromValue(vm.stack[vm.stack.length - 1]!, vm) : null } export function compileFile(path: string, globals?: Record): Bytecode { diff --git a/src/tests/shrimp.test.ts b/src/tests/shrimp.test.ts index e905901..b838cf5 100644 --- a/src/tests/shrimp.test.ts +++ b/src/tests/shrimp.test.ts @@ -136,7 +136,7 @@ describe('Shrimp', () => { const shrimp = new Shrimp() await shrimp.run(`greet = do name: - concat 'Hello ' name + str.join [ 'Hello ' name ] '' end`) const result = await shrimp.call('greet', { name: 'World' })