shithub: libmujs

Download patch

ref: d948459f4459eacc01edba9d62f4e0108fc85306
parent: 546ee1e6c2a129369a098e13d4a841101397876d
author: Tor Andersson <tor@ccxvii.net>
date: Thu Jan 23 20:26:34 EST 2014

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.

--- a/jscompile.c
+++ b/jscompile.c
@@ -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 @@
 	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 @@
 
 	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 @@
 		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 @@
 	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 @@
 
 js_Function *jsC_compile(js_State *J, js_Ast *prog)
 {
-	return newfun(J, NULL, NULL, prog);
+	return newfun(J, NULL, NULL, prog, 1);
 }
--- a/jscompile.h
+++ b/jscompile.h
@@ -106,6 +106,7 @@
 struct js_Function
 {
 	const char *name;
+	int script;
 
 	int numparams;
 	const char **params;