-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathexception.cpp
More file actions
133 lines (121 loc) · 4.1 KB
/
Copy pathexception.cpp
File metadata and controls
133 lines (121 loc) · 4.1 KB
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
#include "quickjspp.hpp"
#include <iostream>
#include <stdexcept>
struct A{};
struct B {
B() {
throw std::runtime_error("constructor error");
}
B(int) {}
void a_method() {
throw std::runtime_error("method error");
}
};
int main()
{
qjs::Runtime runtime;
qjs::Context context(runtime);
context.global()["println"] = [](const std::string& s) { std::cout << s << std::endl; };
context.registerClass<A>("A");
context.registerClass<A>("A");
auto& testMod = context.addModule("test");
testMod.class_<B>("B")
.constructor<>("B")
.constructor<int>("B_int")
.fun<&B::a_method>("a_method");
try
{
auto obj = context.eval("Symbol.toPrimitive");
std::cout << static_cast<int>(obj) << std::endl;
assert(false);
}
catch(qjs::exception)
{
auto exc = context.getException();
std::cerr << (exc.isError() ? "Error: " : "Throw: ") << (std::string)exc << std::endl;
if((bool)exc["stack"])
std::cerr << (std::string)exc["stack"] << std::endl;
assert(exc.isError() && (std::string) exc == "TypeError: cannot convert symbol to number");
}
try
{
auto f = (std::function<void ()>) context.eval("(function() { +Symbol.toPrimitive })");
f();
assert(false);
}
catch(qjs::exception)
{
auto exc = context.getException();
std::cerr << (exc.isError() ? "Error: " : "Throw: ") << (std::string)exc << std::endl;
if((bool)exc["stack"])
std::cerr << (std::string)exc["stack"] << std::endl;
assert(exc.isError() && (std::string) exc == "TypeError: cannot convert symbol to number");
}
try
{
qjs::Value function = context.eval("() => { let a = b; }", "<test>");
auto native = function.as<std::function<qjs::Value()>>();
qjs::Value result = native();
assert(false);
}
catch(qjs::exception)
{
auto exc = context.getException();
std::cerr << (exc.isError() ? "Error: " : "Throw: ") << (std::string)exc << std::endl;
if((bool)exc["stack"])
std::cerr << (std::string)exc["stack"] << std::endl;
assert(exc.isError() && (std::string) exc == "ReferenceError: 'b' is not defined");
}
// test the `wrap_call` case
try
{
qjs::Value caller = context.eval("(f) => f();", "<test>");
caller.as<std::function<void(std::function<void()>)>>()([](){
throw std::runtime_error("some error");
});
assert(false);
}
catch(qjs::exception)
{
auto exc = context.getException();
std::cerr << (exc.isError() ? "Error: " : "Throw: ") << (std::string)exc << std::endl;
if((bool)exc["stack"])
std::cerr << (std::string)exc["stack"] << std::endl;
assert(exc.isError() && (std::string) exc == "InternalError: some error");
}
// test the `ctor_wrapper` case
try
{
qjs::Value caller = context.eval(R"xxx(
import { B } from "test";
const b = new B();
)xxx", "<eval>", JS_EVAL_TYPE_MODULE);
assert(false);
}
catch(qjs::exception)
{
auto exc = context.getException();
std::cerr << (exc.isError() ? "Error: " : "Throw: ") << (std::string)exc << std::endl;
if((bool)exc["stack"])
std::cerr << (std::string)exc["stack"] << std::endl;
assert(exc.isError() && (std::string) exc == "InternalError: constructor error");
}
// test the `wrap_this_call` case
try
{
qjs::Value caller = context.eval(R"xxx(
import { B_int } from "test";
const b = new B_int(123);
b.a_method();
)xxx", "<eval>", JS_EVAL_TYPE_MODULE);
assert(false);
}
catch(qjs::exception)
{
auto exc = context.getException();
std::cerr << (exc.isError() ? "Error: " : "Throw: ") << (std::string)exc << std::endl;
if((bool)exc["stack"])
std::cerr << (std::string)exc["stack"] << std::endl;
assert(exc.isError() && (std::string) exc == "InternalError: method error");
}
}