Browse Source

add an eval example (used by emscriptenduktest)

pull/2/head
Sami Vaarala 11 years ago
parent
commit
2a4e9ab7ea
  1. 49
      examples/eval/eval.c
  2. 8
      util/make_dist.sh

49
examples/eval/eval.c

@ -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;
}

8
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 \

Loading…
Cancel
Save