From 6071c5fde0f386d604b6f3a834c8cf8bae4dbd5f Mon Sep 17 00:00:00 2001 From: Sami Vaarala Date: Wed, 9 Apr 2014 17:41:06 +0300 Subject: [PATCH] add 'noresult' variants for eval/peval calls, which are often convenient --- src/duk_api_compile.c | 8 +++++++- src/duktape.h | 31 ++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/duk_api_compile.c b/src/duk_api_compile.c index 164ee863..4fec139b 100644 --- a/src/duk_api_compile.c +++ b/src/duk_api_compile.c @@ -21,7 +21,8 @@ int duk_eval_raw(duk_context *ctx, int flags) { /* [ ... closure/error ] */ if (rc != DUK_EXEC_SUCCESS) { - return DUK_EXEC_ERROR; + rc = DUK_EXEC_ERROR; + goto got_rc; } if (flags & DUK_COMPILE_SAFE) { @@ -33,6 +34,11 @@ int duk_eval_raw(duk_context *ctx, int flags) { /* [ ... result/error ] */ + got_rc: + if (flags & DUK_COMPILE_NORESULT) { + duk_pop(ctx); + } + return rc; } diff --git a/src/duktape.h b/src/duktape.h index 5a9c4f51..b45d283a 100644 --- a/src/duktape.h +++ b/src/duktape.h @@ -176,7 +176,8 @@ struct duk_memory_functions { #define DUK_COMPILE_EVAL (1 << 0) /* compile eval code (instead of program) */ #define DUK_COMPILE_FUNCTION (1 << 1) /* compile function code (instead of program) */ #define DUK_COMPILE_STRICT (1 << 2) /* use strict (outer) context for program, eval, or function */ -#define DUK_COMPILE_SAFE (1 << 3) /* catch compilation errors */ +#define DUK_COMPILE_SAFE (1 << 3) /* (internal) catch compilation errors */ +#define DUK_COMPILE_NORESULT (1 << 4) /* (internal) omit eval result */ /* Duktape specific error codes */ #define DUK_ERR_UNIMPLEMENTED_ERROR 50 /* UnimplementedError */ @@ -592,10 +593,18 @@ int duk_compile_raw(duk_context *ctx, int flags); ((void) duk_push_string((ctx), __FILE__), \ (void) duk_eval_raw((ctx), DUK_COMPILE_EVAL)) +#define duk_eval_noresult(ctx) \ + ((void) duk_push_string((ctx), __FILE__), \ + (void) duk_eval_raw((ctx), DUK_COMPILE_EVAL | DUK_COMPILE_NORESULT)) + #define duk_peval(ctx) \ ((void) duk_push_string((ctx), __FILE__), \ duk_eval_raw((ctx), DUK_COMPILE_EVAL | DUK_COMPILE_SAFE)) +#define duk_peval_noresult(ctx) \ + ((void) duk_push_string((ctx), __FILE__), \ + duk_eval_raw((ctx), DUK_COMPILE_EVAL | DUK_COMPILE_SAFE | DUK_COMPILE_NORESULT)) + #define duk_compile(ctx,flags) \ ((void) duk_compile_raw((ctx), (flags))) @@ -607,11 +616,21 @@ int duk_compile_raw(duk_context *ctx, int flags); (void) duk_push_string((ctx), __FILE__), \ (void) duk_eval_raw((ctx), DUK_COMPILE_EVAL)) +#define duk_eval_string_noresult(ctx,src) \ + ((void) duk_push_string((ctx), (src)), \ + (void) duk_push_string((ctx), __FILE__), \ + (void) duk_eval_raw((ctx), DUK_COMPILE_EVAL | DUK_COMPILE_NORESULT)) + #define duk_peval_string(ctx,src) \ ((void) duk_push_string((ctx), (src)), \ (void) duk_push_string((ctx), __FILE__), \ duk_eval_raw((ctx), DUK_COMPILE_EVAL | DUK_COMPILE_SAFE)) +#define duk_peval_string_noresult(ctx,src) \ + ((void) duk_push_string((ctx), (src)), \ + (void) duk_push_string((ctx), __FILE__), \ + duk_eval_raw((ctx), DUK_COMPILE_EVAL | DUK_COMPILE_SAFE | DUK_COMPILE_NORESULT)) + #define duk_compile_string(ctx,flags,src) \ ((void) duk_push_string((ctx), (src)), \ (void) duk_push_string((ctx), __FILE__), \ @@ -627,11 +646,21 @@ int duk_compile_raw(duk_context *ctx, int flags); (void) duk_push_string((ctx), (path)), \ (void) duk_eval_raw((ctx), DUK_COMPILE_EVAL)) +#define duk_eval_file_noresult(ctx,path) \ + ((void) duk_push_string_file((ctx), (path)), \ + (void) duk_push_string((ctx), (path)), \ + (void) duk_eval_raw((ctx), DUK_COMPILE_EVAL | DUK_COMPILE_NORESULT)) + #define duk_peval_file(ctx,path) \ ((void) duk_push_string_file((ctx), (path)), \ (void) duk_push_string((ctx), (path)), \ duk_eval_raw((ctx), DUK_COMPILE_EVAL | DUK_COMPILE_SAFE)) +#define duk_peval_file_noresult(ctx,path) \ + ((void) duk_push_string_file((ctx), (path)), \ + (void) duk_push_string((ctx), (path)), \ + duk_eval_raw((ctx), DUK_COMPILE_EVAL | DUK_COMPILE_SAFE | DUK_COMPILE_NORESULT)) + #define duk_compile_file(ctx,flags,path) \ ((void) duk_push_string_file((ctx), (path)), \ (void) duk_push_string((ctx), (path)), \