2016-10-27 10:55:26 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Charybdis Development Team
|
|
|
|
* Copyright (C) 2016 Jason Volk <jason@zemos.net>
|
|
|
|
*
|
|
|
|
* 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 {
|
|
|
|
|
2016-11-13 02:29:34 +01:00
|
|
|
template<class T>
|
|
|
|
using handle = typename T::handle;
|
|
|
|
|
2016-11-25 03:21:46 +01:00
|
|
|
// This conversion is missing in the jsapi. Is this object not gc rooted? Can it not have a handle?
|
|
|
|
template<class T>
|
|
|
|
JS::Handle<T>
|
|
|
|
operator&(const JS::Heap<T> &h)
|
|
|
|
{
|
|
|
|
return JS::Handle<T>::fromMarkedLocation(h.address());
|
|
|
|
}
|
|
|
|
|
|
|
|
// This conversion is missing in the jsapi. Is this object not gc rooted? Can it not have a handle?
|
|
|
|
template<class T>
|
|
|
|
JS::MutableHandle<T>
|
|
|
|
operator&(JS::Heap<T> &h)
|
|
|
|
{
|
|
|
|
const auto ptr(const_cast<T *>(h.address()));
|
|
|
|
return JS::MutableHandle<T>::fromMarkedLocation(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This conversion is missing in the jsapi. Is this object not gc rooted? Can it not have a handle?
|
|
|
|
template<class T>
|
|
|
|
JS::Handle<T>
|
|
|
|
operator&(const JS::TenuredHeap<T> &h)
|
|
|
|
{
|
|
|
|
return JS::Handle<T>::fromMarkedLocation(h.address());
|
|
|
|
}
|
|
|
|
|
|
|
|
// This conversion is missing in the jsapi. Is this object not gc rooted? Can it not have a handle?
|
|
|
|
template<class T>
|
|
|
|
JS::MutableHandle<T>
|
|
|
|
operator&(JS::TenuredHeap<T> &h)
|
|
|
|
{
|
|
|
|
const auto ptr(const_cast<T *>(h.address()));
|
|
|
|
return JS::MutableHandle<T>::fromMarkedLocation(ptr);
|
|
|
|
}
|
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
template<class T>
|
2016-11-25 09:15:27 +01:00
|
|
|
struct root
|
|
|
|
:JS::Heap<T>
|
2016-10-27 10:55:26 +02:00
|
|
|
{
|
2016-11-25 09:15:27 +01:00
|
|
|
using value_type = T;
|
|
|
|
using base_type = JS::Heap<T>;
|
2016-10-27 10:55:26 +02:00
|
|
|
using handle = JS::Handle<T>;
|
2016-11-25 09:15:27 +01:00
|
|
|
using handle_mutable = JS::MutableHandle<T>;
|
|
|
|
using type = root<value_type>;
|
2016-10-27 10:55:26 +02:00
|
|
|
|
2016-11-25 03:21:46 +01:00
|
|
|
operator handle() const
|
|
|
|
{
|
2016-11-25 09:15:27 +01:00
|
|
|
const auto ptr(this->address());
|
|
|
|
return JS::Handle<T>::fromMarkedLocation(ptr);
|
2016-11-25 03:21:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
operator handle_mutable()
|
|
|
|
{
|
|
|
|
const auto ptr(this->address());
|
|
|
|
return JS::MutableHandle<T>::fromMarkedLocation(ptr);
|
|
|
|
}
|
|
|
|
|
2016-11-13 02:36:03 +01:00
|
|
|
private:
|
|
|
|
tracing::list::iterator tracing_it;
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
tracing::list::iterator add(base_type *const &ptr)
|
2016-11-13 02:36:03 +01:00
|
|
|
{
|
|
|
|
return rt->tracing.heap.emplace(end(rt->tracing.heap), tracing::thing { ptr, js::type<T>() });
|
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
2016-10-27 10:55:26 +02:00
|
|
|
root(const handle &h)
|
2016-11-25 09:15:27 +01:00
|
|
|
:base_type{h.get()}
|
2016-11-13 02:36:03 +01:00
|
|
|
,tracing_it{add(this)}
|
2016-10-27 10:55:26 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
root(const handle_mutable &h)
|
2016-11-25 09:15:27 +01:00
|
|
|
:base_type{h.get()}
|
2016-11-13 02:36:03 +01:00
|
|
|
,tracing_it{add(this)}
|
2016-10-27 10:55:26 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit root(const T &t)
|
2016-11-25 09:15:27 +01:00
|
|
|
:base_type{t}
|
2016-11-13 02:36:03 +01:00
|
|
|
,tracing_it{add(this)}
|
2016-10-27 10:55:26 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
root()
|
2016-11-25 09:15:27 +01:00
|
|
|
:base_type{}
|
2016-11-13 02:36:03 +01:00
|
|
|
,tracing_it{add(this)}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
root(root &&other) noexcept
|
2016-11-25 09:15:27 +01:00
|
|
|
:base_type{other.get()}
|
2016-11-13 02:36:03 +01:00
|
|
|
,tracing_it{std::move(other.tracing_it)}
|
2016-10-27 10:55:26 +02:00
|
|
|
{
|
2016-11-13 02:36:03 +01:00
|
|
|
other.tracing_it = end(rt->tracing.heap);
|
2016-11-25 09:15:27 +01:00
|
|
|
tracing_it->ptr = static_cast<base_type *>(this);
|
2016-10-27 10:55:26 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 02:36:03 +01:00
|
|
|
root(const root &other)
|
2016-11-25 09:15:27 +01:00
|
|
|
:base_type{other.get()}
|
2016-11-13 02:36:03 +01:00
|
|
|
,tracing_it{add(this)}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
root &operator=(root &&other) noexcept
|
|
|
|
{
|
2016-11-25 09:15:27 +01:00
|
|
|
base_type::operator=(std::move(other.get()));
|
2016-11-13 02:36:03 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
root &operator=(const root &other)
|
|
|
|
{
|
2016-11-25 09:15:27 +01:00
|
|
|
base_type::operator=(other.get());
|
2016-11-13 02:36:03 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
~root() noexcept
|
|
|
|
{
|
|
|
|
del(tracing_it);
|
|
|
|
}
|
2016-10-27 10:55:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace js
|
|
|
|
} // namespace ircd
|