From 47fb8ab2dd34e0adc60f415c2ebe58b18f1a5e8b Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Mon, 25 Apr 2016 15:50:44 +0200 Subject: murun: Print result of evaluating a line in interactive mode. --- source/tools/murun.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'source/tools') diff --git a/source/tools/murun.c b/source/tools/murun.c index 03e092f7..eba987bb 100644 --- a/source/tools/murun.c +++ b/source/tools/murun.c @@ -23,6 +23,25 @@ static void *alloc(void *actx, void *ptr, unsigned int n) return fz_malloc_array(ctx, n, 1); } +static int eval_print(js_State *J, const char *source) +{ + if (js_ploadstring(J, "[string]", source)) { + fprintf(stderr, "%s\n", js_tostring(J, -1)); + js_pop(J, 1); + return 1; + } + js_pushglobal(J); + if (js_pcall(J, 0)) { + fprintf(stderr, "%s\n", js_tostring(J, -1)); + js_pop(J, 1); + return 1; + } + if (js_isdefined(J, -1)) + printf("%s\n", js_tostring(J, -1)); + js_pop(J, 1); + return 0; +} + static void jsB_propfun(js_State *J, const char *name, js_CFunction cfun, int n) { const char *realname = strchr(name, '.'); @@ -3174,7 +3193,7 @@ int murun_main(int argc, char **argv) char line[256]; fputs(PS1, stdout); while (fgets(line, sizeof line, stdin)) { - js_dostring(J, line); + eval_print(J, line); fputs(PS1, stdout); } putchar('\n'); -- cgit v1.2.3