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

Cannot pass function as parameter to JX_CallFunction #745

Closed
helmutkian opened this issue Dec 24, 2015 · 4 comments
Closed

Cannot pass function as parameter to JX_CallFunction #745

helmutkian opened this issue Dec 24, 2015 · 4 comments

Comments

@helmutkian
Copy link

When I pass a JXValue with a type of RT_Function as a parameter to JX_CallFunction, it somehow gets transformed into a number.

// ...
JXValue callback;
JXValue fn;
JXValue result;

JX_Evaluate("(function (x) { console.log(x); })", "", &callback);
JX_Evaluate("(function (cb) { console.log(typeof cb); console.log(cb); cb('test'); })", "", &fn);
JX_CallFunction(&fn, &callback, 1, &result);
// ...

What I expect to see is

function
[object Function]
test

What I actually see is

number
102789

And the final line doesn't execute because it's attempting to call a number as if it was a function.

Any idea why this is? Am I doing something wrong?

@karaxuna
Copy link
Member

JX_CallFunction expects array as second argument. Please try this way:

JXValue params[1] = { callback };
JX_CallFunction(&fn, params, 1, &result);

@helmutkian
Copy link
Author

#include <stdbool.h>
#include "jx.h"

int main(int argc, char **argv)
{
  JXValue cb;
  JXValue fn;
  JXValue out;

  char *contents = "console.log('Engine started');";

  JX_InitializeOnce("");
  JX_InitializeNewEngine();
  JX_DefineMainFile(contents);

  JX_StartEngine();

  JX_Loop();

  JX_Evaluate("(function (x) { console.log(x); })", "", &cb);
  JX_Evaluate("(function (cb) { console.log(typeof cb); console.log(cb); cb('test'); })", "", &fn);

  JXValue params[1] = { cb };
  JX_CallFunction(&fn, params, 1, &out);

  JX_Loop();

  JX_Free(&cb);
  JX_Free(&fn);
  JX_Free(&out);

  JX_StopEngine();

  return 0;  
}

Compiled and dynamically linked against libjx.so on Linux:

gcc -std=c99 -L <path>/jxcore-0.3.1.0/out/Release/ -I <path>/jxcore-0.3.1.0/src/public/ -Wall -o test test.c -l jx

Where <path> stands fro the leading path.

Same result:

number 
0

EDIT:

Thought maybe using C was the problem. Compiled with g++

g++ -L <path>/jxcore-0.3.1.0/out/Release/ -I <path>/jxcore-0.3.1.0/src/public/ -Wall -o test test.c -l jx

Doesn't fix anything.

@karaxuna
Copy link
Member

You are passing "" as script parameter. Try:

JX_Evaluate("(function (x) { console.log(x); })", "eval", &cb);
JX_Evaluate("(function (cb) { console.log(typeof cb); console.log(cb); cb('test'); })", "eval", &fn);

@obastemur
Copy link
Member

By default JX_CallFunction wasn't considering function as a value type. This is sort of both bug and feature request.

However, @helmutkian Thanks for the details cfb3cd3 fixes the issue.

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