MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

BigInt number coercion

+21 -3
+1
include/modules/bigint.h
··· 24 24 25 25 bool bigint_is_negative(ant_t *js, ant_value_t v); 26 26 bool bigint_is_zero(ant_t *js, ant_value_t v); 27 + double bigint_to_double(ant_t *js, ant_value_t v); 27 28 28 29 size_t bigint_digits_len(ant_t *js, ant_value_t v); 29 30 size_t strbigint(ant_t *js, ant_value_t value, char *buf, size_t len);
+5 -3
src/ant.c
··· 2642 2642 } 2643 2643 2644 2644 double js_to_number(ant_t *js, ant_value_t arg) { 2645 - if (vtype(arg) == T_NUM) return tod(arg); 2646 - if (vtype(arg) == T_BOOL) return vdata(arg) ? 1.0 : 0.0; 2647 2645 if (vtype(arg) == T_NULL) return 0.0; 2648 2646 if (vtype(arg) == T_UNDEF) return JS_NAN; 2649 2647 2648 + if (vtype(arg) == T_NUM) return tod(arg); 2649 + if (vtype(arg) == T_BOOL) return vdata(arg) ? 1.0 : 0.0; 2650 + if (vtype(arg) == T_BIGINT) return bigint_to_double(js, arg); 2651 + 2650 2652 if (vtype(arg) == T_STR) { 2651 2653 ant_offset_t len, off = vstr(js, arg, &len); 2652 2654 const char *s = (char *)(uintptr_t)(off), *end; ··· 2656 2658 while (*end == ' ' || *end == '\t' || *end == '\n' || *end == '\r') end++; 2657 2659 return (end == s || *end) ? JS_NAN : val; 2658 2660 } 2659 - 2661 + 2660 2662 if (vtype(arg) == T_OBJ || vtype(arg) == T_ARR) { 2661 2663 if (vtype(arg) == T_OBJ) { 2662 2664 ant_value_t prim = js_call_valueOf(js, arg);
+15
src/modules/bigint.c
··· 92 92 return payload->limbs; 93 93 } 94 94 95 + double bigint_to_double(ant_t *js, ant_value_t v) { 96 + size_t count; 97 + const uint32_t *limbs = bigint_limbs(js, v, &count); 98 + if (limbs_is_zero(limbs, count)) return 0.0; 99 + 100 + double result = 0.0; 101 + double base = 1.0; 102 + for (size_t i = 0; i < count; i++) { 103 + result += (double)limbs[i] * base; 104 + base *= (double)BIGINT_BASE; 105 + } 106 + 107 + return bigint_is_negative(js, v) ? -result : result; 108 + } 109 + 95 110 static ant_value_t js_mkbigint_limbs(ant_t *js, const uint32_t *limbs, size_t count, bool negative) { 96 111 uint32_t zero = 0; 97 112