mirror of
https://github.com/ccxvii/mujs.git
synced 2026-09-22 07:03:36 +08:00
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:
@@ -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",
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user