@@ -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+
99128static 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 ) {
0 commit comments