forked from ftk/quickjspp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqjs.cpp
58 lines (45 loc) · 1.26 KB
/
qjs.cpp
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
#include <quickjspp.hpp>
#include <iostream>
int main(int argc, char ** argv)
{
JSRuntime * rt;
JSContext * ctx;
using namespace qjs;
Runtime runtime;
rt = runtime.rt;
Context context(runtime);
ctx = context.ctx;
js_std_init_handlers(rt);
/* loader for ES6 modules */
JS_SetModuleLoaderFunc(rt, nullptr, js_module_loader, nullptr);
js_std_add_helpers(ctx, argc - 1, argv + 1);
/* system modules */
js_init_module_std(ctx, "std");
js_init_module_os(ctx, "os");
/* make 'std' and 'os' visible to non module code */
const char * str = R"(
import * as std from 'std';
import * as os from 'os';
globalThis.std = std;
globalThis.os = os;
)";
context.eval(str, "<input>", JS_EVAL_TYPE_MODULE);
try
{
if(argv[1])
context.evalFile(argv[1], JS_EVAL_TYPE_MODULE);
}
catch(qjs::exception)
{
//js_std_dump_error(ctx);
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;
js_std_free_handlers(rt);
return 1;
}
js_std_loop(ctx);
js_std_free_handlers(rt);
return 0;
}