File: FunctionWrapper.h

package info (click to toggle)
0ad 0.27.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 173,296 kB
  • sloc: cpp: 194,003; javascript: 19,098; ansic: 15,066; python: 6,328; sh: 1,699; perl: 1,575; java: 533; xml: 482; php: 192; makefile: 99
file content (458 lines) | stat: -rw-r--r-- 17,490 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/* Copyright (C) 2024 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * 0 A.D. is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef INCLUDED_FUNCTIONWRAPPER
#define INCLUDED_FUNCTIONWRAPPER

#include "ScriptConversions.h"
#include "ScriptExceptions.h"
#include "ScriptRequest.h"

#include <fmt/format.h>
#include <tuple>
#include <type_traits>
#include <stdexcept>
#include <utility>

class ScriptInterface;

/**
 * This class introduces templates to conveniently wrap C++ functions in JSNative functions.
 * This _is_ rather template heavy, so compilation times beware.
 * The C++ code can have arbitrary arguments and arbitrary return types, so long
 * as they can be converted to/from JS using Script::ToJSVal (FromJSVal respectively),
 * and they are default-constructible (TODO: that can probably changed).
 * (This could be a namespace, but I like being able to specify public/private).
 */
class ScriptFunction
{
private:
	ScriptFunction() = delete;
	ScriptFunction(const ScriptFunction&) = delete;
	ScriptFunction(ScriptFunction&&) = delete;

	/**
	 * 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 ScriptRequest& (as the first argument only, for implementation simplicity).
	 *  - const ScriptInterface& (as the first argument only, for implementation simplicity).
	 *  - JS::HandleValue
	 */
	template<typename T>
	using type_transform = std::conditional_t<
		std::is_same_v<const ScriptRequest&, T> || std::is_same_v<const ScriptInterface&, T>,
		T,
		std::remove_const_t<typename std::remove_reference_t<T>>
	>;

	/**
	 * Convenient struct to get info on a [class] [const] function pointer.
	 * TODO VS19: I ran into a really weird bug with an auto specialisation on this taking function pointers.
	 * It'd be good to add it back once we upgrade.
	 */
	template <class T> struct args_info;

	template<typename R, typename ...Types>
	struct args_info<R(*)(Types ...)>
	{
		static constexpr const size_t nb_args = sizeof...(Types);
		using return_type = R;
		using object_type = void;
		using arg_types = std::tuple<type_transform<Types>...>;
	};

	template<typename C, typename R, typename ...Types>
	struct args_info<R(C::*)(Types ...)> : public args_info<R(*)(Types ...)> { using object_type = C; };
	template<typename C, typename R, typename ...Types>
	struct args_info<R(C::*)(Types ...) const> : public args_info<R(C::*)(Types ...)> {};

	struct IteratorResultError : std::runtime_error
	{
		IteratorResultError(const std::string& property) :
			IteratorResultError{property.c_str()}
		{}
		IteratorResultError(const char* property) :
			std::runtime_error{fmt::format("Failed to get `{}` from an `IteratorResult`.", property)}
		{}
		using std::runtime_error::runtime_error;
	};

	///////////////////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////

	/**
	 * 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.
	 * @param wentOk - true if the conversion succeeded and wentOk was true before, false otherwise.
	 */
	template<size_t idx, typename T>
	static T DoConvertFromJS(const ScriptRequest& rq, JS::CallArgs& args, bool& wentOk)
	{
		// No need to convert JS values.
		if constexpr (std::is_same_v<T, JS::HandleValue>)
		{
			// Default-construct values that aren't passed by JS.
			// TODO: this should perhaps be removed, as it's distinct from C++ default values and kind of tricky.
			if (idx >= args.length())
				return JS::UndefinedHandleValue;
			else
			{
				// GCC (at least < 9) & VS17 prints warnings if arguments are not used in some constexpr branch.
				UNUSED2(rq); UNUSED2(args); UNUSED2(wentOk);
				return args[idx]; // This passes the null handle value if idx is beyond the length of args.
			}
		}
		else
		{
			// Default-construct values that aren't passed by JS.
			// TODO: this should perhaps be removed, as it's distinct from C++ default values and kind of tricky.
			if (idx >= args.length())
				return {};
			else
			{
				T ret;
				wentOk &= Script::FromJSVal<T>(rq, args[idx], ret);
				return ret;
			}
		}
	}

