Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call an interpreted function #153

Closed
fpelliccioni opened this issue Feb 1, 2019 · 3 comments
Closed

Call an interpreted function #153

fpelliccioni opened this issue Feb 1, 2019 · 3 comments

Comments

@fpelliccioni
Copy link

Hello,

I have the following Javascript code that I am interpreting using your library:

callback(function(x, y) { return x < y; });

I had setup a wrapper function this way:

var callback_wrapper = function(cb) {
    // **1
};

interpreter.setProperty(scope, 'callback',    interpreter.createNativeFunction(callback_wrapper));

I want to call the function passed to callback() inside the callback_wrapper() (**1).
Is it possible?
How can I do it?

Thanks in advance,

@cpcallen
Copy link
Collaborator

cpcallen commented Feb 1, 2019

This is a duplicate of #130. Unfortunately what you want to do is not at all straight forward.(I recall having an even more extensive discussion of the issues involved in another bug, but I can't find it at the moment.)

It is possible, but it entails writing the native function as a state machine which will be invoked multiple times:

  1. The interpreter invokes the native function for the first time. It does whatever needs to be done before calling the callback, then creates a new state for the call to the callback and places it on the top of the state stack.
  2. Native function returns (without removing itself from the state stack), allowing interpreter to continue with execution of callback.
  3. When execution of callback is complete, interpreter will re-invoke native function. It must remember that it's already done step 1. and not do it again, but instead continue with whatever it should do next.
  4. Eventually the native function returns in a way that pops itself off the state stack.

The interpreter in Code City, which was originally forked from this one, has been modified with various mechanisms to facilitate this; an example of their usage can be seen in the definition of String(), which calls user-supplied .toString and .valueOf methods, but it would be a large project to back-port these changes and the Code City interpreter is not a good general-purpose replacement for JS Interpreter.

See also #147, which is related but would not by itself solve your particular issue.

@cpcallen cpcallen closed this as completed Feb 1, 2019
@fpelliccioni
Copy link
Author

Thank you very much for all the information.

@semiaddict
Copy link

semiaddict commented Aug 11, 2022

[moved my comment to 147#1211911798]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants