|
| 1 | +import clone from 'clone'; |
1 | 2 | import JSInterpreter from 'js-interpreter'; |
2 | 3 | import { observer as globalObserver } from '../../common/utils/observer'; |
3 | 4 | import { createScope } from './CliTools'; |
4 | 5 | import Interface from './Interface'; |
5 | 6 |
|
| 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 | + |
6 | 20 | const unrecoverableErrors = [ |
7 | 21 | 'InsufficientBalance', |
8 | 22 | 'CustomLimitsReached', |
@@ -55,9 +69,9 @@ export default class Interpreter { |
55 | 69 |
|
56 | 70 | const pseudoBotIf = interpreter.nativeToPseudo(BotIf); |
57 | 71 |
|
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 | + }); |
61 | 75 |
|
62 | 76 | interpreter.setProperty( |
63 | 77 | pseudoBotIf, |
@@ -168,16 +182,31 @@ export default class Interpreter { |
168 | 182 | } |
169 | 183 | } |
170 | 184 | createAsync(interpreter, func) { |
171 | | - return interpreter.createAsyncFunction((...args) => { |
| 185 | + const asyncFunc = (...args) => { |
172 | 186 | const callback = args.pop(); |
173 | 187 |
|
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))) |
175 | 197 | .then(rv => { |
176 | 198 | callback(interpreter.nativeToPseudo(rv)); |
177 | 199 | this.loop(); |
178 | 200 | }) |
179 | 201 | .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); |
181 | 210 | } |
182 | 211 | hasStarted() { |
183 | 212 | return !this.stopped; |
|
0 commit comments