	/**
	 * Wrapper: calls DoConvertFromJS for each element in T.
	 */
	template<typename... T, size_t... idx>
	static std::tuple<T...> DoConvertFromJS(std::index_sequence<idx...>, const ScriptRequest& rq,
		JS::CallArgs& args, bool& wentOk)
	{
		return {DoConvertFromJS<idx, T>(rq, args, wentOk)...};
	}

	/**
	 * ConvertFromJS is a wrapper around DoConvertFromJS, and handles specific cases for the
	 * first argument (ScriptRequest, ...).
	 *
	 * 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.
	 */
	template<typename ...Types>
	static std::tuple<Types...> ConvertFromJS(const ScriptRequest& rq, JS::CallArgs& args, bool& wentOk,
		std::tuple<Types...>*)
	{
		return DoConvertFromJS<Types...>(std::index_sequence_for<Types...>(), rq, args, wentOk);
	}

	// Overloads for ScriptRequest& first argument.
	template<typename ...Types>
	static std::tuple<const ScriptRequest&, Types...> ConvertFromJS(const ScriptRequest& rq,
		JS::CallArgs& args, bool& wentOk, std::tuple<const ScriptRequest&, Types...>*)
	{
		return std::tuple_cat(std::tie(rq), DoConvertFromJS<Types...>(
			std::index_sequence_for<Types...>(), rq, args, wentOk));
	}

	// Overloads for ScriptInterface& first argument.
	template<typename ...Types>
	static std::tuple<const ScriptInterface&, Types...> ConvertFromJS(const ScriptRequest& rq,
		JS::CallArgs& args, bool& wentOk, std::tuple<const ScriptInterface&, Types...>*)
	{
		return std::tuple_cat(std::tie(rq.GetScriptInterface()),
			DoConvertFromJS<Types...>(std::index_sequence_for<Types...>(), rq, args, wentOk));
	}

	///////////////////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////

	/**
	 * Wrap std::apply for the case where we have an object method or a regular function.
	 */
	template <auto callable, typename T, typename tuple>
	static typename args_info<decltype(callable)>::return_type call(T* object, tuple& args)
	{
		if constexpr(std::is_same_v<T, void>)
		{
			// GCC (at least < 9) & VS17 prints warnings if arguments are not used in some constexpr branch.
			UNUSED2(object);
			return std::apply(callable, args);
		}
		else
			return std::apply(callable, std::tuple_cat(std::forward_as_tuple(*object), args));
	}

	///////////////////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////

	struct IgnoreResult_t {};
	static inline IgnoreResult_t IgnoreResult;

	/**
	 * 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.
	 */
	template<typename... Types, size_t... idx>
	static void ToJSValVector(std::index_sequence<idx...>, const ScriptRequest& rq,
		[[maybe_unused]] JS::MutableHandleValueVector argv, const Types&... params)
	{
		(Script::ToJSVal(rq, argv[idx], params), ...);
	}

	/**
	 * 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.
	 */
	template<typename R, typename ...Args>
	static bool Call_(const ScriptRequest& rq, JS::HandleValue val, const char* name, R& ret, const Args&... args)
	{
		JS::RootedObject obj(rq.cx);
		if (!JS_ValueToObject(rq.cx, val, &obj) || !obj)
			return false;

		// Fetch the property explicitly - this avoids converting the arguments if it doesn't exist.
		JS::RootedValue func(rq.cx);
		if (!JS_GetProperty(rq.cx, obj, name, &func) || func.isUndefined())
			return false;

		JS::RootedValueVector argv(rq.cx);
		ignore_result(argv.resize(sizeof...(Args)));
		ToJSValVector(std::index_sequence_for<Args...>{}, rq, &argv, args...);

		bool success;
		if constexpr (std::is_same_v<R, JS::MutableHandleValue>)
			success = JS_CallFunctionValue(rq.cx, obj, func, argv, ret);
		else
		{
			JS::RootedValue jsRet(rq.cx);
			success = JS_CallFunctionValue(rq.cx, obj, func, argv, &jsRet);
			if constexpr (!std::is_same_v<R, IgnoreResult_t>)
			{
				if (success)
					Script::FromJSVal(rq, jsRet, ret);
			}
			else
				UNUSED2(ret); // VS2017 complains.
		}
		// Even if everything succeeded, there could be pending exceptions
		return !ScriptException::CatchPending(rq) && success;
	}

