tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

testSetPropertyIgnoringNamedGetter.cpp (2317B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
      2 * vim: set ts=8 sts=2 et sw=2 tw=80:
      3 */
      4 
      5 #include "jsfriendapi.h"
      6 
      7 #include "js/PropertyAndElement.h"  // JS_DefineProperty
      8 #include "js/Proxy.h"
      9 
     10 #include "jsapi-tests/tests.h"
     11 
     12 using namespace js;
     13 using namespace JS;
     14 
     15 class CustomProxyHandler : public Wrapper {
     16 public:
     17  CustomProxyHandler() : Wrapper(0) {}
     18 
     19  bool getOwnPropertyDescriptor(
     20      JSContext* cx, HandleObject proxy, HandleId id,
     21      MutableHandle<mozilla::Maybe<PropertyDescriptor>> desc) const override {
     22    if (id.isString() &&
     23        JS_LinearStringEqualsLiteral(id.toLinearString(), "phantom")) {
     24      desc.set(mozilla::Some(PropertyDescriptor::Data(
     25          Int32Value(42),
     26          {PropertyAttribute::Configurable, PropertyAttribute::Enumerable,
     27           PropertyAttribute::Writable})));
     28      return true;
     29    }
     30 
     31    return Wrapper::getOwnPropertyDescriptor(cx, proxy, id, desc);
     32  }
     33 
     34  bool set(JSContext* cx, HandleObject proxy, HandleId id, HandleValue v,
     35           HandleValue receiver, ObjectOpResult& result) const override {
     36    Rooted<mozilla::Maybe<PropertyDescriptor>> desc(cx);
     37    if (!Wrapper::getOwnPropertyDescriptor(cx, proxy, id, &desc)) {
     38      return false;
     39    }
     40    return SetPropertyIgnoringNamedGetter(cx, proxy, id, v, receiver, desc,
     41                                          result);
     42  }
     43 };
     44 
     45 MOZ_RUNINIT const CustomProxyHandler customProxyHandler;
     46 
     47 BEGIN_TEST(testSetPropertyIgnoringNamedGetter_direct) {
     48  RootedValue protov(cx);
     49  EVAL("Object.prototype", &protov);
     50 
     51  RootedValue targetv(cx);
     52  EVAL("({})", &targetv);
     53 
     54  RootedObject proxyObj(cx, NewProxyObject(cx, &customProxyHandler, targetv,
     55                                           &protov.toObject(), ProxyOptions()));
     56  CHECK(proxyObj);
     57 
     58  CHECK(JS_DefineProperty(cx, global, "target", targetv, 0));
     59  CHECK(JS_DefineProperty(cx, global, "proxy", proxyObj, 0));
     60 
     61  RootedValue v(cx);
     62  EVAL("Object.getOwnPropertyDescriptor(proxy, 'phantom').value", &v);
     63  CHECK_SAME(v, Int32Value(42));
     64 
     65  EXEC("proxy.phantom = 123");
     66  EVAL("Object.getOwnPropertyDescriptor(proxy, 'phantom').value", &v);
     67  CHECK_SAME(v, Int32Value(42));
     68  EVAL("target.phantom", &v);
     69  CHECK_SAME(v, Int32Value(123));
     70 
     71  return true;
     72 }
     73 END_TEST(testSetPropertyIgnoringNamedGetter_direct)