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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

External Higher Order Functions #130

Closed
FynnMazurkiewicz opened this issue Jan 18, 2018 · 1 comment
Closed

External Higher Order Functions #130

FynnMazurkiewicz opened this issue Jan 18, 2018 · 1 comment

Comments

@FynnMazurkiewicz
Copy link

FynnMazurkiewicz commented Jan 18, 2018

Hey everyone! 馃槉

I'm trying to use higher order functions.
I created a higher-order function outside of the interpreter and exposed it to the sandbox.
However, this fails because the higher order function receives the psuedoFunction and therefore can't call it (TypeError: fn is not a function).

A minimal test case:

var myCode = 'wrapper(function(){});';
var initFunc = function(interpreter, scope) {
    var wrapper = function(fn) {
        fn();
    };
    interpreter.setProperty(scope, 'wrapper',
        interpreter.createNativeFunction(wrapper));
};
var myInterpreter = new Interpreter(myCode, initFunc);
myInterpreter.run();

I couldn't find any examples or documentation on how to handle this.
Sandboxed higher order functions work fine, of course.

Not sure if this is intended behaviour, but I would be more than thankful for any leads.

@FynnMazurkiewicz FynnMazurkiewicz changed the title Implementation of Higher Order Functions External Higher Order Functions Jan 18, 2018
@NeilFraser
Copy link
Owner

Yes, this is a limitation of the interpreter, it is really hard for external JavaScript to initiate a callback into the interpreter. The solution is to move the higher order function into the interpreted code.

Check out Array.prototype.map. This higher order function takes a function as an argument. To achieve this the map function is written as a polyfill. See the examples from line 869 to line 1046. There are a few more examples if you search for "this.polyfills_.push".

Feel free to reopen if you need more help.

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

2 participants