1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/jsapi-tests/testSetPropertyIgnoringNamedGetter.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,92 @@ 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 + */ 1.7 + 1.8 +#include "jsfriendapi.h" 1.9 +#include "jsproxy.h" 1.10 + 1.11 +#include "jsapi-tests/tests.h" 1.12 + 1.13 +using namespace js; 1.14 +using namespace JS; 1.15 + 1.16 +class CustomProxyHandler : public DirectProxyHandler { 1.17 + public: 1.18 + CustomProxyHandler() : DirectProxyHandler(nullptr) {} 1.19 + 1.20 + bool getPropertyDescriptor(JSContext *cx, HandleObject proxy, HandleId id, 1.21 + MutableHandle<JSPropertyDescriptor> desc) MOZ_OVERRIDE 1.22 + { 1.23 + return impl(cx, proxy, id, desc, false); 1.24 + } 1.25 + 1.26 + bool getOwnPropertyDescriptor(JSContext *cx, HandleObject proxy, HandleId id, 1.27 + MutableHandle<JSPropertyDescriptor> desc) MOZ_OVERRIDE 1.28 + { 1.29 + return impl(cx, proxy, id, desc, true); 1.30 + } 1.31 + 1.32 + bool set(JSContext *cx, HandleObject proxy, HandleObject receiver, 1.33 + HandleId id, bool strict, MutableHandleValue vp) MOZ_OVERRIDE 1.34 + { 1.35 + Rooted<JSPropertyDescriptor> desc(cx); 1.36 + if (!DirectProxyHandler::getPropertyDescriptor(cx, proxy, id, &desc)) 1.37 + return false; 1.38 + return SetPropertyIgnoringNamedGetter(cx, this, proxy, receiver, id, &desc, 1.39 + desc.object() == proxy, strict, vp); 1.40 + } 1.41 + 1.42 + private: 1.43 + bool impl(JSContext *cx, HandleObject proxy, HandleId id, 1.44 + MutableHandle<JSPropertyDescriptor> desc, bool ownOnly) 1.45 + { 1.46 + if (JSID_IS_STRING(id)) { 1.47 + bool match; 1.48 + if (!JS_StringEqualsAscii(cx, JSID_TO_STRING(id), "phantom", &match)) 1.49 + return false; 1.50 + if (match) { 1.51 + desc.object().set(proxy); 1.52 + desc.attributesRef() = JSPROP_READONLY | JSPROP_ENUMERATE; 1.53 + desc.value().setInt32(42); 1.54 + return true; 1.55 + } 1.56 + } 1.57 + 1.58 + if (ownOnly) 1.59 + return DirectProxyHandler::getOwnPropertyDescriptor(cx, proxy, id, desc); 1.60 + return DirectProxyHandler::getPropertyDescriptor(cx, proxy, id, desc); 1.61 + } 1.62 + 1.63 +}; 1.64 + 1.65 +CustomProxyHandler customProxyHandler; 1.66 + 1.67 + 1.68 +BEGIN_TEST(testSetPropertyIgnoringNamedGetter_direct) 1.69 +{ 1.70 + RootedValue protov(cx); 1.71 + EVAL("Object.prototype", &protov); 1.72 + 1.73 + RootedValue targetv(cx); 1.74 + EVAL("({})", &targetv); 1.75 + 1.76 + RootedObject proxyObj(cx, NewProxyObject(cx, &customProxyHandler, targetv, 1.77 + &protov.toObject(), global, ProxyOptions())); 1.78 + CHECK(proxyObj); 1.79 + 1.80 + CHECK(JS_DefineProperty(cx, global, "target", targetv, 0)); 1.81 + CHECK(JS_DefineProperty(cx, global, "proxy", proxyObj, 0)); 1.82 + 1.83 + RootedValue v(cx); 1.84 + EVAL("Object.getOwnPropertyDescriptor(proxy, 'phantom').value", &v); 1.85 + CHECK_SAME(v, Int32Value(42)); 1.86 + 1.87 + EXEC("proxy.phantom = 123"); 1.88 + EVAL("Object.getOwnPropertyDescriptor(proxy, 'phantom').value", &v); 1.89 + CHECK_SAME(v, Int32Value(42)); 1.90 + EVAL("target.phantom", &v); 1.91 + CHECK_SAME(v, Int32Value(123)); 1.92 + 1.93 + return true; 1.94 +} 1.95 +END_TEST(testSetPropertyIgnoringNamedGetter_direct)