Skip to content

Commit

Permalink
Support qjs -m flag in combination with -e (#863)
Browse files Browse the repository at this point in the history
Evaluate the expression as a module when -m is specified, not as a
classic script.

Fixes: #862
  • Loading branch information
bnoordhuis authored Jan 28, 2025
1 parent f45e4f0 commit f316533
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions qjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ static JSValue load_standalone_module(JSContext *ctx)
static int eval_buf(JSContext *ctx, const void *buf, int buf_len,
const char *filename, int eval_flags)
{
bool use_realpath;
JSValue val;
int ret;

Expand All @@ -119,7 +120,8 @@ static int eval_buf(JSContext *ctx, const void *buf, int buf_len,
val = JS_Eval(ctx, buf, buf_len, filename,
eval_flags | JS_EVAL_FLAG_COMPILE_ONLY);
if (!JS_IsException(val)) {
if (js_module_set_import_meta(ctx, val, true, true) < 0) {
use_realpath = (*filename != '<'); // ex. "<cmdline>"
if (js_module_set_import_meta(ctx, val, use_realpath, true) < 0) {
js_std_dump_error(ctx);
ret = -1;
goto end;
Expand Down Expand Up @@ -669,7 +671,8 @@ int main(int argc, char **argv)
JS_FreeValue(ctx, args[1]);
JS_FreeValue(ctx, args[2]);
} else if (expr) {
if (eval_buf(ctx, expr, strlen(expr), "<cmdline>", 0))
int flags = module ? JS_EVAL_TYPE_MODULE : 0;
if (eval_buf(ctx, expr, strlen(expr), "<cmdline>", flags))
goto fail;
} else if (optind >= argc) {
/* interactive mode */
Expand Down

0 comments on commit f316533

Please sign in to comment.