-
Notifications
You must be signed in to change notification settings - Fork 85
/
qjs.cpp
112 lines (93 loc) · 2.83 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
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
#include "quickjspp.hpp"
#include "quickjs/quickjs-libc.h"
#include <iostream>
#include <string_view>
static bool bignum_ext = false;
/* also used to initialize the worker context */
static JSContext *JS_NewCustomContext(JSRuntime *rt)
{
JSContext *ctx;
ctx = JS_NewContext(rt);
if (!ctx)
return NULL;
if (bignum_ext) {
JS_AddIntrinsicBigFloat(ctx);
JS_AddIntrinsicBigDecimal(ctx);
JS_AddIntrinsicOperators(ctx);
JS_EnableBignumExt(ctx, true);
}
/* system modules */
js_init_module_std(ctx, "std");
js_init_module_os(ctx, "os");
return ctx;
}
int main(int argc, char ** argv)
{
qjs::Runtime runtime;
auto rt = runtime.rt;
js_std_set_worker_new_context_func(JS_NewCustomContext);
js_std_init_handlers(rt);
/* loader for ES6 modules */
JS_SetModuleLoaderFunc(rt, NULL, js_module_loader, NULL);
qjs::Context context(JS_NewCustomContext(rt));
auto ctx = context.ctx;
int flags = -1;
int optind = 1;
// load as ES6 module
if(argv[optind] && (argv[optind] == std::string_view{"-m"} || argv[optind] == std::string_view{"--module"}))
{
flags = JS_EVAL_TYPE_MODULE;
optind++;
}
// load as ES6 script
else if(argv[optind] && argv[optind] == std::string_view{"--script"})
{
flags = JS_EVAL_TYPE_GLOBAL;
optind++;
}
// enable bignum
if(argv[optind] && argv[optind] == std::string_view{"--bignum"})
{
bignum_ext = true;
optind++;
}
js_std_add_helpers(ctx, argc - optind, argv + optind);
/* make 'std' and 'os' visible to non module code */
context.eval(R"xxx(
import * as std from 'std';
import * as os from 'os';
globalThis.std = std;
globalThis.os = os;
)xxx", "<input>", JS_EVAL_TYPE_MODULE);
try
{
if(auto filename = argv[optind])
{
auto buf = qjs::detail::readFile(filename);
if (!buf)
throw std::runtime_error{std::string{"can't read file: "} + filename};
// autodetect file type
if(flags == -1)
flags = JS_DetectModule(buf->data(), buf->size()) ? JS_EVAL_TYPE_MODULE : JS_EVAL_TYPE_GLOBAL;
context.eval(*buf, filename, flags);
}
else
{
std::cout << argv[0] << " [--module|--script] [--bignum] <filename>" << std::endl;
js_std_free_handlers(rt);
return 1;
}
}
catch(qjs::exception & e)
{
auto exc = e.get();
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;
}