DisposableStackObjectBase.h (2597B)
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 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef builtin_DisposableStackObjectBase_h 8 #define builtin_DisposableStackObjectBase_h 9 10 #include "vm/JSObject.h" 11 #include "vm/NativeObject.h" 12 #include "vm/UsingHint.h" 13 14 namespace js { 15 16 enum AdoptClosureSlots { 17 AdoptClosureSlot_ValueSlot = 0, 18 AdoptClosureSlot_OnDisposeSlot, 19 }; 20 21 bool ThrowIfOnDisposeNotCallable(JSContext* cx, 22 JS::Handle<JS::Value> onDispose); 23 24 bool AdoptClosure(JSContext* cx, unsigned argc, JS::Value* vp); 25 26 bool AddDisposableResource(JSContext* cx, 27 JS::Handle<ArrayObject*> disposeCapability, 28 JS::Handle<JS::Value> val, UsingHint hint); 29 30 bool AddDisposableResource(JSContext* cx, 31 JS::Handle<ArrayObject*> disposeCapability, 32 JS::Handle<JS::Value> val, UsingHint hint, 33 JS::Handle<JS::Value> methodVal); 34 35 bool CreateDisposableResource(JSContext* cx, JS::Handle<JS::Value> objVal, 36 UsingHint hint, 37 JS::MutableHandle<JS::Value> result); 38 39 bool CreateDisposableResource(JSContext* cx, JS::Handle<JS::Value> objVal, 40 UsingHint hint, JS::Handle<JS::Value> methodVal, 41 JS::MutableHandle<JS::Value> result); 42 43 bool GetDisposeMethod(JSContext* cx, JS::Handle<JS::Value> obj, UsingHint hint, 44 JS::MutableHandle<JS::Value> disposeMethod); 45 46 // This is a shared base class for common functionality between 47 // DisposableStackObject and AsyncDisposableStackObject. 48 class DisposableStackObjectBase : public NativeObject { 49 public: 50 enum DisposableState : uint8_t { Pending, Disposed }; 51 52 static constexpr uint32_t DISPOSABLE_RESOURCE_STACK_SLOT = 0; 53 static constexpr uint32_t STATE_SLOT = 1; 54 static constexpr uint32_t RESERVED_SLOTS = 2; 55 56 protected: 57 static ArrayObject* GetOrCreateDisposeCapability( 58 JSContext* cx, JS::Handle<DisposableStackObjectBase*> obj); 59 60 bool isDisposableResourceStackEmpty() const; 61 void clearDisposableResourceStack(); 62 ArrayObject* nonEmptyDisposableResourceStack() const; 63 DisposableState state() const; 64 void setState(DisposableState state); 65 }; 66 67 } /* namespace js */ 68 69 #endif /* builtin_DisposableStackObjectBase_h */