Skip to content

Instantly share code, notes, and snippets.

@sumantro93
Created August 21, 2023 09:40
Show Gist options
  • Save sumantro93/039ad8ad9a9aea7c3dc960196506e05e to your computer and use it in GitHub Desktop.
Save sumantro93/039ad8ad9a9aea7c3dc960196506e05e to your computer and use it in GitHub Desktop.
duktape_basic.c
#include <stdio.h>
#include <duktape.h>
static duk_ret_t native_print(duk_context *ctx) {
printf("%s\n", duk_to_string(ctx, 0));
return 0; // No return value (= undefined)
}
int main(int argc, char *argv[]) {
duk_context *ctx = duk_create_heap_default();
if (!ctx) {
printf("Failed to create a Duktape heap.\n");
return 1;
}
// Register a native function
duk_push_c_function(ctx, native_print, 1);
duk_put_global_string(ctx, "print");
// Evaluate a string containing JavaScript
duk_eval_string(ctx, "print('Hello from Duktape embedded in C!');");
// Clean up and exit
duk_destroy_heap(ctx);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment