Skip to content

Commit 0665131

Browse files
committed
Fix compilation with -DCONFIG_BIGNUM
- disable BigDecimal convertion in `JS_ReadBigNum` - fix some error messages
1 parent 65ecb0b commit 0665131

File tree

2 files changed

+43
-32
lines changed

2 files changed

+43
-32
lines changed

libbf.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ static inline slimb_t ceil_div(slimb_t a, slimb_t b)
136136
return a / b;
137137
}
138138

139+
#ifdef USE_BF_DEC
139140
/* b must be >= 1 */
140141
static inline slimb_t floor_div(slimb_t a, slimb_t b)
141142
{
@@ -145,6 +146,7 @@ static inline slimb_t floor_div(slimb_t a, slimb_t b)
145146
return (a - b + 1) / b;
146147
}
147148
}
149+
#endif
148150

149151
/* return r = a modulo b (0 <= r <= b - 1. b must be >= 1 */
150152
static inline limb_t smod(slimb_t a, slimb_t b)

quickjs.c

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -35896,11 +35896,10 @@ static JSValue JS_ReadBigNum(BCReaderState *s, int tag)
3589635896
uint8_t v8;
3589735897
int32_t e;
3589835898
uint32_t len;
35899-
limb_t l, i, n, j;
35899+
limb_t l, i, n;
3590035900
JSBigFloat *p;
3590135901
limb_t v;
3590235902
bf_t *a;
35903-
int bpos, d;
3590435903

3590535904
p = js_new_bf(s->ctx);
3590635905
if (!p)
@@ -35950,39 +35949,23 @@ static JSValue JS_ReadBigNum(BCReaderState *s, int tag)
3595035949
JS_ThrowInternalError(s->ctx, "invalid bignum length");
3595135950
goto fail;
3595235951
}
35953-
if (tag != BC_TAG_BIG_DECIMAL)
35954-
l = (len + sizeof(limb_t) - 1) / sizeof(limb_t);
35955-
else
35952+
#ifdef CONFIG_BIGNUM
35953+
if (tag == BC_TAG_BIG_DECIMAL) {
3595635954
l = (len + LIMB_DIGITS - 1) / LIMB_DIGITS;
35955+
} else
35956+
#endif
35957+
{
35958+
l = (len + sizeof(limb_t) - 1) / sizeof(limb_t);
35959+
}
3595735960
if (bf_resize(a, l)) {
3595835961
JS_ThrowOutOfMemory(s->ctx);
3595935962
goto fail;
3596035963
}
35961-
if (tag != BC_TAG_BIG_DECIMAL) {
35962-
n = len & (sizeof(limb_t) - 1);
35963-
if (n != 0) {
35964-
v = 0;
35965-
for(i = 0; i < n; i++) {
35966-
if (bc_get_u8(s, &v8))
35967-
goto fail;
35968-
v |= (limb_t)v8 << ((sizeof(limb_t) - n + i) * 8);
35969-
}
35970-
a->tab[0] = v;
35971-
i = 1;
35972-
} else {
35973-
i = 0;
35974-
}
35975-
for(; i < l; i++) {
35976-
#if LIMB_BITS == 32
35977-
if (bc_get_u32(s, &v))
35978-
goto fail;
35979-
#else
35980-
if (bc_get_u64(s, &v))
35981-
goto fail;
35982-
#endif
35983-
a->tab[i] = v;
35984-
}
35985-
} else {
35964+
#ifdef CONFIG_BIGNUM
35965+
if (tag == BC_TAG_BIG_DECIMAL) {
35966+
limb_t j;
35967+
int bpos, d;
35968+
3598635969
bpos = 0;
3598735970
for(i = 0; i < l; i++) {
3598835971
if (i == 0 && (n = len % LIMB_DIGITS) != 0) {
@@ -36009,6 +35992,32 @@ static JSValue JS_ReadBigNum(BCReaderState *s, int tag)
3600935992
}
3601035993
a->tab[i] = v;
3601135994
}
35995+
} else
35996+
#endif /* CONFIG_BIGNUM */
35997+
{
35998+
n = len & (sizeof(limb_t) - 1);
35999+
if (n != 0) {
36000+
v = 0;
36001+
for(i = 0; i < n; i++) {
36002+
if (bc_get_u8(s, &v8))
36003+
goto fail;
36004+
v |= (limb_t)v8 << ((sizeof(limb_t) - n + i) * 8);
36005+
}
36006+
a->tab[0] = v;
36007+
i = 1;
36008+
} else {
36009+
i = 0;
36010+
}
36011+
for(; i < l; i++) {
36012+
#if LIMB_BITS == 32
36013+
if (bc_get_u32(s, &v))
36014+
goto fail;
36015+
#else
36016+
if (bc_get_u64(s, &v))
36017+
goto fail;
36018+
#endif
36019+
a->tab[i] = v;
36020+
}
3601236021
}
3601336022
}
3601436023
bc_read_trace(s, "}\n");
@@ -50879,7 +50888,7 @@ static JSValue JS_ToBigIntCtorFree(JSContext *ctx, JSValue val)
5087950888
}
5088050889
if (!bf_is_finite(a)) {
5088150890
JS_FreeValue(ctx, val);
50882-
val = JS_ThrowRangeError(ctx, "cannot convert NaN or Infinity to bigint");
50891+
val = JS_ThrowRangeError(ctx, "cannot convert NaN or Infinity to BigInt");
5088350892
} else {
5088450893
JSValue val1 = JS_NewBigInt(ctx);
5088550894
bf_t *r;
@@ -50897,7 +50906,7 @@ static JSValue JS_ToBigIntCtorFree(JSContext *ctx, JSValue val)
5089750906
val = JS_ThrowOutOfMemory(ctx);
5089850907
} else if (ret & BF_ST_INEXACT) {
5089950908
JS_FreeValue(ctx, val1);
50900-
val = JS_ThrowRangeError(ctx, "cannot convert to bigint: not an integer");
50909+
val = JS_ThrowRangeError(ctx, "cannot convert to BigInt: not an integer");
5090150910
} else {
5090250911
val = JS_CompactBigInt(ctx, val1);
5090350912
}

0 commit comments

Comments
 (0)