Bug 798678 - Weakmap key preservation test (r=mccr8)
authorBill McCloskey <wmccloskey@mozilla.com>
Wed, 17 Oct 2012 18:22:54 -0700
changeset 110760 2f193f44cb99ff2275a14e04f50f0b22f5018c53
parent 110759 1e5bf398fc887b518462e789526ee4159fdaf632
child 110761 77deb50b07e19baa0fea0db55e54b355b8a5ff11
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewersmccr8
bugs798678
milestone19.0a1
Bug 798678 - Weakmap key preservation test (r=mccr8)
js/src/jsweakmap.cpp
js/xpconnect/tests/chrome/Makefile.in
js/xpconnect/tests/chrome/test_weakmap_keys_preserved2.xul
--- a/js/src/jsweakmap.cpp
+++ b/js/src/jsweakmap.cpp
@@ -266,27 +266,31 @@ WeakMap_set(JSContext *cx, unsigned argc
 {
     CallArgs args = CallArgsFromVp(argc, vp);
     return CallNonGenericMethod<IsWeakMap, WeakMap_set_impl>(cx, args);
 }
 
 JS_FRIEND_API(JSBool)
 JS_NondeterministicGetWeakMapKeys(JSContext *cx, JSObject *obj, JSObject **ret)
 {
+    obj = UnwrapObject(obj);
     if (!obj || !obj->isWeakMap()) {
         *ret = NULL;
         return true;
     }
     RootedObject arr(cx, NewDenseEmptyArray(cx));
     if (!arr)
         return false;
     ObjectValueMap *map = GetObjectMap(obj);
     if (map) {
         for (ObjectValueMap::Base::Range r = map->all(); !r.empty(); r.popFront()) {
-            if (!js_NewbornArrayPush(cx, arr, ObjectValue(*r.front().key)))
+            RootedObject key(cx, r.front().key);
+            if (!JS_WrapObject(cx, key.address()))
+                return false;
+            if (!js_NewbornArrayPush(cx, arr, ObjectValue(*key)))
                 return false;
         }
     }
     *ret = arr;
     return true;
 }
 
 static void
--- a/js/xpconnect/tests/chrome/Makefile.in
+++ b/js/xpconnect/tests/chrome/Makefile.in
@@ -58,16 +58,17 @@ MOCHITEST_CHROME_FILES = \
 		file_expandosharing.jsm \
 		test_getweakmapkeys.xul \
 		test_mozMatchesSelector.xul \
 		test_nodelists.xul \
 		test_precisegc.xul \
 		test_sandboxImport.xul \
 		test_weakmaps.xul \
 		test_weakmap_keys_preserved.xul \
+		test_weakmap_keys_preserved2.xul \
 		test_weakref.xul \
 		test_wrappers.xul \
 		$(NULL)
 
 # Disabled until this test gets updated to test the new proxy based
 # wrappers.
 #		test_wrappers-2.xul \
 
new file mode 100644
--- /dev/null
+++ b/js/xpconnect/tests/chrome/test_weakmap_keys_preserved2.xul
@@ -0,0 +1,84 @@
+<?xml version="1.0"?>
+<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
+<?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=673468
+-->
+<window title="Mozilla Bug "
+        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
+  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
+
+  <!-- test results are displayed in the html:body -->
+  <body xmlns="http://www.w3.org/1999/xhtml">
+  <a id="testelem" href="https://bugzilla.mozilla.org/show_bug.cgi?id="
+     target="_blank">Mozilla Bug 673468</a>
+  </body>
+
+  <!-- test code goes here -->
+  <script type="application/javascript">
+  <![CDATA[
+  /** Test for Bug 673468 **/
+
+  let Cc = Components.classes;
+  let Cu = Components.utils;
+  let Ci = Components.interfaces;
+
+  SpecialPowers.DOMWindowUtils.garbageCollect();
+
+  let get_live_dom = function () {
+    return document.getElementById("testelem");
+  };
+
+  let wrappers_as_keys_test = function () {
+    let e = document.createEvent("MessageEvent");
+    e.initMessageEvent("foo", false, false, { dummy: document.createElement("foo") }, null, null, null);
+    window.eeeevent = e;
+
+    let live_dom = e.data.dummy;
+    let dead_dom = document.createElement("div");
+    let dead_child = document.createElement("div");
+    dead_dom.appendChild(dead_child);
+    is(dead_dom.children.length, 1, "children have wrong length");
+    let wrappee = new Object();
+
+    dead_dom.abcxyz = wrappee;
+
+    let system = Cc["@mozilla.org/systemprincipal;1"].createInstance();
+    let sandbox = Cu.Sandbox(system);
+
+    sandbox.wrapper = wrappee;
+    sandbox.value = dead_dom;
+    let map = Cu.evalInSandbox("wm = new WeakMap(); wm.set(wrapper, value); wm", sandbox);
+    sandbox.wrapper = null;
+    sandbox.value = null;
+
+    live_dom.xyzabc = {wrappee: wrappee, m: map, sb: sandbox};
+
+    let key = Cu.nondeterministicGetWeakMapKeys(map)[0];
+    let value = map.get(key);
+    is(value.children.length, 1, "children have wrong length");
+  }
+
+  wrappers_as_keys_test();
+
+  let check_wrappers_as_keys = function () {
+    let live_dom = window.eeeevent.data.dummy;
+    let live_map = live_dom.xyzabc.m;
+    let sandbox = live_dom.xyzabc.sb;
+    is(Cu.nondeterministicGetWeakMapKeys(live_map).length, 1,
+      "Map should not be empty.");
+    let key = Cu.nondeterministicGetWeakMapKeys(live_map)[0];
+    let value = live_map.get(key);
+    is(value.children.length, 1, "children have wrong length");
+  }
+
+  Cu.schedulePreciseGC(function () {
+    SpecialPowers.DOMWindowUtils.cycleCollect();
+    SpecialPowers.DOMWindowUtils.garbageCollect();
+
+    check_wrappers_as_keys();
+  });
+
+  ]]>
+  </script>
+</window>