use works more like fn, for now

This commit is contained in:
Chris Wanstrath 2025-10-28 22:20:03 -07:00
parent f25ec024c2
commit bf1196bf96
2 changed files with 29 additions and 15 deletions

View File

@ -78,7 +78,7 @@ export const globalFunctions = {
},
// modules
use: async function (this: VM, path: string) {
use: async function (this: VM, path: string): Promise<Record<string, Value>> {
const scope = this.scope
const pc = this.pc
@ -92,16 +92,16 @@ export const globalFunctions = {
await this.continue()
const module: Map<string, Value> = new Map
const module: Record<string, Value> = {}
for (const [name, value] of this.scope.locals.entries())
module.set(name, value)
module[name] = value
this.scope = scope
this.pc = pc
this.stopped = false
this.scope.set(parse(fullPath).name, { type: 'dict', value: module })
}
return module
},
}
export function formatValue(value: Value, inner = false): string {

View File

@ -4,25 +4,39 @@ import { globalFunctions } from '#prelude'
describe('use', () => {
test(`imports all a file's functions`, async () => {
expect(`
use ./src/prelude/tests/math
dbl = math | at double
dbl 4
math = use ./src/prelude/tests/math
math.double 4
`).toEvaluateTo(8, globalFunctions)
expect(`
use ./src/prelude/tests/math
math | at pi
math = use ./src/prelude/tests/math
math.double (math.double 4)
`).toEvaluateTo(16, globalFunctions)
expect(`
math = use ./src/prelude/tests/math
dbl = math.double
dbl (dbl 2)
`).toEvaluateTo(8, globalFunctions)
expect(`
math = use ./src/prelude/tests/math
math.pi
`).toEvaluateTo(3.14, globalFunctions)
expect(`
use ./src/prelude/tests/math
math = use ./src/prelude/tests/math
math | at 🥧
`).toEvaluateTo(3.14159265359, globalFunctions)
expect(`
use ./src/prelude/tests/math
call = do x y: x y end
call (math | at add1) 5
math = use ./src/prelude/tests/math
math.🥧
`).toEvaluateTo(3.14159265359, globalFunctions)
expect(`
math = use ./src/prelude/tests/math
math.add1 5
`).toEvaluateTo(6, globalFunctions)
})
})
})