DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (d8847129d134)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * vim: set ts=8 sts=4 et sw=4 tw=99:
 */

#include "jsfriendapi.h"
#include "jsproxy.h"

#include "jsapi-tests/tests.h"

using namespace js;
using namespace JS;

class CustomProxyHandler : public DirectProxyHandler {
  public:
    CustomProxyHandler() : DirectProxyHandler(nullptr) {}

    bool getPropertyDescriptor(JSContext* cx, HandleObject proxy, HandleId id,
                               MutableHandle<JSPropertyDescriptor> desc) const MOZ_OVERRIDE
    {
        return impl(cx, proxy, id, desc, false);
    }

    bool getOwnPropertyDescriptor(JSContext* cx, HandleObject proxy, HandleId id,
                                  MutableHandle<JSPropertyDescriptor> desc) const MOZ_OVERRIDE
    {
        return impl(cx, proxy, id, desc, true);
    }

    bool set(JSContext* cx, HandleObject proxy, HandleObject receiver,
             HandleId id, bool strict, MutableHandleValue vp) const MOZ_OVERRIDE
    {
        Rooted<JSPropertyDescriptor> desc(cx);
        if (!DirectProxyHandler::getPropertyDescriptor(cx, proxy, id, &desc))
            return false;
        return SetPropertyIgnoringNamedGetter(cx, this, proxy, receiver, id, &desc,
                                              desc.object() == proxy, strict, vp);
    }

  private:
    bool impl(JSContext* cx, HandleObject proxy, HandleId id,
              MutableHandle<JSPropertyDescriptor> desc, bool ownOnly) const
    {
        if (JSID_IS_STRING(id)) {
            bool match;
            if (!JS_StringEqualsAscii(cx, JSID_TO_STRING(id), "phantom", &match))
                return false;
            if (match) {
                desc.object().set(proxy);
                desc.attributesRef() = JSPROP_READONLY | JSPROP_ENUMERATE;
                desc.value().setInt32(42);
                return true;
            }
        }

        if (ownOnly)
            return DirectProxyHandler::getOwnPropertyDescriptor(cx, proxy, id, desc);
        return DirectProxyHandler::getPropertyDescriptor(cx, proxy, id, desc);
    }

};

const CustomProxyHandler customProxyHandler;


BEGIN_TEST(testSetPropertyIgnoringNamedGetter_direct)
{
    RootedValue protov(cx);
    EVAL("Object.prototype", &protov);

    RootedValue targetv(cx);
    EVAL("({})", &targetv);

    RootedObject proxyObj(cx, NewProxyObject(cx, &customProxyHandler, targetv,
                                             &protov.toObject(), global, ProxyOptions()));
    CHECK(proxyObj);

    CHECK(JS_DefineProperty(cx, global, "target", targetv, 0));
    CHECK(JS_DefineProperty(cx, global, "proxy", proxyObj, 0));

    RootedValue v(cx);
    EVAL("Object.getOwnPropertyDescriptor(proxy, 'phantom').value", &v);
    CHECK_SAME(v, Int32Value(42));

    EXEC("proxy.phantom = 123");
    EVAL("Object.getOwnPropertyDescriptor(proxy, 'phantom').value", &v);
    CHECK_SAME(v, Int32Value(42));
    EVAL("target.phantom", &v);
    CHECK_SAME(v, Int32Value(123));

    return true;
}
END_TEST(testSetPropertyIgnoringNamedGetter_direct)