Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: missing functions in Source Native variants #1431

Merged
merged 3 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading