Skip to content
Snippets Groups Projects
Commit 0fe85a9c authored by rswindell's avatar rswindell
Browse files

Added support for thread-safe JavaScript usage.

Added js_initcx() member function to sbbs_t class.
parent dc23b2b7
No related branches found
No related tags found
No related merge requests found
......@@ -339,6 +339,61 @@ js_ErrorReporter(JSContext *cx, const char *message, JSErrorReport *report)
}
}
bool sbbs_t::js_initcx()
{
if((js_cx = JS_NewContext(js_runtime, JAVASCRIPT_CONTEXT_STACK))==NULL)
return(false);
bool success=false;
JS_BeginRequest(js_cx); /* Required for multi-thread support */
do {
JS_SetErrorReporter(js_cx, js_ErrorReporter);
JS_SetContextPrivate(js_cx, this); /* Store a pointer to sbbs_t instance */
if((js_glob = JS_NewObject(js_cx, &js_global_class, NULL, NULL)) ==NULL)
break;
if (!JS_InitStandardClasses(js_cx, js_glob))
break;
if (!JS_DefineFunctions(js_cx, js_glob, js_global_functions))
break;
JSObject* sysobj;
if((sysobj=js_CreateSystemObject(&cfg, js_cx, js_glob))==NULL)
break;
char ver[256];
jsval val;
sprintf(ver,"%s v%s",TELNET_SERVER,VERSION);
val = STRING_TO_JSVAL(JS_NewStringCopyZ(js_cx, ver));
if(!JS_SetProperty(js_cx, sysobj, "version", &val))
break;
val = STRING_TO_JSVAL(JS_NewStringCopyZ(js_cx, bbs_ver()));
if(!JS_SetProperty(js_cx, sysobj, "version_detail", &val))
break;
success=true;
} while(0);
JS_EndRequest(js_cx); /* Required for multi-thread support */
if(!success) {
JS_DestroyContext(js_cx);
js_cx=NULL;
return(false);
}
return(true);
}
#endif /* JAVASCRIPT */
#ifdef _WINSOCKAPI_
......@@ -854,6 +909,8 @@ void event_thread(void* arg)
thread_up();
sbbs->js_initcx(); /* This must be done in the context of the event thread */
while(1) {
pthread_mutex_lock(&event_mutex);
......@@ -1634,44 +1691,6 @@ bool sbbs_t::init()
/** Put in if(cfg.node_num) ? (not needed for server and event threads) */
backout();
#ifdef JAVASCRIPT
if((js_cx = JS_NewContext(js_runtime, JAVASCRIPT_CONTEXT_STACK))==NULL)
return(false);
JS_SetErrorReporter(js_cx, js_ErrorReporter);
JS_SetContextPrivate(js_cx, this); /* Store a pointer to sbbs_t instance */
// JS_SetErrorReporter(js_cx, js_ErrorReporter);
if((js_glob = JS_NewObject(js_cx, &js_global_class, NULL, NULL)) ==NULL)
return(false);
if (!JS_InitStandardClasses(js_cx, js_glob))
return(false);
if (!JS_DefineFunctions(js_cx, js_glob, js_global_functions))
return(false);
JSObject* sysobj;
if((sysobj=js_CreateSystemObject(&cfg, js_cx, js_glob))==NULL)
return(false);
char ver[256];
jsval val;
sprintf(ver,"%s v%s",TELNET_SERVER,VERSION);
val = STRING_TO_JSVAL(JS_NewStringCopyZ(js_cx, ver));
if(!JS_SetProperty(js_cx, sysobj, "version", &val))
return(FALSE);
val = STRING_TO_JSVAL(JS_NewStringCopyZ(js_cx, bbs_ver()));
if(!JS_SetProperty(js_cx, sysobj, "version_detail", &val))
return(FALSE);
#endif /* JAVASCRIPT */
/* Reset COMMAND SHELL */
main_csi.str=(char *)MALLOC(1024);
......@@ -2539,6 +2558,8 @@ void node_thread(void* arg)
update_clients();
thread_up();
sbbs->js_initcx(); /* This must be done in the context of the node thread */
if(sbbs->answer()) {
if(sbbs->qwklogon) {
......
......@@ -86,6 +86,7 @@
#else
#define XP_PC
#endif
#define JS_THREADSAFE /* required for multi-thread support */
#include <jsapi.h>
#define JAVASCRIPT_RUNTIME_MEMORY (1*1024*1024)
......@@ -166,6 +167,7 @@ public:
JSContext* js_cx;
JSObject* js_glob;
long js_execfile(char *fname);
bool js_initcx(void);
#endif
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment