Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
Script::Function Class Reference

#include <FunctionWrapper.h>

Collaboration diagram for Script::Function:

Classes

struct  args_info_t
 Convenient struct to get info on a [class] [const] function pointer. More...
 
struct  args_info_t< R(*)(Types ...)>
 
struct  args_info_t< R(C::*)(Types ...) const >
 
struct  args_info_t< R(C::*)(Types ...)>
 
struct  IgnoreResult_t
 
struct  IteratorResultError
 
class  StatefulCallback
 
struct  StatefullCallbackPrivateSlot
 

Public Types

template<typename T >
using ObjectGetter = T *(*)(const Request &, JS::CallArgs &)
 
template<auto callable>
using GetterFor = ObjectGetter< typename args_info< callable >::object_type >
 

Static Public Member Functions

template<auto callable, GetterFor< callable > thisGetter = nullptr>
static bool ToJSNative (JSContext *cx, unsigned argc, JS::Value *vp)
 The meat of this file. More...
 
template<typename R , typename ... Args>
static bool Call (const Request &rq, JS::HandleValue val, const char *name, R &ret, const Args &... args)
 Call a JS function name, property of object val, with the arguments args. More...
 
template<typename ... Args>
static bool Call (const Request &rq, JS::HandleValue val, const char *name, JS::MutableHandleValue ret, const Args &... args)
 
template<typename ... Args>
static bool CallVoid (const Request &rq, JS::HandleValue val, const char *name, const Args &... args)
 Call a JS function name, property of object val, with the arguments args. More...
 
template<typename Callback >
static JS::Value RunGenerator (const Request &rq, JS::HandleValue val, const char *name, JS::HandleValue arg, Callback yieldCallback)
 Call a JS function name, property of object val, with the argument args. More...
 
template<auto callable, GetterFor< callable > thisGetter = nullptr>
static JSFunctionSpec Wrap (const char *name, const std::uint16_t flags=JSPROP_ENUMERATE|JSPROP_READONLY|JSPROP_PERMANENT)
 Return a function spec from a C++ function. More...
 
template<auto callable, GetterFor< callable > thisGetter = nullptr>
static JSFunction * Create (const Request &rq, const char *name, const std::uint16_t flags=JSPROP_ENUMERATE|JSPROP_READONLY|JSPROP_PERMANENT)
 Return a JSFunction from a C++ function. More...
 
template<auto callable, GetterFor< callable > thisGetter = nullptr>
static void Register (const Request &rq, const char *name, const std::uint16_t flags=JSPROP_ENUMERATE|JSPROP_READONLY|JSPROP_PERMANENT)
 Register a function on the native scope (usually 'Engine'). More...
 
template<auto callable, GetterFor< callable > thisGetter = nullptr>
static void Register (JSContext *cx, JS::HandleObject scope, const char *name, const std::uint16_t flags=JSPROP_ENUMERATE|JSPROP_READONLY|JSPROP_PERMANENT)
 Register a function on. More...
 
template<typename Callable >
static StatefulCallback< Callable > Register (const Request &rq, std::string name, const unsigned flags, Callable callable)
 
template<typename Callable >
static StatefulCallback< Callable > Register (const Request &rq, std::string name, Callable callable)
 

Private Types

template<typename T >
using type_transform = std::conditional_t< std::is_same_v< const Request &, T >||std::is_same_v< const Interface &, T >, T, std::remove_const_t< typename std::remove_reference_t< T > > >
 In JS->C++ calls, types are converted using FromJSVal, and this requires them to be default-constructible (as that function takes an out parameter) thus constref needs to be removed when defining the tuple. More...
 
template<auto T>
using args_info = args_info_t< decltype(T)>
 

Private Member Functions

 Function ()=delete
 
 Function (const Function &)=delete
 
 Function (Function &&)=delete
 

Static Private Member Functions

template<size_t idx, typename T >
static T DoConvertFromJS (const Request &rq, JS::CallArgs &args, bool &wentOk)
 DoConvertFromJS takes a type, a JS argument, and converts. More...
 
template<typename... T, size_t... idx>
static std::tuple< T... > DoConvertFromJS (std::index_sequence< idx... >, const Request &rq, JS::CallArgs &args, bool &wentOk)
 Wrapper: calls DoConvertFromJS for each element in T. More...
 
template<typename ... Types>
static std::tuple< Types... > ConvertFromJS (const Request &rq, JS::CallArgs &args, bool &wentOk, std::tuple< Types... > *)
 ConvertFromJS is a wrapper around DoConvertFromJS, and handles specific cases for the first argument (Request, ...). More...
 
template<typename ... Types>
static std::tuple< const Request &, Types... > ConvertFromJS (const Request &rq, JS::CallArgs &args, bool &wentOk, std::tuple< const Request &, Types... > *)
 