	///////////////////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////
public:
	template <typename T>
	using ObjectGetter = T*(*)(const ScriptRequest&, JS::CallArgs&);

	// TODO: the fact that this takes class and not auto is to work around an odd VS17 bug.
	// It can be removed with VS19.
	template <class callableType>
	using GetterFor = ObjectGetter<typename args_info<callableType>::object_type>;

	/**
	 * 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 ScriptRequest& or ScriptInterface& as its first argument.
	 * The function may be an object method, in which case you need to pass an appropriate getter
	 *
	 * Optimisation note: the ScriptRequest object is created even without arguments,
	 * as it's necessary for IsExceptionPending.
	 *
	 * @param thisGetter to get the object, if necessary.
	 */
	template <auto callable, GetterFor<decltype(callable)> thisGetter = nullptr>
	static bool ToJSNative(JSContext* cx, unsigned argc, JS::Value* vp)
	{
		using ObjType = typename args_info<decltype(callable)>::object_type;

		JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
		ScriptRequest rq(cx);

		// If the callable is an object method, we must specify how to fetch the object.
		static_assert(std::is_same_v<typename args_info<decltype(callable)>::object_type, void> || thisGetter != nullptr,
					  "ScriptFunction::Register - No getter specified for object method");

// GCC 7 triggers spurious warnings
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Waddress"
#endif
		ObjType* obj = nullptr;
		if constexpr (thisGetter != nullptr)
		{
			obj = thisGetter(rq, args);
			if (!obj)
				return false;
		}
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif

		bool wentOk = true;
		typename args_info<decltype(callable)>::arg_types outs = ConvertFromJS(rq, args, wentOk,
			static_cast<typename args_info<decltype(callable)>::arg_types*>(nullptr));
		if (!wentOk)
			return false;

		/**
		 * TODO: error handling isn't standard, and since this can call any C++ function,
		 * there's no simple obvious way to deal with it.
		 * For now we check for pending JS exceptions, but it would probably be nicer
		 * to standardise on something, or perhaps provide an "errorHandler" here.
		 */
		if constexpr (std::is_same_v<void, typename args_info<decltype(callable)>::return_type>)
			call<callable>(obj, outs);
		else if constexpr (std::is_same_v<JS::Value, typename args_info<decltype(callable)>::return_type>)
			args.rval().set(call<callable>(obj, outs));
		else
			Script::ToJSVal(rq, args.rval(), call<callable>(obj, outs));

		return !ScriptException::IsPending(rq);
	}

	/**
	 * Call a JS function @a name, property of object @a val, with the arguments @a args.
	 * @a ret will be updated with the return value, if any.
	 * @return the success (or failure) thereof.
	 */
	template<typename R, typename ...Args>
	static bool Call(const ScriptRequest& rq, JS::HandleValue val, const char* name, R& ret, const Args&... args)
	{
		return Call_(rq, val, name, ret, std::forward<const Args>(args)...);
	}

	// Specialisation for MutableHandleValue return.
	template<typename ...Args>
	static bool Call(const ScriptRequest& rq, JS::HandleValue val, const char* name, JS::MutableHandleValue ret, const Args&... args)
	{
		return Call_(rq, val, name, ret, std::forward<const Args>(args)...);
	}

