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.

implement BigInt.asUintN and BigInt.asIntN methods

+81 -4
+8
examples/spec/bigint.js
··· 26 26 27 27 test('bigint toString', (255n).toString(16), 'ff'); 28 28 29 + test('BigInt.asUintN 0 bits', BigInt.asUintN(0, 123n), 0n); 30 + test('BigInt.asUintN wrap', BigInt.asUintN(8, 256n), 0n); 31 + test('BigInt.asUintN negative', BigInt.asUintN(8, -1n), 255n); 32 + test('BigInt.asIntN 0 bits', BigInt.asIntN(0, 123n), 0n); 33 + test('BigInt.asIntN positive', BigInt.asIntN(8, 127n), 127n); 34 + test('BigInt.asIntN negative', BigInt.asIntN(8, 255n), -1n); 35 + test('BigInt.asIntN sign bit', BigInt.asIntN(8, 128n), -128n); 36 + 29 37 summary();
+73 -4
src/ant.c
··· 675 675 static size_t tostr(struct js *js, jsval_t value, char *buf, size_t len); 676 676 static size_t strpromise(struct js *js, jsval_t value, char *buf, size_t len); 677 677 static size_t js_to_pcre2_pattern(const char *src, size_t src_len, char *dst, size_t dst_size); 678 + static double js_to_number(struct js *js, jsval_t arg); 678 679 679 680 static jsval_t js_stmt_impl(struct js *js); 680 681 static jsval_t js_expr(struct js *js); ··· 796 797 } 797 798 buf[len] = '\0'; 798 799 return len; 800 + } 801 + 802 + static jsval_t bigint_from_u64(struct js *js, uint64_t value) { 803 + char buf[32]; 804 + size_t len = uint_to_str(buf, sizeof(buf), value); 805 + return js_mkbigint(js, buf, len, false); 799 806 } 800 807 801 808 #define MAX_STRINGIFY_DEPTH 64 ··· 2401 2408 return js_mkerr(js, "Cannot convert to BigInt"); 2402 2409 } 2403 2410 2411 + static jsval_t bigint_pow2(struct js *js, uint64_t bits) { 2412 + jsval_t two = js_mkbigint(js, "2", 1, false); 2413 + if (is_err(two)) return two; 2414 + jsval_t exp = bigint_from_u64(js, bits); 2415 + if (is_err(exp)) return exp; 2416 + return bigint_exp(js, two, exp); 2417 + } 2418 + 2419 + static jsval_t bigint_asint_bits(struct js *js, jsval_t arg, uint64_t *bits_out) { 2420 + double bits = js_to_number(js, arg); 2421 + if (!isfinite(bits) || bits < 0 || bits != floor(bits)) { 2422 + return js_mkerr_typed(js, JS_ERR_RANGE, "Invalid bits"); 2423 + } 2424 + if (bits > 18446744073709551615.0) { 2425 + return js_mkerr_typed(js, JS_ERR_RANGE, "Invalid bits"); 2426 + } 2427 + *bits_out = (uint64_t)bits; 2428 + return js_mkundef(); 2429 + } 2430 + 2404 2431 static jsval_t builtin_BigInt_asIntN(struct js *js, jsval_t *args, int nargs) { 2405 - (void)js; (void)args; (void)nargs; 2406 - return js_mkerr(js, "BigInt.asIntN not implemented"); 2432 + if (nargs < 2) return js_mkerr(js, "BigInt.asIntN requires 2 arguments"); 2433 + uint64_t bits = 0; 2434 + jsval_t err = bigint_asint_bits(js, args[0], &bits); 2435 + if (is_err(err)) return err; 2436 + if (vtype(args[1]) != T_BIGINT) { 2437 + return js_mkerr_typed(js, JS_ERR_TYPE, "Cannot convert to BigInt"); 2438 + } 2439 + if (bits == 0) return js_mkbigint(js, "0", 1, false); 2440 + 2441 + jsval_t mod = bigint_pow2(js, bits); 2442 + if (is_err(mod)) return mod; 2443 + jsval_t res = bigint_mod(js, args[1], mod); 2444 + if (is_err(res)) return res; 2445 + if (bigint_IsNegative(js, res)) { 2446 + jsval_t adj = bigint_add(js, res, mod); 2447 + if (is_err(adj)) return adj; 2448 + res = adj; 2449 + } 2450 + 2451 + jsval_t threshold = bigint_pow2(js, bits - 1); 2452 + if (is_err(threshold)) return threshold; 2453 + if (bigint_compare(js, res, threshold) >= 0) { 2454 + jsval_t adj = bigint_sub(js, res, mod); 2455 + if (is_err(adj)) return adj; 2456 + res = adj; 2457 + } 2458 + return res; 2407 2459 } 2408 2460 2409 2461 static jsval_t builtin_BigInt_asUintN(struct js *js, jsval_t *args, int nargs) { 2410 - (void)js; (void)args; (void)nargs; 2411 - return js_mkerr(js, "BigInt.asUintN not implemented"); 2462 + if (nargs < 2) return js_mkerr(js, "BigInt.asUintN requires 2 arguments"); 2463 + uint64_t bits = 0; 2464 + jsval_t err = bigint_asint_bits(js, args[0], &bits); 2465 + if (is_err(err)) return err; 2466 + if (vtype(args[1]) != T_BIGINT) { 2467 + return js_mkerr_typed(js, JS_ERR_TYPE, "Cannot convert to BigInt"); 2468 + } 2469 + if (bits == 0) return js_mkbigint(js, "0", 1, false); 2470 + 2471 + jsval_t mod = bigint_pow2(js, bits); 2472 + if (is_err(mod)) return mod; 2473 + jsval_t res = bigint_mod(js, args[1], mod); 2474 + if (is_err(res)) return res; 2475 + if (bigint_IsNegative(js, res)) { 2476 + jsval_t adj = bigint_add(js, res, mod); 2477 + if (is_err(adj)) return adj; 2478 + res = adj; 2479 + } 2480 + return res; 2412 2481 } 2413 2482 2414 2483 static jsval_t builtin_bigint_toString(struct js *js, jsval_t *args, int nargs) {