0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-09-28 19:58:53 +02:00

ircd::js: Add script object + caller / deinline function caller.

This commit is contained in:
Jason Volk 2016-10-18 18:00:25 -07:00
parent 8108773021
commit 23318a2b4f
4 changed files with 128 additions and 53 deletions

View file

@ -25,49 +25,25 @@
namespace ircd {
namespace js {
inline value
value
call(const object &obj,
const JS::HandleFunction &func,
const JS::HandleValueArray &args = JS::HandleValueArray::empty())
{
value ret;
if(!JS_CallFunction(*cx, obj, func, args, &ret))
throw internal_error("Failed to call function");
const JS::HandleValueArray &args = JS::HandleValueArray::empty());
return ret;
}
inline value
value
call(const object &obj,
const JS::HandleValue &val,
const JS::HandleValueArray &args)
{
value ret;
if(!JS_CallFunctionValue(*cx, obj, val, args, &ret))
throw internal_error("Failed to apply function value to object");
const JS::HandleValueArray &args = JS::HandleValueArray::empty());
return ret;
}
inline value
value
call(const object &obj,
const char *const &name,
const JS::HandleValueArray &args)
{
value ret;
if(!JS_CallFunctionName(*cx, obj, name, args, &ret))
throw reference_error("Failed to call function \"%s\"", name);
const JS::HandleValueArray &args = JS::HandleValueArray::empty());
return ret;
}
inline value
value
call(const object &obj,
const std::string &name,
const JS::HandleValueArray &args)
{
return call(obj, name.c_str(), args);
}
const JS::HandleValueArray &args = JS::HandleValueArray::empty());
} // namespace js
} // namespace ircd

View file

@ -90,25 +90,6 @@ function::function(JSFunction *const &func)
{
}
inline value
function::operator()(const object &that)
const
{
return operator()(that, JS::HandleValueArray::empty());
}
inline value
function::operator()(const object &that,
const JS::HandleValueArray &args)
const
{
value ret;
if(!JS_CallFunction(*cx, that, *this, args, &ret))
throw internal_error("Failed to call Function");
return ret;
}
inline string
name(const function &f)
{

View file

@ -26,19 +26,34 @@ namespace ircd {
namespace js {
struct script
:JS::Rooted<JSScript *>
{
value operator()(JS::AutoObjectVector &stack) const;
value operator()() const;
script(const JS::CompileOptions &opts, const std::string &src); // new script
script(JSScript *const &);
script();
~script() noexcept;
script(script &&) noexcept;
script(const script &) = delete;
};
inline
script::script()
:JS::Rooted<JSScript *>{*cx}
{
}
inline
script::~script()
script::script(script &&other)
noexcept
:JS::Rooted<JSScript *>{*cx, other}
{
}
inline
script::script(JSScript *const &val)
:JS::Rooted<JSScript *>{*cx, val}
{
}

View file

@ -570,6 +570,41 @@ ircd::js::trap::on_add(const JSObject &obj,
return val;
}
///////////////////////////////////////////////////////////////////////////////
//
// ircd/js/script.h
//
ircd::js::script::script(const JS::CompileOptions &opts,
const std::string &src)
:JS::Rooted<JSScript *>{*cx}
{
if(!JS::Compile(*cx, opts, src.data(), src.size(), &(*this)))
throw syntax_error("Failed to compile script");
}
ircd::js::value
ircd::js::script::operator()()
const
{
value ret;
if(!JS_ExecuteScript(*cx, *this, &ret))
throw internal_error("Failed to execute script");
return ret;
}
ircd::js::value
ircd::js::script::operator()(JS::AutoObjectVector &stack)
const
{
value ret;
if(!JS_ExecuteScript(*cx, stack, *this, &ret))
throw internal_error("Failed to execute script");
return ret;
}
///////////////////////////////////////////////////////////////////////////////
//
// ircd/js/function.h
@ -603,6 +638,74 @@ ircd::js::function::function(JS::AutoObjectVector &stack,
}
}
ircd::js::value
ircd::js::function::operator()(const object &that)
const
{
return operator()(that, JS::HandleValueArray::empty());
}
ircd::js::value
ircd::js::function::operator()(const object &that,
const JS::HandleValueArray &args)
const
{
value ret;
if(!JS_CallFunction(*cx, that, *this, args, &ret))
throw internal_error("Failed to call Function");
return ret;
}
///////////////////////////////////////////////////////////////////////////////
//
// ircd/js/call.h
//
ircd::js::value
ircd::js::call(const object &obj,
const JS::HandleFunction &func,
const JS::HandleValueArray &args)
{
value ret;
if(!JS_CallFunction(*cx, obj, func, args, &ret))
throw internal_error("Failed to call function");
return ret;
}
ircd::js::value
ircd::js::call(const object &obj,
const JS::HandleValue &val,
const JS::HandleValueArray &args)
{
value ret;
if(!JS_CallFunctionValue(*cx, obj, val, args, &ret))
throw internal_error("Failed to apply function value to object");
return ret;
}
ircd::js::value
ircd::js::call(const object &obj,
const char *const &name,
const JS::HandleValueArray &args)
{
value ret;
if(!JS_CallFunctionName(*cx, obj, name, args, &ret))
throw reference_error("Failed to call function \"%s\"", name);
return ret;
}
ircd::js::value
ircd::js::call(const object &obj,
const std::string &name,
const JS::HandleValueArray &args)
{
return call(obj, name.c_str(), args);
}
///////////////////////////////////////////////////////////////////////////////
//
// ircd/js/string.h