Skip to content

Commit 05bb216

Browse files
Move workspace saga helper functions to its own files (#2870)
* Remove global variable from workspace saga * Update tests * Move workspace helper functions to it own files * Undo changes done to the tests * Fix linting errors * Fix indent --------- Co-authored-by: Richard Dominick <34370238+RichDom2185@users.noreply.github.com>
1 parent 5f29220 commit 05bb216

13 files changed

+1468
-1372
lines changed

src/commons/sagas/WorkspaceSaga.ts

Lines changed: 0 additions & 1371 deletions
This file was deleted.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { Context } from 'js-slang';
2+
import { call } from 'redux-saga/effects';
3+
4+
import {
5+
getBlockExtraMethodsString,
6+
getDifferenceInMethods,
7+
getStoreExtraMethodsString
8+
} from '../../../utils/JsSlangHelper';
9+
import { EVAL_SILENT, WorkspaceLocation } from '../../../workspace/WorkspaceTypes';
10+
import { evalCode } from './evalCode';
11+
12+
export function* blockExtraMethods(
13+
elevatedContext: Context,
14+
context: Context,
15+
execTime: number,
16+
workspaceLocation: WorkspaceLocation,
17+
unblockKey?: string
18+
) {
19+
// Extract additional methods available in the elevated context relative to the context
20+
const toBeBlocked = getDifferenceInMethods(elevatedContext, context);
21+
if (unblockKey) {
22+
const storeValues = getStoreExtraMethodsString(toBeBlocked, unblockKey);
23+
const storeValuesFilePath = '/storeValues.js';
24+
const storeValuesFiles = {
25+
[storeValuesFilePath]: storeValues
26+
};
27+
yield call(
28+
evalCode,
29+
storeValuesFiles,
30+
storeValuesFilePath,
31+
elevatedContext,
32+
execTime,
33+
workspaceLocation,
34+
EVAL_SILENT
35+
);
36+
}
37+
38+
const nullifier = getBlockExtraMethodsString(toBeBlocked);
39+
const nullifierFilePath = '/nullifier.js';
40+
const nullifierFiles = {
41+
[nullifierFilePath]: nullifier
42+
};
43+
yield call(
44+
evalCode,
45+
nullifierFiles,
46+
nullifierFilePath,
47+
elevatedContext,
48+
execTime,
49+
workspaceLocation,
50+
EVAL_SILENT
51+
);
52+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { Context } from 'js-slang';
2+
import { defineSymbol } from 'js-slang/dist/createContext';
3+
import { Variant } from 'js-slang/dist/types';
4+
import { put, select, take } from 'redux-saga/effects';
5+
6+
import { OverallState } from '../../../application/ApplicationTypes';
7+
import { ExternalLibraryName } from '../../../application/types/ExternalTypes';
8+
import { actions } from '../../../utils/ActionsHelper';
9+
import { END_CLEAR_CONTEXT, WorkspaceLocation } from '../../../workspace/WorkspaceTypes';
10+
11+
export function* clearContext(workspaceLocation: WorkspaceLocation, entrypointCode: string) {
12+
const [chapter, symbols, externalLibraryName, globals, variant]: [
13+
number,
14+
string[],
15+
ExternalLibraryName,
16+
Array<[string, any]>,
17+
Variant
18+
] = yield select((state: OverallState) => [
19+
state.workspaces[workspaceLocation].context.chapter,
20+
state.workspaces[workspaceLocation].context.externalSymbols,
21+
state.workspaces[workspaceLocation].externalLibrary,
22+
state.workspaces[workspaceLocation].globals,
23+
state.workspaces[workspaceLocation].context.variant
24+
]);
25+
26+
const library = {
27+
chapter,
28+
variant,
29+
external: {
30+
name: externalLibraryName,
31+
symbols
32+
},
33+
globals
34+
};
35+
36+
// Clear the context, with the same chapter and externalSymbols as before.
37+
yield put(actions.beginClearContext(workspaceLocation, library, false));
38+
// Wait for the clearing to be done.
39+
yield take(END_CLEAR_CONTEXT);
40+
41+
const context: Context = yield select(
42+
(state: OverallState) => state.workspaces[workspaceLocation].context
43+
);
44+
defineSymbol(context, '__PROGRAM__', entrypointCode);
45+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { put, StrictEffect } from 'redux-saga/effects';
2+
3+
import { actions } from '../../../utils/ActionsHelper';
4+
import DisplayBufferService from '../../../utils/DisplayBufferService';
5+
import { WorkspaceLocation } from '../../../workspace/WorkspaceTypes';
6+
7+
export function* dumpDisplayBuffer(
8+
workspaceLocation: WorkspaceLocation,
9+
isStoriesBlock: boolean = false,
10+
storyEnv?: string
11+
): Generator<StrictEffect, void, any> {
12+
if (!isStoriesBlock) {
13+
yield put(actions.handleConsoleLog(workspaceLocation, ...DisplayBufferService.dump()));
14+
} else {
15+
yield put(actions.handleStoriesConsoleLog(storyEnv!, ...DisplayBufferService.dump()));
16+
}
17+
}

0 commit comments

Comments
 (0)