diff options
Diffstat (limited to 'src/civetweb/src/third_party/duktape-1.8.0/examples/eval/eval.c')
-rw-r--r-- | src/civetweb/src/third_party/duktape-1.8.0/examples/eval/eval.c | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/civetweb/src/third_party/duktape-1.8.0/examples/eval/eval.c b/src/civetweb/src/third_party/duktape-1.8.0/examples/eval/eval.c new file mode 100644 index 000000000..44099260d --- /dev/null +++ b/src/civetweb/src/third_party/duktape-1.8.0/examples/eval/eval.c @@ -0,0 +1,48 @@ +/* + * Very simple example program for evaluating expressions from + * command line + */ + +#include "duktape.h" +#include <stdio.h> + +static int eval_raw(duk_context *ctx) { + duk_eval(ctx); + return 1; +} + +static int tostring_raw(duk_context *ctx) { + duk_to_string(ctx, -1); + return 1; +} + +static void usage_exit(void) { + fprintf(stderr, "Usage: eval <expression> [<expression>] ...\n"); + fflush(stderr); + exit(1); +} + +int main(int argc, char *argv[]) { + duk_context *ctx; + int i; + const char *res; + + if (argc < 2) { + usage_exit(); + } + + ctx = duk_create_heap_default(); + for (i = 1; i < argc; i++) { + printf("=== eval: '%s' ===\n", argv[i]); + duk_push_string(ctx, argv[i]); + duk_safe_call(ctx, eval_raw, 1 /*nargs*/, 1 /*nrets*/); + duk_safe_call(ctx, tostring_raw, 1 /*nargs*/, 1 /*nrets*/); + res = duk_get_string(ctx, -1); + printf("%s\n", res ? res : "null"); + duk_pop(ctx); + } + + duk_destroy_heap(ctx); + + return 0; +} |