@@ -382,6 +382,7 @@ struct JSContext {
382
382
JSValue native_error_proto[JS_NATIVE_ERROR_COUNT];
383
383
JSValue error_ctor;
384
384
JSValue error_prepare_stack;
385
+ int error_stack_trace_limit;
385
386
JSValue iterator_proto;
386
387
JSValue async_iterator_proto;
387
388
JSValue array_proto_values;
@@ -2101,6 +2102,7 @@ JSContext *JS_NewContextRaw(JSRuntime *rt)
2101
2102
ctx->promise_ctor = JS_NULL;
2102
2103
ctx->error_ctor = JS_NULL;
2103
2104
ctx->error_prepare_stack = JS_UNDEFINED;
2105
+ ctx->error_stack_trace_limit = 10;
2104
2106
init_list_head(&ctx->loaded_modules);
2105
2107
2106
2108
JS_AddIntrinsicBasicObjects(ctx);
@@ -6385,9 +6387,6 @@ static void build_backtrace(JSContext *ctx, JSValue error_obj,
6385
6387
const char *filename, int line_num, int col_num,
6386
6388
int backtrace_flags)
6387
6389
{
6388
- // TODO(saghul) Make this configurable with Error.stackTraceLimit
6389
- static const int stack_limit = 10;
6390
-
6391
6390
JSStackFrame *sf;
6392
6391
JSValue stack, prepare, saved_exception;
6393
6392
DynBuf dbuf;
@@ -6397,9 +6396,13 @@ static void build_backtrace(JSContext *ctx, JSValue error_obj,
6397
6396
JSFunctionBytecode *b;
6398
6397
BOOL backtrace_barrier, has_prepare;
6399
6398
JSRuntime *rt;
6400
- JSCallSiteData csd[stack_limit ];
6399
+ JSCallSiteData csd[64 ];
6401
6400
uint32_t i;
6401
+ int stack_trace_limit;
6402
6402
6403
+ stack_trace_limit = ctx->error_stack_trace_limit;
6404
+ stack_trace_limit = min_int(stack_trace_limit, countof(csd));
6405
+ stack_trace_limit = max_int(stack_trace_limit, 0);
6403
6406
rt = ctx->rt;
6404
6407
has_prepare = FALSE;
6405
6408
i = 0;
@@ -6413,10 +6416,14 @@ static void build_backtrace(JSContext *ctx, JSValue error_obj,
6413
6416
if (has_prepare) {
6414
6417
saved_exception = rt->current_exception;
6415
6418
rt->current_exception = JS_NULL;
6419
+ if (stack_trace_limit == 0)
6420
+ goto done;
6416
6421
if (filename)
6417
6422
js_new_callsite_data2(ctx, &csd[i++], filename, line_num, col_num);
6418
6423
} else {
6419
6424
js_dbuf_init(ctx, &dbuf);
6425
+ if (stack_trace_limit == 0)
6426
+ goto done;
6420
6427
if (filename) {
6421
6428
i++;
6422
6429
dbuf_printf(&dbuf, " at %s", filename);
@@ -6429,7 +6436,7 @@ static void build_backtrace(JSContext *ctx, JSValue error_obj,
6429
6436
if (filename && (backtrace_flags & JS_BACKTRACE_FLAG_SINGLE_LEVEL))
6430
6437
goto done;
6431
6438
6432
- for (sf = rt->current_stack_frame; sf != NULL && i < stack_limit ; sf = sf->prev_frame) {
6439
+ for (sf = rt->current_stack_frame; sf != NULL && i < stack_trace_limit ; sf = sf->prev_frame) {
6433
6440
if (backtrace_flags & JS_BACKTRACE_FLAG_SKIP_FIRST_LEVEL) {
6434
6441
backtrace_flags &= ~JS_BACKTRACE_FLAG_SKIP_FIRST_LEVEL;
6435
6442
continue;
@@ -36178,6 +36185,28 @@ static const JSCFunctionListEntry js_error_proto_funcs[] = {
36178
36185
JS_PROP_STRING_DEF("message", "", JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE ),
36179
36186
};
36180
36187
36188
+ static JSValue js_error_get_stackTraceLimit(JSContext *ctx, JSValue this_val)
36189
+ {
36190
+ JSValue val, ret;
36191
+
36192
+ val = JS_ToObject(ctx, this_val);
36193
+ if (JS_IsException(val))
36194
+ return val;
36195
+ JS_FreeValue(ctx, val);
36196
+ return js_int32(ctx->error_stack_trace_limit);
36197
+ }
36198
+
36199
+ static JSValue js_error_set_stackTraceLimit(JSContext *ctx, JSValue this_val, JSValue value)
36200
+ {
36201
+ if (JS_IsUndefined(this_val) || JS_IsNull(this_val))
36202
+ return JS_ThrowTypeErrorNotAnObject(ctx);
36203
+ int limit;
36204
+ if (JS_ToInt32(ctx, &limit, value) < 0)
36205
+ return JS_EXCEPTION;
36206
+ ctx->error_stack_trace_limit = limit;
36207
+ return JS_UNDEFINED;
36208
+ }
36209
+
36181
36210
static JSValue js_error_get_prepareStackTrace(JSContext *ctx, JSValue this_val)
36182
36211
{
36183
36212
JSValue val, ret;
@@ -36199,6 +36228,7 @@ static JSValue js_error_set_prepareStackTrace(JSContext *ctx, JSValue this_val,
36199
36228
}
36200
36229
36201
36230
static const JSCFunctionListEntry js_error_funcs[] = {
36231
+ JS_CGETSET_DEF("stackTraceLimit", js_error_get_stackTraceLimit, js_error_set_stackTraceLimit ),
36202
36232
JS_CGETSET_DEF("prepareStackTrace", js_error_get_prepareStackTrace, js_error_set_prepareStackTrace ),
36203
36233
};
36204
36234
0 commit comments