question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Call JavaScript function from C

See original GitHub issue

Hey,

It’s not pretty clear to me how I can call functions from JavaScript code within my C code.

For example I run this code

int main(int argc, char *argv[]) {
  duk_context *ctx = duk_create_heap_default();
  duk_eval_file(ctx, "test.js");
  duk_destroy_heap(ctx);
  return 0;
}

and inside my test.js is a function like

function GotCalledFromC()
{
   // stuff 
} 

How would it work to call this function in C code ?

Thank you 😃

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

5reactions
fatcerberuscommented, Dec 29, 2016
duk_get_global_string(ctx, "GotCalledFromC");
/* push arguments here */
duk_call(ctx, num_args_pushed);

The function’s return value will then be left on top of the value stack.

1reaction
svaaralacommented, Dec 30, 2016

@Memorix101 The getting started processLine() case should be similar, the pieces in the example are:

duk_push_global_object(ctx);
duk_get_prop_string(ctx, -1 /*index*/, "processLine");

duk_push_string(ctx, line);

if (duk_pcall(ctx, 1 /*nargs*/) != 0) {
    printf("Error: %s\n", duk_safe_to_string(ctx, -1));
} else {
    printf("%s\n", duk_safe_to_string(ctx, -1));
}
duk_pop(ctx);  /* pop result/error */

The example uses duk_pcall() to avoid the uncaught error issue mentioned by @fatcerberus above.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Calling a JavaScript function from C - Stack Overflow
To call JS from C, the general process is: Get QuickJS and esbuild; esbuild your desired library/script into an ESM format using CommonJS....
Read more >
Calling a JS Function from C++ - Ultralight
Our goal is to call the JavaScript function ShowMessage() from C++. ; The easiest way to call ShowMessage() from C++ is by evaluating...
Read more >
Calling JavaScript code from C/C++ using WebAssembly
void emscripten_run_script (const char\script*)is the most direct way of calling JavaScript from C/C++. It effectively runs the code using eval ...
Read more >
How to call a JavaScript function from C++? - Tutorialspoint
To call a JavaScript function from C++, generate a js file, which calls the function. The web page will load the JS and...
Read more >
Interacting with code — Emscripten 3.1.26-git (dev ...
Calling compiled C functions from JavaScript using ccall/cwrap¶. The easiest way to call compiled C functions from JavaScript is to use ccall() or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found