@ 261103.1

261103.1 · 9f2fdfa2a923784a73cc97da9b369edaf4c6c243

Fix BigInt::toString for values larger than 64 bits (#18)

BigInt values wider than 64 bits (for example 2^64, 10^40 or 2^128-1) gave wrong strings from bigintToString. Rework the division loop so it divides in place, resets the remainder on every pass and trims leading zeros. Also correct the per-word character estimate used to reserve the output string.

Add a BigIntToStringMultiWord test that compares the result against the engine's BigInt.prototype.toString for radixes 2-36.

Co-authored-by: Anukrati Agrawal <anuagra@microsoft.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 6571d0a2-c95e-4c83-8fb7-82debbd8de73

reading declarations

Changes to tests/. All files

tests/JsiRuntimeTests.cpp

@@ -5,6 +5,7 @@ #include <ApiLoaders/V8Api.h> #include <NodeApiJsiRuntime.h> #include <jsi/test/testlib.h>+#include <string> #include <vector>  using namespace Microsoft::NodeApiJsi;@@ -51,3 +52,41 @@ std::vector<RuntimeFactory> runtimeGenerators() { }  } // namespace facebook::jsi++#if JSI_VERSION >= 8+namespace facebook::jsi {++class NodeApiJsiRuntimeTest : public JSITestBase {};++// BigInt::toString must match the engine's BigInt.prototype.toString for values that span several 64-bit words.+TEST_P(NodeApiJsiRuntimeTest, BigIntToStringMultiWord) {+  const char *values[] = {+      "0x10000000000000000n",+      "0x100000000000000000000000n",+      "0xffffffffffffffffffffffffffffffffn",+      "0x100000000000000000000000000000000n",+      "10n ** 40n",+      "36n ** 30n",+      "0x123456789abcdef0fedcba98765432100123456789abcdefn",+      "(1n << 512n) - 1n",+      "(1n << 1000n) + 12345n",+      "0xdeadbeef00000000000000000000000000000000cafebaben",+  };++  for (const char *value : values) {+    for (const char *sign : {"", "-"}) {+      std::string expr = std::string{sign} + "(" + value + ")";+      BigInt bigint = eval(expr.c_str()).asBigInt(rt);+      Function toString = eval("(function(v, r) { return v.toString(r); })").getObject(rt).getFunction(rt);+      for (int radix = 2; radix <= 36; ++radix) {+        std::string expected = toString.call(rt, bigint, radix).getString(rt).utf8(rt);+        EXPECT_EQ(bigint.toString(rt, radix).utf8(rt), expected) << expr << " radix " << radix;+      }+    }+  }+}++INSTANTIATE_TEST_SUITE_P(Runtimes, NodeApiJsiRuntimeTest, ::testing::ValuesIn(runtimeGenerators()));++} // namespace facebook::jsi+#endif