Commit b898eb73 authored by rswindell's avatar rswindell

An experimental external() mode flag that runs JS modules in their own...

An experimental external() mode flag that runs JS modules in their own separate runtime and context - currently does not work (appears to corrupt the shared JS context). Not actually used currently.
The reason I wanted to run a JS module (actually JS doorgame) in its own context was to get useful error messages (filenames and line numbers) when the module throws an exception. Currently, the error is just reported as occuring in xtrn_sec.js, which isn't helpful or correct.
parent fe6439cf
......@@ -39,6 +39,7 @@
#include "sbbs.h"
#include "cmdshell.h"
#include "js_request.h"
#include "js_rtpool.h"
char ** sbbs_t::getstrvar(csi_t *bin, uint32_t name)
{
......@@ -688,9 +689,8 @@ long sbbs_t::js_execfile(const char *cmd, const char* startup_dir, JSObject* sco
#else
JS_SetBranchCallback(js_cx, js_BranchCallback);
#endif
js_PrepareToExecute(js_cx, js_glob, path, startup_dir, js_scope);
}
js_PrepareToExecute(js_cx, js_glob, path, startup_dir, js_scope);
JS_ExecuteScript(js_cx, js_scope, js_script, &rval);
sys_status &=~ SS_ABORT;
......@@ -728,6 +728,27 @@ long sbbs_t::js_execfile(const char *cmd, const char* startup_dir, JSObject* sco
return(result);
}
// Execute a JS Module in its own runtime and context
// Experimental and currently broken
long sbbs_t::js_execmodule(const char *cmd, long mode, const char* startup_dir)
{
if(!(mode&EX_JS_CX))
return js_execfile(cmd, startup_dir);
JSRuntime* js_runtime;
JSObject* js_glob;
JSContext* js_cx = js_init(&js_runtime, &js_glob, "XtrnModule");
js_create_user_objects(js_cx, js_glob);
long result = js_execfile(cmd, startup_dir, js_glob, js_cx);
JS_BEGINREQUEST(js_cx);
JS_RemoveObjectRoot(js_cx, &js_glob);
JS_ENDREQUEST(js_cx);
JS_DestroyContext(js_cx);
lprintf(LOG_DEBUG,"JavaScript: Destroying runtime");
jsrt_Release(js_runtime);
return result;
}
#endif
/* Important change as of Nov-16-2006, 'cmdline' may contain args */
......
......@@ -406,6 +406,7 @@ public:
JSObject* js_hotkey_glob;
js_callback_t js_callback;
long js_execfile(const char *fname, const char* startup_dir, JSObject* scope = NULL, JSContext* cx = NULL);
long js_execmodule(const char *fname, long mode, const char* startup_dir);
JSContext* js_init(JSRuntime**, JSObject**, const char* desc);
void js_cleanup(void);
bool js_create_user_objects(JSContext*, JSObject* glob);
......
......@@ -415,6 +415,7 @@ typedef enum { /* Values for xtrn_t.event */
#define QUOTEWRAP (1<<21) /* Word-wrap quoted message text */
#define SAVECOLUMNS (1<<22) /* Save/share current terminal width */
#define XTRN_UTF8 (1<<23) /* External program supports UTF-8 */
#define XTRN_JS_CX (1<<24) /* New JavaScript Context */
#define XTRN_CONIO (1<<31) /* Intercept Windows Console I/O (Drwy) */
/* Bits in cfg.xtrn_misc */
......@@ -843,6 +844,7 @@ enum { /* readmail and delmailidx which types */
#define EX_CHKTIME XTRN_CHKTIME /* Check time left */
#define EX_NOECHO XTRN_NOECHO /* Don't echo stdin to stdout */
#define EX_STDIO (EX_STDIN|EX_STDOUT)
#define EX_JS_CX (1<<24) /* New JavaScript context */
#define EX_NOLOG (1<<30) /* Don't log intercepted stdio */
#define EX_CONIO (1<<31) /* Intercept Windows console I/O (doorway) */
......
......@@ -263,9 +263,9 @@ static bool native_executable(scfg_t* cfg, const char* cmdline, long mode)
if(cmdline[0]=='*') /* Baja module or JavaScript */ \
return(exec_bin(cmdline+1,&main_csi,startup_dir))
#ifdef JAVASCRIPT
#define XTRN_LOADABLE_JS_MODULE(cmdline,startup_dir) \
#define XTRN_LOADABLE_JS_MODULE(cmdline,mode,startup_dir) \
if(cmdline[0]=='?') /* JavaScript */ \
return(js_execfile(cmdline+1,startup_dir))
return(js_execmodule(cmdline+1,mode,startup_dir))
#else
#define XTRN_LOADABLE_JS_MODULE
#endif
......@@ -367,7 +367,7 @@ int sbbs_t::external(const char* cmdline, long mode, const char* startup_dir)
}
XTRN_LOADABLE_MODULE(cmdline,startup_dir);
XTRN_LOADABLE_JS_MODULE(cmdline,startup_dir);
XTRN_LOADABLE_JS_MODULE(cmdline,mode,startup_dir);
attr(cfg.color[clr_external]); /* setup default attributes */
......@@ -1095,7 +1095,7 @@ int sbbs_t::external(const char* cmdline, long mode, const char* startup_dir)
startup_dir=nulstr;
XTRN_LOADABLE_MODULE(cmdline,startup_dir);
XTRN_LOADABLE_JS_MODULE(cmdline,startup_dir);
XTRN_LOADABLE_JS_MODULE(cmdline,mode,startup_dir);
attr(cfg.color[clr_external]); /* setup default attributes */
......
......@@ -1598,7 +1598,7 @@ bool sbbs_t::exec_xtrn(uint xtrnnum)
mode|=EX_STDIO;
else if(cfg.xtrn[xtrnnum]->misc&XTRN_CONIO)
mode|=EX_CONIO;
mode|=(cfg.xtrn[xtrnnum]->misc&(XTRN_CHKTIME|XTRN_NATIVE|XTRN_NOECHO|WWIVCOLOR));
mode|=(cfg.xtrn[xtrnnum]->misc&(XTRN_CHKTIME|XTRN_NATIVE|XTRN_NOECHO|WWIVCOLOR|XTRN_JS_CX));
if(cfg.xtrn[xtrnnum]->misc&MODUSERDAT) { /* Delete MODUSER.DAT */
SAFEPRINTF(str,"%sMODUSER.DAT",dropdir); /* if for some weird */
removecase(str); /* reason it's there */
......
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