template<typename ... Types>
static std::tuple< const Interface &, Types... > ConvertFromJS (const Request &rq, JS::CallArgs &args, bool &wentOk, std::tuple< const Interface &, Types... > *)
 
template<auto callable, typename T , typename tuple >
static args_info< callable >::return_type call (T *object, tuple &args)
 Wrap std::apply for the case where we have an object method or a regular function. More...
 
template<typename... Types, size_t... idx>
static void ToJSValVector (std::index_sequence< idx... >, const Request &rq, JS::MutableHandleValueVector argv, const Types &... params)
 Converts any number of arguments to a JS::MutableHandleValueVector. More...
 
template<typename R , typename ... Args>
static bool Call_ (const Request &rq, JS::HandleValue val, const char *name, R &ret, const Args &... args)
 Wrapper around calling a JS function from C++. More...
 

Static Private Attributes

static IgnoreResult_t IgnoreResult
 

Member Typedef Documentation

◆ args_info

template<auto T>
using Script::Function::args_info = args_info_t<decltype(T)>
private

◆ GetterFor

template<auto callable>
using Script::Function::GetterFor = ObjectGetter<typename args_info<callable>::object_type>

◆ ObjectGetter

template<typename T >
using Script::Function::ObjectGetter = T*(*)(const Request&, JS::CallArgs&)

◆ type_transform

template<typename T >
using Script::Function::type_transform = std::conditional_t< std::is_same_v<const Request&, T> || std::is_same_v<const Interface&, T>, T, std::remove_const_t<typename std::remove_reference_t<T> > >
private

In JS->C++ calls, types are converted using FromJSVal, and this requires them to be default-constructible (as that function takes an out parameter) thus constref needs to be removed when defining the tuple.

Exceptions are:

  • const Request& (as the first argument only, for implementation simplicity).
  • const Interface& (as the first argument only, for implementation simplicity).
  • JS::HandleValue

Constructor & Destructor Documentation

◆ Function() [1/3]

Script::Function::Function ( )
privatedelete

◆ Function() [2/3]

Script::Function::Function ( const Function &  )
privatedelete

◆ Function() [3/3]

Script::Function::Function ( Function &&  )
privatedelete

Member Function Documentation

◆ Call() [1/2]

template<typename ... Args>
static bool Script::Function::Call ( const Request &  rq,
JS::HandleValue  val,
const char *  name,
JS::MutableHandleValue  ret,
const Args &...  args 
)
inlinestatic

◆ Call() [2/2]

template<typename R , typename ... Args>
static bool Script::Function::Call ( const Request &  rq,
JS::HandleValue  val,
const char *  name,
R &  ret,
const Args &...  args 
)
inlinestatic

Call a JS function name, property of object val, with the arguments args.

ret will be updated with the return value, if any.

Returns
the success (or failure) thereof.

◆ call()

template<auto callable, typename T , typename tuple >
static args_info< callable >::return_type Script::Function::call ( T *  object,
tuple &  args 
)
inlinestaticprivate

Wrap std::apply for the case where we have an object method or a regular function.

◆ Call_()

template<typename R , typename ... Args>
static bool Script::Function::Call_ ( const Request &  rq,
JS::HandleValue  val,
const char *  name,
R &  ret,
const Args &...  args 
)
inlinestaticprivate

Wrapper around calling a JS function from C++.

Arguments are const& to avoid lvalue/rvalue issues, and so can't be used as out-parameters. In particular, the problem is that Rooted are deduced as Rooted, not Handle, and so can't be copied. This could be worked around with more templates, but it doesn't seem particularly worth doing.

◆ CallVoid()

template<typename ... Args>
static bool Script::Function::CallVoid ( const Request &  rq,
JS::HandleValue  val,
const char *  name,
const Args &...  args 
)
inlinestatic

Call a JS function name, property of object val, with the arguments args.

Returns
the success (or failure) thereof.

◆ ConvertFromJS() [1/3]

template<typename ... Types>
static std::tuple< const Interface &, Types... > Script::Function::ConvertFromJS ( const Request &  rq,
JS::CallArgs &  args,
bool &  wentOk,
std::tuple< const Interface &, Types... > *   
)
inlinestaticprivate

◆ ConvertFromJS() [2/3]

template<typename ... Types>
static std::tuple< const Request &, Types... > Script::Function::ConvertFromJS ( const Request &  rq,
JS::CallArgs &  args,
bool &  wentOk,
std::tuple< const Request &, Types... > *   
)
inlinestaticprivate

◆ ConvertFromJS() [3/3]

template<typename ... Types>
static std::tuple< Types... > Script::Function::ConvertFromJS ( const Request &  rq,
JS::CallArgs &  args,
bool &  wentOk,
std::tuple< Types... > *   
)
inlinestaticprivate

