mirror of
https://github.com/matrix-construct/construct
synced 2025-01-13 08:23:56 +01:00
ircd::js: Improve private slot access.
This commit is contained in:
parent
7a99d919d6
commit
c3e6c52192
4 changed files with 110 additions and 37 deletions
|
@ -47,11 +47,12 @@ namespace js { struct InterpreterFrame; }
|
|||
namespace ircd {
|
||||
namespace js {
|
||||
|
||||
// Extend this class to store your data with any priv(),
|
||||
// i.e priv(runtime, privdata*) or priv(context, privdata*) etc
|
||||
// Extend this class to store your data with any priv(), i.e priv(runtime, privdata*) or
|
||||
// priv(context, privdata*). Also useful with reserved slots and private object variables.
|
||||
struct privdata
|
||||
:std::enable_shared_from_this<privdata>
|
||||
{
|
||||
virtual ~privdata() noexcept = 0; // Your object is managed by the host
|
||||
virtual ~privdata() noexcept; // Your object is managed by the host
|
||||
};
|
||||
|
||||
inline const char *version(const JSVersion &v) { return JS_VersionToString(v); }
|
||||
|
|
|
@ -35,16 +35,26 @@ bool is_extensible(const object_handle &);
|
|||
bool is_array(const object_handle &);
|
||||
uint32_t size(const object_handle &);
|
||||
|
||||
// Private data slot (trap must have flag JSCLASS_HAS_PRIVATE)
|
||||
template<class T> T &priv(const object_handle &);
|
||||
void priv(const object_handle &, void *const &);
|
||||
void priv(const object_handle &, const void *const &);
|
||||
|
||||
bool deep_freeze(const object_handle &);
|
||||
bool freeze(const object_handle &);
|
||||
|
||||
IRCD_STRONG_TYPEDEF(uint, reserved)
|
||||
|
||||
// Get private data slot (trap must have flag JSCLASS_HAS_PRIVATE)
|
||||
template<class T> const T *priv(const JSObject &);
|
||||
template<class T> T *priv(JSObject &);
|
||||
template<class T> T &priv(const object_handle &);
|
||||
|
||||
// Set private data slot (trap must have flag JSCLASS_HAS_PRIVATE)
|
||||
void priv(JSObject &, nullptr_t);
|
||||
void priv(JSObject &, std::shared_ptr<privdata>);
|
||||
|
||||
// Set private data slot (morphisms: object -> JS::HandleObject -> JSObject *)
|
||||
void priv(JSObject *const &, nullptr_t);
|
||||
void priv(JSObject *const &, std::shared_ptr<privdata>);
|
||||
|
||||
template<class T, class... args> std::shared_ptr<privdata> make_priv(args&&...);
|
||||
|
||||
namespace basic {
|
||||
|
||||
template<lifetime L>
|
||||
|
@ -260,6 +270,81 @@ const
|
|||
|
||||
} // namespace basic
|
||||
|
||||
template<class T,
|
||||
class... args>
|
||||
std::shared_ptr<privdata>
|
||||
make_priv(args&&... a)
|
||||
{
|
||||
return std::make_shared<T>(std::forward<args>(a)...);
|
||||
}
|
||||
|
||||
inline void
|
||||
priv(JSObject *const &obj,
|
||||
std::shared_ptr<privdata> ptr)
|
||||
{
|
||||
assert(obj);
|
||||
priv(*obj, ptr);
|
||||
}
|
||||
|
||||
inline void
|
||||
priv(JSObject *const &obj,
|
||||
nullptr_t)
|
||||
{
|
||||
assert(obj);
|
||||
priv(*obj, nullptr);
|
||||
}
|
||||
|
||||
inline void
|
||||
priv(JSObject &obj,
|
||||
std::shared_ptr<privdata> ptr)
|
||||
{
|
||||
JS_SetPrivate(&obj, new std::shared_ptr<privdata>(ptr));
|
||||
}
|
||||
|
||||
inline void
|
||||
priv(JSObject &obj,
|
||||
nullptr_t)
|
||||
{
|
||||
void *const vp(JS_GetPrivate(&obj));
|
||||
auto *const sp(reinterpret_cast<std::shared_ptr<privdata> *>(vp));
|
||||
delete sp;
|
||||
JS_SetPrivate(&obj, nullptr);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
T *
|
||||
priv(JSObject &obj)
|
||||
{
|
||||
void *const vp(JS_GetPrivate(&obj));
|
||||
auto *const sp(reinterpret_cast<std::shared_ptr<privdata> *>(vp));
|
||||
return sp? reinterpret_cast<T *>(sp->get()) : nullptr;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
const T *
|
||||
priv(const JSObject &obj)
|
||||
{
|
||||
void *const vp(JS_GetPrivate(const_cast<JSObject *>(&obj)));
|
||||
auto *const sp(reinterpret_cast<std::shared_ptr<privdata> *>(vp));
|
||||
return sp? reinterpret_cast<T *>(sp->get()) : nullptr;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
T &
|
||||
priv(const object_handle &obj)
|
||||
{
|
||||
const auto &jsc(jsclass(obj));
|
||||
const auto vp(JS_GetInstancePrivate(*cx, obj, &jsc, nullptr));
|
||||
if(!vp)
|
||||
throw error("Object has no private data");
|
||||
|
||||
auto *const sp(reinterpret_cast<std::shared_ptr<privdata> *>(vp));
|
||||
if(!*sp)
|
||||
throw error("Object has no private data");
|
||||
|
||||
return *reinterpret_cast<T *>(sp->get());
|
||||
}
|
||||
|
||||
inline bool
|
||||
freeze(const object_handle &obj)
|
||||
{
|
||||
|
@ -302,32 +387,6 @@ is_extensible(const object_handle &obj)
|
|||
return ret;
|
||||
}
|
||||
|
||||
inline void
|
||||
priv(const object_handle &obj,
|
||||
const void *const &ptr)
|
||||
{
|
||||
JS_SetPrivate(obj, const_cast<void *>(ptr));
|
||||
}
|
||||
|
||||
inline void
|
||||
priv(const object_handle &obj,
|
||||
void *const &ptr)
|
||||
{
|
||||
JS_SetPrivate(obj, ptr);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
T &
|
||||
priv(const object_handle &obj)
|
||||
{
|
||||
const auto &jsc(jsclass(obj));
|
||||
const auto ret(JS_GetInstancePrivate(*cx, obj, &jsc, nullptr));
|
||||
if(!ret)
|
||||
throw error("Object has no private data");
|
||||
|
||||
return *reinterpret_cast<T *>(ret);
|
||||
}
|
||||
|
||||
inline const JSClass &
|
||||
jsclass(const object_handle &obj)
|
||||
{
|
||||
|
|
|
@ -64,7 +64,7 @@ task::get()
|
|||
inline task &
|
||||
task::get(const object &global)
|
||||
{
|
||||
return priv<task>(global);
|
||||
return *reinterpret_cast<task *>(JS_GetPrivate(global));
|
||||
}
|
||||
|
||||
} // namespace js
|
||||
|
|
17
ircd/js.cc
17
ircd/js.cc
|
@ -117,6 +117,12 @@ noexcept
|
|||
JS_ShutDown();
|
||||
}
|
||||
|
||||
// Plant down the location of the struct privdata vtable here
|
||||
ircd::js::privdata::~privdata()
|
||||
noexcept
|
||||
{
|
||||
}
|
||||
|
||||
const char *
|
||||
ircd::js::version(const ver &type)
|
||||
{
|
||||
|
@ -222,7 +228,7 @@ try
|
|||
// `struct task` from the global object using task::get(object). The global object
|
||||
// can first be found from a context or active compartment. As a convenience `struct task`
|
||||
// can be found contextually with task::get(void).
|
||||
priv(global, this);
|
||||
JS_SetPrivate(global, this);
|
||||
|
||||
return global;
|
||||
}()}
|
||||
|
@ -1049,7 +1055,14 @@ const
|
|||
}
|
||||
|
||||
void
|
||||
ircd::js::trap::on_gc(JSObject &)
|
||||
ircd::js::trap::on_gc(JSObject &that)
|
||||
{
|
||||
if(jsclass().flags & JSCLASS_HAS_PRIVATE)
|
||||
priv(that, nullptr);
|
||||
}
|
||||
|
||||
void
|
||||
ircd::js::trap::on_trace(const JSObject &)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue