Commit f3256d81 authored by deuce's avatar deuce
Browse files

Fix exit() behaviour by introducing the new js.scope value which is the

scripts global scope (as opposed to js.global which is the instance global
scope), and having exit() define exit_code in js.scope instead of js.global.

This also sets exit_code in js.scope to null when preparing to execute a new
script.  If a new script starts in the same scope as an old one, the old
exit_code value will be destroyed.

This should only impact scripts where js.global != js.scope (bbs.exec()ed,
and mailsrvr)
parent 0664ba8b
......@@ -578,7 +578,7 @@ long sbbs_t::js_execfile(const char *cmd, const char* startup_dir, JSObject* sco
JSObject* js_scope=scope;
JSObject* js_script=NULL;
jsval rval;
int32 result=0;
int32_t result=0;
if(js_cx==NULL) {
errormsg(WHERE,ERR_CHK,"JavaScript support",0);
......@@ -665,7 +665,7 @@ long sbbs_t::js_execfile(const char *cmd, const char* startup_dir, JSObject* sco
JS_SetBranchCallback(js_cx, js_BranchCallback);
#endif
js_PrepareToExecute(js_cx, js_glob, path, startup_dir);
js_PrepareToExecute(js_cx, js_glob, path, startup_dir, js_scope);
}
JS_ExecuteScript(js_cx, js_scope, js_script, &rval);
......
......@@ -1010,7 +1010,7 @@ BOOL js_generate_index(JSContext* js_cx, JSObject* parent,
break;
}
js_PrepareToExecute(js_cx, parent, spath, /* startup_dir: */NULL);
js_PrepareToExecute(js_cx, parent, spath, /* startup_dir: */NULL, parent);
if((success=JS_ExecuteScript(js_cx, parent, js_script, &rval))!=TRUE) {
lprintf(LOG_ERR,"%04d !JavaScript FAILED to execute script (%s)",sock,spath);
break;
......
......@@ -719,9 +719,20 @@ js_exit(JSContext *cx, uintN argc, jsval *arglist)
{
JSObject *obj=JS_THIS_OBJECT(cx, arglist);
jsval *argv=JS_ARGV(cx, arglist);
if(argc)
jsval val;
if(argc) {
if(JS_GetProperty(cx, obj, "js", &val) && JSVAL_IS_OBJECT(val)) {
obj = JSVAL_TO_OBJECT(val);
if(JS_GetProperty(cx, obj, "scope", &val) && JSVAL_IS_OBJECT(val))
obj = JSVAL_TO_OBJECT(val);
else
obj = JS_THIS_OBJECT(cx, arglist);
}
JS_DefineProperty(cx, obj, "exit_code", argv[0]
,NULL,NULL,JSPROP_ENUMERATE|JSPROP_READONLY);
}
JS_SET_RVAL(cx, arglist, JSVAL_VOID);
......
......@@ -637,7 +637,7 @@ void msvc_invalid_parameter_handler(const wchar_t* expression,
}
#endif
void DLLCALL js_PrepareToExecute(JSContext *cx, JSObject *obj, const char *filename, const char* startup_dir)
void DLLCALL js_PrepareToExecute(JSContext *cx, JSObject *obj, const char *filename, const char* startup_dir, JSObject *scope)
{
JSString* str;
jsval val;
......@@ -667,7 +667,11 @@ void DLLCALL js_PrepareToExecute(JSContext *cx, JSObject *obj, const char *filen
if((str=JS_NewStringCopyZ(cx, startup_dir)) != NULL)
JS_DefineProperty(cx, js, "startup_dir", STRING_TO_JSVAL(str)
,NULL,NULL,JSPROP_ENUMERATE|JSPROP_READONLY);
JS_DefineProperty(cx, js, "scope", OBJECT_TO_JSVAL(scope)
,NULL,NULL,JSPROP_ENUMERATE|JSPROP_READONLY);
}
JS_DefineProperty(cx, scope, "exit_code", JSVAL_NULL
,NULL,NULL,JSPROP_ENUMERATE|JSPROP_PERMANENT);
#if defined(_MSC_VER)
_set_invalid_parameter_handler(msvc_invalid_parameter_handler);
#endif
......
......@@ -940,7 +940,7 @@ long js_exec(const char *fname, char** args)
,path
,diff);
js_PrepareToExecute(js_cx, js_glob, fname==NULL ? NULL : path, orig_cwd);
js_PrepareToExecute(js_cx, js_glob, fname==NULL ? NULL : path, orig_cwd, js_glob);
start=xp_timer();
if(debugger)
debug_prompt(js_cx, js_script);
......
......@@ -1891,7 +1891,7 @@ js_mailproc(SOCKET sock, client_t* client, user_t* user, struct mailproc* mailpr
lprintf(LOG_DEBUG,"%04d %s Executing: %s"
,sock, log_prefix, cmdline);
if((js_script=JS_CompileFile(*js_cx, js_scope, path)) != NULL)
js_PrepareToExecute(*js_cx, js_scope, path, /* startup_dir: */NULL);
js_PrepareToExecute(*js_cx, js_scope, path, /* startup_dir: */NULL, js_scope);
}
if(js_script==NULL)
break;
......@@ -1899,7 +1899,7 @@ js_mailproc(SOCKET sock, client_t* client, user_t* user, struct mailproc* mailpr
/* ToDo: Set operational callback */
success=JS_ExecuteScript(*js_cx, js_scope, js_script, &rval);
JS_GetProperty(*js_cx, *js_glob, "exit_code", &rval);
JS_GetProperty(*js_cx, js_scope, "exit_code", &rval);
if(rval!=JSVAL_VOID && JSVAL_IS_NUMBER(rval))
JS_ValueToInt32(*js_cx,rval,result);
......
......@@ -1194,7 +1194,7 @@ extern "C" {
DLLEXPORT JSObject* DLLCALL js_CreateInternalJsObject(JSContext*, JSObject* parent, js_callback_t*, js_startup_t*);
DLLEXPORT JSBool DLLCALL js_CommonOperationCallback(JSContext*, js_callback_t*);
DLLEXPORT void DLLCALL js_EvalOnExit(JSContext*, JSObject*, js_callback_t*);
DLLEXPORT void DLLCALL js_PrepareToExecute(JSContext*, JSObject*, const char *filename, const char* startup_dir);
DLLEXPORT void DLLCALL js_PrepareToExecute(JSContext*, JSObject*, const char *filename, const char* startup_dir, JSObject *);
DLLEXPORT char* DLLCALL js_getstring(JSContext *cx, JSString *str);
/* js_system.c */
......
......@@ -1199,7 +1199,7 @@ static void js_service_thread(void* arg)
if(js_script==NULL)
lprintf(LOG_ERR,"%04d !JavaScript FAILED to compile script (%s)",socket,spath);
else {
js_PrepareToExecute(js_cx, js_glob, spath, /* startup_dir */NULL);
js_PrepareToExecute(js_cx, js_glob, spath, /* startup_dir */NULL, js_glob);
JS_SetOperationCallback(js_cx, js_OperationCallback);
JS_ExecuteScript(js_cx, js_glob, js_script, &rval);
js_EvalOnExit(js_cx, js_glob, &service_client.callback);
......@@ -1307,7 +1307,7 @@ static void js_static_service_thread(void* arg)
break;
}
js_PrepareToExecute(js_cx, js_glob, spath, /* startup_dir */NULL);
js_PrepareToExecute(js_cx, js_glob, spath, /* startup_dir */NULL, js_glob);
JS_ExecuteScript(js_cx, js_glob, js_script, &rval);
js_EvalOnExit(js_cx, js_glob, &service_client.callback);
JS_RemoveObjectRoot(js_cx, &js_glob);
......
......@@ -4736,7 +4736,7 @@ static BOOL exec_ssjs(http_session_t* session, char* script) {
lprintf(LOG_DEBUG,"%04d JavaScript: Executing script: %s",session->socket,script);
start=xp_timer();
js_PrepareToExecute(session->js_cx, session->js_glob, script, /* startup_dir */NULL);
js_PrepareToExecute(session->js_cx, session->js_glob, script, /* startup_dir */NULL, session->js_glob);
JS_ExecuteScript(session->js_cx, session->js_glob, js_script, &rval);
js_EvalOnExit(session->js_cx, session->js_glob, &session->js_callback);
JS_RemoveObjectRoot(session->js_cx, &session->js_glob);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment