@@ -96,6 +96,35 @@ static int eval_file(JSContext *ctx, const char *filename, int module)
96
96
return ret ;
97
97
}
98
98
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
+
99
128
static JSValue js_gc (JSContext * ctx , JSValue this_val ,
100
129
int argc , JSValue * argv )
101
130
{
@@ -275,8 +304,8 @@ void help(void)
275
304
" --std make 'std' and 'os' available to the loaded script\n"
276
305
"-T --trace trace memory allocation\n"
277
306
"-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"
280
309
" --unhandled-rejection dump unhandled promise rejections\n"
281
310
"-q --quit just instantiate the interpreter and quit\n" , JS_GetVersion ());
282
311
exit (1 );
@@ -404,8 +433,7 @@ int main(int argc, char **argv)
404
433
}
405
434
opt_arg = argv [optind ++ ];
406
435
}
407
- // TODO(chqrlie): accept kmg suffixes
408
- memory_limit = strtoull (opt_arg , NULL , 0 );
436
+ memory_limit = parse_limit (opt_arg );
409
437
break ;
410
438
}
411
439
if (!strcmp (longopt , "stack-size" )) {
@@ -416,8 +444,7 @@ int main(int argc, char **argv)
416
444
}
417
445
opt_arg = argv [optind ++ ];
418
446
}
419
- // TODO(chqrlie): accept kmg suffixes
420
- stack_size = strtoull (opt_arg , NULL , 0 );
447
+ stack_size = parse_limit (opt_arg );
421
448
break ;
422
449
}
423
450
if (opt ) {
0 commit comments