From ba9b4a82ec72470b10fcc549750e06b49b4b4b58 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Wed, 3 Jul 2024 00:51:30 -0700 Subject: [PATCH] Add `qjsc -n` parameter to override script name stored in bytecode (#459) --- qjsc.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/qjsc.c b/qjsc.c index 95a22d6..33c6cf4 100644 --- a/qjsc.c +++ b/qjsc.c @@ -261,6 +261,7 @@ JSModuleDef *jsc_module_loader(JSContext *ctx, static void compile_file(JSContext *ctx, FILE *fo, const char *filename, + const char *script_name, const char *c_name1, int module) { @@ -284,7 +285,7 @@ static void compile_file(JSContext *ctx, FILE *fo, eval_flags |= JS_EVAL_TYPE_MODULE; else eval_flags |= JS_EVAL_TYPE_GLOBAL; - obj = JS_Eval(ctx, (const char *)buf, buf_len, filename, eval_flags); + obj = JS_Eval(ctx, (const char *)buf, buf_len, script_name ? script_name : filename, eval_flags); if (JS_IsException(obj)) { js_std_dump_error(ctx); exit(1); @@ -327,6 +328,7 @@ void help(void) "options are:\n" "-e output main() and bytecode in a C file\n" "-o output set the output filename\n" + "-n script_name set the script name (as used in stack traces)\n" "-N cname set the C name of the generated data\n" "-m compile as Javascript module (default=autodetect)\n" "-D module_name compile a dynamically loaded module or worker\n" @@ -347,7 +349,7 @@ typedef enum { int main(int argc, char **argv) { int c, i, verbose; - const char *out_filename, *cname; + const char *out_filename, *cname, *script_name; char cfilename[1024]; FILE *fo; JSRuntime *rt; @@ -358,6 +360,7 @@ int main(int argc, char **argv) namelist_t dynamic_module_list; out_filename = NULL; + script_name = NULL; output_type = OUTPUT_C; cname = NULL; module = -1; @@ -371,7 +374,7 @@ int main(int argc, char **argv) namelist_add(&cmodule_list, "os", "os", 0); for(;;) { - c = getopt(argc, argv, "ho:N:mxesvM:p:S:D:"); + c = getopt(argc, argv, "ho:N:mn:mxesvM:p:S:D:"); if (c == -1) break; switch(c) { @@ -383,6 +386,9 @@ int main(int argc, char **argv) case 'e': output_type = OUTPUT_C_MAIN; break; + case 'n': + script_name = optarg; + break; case 'N': cname = optarg; break; @@ -462,7 +468,7 @@ int main(int argc, char **argv) for(i = optind; i < argc; i++) { const char *filename = argv[i]; - compile_file(ctx, fo, filename, cname, module); + compile_file(ctx, fo, filename, script_name, cname, module); cname = NULL; }