0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-25 16:22:35 +01:00

ircd::js: Add barebones accoutrements.

This commit is contained in:
Jason Volk 2016-10-12 19:58:48 -07:00
parent 191258cea5
commit c1fd6391c6
6 changed files with 202 additions and 8 deletions

View file

@ -0,0 +1,54 @@
/*
* 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_COMPARTMENT_GUARD_H
namespace ircd {
namespace js {
class compartment_guard
{
JSContext *cx;
JSCompartment *cp;
public:
compartment_guard(JSContext &, JSObject &);
~compartment_guard() noexcept;
};
inline
compartment_guard::compartment_guard(JSContext &cx,
JSObject &obj)
:cx{&cx}
,cp{JS_EnterCompartment(&cx, &obj)}
{
}
inline
compartment_guard::~compartment_guard()
noexcept
{
JS_LeaveCompartment(cx, cp);
}
} // namespace js
} // namespace ircd

View file

@ -28,8 +28,15 @@ namespace js {
struct context
:custom_ptr<JSContext>
{
operator const JSContext *() const { return get(); }
operator JSContext *() { return get(); }
struct privdata
{
virtual ~privdata() noexcept = 0;
};
operator JSContext *() const { return get(); }
operator JSContext &() const { return custom_ptr<JSContext>::operator*(); }
void reset() { custom_ptr<JSContext>::reset(nullptr); }
template<class... args> context(JSRuntime *const &, args&&...);
context() = default;
@ -39,16 +46,71 @@ struct context
// for misc/utility/system purposes if necessary.
extern context mc;
// 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<size_t at, class T = context::privdata> T *priv(JSContext &);
template<size_t at, class T> void priv(JSContext &, T *);
template<class... args>
context::context(JSRuntime *const &runtime,
args&&... a)
:custom_ptr<JSContext>
{
JS_NewContext(runtime, std::forward<args>(a)...),
JS_DestroyContext
[](JSContext *const ctx)
{
if(likely(ctx))
{
delete priv<0>(*ctx);
delete priv<1>(*ctx);
JS_DestroyContext(ctx);
}
}
}
{
}
template<size_t at,
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 *
priv(JSContext &c)
{
switch(at)
{
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,
class T>
const T *
priv(const JSContext &c)
{
switch(at)
{
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)));
}
}
} // namespace js
} // namespace ircd

View file

@ -46,3 +46,5 @@ namespace js {
#include "runtime.h"
#include "context.h"
#include "request_guard.h"
#include "compartment_guard.h"

View file

@ -0,0 +1,52 @@
/*
* 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_REQUEST_GUARD_H
namespace ircd {
namespace js {
class request_guard
{
JSContext *c;
public:
request_guard(JSContext &);
~request_guard() noexcept;
};
inline
request_guard::request_guard(JSContext &c)
:c(&c)
{
JS_BeginRequest(&c);
}
inline
request_guard::~request_guard()
noexcept
{
JS_EndRequest(c);
}
} // namespace js
} // namespace ircd

View file

@ -25,11 +25,16 @@
namespace ircd {
namespace js {
struct runtime
class runtime
:custom_ptr<JSRuntime>
{
operator const JSRuntime *() const { return get(); }
operator JSRuntime *() { return get(); }
static void handle_error(JSContext *, const char *msg, JSErrorReport *);
public:
operator JSRuntime *() const { return get(); }
operator JSRuntime &() const { return custom_ptr<JSRuntime>::operator*(); }
void reset() { custom_ptr<JSRuntime>::reset(nullptr); }
template<class... args> runtime(args&&...);
runtime() = default;
@ -47,6 +52,7 @@ runtime::runtime(args&&... a)
JS_DestroyRuntime
}
{
JS_SetErrorReporter(get(), handle_error);
}
} // namespace js

View file

@ -73,8 +73,8 @@ ircd::js::init::~init()
noexcept
{
log.info("Terminating the main JS Runtime");
mc.reset(nullptr);
main.reset(nullptr);
mc.reset();
main.reset();
log.info("Terminating the JS engine");
JS_ShutDown();
@ -97,3 +97,21 @@ ircd::js::version(const ver &type)
//
// ircd/js/js.h - With 3rd party (JSAPI) symbols
//
///////////////////////////////////////////////////////////////////////////////
//
// ircd/js/context.h
//
///////////////////////////////////////////////////////////////////////////////
//
// ircd/js/runtime.h
//
void
ircd::js::runtime::handle_error(JSContext *const ctx,
const char *const msg,
JSErrorReport *const report)
{
log.error("JSContext(%p): %s", ctx, msg);
}