mirror of https://github.com/svaarala/duktape.git
Sami Vaarala
11 years ago
2 changed files with 57 additions and 0 deletions
@ -0,0 +1,49 @@ |
|||
/*
|
|||
* 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_INVALID_INDEX); |
|||
duk_safe_call(ctx, tostring_raw, 1 /*nargs*/, 1 /*nrets*/, DUK_INVALID_INDEX); |
|||
res = duk_get_string(ctx, -1); |
|||
printf("%s\n", res ? res : "null"); |
|||
duk_pop(ctx); |
|||
} |
|||
|
|||
duk_destroy_heap(ctx); |
|||
|
|||
return 0; |
|||
} |
|||
|
Loading…
Reference in new issue