Skip to content

Commit

Permalink
Fix diagnosis of undefined symbols (fix #56)
Browse files Browse the repository at this point in the history
We were testing for `undefined` in our final check for whether the
symbol was a global, but were getting `ArkUndefined` as the return
result. A shame that TypeScript doesn't flag this up!
  • Loading branch information
rrthomas committed Jul 24, 2024
1 parent 5b79697 commit ba93dcd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/ark/basics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ import {compile} from './reader.js'
import {testArkGroup as testGroup} from '../testutil.js'
import {expToInst} from './flatten.js'

test('Undefined symbol', (t) => {
const error = t.throws(() => new ArkState(expToInst(compile(['f']))).run())
t.not(error, undefined)
t.is(error.message, 'Undefined symbol f')
})

testGroup('Concrete values', [
['4', 4],
['["str","hello é"]', 'hello é'],
Expand Down
3 changes: 2 additions & 1 deletion src/ark/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
ArkSet, ArkLocal, ArkCapture, ArkListLiteral, ArkObjectLiteral, ArkMapLiteral,
ArkFn, ArkGenerator, ArkReturn, ArkYield,
ArkProperty, ArkLet, ArkCall, ArkLiteral, ArkObject,
ArkUndefined,
} from './interpreter.js'
import {expToInst} from './flatten.js'

Expand Down Expand Up @@ -124,7 +125,7 @@ export function symRef(env: Environment, name: string): ArkLvalue {
}
// Finally, see if it's a global, and if not, error.
if (lexp === undefined) {
if (env.externalSyms.get(name) === undefined) {
if (env.externalSyms.get(name) === ArkUndefined) {
throw new ArkCompilerError(`Undefined symbol ${name}`)
}
lexp = new ArkProperty(new ArkLiteral(env.externalSyms), name)
Expand Down

0 comments on commit ba93dcd

Please sign in to comment.