ConvertFromJS is a wrapper around DoConvertFromJS, and handles specific cases for the first argument (Request, ...).

Trick: to unpack the types of the tuple as a parameter pack, we deduce them from the function signature. To do that, we want the tuple in the arguments, but we don't want to actually have to default-instantiate, so we'll pass a nullptr that's static_cast to what we want.

◆ Create()

template<auto callable, GetterFor< callable > thisGetter = nullptr>
static JSFunction * Script::Function::Create ( const Request &  rq,
const char *  name,
const std::uint16_t  flags = JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT 
)
inlinestatic

Return a JSFunction from a C++ function.

◆ DoConvertFromJS() [1/2]

template<size_t idx, typename T >
static T Script::Function::DoConvertFromJS ( const Request &  rq,
JS::CallArgs &  args,
bool &  wentOk 
)
inlinestaticprivate

DoConvertFromJS takes a type, a JS argument, and converts.

The type T must be default constructible (except for HandleValue, which is handled specially). (possible) TODO: this could probably be changed if FromJSVal had a different signature.

Parameters
wentOk- true if the conversion succeeded and wentOk was true before, false otherwise.

◆ DoConvertFromJS() [2/2]

template<typename... T, size_t... idx>
static std::tuple< T... > Script::Function::DoConvertFromJS ( std::index_sequence< idx... >  ,
const Request &  rq,
JS::CallArgs &  args,
bool &  wentOk 
)
inlinestaticprivate

Wrapper: calls DoConvertFromJS for each element in T.

◆ Register() [1/4]

template<auto callable, GetterFor< callable > thisGetter = nullptr>
static void Script::Function::Register ( const Request &  rq,
const char *  name,
const std::uint16_t  flags = JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT 
)
inlinestatic

Register a function on the native scope (usually 'Engine').

◆ Register() [2/4]

template<typename Callable >
static StatefulCallback< Callable > Script::Function::Register ( const Request &  rq,
std::string  name,
Callable  callable 
)
inlinestatic

◆ Register() [3/4]

template<typename Callable >
static StatefulCallback< Callable > Script::Function::Register ( const Request &  rq,
std::string  name,
const unsigned  flags,
Callable  callable 
)
inlinestatic

◆ Register() [4/4]

template<auto callable, GetterFor< callable > thisGetter = nullptr>
static void Script::Function::Register ( JSContext *  cx,
JS::HandleObject  scope,
const char *  name,
const std::uint16_t  flags = JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT 
)
inlinestatic

Register a function on.

Parameters
scope.Prefer the version taking Request unless you have a good reason not to.
See also
Register

◆ RunGenerator()

template<typename Callback >
static JS::Value Script::Function::RunGenerator ( const Request &  rq,
JS::HandleValue  val,
const char *  name,
JS::HandleValue  arg,
Callback  yieldCallback 
)
inlinestatic

Call a JS function name, property of object val, with the argument args.

Repeatetly invokes yieldCallback with the yielded value.

Returns
the final value of the generator.

◆ ToJSNative()

template<auto callable, GetterFor< callable > thisGetter = nullptr>
static bool Script::Function::ToJSNative ( JSContext *  cx,
unsigned  argc,
JS::Value *  vp 
)
inlinestatic

The meat of this file.

This wraps a C++ function into a JSNative, so that it can be called from JS and manipulated in Spidermonkey. Most C++ functions can be directly wrapped, so long as their arguments are convertible from JS::Value and their return value is convertible to JS::Value (or void) The C++ function may optionally take const Request& or Interface& as its first argument. The function may be an object method, in which case you need to pass an appropriate getter

Optimisation note: the Request object is created even without arguments, as it's necessary for IsExceptionPending.

Parameters
thisGetterto get the object, if necessary.

◆ ToJSValVector()

template<typename... Types, size_t... idx>
static void Script::Function::ToJSValVector ( std::index_sequence< idx... >  ,
const Request &  rq,
JS::MutableHandleValueVector  argv,
const Types &...  params 
)
inlinestaticprivate

Converts any number of arguments to a JS::MutableHandleValueVector.

If idx is empty this function does nothing. For that case there is a [[maybe_unused]] on argv. GCC would issue a "-Wunused-but-set-parameter" warning. For references like rq this warning isn't issued.

◆ Wrap()

template<auto callable, GetterFor< callable > thisGetter = nullptr>
static JSFunctionSpec Script::Function::Wrap ( const char *  name,
const std::uint16_t  flags = JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT 
)
inlinestatic

Return a function spec from a C++ function.

Member Data Documentation

◆ IgnoreResult

IgnoreResult_t Script::Function::IgnoreResult
inlinestaticprivate

The documentation for this class was generated from the following file: