js/src/jsapi-tests/testSetPropertyIgnoringNamedGetter.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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 */
michael@0 4
michael@0 5 #include "jsfriendapi.h"
michael@0 6 #include "jsproxy.h"
michael@0 7
michael@0 8 #include "jsapi-tests/tests.h"
michael@0 9
michael@0 10 using namespace js;
michael@0 11 using namespace JS;
michael@0 12
michael@0 13 class CustomProxyHandler : public DirectProxyHandler {
michael@0 14 public:
michael@0 15 CustomProxyHandler() : DirectProxyHandler(nullptr) {}
michael@0 16
michael@0 17 bool getPropertyDescriptor(JSContext *cx, HandleObject proxy, HandleId id,
michael@0 18 MutableHandle<JSPropertyDescriptor> desc) MOZ_OVERRIDE
michael@0 19 {
michael@0 20 return impl(cx, proxy, id, desc, false);
michael@0 21 }
michael@0 22
michael@0 23 bool getOwnPropertyDescriptor(JSContext *cx, HandleObject proxy, HandleId id,
michael@0 24 MutableHandle<JSPropertyDescriptor> desc) MOZ_OVERRIDE
michael@0 25 {
michael@0 26 return impl(cx, proxy, id, desc, true);
michael@0 27 }
michael@0 28
michael@0 29 bool set(JSContext *cx, HandleObject proxy, HandleObject receiver,
michael@0 30 HandleId id, bool strict, MutableHandleValue vp) MOZ_OVERRIDE
michael@0 31 {
michael@0 32 Rooted<JSPropertyDescriptor> desc(cx);
michael@0 33 if (!DirectProxyHandler::getPropertyDescriptor(cx, proxy, id, &desc))
michael@0 34 return false;
michael@0 35 return SetPropertyIgnoringNamedGetter(cx, this, proxy, receiver, id, &desc,
michael@0 36 desc.object() == proxy, strict, vp);
michael@0 37 }
michael@0 38
michael@0 39 private:
michael@0 40 bool impl(JSContext *cx, HandleObject proxy, HandleId id,
michael@0 41 MutableHandle<JSPropertyDescriptor> desc, bool ownOnly)
michael@0 42 {
michael@0 43 if (JSID_IS_STRING(id)) {
michael@0 44 bool match;
michael@0 45 if (!JS_StringEqualsAscii(cx, JSID_TO_STRING(id), "phantom", &match))
michael@0 46 return false;
michael@0 47 if (match) {
michael@0 48 desc.object().set(proxy);
michael@0 49 desc.attributesRef() = JSPROP_READONLY | JSPROP_ENUMERATE;
michael@0 50 desc.value().setInt32(42);
michael@0 51 return true;
michael@0 52 }
michael@0 53 }
michael@0 54
michael@0 55 if (ownOnly)
michael@0 56 return DirectProxyHandler::getOwnPropertyDescriptor(cx, proxy, id, desc);
michael@0 57 return DirectProxyHandler::getPropertyDescriptor(cx, proxy, id, desc);
michael@0 58 }
michael@0 59
michael@0 60 };
michael@0 61
michael@0 62 CustomProxyHandler customProxyHandler;
michael@0 63
michael@0 64
michael@0 65 BEGIN_TEST(testSetPropertyIgnoringNamedGetter_direct)
michael@0 66 {
michael@0 67 RootedValue protov(cx);
michael@0 68 EVAL("Object.prototype", &protov);
michael@0 69
michael@0 70 RootedValue targetv(cx);
michael@0 71 EVAL("({})", &targetv);
michael@0 72
michael@0 73 RootedObject proxyObj(cx, NewProxyObject(cx, &customProxyHandler, targetv,
michael@0 74 &protov.toObject(), global, ProxyOptions()));
michael@0 75 CHECK(proxyObj);
michael@0 76
michael@0 77 CHECK(JS_DefineProperty(cx, global, "target", targetv, 0));
michael@0 78 CHECK(JS_DefineProperty(cx, global, "proxy", proxyObj, 0));
michael@0 79
michael@0 80 RootedValue v(cx);
michael@0 81 EVAL("Object.getOwnPropertyDescriptor(proxy, 'phantom').value", &v);
michael@0 82 CHECK_SAME(v, Int32Value(42));
michael@0 83
michael@0 84 EXEC("proxy.phantom = 123");
michael@0 85 EVAL("Object.getOwnPropertyDescriptor(proxy, 'phantom').value", &v);
michael@0 86 CHECK_SAME(v, Int32Value(42));
michael@0 87 EVAL("target.phantom", &v);
michael@0 88 CHECK_SAME(v, Int32Value(123));
michael@0 89
michael@0 90 return true;
michael@0 91 }
michael@0 92 END_TEST(testSetPropertyIgnoringNamedGetter_direct)

mercurial