Skip to content

Commit 1f6d785

Browse files
committed
[MERGE #5788 @duongnhn] BigInt: reallocate space, digit_t, increment, decrement
Merge pull request #5788 from duongnhn:user/duongn/bigint_size_t In this PR, we implement - reallocate space for BigInt if necessary -> make it arbitrarily-precision. - make use of configuration with digit_t - implement increment/decrement operators - native tests for methods in Javascript BigInt class I defer "assign along with inc/dec" `y=x++` and `y=++x` to a future PR
2 parents 21624b8 + 88ce20f commit 1f6d785

File tree

13 files changed

+627
-27
lines changed

13 files changed

+627
-27
lines changed

bin/ChakraCore/TestHooks.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ int LogicalStringCompareImpl(const char16* p1, int p1size, const char16* p2, int
1919
}
2020
}
2121

22+
namespace Js
23+
{
24+
static digit_t AddDigit(digit_t a, digit_t b, digit_t * carry);
25+
static digit_t SubtractDigit(digit_t a, digit_t b, digit_t * borrow);
26+
static digit_t MulDigit(digit_t a, digit_t b, digit_t * high);
27+
}
28+
2229
#ifdef ENABLE_TEST_HOOKS
2330

2431
HRESULT __stdcall SetConfigFlags(__in int argc, __in_ecount(argc) LPWSTR argv[], ICustomConfigFlags* customConfigFlags)
@@ -168,6 +175,11 @@ HRESULT OnChakraCoreLoaded(OnChakraCoreLoadedPtr pfChakraCoreLoaded)
168175
SetEnableCheckMemoryLeakOutput,
169176
PlatformAgnostic::UnicodeText::Internal::LogicalStringCompareImpl,
170177

178+
//BigInt hooks
179+
Js::JavascriptBigInt::AddDigit,
180+
Js::JavascriptBigInt::SubDigit,
181+
Js::JavascriptBigInt::MulDigit,
182+
171183
#define FLAG(type, name, description, defaultValue, ...) FLAG_##type##(name)
172184
#define FLAGINCLUDE(name) \
173185
IsEnabled##name##Flag, \

bin/ChakraCore/TestHooks.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ struct TestHooks
3131
SetEnableCheckMemoryLeakOutputPtr pfSetEnableCheckMemoryLeakOutput;
3232
LogicalStringCompareImpl pfLogicalCompareStringImpl;
3333

34+
// Javasscript Bigint hooks
35+
typedef digit_t(TESTHOOK_CALL *AddDigit)(digit_t a, digit_t b, digit_t* carry);
36+
typedef digit_t(TESTHOOK_CALL *SubDigit)(digit_t a, digit_t b, digit_t* borrow);
37+
typedef digit_t(TESTHOOK_CALL *MulDigit)(digit_t a, digit_t b, digit_t* high);
38+
AddDigit pfAddDigit;
39+
SubDigit pfSubDigit;
40+
MulDigit pfMulDigit;
41+
3442
#define FLAG(type, name, description, defaultValue, ...) FLAG_##type##(name)
3543
#define FLAG_String(name) \
3644
bool (TESTHOOK_CALL *pfIsEnabled##name##Flag)(); \
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//-------------------------------------------------------------------------------------------------------
2+
// Copyright (C) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
4+
//-------------------------------------------------------------------------------------------------------
5+
6+
#include "stdafx.h"
7+
#pragma warning(disable:26434) // Function definition hides non-virtual function in base class
8+
#pragma warning(disable:26439) // Implicit noexcept
9+
#pragma warning(disable:26451) // Arithmetic overflow
10+
#pragma warning(disable:26495) // Uninitialized member variable
11+
#include "catch.hpp"
12+
13+
#pragma warning(disable:4100) // unreferenced formal parameter
14+
#pragma warning(disable:6387) // suppressing preFAST which raises warning for passing null to the JsRT APIs
15+
#pragma warning(disable:6262) // CATCH is using stack variables to report errors, suppressing the preFAST warning.
16+
17+
namespace JavascriptBigIntTests
18+
{
19+
void Test_AddDigit(digit_t digit1, digit_t digit2, digit_t * carry, digit_t expectedResult, digit_t expectedCarry)
20+
{
21+
REQUIRE(g_testHooksLoaded);
22+
23+
digit_t res = g_testHooks.pfAddDigit(digit1, digit2, carry);
24+
25+
//test to check that the result from call to AddDigit is the expected value
26+
REQUIRE(res == expectedResult);
27+
REQUIRE(expectedCarry == *carry);
28+
}
29+
30+
void Test_SubDigit(digit_t digit1, digit_t digit2, digit_t * borrow, digit_t expectedResult, digit_t expectedBorrow)
31+
{
32+
REQUIRE(g_testHooksLoaded);
33+
34+
digit_t res = g_testHooks.pfSubDigit(digit1, digit2, borrow);
35+
36+
//test to check that the result from call to SubtractDigit is the expected value
37+
REQUIRE(res == expectedResult);
38+
REQUIRE(*borrow == expectedBorrow);
39+
}
40+
41+
void Test_MulDigit(digit_t digit1, digit_t digit2, digit_t * high, digit_t expectedResult, digit_t expectedHigh)
42+
{
43+
REQUIRE(g_testHooksLoaded);
44+
45+
digit_t res = g_testHooks.pfMulDigit(digit1, digit2, high);
46+
47+
//test to check that the result from call to SubtractDigit is the expected value
48+
REQUIRE(res == expectedResult);
49+
REQUIRE(*high == expectedHigh);
50+
}
51+
52+
TEST_CASE("AddDigit", "[JavascriptBigIntTests]")
53+
{
54+
digit_t carry = 0;
55+
Test_AddDigit(1, 2, &carry, 3, 0);
56+
57+
digit_t d1 = UINTPTR_MAX;
58+
digit_t d2 = UINTPTR_MAX;
59+
carry = 0;
60+
Test_AddDigit(d1, d2, &carry, UINTPTR_MAX-1, 1);
61+
}
62+
63+
TEST_CASE("SubDigit", "[JavascriptBigIntTests]")
64+
{
65+
digit_t borrow = 0;
66+
Test_SubDigit(3, 2, &borrow, 1, 0);
67+
68+
digit_t d1 = 0;
69+
digit_t d2 = 1;
70+
borrow = 0;
71+
Test_SubDigit(d1, d2, &borrow, UINTPTR_MAX, 1);
72+
}
73+
74+
TEST_CASE("MulDigit", "[JavascriptBigIntTests]")
75+
{
76+
digit_t high = 0;
77+
Test_MulDigit(3, 2, &high, 6, 0);
78+
79+
digit_t d1 = UINTPTR_MAX;
80+
digit_t d2 = 2;
81+
high = 0;
82+
Test_MulDigit(d1, d2, &high, UINTPTR_MAX-1, 1);
83+
}
84+
}

bin/NativeTests/NativeTests.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
<ClInclude Include="stdafx.h" />
4949
</ItemGroup>
5050
<ItemGroup>
51+
<ClCompile Include="JavascriptBigIntTests.cpp" />
5152
<ClCompile Include="BigUIntTest.cpp" />
5253
<ClCompile Include="CodexAssert.cpp" />
5354
<ClCompile Include="CodexTests.cpp" />

lib/Common/Core/CommonTypedefs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,6 @@ namespace Js
6161
{
6262
typedef uint32 LocalFunctionId;
6363
};
64+
65+
// digit_t represents a digit in bigint underline
66+
typedef uintptr_t digit_t;

0 commit comments

Comments
 (0)