Skip to content
3 changes: 2 additions & 1 deletion src/commons/assessment/AssessmentTypes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Chapter, SourceError, Variant } from 'js-slang/dist/types';
import { Chapter, LanguageOptions, SourceError, Variant } from 'js-slang/dist/types';

import { ExternalLibrary, ExternalLibraryName } from '../application/types/ExternalTypes';

Expand Down Expand Up @@ -177,6 +177,7 @@ export type Library = {
2?: string; // For mission control
}>;
moduleParams?: any;
languageOptions?: LanguageOptions;
};

export type Testcase = {
Expand Down
9 changes: 7 additions & 2 deletions src/commons/sagas/WorkspaceSaga/helpers/blockExtraMethods.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { Context } from 'js-slang';
import { Context } from 'js-slang';
import { Variant } from 'js-slang/dist/types';
import { call } from 'redux-saga/effects';

import {
getBlockExtraMethodsString,
getBlockExtraMethodsStringTypedVariant,
getDifferenceInMethods,
getStoreExtraMethodsString
} from '../../../utils/JsSlangHelper';
Expand Down Expand Up @@ -35,7 +37,10 @@ export function* blockExtraMethods(
);
}

const nullifier = getBlockExtraMethodsString(toBeBlocked);
const nullifier =
context.variant === Variant.TYPED
? getBlockExtraMethodsStringTypedVariant(toBeBlocked)
: getBlockExtraMethodsString(toBeBlocked);
const nullifierFilePath = '/nullifier.js';
const nullifierFiles = {
[nullifierFilePath]: nullifier
Expand Down
26 changes: 19 additions & 7 deletions src/commons/sagas/WorkspaceSaga/helpers/clearContext.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
import type { Context } from 'js-slang';
import { defineSymbol } from 'js-slang/dist/createContext';
import { LanguageOptions, Variant } from 'js-slang/dist/types';
import { put, select, take } from 'redux-saga/effects';
import { ExternalLibraryName } from 'src/commons/application/types/ExternalTypes';
import WorkspaceActions from 'src/commons/workspace/WorkspaceActions';

import type { OverallState } from '../../../application/ApplicationTypes';
import { actions } from '../../../utils/ActionsHelper';
import type { WorkspaceLocation } from '../../../workspace/WorkspaceTypes';
import { selectWorkspace } from '../../SafeEffects';

export function* clearContext(workspaceLocation: WorkspaceLocation, entrypointCode: string) {
const {
context: { chapter, externalSymbols: symbols, variant },
externalLibrary: externalLibraryName,
globals
} = yield* selectWorkspace(workspaceLocation);
const [chapter, symbols, externalLibraryName, globals, variant, languageOptions]: [
number,
string[],
ExternalLibraryName,
Array<[string, any]>,
Variant,
LanguageOptions
] = yield select((state: OverallState) => [
state.workspaces[workspaceLocation].context.chapter,
state.workspaces[workspaceLocation].context.externalSymbols,
state.workspaces[workspaceLocation].externalLibrary,
state.workspaces[workspaceLocation].globals,
state.workspaces[workspaceLocation].context.variant,
state.workspaces[workspaceLocation].context.languageOptions
]);

const library = {
chapter,
Expand All @@ -22,7 +33,8 @@ export function* clearContext(workspaceLocation: WorkspaceLocation, entrypointCo
name: externalLibraryName,
symbols
},
globals
globals,
languageOptions
};

// Clear the context, with the same chapter and externalSymbols as before.
Expand Down
30 changes: 20 additions & 10 deletions src/commons/sagas/WorkspaceSaga/helpers/evalEditor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { FSModule } from 'browserfs/dist/node/core/FS';
import { FSModule } from 'browserfs/dist/node/core/FS';
import { Variant } from 'js-slang/dist/types';
import { call, put, select, StrictEffect } from 'redux-saga/effects';
import WorkspaceActions from 'src/commons/workspace/WorkspaceActions';

Expand Down Expand Up @@ -82,19 +83,28 @@
const prependFiles = {
[prependFilePath]: prepend
};
yield call(
evalCodeSaga,
prependFiles,
prependFilePath,
elevatedContext,
execTime,
EVAL_SILENT,
workspaceLocation
);
if (context.variant !== Variant.TYPED) {
yield call(
evalCodeSaga,

Check failure on line 88 in src/commons/sagas/WorkspaceSaga/helpers/evalEditor.ts

View workflow job for this annotation

GitHub Actions / lint (build)

No overload matches this call.

Check failure on line 88 in src/commons/sagas/WorkspaceSaga/helpers/evalEditor.ts

View workflow job for this annotation

GitHub Actions / lint (tsc)

No overload matches this call.
prependFiles,
prependFilePath,
elevatedContext,
execTime,
workspaceLocation,
EVAL_SILENT
);
}

// Block use of methods from privileged context
yield* blockExtraMethods(elevatedContext, context, execTime, workspaceLocation);
}

if (context.variant === Variant.TYPED) {
// Prepend was multi-line, now we need to split them by \n and join them
// This is to avoid extra lines in the editor which affects the error message location
const prependSingleLine = prepend.split('\n').join('');
files[entrypointFilePath] = prependSingleLine + files[entrypointFilePath];
}
yield call(
evalCodeSaga,
files,
Expand Down
11 changes: 11 additions & 0 deletions src/commons/utils/JsSlangHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ function loadStandardLibraries(proxyContext: Context, customBuiltIns: CustomBuil
// intercepts reads from the underlying Context and returns desired values
export function makeElevatedContext(context: Context) {
function ProxyFrame() {}

ProxyFrame.prototype = context.runtime.environments[0].head;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Expand Down Expand Up @@ -258,3 +259,13 @@ export function getBlockExtraMethodsString(toRemove: string[]) {
)
.join('\n');
}

export function getBlockExtraMethodsStringTypedVariant(toRemove: string[]) {
return toRemove
.map(x =>
x === 'makeUndefinedErrorFunction'
? ''
: `const ${x} : string = makeUndefinedErrorFunction('${x}');`
)
.join('\n');
}
3 changes: 2 additions & 1 deletion src/commons/workspace/WorkspaceReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ const newWorkspaceReducer = createReducer(defaultWorkspaceManager, builder => {
action.payload.library.chapter,
action.payload.library.external.symbols,
workspaceLocation,
action.payload.library.variant
action.payload.library.variant,
action.payload.library.languageOptions
),
globals: action.payload.library.globals,
externalLibrary: action.payload.library.external.name
Expand Down
Loading