jsexecute: Use new Function instead of eval

This shows significant performance improvement in some projects
This commit is contained in:
Thomas Weber committed 2021-07-11 19:12:58 -05:00
1 parent 705b3da07d
commit e156ed7ccb
1 file changed
+3 -6
+3 -6
View File
@@ -30,11 +30,7 @@ const globalState = {
thread: null
};
// All the functions defined here will be available to compiled scripts.
// The JSDoc annotations define the function's contract.
// Most of these functions are only used at runtime by generated scripts.
// Despite what your editor may say, they are not unused.
const RUNTIME = `
let stuckCounter = 0;
/**
* Determine whether the current tick is likely stuck.
@@ -550,6 +546,7 @@ const mod = (n, modulus) => {
if (result / modulus < 0) result += modulus;
return result;
};
`;
/**
* Step a compiled thread.
@@ -568,7 +565,7 @@ const execute = thread => {
const scopedEval = source => {
try {
// eslint-disable-next-line no-eval
return eval(source);
return new Function('globalState', `${RUNTIME}return ${source}`)(globalState);
} catch (e) {
globalState.log.error('was unable to compile script', source);
throw e;