-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathenum.cpp
More file actions
52 lines (47 loc) · 1.33 KB
/
Copy pathenum.cpp
File metadata and controls
52 lines (47 loc) · 1.33 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
#include <iostream>
#include <stdio.h>
#include "quickjspp.hpp"
enum TestEnum : int64_t{
TestEnum_A,
TestEnum_B = (1ll << 51),
TestEnum_C,
};
class TestClass {
public:
TestEnum e = TestEnum_C;
TestEnum getE() {
return e;
}
};
void println(qjs::rest<std::string> args) {
for (auto const &arg: args) std::cout << arg << " ";
std::cout << "\n";
}
int main() {
qjs::Runtime runtime;
qjs::Context context(runtime);
try {
auto &module = context.addModule("MyModule");
module.function<&println>("println");
module.class_<TestClass>("TestClass")
.constructor<>()
.fun<&TestClass::getE>("getE");
// import module
context.eval(R"xxx(
import * as my from 'MyModule';
globalThis.my = my;
)xxx",
"<import>", JS_EVAL_TYPE_MODULE);
context.eval(R"xxx(
let v1 = new my.TestClass();
let e = v1.getE();
my.println("enum value "+e);
)xxx");
assert(context.eval("e").as<TestEnum>() == TestEnum_C);
} catch (qjs::exception) {
auto exc = context.getException();
std::cerr << (std::string) exc << std::endl;
if ((bool) exc["stack"]) std::cerr << (std::string) exc["stack"] << std::endl;
return 1;
}
}