Skip to content

Commit 6bd3d56

Browse files
authored
Use JS_ToBigIntFree() instead of JS_ToBigInt() (#190)
Reduces reference count juggling in the happy path and, to a lesser extent, in error paths.
1 parent 4fc8143 commit 6bd3d56

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

quickjs.c

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11867,7 +11867,7 @@ static int js_unary_arith_bigint(JSContext *ctx,
1186711867
return -1;
1186811868
}
1186911869
r = JS_GetBigInt(res);
11870-
a = JS_ToBigInt(ctx, &a_s, op1);
11870+
a = JS_ToBigIntFree(ctx, &a_s, op1); // infallible, always a bigint
1187111871
ret = 0;
1187211872
switch(op) {
1187311873
case OP_inc:
@@ -11890,7 +11890,6 @@ static int js_unary_arith_bigint(JSContext *ctx,
1189011890
abort();
1189111891
}
1189211892
JS_FreeBigInt(ctx, a, &a_s);
11893-
JS_FreeValue(ctx, op1);
1189411893
if (unlikely(ret)) {
1189511894
JS_FreeValue(ctx, res);
1189611895
throw_bf_exception(ctx, ret);
@@ -12023,16 +12022,21 @@ static int js_binary_arith_bigint(JSContext *ctx, OPCodeEnum op,
1202312022
int ret;
1202412023
JSValue res;
1202512024

12026-
res = JS_NewBigInt(ctx);
12027-
if (JS_IsException(res))
12028-
goto fail;
12029-
a = JS_ToBigInt(ctx, &a_s, op1);
12030-
if (!a)
12031-
goto fail;
12032-
b = JS_ToBigInt(ctx, &b_s, op2);
12025+
a = JS_ToBigIntFree(ctx, &a_s, op1);
12026+
if (!a) {
12027+
JS_FreeValue(ctx, op2);
12028+
return -1;
12029+
}
12030+
b = JS_ToBigIntFree(ctx, &b_s, op2);
1203312031
if (!b) {
1203412032
JS_FreeBigInt(ctx, a, &a_s);
12035-
goto fail;
12033+
return -1;
12034+
}
12035+
res = JS_NewBigInt(ctx);
12036+
if (JS_IsException(res)) {
12037+
JS_FreeBigInt(ctx, a, &a_s);
12038+
JS_FreeBigInt(ctx, b, &b_s);
12039+
return -1;
1203612040
}
1203712041
r = JS_GetBigInt(res);
1203812042
ret = 0;
@@ -12103,20 +12107,13 @@ static int js_binary_arith_bigint(JSContext *ctx, OPCodeEnum op,
1210312107
}
1210412108
JS_FreeBigInt(ctx, a, &a_s);
1210512109
JS_FreeBigInt(ctx, b, &b_s);
12106-
JS_FreeValue(ctx, op1);
12107-
JS_FreeValue(ctx, op2);
1210812110
if (unlikely(ret)) {
1210912111
JS_FreeValue(ctx, res);
1211012112
throw_bf_exception(ctx, ret);
1211112113
return -1;
1211212114
}
1211312115
*pres = JS_CompactBigInt(ctx, res);
1211412116
return 0;
12115-
fail:
12116-
JS_FreeValue(ctx, res);
12117-
JS_FreeValue(ctx, op1);
12118-
JS_FreeValue(ctx, op2);
12119-
return -1;
1212012117
}
1212112118

1212212119
static no_inline __exception int js_binary_arith_slow(JSContext *ctx, JSValue *sp,

0 commit comments

Comments
 (0)