changeset 19:bbbad7e4b4aa

Minor refactorings, made the wrapping code a bit more robust.
author Atul Varma <varmaa@toolness.com>
date Mon, 22 Jun 2009 08:12:21 -0700
parents 33b2d70d8ada
children 802ab1d478c6
files spidermonkey-playground.cpp wrapper.cpp wrapper.h
diffstat 3 files changed, 33 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/spidermonkey-playground.cpp	Mon Jun 22 07:03:31 2009 -0700
+++ b/spidermonkey-playground.cpp	Mon Jun 22 08:12:21 2009 -0700
@@ -70,23 +70,6 @@
   return JS_TRUE;
 }
 
-static JSBool wrap(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
-                   jsval *rval)
-{
-  JSObject *wrappee;
-  JSObject *resolver;
-
-  if (!JS_ConvertArguments(cx, argc, argv, "oo", &wrappee, &resolver))
-    return JS_FALSE;
-
-  JSObject *result;
-
-  result = wrapObject(cx, argv[0], argv[1]);
-
-  *rval = OBJECT_TO_JSVAL(result);
-  return JS_TRUE;
-}
-
 static JSBool require(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
                       jsval *rval)
 {
@@ -314,12 +297,12 @@
 }
 
 static JSFunctionSpec tcb_global_functions[] = {
-  JS_FS("print",   print,   1, 0, 0),
-  JS_FS("wrap",    wrap,    2, 0, 0),
-  JS_FS("stack",   stack,   0, 0, 0),
-  JS_FS("require", require, 2, 0, 0),
+  JS_FS("print",          print,          1, 0, 0),
+  JS_FS("wrap",           wrapObject,     2, 0, 0),
+  JS_FS("stack",          stack,          0, 0, 0),
+  JS_FS("require",        require,        2, 0, 0),
   JS_FS("lookupProperty", lookupProperty, 2, 0, 0),
-  JS_FS("functionInfo", functionInfo, 1, 0, 0),
+  JS_FS("functionInfo",   functionInfo,   1, 0, 0),
   JS_FS_END
 };
 
--- a/wrapper.cpp	Mon Jun 22 07:03:31 2009 -0700
+++ b/wrapper.cpp	Mon Jun 22 08:12:21 2009 -0700
@@ -290,9 +290,9 @@
   return NULL;
 }
 
-JSExtendedClass sXPC_FlexibleWrapper_JSClass = {
+JSExtendedClass sFlexibleWrapper_JSClass = {
   // JSClass (JSExtendedClass.base) initialization
-  { "XPCFlexibleWrapper",
+  { "FlexibleWrapper",
     JSCLASS_NEW_RESOLVE | JSCLASS_IS_EXTENDED |
     JSCLASS_HAS_RESERVED_SLOTS(2),
     addProperty,        delProperty,
@@ -313,15 +313,32 @@
   JSCLASS_NO_RESERVED_MEMBERS
 };
 
-JSObject *wrapObject(JSContext *cx, jsval object, jsval resolver)
+JSBool wrapObject(JSContext *cx, JSObject *obj, uintN argc,
+                  jsval *argv, jsval *rval)
 {
-  JSObject *obj = JS_NewObjectWithGivenProto(
+  JSObject *wrappee;
+  JSObject *resolver;
+
+  if (!JS_ConvertArguments(cx, argc, argv, "oo", &wrappee, &resolver))
+    return JS_FALSE;
+
+  JSObject *wrapper = JS_NewObjectWithGivenProto(
     cx,
-    &sXPC_FlexibleWrapper_JSClass.base,
+    &sFlexibleWrapper_JSClass.base,
     NULL,
     JS_GetScopeChain(cx)
     );
-  JS_SetReservedSlot(cx, obj, SLOT_RESOLVER, resolver);
-  JS_SetReservedSlot(cx, obj, SLOT_WRAPPEE, object);
-  return obj;
+  if (wrapper == NULL) {
+    JS_ReportError(cx, "Creating new wrapper failed.");
+    return JS_FALSE;
+  }
+
+  if (!JS_SetReservedSlot(cx, wrapper, SLOT_RESOLVER,
+                          OBJECT_TO_JSVAL(resolver)) ||
+      !JS_SetReservedSlot(cx, wrapper, SLOT_WRAPPEE,
+                          OBJECT_TO_JSVAL(wrappee)))
+    return JS_FALSE;
+
+  *rval = OBJECT_TO_JSVAL(wrapper);
+  return JS_TRUE;
 }
--- a/wrapper.h	Mon Jun 22 07:03:31 2009 -0700
+++ b/wrapper.h	Mon Jun 22 08:12:21 2009 -0700
@@ -36,5 +36,6 @@
 
 #include "jsapi.h"
 
-extern JSExtendedClass sXPC_FlexibleWrapper_JSClass;
-extern JSObject *wrapObject(JSContext *cx, jsval object, jsval resolver);
+extern JSExtendedClass sFlexibleWrapper_JSClass;
+extern JSBool wrapObject(JSContext *cx, JSObject *obj, uintN argc,
+                         jsval *argv, jsval *rval);