forked from mirrors/0ad
Add support for const methods in components and make those that can be const const.
Reviewed By: Itms Differential Revision: https://code.wildfiregames.com/D75 This was SVN commit r19156.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
/* Copyright (C) 2016 Wildfire Games.
|
||||
/* Copyright (C) 2017 Wildfire Games.
|
||||
* This file is part of 0 A.D.
|
||||
*
|
||||
* 0 A.D. is free software: you can redistribute it and/or modify
|
||||
@@ -33,7 +33,7 @@ template <typename T> struct MaybeRef;
|
||||
// Some other things
|
||||
#define TYPED_ARGS(z, i, data) , T##i a##i
|
||||
#define TYPED_ARGS_MAYBE_REF(z, i, data) , typename MaybeRef<T##i>::Type a##i
|
||||
#define TYPED_ARGS_CONST_REF(z, i, data) const T##i& a##i,
|
||||
#define TYPED_ARGS_CONST_REF(z, i, data) , const T##i& a##i
|
||||
|
||||
// TODO: We allow optional parameters when the C++ type can be converted from JS::UndefinedValue.
|
||||
// FromJSVal is expected to either set a##i or return false (otherwise we could get undefined
|
||||
@@ -62,7 +62,7 @@ template <typename T> struct MaybeRef;
|
||||
#define T0_TAIL_MAYBE_REF(z, i) BOOST_PP_REPEAT_##z (i, NUMBERED_LIST_TAIL_MAYBE_REF, T) // ", const T0&, T1"
|
||||
#define T0_A0(z, i) BOOST_PP_REPEAT_##z (i, TYPED_ARGS, ~) // ",T0 a0, T1 a1"
|
||||
#define T0_A0_MAYBE_REF(z, i) BOOST_PP_REPEAT_##z (i, TYPED_ARGS_MAYBE_REF, ~) // ",const T0& a0, T1 a1"
|
||||
#define T0_A0_CONST_REF(z, i) BOOST_PP_REPEAT_##z (i, TYPED_ARGS_CONST_REF, ~) // ", const T0& a0, const T1& a1, "
|
||||
#define T0_A0_TAIL_CONST_REF(z, i) BOOST_PP_REPEAT_##z (i, TYPED_ARGS_CONST_REF, ~) // ", const T0& a0, const T1& a1"
|
||||
#define A0(z, i) BOOST_PP_REPEAT_##z (i, NUMBERED_LIST_BALANCED, a) // "a0, a1"
|
||||
#define A0_TAIL(z, i) BOOST_PP_REPEAT_##z (i, NUMBERED_LIST_TAIL, a) // ", a0, a1"
|
||||
|
||||
@@ -90,6 +90,13 @@ BOOST_PP_REPEAT(SCRIPT_INTERFACE_MAX_ARGS, OVERLOADS, ~)
|
||||
BOOST_PP_REPEAT(SCRIPT_INTERFACE_MAX_ARGS, OVERLOADS, ~)
|
||||
#undef OVERLOADS
|
||||
|
||||
// const methods
|
||||
#define OVERLOADS(z, i, data) \
|
||||
template <typename R, TYPENAME_T0_HEAD(z,i) JSClass* CLS, typename TC, R (TC::*fptr) ( T0_MAYBE_REF(z,i) ) const> \
|
||||
static bool callMethodConst(JSContext* cx, uint argc, jsval* vp);
|
||||
BOOST_PP_REPEAT(SCRIPT_INTERFACE_MAX_ARGS, OVERLOADS, ~)
|
||||
#undef OVERLOADS
|
||||
|
||||
// Argument-number counter
|
||||
#define OVERLOADS(z, i, data) \
|
||||
template <int dummy TYPENAME_T0_TAIL(z,i)> /* add a dummy parameter so we still compile with 0 template args */ \
|
||||
@@ -100,7 +107,7 @@ BOOST_PP_REPEAT(SCRIPT_INTERFACE_MAX_ARGS, OVERLOADS, ~)
|
||||
// Call the named property on the given object
|
||||
#define OVERLOADS(z, i, data) \
|
||||
template <typename R TYPENAME_T0_TAIL(z, i)> \
|
||||
bool CallFunction(JS::HandleValue val, const char* name, T0_A0_CONST_REF(z,i) R& ret);
|
||||
bool CallFunction(JS::HandleValue val, const char* name T0_A0_TAIL_CONST_REF(z,i), R& ret) const;
|
||||
BOOST_PP_REPEAT(SCRIPT_INTERFACE_MAX_ARGS, OVERLOADS, ~)
|
||||
#undef OVERLOADS
|
||||
|
||||
@@ -109,7 +116,7 @@ BOOST_PP_REPEAT(SCRIPT_INTERFACE_MAX_ARGS, OVERLOADS, ~)
|
||||
// (as people would expect it to work based on the SpiderMonkey rooting guide).
|
||||
#define OVERLOADS(z, i, data) \
|
||||
template <typename R TYPENAME_T0_TAIL(z, i)> \
|
||||
bool CallFunction(JS::HandleValue val, const char* name, T0_A0_CONST_REF(z,i) JS::Rooted<R>* ret);
|
||||
bool CallFunction(JS::HandleValue val, const char* name T0_A0_TAIL_CONST_REF(z,i), JS::Rooted<R>* ret) const;
|
||||
BOOST_PP_REPEAT(SCRIPT_INTERFACE_MAX_ARGS, OVERLOADS, ~)
|
||||
#undef OVERLOADS
|
||||
|
||||
@@ -117,7 +124,7 @@ BOOST_PP_REPEAT(SCRIPT_INTERFACE_MAX_ARGS, OVERLOADS, ~)
|
||||
// without requiring implicit conversion.
|
||||
#define OVERLOADS(z, i, data) \
|
||||
template <typename R TYPENAME_T0_TAIL(z, i)> \
|
||||
bool CallFunction(JS::HandleValue val, const char* name, T0_A0_CONST_REF(z,i) JS::MutableHandle<R> ret);
|
||||
bool CallFunction(JS::HandleValue val, const char* name T0_A0_TAIL_CONST_REF(z,i), JS::MutableHandle<R> ret) const;
|
||||
BOOST_PP_REPEAT(SCRIPT_INTERFACE_MAX_ARGS, OVERLOADS, ~)
|
||||
#undef OVERLOADS
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright (C) 2016 Wildfire Games.
|
||||
/* Copyright (C) 2017 Wildfire Games.
|
||||
* This file is part of 0 A.D.
|
||||
*
|
||||
* 0 A.D. is free software: you can redistribute it and/or modify
|
||||
@@ -120,7 +120,8 @@ struct ScriptInterface_NativeMethodWrapper<void, TC> {
|
||||
// JSFastNative-compatible function that wraps the function identified in the template argument list
|
||||
#define OVERLOADS(z, i, data) \
|
||||
template <typename R, TYPENAME_T0_HEAD(z,i) R (*fptr) ( ScriptInterface::CxPrivate* T0_TAIL_MAYBE_REF(z,i) )> \
|
||||
bool ScriptInterface::call(JSContext* cx, uint argc, jsval* vp) { \
|
||||
bool ScriptInterface::call(JSContext* cx, uint argc, jsval* vp) \
|
||||
{ \
|
||||
JS::CallArgs args = JS::CallArgsFromVp(argc, vp); \
|
||||
JSAutoRequest rq(cx); \
|
||||
BOOST_PP_REPEAT_##z (i, CONVERT_ARG, ~) \
|
||||
@@ -135,7 +136,8 @@ BOOST_PP_REPEAT(SCRIPT_INTERFACE_MAX_ARGS, OVERLOADS, ~)
|
||||
// Same idea but for methods
|
||||
#define OVERLOADS(z, i, data) \
|
||||
template <typename R, TYPENAME_T0_HEAD(z,i) JSClass* CLS, typename TC, R (TC::*fptr) ( T0_MAYBE_REF(z,i) )> \
|
||||
bool ScriptInterface::callMethod(JSContext* cx, uint argc, jsval* vp) { \
|
||||
bool ScriptInterface::callMethod(JSContext* cx, uint argc, jsval* vp) \
|
||||
{ \
|
||||
JS::CallArgs args = JS::CallArgsFromVp(argc, vp); \
|
||||
JSAutoRequest rq(cx); \
|
||||
JS::RootedObject thisObj(cx, JS_THIS_OBJECT(cx, vp)); \
|
||||
@@ -151,11 +153,31 @@ BOOST_PP_REPEAT(SCRIPT_INTERFACE_MAX_ARGS, OVERLOADS, ~)
|
||||
BOOST_PP_REPEAT(SCRIPT_INTERFACE_MAX_ARGS, OVERLOADS, ~)
|
||||
#undef OVERLOADS
|
||||
|
||||
// const methods
|
||||
#define OVERLOADS(z, i, data) \
|
||||
template <typename R, TYPENAME_T0_HEAD(z,i) JSClass* CLS, typename TC, R (TC::*fptr) ( T0_MAYBE_REF(z,i) ) const> \
|
||||
bool ScriptInterface::callMethodConst(JSContext* cx, uint argc, jsval* vp) \
|
||||
{ \
|
||||
JS::CallArgs args = JS::CallArgsFromVp(argc, vp); \
|
||||
JSAutoRequest rq(cx); \
|
||||
JS::RootedObject thisObj(cx, JS_THIS_OBJECT(cx, vp)); \
|
||||
if (ScriptInterface::GetClass(thisObj) != CLS) return false; \
|
||||
TC* c = static_cast<TC*>(ScriptInterface::GetPrivate(thisObj)); \
|
||||
if (! c) return false; \
|
||||
BOOST_PP_REPEAT_##z (i, CONVERT_ARG, ~) \
|
||||
JS::RootedValue rval(cx); \
|
||||
ScriptInterface_NativeMethodWrapper<R, TC>::template call<T0_HEAD(z,i) R (TC::*)(T0_MAYBE_REF(z,i)) const>(cx, &rval, c, fptr A0_TAIL(z,i)); \
|
||||
args.rval().set(rval); \
|
||||
return !ScriptInterface::IsExceptionPending(cx); \
|
||||
}
|
||||
BOOST_PP_REPEAT(SCRIPT_INTERFACE_MAX_ARGS, OVERLOADS, ~)
|
||||
#undef OVERLOADS
|
||||
|
||||
#define ASSIGN_OR_TO_JS_VAL(z, i, data) AssignOrToJSVal(cx, argv[i], a##i);
|
||||
|
||||
#define OVERLOADS(z, i, data) \
|
||||
template<typename R TYPENAME_T0_TAIL(z, i)> \
|
||||
bool ScriptInterface::CallFunction(JS::HandleValue val, const char* name, T0_A0_CONST_REF(z,i) R& ret) \
|
||||
bool ScriptInterface::CallFunction(JS::HandleValue val, const char* name T0_A0_TAIL_CONST_REF(z,i), R& ret) const \
|
||||
{ \
|
||||
JSContext* cx = GetContext(); \
|
||||
JSAutoRequest rq(cx); \
|
||||
@@ -173,7 +195,7 @@ BOOST_PP_REPEAT(SCRIPT_INTERFACE_MAX_ARGS, OVERLOADS, ~)
|
||||
|
||||
#define OVERLOADS(z, i, data) \
|
||||
template<typename R TYPENAME_T0_TAIL(z, i)> \
|
||||
bool ScriptInterface::CallFunction(JS::HandleValue val, const char* name, T0_A0_CONST_REF(z,i) JS::Rooted<R>* ret) \
|
||||
bool ScriptInterface::CallFunction(JS::HandleValue val, const char* name T0_A0_TAIL_CONST_REF(z,i), JS::Rooted<R>* ret) const \
|
||||
{ \
|
||||
JSContext* cx = GetContext(); \
|
||||
JSAutoRequest rq(cx); \
|
||||
@@ -181,17 +203,14 @@ bool ScriptInterface::CallFunction(JS::HandleValue val, const char* name, T0_A0_
|
||||
JS::AutoValueVector argv(cx); \
|
||||
argv.resize(i); \
|
||||
BOOST_PP_REPEAT_##z (i, ASSIGN_OR_TO_JS_VAL, ~) \
|
||||
bool ok = CallFunction_(val, name, argv, jsRet); \
|
||||
if (!ok) \
|
||||
return false; \
|
||||
return true; \
|
||||
return CallFunction_(val, name, argv, jsRet); \
|
||||
}
|
||||
BOOST_PP_REPEAT(SCRIPT_INTERFACE_MAX_ARGS, OVERLOADS, ~)
|
||||
#undef OVERLOADS
|
||||
|
||||
#define OVERLOADS(z, i, data) \
|
||||
template<typename R TYPENAME_T0_TAIL(z, i)> \
|
||||
bool ScriptInterface::CallFunction(JS::HandleValue val, const char* name, T0_A0_CONST_REF(z,i) JS::MutableHandle<R> ret) \
|
||||
bool ScriptInterface::CallFunction(JS::HandleValue val, const char* name T0_A0_TAIL_CONST_REF(z,i), JS::MutableHandle<R> ret) const \
|
||||
{ \
|
||||
JSContext* cx = GetContext(); \
|
||||
JSAutoRequest rq(cx); \
|
||||
@@ -226,6 +245,6 @@ BOOST_PP_REPEAT(SCRIPT_INTERFACE_MAX_ARGS, OVERLOADS, ~)
|
||||
#undef T0_TAIL_MAYBE_REF
|
||||
#undef T0_A0
|
||||
#undef T0_A0_MAYBE_REF
|
||||
#undef T0_A0_CONST_REF
|
||||
#undef T0_A0_TAIL_CONST_REF
|
||||
#undef A0
|
||||
#undef A0_TAIL
|
||||
|
||||
@@ -569,7 +569,8 @@ bool ScriptInterface::CallFunctionVoid(JS::HandleValue val, const char* name)
|
||||
return CallFunction_(val, name, JS::HandleValueArray::empty(), &jsRet);
|
||||
}
|
||||
|
||||
bool ScriptInterface::CallFunction_(JS::HandleValue val, const char* name, JS::HandleValueArray argv, JS::MutableHandleValue ret)
|
||||
|
||||
bool ScriptInterface::CallFunction_(JS::HandleValue val, const char* name, JS::HandleValueArray argv, JS::MutableHandleValue ret) const
|
||||
{
|
||||
JSAutoRequest rq(m->m_cx);
|
||||
JS::RootedObject obj(m->m_cx);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright (C) 2016 Wildfire Games.
|
||||
/* Copyright (C) 2017 Wildfire Games.
|
||||
* This file is part of 0 A.D.
|
||||
*
|
||||
* 0 A.D. is free software: you can redistribute it and/or modify
|
||||
@@ -379,7 +379,7 @@ public:
|
||||
|
||||
private:
|
||||
|
||||
bool CallFunction_(JS::HandleValue val, const char* name, JS::HandleValueArray argv, JS::MutableHandleValue ret);
|
||||
bool CallFunction_(JS::HandleValue val, const char* name, JS::HandleValueArray argv, JS::MutableHandleValue ret) const;
|
||||
bool Eval_(const char* code, JS::MutableHandleValue ret);
|
||||
bool Eval_(const wchar_t* code, JS::MutableHandleValue ret);
|
||||
bool SetGlobal_(const char* name, JS::HandleValue value, bool replace);
|
||||
@@ -440,6 +440,9 @@ public:
|
||||
// template <R, T0..., JSClass*, TC, TR (TC:*fptr) (T0...)>
|
||||
// static JSNative callMethod;
|
||||
//
|
||||
// template <R, T0..., JSClass*, TC, TR (TC:*fptr) const (T0...)>
|
||||
// static JSNative callMethodConst;
|
||||
//
|
||||
// template <dummy, T0...>
|
||||
// static size_t nargs();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user