Skip to content
This repository was archived by the owner on Feb 22, 2024. It is now read-only.

Commit c80ef61

Browse files
author
Aaron
authored
Merge pull request #2439 from aaron-binary/update-js-interpreter-2.1.0
aaron/JS-Interpreter@^2.1.0
2 parents a08887f + a85bcc4 commit c80ef61

File tree

3 files changed

+47
-25
lines changed

3 files changed

+47
-25
lines changed

package-lock.json

Lines changed: 10 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
"jquery": "^3.4.1",
8383
"jquery-ui": "1.12.1",
8484
"jquery-ui-css": "1.11.4",
85-
"js-interpreter": "^1.4.6",
85+
"js-interpreter": "^2.1.0",
8686
"json2csv": "^3.11.5",
8787
"lint-staged": "^8.1.7",
8888
"loader-utils": "^1.1.0",
@@ -105,6 +105,7 @@
105105
"@binary-com/smartcharts": "^0.6.1",
106106
"binary-style": "^0.2.4",
107107
"blockly": "github:google/blockly#59e5ac6",
108+
"clone": "aminmarashi/clone#d97b4f",
108109
"commander": "^2.20.0",
109110
"concat-stream": "^2.0.0",
110111
"core-js": "^2.6.5",

src/botPage/bot/Interpreter.js

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
1+
import clone from 'clone';
12
import JSInterpreter from 'js-interpreter';
23
import { observer as globalObserver } from '../../common/utils/observer';
34
import { createScope } from './CliTools';
45
import Interface from './Interface';
56

7+
/* eslint-disable func-names, no-underscore-dangle */
8+
JSInterpreter.prototype.takeStateSnapshot = function() {
9+
const newStateStack = clone(this.stateStack, undefined, undefined, undefined, true);
10+
return newStateStack;
11+
};
12+
13+
JSInterpreter.prototype.restoreStateSnapshot = function(snapshot) {
14+
this.stateStack = clone(snapshot, undefined, undefined, undefined, true);
15+
this.global = this.stateStack[0].scope;
16+
this.initFunc_(this, this.global);
17+
};
18+
/* eslint-enable */
19+
620
const unrecoverableErrors = [
721
'InsufficientBalance',
822
'CustomLimitsReached',
@@ -55,9 +69,9 @@ export default class Interpreter {
5569

5670
const pseudoBotIf = interpreter.nativeToPseudo(BotIf);
5771

58-
Object.entries(ticksIf).forEach(([name, f]) =>
59-
interpreter.setProperty(pseudoBotIf, name, this.createAsync(interpreter, f))
60-
);
72+
Object.entries(ticksIf).forEach(([name, f]) => {
73+
interpreter.setProperty(pseudoBotIf, name, this.createAsync(interpreter, f));
74+
});
6175

6276
interpreter.setProperty(
6377
pseudoBotIf,
@@ -168,16 +182,31 @@ export default class Interpreter {
168182
}
169183
}
170184
createAsync(interpreter, func) {
171-
return interpreter.createAsyncFunction((...args) => {
185+
const asyncFunc = (...args) => {
172186
const callback = args.pop();
173187

174-
func(...args.map(arg => interpreter.pseudoToNative(arg)))
188+
// Workaround for unknown number of args
189+
const reversedArgs = args.slice().reverse();
190+
const firsDefinedArgIdx = reversedArgs.findIndex(arg => arg !== undefined);
191+
192+
// Remove extra undefined args from end of the args
193+
const functionArgs = firsDefinedArgIdx < 0 ? [] : reversedArgs.slice(firsDefinedArgIdx).reverse();
194+
// End of workaround
195+
196+
func(...functionArgs.map(arg => interpreter.pseudoToNative(arg)))
175197
.then(rv => {
176198
callback(interpreter.nativeToPseudo(rv));
177199
this.loop();
178200
})
179201
.catch(e => this.$scope.observer.emit('Error', e));
180-
});
202+
};
203+
204+
// TODO: This is a workaround, create issue on original repo, once fixed
205+
// remove this. We don't know how many args are going to be passed, so we
206+
// assume a max of 100.
207+
const MAX_ACCEPTABLE_FUNC_ARGS = 100;
208+
Object.defineProperty(asyncFunc, 'length', { value: MAX_ACCEPTABLE_FUNC_ARGS + 1 });
209+
return interpreter.createAsyncFunction(asyncFunc);
181210
}
182211
hasStarted() {
183212
return !this.stopped;

0 commit comments

Comments
 (0)