From 9650392e04a54e2e8fdf0e41f03b3bbd668f0239 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Sat, 18 Jan 2014 17:19:31 +0100 Subject: [PATCH] Return value of last evaluated expression in eval and script code. Patch in a return statement at the end of such code. --- jsdump.c | 2 +- jsparse.c | 15 ++++++++++++++- jsparse.h | 3 +-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/jsdump.c b/jsdump.c index 7d2e2d0..cfe00c1 100644 --- a/jsdump.c +++ b/jsdump.c @@ -6,7 +6,7 @@ #include static const char *astname[] = { - "list", "ident", "number", "string", "regexp", "fundec", "undef", + "list", "fundec", "ident", "number", "string", "regexp", "undef", "null", "true", "false", "this", "fun", "array", "object", "prop_val", "prop_get", "prop_set", "index", "member", "call", "new", "delete", "void", "typeof", "preinc", "predec", "postinc", "postdec", "pos", diff --git a/jsparse.c b/jsparse.c index a372ecc..75dc021 100644 --- a/jsparse.c +++ b/jsparse.c @@ -864,6 +864,8 @@ int jsP_error(js_State *J, const char *fmt, ...) js_Ast *jsP_parse(js_State *J, const char *filename, const char *source) { + js_Ast *p, *last; + jsP_initlex(J, filename, source); if (setjmp(J->jb)) { @@ -872,5 +874,16 @@ js_Ast *jsP_parse(js_State *J, const char *filename, const char *source) } next(J); - return script(J); + p = script(J); + + /* patch up global and eval code to return value of last expression */ + last = p; + if (last) { + while (last->b) + last = last->b; + if (last->a->type >= AST_IDENTIFIER && last->a->type < STM_BLOCK) + last->a = STM1(RETURN, last->a); + } + + return p; } diff --git a/jsparse.h b/jsparse.h index d715382..7667b22 100644 --- a/jsparse.h +++ b/jsparse.h @@ -4,14 +4,13 @@ enum { AST_LIST, + AST_FUNDEC, AST_IDENTIFIER, AST_NUMBER, AST_STRING, AST_REGEXP, - AST_FUNDEC, - /* literals */ EXP_UNDEF, /* for array elisions */ EXP_NULL,