Implement Function constructor.

This commit is contained in:
Tor Andersson
2014-02-07 11:44:39 +01:00
parent 979c7bc1bd
commit d5b83c3bf2
7 changed files with 56 additions and 1 deletions
+5
View File
@@ -1109,6 +1109,11 @@ static void cfunbody(JF, js_Ast *name, js_Ast *params, js_Ast *body)
}
}
js_Function *jsC_compilefunction(js_State *J, js_Ast *prog)
{
return newfun(J, prog->a, prog->b, prog->c, 0);
}
js_Function *jsC_compile(js_State *J, js_Ast *prog)
{
return newfun(J, NULL, NULL, prog, 1);
+1
View File
@@ -138,6 +138,7 @@ struct js_Function
int gcmark;
};
js_Function *jsC_compilefunction(js_State *J, js_Ast *prog);
js_Function *jsC_compile(js_State *J, js_Ast *prog);
const char *jsC_opcodestring(int opcode);
void jsC_dumpfunction(js_State *J, js_Function *fun);
+36 -1
View File
@@ -1,11 +1,46 @@
#include "jsi.h"
#include "jsparse.h"
#include "jscompile.h"
#include "jsvalue.h"
#include "jsbuiltin.h"
static int jsB_Function(js_State *J, int argc)
{
return 0;
const char *source;
struct sbuffer *sb;
js_Ast *parse;
js_Function *fun;
int i;
if (argc == 0)
source = "";
else {
source = js_tostring(J, argc);
sb = NULL;
if (argc > 1) {
for (i = 1; i < argc; ++i) {
if (i > 1) sb = sb_putc(sb, ',');
sb = sb_puts(sb, js_tostring(J, i));
}
sb = sb_putc(sb, ')');
}
}
if (js_try(J)) {
free(sb);
jsP_freeparse(J);
js_throw(J);
}
parse = jsP_parsefunction(J, "Function", sb->s, source);
fun = jsC_compilefunction(J, parse);
js_endtry(J);
free(sb);
jsP_freeparse(J);
js_newfunction(J, fun, J->GE);
return 1;
}
static int jsB_Function_prototype(js_State *J, int argc)
+1
View File
@@ -116,6 +116,7 @@ struct js_State
js_Object *R; /* registry of hidden values */
js_Object *G; /* the global object */
js_Environment *E; /* current environment scope */
js_Environment *GE; /* global environment scope (at the root) */
/* execution stack */
int top, bot;
+11
View File
@@ -967,3 +967,14 @@ js_Ast *jsP_parse(js_State *J, const char *filename, const char *source)
return p;
}
js_Ast *jsP_parsefunction(js_State *J, const char *filename, const char *params, const char *body)
{
js_Ast *p = NULL;
if (params) {
jsY_initlex(J, filename, params);
next(J);
p = parameters(J);
}
return EXP3(FUN, NULL, p, jsP_parse(J, filename, body));
}
+1
View File
@@ -136,6 +136,7 @@ struct js_Ast
js_Ast *gcnext; /* next in alloc list */
};
js_Ast *jsP_parsefunction(js_State *J, const char *filename, const char *params, const char *body);
js_Ast *jsP_parse(js_State *J, const char *filename, const char *source);
void jsP_freeparse(js_State *J);
+1
View File
@@ -114,6 +114,7 @@ js_State *js_newstate(void)
J->R = jsV_newobject(J, JS_COBJECT, NULL);
J->G = jsV_newobject(J, JS_COBJECT, NULL);
J->E = jsR_newenvironment(J, J->G, NULL);
J->GE = J->E;
jsB_init(J);