Skip to content

Commit 0c00c48

Browse files
committed
qjs.cpp: port script type autodetection and a few other features from quickjs/qjs.c interpreter
should fix some issues with quickjs tests failing (#65)
1 parent 80b6226 commit 0c00c48

1 file changed

Lines changed: 71 additions & 12 deletions

File tree

qjs.cpp

Lines changed: 71 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,65 @@
22
#include "quickjs/quickjs-libc.h"
33

44
#include <iostream>
5+
#include <string_view>
6+
7+
static bool bignum_ext = false;
8+
9+
/* also used to initialize the worker context */
10+
static JSContext *JS_NewCustomContext(JSRuntime *rt)
11+
{
12+
JSContext *ctx;
13+
ctx = JS_NewContext(rt);
14+
if (!ctx)
15+
return NULL;
16+
if (bignum_ext) {
17+
JS_AddIntrinsicBigFloat(ctx);
18+
JS_AddIntrinsicBigDecimal(ctx);
19+
JS_AddIntrinsicOperators(ctx);
20+
JS_EnableBignumExt(ctx, true);
21+
}
22+
/* system modules */
23+
js_init_module_std(ctx, "std");
24+
js_init_module_os(ctx, "os");
25+
return ctx;
26+
}
527

628
int main(int argc, char ** argv)
729
{
8-
using namespace qjs;
30+
qjs::Runtime runtime;
31+
auto rt = runtime.rt;
32+
33+
js_std_set_worker_new_context_func(JS_NewCustomContext);
34+
js_std_init_handlers(rt);
935

10-
Runtime runtime;
11-
Context context(runtime);
36+
/* loader for ES6 modules */
37+
JS_SetModuleLoaderFunc(rt, NULL, js_module_loader, NULL);
1238

13-
auto rt = runtime.rt;
39+
qjs::Context context(JS_NewCustomContext(rt));
1440
auto ctx = context.ctx;
1541

16-
js_std_init_handlers(rt);
17-
js_std_add_helpers(ctx, argc - 1, argv + 1);
42+
int flags = -1;
43+
int optind = 1;
44+
// load as ES6 module
45+
if(argv[optind] && (argv[optind] == std::string_view{"-m"} || argv[optind] == std::string_view{"--module"}))
46+
{
47+
flags = JS_EVAL_TYPE_MODULE;
48+
optind++;
49+
}
50+
// load as ES6 script
51+
else if(argv[optind] && argv[optind] == std::string_view{"--script"})
52+
{
53+
flags = JS_EVAL_TYPE_GLOBAL;
54+
optind++;
55+
}
56+
// enable bignum
57+
if(argv[optind] && argv[optind] == std::string_view{"--bignum"})
58+
{
59+
bignum_ext = true;
60+
optind++;
61+
}
1862

19-
/* system modules */
20-
js_init_module_std(ctx, "std");
21-
js_init_module_os(ctx, "os");
63+
js_std_add_helpers(ctx, argc - optind, argv + optind);
2264

2365
/* make 'std' and 'os' visible to non module code */
2466
context.eval(R"xxx(
@@ -28,12 +70,29 @@ int main(int argc, char ** argv)
2870
globalThis.os = os;
2971
)xxx", "<input>", JS_EVAL_TYPE_MODULE);
3072

73+
3174
try
3275
{
33-
if(argv[1])
34-
context.evalFile(argv[1], JS_EVAL_TYPE_MODULE);
76+
if(auto filename = argv[optind])
77+
{
78+
auto buf = qjs::detail::readFile(filename);
79+
if (!buf)
80+
throw std::runtime_error{std::string{"can't read file: "} + filename};
81+
82+
// autodetect file type
83+
if(flags == -1)
84+
flags = JS_DetectModule(buf->data(), buf->size()) ? JS_EVAL_TYPE_MODULE : JS_EVAL_TYPE_GLOBAL;
85+
86+
context.eval(*buf, filename, flags);
87+
}
88+
else
89+
{
90+
std::cout << argv[0] << " [--module|--script] [--bignum] <filename>" << std::endl;
91+
js_std_free_handlers(rt);
92+
return 1;
93+
}
3594
}
36-
catch(exception & e)
95+
catch(qjs::exception & e)
3796
{
3897
auto exc = e.get();
3998
std::cerr << (exc.isError() ? "Error: " : "Throw: ") << (std::string)exc << std::endl;

0 commit comments

Comments
 (0)