forked from algorithm-visualizer/algorithm-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor.js
More file actions
33 lines (29 loc) · 911 Bytes
/
executor.js
File metadata and controls
33 lines (29 loc) · 911 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
'use strict';
const execute = (tracerManager, code, dataLines) => {
// all modules available to eval are obtained from window
try {
tracerManager.deallocateAll();
const lines = code.split('\n');
const newLines = [];
lines.forEach((line, i) => {
newLines.push(line.replace(/(.+\. *_wait *)(\( *\))/g, `$1(${i - dataLines})`));
});
eval(Babel.transform(newLines.join('\n'), {presets: ['es2015']}).code);
tracerManager.visualize();
} catch (err) {
return err;
} finally {
tracerManager.removeUnallocated();
}
};
const executeData = (tracerManager, algoData) => {
return execute(tracerManager, algoData);
};
const executeDataAndCode = (tracerManager, algoData, algoCode) => {
const dataLines = algoData.split('\n').length;
return execute(tracerManager, `${algoData}\n${algoCode}`, dataLines);
};
module.exports = {
executeData,
executeDataAndCode
};