changeset 70:f6e3733eac01

Refactored some code, fixed some TODOs.
author Atul Varma <varmaa@toolness.com>
date Thu, 25 Jun 2009 18:11:01 -0700
parents be61430630ab
children e4aec93c1e3c
files spidermonkey-playground.cpp tcb.cpp tcb.h
diffstat 3 files changed, 47 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/spidermonkey-playground.cpp	Thu Jun 25 17:53:28 2009 -0700
+++ b/spidermonkey-playground.cpp	Thu Jun 25 18:11:01 2009 -0700
@@ -151,12 +151,6 @@
 }
 
 static JSFunctionSpec tcb_global_functions[] = {
-  JS_FS("print",          TCB_print,          1, 0, 0),
-  JS_FS("stack",          TCB_stack,          0, 0, 0),
-  JS_FS("lookupProperty", TCB_lookupProperty, 2, 0, 0),
-  JS_FS("functionInfo",   TCB_functionInfo,   1, 0, 0),
-  JS_FS("enumerate",      TCB_enumerate,      1, 0, 0),
-  JS_FS("forceGC",        TCB_forceGC,        0, 0, 0),
   JS_FS("getWrapper",     getWrapper,         1, 0, 0),
   JS_FS("unwrap",         unwrapObject,       1, 0, 0),
   JS_FS("wrap",           wrapObject,         2, 0, 0),
@@ -193,34 +187,17 @@
 
   JS_BeginRequest(tcb_cx);
 
-#ifdef DEBUG
-  JS_SetGCZeal(tcb_cx, 2);
-#endif
+  jsval rval;
 
-  /* Create the TCB's global object. */
-  tcb_global = JS_NewObject(tcb_cx, &TCB_global_class, NULL, NULL);
-  if (tcb_global == NULL)
+  if (!TCB_init(tcb_cx, &rval))
     return 1;
+  tcb_global = JSVAL_TO_OBJECT(rval);
 
   JS_AddNamedRoot(tcb_cx, &tcb_global, "TCB Global");
 
-  /* Populate the tcb_global object with the standard globals,
-     like Object and Array. */
-  if (!JS_InitStandardClasses(tcb_cx, tcb_global))
-    return 1;
-
   if (!JS_DefineFunctions(tcb_cx, tcb_global, tcb_global_functions))
     return 1;
 
-  // TODO: Check for return values here.
-  JS_SetThrowHook(rt, TCB_throwHook, tcb_global);
-  JS_DefineProperty(tcb_cx, tcb_global, "lastExceptionTraceback", JSVAL_NULL,
-                    NULL, NULL, 0);
-  JS_DefineProperty(tcb_cx, tcb_global, "lastException", JSVAL_NULL,
-                    NULL, NULL, 0);
-
-  /* Your application code here. This may include JSAPI calls
-     to create your own custom JS objects and run scripts. */
   FILE *f = fopen(TCB_FILENAME, "r");
   if (!f)
     return 1;
@@ -233,7 +210,6 @@
   fread(source, fileSize, 1, f);
   fclose(f);
 
-  jsval rval;
   if (!JS_EvaluateScript(tcb_cx, tcb_global, source,
                          fileSize, TCB_FILENAME, 1,
                          &rval)) {
--- a/tcb.cpp	Thu Jun 25 17:53:28 2009 -0700
+++ b/tcb.cpp	Thu Jun 25 18:11:01 2009 -0700
@@ -1,5 +1,15 @@
 #include "tcb.h"
 
+static JSFunctionSpec TCB_global_functions[] = {
+  JS_FS("print",          TCB_print,          1, 0, 0),
+  JS_FS("stack",          TCB_stack,          0, 0, 0),
+  JS_FS("lookupProperty", TCB_lookupProperty, 2, 0, 0),
+  JS_FS("functionInfo",   TCB_functionInfo,   1, 0, 0),
+  JS_FS("enumerate",      TCB_enumerate,      1, 0, 0),
+  JS_FS("forceGC",        TCB_forceGC,        0, 0, 0),
+  JS_FS_END
+};
+
 // The class of the global object.
 JSClass TCB_global_class = {
   "TCBGlobal", JSCLASS_GLOBAL_FLAGS,
@@ -231,3 +241,35 @@
 
   return JSTRAP_CONTINUE;
 }
+
+JSBool TCB_init(JSContext *cx, jsval *rval)
+{
+#ifdef DEBUG
+  JS_SetGCZeal(cx, 2);
+#endif
+
+  JSRuntime *rt = JS_GetRuntime(cx);
+
+  /* Create the TCB's global object. */
+  JSObject *global = JS_NewObject(cx, &TCB_global_class, NULL, NULL);
+  if (global == NULL) {
+    JS_ReportOutOfMemory(cx);
+    return JS_FALSE;
+  }
+
+  /* Populate the global object with the standard globals,
+     like Object and Array. */
+  if (!JS_InitStandardClasses(cx, global) ||
+      !JS_DefineFunctions(cx, global, TCB_global_functions))
+    return JS_FALSE;
+
+  if (!JS_SetThrowHook(rt, TCB_throwHook, global) ||
+      !JS_DefineProperty(cx, global, "lastExceptionTraceback", JSVAL_NULL,
+                         NULL, NULL, 0) ||
+      !JS_DefineProperty(cx, global, "lastException", JSVAL_NULL,
+                         NULL, NULL, 0))
+    return JS_FALSE;
+
+  *rval = OBJECT_TO_JSVAL(global);
+  return JS_TRUE;
+}
--- a/tcb.h	Thu Jun 25 17:53:28 2009 -0700
+++ b/tcb.h	Thu Jun 25 18:11:01 2009 -0700
@@ -23,3 +23,5 @@
 
 extern JSTrapStatus TCB_throwHook(JSContext *cx, JSScript *script,
                                   jsbytecode *pc, jsval *rval, void *closure);
+
+extern JSBool TCB_init(JSContext *cx, jsval *rval);