use works more like fn, for now
This commit is contained in:
parent
f25ec024c2
commit
bf1196bf96
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
})
|
||||
})
|
||||
Loading…
Reference in New Issue
Block a user