Skip to content

Language options #3123

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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
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';

Check failure on line 1 in src/commons/assessment/AssessmentTypes.ts

View workflow job for this annotation

GitHub Actions / lint (tsc)

Module '"js-slang/dist/types"' has no exported member 'LanguageOptions'.

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

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

export type Testcase = {
Expand Down
7 changes: 6 additions & 1 deletion src/commons/sagas/WorkspaceSaga/helpers/blockExtraMethods.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
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
13 changes: 8 additions & 5 deletions src/commons/sagas/WorkspaceSaga/helpers/clearContext.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Context } from 'js-slang';
import { defineSymbol } from 'js-slang/dist/createContext';
import { Variant } from 'js-slang/dist/types';
import { LanguageOptions, Variant } from 'js-slang/dist/types';

Check failure on line 3 in src/commons/sagas/WorkspaceSaga/helpers/clearContext.ts

View workflow job for this annotation

GitHub Actions / lint (tsc)

Module '"js-slang/dist/types"' has no exported member 'LanguageOptions'.
import { put, select, take } from 'redux-saga/effects';
import WorkspaceActions from 'src/commons/workspace/WorkspaceActions';

Expand All @@ -10,18 +10,20 @@
import { WorkspaceLocation } from '../../../workspace/WorkspaceTypes';

export function* clearContext(workspaceLocation: WorkspaceLocation, entrypointCode: string) {
const [chapter, symbols, externalLibraryName, globals, variant]: [
const [chapter, symbols, externalLibraryName, globals, variant, languageOptions]: [
number,
string[],
ExternalLibraryName,
Array<[string, any]>,
Variant
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.variant,
state.workspaces[workspaceLocation].context.languageOptions

Check failure on line 26 in src/commons/sagas/WorkspaceSaga/helpers/clearContext.ts

View workflow job for this annotation

GitHub Actions / lint (tsc)

Property 'languageOptions' does not exist on type 'Context<any>'.
]);

const library = {
Expand All @@ -31,7 +33,8 @@
name: externalLibraryName,
symbols
},
globals
globals,
languageOptions
};

// Clear the context, with the same chapter and externalSymbols as before.
Expand Down
28 changes: 19 additions & 9 deletions src/commons/sagas/WorkspaceSaga/helpers/evalEditor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
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 @@ -93,19 +94,28 @@ export function* evalEditorSaga(
const prependFiles = {
[prependFilePath]: prepend
};
yield call(
evalCodeSaga,
prependFiles,
prependFilePath,
elevatedContext,
execTime,
workspaceLocation,
EVAL_SILENT
);
if (context.variant !== Variant.TYPED) {
yield call(
evalCodeSaga,
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
32 changes: 29 additions & 3 deletions src/commons/utils/JsSlangHelper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
/* tslint:disable: ban-types*/
import createSlangContext, { defineBuiltin, importBuiltins } from 'js-slang/dist/createContext';
import { Chapter, Context, CustomBuiltIns, Value, Variant } from 'js-slang/dist/types';
import {
Chapter,
Context,
CustomBuiltIns,
LanguageOptions,

Check failure on line 7 in src/commons/utils/JsSlangHelper.ts

View workflow job for this annotation

GitHub Actions / lint (tsc)

Module '"js-slang/dist/types"' has no exported member 'LanguageOptions'.
Value,
Variant
} from 'js-slang/dist/types';
import { stringify } from 'js-slang/dist/utils/stringify';
import { difference, keys } from 'lodash';
import CseMachine from 'src/features/cseMachine/CseMachine';
Expand Down Expand Up @@ -148,9 +155,17 @@
chapter: Chapter,
externals: string[],
externalContext: T,
variant: Variant = Variant.DEFAULT
variant: Variant = Variant.DEFAULT,
languageOptions?: LanguageOptions
) {
return createSlangContext<T>(chapter, variant, externals, externalContext, externalBuiltIns);
return createSlangContext<T>(
chapter,
variant,
languageOptions,
externals,
externalContext,
externalBuiltIns

Check failure on line 167 in src/commons/utils/JsSlangHelper.ts

View workflow job for this annotation

GitHub Actions / lint (tsc)

Expected 0-5 arguments, but got 6.
);
}

// Assumes that the grader doesn't need additional external libraries apart from the standard
Expand All @@ -166,6 +181,7 @@
// 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 @@ -243,3 +259,13 @@
)
.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 @@ -98,7 +98,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