File: test_FunctionWrapper.h

package info (click to toggle)
0ad 0.28.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 182,352 kB
  • sloc: cpp: 201,989; javascript: 19,730; ansic: 15,057; python: 6,597; sh: 2,046; perl: 1,232; xml: 543; java: 533; makefile: 105
file content (170 lines) | stat: -rw-r--r-- 6,198 bytes parent folder | download | duplicates (2)
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
/* Copyright (C) 2025 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/>.
 */

#include "lib/self_test.h"

#include "lib/file/vfs/vfs.h"
#include "lib/path.h"
#include "ps/CLogger.h"
#include "ps/Filesystem.h"
#include "scriptinterface/FunctionWrapper.h"
#include "scriptinterface/ModuleLoader.h"
#include "scriptinterface/ScriptInterface.h"
#include "scriptinterface/ScriptRequest.h"

#include <functional>
#include <js/CallArgs.h>
#include <js/RootingAPI.h>
#include <js/TypeDecls.h>
#include <js/Value.h>
#include <stdexcept>
#include <string>
#include <tuple>
#include <type_traits>

class TestFunctionWrapper : public CxxTest::TestSuite
{
public:

	// TODO C++20: use lambda functions directly, names are 'N params, void/returns'.
	static void _1p_v(int) {};
	static void _3p_v(int, bool, std::string) {};
	static int _3p_r(int a, bool, std::string) { return a; };

	static void _0p_v() {};
	static int _0p_r() { return 1; };

	void test_simple_wrappers()
	{
		static_assert(std::is_same_v<decltype(&ScriptFunction::ToJSNative<&TestFunctionWrapper::_1p_v>), JSNative>);
		static_assert(std::is_same_v<decltype(&ScriptFunction::ToJSNative<&TestFunctionWrapper::_3p_v>), JSNative>);
		static_assert(std::is_same_v<decltype(&ScriptFunction::ToJSNative<&TestFunctionWrapper::_3p_r>), JSNative>);
		static_assert(std::is_same_v<decltype(&ScriptFunction::ToJSNative<&TestFunctionWrapper::_0p_v>), JSNative>);
		static_assert(std::is_same_v<decltype(&ScriptFunction::ToJSNative<&TestFunctionWrapper::_0p_r>), JSNative>);
	}

	static void _handle(JS::HandleValue) {};
	static void _handle_2(int, JS::HandleValue, bool) {};

	static void _script_interface(const ScriptInterface&) {};
	static int _script_interface_2(const ScriptInterface&, int a, bool) { return a; };

	static void _script_request(const ScriptRequest&) {};
	static int _script_request_2(const ScriptRequest&, int a, bool) { return a; };

	void test_special_wrappers()
	{
		static_assert(std::is_same_v<decltype(&ScriptFunction::ToJSNative<&TestFunctionWrapper::_handle>), JSNative>);
		static_assert(std::is_same_v<decltype(&ScriptFunction::ToJSNative<&TestFunctionWrapper::_handle_2>), JSNative>);
		static_assert(std::is_same_v<decltype(&ScriptFunction::ToJSNative<&TestFunctionWrapper::_script_interface>), JSNative>);
		static_assert(std::is_same_v<decltype(&ScriptFunction::ToJSNative<&TestFunctionWrapper::_script_interface_2>), JSNative>);
		static_assert(std::is_same_v<decltype(&ScriptFunction::ToJSNative<&TestFunctionWrapper::_script_request>), JSNative>);
		static_assert(std::is_same_v<decltype(&ScriptFunction::ToJSNative<&TestFunctionWrapper::_script_request_2>), JSNative>);
	}

	class test_method
	{
	public:
		void method_1() {};
		int method_2(int, const int&) { return 4; };
		void const_method_1() const {};
		int const_method_2(int, const int&) const { return 4; };
	};

	void test_method_wrappers()
	{
		static_assert(std::is_same_v<decltype(&ScriptFunction::ToJSNative<&TestFunctionWrapper::test_method::method_1,
											  &ScriptInterface::ObjectFromCBData<test_method>>), JSNative>);
		static_assert(std::is_same_v<decltype(&ScriptFunction::ToJSNative<&TestFunctionWrapper::test_method::method_2,
											  &ScriptInterface::ObjectFromCBData<test_method>>), JSNative>);
		static_assert(std::is_same_v<decltype(&ScriptFunction::ToJSNative<&TestFunctionWrapper::test_method::const_method_1,
											  &ScriptInterface::ObjectFromCBData<test_method>>), JSNative>);
		static_assert(std::is_same_v<decltype(&ScriptFunction::ToJSNative<&TestFunctionWrapper::test_method::const_method_2,
											  &ScriptInterface::ObjectFromCBData<test_method>>), JSNative>);
	}

	void test_calling()
	{
		ScriptInterface script("Test", "Test", g_ScriptContext);
		ScriptRequest rq(script);

		ScriptFunction::Register<&TestFunctionWrapper::_1p_v>(script, "_1p_v");
		{
			std::string input = "Test._1p_v(0);";
			JS::RootedValue val(rq.cx);
			TS_ASSERT(script.Eval(input.c_str(), &val));
		}

		ScriptFunction::Register<&TestFunctionWrapper::_3p_r>(script, "_3p_r");
		{
			std::string input = "Test._3p_r(4, false, 'test');";
			int ret = 0;
			TS_ASSERT(script.Eval(input.c_str(), ret));
			TS_ASSERT_EQUALS(ret, 4);
		}

		ScriptFunction::Register<&TestFunctionWrapper::_script_interface_2>(script, "_cmpt_private_2");
		{
			std::string input = "Test._cmpt_private_2(4);";
			int ret = 0;
			TS_ASSERT(script.Eval(input.c_str(), ret));
			TS_ASSERT_EQUALS(ret, 4);
		}
	}

	void test_statefull()
	{
		ScriptInterface script{"Test", "Test", g_ScriptContext};
		const ScriptRequest rq{script};
		JS::RootedValue nativeScope{rq.cx, JS::ObjectValue(*rq.nativeScope)};

		constexpr const char* name{"callback"};
		{
			bool called{false};
			auto _ = ScriptFunction::Register(rq, name, [&](){
				called = true;
			});
			TS_ASSERT(!called);
			TS_ASSERT(ScriptFunction::CallVoid(rq, nativeScope, name));
			TS_ASSERT(called);
		}

		TS_ASSERT(!ScriptFunction::CallVoid(rq, nativeScope, name));
	}

	void test_exception()
	{
		g_VFS = CreateVfs();
		TS_ASSERT_OK(g_VFS->Mount(L"", DataDir() / "mods" / "_test.scriptinterface" / "exception" / "",
			VFS_MOUNT_MUST_EXIST));

		ScriptInterface script{"Engine", "Test", g_ScriptContext, [](const VfsPath&){
			return true;
		}};
		const ScriptRequest rq{script};

		auto _ = ScriptFunction::Register(rq, "callback", [&](){
			throw std::runtime_error{"Testerror"};
		});

		TestLogger logger;
		std::ignore = script.GetModuleLoader().LoadModule(rq, "catch.js");
		TS_ASSERT_STR_CONTAINS(logger.GetOutput(), "Testerror");

		g_VFS.reset();
	}
};