Return value of last evaluated expression in eval and script code.

Patch in a return statement at the end of such code.
This commit is contained in:
Tor Andersson
2014-01-18 17:19:31 +01:00
parent 8e20fae71a
commit 9650392e04
3 changed files with 16 additions and 4 deletions
+1 -1
View File
@@ -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",
+14 -1
View File
@@ -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;
}
+1 -2
View File
@@ -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,