0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-07-03 09:18:19 +02:00

ircd::js: Stub runtime/context callback framework.

This commit is contained in:
Jason Volk 2016-10-13 17:24:50 -07:00
parent c1fd6391c6
commit 01e6e732cc
4 changed files with 288 additions and 79 deletions

View file

@ -26,90 +26,86 @@ namespace ircd {
namespace js { namespace js {
struct context struct context
:custom_ptr<JSContext> :private custom_ptr<JSContext>
{ {
struct privdata struct opts
{ {
virtual ~privdata() noexcept = 0; size_t stack_chunk_size = 8_KiB;
}; bool dtor_gc = true;
}
opts; // We keep a copy of the given opts here.
operator JSContext *() const { return get(); } operator JSContext *() const { return get(); }
operator JSContext &() const { return custom_ptr<JSContext>::operator*(); } operator JSContext &() const { return custom_ptr<JSContext>::operator*(); }
bool operator!() const { return !custom_ptr<JSContext>::operator bool(); }
auto &runtime() const { return our(JS_GetRuntime(get())); }
auto &runtime() { return our(JS_GetRuntime(get())); }
auto ptr() const { return get(); }
auto ptr() { return get(); }
void reset() { custom_ptr<JSContext>::reset(nullptr); } context(JSRuntime *const &, const struct opts &);
template<class... args> context(JSRuntime *const &, args&&...);
context() = default; context() = default;
context(context &&) noexcept;
context(const context &) = delete;
context &operator=(context &&) noexcept;
context &operator=(const context &) = delete;
}; };
// A default JSContext instance is provided residing near the main runtime as a convenience // A default JSContext instance is provided residing near the main runtime as a convenience
// for misc/utility/system purposes if necessary. // for misc/utility/system purposes if necessary.
extern context mc; extern context mc;
// Get to our `struct context` from any upstream JSContext
const context &our(const JSContext *const &);
context &our(JSContext *const &);
// Get/Set your privdata managed by this object, casting to your expected type. // Get/Set your privdata managed by this object, casting to your expected type.
template<size_t at, class T = context::privdata> const T *priv(const JSContext &); template<class T = privdata> const T *priv(const context &);
template<size_t at, class T = context::privdata> T *priv(JSContext &); template<class T = privdata> T *priv(context &);
template<size_t at, class T> void priv(JSContext &, T *); void priv(context &, privdata *const &);
auto version(const context &c) { return version(JS_GetVersion(c)); }
auto running(const context &c) { return JS_IsRunning(c); }
auto uncaught_exception(const context &c) { return JS_IsExceptionPending(c); }
auto rethrow_exception(context &c) { return JS_ReportPendingException(c); }
auto interrupted(const context &c) { return JS_CheckForInterrupt(c); }
void out_of_memory(context &c) { JS_ReportOutOfMemory(c); }
void allocation_overflow(context &c) { JS_ReportAllocationOverflow(c); }
void run_gc(context &c) { JS_MaybeGC(c); }
template<class... args> inline void
context::context(JSRuntime *const &runtime, priv(context &c,
args&&... a) privdata *const &ptr)
:custom_ptr<JSContext>
{
JS_NewContext(runtime, std::forward<args>(a)...),
[](JSContext *const ctx)
{
if(likely(ctx))
{
delete priv<0>(*ctx);
delete priv<1>(*ctx);
JS_DestroyContext(ctx);
}
}
}
{ {
delete priv(c); // Free any existing object to overwrite/null
JS_SetSecondContextPrivate(c, ptr);
} }
template<size_t at, template<class T>
class T>
void
priv(JSContext &c,
T *const &ptr)
{
delete priv<at>(c);
switch(at)
{
default:
case 0: JS_SetContextPrivate(&c, ptr); break;
case 1: JS_SetSecondContextPrivate(&c, ptr); break;
}
}
template<size_t at,
class T>
T * T *
priv(JSContext &c) priv(context &c)
{ {
switch(at) return dynamic_cast<T *>(static_cast<privdata *>(JS_GetSecondContextPrivate(c)));
{
default:
case 0: return dynamic_cast<T *>(static_cast<context::privdata *>(JS_GetContextPrivate(&c)));
case 1: return dynamic_cast<T *>(static_cast<context::privdata *>(JS_GetSecondContextPrivate(&c)));
}
} }
template<size_t at, template<class T>
class T>
const T * const T *
priv(const JSContext &c) priv(const context &c)
{ {
switch(at) return dynamic_cast<const T *>(static_cast<const privdata *>(JS_GetSecondContextPrivate(c)));
{
default:
case 0: return dynamic_cast<const T *>(static_cast<const context::privdata *>(JS_GetContextPrivate(&c)));
case 1: return dynamic_cast<const T *>(static_cast<const context::privdata *>(JS_GetSecondContextPrivate(&c)));
} }
inline context &
our(JSContext *const &c)
{
return *static_cast<context *>(JS_GetContextPrivate(c));
}
inline const context &
our(const JSContext *const &c)
{
return *static_cast<const context *>(JS_GetContextPrivate(const_cast<JSContext *>(c)));
} }
} // namespace js } // namespace js

View file

@ -41,6 +41,16 @@
namespace ircd { namespace ircd {
namespace js { namespace js {
// Extend this class to store your data with any priv(),
// i.e priv(runtime, privdata*) or priv(context, privdata*) etc
struct privdata
{
virtual ~privdata() noexcept = 0; // Your object is managed by the host
};
const char *version(const JSVersion &v) { return JS_VersionToString(v); }
JSVersion version(const char *const &v) { return JS_StringToVersion(v); }
} // namespace js } // namespace js
} // namespace ircd } // namespace ircd

View file

@ -29,31 +29,79 @@ class runtime
:custom_ptr<JSRuntime> :custom_ptr<JSRuntime>
{ {
static void handle_error(JSContext *, const char *msg, JSErrorReport *); static void handle_error(JSContext *, const char *msg, JSErrorReport *);
static void handle_out_of_memory(JSContext *, void *);
static void handle_large_allocation_failure(void *);
static void handle_gc(JSRuntime *, JSGCStatus, void *);
static void handle_finalize(JSFreeOp *, JSFinalizeStatus, bool is_compartment, void *);
static void handle_destroy_compartment(JSFreeOp *, JSCompartment *);
static void handle_iterate_compartments(JSRuntime *, void *, JSCompartment *);
static bool handle_context(JSContext *, uint op, void *);
static bool handle_interrupt(JSContext *);
public: public:
struct opts
{
size_t maxbytes = 64_MiB;
size_t code_stack_max = 0;
size_t trusted_stack_max = 0;
size_t untrusted_stack_max = 0;
};
struct opts opts; // We keep a copy of the given opts here
operator JSRuntime *() const { return get(); } operator JSRuntime *() const { return get(); }
operator JSRuntime &() const { return custom_ptr<JSRuntime>::operator*(); } operator JSRuntime &() const { return custom_ptr<JSRuntime>::operator*(); }
bool operator!() const { return !custom_ptr<JSRuntime>::operator bool(); }
auto ptr() const { return get(); }
auto ptr() { return get(); }
void reset() { custom_ptr<JSRuntime>::reset(nullptr); } runtime(const struct opts &);
template<class... args> runtime(args&&...);
runtime() = default; runtime() = default;
runtime(runtime &&) noexcept;
runtime(const runtime &) = delete;
runtime &operator=(runtime &&) noexcept;
runtime &operator=(const runtime &) = delete;
friend void interrupt(runtime &);
}; };
// Main JSRuntime instance. This should be passable in any argument requiring a // Main JSRuntime instance. This should be passable in any argument requiring a
// JSRuntime pointer. It is only valid while the js::init object is held by ircd::main(). // JSRuntime pointer. It is only valid while the js::init object is held by ircd::main().
extern runtime main; extern runtime main;
template<class... args> // Get to our `struct runtime` from any upstream JSRuntime
runtime::runtime(args&&... a) const runtime &our(const JSRuntime *const &);
:custom_ptr<JSRuntime> runtime &our(JSRuntime *const &);
void interrupt(runtime &r);
void run_gc(runtime &r);
inline void
run_gc(runtime &r)
{ {
JS_NewRuntime(std::forward<args>(a)...), JS_GC(r);
JS_DestroyRuntime
} }
inline void
interrupt(runtime &r)
{ {
JS_SetErrorReporter(get(), handle_error); JS_SetInterruptCallback(r, runtime::handle_interrupt);
JS_RequestInterruptCallback(r);
} }
inline runtime &
our(JSRuntime *const &c)
{
return *static_cast<runtime *>(JS_GetRuntimePrivate(c));
}
inline const runtime &
our(const JSRuntime *const &c)
{
return *static_cast<const runtime *>(JS_GetRuntimePrivate(const_cast<JSRuntime *>(c)));
}
} // namespace js } // namespace js
} // namespace ircd } // namespace ircd

View file

@ -58,23 +58,25 @@ ircd::js::init::init()
if(!JS_Init()) if(!JS_Init())
throw error("JS_Init(): failure"); throw error("JS_Init(): failure");
const size_t main_maxbytes(64_MiB); //TODO: Configuration struct runtime::opts runtime_opts;
const size_t mc_stackchunk(8_KiB); //TODO: Configuration struct context::opts context_opts;
log.info("Initializing the main JS Runtime (main_maxbytes: %zu)",
runtime_opts.maxbytes);
log.info("Initializing the main JS Runtime (main_maxbytes: %zu, mc_stackchunk: %zu)", main = runtime(runtime_opts);
main_maxbytes, mc = context(main, context_opts);
mc_stackchunk); log.info("Initialized main JS Runtime and context (version: '%s')",
version(mc));
main = runtime(main_maxbytes);
mc = context(main, mc_stackchunk);
} }
ircd::js::init::~init() ircd::js::init::~init()
noexcept noexcept
{ {
log.info("Terminating the main JS Runtime"); log.info("Terminating the main JS Runtime");
mc.reset();
main.reset(); // Assign empty objects to free and reset
mc = context{};
main = runtime{};
log.info("Terminating the JS engine"); log.info("Terminating the JS engine");
JS_ShutDown(); JS_ShutDown();
@ -103,15 +105,168 @@ ircd::js::version(const ver &type)
// ircd/js/context.h // ircd/js/context.h
// //
ircd::js::context::context(JSRuntime *const &runtime,
const struct opts &opts)
:custom_ptr<JSContext>
{
JS_NewContext(runtime, opts.stack_chunk_size),
[opts](JSContext *const ctx) //TODO: old gcc/clang can't copy from opts.dtor_gc
noexcept
{
if(!ctx)
return;
// Free the user's privdata managed object
delete static_cast<const privdata *>(JS_GetSecondContextPrivate(ctx));
if(opts.dtor_gc)
JS_DestroyContext(ctx);
else
JS_DestroyContextNoGC(ctx);
}
}
,opts{opts}
{
JS_SetContextPrivate(get(), this);
}
ircd::js::context::context(context &&other)
noexcept
:custom_ptr<JSContext>{std::move(other)}
,opts{std::move(other.opts)}
{
// Branch not taken for null/defaulted instance of JSContext smart ptr
if(!!*this)
JS_SetContextPrivate(get(), this);
}
ircd::js::context &
ircd::js::context::operator=(context &&other)
noexcept
{
static_cast<custom_ptr<JSContext> &>(*this) = std::move(other);
opts = std::move(other.opts);
// Branch not taken for null/defaulted instance of JSContext smart ptr
if(!!*this)
JS_SetContextPrivate(get(), this);
return *this;
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// //
// ircd/js/runtime.h // ircd/js/runtime.h
// //
ircd::js::runtime::runtime(const struct opts &opts)
:custom_ptr<JSRuntime>
{
JS_NewRuntime(opts.maxbytes),
JS_DestroyRuntime
}
,opts(opts)
{
JS_SetRuntimePrivate(get(), this);
JS_SetErrorReporter(get(), handle_error);
JS::SetOutOfMemoryCallback(get(), handle_out_of_memory, nullptr);
JS::SetLargeAllocationFailureCallback(get(), handle_large_allocation_failure, nullptr);
JS_SetGCCallback(get(), handle_gc, nullptr);
JS_AddFinalizeCallback(get(), handle_finalize, nullptr);
JS_SetDestroyCompartmentCallback(get(), handle_destroy_compartment);
JS_SetContextCallback(get(), handle_context, nullptr);
JS_SetNativeStackQuota(get(), opts.code_stack_max, opts.trusted_stack_max, opts.untrusted_stack_max);
}
ircd::js::runtime::runtime(runtime &&other)
noexcept
:custom_ptr<JSRuntime>{std::move(other)}
,opts(std::move(other.opts))
{
// Branch not taken for null/defaulted instance of JSRuntime smart ptr
if(!!*this)
JS_SetRuntimePrivate(get(), this);
}
ircd::js::runtime &
ircd::js::runtime::operator=(runtime &&other)
noexcept
{
static_cast<custom_ptr<JSRuntime> &>(*this) = std::move(other);
opts = std::move(other.opts);
// Branch not taken for null/defaulted instance of JSRuntime smart ptr
if(!!*this)
JS_SetRuntimePrivate(get(), this);
return *this;
}
bool
ircd::js::runtime::handle_interrupt(JSContext *const ctx)
{
auto &runtime(our(ctx).runtime());
JS_SetInterruptCallback(runtime, nullptr);
return false;
}
bool
ircd::js::runtime::handle_context(JSContext *const c,
const uint op,
void *const priv)
{
return true;
}
void
ircd::js::runtime::handle_iterate_compartments(JSRuntime *const rt,
void *const priv,
JSCompartment *const compartment)
{
}
void
ircd::js::runtime::handle_destroy_compartment(JSFreeOp *const fop,
JSCompartment *const compartment)
{
}
void
ircd::js::runtime::handle_finalize(JSFreeOp *const fop,
const JSFinalizeStatus status,
const bool is_compartment,
void *const priv)
{
}
void
ircd::js::runtime::handle_gc(JSRuntime *const rt,
const JSGCStatus status,
void *const priv)
{
}
void
ircd::js::runtime::handle_large_allocation_failure(void *const priv)
{
log.error("Large allocation failure");
}
void
ircd::js::runtime::handle_out_of_memory(JSContext *const ctx,
void *const priv)
{
log.error("JSContext(%p): out of memory", (const void *)ctx);
}
void void
ircd::js::runtime::handle_error(JSContext *const ctx, ircd::js::runtime::handle_error(JSContext *const ctx,
const char *const msg, const char *const msg,
JSErrorReport *const report) JSErrorReport *const report)
{ {
log.error("JSContext(%p): %s", ctx, msg); log.error("JSContext(%p): %s", (const void *)ctx, msg);
} }