Skip to content

Commit

Permalink
Match use of ‘async’ in Ark 0.1.14, and restore ‘use js’ test
Browse files Browse the repository at this point in the history
Now we have dynamic import() working, and hence ‘use js’, test it.

Subsequent commits will switch to ‘use js’ from our current manual bindings.
  • Loading branch information
rrthomas committed Nov 29, 2023
1 parent a4f7645 commit 3f6693e
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 14 deletions.
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Version 0.2

* Use `use js` to move most of the globals into the prelude.
* Sets: use `{ Exp, }` notation, with mandatory first comma.
* Map interface to file system and internet.

Expand Down
10 changes: 5 additions & 5 deletions src/basics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ testGroup('Conditionals', [
['if 3 + 4 == 8 {1} else if 3 + 4 == 7 {2} else {3}', 2],
])

test('loop and break', (t) => {
test('loop and break', async (t) => {
const error = t.throws(() => new ArkState().run(compile('break')), {instanceOf: UrsaCompilerError})
assert(error !== undefined)
t.is(error.message, `\
Expand All @@ -70,19 +70,19 @@ Line 1, col 1:
^~~~~
break used outside a loop`)
t.is(toJs(new ArkState().run(compile('loop { break 3 }'))), 3)
t.is(toJs(await new ArkState().run(compile('loop { break 3 }'))), 3)
})

test('return', (t) => {
const error = t.throws(() => new ArkState().run(compile('return')), {instanceOf: UrsaCompilerError})
test('return', async (t) => {
const error = await t.throwsAsync(async () => new ArkState().run(compile('return')), {instanceOf: UrsaCompilerError})
assert(error !== undefined)
t.is(error.message, `\
Line 1, col 1:
> 1 | return
^~~~~~
return used outside a function`)
t.is(toJs(new ArkState().run(compile('fn () { return 3 }()'))), 3)
t.is(toJs(await new ArkState().run(compile('fn () { return 3 }()'))), 3)
})

testGroup('let', [
Expand Down
6 changes: 3 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ async function repl() {
env = env.push(compiled.boundVars)
ark.stack.push(Array(compiled.boundVars.length).fill(new ArkValRef(ArkUndefined)))
}
val = toJs(runWithTraceback(ark, compiled))
val = toJs(await runWithTraceback(ark, compiled))
console.dir(val, {depth: null})
}
} catch (error) {
Expand Down Expand Up @@ -148,7 +148,7 @@ async function main() {
fs.readFileSync(path.join(thisDir, 'prelude.ursa'), {encoding: 'utf-8'}),
)
const ark = new ArkState()
const preludeObj = prelude.value.eval(ark) as ArkObject
const preludeObj = await prelude.value.eval(ark) as ArkObject
for (const [sym, val] of preludeObj.val) {
globals.set(sym, new ArkValRef(val))
}
Expand All @@ -168,7 +168,7 @@ async function main() {
// Run the program
let result
if (source !== undefined) {
result = runWithTraceback(ark, compile(source))
result = await runWithTraceback(ark, compile(source))
}
if (source === undefined || args.interactive) {
result = await repl()
Expand Down
4 changes: 2 additions & 2 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -833,9 +833,9 @@ export function compile(
return new PartialCompiledArk(compiled, freeVars, ast.boundVars)
}

export function runWithTraceback(ark: ArkState, compiledVal: CompiledArk): ArkVal {
export async function runWithTraceback(ark: ArkState, compiledVal: CompiledArk): Promise<ArkVal> {
try {
return ark.run(compiledVal)
return await ark.run(compiledVal)
} catch (e) {
if (e instanceof ArkRuntimeError) {
throw new UrsaRuntimeError(ark, e.sourceLoc as Interval, e.message)
Expand Down
3 changes: 1 addition & 2 deletions src/examples.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ test('Test I/O', 'test/print', [], 'Hello, world!')

test("'fs' module", 'test/fs', [], 'foo')

// FIXME: make this work again
// test('use fs', 'test/use-fs', 'foo')
test('use fs', 'test/use-fs', [], 'foo')

test('Find symbols in input', 'test/syms', ['./test/fs.ursa'], 'fs\nwriteSync\nfoo\nis\nstdout')

Expand Down
5 changes: 3 additions & 2 deletions src/testutil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ function doTestGroup(
compile: (expr: string) => CompiledArk,
tests: [string, any][],
) {
test(title, (t) => {
test(title, async (t) => {
for (const [source, expected] of tests) {
const compiled = compile(source)
if (process.env.DEBUG) {
debug(compiled, null)
}
t.deepEqual(toJs(new ArkState().run(compiled)), expected)
// eslint-disable-next-line no-await-in-loop
t.deepEqual(toJs(await new ArkState().run(compiled)), expected)
}
})
}
Expand Down

0 comments on commit 3f6693e

Please sign in to comment.