ref: 9650392e04a54e2e8fdf0e41f03b3bbd668f0239
parent: 8e20fae71a16f5e9564cce2aebec0a1ad8fbf9f4
author: Tor Andersson <[email protected]>
date: Sat Jan 18 12:19:31 EST 2014
Return value of last evaluated expression in eval and script code. Patch in a return statement at the end of such code.
--- a/jsdump.c
+++ b/jsdump.c
@@ -6,7 +6,7 @@
#include <assert.h>
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",
--- a/jsparse.c
+++ b/jsparse.c
@@ -864,6 +864,8 @@
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 @@
}
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;
}
--- a/jsparse.h
+++ b/jsparse.h
@@ -4,13 +4,12 @@
enum
{
AST_LIST,
+ AST_FUNDEC,
AST_IDENTIFIER,
AST_NUMBER,
AST_STRING,
AST_REGEXP,
-
- AST_FUNDEC,
/* literals */
EXP_UNDEF, /* for array elisions */