Skip to content

Commit 6cb1301

Browse files
committed
Accept "kmg" suffixes for memory limits
Switch the default in the CLI to kilobytes too.
1 parent e5ae6cf commit 6cb1301

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

qjs.c

+33-6
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,35 @@ static int eval_file(JSContext *ctx, const char *filename, int module)
9696
return ret;
9797
}
9898

99+
static int64_t parse_limit(const char *arg) {
100+
char *p;
101+
unsigned long unit = 1024; /* default to traditional KB */
102+
double d = strtod(arg, &p);
103+
104+
if (p == arg) {
105+
fprintf(stderr, "Invalid limit: %s\n", arg);
106+
return -1;
107+
}
108+
109+
if (*p) {
110+
switch (*p++) {
111+
case 'b': case 'B': unit = 1UL << 0; break;
112+
case 'k': case 'K': unit = 1UL << 10; break; /* IEC kibibytes */
113+
case 'm': case 'M': unit = 1UL << 20; break; /* IEC mebibytes */
114+
case 'g': case 'G': unit = 1UL << 30; break; /* IEC gigibytes */
115+
default:
116+
fprintf(stderr, "Invalid limit: %s, unrecognized suffix, only k,m,g are allowed\n", arg);
117+
return -1;
118+
}
119+
if (*p) {
120+
fprintf(stderr, "Invalid limit: %s, only one suffix allowed\n", arg);
121+
return -1;
122+
}
123+
}
124+
125+
return (int64_t)(d * unit);
126+
}
127+
99128
static JSValue js_gc(JSContext *ctx, JSValue this_val,
100129
int argc, JSValue *argv)
101130
{
@@ -275,8 +304,8 @@ void help(void)
275304
" --std make 'std' and 'os' available to the loaded script\n"
276305
"-T --trace trace memory allocation\n"
277306
"-d --dump dump the memory usage stats\n"
278-
" --memory-limit n limit the memory usage to 'n' bytes\n"
279-
" --stack-size n limit the stack size to 'n' bytes\n"
307+
" --memory-limit n limit the memory usage to 'n' Kbytes\n"
308+
" --stack-size n limit the stack size to 'n' Kbytes\n"
280309
" --unhandled-rejection dump unhandled promise rejections\n"
281310
"-q --quit just instantiate the interpreter and quit\n", JS_GetVersion());
282311
exit(1);
@@ -404,8 +433,7 @@ int main(int argc, char **argv)
404433
}
405434
opt_arg = argv[optind++];
406435
}
407-
// TODO(chqrlie): accept kmg suffixes
408-
memory_limit = strtoull(opt_arg, NULL, 0);
436+
memory_limit = parse_limit(opt_arg);
409437
break;
410438
}
411439
if (!strcmp(longopt, "stack-size")) {
@@ -416,8 +444,7 @@ int main(int argc, char **argv)
416444
}
417445
opt_arg = argv[optind++];
418446
}
419-
// TODO(chqrlie): accept kmg suffixes
420-
stack_size = strtoull(opt_arg, NULL, 0);
447+
stack_size = parse_limit(opt_arg);
421448
break;
422449
}
423450
if (opt) {

v8.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ for (const file of files) {
9696
//print(`=== ${file}${envstr}${flagstr}`)
9797
print(`=== ${file}${envstr}`)
9898
const args = [argv0,
99-
"--stack-size", `${flags["--stack-size"]*1024}`,
99+
"--stack-size", `${flags["--stack-size"]}`,
100100
"-I", "mjsunit.js",
101101
"-I", tweak,
102102
file]

0 commit comments

Comments
 (0)