diff --git a/jsbuiltin.c b/jsbuiltin.c index 6b57af6..84e61e3 100644 --- a/jsbuiltin.c +++ b/jsbuiltin.c @@ -34,7 +34,7 @@ static void jsB_eval(js_State *J) js_copy(J, 1); return; } - js_loadstring(J, "(eval)", js_tostring(J, -1)); + js_loadeval(J, "(eval)", js_tostring(J, -1)); js_pushglobal(J); js_call(J, 0); } diff --git a/jsi.h b/jsi.h index 225b1cd..39df4d2 100644 --- a/jsi.h +++ b/jsi.h @@ -71,7 +71,8 @@ double js_strtod(const char *as, char **aas); /* Private stack functions */ void js_newfunction(js_State *J, js_Function *function, js_Environment *scope); -void js_newscript(js_State *J, js_Function *function); +void js_newscript(js_State *J, js_Function *function, js_Environment *scope); +void js_loadeval(js_State *J, const char *filename, const char *source); js_Regexp *js_toregexp(js_State *J, int idx); int js_isarrayindex(js_State *J, const char *str, unsigned int *idx); diff --git a/jsrun.c b/jsrun.c index bbf0387..31b704e 100644 --- a/jsrun.c +++ b/jsrun.c @@ -890,14 +890,21 @@ static void jsR_callfunction(js_State *J, unsigned int n, js_Function *F, js_Env jsR_restorescope(J); } -static void jsR_callscript(js_State *J, unsigned int n, js_Function *F) +static void jsR_callscript(js_State *J, unsigned int n, js_Function *F, js_Environment *scope) { js_Value v; + + if (scope) + jsR_savescope(J, scope); + js_pop(J, n); jsR_run(J, F); v = js_tovalue(J, -1); TOP = --BOT; /* clear stack */ js_pushvalue(J, v); + + if (scope) + jsR_restorescope(J); } static void jsR_callcfunction(js_State *J, unsigned int n, unsigned int min, js_CFunction F) @@ -933,7 +940,7 @@ void js_call(js_State *J, int n) else jsR_callfunction(J, n, obj->u.f.function, obj->u.f.scope); } else if (obj->type == JS_CSCRIPT) - jsR_callscript(J, n, obj->u.f.function); + jsR_callscript(J, n, obj->u.f.function, obj->u.f.scope); else if (obj->type == JS_CCFUNCTION) jsR_callcfunction(J, n, obj->u.c.length, obj->u.c.function); diff --git a/jsstate.c b/jsstate.c index 1197ee3..d42d10b 100644 --- a/jsstate.c +++ b/jsstate.c @@ -40,7 +40,7 @@ int js_ploadfile(js_State *J, const char *filename) return 0; } -void js_loadstring(js_State *J, const char *filename, const char *source) +static void js_loadstringx(js_State *J, const char *filename, const char *source, int iseval) { js_Ast *P; js_Function *F; @@ -53,11 +53,21 @@ void js_loadstring(js_State *J, const char *filename, const char *source) P = jsP_parse(J, filename, source); F = jsC_compile(J, P); jsP_freeparse(J); - js_newscript(J, F); + js_newscript(J, F, iseval ? NULL : J->GE); js_endtry(J); } +void js_loadeval(js_State *J, const char *filename, const char *source) +{ + js_loadstringx(J, filename, source, 1); +} + +void js_loadstring(js_State *J, const char *filename, const char *source) +{ + js_loadstringx(J, filename, source, 0); +} + void js_loadfile(js_State *J, const char *filename) { FILE *f; diff --git a/jsvalue.c b/jsvalue.c index 867e118..79af7dd 100644 --- a/jsvalue.c +++ b/jsvalue.c @@ -339,11 +339,11 @@ void js_newfunction(js_State *J, js_Function *fun, js_Environment *scope) } } -void js_newscript(js_State *J, js_Function *fun) +void js_newscript(js_State *J, js_Function *fun, js_Environment *scope) { js_Object *obj = jsV_newobject(J, JS_CSCRIPT, NULL); obj->u.f.function = fun; - obj->u.f.scope = NULL; + obj->u.f.scope = scope; js_pushobject(J, obj); }