2

The task is to call a javascript function as a callback in order to show a progress of while-loop operation. E.g. JS:

var my_js_fn = function(curstate, maxstate){//int variables
console.log(curstate.toString() + " of " + maxstate.toString());
}

C pseudocode:

int smth_that_calls_my_fn(int i, int max) {
/*
the_magic to call my_js_fn()
*/
}
    int main(){
    //....
        while (i < max){
        smth_that_calls_my_fn(i,max);
        }
    //....
    return 0;
    }

How can I link smth_that_calls_my_fn and my_js_fn ?

1
  • You can run inline javascript (doc). Or you can implement C API in JS (doc). Commented Aug 5, 2015 at 10:47

1 Answer 1

5

The magic you're looking for is pretty simple -- you need to use the EM_ASM_ARGS macro.

Specifically, it can look like

int smth_that_calls_my_fn(int i, int max) {
  EM_ASM_ARGS({ my_js_fn($0, $1); }, i, max);
}

Make sure that you #include <emscripten.h> in your C file so that this macro exists.

The EM_ASM_ARGS macro takes JavaScript code (in braces) as the first argument, and then any other parameters you want to pass in. In the JS code, $0 is the first argument to follow, $1 the next and so on.

I just wrote a blog entry going into detail on this topic if you want more information: http://devosoft.org/an-introduction-to-web-development-with-emscripten/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.