- removed the 'use strip' extension

- removed the JS_EVAL_FLAG_STRIP eval flag and replaced it with JS_SetStripInfo() which has simpler semantics.
- qjs: added the '-s' and '--strip-source' options
- qjsc: added the '-s' and '--keep-source' options
This commit is contained in:
Fabrice Bellard
2025-04-12 12:14:37 +02:00
parent c50de13b15
commit 67b48ae4e6
9 changed files with 207 additions and 87 deletions

181
qjsc.c
View File

@@ -352,7 +352,9 @@ void help(void)
"-M module_name[,cname] add initialization code for an external C module\n"
"-x byte swapped output\n"
"-p prefix set the prefix of the generated C names\n"
"-S n set the maximum stack size to 'n' bytes (default=%d)\n",
"-S n set the maximum stack size to 'n' bytes (default=%d)\n"
"-s strip all the debug info\n"
"--keep-source keep the source code\n",
JS_DEFAULT_STACK_SIZE);
#ifdef CONFIG_LTO
{
@@ -471,6 +473,31 @@ static int output_executable(const char *out_filename, const char *cfilename,
}
#endif
static size_t get_suffixed_size(const char *str)
{
char *p;
size_t v;
v = (size_t)strtod(str, &p);
switch(*p) {
case 'G':
v <<= 30;
break;
case 'M':
v <<= 20;
break;
case 'k':
case 'K':
v <<= 10;
break;
default:
if (*p != '\0') {
fprintf(stderr, "qjs: invalid suffix: %s\n", p);
exit(1);
}
break;
}
return v;
}
typedef enum {
OUTPUT_C,
@@ -478,9 +505,24 @@ typedef enum {
OUTPUT_EXECUTABLE,
} OutputTypeEnum;
static const char *get_short_optarg(int *poptind, int opt,
const char *arg, int argc, char **argv)
{
const char *optarg;
if (*arg) {
optarg = arg;
} else if (*poptind < argc) {
optarg = argv[(*poptind)++];
} else {
fprintf(stderr, "qjsc: expecting parameter for -%c\n", opt);
exit(1);
}
return optarg;
}
int main(int argc, char **argv)
{
int c, i, verbose;
int i, verbose, strip_flags;
const char *out_filename, *cname;
char cfilename[1024];
FILE *fo;
@@ -499,6 +541,7 @@ int main(int argc, char **argv)
module = -1;
byte_swap = FALSE;
verbose = 0;
strip_flags = JS_STRIP_SOURCE;
use_lto = FALSE;
stack_size = 0;
memset(&dynamic_module_list, 0, sizeof(dynamic_module_list));
@@ -507,30 +550,51 @@ int main(int argc, char **argv)
namelist_add(&cmodule_list, "std", "std", 0);
namelist_add(&cmodule_list, "os", "os", 0);
for(;;) {
c = getopt(argc, argv, "ho:cN:f:mxevM:p:S:D:");
if (c == -1)
optind = 1;
while (optind < argc && *argv[optind] == '-') {
char *arg = argv[optind] + 1;
const char *longopt = "";
const char *optarg;
/* a single - is not an option, it also stops argument scanning */
if (!*arg)
break;
switch(c) {
case 'h':
help();
case 'o':
out_filename = optarg;
break;
case 'c':
output_type = OUTPUT_C;
break;
case 'e':
output_type = OUTPUT_C_MAIN;
break;
case 'N':
cname = optarg;
break;
case 'f':
{
optind++;
if (*arg == '-') {
longopt = arg + 1;
arg += strlen(arg);
/* -- stops argument scanning */
if (!*longopt)
break;
}
for (; *arg || *longopt; longopt = "") {
char opt = *arg;
if (opt)
arg++;
if (opt == 'h' || opt == '?' || !strcmp(longopt, "help")) {
help();
continue;
}
if (opt == 'o') {
out_filename = get_short_optarg(&optind, opt, arg, argc, argv);
break;
}
if (opt == 'c') {
output_type = OUTPUT_C;
continue;
}
if (opt == 'e') {
output_type = OUTPUT_C_MAIN;
continue;
}
if (opt == 'N') {
cname = get_short_optarg(&optind, opt, arg, argc, argv);
break;
}
if (opt == 'f') {
const char *p;
optarg = get_short_optarg(&optind, opt, arg, argc, argv);
p = optarg;
if (!strcmp(optarg, "lto")) {
if (!strcmp(p, "lto")) {
use_lto = TRUE;
} else if (strstart(p, "no-", &p)) {
use_lto = TRUE;
@@ -547,16 +611,18 @@ int main(int argc, char **argv)
fprintf(stderr, "unsupported feature: %s\n", optarg);
exit(1);
}
break;
}
break;
case 'm':
module = 1;
break;
case 'M':
{
if (opt == 'm') {
module = 1;
continue;
}
if (opt == 'M') {
char *p;
char path[1024];
char cname[1024];
optarg = get_short_optarg(&optind, opt, arg, argc, argv);
pstrcpy(path, sizeof(path), optarg);
p = strchr(path, ',');
if (p) {
@@ -566,25 +632,44 @@ int main(int argc, char **argv)
get_c_name(cname, sizeof(cname), path);
}
namelist_add(&cmodule_list, path, cname, 0);
break;
}
break;
case 'D':
namelist_add(&dynamic_module_list, optarg, NULL, 0);
break;
case 'x':
byte_swap = TRUE;
break;
case 'v':
verbose++;
break;
case 'p':
c_ident_prefix = optarg;
break;
case 'S':
stack_size = (size_t)strtod(optarg, NULL);
break;
default:
break;
if (opt == 'D') {
optarg = get_short_optarg(&optind, opt, arg, argc, argv);
namelist_add(&dynamic_module_list, optarg, NULL, 0);
break;
}
if (opt == 'x') {
byte_swap = 1;
continue;
}
if (opt == 'v') {
verbose++;
continue;
}
if (opt == 'p') {
c_ident_prefix = get_short_optarg(&optind, opt, arg, argc, argv);
break;
}
if (opt == 'S') {
optarg = get_short_optarg(&optind, opt, arg, argc, argv);
stack_size = get_suffixed_size(optarg);
break;
}
if (opt == 's') {
strip_flags = JS_STRIP_DEBUG;
continue;
}
if (!strcmp(longopt, "keep-source")) {
strip_flags = 0;
continue;
}
if (opt) {
fprintf(stderr, "qjsc: unknown option '-%c'\n", opt);
} else {
fprintf(stderr, "qjsc: unknown option '--%s'\n", longopt);
}
help();
}
}
@@ -620,6 +705,8 @@ int main(int argc, char **argv)
rt = JS_NewRuntime();
ctx = JS_NewContext(rt);
JS_SetStripInfo(rt, strip_flags);
/* loader for ES6 modules */
JS_SetModuleLoaderFunc(rt, NULL, jsc_module_loader, NULL);