	/**
	 * Call a JS function @a name, property of object @a val, with the arguments @a args.
	 * @return the success (or failure) thereof.
	 */
	template<typename ...Args>
	static bool CallVoid(const ScriptRequest& rq, JS::HandleValue val, const char* name, const Args&... args)
	{
		return Call(rq, val, name, IgnoreResult, std::forward<const Args>(args)...);
	}

	/**
	 * Call a JS function @a name, property of object @a val, with the argument @a args. Repeatetly
	 * invokes @a yieldCallback with the yielded value.
	 * @return the final value of the generator.
	 */
	template<typename Callback>
	static JS::Value RunGenerator(const ScriptRequest& rq, JS::HandleValue val, const char* name,
		JS::HandleValue arg, Callback yieldCallback)
	{
		JS::RootedValue generator{rq.cx};
		if (!ScriptFunction::Call(rq, val, name, &generator, arg))
			throw std::runtime_error{fmt::format("Failed to call the generator `{}`.", name)};

		const auto continueGenerator = [&](const char* property, auto... args) -> JS::Value
			{
				JS::RootedValue iteratorResult{rq.cx};
				if (!ScriptFunction::Call(rq, generator, property, &iteratorResult, args...))
					throw std::runtime_error{fmt::format("Failed to call `{}`.", name)};
				return iteratorResult;
			};

		JS::PersistentRootedValue error{rq.cx, JS::UndefinedValue()};
		while (true)
		{
			JS::RootedValue iteratorResult{rq.cx, error.isUndefined() ? continueGenerator("next") :
				continueGenerator("throw", std::exchange(error, JS::UndefinedValue()))};

			try
			{
				JS::RootedObject iteratorResultObject{rq.cx, &iteratorResult.toObject()};

				bool done;
				if (!Script::FromJSProperty(rq, iteratorResult, "done", done, true))
					throw IteratorResultError{"done"};

				JS::RootedValue value{rq.cx};
				if (!JS_GetProperty(rq.cx, iteratorResultObject, "value", &value))
					throw IteratorResultError{"value"};

				if (done)
					return value;

				yieldCallback(value);
			}
			catch (const std::exception& e)
			{
				JS::RootedValue global{rq.cx, rq.globalValue()};
				if (!ScriptFunction::Call(rq, global, "Error", &error, e.what()))
					throw std::runtime_error{"Failed to construct `Error`."};
			}
		}
	}

	/**
	 * Return a function spec from a C++ function.
	 */
	template <auto callable, GetterFor<decltype(callable)> thisGetter = nullptr>
	static JSFunctionSpec Wrap(const char* name,
		const u16 flags = JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT)
	{
		return JS_FN(name, (&ToJSNative<callable, thisGetter>), args_info<decltype(callable)>::nb_args, flags);
	}

	/**
	 * Return a JSFunction from a C++ function.
	 */
	template <auto callable, GetterFor<decltype(callable)> thisGetter = nullptr>
	static JSFunction* Create(const ScriptRequest& rq, const char* name,
		const u16 flags = JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT)
	{
		return JS_NewFunction(rq.cx, &ToJSNative<callable, thisGetter>, args_info<decltype(callable)>::nb_args, flags, name);
	}

	/**
	 * Register a function on the native scope (usually 'Engine').
	 */
	template <auto callable, GetterFor<decltype(callable)> thisGetter = nullptr>
	static void Register(const ScriptRequest& rq, const char* name,
		const u16 flags = JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT)
	{
		JS_DefineFunction(rq.cx, rq.nativeScope, name, &ToJSNative<callable, thisGetter>, args_info<decltype(callable)>::nb_args, flags);
	}

	/**
	 * Register a function on @param scope.
	 * Prefer the version taking ScriptRequest unless you have a good reason not to.
	 * @see Register
	 */
	template <auto callable, GetterFor<decltype(callable)> thisGetter = nullptr>
	static void Register(JSContext* cx, JS::HandleObject scope, const char* name,
		const u16 flags = JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT)
	{
		JS_DefineFunction(cx, scope, name, &ToJSNative<callable, thisGetter>, args_info<decltype(callable)>::nb_args, flags);
	}
};

#endif // INCLUDED_FUNCTIONWRAPPER