Pyrogenesis  trunk
NativeWrapperDecls.h
Go to the documentation of this file.
1 /* Copyright (C) 2017 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <boost/preprocessor/punctuation/comma_if.hpp>
19 #include <boost/preprocessor/repetition/repeat.hpp>
20 
21 // MaybeRef should be private, but has to be public due to a compiler bug in clang.
22 // TODO: Make this private when the bug is fixed in all supported versions of clang.
23 template <typename T> struct MaybeRef;
24 
25 // Define lots of useful macros:
26 
27 // Varieties of comma-separated list to fit on the head/tail/whole of another comma-separated list
28 #define NUMBERED_LIST_HEAD(z, i, data) data##i,
29 #define NUMBERED_LIST_TAIL(z, i, data) ,data##i
30 #define NUMBERED_LIST_TAIL_MAYBE_REF(z, i, data) , typename MaybeRef<data##i>::Type
31 #define NUMBERED_LIST_BALANCED(z, i, data) BOOST_PP_COMMA_IF(i) data##i
32 #define NUMBERED_LIST_BALANCED_MAYBE_REF(z, i, data) BOOST_PP_COMMA_IF(i) typename MaybeRef<data##i>::Type
33 
34 // TODO: We allow optional parameters when the C++ type can be converted from JS::UndefinedValue.
35 // FromJSVal is expected to either set a##i or return false (otherwise we could get undefined
36 // behaviour because some types have undefined values when not being initialized).
37 // This is not very clear and also a bit fragile. Another problem is that the error reporting lacks
38 // a bit. SpiderMonkey will throw a JS exception and abort the execution of the current function when
39 // we return false here (without printing a callstack or additional detail telling that an argument
40 // conversion failed). So we have two TODOs here:
41 // 1. On the conceptual side: How to consistently work with optional parameters (or drop them completely?)
42 // 2. On the technical side: Improve error handling, find a better way to ensure parameters are initialized
43 #define CONVERT_ARG(z, i, data) \
44  bool typeConvRet##i; \
45  T##i a##i = ScriptInterface::AssignOrFromJSVal<T##i>( \
46  cx, \
47  i < args.length() ? args[i] : JS::UndefinedHandleValue, \
48  typeConvRet##i); \
49  if (!typeConvRet##i) return false;
50 
51 // List-generating macros, named roughly after their first list item
52 #define TYPENAME_T0_HEAD(z, i) BOOST_PP_REPEAT_##z (i, NUMBERED_LIST_HEAD, typename T) // "typename T0, typename T1, "
53 #define T0(z, i) BOOST_PP_REPEAT_##z (i, NUMBERED_LIST_BALANCED, T) // "T0, T1"
54 #define T0_MAYBE_REF(z, i) BOOST_PP_REPEAT_##z (i, NUMBERED_LIST_BALANCED_MAYBE_REF, T) // "const T0&, T1"
55 #define T0_TAIL(z, i) BOOST_PP_REPEAT_##z (i, NUMBERED_LIST_TAIL, T) // ", T0, T1"
56 #define T0_TAIL_MAYBE_REF(z, i) BOOST_PP_REPEAT_##z (i, NUMBERED_LIST_TAIL_MAYBE_REF, T) // ", const T0&, T1"
57 #define A0_TAIL(z, i) BOOST_PP_REPEAT_##z (i, NUMBERED_LIST_TAIL, a) // ", a0, a1"
58 
59 // Define RegisterFunction<TR, T0..., f>
60 #define OVERLOADS(z, i, data) \
61  template <typename R, TYPENAME_T0_HEAD(z,i) R (*fptr) ( ScriptInterface::CxPrivate* T0_TAIL_MAYBE_REF(z,i) )> \
62  void RegisterFunction(const char* name) { \
63  Register(name, call<R T0_TAIL(z,i), fptr>, nargs<T0(z,i)>()); \
64  }
65 BOOST_PP_REPEAT(SCRIPT_INTERFACE_MAX_ARGS, OVERLOADS, ~)
66 #undef OVERLOADS
67 
68 // JSFastNative-compatible function that wraps the function identified in the template argument list
69 // (Definition comes later, since it depends on some things we haven't defined yet)
70 #define OVERLOADS(z, i, data) \
71  template <typename R, TYPENAME_T0_HEAD(z,i) R (*fptr) ( ScriptInterface::CxPrivate* T0_TAIL_MAYBE_REF(z,i) )> \
72  static bool call(JSContext* cx, uint argc, jsval* vp);
73 BOOST_PP_REPEAT(SCRIPT_INTERFACE_MAX_ARGS, OVERLOADS, ~)
74 #undef OVERLOADS
75 
76 // Similar, for class methods
77 #define OVERLOADS(z, i, data) \
78  template <typename R, TYPENAME_T0_HEAD(z,i) JSClass* CLS, typename TC, R (TC::*fptr) ( T0_MAYBE_REF(z,i) )> \
79  static bool callMethod(JSContext* cx, uint argc, jsval* vp);
80 BOOST_PP_REPEAT(SCRIPT_INTERFACE_MAX_ARGS, OVERLOADS, ~)
81 #undef OVERLOADS
82 
83 // const methods
84 #define OVERLOADS(z, i, data) \
85  template <typename R, TYPENAME_T0_HEAD(z,i) JSClass* CLS, typename TC, R (TC::*fptr) ( T0_MAYBE_REF(z,i) ) const> \
86  static bool callMethodConst(JSContext* cx, uint argc, jsval* vp);
87 BOOST_PP_REPEAT(SCRIPT_INTERFACE_MAX_ARGS, OVERLOADS, ~)
88 #undef OVERLOADS
89 
90 // Argument-number counter
91 template<typename... Ts>
92 static size_t nargs() { return sizeof...(Ts); }
93 
94 // Call the named property on the given object
95 template<typename R, typename... Ts>
96 bool CallFunction(JS::HandleValue val, const char* name, R& ret, const Ts&... params) const;
97 
98 // Implicit conversion from JS::Rooted<R>* to JS::MutableHandle<R> does not work with template argument deduction
99 // (only exact type matches allowed). We need this overload to allow passing Rooted<R>* using the & operator
100 // (as people would expect it to work based on the SpiderMonkey rooting guide).
101 template<typename R, typename... Ts>
102 bool CallFunction(JS::HandleValue val, const char* name, JS::Rooted<R>* ret, const Ts&... params) const;
103 
104 // This overload is for the case when a JS::MutableHandle<R> type gets passed into CallFunction directly and
105 // without requiring implicit conversion.
106 template<typename R, typename... Ts>
107 bool CallFunction(JS::HandleValue val, const char* name, JS::MutableHandle<R> ret, const Ts&... params) const;
108 
109 // Call the named property on the given object, with void return type
110 template<typename... Ts> \
111 bool CallFunctionVoid(JS::HandleValue val, const char* name, const Ts&... params) const;
Definition: NativeWrapperDecls.h:23
#define SCRIPT_INTERFACE_MAX_ARGS
Definition: ScriptInterface.h:50
#define R(t)
bool CallFunctionVoid(JS::HandleValue val, const char *name, const Ts &...params) const
bool CallFunction(JS::HandleValue val, const char *name, R &ret, const Ts &...params) const
static size_t nargs()
Definition: NativeWrapperDecls.h:92
#define OVERLOADS(z, i, data)
Definition: ScriptInterface.h:85