Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sts=4 et sw=4 tw=99:
3 *
4 * Tests JS_TransplantObject
5 */
6 /* This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
10 #include "jsobj.h"
11 #include "jswrapper.h"
13 #include "jsapi-tests/tests.h"
15 #include "vm/ProxyObject.h"
17 const js::Class OuterWrapperClass =
18 PROXY_CLASS_WITH_EXT(
19 "Proxy",
20 0, /* additional slots */
21 0, /* additional class flags */
22 nullptr, /* call */
23 nullptr, /* construct */
24 PROXY_MAKE_EXT(
25 nullptr, /* outerObject */
26 js::proxy_innerObject,
27 nullptr, /* iteratorObject */
28 false /* isWrappedNative */
29 ));
31 static JSObject *
32 wrap(JSContext *cx, JS::HandleObject toWrap, JS::HandleObject target)
33 {
34 JSAutoCompartment ac(cx, target);
35 JS::RootedObject wrapper(cx, toWrap);
36 if (!JS_WrapObject(cx, &wrapper))
37 return nullptr;
38 return wrapper;
39 }
41 static JSObject *
42 PreWrap(JSContext *cx, JS::HandleObject scope, JS::HandleObject obj, unsigned flags)
43 {
44 JS_GC(JS_GetRuntime(cx));
45 return obj;
46 }
48 static JSObject *
49 Wrap(JSContext *cx, JS::HandleObject existing, JS::HandleObject obj,
50 JS::HandleObject proto, JS::HandleObject parent, unsigned flags)
51 {
52 return js::Wrapper::New(cx, obj, parent, &js::CrossCompartmentWrapper::singleton);
53 }
55 static const JSWrapObjectCallbacks WrapObjectCallbacks = {
56 Wrap,
57 PreWrap
58 };
60 BEGIN_TEST(testBug604087)
61 {
62 js::WrapperOptions options;
63 options.setClass(&OuterWrapperClass);
64 options.setSingleton(true);
65 JS::RootedObject outerObj(cx, js::Wrapper::New(cx, global, global, &js::Wrapper::singleton, &options));
66 JS::RootedObject compartment2(cx, JS_NewGlobalObject(cx, getGlobalClass(), nullptr, JS::FireOnNewGlobalHook));
67 JS::RootedObject compartment3(cx, JS_NewGlobalObject(cx, getGlobalClass(), nullptr, JS::FireOnNewGlobalHook));
68 JS::RootedObject compartment4(cx, JS_NewGlobalObject(cx, getGlobalClass(), nullptr, JS::FireOnNewGlobalHook));
70 JS::RootedObject c2wrapper(cx, wrap(cx, outerObj, compartment2));
71 CHECK(c2wrapper);
72 c2wrapper->as<js::ProxyObject>().setExtra(0, js::Int32Value(2));
74 JS::RootedObject c3wrapper(cx, wrap(cx, outerObj, compartment3));
75 CHECK(c3wrapper);
76 c3wrapper->as<js::ProxyObject>().setExtra(0, js::Int32Value(3));
78 JS::RootedObject c4wrapper(cx, wrap(cx, outerObj, compartment4));
79 CHECK(c4wrapper);
80 c4wrapper->as<js::ProxyObject>().setExtra(0, js::Int32Value(4));
81 compartment4 = c4wrapper = nullptr;
83 JS::RootedObject next(cx);
84 {
85 JSAutoCompartment ac(cx, compartment2);
86 next = js::Wrapper::New(cx, compartment2, compartment2, &js::Wrapper::singleton, &options);
87 CHECK(next);
88 }
90 JS_SetWrapObjectCallbacks(JS_GetRuntime(cx), &WrapObjectCallbacks);
91 CHECK(JS_TransplantObject(cx, outerObj, next));
92 return true;
93 }
94 END_TEST(testBug604087)