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: * 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 "vm/ProxyObject.h" michael@0: michael@0: #include "jscompartment.h" michael@0: #include "jsgcinlines.h" michael@0: #include "jsinferinlines.h" michael@0: #include "jsobjinlines.h" michael@0: michael@0: using namespace js; michael@0: michael@0: /* static */ ProxyObject * michael@0: ProxyObject::New(JSContext *cx, BaseProxyHandler *handler, HandleValue priv, TaggedProto proto_, michael@0: JSObject *parent_, const ProxyOptions &options) michael@0: { michael@0: Rooted proto(cx, proto_); michael@0: RootedObject parent(cx, parent_); michael@0: michael@0: const Class *clasp = options.clasp(); michael@0: michael@0: JS_ASSERT(isValidProxyClass(clasp)); michael@0: JS_ASSERT_IF(proto.isObject(), cx->compartment() == proto.toObject()->compartment()); michael@0: JS_ASSERT_IF(parent, cx->compartment() == parent->compartment()); michael@0: michael@0: /* michael@0: * Eagerly mark properties unknown for proxies, so we don't try to track michael@0: * their properties and so that we don't need to walk the compartment if michael@0: * their prototype changes later. But don't do this for DOM proxies, michael@0: * because we want to be able to keep track of them in typesets in useful michael@0: * ways. michael@0: */ michael@0: if (proto.isObject() && !options.singleton() && !clasp->isDOMClass()) { michael@0: RootedObject protoObj(cx, proto.toObject()); michael@0: if (!JSObject::setNewTypeUnknown(cx, clasp, protoObj)) michael@0: return nullptr; michael@0: } michael@0: michael@0: NewObjectKind newKind = options.singleton() ? SingletonObject : GenericObject; michael@0: gc::AllocKind allocKind = gc::GetGCObjectKind(clasp); michael@0: if (handler->finalizeInBackground(priv)) michael@0: allocKind = GetBackgroundAllocKind(allocKind); michael@0: RootedObject obj(cx, NewObjectWithGivenProto(cx, clasp, proto, parent, allocKind, newKind)); michael@0: if (!obj) michael@0: return nullptr; michael@0: michael@0: Rooted proxy(cx, &obj->as()); michael@0: proxy->initHandler(handler); michael@0: proxy->initCrossCompartmentPrivate(priv); michael@0: michael@0: /* Don't track types of properties of non-DOM and non-singleton proxies. */ michael@0: if (newKind != SingletonObject && !clasp->isDOMClass()) michael@0: MarkTypeObjectUnknownProperties(cx, proxy->type()); michael@0: michael@0: return proxy; michael@0: } michael@0: michael@0: void michael@0: ProxyObject::initCrossCompartmentPrivate(HandleValue priv) michael@0: { michael@0: initCrossCompartmentSlot(PRIVATE_SLOT, priv); michael@0: } michael@0: michael@0: void michael@0: ProxyObject::initHandler(BaseProxyHandler *handler) michael@0: { michael@0: initSlot(HANDLER_SLOT, PrivateValue(handler)); michael@0: } michael@0: michael@0: static void michael@0: NukeSlot(ProxyObject *proxy, uint32_t slot) michael@0: { michael@0: Value old = proxy->getSlot(slot); michael@0: if (old.isMarkable()) { michael@0: Zone *zone = ZoneOfValue(old); michael@0: AutoMarkInDeadZone amd(zone); michael@0: proxy->setReservedSlot(slot, NullValue()); michael@0: } else { michael@0: proxy->setReservedSlot(slot, NullValue()); michael@0: } michael@0: } michael@0: michael@0: void michael@0: ProxyObject::nuke(BaseProxyHandler *handler) michael@0: { michael@0: /* Allow people to add their own number of reserved slots beyond the expected 4 */ michael@0: unsigned numSlots = JSCLASS_RESERVED_SLOTS(getClass()); michael@0: for (unsigned i = 0; i < numSlots; i++) michael@0: NukeSlot(this, i); michael@0: /* Restore the handler as requested after nuking. */ michael@0: setHandler(handler); michael@0: }