1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/jsapi-tests/testBug604087.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,94 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99: 1.6 + * 1.7 + * Tests JS_TransplantObject 1.8 + */ 1.9 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.10 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.11 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.12 + 1.13 +#include "jsobj.h" 1.14 +#include "jswrapper.h" 1.15 + 1.16 +#include "jsapi-tests/tests.h" 1.17 + 1.18 +#include "vm/ProxyObject.h" 1.19 + 1.20 +const js::Class OuterWrapperClass = 1.21 + PROXY_CLASS_WITH_EXT( 1.22 + "Proxy", 1.23 + 0, /* additional slots */ 1.24 + 0, /* additional class flags */ 1.25 + nullptr, /* call */ 1.26 + nullptr, /* construct */ 1.27 + PROXY_MAKE_EXT( 1.28 + nullptr, /* outerObject */ 1.29 + js::proxy_innerObject, 1.30 + nullptr, /* iteratorObject */ 1.31 + false /* isWrappedNative */ 1.32 + )); 1.33 + 1.34 +static JSObject * 1.35 +wrap(JSContext *cx, JS::HandleObject toWrap, JS::HandleObject target) 1.36 +{ 1.37 + JSAutoCompartment ac(cx, target); 1.38 + JS::RootedObject wrapper(cx, toWrap); 1.39 + if (!JS_WrapObject(cx, &wrapper)) 1.40 + return nullptr; 1.41 + return wrapper; 1.42 +} 1.43 + 1.44 +static JSObject * 1.45 +PreWrap(JSContext *cx, JS::HandleObject scope, JS::HandleObject obj, unsigned flags) 1.46 +{ 1.47 + JS_GC(JS_GetRuntime(cx)); 1.48 + return obj; 1.49 +} 1.50 + 1.51 +static JSObject * 1.52 +Wrap(JSContext *cx, JS::HandleObject existing, JS::HandleObject obj, 1.53 + JS::HandleObject proto, JS::HandleObject parent, unsigned flags) 1.54 +{ 1.55 + return js::Wrapper::New(cx, obj, parent, &js::CrossCompartmentWrapper::singleton); 1.56 +} 1.57 + 1.58 +static const JSWrapObjectCallbacks WrapObjectCallbacks = { 1.59 + Wrap, 1.60 + PreWrap 1.61 +}; 1.62 + 1.63 +BEGIN_TEST(testBug604087) 1.64 +{ 1.65 + js::WrapperOptions options; 1.66 + options.setClass(&OuterWrapperClass); 1.67 + options.setSingleton(true); 1.68 + JS::RootedObject outerObj(cx, js::Wrapper::New(cx, global, global, &js::Wrapper::singleton, &options)); 1.69 + JS::RootedObject compartment2(cx, JS_NewGlobalObject(cx, getGlobalClass(), nullptr, JS::FireOnNewGlobalHook)); 1.70 + JS::RootedObject compartment3(cx, JS_NewGlobalObject(cx, getGlobalClass(), nullptr, JS::FireOnNewGlobalHook)); 1.71 + JS::RootedObject compartment4(cx, JS_NewGlobalObject(cx, getGlobalClass(), nullptr, JS::FireOnNewGlobalHook)); 1.72 + 1.73 + JS::RootedObject c2wrapper(cx, wrap(cx, outerObj, compartment2)); 1.74 + CHECK(c2wrapper); 1.75 + c2wrapper->as<js::ProxyObject>().setExtra(0, js::Int32Value(2)); 1.76 + 1.77 + JS::RootedObject c3wrapper(cx, wrap(cx, outerObj, compartment3)); 1.78 + CHECK(c3wrapper); 1.79 + c3wrapper->as<js::ProxyObject>().setExtra(0, js::Int32Value(3)); 1.80 + 1.81 + JS::RootedObject c4wrapper(cx, wrap(cx, outerObj, compartment4)); 1.82 + CHECK(c4wrapper); 1.83 + c4wrapper->as<js::ProxyObject>().setExtra(0, js::Int32Value(4)); 1.84 + compartment4 = c4wrapper = nullptr; 1.85 + 1.86 + JS::RootedObject next(cx); 1.87 + { 1.88 + JSAutoCompartment ac(cx, compartment2); 1.89 + next = js::Wrapper::New(cx, compartment2, compartment2, &js::Wrapper::singleton, &options); 1.90 + CHECK(next); 1.91 + } 1.92 + 1.93 + JS_SetWrapObjectCallbacks(JS_GetRuntime(cx), &WrapObjectCallbacks); 1.94 + CHECK(JS_TransplantObject(cx, outerObj, next)); 1.95 + return true; 1.96 +} 1.97 +END_TEST(testBug604087)