Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * vim: set ts=8 sts=4 et sw=4 tw=99: |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "vm/ProxyObject.h" |
michael@0 | 8 | |
michael@0 | 9 | #include "jscompartment.h" |
michael@0 | 10 | #include "jsgcinlines.h" |
michael@0 | 11 | #include "jsinferinlines.h" |
michael@0 | 12 | #include "jsobjinlines.h" |
michael@0 | 13 | |
michael@0 | 14 | using namespace js; |
michael@0 | 15 | |
michael@0 | 16 | /* static */ ProxyObject * |
michael@0 | 17 | ProxyObject::New(JSContext *cx, BaseProxyHandler *handler, HandleValue priv, TaggedProto proto_, |
michael@0 | 18 | JSObject *parent_, const ProxyOptions &options) |
michael@0 | 19 | { |
michael@0 | 20 | Rooted<TaggedProto> proto(cx, proto_); |
michael@0 | 21 | RootedObject parent(cx, parent_); |
michael@0 | 22 | |
michael@0 | 23 | const Class *clasp = options.clasp(); |
michael@0 | 24 | |
michael@0 | 25 | JS_ASSERT(isValidProxyClass(clasp)); |
michael@0 | 26 | JS_ASSERT_IF(proto.isObject(), cx->compartment() == proto.toObject()->compartment()); |
michael@0 | 27 | JS_ASSERT_IF(parent, cx->compartment() == parent->compartment()); |
michael@0 | 28 | |
michael@0 | 29 | /* |
michael@0 | 30 | * Eagerly mark properties unknown for proxies, so we don't try to track |
michael@0 | 31 | * their properties and so that we don't need to walk the compartment if |
michael@0 | 32 | * their prototype changes later. But don't do this for DOM proxies, |
michael@0 | 33 | * because we want to be able to keep track of them in typesets in useful |
michael@0 | 34 | * ways. |
michael@0 | 35 | */ |
michael@0 | 36 | if (proto.isObject() && !options.singleton() && !clasp->isDOMClass()) { |
michael@0 | 37 | RootedObject protoObj(cx, proto.toObject()); |
michael@0 | 38 | if (!JSObject::setNewTypeUnknown(cx, clasp, protoObj)) |
michael@0 | 39 | return nullptr; |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | NewObjectKind newKind = options.singleton() ? SingletonObject : GenericObject; |
michael@0 | 43 | gc::AllocKind allocKind = gc::GetGCObjectKind(clasp); |
michael@0 | 44 | if (handler->finalizeInBackground(priv)) |
michael@0 | 45 | allocKind = GetBackgroundAllocKind(allocKind); |
michael@0 | 46 | RootedObject obj(cx, NewObjectWithGivenProto(cx, clasp, proto, parent, allocKind, newKind)); |
michael@0 | 47 | if (!obj) |
michael@0 | 48 | return nullptr; |
michael@0 | 49 | |
michael@0 | 50 | Rooted<ProxyObject*> proxy(cx, &obj->as<ProxyObject>()); |
michael@0 | 51 | proxy->initHandler(handler); |
michael@0 | 52 | proxy->initCrossCompartmentPrivate(priv); |
michael@0 | 53 | |
michael@0 | 54 | /* Don't track types of properties of non-DOM and non-singleton proxies. */ |
michael@0 | 55 | if (newKind != SingletonObject && !clasp->isDOMClass()) |
michael@0 | 56 | MarkTypeObjectUnknownProperties(cx, proxy->type()); |
michael@0 | 57 | |
michael@0 | 58 | return proxy; |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | void |
michael@0 | 62 | ProxyObject::initCrossCompartmentPrivate(HandleValue priv) |
michael@0 | 63 | { |
michael@0 | 64 | initCrossCompartmentSlot(PRIVATE_SLOT, priv); |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | void |
michael@0 | 68 | ProxyObject::initHandler(BaseProxyHandler *handler) |
michael@0 | 69 | { |
michael@0 | 70 | initSlot(HANDLER_SLOT, PrivateValue(handler)); |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | static void |
michael@0 | 74 | NukeSlot(ProxyObject *proxy, uint32_t slot) |
michael@0 | 75 | { |
michael@0 | 76 | Value old = proxy->getSlot(slot); |
michael@0 | 77 | if (old.isMarkable()) { |
michael@0 | 78 | Zone *zone = ZoneOfValue(old); |
michael@0 | 79 | AutoMarkInDeadZone amd(zone); |
michael@0 | 80 | proxy->setReservedSlot(slot, NullValue()); |
michael@0 | 81 | } else { |
michael@0 | 82 | proxy->setReservedSlot(slot, NullValue()); |
michael@0 | 83 | } |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | void |
michael@0 | 87 | ProxyObject::nuke(BaseProxyHandler *handler) |
michael@0 | 88 | { |
michael@0 | 89 | /* Allow people to add their own number of reserved slots beyond the expected 4 */ |
michael@0 | 90 | unsigned numSlots = JSCLASS_RESERVED_SLOTS(getClass()); |
michael@0 | 91 | for (unsigned i = 0; i < numSlots; i++) |
michael@0 | 92 | NukeSlot(this, i); |
michael@0 | 93 | /* Restore the handler as requested after nuking. */ |
michael@0 | 94 | setHandler(handler); |
michael@0 | 95 | } |