ObservableArrayProxyHandler.h (4706B)
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 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_dom_ObservableArrayProxyHandler_h 8 #define mozilla_dom_ObservableArrayProxyHandler_h 9 10 #include "js/TypeDecls.h" 11 #include "js/Wrapper.h" 12 13 namespace mozilla::dom { 14 15 /** 16 * Proxy handler for observable array exotic object. 17 * 18 * The indexed properties are stored in the backing list object in reserved slot 19 * of the proxy object with special treatment intact. 20 * 21 * The additional properties are stored in the proxy target object. 22 */ 23 24 class ObservableArrayProxyHandler : public js::ForwardingProxyHandler { 25 public: 26 explicit constexpr ObservableArrayProxyHandler() 27 : js::ForwardingProxyHandler(&family) {} 28 29 // Implementations of methods that can be implemented in terms of 30 // other lower-level methods. 31 bool defineProperty(JSContext* aCx, JS::Handle<JSObject*> aProxy, 32 JS::Handle<JS::PropertyKey> aId, 33 JS::Handle<JS::PropertyDescriptor> aDesc, 34 JS::ObjectOpResult& aResult) const override; 35 36 bool delete_(JSContext* aCx, JS::Handle<JSObject*> aProxy, 37 JS::Handle<JS::PropertyKey> aId, 38 JS::ObjectOpResult& aResult) const override; 39 40 bool get(JSContext* aCx, JS::Handle<JSObject*> aProxy, 41 JS::Handle<JS::Value> aReceiver, JS::Handle<JS::PropertyKey> aId, 42 JS::MutableHandle<JS::Value> aVp) const override; 43 44 bool getOwnPropertyDescriptor( 45 JSContext* aCx, JS::Handle<JSObject*> aProxy, 46 JS::Handle<JS::PropertyKey> aId, 47 JS::MutableHandle<Maybe<JS::PropertyDescriptor>> aDesc) const override; 48 49 bool has(JSContext* aCx, JS::Handle<JSObject*> aProxy, 50 JS::Handle<JS::PropertyKey> aId, bool* aBp) const override; 51 52 bool ownPropertyKeys(JSContext* aCx, JS::Handle<JSObject*> aProxy, 53 JS::MutableHandleVector<jsid> aProps) const override; 54 55 bool preventExtensions(JSContext* aCx, JS::Handle<JSObject*> aProxy, 56 JS::ObjectOpResult& aResult) const override; 57 58 bool set(JSContext* aCx, JS::Handle<JSObject*> aProxy, 59 JS::Handle<JS::PropertyKey> aId, JS::Handle<JS::Value> aV, 60 JS::Handle<JS::Value> aReceiver, 61 JS::ObjectOpResult& aResult) const override; 62 63 bool SetLength(JSContext* aCx, JS::Handle<JSObject*> aProxy, 64 uint32_t aLength) const; 65 66 static const char family; 67 68 protected: 69 bool GetBackingListObject( 70 JSContext* aCx, JS::Handle<JSObject*> aProxy, 71 JS::MutableHandle<JSObject*> aBackingListObject) const; 72 73 bool GetBackingListLength(JSContext* aCx, JS::Handle<JSObject*> aProxy, 74 uint32_t* aLength) const; 75 76 bool SetLength(JSContext* aCx, JS::Handle<JSObject*> aProxy, 77 JS::Handle<JSObject*> aBackingList, uint32_t aLength, 78 JS::ObjectOpResult& aResult) const; 79 80 bool SetLength(JSContext* aCx, JS::Handle<JSObject*> aProxy, 81 JS::Handle<JSObject*> aBackingList, 82 JS::Handle<JS::Value> aValue, 83 JS::ObjectOpResult& aResult) const; 84 85 // Hook for subclasses to invoke the setting the indexed value steps which 86 // would invoke DeleteAlgorithm/SetAlgorithm defined and implemented per 87 // interface. Returns false and throw exception on failure. 88 virtual bool SetIndexedValue(JSContext* aCx, JS::Handle<JSObject*> aProxy, 89 JS::Handle<JSObject*> aBackingList, 90 uint32_t aIndex, JS::Handle<JS::Value> aValue, 91 JS::ObjectOpResult& aResult) const = 0; 92 93 // Hook for subclasses to invoke the DeleteAlgorithm defined and implemented 94 // per interface. Returns false and throw exception on failure. 95 virtual bool OnDeleteItem(JSContext* aCx, JS::Handle<JSObject*> aProxy, 96 JS::Handle<JS::Value> aValue, 97 uint32_t aIndex) const = 0; 98 }; 99 100 inline bool IsObservableArrayProxy(JSObject* obj) { 101 return js::IsProxy(obj) && js::GetProxyHandler(obj)->family() == 102 &ObservableArrayProxyHandler::family; 103 } 104 105 inline const ObservableArrayProxyHandler* GetObservableArrayProxyHandler( 106 JSObject* obj) { 107 MOZ_ASSERT(IsObservableArrayProxy(obj)); 108 return static_cast<const ObservableArrayProxyHandler*>( 109 js::GetProxyHandler(obj)); 110 } 111 112 } // namespace mozilla::dom 113 114 #endif /* mozilla_dom_ObservableArrayProxyHandler_h */