From f31653305ed1da8e6dc79f6706aae8af943322a6 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Tue, 28 Jan 2025 23:23:08 +0100 Subject: [PATCH] Support qjs -m flag in combination with -e (#863) Evaluate the expression as a module when -m is specified, not as a classic script. Fixes: https://github.com/quickjs-ng/quickjs/issues/862 --- qjs.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/qjs.c b/qjs.c index fb5d53a46..10609b1fd 100644 --- a/qjs.c +++ b/qjs.c @@ -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; @@ -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. "" + if (js_module_set_import_meta(ctx, val, use_realpath, true) < 0) { js_std_dump_error(ctx); ret = -1; goto end; @@ -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), "", 0)) + int flags = module ? JS_EVAL_TYPE_MODULE : 0; + if (eval_buf(ctx, expr, strlen(expr), "", flags)) goto fail; } else if (optind >= argc) { /* interactive mode */