mirror of
https://github.com/matrix-construct/construct
synced 2024-11-25 16:22:35 +01:00
ircd::js: Add trap_function alternative to full object trap.
This commit is contained in:
parent
56d8e429a2
commit
54c8044ca1
3 changed files with 147 additions and 0 deletions
|
@ -79,3 +79,4 @@ inline JSVersion version(const char *const &v) { return JS_StringToVersion(v);
|
|||
#include "for_each.h"
|
||||
#include "args.h"
|
||||
#include "trap.h"
|
||||
#include "trap_function.h"
|
||||
|
|
54
include/ircd/js/trap_function.h
Normal file
54
include/ircd/js/trap_function.h
Normal 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_TRAP_FUNCTION
|
||||
|
||||
namespace ircd {
|
||||
namespace js {
|
||||
|
||||
struct trap_function
|
||||
{
|
||||
std::string name;
|
||||
uint arity;
|
||||
uint flags;
|
||||
|
||||
protected:
|
||||
virtual value on_call(object::handle, const args &);
|
||||
|
||||
private:
|
||||
static trap_function &from(JSObject *const &);
|
||||
static bool handle_call(JSContext *, unsigned, JS::Value *) noexcept;
|
||||
|
||||
public:
|
||||
function operator()(const object::handle &);
|
||||
|
||||
trap_function(std::string name,
|
||||
const uint &arity = 0,
|
||||
const uint &flags = 0);
|
||||
|
||||
trap_function(trap_function &&) = delete;
|
||||
trap_function(const trap_function &) = delete;
|
||||
virtual ~trap_function() noexcept;
|
||||
};
|
||||
|
||||
} // namespace js
|
||||
} // namespace ircd
|
92
ircd/js.cc
92
ircd/js.cc
|
@ -138,6 +138,98 @@ js::ReportOutOfMemory(ExclusiveContext *const c)
|
|||
// ircd/js/js.h - With 3rd party (JSAPI) symbols
|
||||
//
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// ircd/js/trap_function.h
|
||||
//
|
||||
|
||||
ircd::js::trap_function::trap_function(std::string name,
|
||||
const uint &arity,
|
||||
const uint &flags)
|
||||
:name{std::move(name)}
|
||||
,arity{arity}
|
||||
,flags{flags}
|
||||
{
|
||||
}
|
||||
|
||||
ircd::js::trap_function::~trap_function()
|
||||
noexcept
|
||||
{
|
||||
}
|
||||
|
||||
ircd::js::function
|
||||
ircd::js::trap_function::operator()(const object::handle &obj)
|
||||
{
|
||||
const auto jsf(::js::DefineFunctionWithReserved(*cx,
|
||||
obj,
|
||||
name.c_str(),
|
||||
handle_call,
|
||||
arity,
|
||||
flags));
|
||||
if(unlikely(!jsf))
|
||||
throw internal_error("Failed to create trap_function");
|
||||
|
||||
function ret(jsf);
|
||||
::js::SetFunctionNativeReserved(ret, 0, pointer_value(this));
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool
|
||||
ircd::js::trap_function::handle_call(JSContext *const c,
|
||||
const unsigned argc,
|
||||
JS::Value *const argv)
|
||||
noexcept try
|
||||
{
|
||||
assert(&our(c) == cx);
|
||||
|
||||
const struct args args(argc, argv);
|
||||
//const auto that(args.computeThis(c));
|
||||
const object func(args.callee());
|
||||
auto &trap(from(func));
|
||||
log.debug("trap_function(%p) \"%s\": call",
|
||||
(const void *)&trap,
|
||||
trap.name.c_str());
|
||||
|
||||
args.rval().set(trap.on_call(func, args));
|
||||
return true;
|
||||
}
|
||||
catch(const jserror &e)
|
||||
{
|
||||
e.set_pending();
|
||||
return false;
|
||||
}
|
||||
catch(const std::exception &e)
|
||||
{
|
||||
const struct args args(argc, argv);
|
||||
auto &func(args.callee());
|
||||
auto &trap(from(&func));
|
||||
|
||||
log.error("trap_function(%p) \"%s\": %s",
|
||||
reinterpret_cast<const void *>(&trap),
|
||||
trap.name.c_str(),
|
||||
e.what());
|
||||
|
||||
JS_ReportError(*cx, "BUG: trap_function(%p) \"%s\": %s",
|
||||
reinterpret_cast<const void *>(&trap),
|
||||
trap.name.c_str(),
|
||||
e.what());
|
||||
return false;
|
||||
}
|
||||
|
||||
ircd::js::trap_function &
|
||||
ircd::js::trap_function::from(JSObject *const &func)
|
||||
{
|
||||
const auto tval(::js::GetFunctionNativeReserved(func, 0));
|
||||
return *pointer_value<trap_function>(tval);
|
||||
}
|
||||
|
||||
ircd::js::value
|
||||
ircd::js::trap_function::on_call(object::handle,
|
||||
const args &)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// ircd/js/trap.h
|
||||
|
|
Loading…
Reference in a new issue