diff --git a/examples/eval/eval.c b/examples/eval/eval.c new file mode 100644 index 00000000..b47954a1 --- /dev/null +++ b/examples/eval/eval.c @@ -0,0 +1,49 @@ +/* + * Very simple example program for evaluating expressions from + * command line + */ + +#include "duktape.h" +#include + +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 [] ...\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; +} + diff --git a/util/make_dist.sh b/util/make_dist.sh index 5b786732..3fbb9b85 100644 --- a/util/make_dist.sh +++ b/util/make_dist.sh @@ -48,6 +48,7 @@ mkdir $DIST/src mkdir $DIST/licenses mkdir $DIST/examples mkdir $DIST/examples/hello +mkdir $DIST/examples/eval mkdir $DIST/examples/cmdline mkdir $DIST/examples/guide mkdir $DIST/examples/coffee @@ -186,6 +187,13 @@ for i in \ cp examples/hello/$i $DIST/examples/hello/ done +for i in \ + eval.c \ + ; do + cp examples/eval/$i $DIST/examples/eval/ +done + + for i in \ fib.js \ process.js \