Bug 513788 - Revise js-facing API for js-ctypes. Followup: seal important bits of the global object.
authorDan Witte <dwitte@mozilla.com>
Wed, 17 Mar 2010 10:48:48 -0700
changeset 39531 a8004c4896ffa71689157c095b5267d5300f0a87
parent 39530 aff1d6e46f2cdfc6045c7c5c8303475c1e7e4174
child 39532 cb793a909545f8744b0bad2023810282b790e2fa
push id12249
push userdwitte@mozilla.com
push dateWed, 17 Mar 2010 17:49:07 +0000
treeherdermozilla-central@a8004c4896ff [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs513788
milestone1.9.3a4pre
Bug 513788 - Revise js-facing API for js-ctypes. Followup: seal important bits of the global object. r=jorendorff
js/ctypes/Module.cpp
--- a/js/ctypes/Module.cpp
+++ b/js/ctypes/Module.cpp
@@ -88,16 +88,32 @@ Module::Call(nsIXPConnectWrappedNative* 
   (JSFUN_FAST_NATIVE | JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT)
 
 static JSFunctionSpec sModuleFunctions[] = {
   JS_FN("open", Library::Open, 1, CTYPESFN_FLAGS),
   JS_FN("cast", CData::Cast, 2, CTYPESFN_FLAGS),
   JS_FS_END
 };
 
+static JSBool
+SealObjectAndPrototype(JSContext* cx, JSObject* parent, const char* name)
+{
+  jsval prop;
+  if (!JS_GetProperty(cx, parent, name, &prop))
+    return false;
+
+  JSObject* obj = JSVAL_TO_OBJECT(prop);
+  if (!JS_GetProperty(cx, obj, "prototype", &prop))
+    return false;
+
+  JSObject* prototype = JSVAL_TO_OBJECT(prop);
+  return JS_SealObject(cx, obj, JS_FALSE) &&
+         JS_SealObject(cx, prototype, JS_FALSE);
+}
+
 JSBool
 Module::Init(JSContext* cx, JSObject* aGlobal)
 {
   // attach ctypes property to global object
   JSObject* ctypes = JS_NewObject(cx, NULL, NULL, NULL);
   if (!ctypes)
     return false;
 
@@ -109,17 +125,28 @@ Module::Init(JSContext* cx, JSObject* aG
     return false;
 
   // attach API functions
   if (!JS_DefineFunctions(cx, ctypes, sModuleFunctions))
     return false;
 
   // Seal the ctypes object, to prevent modification. (This single object
   // instance is shared amongst everyone who imports the ctypes module.)
-  return JS_SealObject(cx, ctypes, JS_FALSE) != JS_FALSE;
+  if (!JS_SealObject(cx, ctypes, JS_FALSE))
+    return false;
+
+  // Seal up Object, Function, and Array and their prototypes.
+  if (!SealObjectAndPrototype(cx, aGlobal, "Object") ||
+      !SealObjectAndPrototype(cx, aGlobal, "Function") ||
+      !SealObjectAndPrototype(cx, aGlobal, "Array"))
+    return false;
+
+  // Finally, seal the global object, for good measure. (But not recursively;
+  // this breaks things.)
+  return JS_SealObject(cx, aGlobal, JS_FALSE);
 }
 
 }
 }
 
 static nsModuleComponentInfo components[] =
 {
   {