js/src/vm/ProxyObject.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/vm/ProxyObject.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,95 @@
     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 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "vm/ProxyObject.h"
    1.11 +
    1.12 +#include "jscompartment.h"
    1.13 +#include "jsgcinlines.h"
    1.14 +#include "jsinferinlines.h"
    1.15 +#include "jsobjinlines.h"
    1.16 +
    1.17 +using namespace js;
    1.18 +
    1.19 +/* static */ ProxyObject *
    1.20 +ProxyObject::New(JSContext *cx, BaseProxyHandler *handler, HandleValue priv, TaggedProto proto_,
    1.21 +                 JSObject *parent_, const ProxyOptions &options)
    1.22 +{
    1.23 +    Rooted<TaggedProto> proto(cx, proto_);
    1.24 +    RootedObject parent(cx, parent_);
    1.25 +
    1.26 +    const Class *clasp = options.clasp();
    1.27 +
    1.28 +    JS_ASSERT(isValidProxyClass(clasp));
    1.29 +    JS_ASSERT_IF(proto.isObject(), cx->compartment() == proto.toObject()->compartment());
    1.30 +    JS_ASSERT_IF(parent, cx->compartment() == parent->compartment());
    1.31 +
    1.32 +    /*
    1.33 +     * Eagerly mark properties unknown for proxies, so we don't try to track
    1.34 +     * their properties and so that we don't need to walk the compartment if
    1.35 +     * their prototype changes later.  But don't do this for DOM proxies,
    1.36 +     * because we want to be able to keep track of them in typesets in useful
    1.37 +     * ways.
    1.38 +     */
    1.39 +    if (proto.isObject() && !options.singleton() && !clasp->isDOMClass()) {
    1.40 +        RootedObject protoObj(cx, proto.toObject());
    1.41 +        if (!JSObject::setNewTypeUnknown(cx, clasp, protoObj))
    1.42 +            return nullptr;
    1.43 +    }
    1.44 +
    1.45 +    NewObjectKind newKind = options.singleton() ? SingletonObject : GenericObject;
    1.46 +    gc::AllocKind allocKind = gc::GetGCObjectKind(clasp);
    1.47 +    if (handler->finalizeInBackground(priv))
    1.48 +        allocKind = GetBackgroundAllocKind(allocKind);
    1.49 +    RootedObject obj(cx, NewObjectWithGivenProto(cx, clasp, proto, parent, allocKind, newKind));
    1.50 +    if (!obj)
    1.51 +        return nullptr;
    1.52 +
    1.53 +    Rooted<ProxyObject*> proxy(cx, &obj->as<ProxyObject>());
    1.54 +    proxy->initHandler(handler);
    1.55 +    proxy->initCrossCompartmentPrivate(priv);
    1.56 +
    1.57 +    /* Don't track types of properties of non-DOM and non-singleton proxies. */
    1.58 +    if (newKind != SingletonObject && !clasp->isDOMClass())
    1.59 +        MarkTypeObjectUnknownProperties(cx, proxy->type());
    1.60 +
    1.61 +    return proxy;
    1.62 +}
    1.63 +
    1.64 +void
    1.65 +ProxyObject::initCrossCompartmentPrivate(HandleValue priv)
    1.66 +{
    1.67 +    initCrossCompartmentSlot(PRIVATE_SLOT, priv);
    1.68 +}
    1.69 +
    1.70 +void
    1.71 +ProxyObject::initHandler(BaseProxyHandler *handler)
    1.72 +{
    1.73 +    initSlot(HANDLER_SLOT, PrivateValue(handler));
    1.74 +}
    1.75 +
    1.76 +static void
    1.77 +NukeSlot(ProxyObject *proxy, uint32_t slot)
    1.78 +{
    1.79 +    Value old = proxy->getSlot(slot);
    1.80 +    if (old.isMarkable()) {
    1.81 +        Zone *zone = ZoneOfValue(old);
    1.82 +        AutoMarkInDeadZone amd(zone);
    1.83 +        proxy->setReservedSlot(slot, NullValue());
    1.84 +    } else {
    1.85 +        proxy->setReservedSlot(slot, NullValue());
    1.86 +    }
    1.87 +}
    1.88 +
    1.89 +void
    1.90 +ProxyObject::nuke(BaseProxyHandler *handler)
    1.91 +{
    1.92 +    /* Allow people to add their own number of reserved slots beyond the expected 4 */
    1.93 +    unsigned numSlots = JSCLASS_RESERVED_SLOTS(getClass());
    1.94 +    for (unsigned i = 0; i < numSlots; i++)
    1.95 +        NukeSlot(this, i);
    1.96 +    /* Restore the handler as requested after nuking. */
    1.97 +    setHandler(handler);
    1.98 +}

mercurial