Record whether a function is script or function code.

Script code returns the value of the last evaluated expression.
Function code returns the explicit return value or undefined.
This commit is contained in:
Tor Andersson
2014-01-24 01:26:34 +01:00
parent 546ee1e6c2
commit d948459f44
2 changed files with 7 additions and 5 deletions

View File

@@ -2,7 +2,7 @@
#include "jsparse.h"
#include "jscompile.h"
#define cexp js_cexp /* collision with math.h */
#define cexp jsC_cexp /* collision with math.h */
#define JF js_State *J, js_Function *F
@@ -30,7 +30,7 @@ int jsC_error(js_State *J, js_Ast *node, const char *fmt, ...)
js_throw(J);
}
static js_Function *newfun(js_State *J, js_Ast *name, js_Ast *params, js_Ast *body)
static js_Function *newfun(js_State *J, js_Ast *name, js_Ast *params, js_Ast *body, int script)
{
js_Function *F = malloc(sizeof *F);
memset(F, 0, sizeof *F);
@@ -41,6 +41,7 @@ static js_Function *newfun(js_State *J, js_Ast *name, js_Ast *params, js_Ast *bo
F->filename = js_intern(J, J->filename);
F->line = name ? name->line : params ? params->line : body ? body->line : 1;
F->script = script;
cfunbody(J, F, name, params, body);
@@ -412,7 +413,7 @@ static void cexp(JF, js_Ast *exp)
break;
case EXP_FUN:
emitfunction(J, F, newfun(J, exp->a, exp->b, exp->c));
emitfunction(J, F, newfun(J, exp->a, exp->b, exp->c, 0));
break;
case AST_IDENTIFIER:
@@ -1015,7 +1016,7 @@ static void cfundecs(JF, js_Ast *list)
while (list) {
js_Ast *stm = list->a;
if (stm->type == AST_FUNDEC) {
emitfunction(J, F, newfun(J, stm->a, stm->b, stm->c));
emitfunction(J, F, newfun(J, stm->a, stm->b, stm->c, 0));
emitstring(J, F, OP_FUNDEC, stm->a->string);
}
list = list->b;
@@ -1058,5 +1059,5 @@ static void cfunbody(JF, js_Ast *name, js_Ast *params, js_Ast *body)
js_Function *jsC_compile(js_State *J, js_Ast *prog)
{
return newfun(J, NULL, NULL, prog);
return newfun(J, NULL, NULL, prog, 1);
}

View File

@@ -106,6 +106,7 @@ enum js_OpCode
struct js_Function
{
const char *name;
int script;
int numparams;
const char **params;