Skip to content

Commit

Permalink
qjs.cpp: port script type autodetection and a few other features from…
Browse files Browse the repository at this point in the history
… quickjs/qjs.c interpreter

should fix some issues with quickjs tests failing (#65)
  • Loading branch information
ftk committed Dec 21, 2023
1 parent 80b6226 commit 0c00c48
Showing 1 changed file with 71 additions and 12 deletions.
83 changes: 71 additions & 12 deletions qjs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,65 @@
#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)
{
using namespace qjs;
qjs::Runtime runtime;
auto rt = runtime.rt;

js_std_set_worker_new_context_func(JS_NewCustomContext);
js_std_init_handlers(rt);

Runtime runtime;
Context context(runtime);
/* loader for ES6 modules */
JS_SetModuleLoaderFunc(rt, NULL, js_module_loader, NULL);

auto rt = runtime.rt;
qjs::Context context(JS_NewCustomContext(rt));
auto ctx = context.ctx;

js_std_init_handlers(rt);
js_std_add_helpers(ctx, argc - 1, argv + 1);
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++;
}

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

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


try
{
if(argv[1])
context.evalFile(argv[1], JS_EVAL_TYPE_MODULE);
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(exception & e)
catch(qjs::exception & e)
{
auto exc = e.get();
std::cerr << (exc.isError() ? "Error: " : "Throw: ") << (std::string)exc << std::endl;
Expand Down

0 comments on commit 0c00c48

Please sign in to comment.