diff --git a/src/prelude/index.ts b/src/prelude/index.ts index 1423ef6..a851706 100644 --- a/src/prelude/index.ts +++ b/src/prelude/index.ts @@ -78,7 +78,7 @@ export const globalFunctions = { }, // modules - use: async function (this: VM, path: string) { + use: async function (this: VM, path: string): Promise> { const scope = this.scope const pc = this.pc @@ -92,16 +92,16 @@ export const globalFunctions = { await this.continue() - const module: Map = new Map + const module: Record = {} 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 { diff --git a/src/prelude/tests/use.test.ts b/src/prelude/tests/use.test.ts index 971e1b5..a8ec265 100644 --- a/src/prelude/tests/use.test.ts +++ b/src/prelude/tests/use.test.ts @@ -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) }) -}) \ No newline at end of file +})