Skip to content

Commit

Permalink
fix: missing functions in Source Native variants (#1431)
Browse files Browse the repository at this point in the history
* chore: remove `console.log`

* fix: keep track of declared identifiers

* test: add regression tests for Source Native
  • Loading branch information
shenyih0ng authored Jul 12, 2023
1 parent 8618e26 commit d826f84
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
31 changes: 31 additions & 0 deletions src/runner/__tests__/runners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,37 @@ describe('Additional JavaScript features are not available in Source Native', ()
)
})

describe('Functions in Source libraries (e.g. list, streams) are available in Source Native', () => {
test('List functions are present in Source Native', async () => {
// Test chapters from Source 2 - 4
for (let chapterNum = 2; chapterNum <= 4; chapterNum++) {
const sourceNativeContext: Context = mockContext(chapterNum, Variant.NATIVE)
// The following snippet is equivalent to sum(list(1..10))
const sourceNativeSnippet: string =
'accumulate((x, y) => x + y , 0, append(build_list(x => x + 1, 5), enum_list(6, 10)));'
const result = await runInContext(sourceNativeSnippet, sourceNativeContext)

expect(result.status).toStrictEqual('finished')
expect((result as any).value).toStrictEqual(55)
expect(sourceNativeContext.errors.length).toBe(0)
}
})
test('Stream functions are present in Source Native', async () => {
// Test chapters from Source 3 - 4
for (let chapterNum = 3; chapterNum <= 4; chapterNum++) {
const sourceNativeContext: Context = mockContext(chapterNum, Variant.NATIVE)
// The following snippet is equivalent to sum(list(stream(1..10)))
const sourceNativeSnippet: string =
'accumulate((x, y) => x + y, 0, stream_to_list(stream_append(build_stream(x => x + 1, 5), enum_stream(6, 10))));'
const result = await runInContext(sourceNativeSnippet, sourceNativeContext)

expect(result.status).toStrictEqual('finished')
expect((result as any).value).toStrictEqual(55)
expect(sourceNativeContext.errors.length).toBe(0)
}
})
})

// HTML Unit Tests

test('Error handling script is injected in HTML code', async () => {
Expand Down
4 changes: 4 additions & 0 deletions src/runner/fullJSRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { parse } from '../parser/parser'
import { evallerReplacer, getBuiltins, transpile } from '../transpiler/transpiler'
import type { Context, NativeStorage } from '../types'
import * as create from '../utils/astCreator'
import { getIdentifiersInProgram } from '../utils/uniqueIds'
import { toSourceError } from './errors'
import { appendModulesToContext, resolvedErrorPromise } from './utils'

Expand Down Expand Up @@ -69,6 +70,9 @@ export async function fullJSRunner(
...preludeAndBuiltins,
evallerReplacer(create.identifier(NATIVE_STORAGE_ID), new Set())
])
getIdentifiersInProgram(preEvalProgram).forEach(id =>
context.nativeStorage.previousProgramsIdentifiers.add(id)
)
const preEvalCode: string = generate(preEvalProgram)
const requireProvider = getRequireProvider(context)
await fullJSEval(preEvalCode, requireProvider, context.nativeStorage)
Expand Down
1 change: 0 additions & 1 deletion src/runner/sourceRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ async function runNative(
value
})
} catch (error) {
// console.error(error)
const isDefaultVariant = options.variant === undefined || options.variant === Variant.DEFAULT
if (isDefaultVariant && isPotentialInfiniteLoop(error)) {
const detectedInfiniteLoop = testForInfiniteLoop(program, context.previousPrograms.slice(1))
Expand Down
3 changes: 3 additions & 0 deletions src/transpiler/transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,9 @@ function transpileToFullJS(
globalIds.native
)

getGloballyDeclaredIdentifiers(program).forEach(id =>
context.nativeStorage.previousProgramsIdentifiers.add(id)
)
const transpiledProgram: es.Program = create.program([
evallerReplacer(create.identifier(NATIVE_STORAGE_ID), new Set()),
create.expressionStatement(create.identifier('undefined')),
Expand Down

0 comments on commit d826f84

Please sign in to comment.