summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFabrice Bellard <fabrice@bellard.org>2025-04-07 11:47:57 +0200
committerFabrice Bellard <fabrice@bellard.org>2025-04-07 11:47:57 +0200
commitec83bd209889277b7c9d1600df5a6e67c1f8dae0 (patch)
treefea30c037f1a948816369f851a3b3d19ff2c194c
parentc805d4f7846456bf6dade8b4f4258c85cbd178c3 (diff)
downloadquickjs-ec83bd209889277b7c9d1600df5a6e67c1f8dae0.tar.gz
quickjs-ec83bd209889277b7c9d1600df5a6e67c1f8dae0.zip
qjs: allow SI suffixes in memory sizes - set default stack size to 1 MB
-rw-r--r--qjs.c34
-rw-r--r--quickjs.h4
2 files changed, 33 insertions, 5 deletions
diff --git a/qjs.c b/qjs.c
index 3c1ee99..b6bfca3 100644
--- a/qjs.c
+++ b/qjs.c
@@ -257,6 +257,32 @@ static const JSMallocFunctions trace_mf = {
js_trace_malloc_usable_size,
};
+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;
+}
+
#define PROG_NAME "qjs"
void help(void)
@@ -272,8 +298,8 @@ void help(void)
" --std make 'std' and 'os' available to the loaded script\n"
"-T --trace trace memory allocation\n"
"-d --dump dump the memory usage stats\n"
- " --memory-limit n limit the memory usage to 'n' bytes\n"
- " --stack-size n limit the stack size to 'n' bytes\n"
+ " --memory-limit n limit the memory usage to 'n' bytes (SI suffixes allowed)\n"
+ " --stack-size n limit the stack size to 'n' bytes (SI suffixes allowed)\n"
" --no-unhandled-rejection ignore unhandled promise rejections\n"
"-q --quit just instantiate the interpreter and quit\n");
exit(1);
@@ -384,7 +410,7 @@ int main(int argc, char **argv)
fprintf(stderr, "expecting memory limit");
exit(1);
}
- memory_limit = (size_t)strtod(argv[optind++], NULL);
+ memory_limit = get_suffixed_size(argv[optind++]);
continue;
}
if (!strcmp(longopt, "stack-size")) {
@@ -392,7 +418,7 @@ int main(int argc, char **argv)
fprintf(stderr, "expecting stack size");
exit(1);
}
- stack_size = (size_t)strtod(argv[optind++], NULL);
+ stack_size = get_suffixed_size(argv[optind++]);
continue;
}
if (opt) {
diff --git a/quickjs.h b/quickjs.h
index 7ba2572..0560371 100644
--- a/quickjs.h
+++ b/quickjs.h
@@ -322,7 +322,9 @@ static inline JSValue __JS_NewShortBigInt(JSContext *ctx, int64_t d)
#define JS_PROP_NO_ADD (1 << 16) /* internal use */
#define JS_PROP_NO_EXOTIC (1 << 17) /* internal use */
-#define JS_DEFAULT_STACK_SIZE (256 * 1024)
+#ifndef JS_DEFAULT_STACK_SIZE
+#define JS_DEFAULT_STACK_SIZE (1024 * 1024)
+#endif
/* JS_Eval() flags */
#define JS_EVAL_TYPE_GLOBAL (0 << 0) /* global code (default) */