Fix compilation with -DCONFIG_BIGNUM

- disable BigDecimal convertion in `JS_ReadBigNum`
- fix some error messages
This commit is contained in:
Charlie Gordon 2024-03-22 11:23:33 +01:00
parent 65ecb0b0d6
commit 06651314f5
2 changed files with 43 additions and 32 deletions

View file

@ -136,6 +136,7 @@ static inline slimb_t ceil_div(slimb_t a, slimb_t b)
return a / b; return a / b;
} }
#ifdef USE_BF_DEC
/* b must be >= 1 */ /* b must be >= 1 */
static inline slimb_t floor_div(slimb_t a, slimb_t b) static inline slimb_t floor_div(slimb_t a, slimb_t b)
{ {
@ -145,6 +146,7 @@ static inline slimb_t floor_div(slimb_t a, slimb_t b)
return (a - b + 1) / b; return (a - b + 1) / b;
} }
} }
#endif
/* return r = a modulo b (0 <= r <= b - 1. b must be >= 1 */ /* return r = a modulo b (0 <= r <= b - 1. b must be >= 1 */
static inline limb_t smod(slimb_t a, slimb_t b) static inline limb_t smod(slimb_t a, slimb_t b)

View file

@ -35896,11 +35896,10 @@ static JSValue JS_ReadBigNum(BCReaderState *s, int tag)
uint8_t v8; uint8_t v8;
int32_t e; int32_t e;
uint32_t len; uint32_t len;
limb_t l, i, n, j; limb_t l, i, n;
JSBigFloat *p; JSBigFloat *p;
limb_t v; limb_t v;
bf_t *a; bf_t *a;
int bpos, d;
p = js_new_bf(s->ctx); p = js_new_bf(s->ctx);
if (!p) if (!p)
@ -35950,39 +35949,23 @@ static JSValue JS_ReadBigNum(BCReaderState *s, int tag)
JS_ThrowInternalError(s->ctx, "invalid bignum length"); JS_ThrowInternalError(s->ctx, "invalid bignum length");
goto fail; goto fail;
} }
if (tag != BC_TAG_BIG_DECIMAL) #ifdef CONFIG_BIGNUM
l = (len + sizeof(limb_t) - 1) / sizeof(limb_t); if (tag == BC_TAG_BIG_DECIMAL) {
else
l = (len + LIMB_DIGITS - 1) / LIMB_DIGITS; l = (len + LIMB_DIGITS - 1) / LIMB_DIGITS;
} else
#endif
{
l = (len + sizeof(limb_t) - 1) / sizeof(limb_t);
}
if (bf_resize(a, l)) { if (bf_resize(a, l)) {
JS_ThrowOutOfMemory(s->ctx); JS_ThrowOutOfMemory(s->ctx);
goto fail; goto fail;
} }
if (tag != BC_TAG_BIG_DECIMAL) { #ifdef CONFIG_BIGNUM
n = len & (sizeof(limb_t) - 1); if (tag == BC_TAG_BIG_DECIMAL) {
if (n != 0) { limb_t j;
v = 0; int bpos, d;
for(i = 0; i < n; i++) {
if (bc_get_u8(s, &v8))
goto fail;
v |= (limb_t)v8 << ((sizeof(limb_t) - n + i) * 8);
}
a->tab[0] = v;
i = 1;
} else {
i = 0;
}
for(; i < l; i++) {
#if LIMB_BITS == 32
if (bc_get_u32(s, &v))
goto fail;
#else
if (bc_get_u64(s, &v))
goto fail;
#endif
a->tab[i] = v;
}
} else {
bpos = 0; bpos = 0;
for(i = 0; i < l; i++) { for(i = 0; i < l; i++) {
if (i == 0 && (n = len % LIMB_DIGITS) != 0) { if (i == 0 && (n = len % LIMB_DIGITS) != 0) {
@ -36009,6 +35992,32 @@ static JSValue JS_ReadBigNum(BCReaderState *s, int tag)
} }
a->tab[i] = v; a->tab[i] = v;
} }
} else
#endif /* CONFIG_BIGNUM */
{
n = len & (sizeof(limb_t) - 1);
if (n != 0) {
v = 0;
for(i = 0; i < n; i++) {
if (bc_get_u8(s, &v8))
goto fail;
v |= (limb_t)v8 << ((sizeof(limb_t) - n + i) * 8);
}
a->tab[0] = v;
i = 1;
} else {
i = 0;
}
for(; i < l; i++) {
#if LIMB_BITS == 32
if (bc_get_u32(s, &v))
goto fail;
#else
if (bc_get_u64(s, &v))
goto fail;
#endif
a->tab[i] = v;
}
} }
} }
bc_read_trace(s, "}\n"); bc_read_trace(s, "}\n");
@ -50879,7 +50888,7 @@ static JSValue JS_ToBigIntCtorFree(JSContext *ctx, JSValue val)
} }
if (!bf_is_finite(a)) { if (!bf_is_finite(a)) {
JS_FreeValue(ctx, val); JS_FreeValue(ctx, val);
val = JS_ThrowRangeError(ctx, "cannot convert NaN or Infinity to bigint"); val = JS_ThrowRangeError(ctx, "cannot convert NaN or Infinity to BigInt");
} else { } else {
JSValue val1 = JS_NewBigInt(ctx); JSValue val1 = JS_NewBigInt(ctx);
bf_t *r; bf_t *r;
@ -50897,7 +50906,7 @@ static JSValue JS_ToBigIntCtorFree(JSContext *ctx, JSValue val)
val = JS_ThrowOutOfMemory(ctx); val = JS_ThrowOutOfMemory(ctx);
} else if (ret & BF_ST_INEXACT) { } else if (ret & BF_ST_INEXACT) {
JS_FreeValue(ctx, val1); JS_FreeValue(ctx, val1);
val = JS_ThrowRangeError(ctx, "cannot convert to bigint: not an integer"); val = JS_ThrowRangeError(ctx, "cannot convert to BigInt: not an integer");
} else { } else {
val = JS_CompactBigInt(ctx, val1); val = JS_CompactBigInt(ctx, val1);
} }