forked from Tencent/ScriptX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExceptionTest.cc
244 lines (215 loc) · 6.47 KB
/
ExceptionTest.cc
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
/*
* Tencent is pleased to support the open source community by making ScriptX available.
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "test.h"
namespace script::test {
DEFINE_ENGINE_TEST(ExceptionTest);
TEST_F(ExceptionTest, Normal) {
{
// ok
EngineScope engineScope(engine);
try {
throw Exception("inside");
} catch (const Exception&) {
}
}
}
TEST_F(ExceptionTest, ExceptionOutOfEngineScope) {
try {
EngineScope engineScope(engine);
throw Exception("inside");
// exception go out of engine scope, still ok
} catch (const Exception&) {
}
}
TEST_F(ExceptionTest, Function) {
EngineScope engineScope(engine);
try {
EXPECT_THROW(
{
engine->eval(TS()
.js("throw Error('hello error')")
.lua("error('hello error')")
.py("raise Exception('hello error')")
.select());
},
Exception);
auto func = Function::newFunction([](const script::Arguments& args) {
if (args.size() == 0) {
throw Exception("invalid argument");
}
return Number::newNumber(0);
});
func.call({}, {Object::newObject()});
EXPECT_THROW({ func.call({}); }, Exception);
engine->set("func", func);
Local<Value> ret;
#ifdef SCRIPTX_LANG_PYTHON
engine->eval(R"(
exceptiontest_function_var = None
try:
func()
exceptiontest_function_var = False
except:
exceptiontest_function_var = True
)");
ret = engine->eval("exceptiontest_function_var");
#else
ret = engine->eval(TS().js(R"(
try {
func();
false;
} catch (e) {
true;
}
)")
.lua(R"(
return not pcall(func)
)")
.select());
#endif
EXPECT_TRUE(ret.isBoolean());
EXPECT_TRUE(ret.asBoolean().value());
engine->set("func", {});
try {
func.call({});
FAIL();
} catch (Exception& e) {
EXPECT_NE(e.message().find("invalid argument"), std::string::npos);
}
} catch (Exception& exception) {
FAIL() << exception.message() << exception.stacktrace();
}
}
TEST_F(ExceptionTest, StackTrace) {
EngineScope engineScope(engine);
Local<Value> func;
#ifdef SCRIPTX_LANG_PYTHON
engine->eval(R"(
def exceptionStackTraceTestThrow():
raise Exception("recursive too deep")
def exceptionStackTraceTest(depth):
if (depth >= 10):
exceptionStackTraceTestThrow()
exceptionStackTraceTest(depth + 1)
)");
func = engine->eval("exceptionStackTraceTest");
#else
func = engine->eval(TS().js(R"(
function exceptionStackTraceTestThrow() {
throw new Error("recursive too deep");
}
function exceptionStackTraceTest(depth) {
if (depth >= 10) exceptionStackTraceTestThrow();
exceptionStackTraceTest(depth + 1);
}
exceptionStackTraceTest;
)")
.lua(R"(
function exceptionStackTraceTestThrow()
error("recursive too deep")
end
function exceptionStackTraceTest(depth)
if depth >= 10 then exceptionStackTraceTestThrow() end
exceptionStackTraceTest(depth + 1)
end
return exceptionStackTraceTest
)")
.select());
#endif
try {
#ifdef SCRIPTX_BACKEND_QUICKJS
// update max stack size for QuickJs
// otherwise a "stack overflow" maybe thrown
// even we have limited to 10 layers of recursion.
JS_SetMaxStackSize(qjs_interop::currentRuntime(), 1024 * 1024);
#endif
func.asFunction().call({}, Number::newNumber(0));
FAIL() << "should thrown";
} catch (const Exception& e) {
EXPECT_NE(e.message().find("recursive too deep"), std::string::npos) << e;
EXPECT_NE(e.stacktrace().find("exceptionStackTraceTestThrow"), std::string::npos) << e;
EXPECT_NE(e.stacktrace().find("exceptionStackTraceTest"), std::string::npos) << e;
}
Exception().stacktrace();
std::ostringstream() << Exception();
}
TEST_F(ExceptionTest, Cross) {
EngineScope engineScope(engine);
Exception e("test");
EXPECT_NE(e.message().find("test"), std::string::npos);
EXPECT_NE(std::string(e.what()).find("test"), std::string::npos);
auto exception = e.exception();
try {
EXPECT_FALSE(exception.isNull());
#ifdef SCRIPTX_LANG_PYTHON
engine->eval("def exceptiontest_cross_function(e):\n\traise e");
#endif
auto throwIt = engine->eval(TS().js("function throwIt(e) { throw e; }; throwIt")
.lua("return function (e) error(e) end;")
.py("exceptiontest_cross_function")
.select());
throwIt.asFunction().call({}, exception);
} catch (Exception& ex) {
EXPECT_NE(ex.message().find("test"), std::string::npos);
#ifdef SCRIPTX_LANG_JAVASCRIPT
EXPECT_TRUE(exception == ex.exception());
#endif
#ifdef SCRIPTX_LANG_LUA
auto exException = ex.exception();
EXPECT_TRUE(exException.isObject());
EXPECT_NE(exException.asObject().get("message").asString().toString().find(
exception.asString().toString()),
std::string::npos);
#endif
}
{
Exception ex(String::newString("test"));
EXPECT_NE(ex.message().find("test"), std::string::npos);
}
}
#ifndef SCRIPTX_BACKEND_WEBASSEMBLY
TEST_F(ExceptionTest, EngineScopeOnDestroy) {
bool executed = false;
{
EngineScope engineScope(engine);
utils::Message m([](auto msg) {},
[](auto msg) {
*static_cast<bool*>(msg.ptr0) = true;
EXPECT_THROW({ EngineScope scope(static_cast<ScriptEngine*>(msg.ptr1)); },
std::logic_error);
});
m.tag = engine;
m.ptr0 = &executed;
m.ptr1 = engine;
engine->messageQueue()->postMessage(m);
}
destroyEngine();
EXPECT_TRUE(executed);
}
#endif
TEST_F(ExceptionTest, ThrowPerformance) {
std::string chunk;
chunk.resize(1024, '.');
EngineScope engineScope(engine);
for (int i = 0; i < 1000; ++i) {
try {
throw Exception(chunk);
} catch (Exception&) {
}
}
}
} // namespace script::test