Pyrogenesis  trunk
ScriptConversions.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 #ifndef INCLUDED_SCRIPTCONVERSIONS
19 #define INCLUDED_SCRIPTCONVERSIONS
20 
21 #include "ScriptInterface.h"
22 #include "scriptinterface/ScriptExtraHeaders.h" // for typed arrays
23 
24 #include <limits>
25 
26 template<typename T> static void ToJSVal_vector(JSContext* cx, JS::MutableHandleValue ret, const std::vector<T>& val)
27 {
28  JSAutoRequest rq(cx);
29  JS::RootedObject obj(cx, JS_NewArrayObject(cx, 0));
30  if (!obj)
31  {
32  ret.setUndefined();
33  return;
34  }
35 
36  ENSURE(val.size() <= std::numeric_limits<u32>::max());
37  for (u32 i = 0; i < val.size(); ++i)
38  {
39  JS::RootedValue el(cx);
40  ScriptInterface::ToJSVal<T>(cx, &el, val[i]);
41  JS_SetElement(cx, obj, i, el);
42  }
43  ret.setObject(*obj);
44 }
45 
46 #define FAIL(msg) STMT(JS_ReportError(cx, msg); return false)
47 
48 template<typename T> static bool FromJSVal_vector(JSContext* cx, JS::HandleValue v, std::vector<T>& out)
49 {
50  JSAutoRequest rq(cx);
51  JS::RootedObject obj(cx);
52  if (!v.isObject())
53  FAIL("Argument must be an array");
54 
55  obj = &v.toObject();
56  if (!(JS_IsArrayObject(cx, obj) || JS_IsTypedArrayObject(obj)))
57  FAIL("Argument must be an array");
58 
59  u32 length;
60  if (!JS_GetArrayLength(cx, obj, &length))
61  FAIL("Failed to get array length");
62 
63  out.reserve(length);
64  for (u32 i = 0; i < length; ++i)
65  {
66  JS::RootedValue el(cx);
67  if (!JS_GetElement(cx, obj, i, &el))
68  FAIL("Failed to read array element");
69  T el2;
70  if (!ScriptInterface::FromJSVal<T>(cx, el, el2))
71  return false;
72  out.push_back(el2);
73  }
74  return true;
75 }
76 
77 #undef FAIL
78 
79 #define JSVAL_VECTOR(T) \
80 template<> void ScriptInterface::ToJSVal<std::vector<T> >(JSContext* cx, JS::MutableHandleValue ret, const std::vector<T>& val) \
81 { \
82  ToJSVal_vector(cx, ret, val); \
83 } \
84 template<> bool ScriptInterface::FromJSVal<std::vector<T> >(JSContext* cx, JS::HandleValue v, std::vector<T>& out) \
85 { \
86  return FromJSVal_vector(cx, v, out); \
87 }
88 
89 template<typename T> static bool FromJSProperty(JSContext* cx, JS::HandleValue v, const char* name, T& out)
90 {
91  if (!v.isObject())
92  return false;
93 
94  JSAutoRequest rq(cx);
95  JS::RootedObject obj(cx, &v.toObject());
96 
97  bool hasProperty;
98  if (!JS_HasProperty(cx, obj, name, &hasProperty))
99  return false;
100 
101  JS::RootedValue value(cx);
102  if (!hasProperty || !JS_GetProperty(cx, obj, name, &value))
103  return false;
104 
105  if (!ScriptInterface::FromJSVal(cx, value, out))
106  return false;
107 
108  return true;
109 }
110 
111 #endif //INCLUDED_SCRIPTCONVERSIONS
static void out(const wchar_t *fmt,...)
Definition: wdbg_sym.cpp:419
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
Definition: debug.h:287
uint32_t u32
Definition: types.h:39
#define FAIL(msg)
Definition: ScriptConversions.h:46
static bool FromJSVal_vector(JSContext *cx, JS::HandleValue v, std::vector< T > &out)
Definition: ScriptConversions.h:48
#define T(string_literal)
Definition: secure_crt.cpp:76
static bool FromJSProperty(JSContext *cx, JS::HandleValue v, const char *name, T &out)
Definition: ScriptConversions.h:89
static void ToJSVal_vector(JSContext *cx, JS::MutableHandleValue ret, const std::vector< T > &val)
Definition: ScriptConversions.h:26
static bool FromJSVal(JSContext *cx, const JS::HandleValue val, T &ret)
Convert a jsval to a C++ type.