JS-Interpreter
JS-Interpreter copied to clipboard
Add native callbacks.
I implemented a utility function (queueCall) to call back a pseudo function from the native side. This allows to create "true asynchronous" calls, from the interpreter's point of view. I also wrote a demo ("Callback Demo"), and added some documentation.
Basic usage :
interpreter.queueCall(pseudoFunction, [pseudoArguments]);
interpreter.run();
This works by shifting the given function at the bottom of the stack (just on top of the Program node), so the function is called just before the program ends (that's why I say it's "queued"). This makes sure that there is no thread-related unsolvable bugs...
I would have liked to re-generate the compressed bundle myself, but I do not know how to (maybe it could be interesting to put a notice in README.md explaining how to do it).
(I apologize if my English is not good, I'm not a native speaker... ¯_(ツ)_/¯ )
This is great, just what I was looking for!
What does the following do?
arguments: [this.createPrimitive('Hello !')],
Well, this does nothing, technically... it does not change anything (actually there is nothing I saw that was caused by this) This is a line of code I wrote when testing, and that I forgot to remove because it was not affecting the behavior of the interpreter (I'll go deeper in the explanation as soon as I'll understand everything correctly). Thank you for pointing it out !
@thejohncrafter altough this seems to run fine, I sometimes get into a situation where the callbacks are not executed anymore and the stack just keeps getting bigger and bigger...
I need to push many callbacks quickly onto the stack to cause such behaviour. Any ideias on what could cause this?
@ferrao sorry, I did not find a way to reproduce this bug yet. Can you provide me the code causing it, please ?
@thejohncrafter after some banging against the wall I managed to find my problem and it has nothing to do with your code, it's actually already reported here #84 , once i fixed that everything worked as expected!
@NeilFraser If this isn't a feature that you're interested in merging into JS-Interpreter, do you think this is something that users of JS-Interpreter could implement on their end?
It feels like the solution to this on the user side would be very similar to the way a user can implement multi threaded js? Just curious if you had any thoughts on the best way for a user to implement this using JS-Interpreter in its current state.
Unfortunately, this doesn't work with the latest interpreter.js, but it would really be nice to have a way of calling anonymous interpreted functions passed native functions more directly than is currently possible.
I've added PR #201 to do something similar to this, but for newer versions of the interpreter.