michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: set ts=8 sts=4 et sw=4 tw=99: michael@0: * michael@0: * Tests JS_TransplantObject michael@0: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "jsobj.h" michael@0: #include "jswrapper.h" michael@0: michael@0: #include "jsapi-tests/tests.h" michael@0: michael@0: #include "vm/ProxyObject.h" michael@0: michael@0: const js::Class OuterWrapperClass = michael@0: PROXY_CLASS_WITH_EXT( michael@0: "Proxy", michael@0: 0, /* additional slots */ michael@0: 0, /* additional class flags */ michael@0: nullptr, /* call */ michael@0: nullptr, /* construct */ michael@0: PROXY_MAKE_EXT( michael@0: nullptr, /* outerObject */ michael@0: js::proxy_innerObject, michael@0: nullptr, /* iteratorObject */ michael@0: false /* isWrappedNative */ michael@0: )); michael@0: michael@0: static JSObject * michael@0: wrap(JSContext *cx, JS::HandleObject toWrap, JS::HandleObject target) michael@0: { michael@0: JSAutoCompartment ac(cx, target); michael@0: JS::RootedObject wrapper(cx, toWrap); michael@0: if (!JS_WrapObject(cx, &wrapper)) michael@0: return nullptr; michael@0: return wrapper; michael@0: } michael@0: michael@0: static JSObject * michael@0: PreWrap(JSContext *cx, JS::HandleObject scope, JS::HandleObject obj, unsigned flags) michael@0: { michael@0: JS_GC(JS_GetRuntime(cx)); michael@0: return obj; michael@0: } michael@0: michael@0: static JSObject * michael@0: Wrap(JSContext *cx, JS::HandleObject existing, JS::HandleObject obj, michael@0: JS::HandleObject proto, JS::HandleObject parent, unsigned flags) michael@0: { michael@0: return js::Wrapper::New(cx, obj, parent, &js::CrossCompartmentWrapper::singleton); michael@0: } michael@0: michael@0: static const JSWrapObjectCallbacks WrapObjectCallbacks = { michael@0: Wrap, michael@0: PreWrap michael@0: }; michael@0: michael@0: BEGIN_TEST(testBug604087) michael@0: { michael@0: js::WrapperOptions options; michael@0: options.setClass(&OuterWrapperClass); michael@0: options.setSingleton(true); michael@0: JS::RootedObject outerObj(cx, js::Wrapper::New(cx, global, global, &js::Wrapper::singleton, &options)); michael@0: JS::RootedObject compartment2(cx, JS_NewGlobalObject(cx, getGlobalClass(), nullptr, JS::FireOnNewGlobalHook)); michael@0: JS::RootedObject compartment3(cx, JS_NewGlobalObject(cx, getGlobalClass(), nullptr, JS::FireOnNewGlobalHook)); michael@0: JS::RootedObject compartment4(cx, JS_NewGlobalObject(cx, getGlobalClass(), nullptr, JS::FireOnNewGlobalHook)); michael@0: michael@0: JS::RootedObject c2wrapper(cx, wrap(cx, outerObj, compartment2)); michael@0: CHECK(c2wrapper); michael@0: c2wrapper->as().setExtra(0, js::Int32Value(2)); michael@0: michael@0: JS::RootedObject c3wrapper(cx, wrap(cx, outerObj, compartment3)); michael@0: CHECK(c3wrapper); michael@0: c3wrapper->as().setExtra(0, js::Int32Value(3)); michael@0: michael@0: JS::RootedObject c4wrapper(cx, wrap(cx, outerObj, compartment4)); michael@0: CHECK(c4wrapper); michael@0: c4wrapper->as().setExtra(0, js::Int32Value(4)); michael@0: compartment4 = c4wrapper = nullptr; michael@0: michael@0: JS::RootedObject next(cx); michael@0: { michael@0: JSAutoCompartment ac(cx, compartment2); michael@0: next = js::Wrapper::New(cx, compartment2, compartment2, &js::Wrapper::singleton, &options); michael@0: CHECK(next); michael@0: } michael@0: michael@0: JS_SetWrapObjectCallbacks(JS_GetRuntime(cx), &WrapObjectCallbacks); michael@0: CHECK(JS_TransplantObject(cx, outerObj, next)); michael@0: return true; michael@0: } michael@0: END_TEST(testBug604087)