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

how export function using c code #6

Closed
fkeujjpdc opened this issue Mar 2, 2017 · 3 comments
Closed

how export function using c code #6

fkeujjpdc opened this issue Mar 2, 2017 · 3 comments

Comments

@fkeujjpdc
Copy link

i want export function using c code ,because ffi using much rom ,but i'm not find method,like this https://github.com/cesanta/v7/blob/master/examples/call_c_from_js/call_c_from_js.c

@cpq
Copy link
Member

cpq commented Mar 2, 2017

 mjs_set(mjs, g, "myfunc", ~0, mjs_mk_foreign(mjs,my_func));

See example in https://github.com/cesanta/mjs/blob/master/mjs.c, search for mjs_init_globals

@cpq cpq closed this as completed Mar 2, 2017
@fkeujjpdc
Copy link
Author

fkeujjpdc commented Mar 3, 2017

thanks for your reply,this is my code ,but result is error:
`#include "mjs.h"

void foo(int x) {
printf("Hello %d!\n", x);
}

void *my_dlsym(void *handle, const char *name) {
//if (strcmp(name, "foo") == 0) return foo;
return NULL;
}

int main(void) {
struct mjs *mjs = mjs_create();
mjs_set_ffi_resolver(mjs, my_dlsym);
mjs_set(mjs,mjs_get_global(mjs), "foo", ~0, mjs_mk_foreign(mjs, foo));
//mjs_set(mjs, g, "myfunc", ~0, mjs_mk_foreign(mjs, my_func));
mjs_exec(mjs, " for(let i=0;i<10;++i) foo(1234);", NULL);
return 0;
}`
this is result:
Hello 2922240!
Hello 2922240!
Hello 2922240!
Hello 2922240!
Hello 2922240!
Hello 2922240!
Hello 2922240!
Hello 2922240!
Hello 2922240!
Hello 2922240!

the function param seems error

@cpq
Copy link
Member

cpq commented Mar 3, 2017

your function needs to take the parameter from the JS stack.
See the example I gave you, how those functions take parameters.

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