-
-
Save sumantro93/039ad8ad9a9aea7c3dc960196506e05e to your computer and use it in GitHub Desktop.
duktape_basic.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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