/* * Copyright (C) 2016 Charybdis Development Team * Copyright (C) 2016 Jason Volk * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice is present in all copies. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #pragma once #define HAVE_IRCD_JS_ROOT_H namespace ircd { namespace js { enum class lifetime { stack, // entity lives on the stack and the GC uses a cheap forward list between objects heap, // entity lives on the heap with dynamic duration tenured, // long-life-optimized heap entity with special rules (must read jsapi docs) persist, // entity has duration similar to the runtime itself maybe, // template with a boolean switch for GC participation fake, // noop; does not register with GC at all }; template struct root { }; template using handle = typename T::handle; template struct root :JS::Rooted { using base_type = JS::Rooted; using type = root; using handle_mutable = JS::MutableHandle; using handle = JS::Handle; root(const handle &h) :JS::Rooted{*cx, h} { } root(const handle_mutable &h) :JS::Rooted{*cx, h} { } template root(const root &other) :JS::Rooted{*cx, other.get()} { } explicit root(const T &t) :JS::Rooted{*cx, t} { } root() :JS::Rooted{*cx} { } root(root&& other) noexcept :JS::Rooted{*cx, other.get()} { } root &operator=(root &&other) noexcept { this->set(other.get()); return *this; } root(const root &) = delete; root &operator=(const root &) = delete; }; // This conversion is missing in the jsapi. Is this object not gc rooted? Can it not have a handle? template JS::Handle operator&(const JS::Heap &h) { return JS::Handle::fromMarkedLocation(h.address()); } // This conversion is missing in the jsapi. Is this object not gc rooted? Can it not have a handle? template JS::MutableHandle operator&(JS::Heap &h) { const auto ptr(const_cast(h.address())); return JS::MutableHandle::fromMarkedLocation(ptr); } template struct root :JS::Heap { using type = root; using handle = JS::Handle; using handle_mutable = JS::MutableHandle; private: tracing::list::iterator tracing_it; tracing::list::iterator add(JS::Heap *const &ptr) { return rt->tracing.heap.emplace(end(rt->tracing.heap), tracing::thing { ptr, js::type() }); } void del(const tracing::list::iterator &tracing_it) { if(tracing_it != end(rt->tracing.heap)) { const void *ptr = tracing_it->ptr; rt->tracing.heap.erase(tracing_it); } } public: operator handle() const { return JS::Handle::fromMarkedLocation(this->address()); } operator handle_mutable() { const auto ptr(const_cast(this->address())); return JS::MutableHandle::fromMarkedLocation(ptr); } root(const handle &h) :JS::Heap{h} ,tracing_it{add(this)} { } root(const handle_mutable &h) :JS::Heap{h} ,tracing_it{add(this)} { } template root(const root &other) :JS::Heap{other} ,tracing_it{add(this)} { } explicit root(const T &t) :JS::Heap{t} ,tracing_it{add(this)} { } root() :JS::Heap{} ,tracing_it{add(this)} { } root(root &&other) noexcept :JS::Heap{other.get()} ,tracing_it{std::move(other.tracing_it)} { other.tracing_it = end(rt->tracing.heap); tracing_it->ptr = static_cast *>(this); } root(const root &other) :JS::Heap{other} ,tracing_it{add(this)} { } root &operator=(root &&other) noexcept { JS::Heap::operator=(std::move(other)); return *this; } root &operator=(const root &other) { JS::Heap::operator=(other); return *this; } ~root() noexcept { del(tracing_it); } }; // This conversion is missing in the jsapi. Is this object not gc rooted? Can it not have a handle? template JS::Handle operator&(const JS::TenuredHeap &h) { return JS::Handle::fromMarkedLocation(h.address()); } // This conversion is missing in the jsapi. Is this object not gc rooted? Can it not have a handle? template JS::MutableHandle operator&(JS::TenuredHeap &h) { const auto ptr(const_cast(h.address())); return JS::MutableHandle::fromMarkedLocation(ptr); } template struct root :JS::TenuredHeap { using type = root; using handle_mutable = JS::MutableHandle; using handle = JS::Handle; operator handle() const { return JS::Handle::fromMarkedLocation(this->address()); } operator handle_mutable() { const auto ptr(const_cast(this->address())); return JS::MutableHandle::fromMarkedLocation(ptr); } auto operator&() const { return static_cast(*this); } auto operator&() { return static_cast(*this); } root(const handle &h) :JS::TenuredHeap{h} { } root(const handle_mutable &h) :JS::TenuredHeap{h} { } template root(const root &other) :JS::TenuredHeap{other.get()} { } explicit root(const T &t) :JS::TenuredHeap{t} { } root() :JS::TenuredHeap{} { } root(root&&) = default; root(const root &) = default; root &operator=(root &&) = default; root &operator=(const root &) = default; }; template struct root :JS::PersistentRooted { using type = root; using handle_mutable = JS::MutableHandle; using handle = JS::Handle; template root(const root &other) :JS::PersistentRooted{*cx, other.get()} { } explicit root(const T &t) :JS::PersistentRooted{*cx, t} { } root() :JS::PersistentRooted{*cx} { } root(root&& other) noexcept :JS::PersistentRooted{*cx, other.get()} { } root(const root &) = delete; root &operator=(const root &) = delete; }; template struct root :public ::js::MaybeRooted { using type = root; }; template struct root :public ::js::FakeRooted { using type = root; }; } // namespace js } // namespace ircd