Skip to content
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
2 changes: 2 additions & 0 deletions src/__tests__/__snapshots__/environment.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ Array [
"executionMethod": "auto",
"externalContext": undefined,
"externalSymbols": Array [],
"moduleParams": undefined,
"nativeStorage": Object {
"globals": Object {
"previousScope": null,
Expand Down Expand Up @@ -948,6 +949,7 @@ Array [
"executionMethod": "auto",
"externalContext": undefined,
"externalSymbols": Array [],
"moduleParams": undefined,
"nativeStorage": Object {
"globals": Object {
"previousScope": null,
Expand Down
30 changes: 30 additions & 0 deletions src/__tests__/__snapshots__/inspect.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Object {
"executionMethod": "interpreter",
"externalContext": undefined,
"externalSymbols": Array [],
"moduleParams": undefined,
"nativeStorage": Object {
"globals": Object {
"previousScope": null,
Expand Down Expand Up @@ -2212,6 +2213,7 @@ Object {
"executionMethod": "interpreter",
"externalContext": undefined,
"externalSymbols": Array [],
"moduleParams": undefined,
"nativeStorage": Object {
"globals": Object {
"previousScope": null,
Expand Down Expand Up @@ -4025,6 +4027,7 @@ Object {
"executionMethod": "interpreter",
"externalContext": undefined,
"externalSymbols": Array [],
"moduleParams": undefined,
"nativeStorage": Object {
"globals": Object {
"previousScope": null,
Expand Down Expand Up @@ -4816,6 +4819,7 @@ Object {
"executionMethod": "interpreter",
"externalContext": undefined,
"externalSymbols": Array [],
"moduleParams": undefined,
"nativeStorage": Object {
"globals": Object {
"previousScope": null,
Expand Down Expand Up @@ -5488,6 +5492,7 @@ Object {
"executionMethod": "interpreter",
"externalContext": undefined,
"externalSymbols": Array [],
"moduleParams": undefined,
"nativeStorage": Object {
"globals": Object {
"previousScope": null,
Expand Down Expand Up @@ -6896,6 +6901,7 @@ Object {
"executionMethod": "interpreter",
"externalContext": undefined,
"externalSymbols": Array [],
"moduleParams": undefined,
"nativeStorage": Object {
"globals": Object {
"previousScope": null,
Expand Down Expand Up @@ -8069,6 +8075,7 @@ Object {
"executionMethod": "interpreter",
"externalContext": undefined,
"externalSymbols": Array [],
"moduleParams": undefined,
"nativeStorage": Object {
"globals": Object {
"previousScope": null,
Expand Down Expand Up @@ -10336,6 +10343,7 @@ Object {
"executionMethod": "interpreter",
"externalContext": undefined,
"externalSymbols": Array [],
"moduleParams": undefined,
"nativeStorage": Object {
"globals": Object {
"previousScope": null,
Expand Down Expand Up @@ -25479,6 +25487,7 @@ Object {
"executionMethod": "interpreter",
"externalContext": undefined,
"externalSymbols": Array [],
"moduleParams": undefined,
"nativeStorage": Object {
"globals": Object {
"previousScope": null,
Expand Down Expand Up @@ -27318,6 +27327,7 @@ Object {
"executionMethod": "interpreter",
"externalContext": undefined,
"externalSymbols": Array [],
"moduleParams": undefined,
"nativeStorage": Object {
"globals": Object {
"previousScope": null,
Expand Down Expand Up @@ -29157,6 +29167,7 @@ Object {
"executionMethod": "interpreter",
"externalContext": undefined,
"externalSymbols": Array [],
"moduleParams": undefined,
"nativeStorage": Object {
"globals": Object {
"previousScope": null,
Expand Down Expand Up @@ -30996,6 +31007,7 @@ Object {
"executionMethod": "interpreter",
"externalContext": undefined,
"externalSymbols": Array [],
"moduleParams": undefined,
"nativeStorage": Object {
"globals": Object {
"previousScope": null,
Expand Down Expand Up @@ -32835,6 +32847,7 @@ Object {
"executionMethod": "interpreter",
"externalContext": undefined,
"externalSymbols": Array [],
"moduleParams": undefined,
"nativeStorage": Object {
"globals": Object {
"previousScope": null,
Expand Down Expand Up @@ -33505,6 +33518,7 @@ Object {
"executionMethod": "interpreter",
"externalContext": undefined,
"externalSymbols": Array [],
"moduleParams": undefined,
"nativeStorage": Object {
"globals": Object {
"previousScope": null,
Expand Down Expand Up @@ -34528,6 +34542,7 @@ Object {
"executionMethod": "interpreter",
"externalContext": undefined,
"externalSymbols": Array [],
"moduleParams": undefined,
"nativeStorage": Object {
"globals": Object {
"previousScope": null,
Expand Down Expand Up @@ -35766,6 +35781,7 @@ Object {
"executionMethod": "interpreter",
"externalContext": undefined,
"externalSymbols": Array [],
"moduleParams": undefined,
"nativeStorage": Object {
"globals": Object {
"previousScope": null,
Expand Down Expand Up @@ -36438,6 +36454,7 @@ Object {
"executionMethod": "interpreter",
"externalContext": undefined,
"externalSymbols": Array [],
"moduleParams": undefined,
"nativeStorage": Object {
"globals": Object {
"previousScope": null,
Expand Down Expand Up @@ -37394,6 +37411,7 @@ Object {
"executionMethod": "interpreter",
"externalContext": undefined,
"externalSymbols": Array [],
"moduleParams": undefined,
"nativeStorage": Object {
"globals": Object {
"previousScope": null,
Expand Down Expand Up @@ -38862,6 +38880,7 @@ Object {
"executionMethod": "interpreter",
"externalContext": undefined,
"externalSymbols": Array [],
"moduleParams": undefined,
"nativeStorage": Object {
"globals": Object {
"previousScope": null,
Expand Down Expand Up @@ -40330,6 +40349,7 @@ Object {
"executionMethod": "interpreter",
"externalContext": undefined,
"externalSymbols": Array [],
"moduleParams": undefined,
"nativeStorage": Object {
"globals": Object {
"previousScope": null,
Expand Down Expand Up @@ -41798,6 +41818,7 @@ Object {
"executionMethod": "interpreter",
"externalContext": undefined,
"externalSymbols": Array [],
"moduleParams": undefined,
"nativeStorage": Object {
"globals": Object {
"previousScope": null,
Expand Down Expand Up @@ -43283,6 +43304,7 @@ Object {
"executionMethod": "interpreter",
"externalContext": undefined,
"externalSymbols": Array [],
"moduleParams": undefined,
"nativeStorage": Object {
"globals": Object {
"previousScope": null,
Expand Down Expand Up @@ -43955,6 +43977,7 @@ Object {
"executionMethod": "interpreter",
"externalContext": undefined,
"externalSymbols": Array [],
"moduleParams": undefined,
"nativeStorage": Object {
"globals": Object {
"previousScope": null,
Expand Down Expand Up @@ -45439,6 +45462,7 @@ Object {
"executionMethod": "interpreter",
"externalContext": undefined,
"externalSymbols": Array [],
"moduleParams": undefined,
"nativeStorage": Object {
"globals": Object {
"previousScope": null,
Expand Down Expand Up @@ -46923,6 +46947,7 @@ Object {
"executionMethod": "interpreter",
"externalContext": undefined,
"externalSymbols": Array [],
"moduleParams": undefined,
"nativeStorage": Object {
"globals": Object {
"previousScope": null,
Expand Down Expand Up @@ -47595,6 +47620,7 @@ Object {
"executionMethod": "interpreter",
"externalContext": undefined,
"externalSymbols": Array [],
"moduleParams": undefined,
"nativeStorage": Object {
"globals": Object {
"previousScope": null,
Expand Down Expand Up @@ -49048,6 +49074,7 @@ Object {
"executionMethod": "interpreter",
"externalContext": undefined,
"externalSymbols": Array [],
"moduleParams": undefined,
"nativeStorage": Object {
"globals": Object {
"previousScope": null,
Expand Down Expand Up @@ -50631,6 +50658,7 @@ Object {
"executionMethod": "interpreter",
"externalContext": undefined,
"externalSymbols": Array [],
"moduleParams": undefined,
"nativeStorage": Object {
"globals": Object {
"previousScope": null,
Expand Down Expand Up @@ -52084,6 +52112,7 @@ Object {
"executionMethod": "interpreter",
"externalContext": undefined,
"externalSymbols": Array [],
"moduleParams": undefined,
"nativeStorage": Object {
"globals": Object {
"previousScope": null,
Expand Down Expand Up @@ -53667,6 +53696,7 @@ Object {
"executionMethod": "interpreter",
"externalContext": undefined,
"externalSymbols": Array [],
"moduleParams": undefined,
"nativeStorage": Object {
"globals": Object {
"previousScope": null,
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const CUT = 'cut' // cut operator for Source 4.3
export const TRY_AGAIN = 'try again' // command for Source 4.3
export const GLOBAL = typeof window === 'undefined' ? global : window
export const NATIVE_STORAGE_ID = 'nativeStorage'
export const MODULE_PARAMS_ID = 'moduleParams'
export const MAX_LIST_DISPLAY_LENGTH = 100
export const UNKNOWN_LOCATION: es.SourceLocation = {
start: {
Expand Down
15 changes: 12 additions & 3 deletions src/createContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,15 @@ export const createEmptyContext = <T>(
chapter: number,
variant: Variant = 'default',
externalSymbols: string[],
externalContext?: T
externalContext?: T,
moduleParams?: any
): Context<T> => {
return {
chapter,
externalSymbols,
errors: [],
externalContext,
moduleParams,
runtime: createEmptyRuntime(),
numberOfOuterEnvironments: 1,
prelude: null,
Expand Down Expand Up @@ -268,9 +270,16 @@ const createContext = <T>(
variant: Variant = 'default',
externalSymbols: string[] = [],
externalContext?: T,
externalBuiltIns: CustomBuiltIns = defaultBuiltIns
externalBuiltIns: CustomBuiltIns = defaultBuiltIns,
moduleParams?: any
) => {
const context = createEmptyContext(chapter, variant, externalSymbols, externalContext)
const context = createEmptyContext(
chapter,
variant,
externalSymbols,
externalContext,
moduleParams
)

importBuiltins(context, externalBuiltIns)
importPrelude(context)
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ export async function runInContext(
lastStatementSourceMapJson = temp.evalMap
return Promise.resolve({
status: 'finished',
value: sandboxedEval(transpiled, context.nativeStorage)
value: sandboxedEval(transpiled, context.nativeStorage, context.moduleParams)
} as Result)
} catch (error) {
if (error instanceof RuntimeSourceError) {
Expand Down
4 changes: 2 additions & 2 deletions src/interpreter/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { evaluateBinaryExpression, evaluateUnaryExpression } from '../utils/oper
import * as rttc from '../utils/rttc'
import Closure from './closure'
import { LazyBuiltIn } from '../createContext'
import { loadIIFEModule } from '../modules/moduleLoader'
import { loadModule } from '../modules/moduleLoader'

class BreakValue {}

Expand Down Expand Up @@ -612,7 +612,7 @@ export const evaluators: { [nodeType: string]: Evaluator<es.Node> } = {
ImportDeclaration: function*(node: es.ImportDeclaration, context: Context) {
const moduleName = node.source.value as string
const neededSymbols = node.specifiers.map(spec => spec.local.name)
const module = loadIIFEModule(moduleName)
const module = loadModule(moduleName, context)
declareImports(context, node)
for (const name of neededSymbols) {
defineVariable(context, name, module[name], true);
Expand Down
21 changes: 19 additions & 2 deletions src/modules/__tests__/moduleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,24 @@
* @jest-environment node
*/

import { loadModuleText, loadIIFEModule } from '../moduleLoader'
import { loadModuleText, loadModule } from '../moduleLoader'
import { ModuleNotFound, ModuleInternalError } from '../../errors/errors'
import { stripIndent } from '../../utils/formatters'
import { createEmptyContext } from '../../createContext'

test('Load a valid module', () => {
const path = '_mock_dir/_mock_file'
const moduleText = stripIndent`
(_params) => {
return {
hello: 1
};
}
`
expect(loadModule(path, createEmptyContext(1, 'default', []), moduleText)).toEqual({
hello: 1
})
})

test('Try loading a non-existing module', () => {
const moduleName = '_non_existing_dir/_non_existing_file'
Expand All @@ -17,5 +32,7 @@ test('Try executing a wrongly implemented module', () => {
const wrongModuleText = stripIndent`
export function es6_function(params) {}
`
expect(() => loadIIFEModule(path, wrongModuleText)).toThrow(ModuleInternalError)
expect(() => loadModule(path, createEmptyContext(1, 'default', []), wrongModuleText)).toThrow(
ModuleInternalError
)
})
11 changes: 6 additions & 5 deletions src/modules/moduleLoader.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ModuleNotFound, ModuleInternalError } from '../errors/errors'
import { XMLHttpRequest as NodeXMLHttpRequest } from 'xmlhttprequest-ts'
import { Context } from '..'
const HttpRequest = typeof window === 'undefined' ? NodeXMLHttpRequest : XMLHttpRequest

// TODO: Change this URL to actual Backend URL
let BACKEND_STATIC_URL = 'http://ec2-54-169-81-133.ap-southeast-1.compute.amazonaws.com/static'
let BACKEND_STATIC_URL = 'https://source-academy.github.io/modules'

export function setBackendStaticURL(url: string) {
BACKEND_STATIC_URL = url
Expand All @@ -20,13 +20,14 @@ export function loadModuleText(path: string) {
return req.responseText
}

/* tslint:disable */
export function loadIIFEModule(path: string, moduleText?: string) {
export function loadModule(path: string, context: Context, moduleText?: string) {
try {
if (moduleText === undefined) {
moduleText = loadModuleText(path)
}
return eval(moduleText) as object
// tslint:disable-next-line:no-eval
const moduleLib = eval(moduleText)
return moduleLib({ runes: {}, ...context.moduleParams })
} catch (_error) {
throw new ModuleInternalError(path)
}
Expand Down
Loading