Skip to content

Commit

Permalink
[MERGE #5788 @duongnhn] BigInt: reallocate space, digit_t, increment,…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
duongnhn committed Oct 22, 2018
2 parents 21624b8 + 88ce20f commit 1f6d785
Show file tree
Hide file tree
Showing 13 changed files with 627 additions and 27 deletions.
12 changes: 12 additions & 0 deletions bin/ChakraCore/TestHooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ int LogicalStringCompareImpl(const char16* p1, int p1size, const char16* p2, int
}
}

namespace Js
{
static digit_t AddDigit(digit_t a, digit_t b, digit_t * carry);
static digit_t SubtractDigit(digit_t a, digit_t b, digit_t * borrow);
static digit_t MulDigit(digit_t a, digit_t b, digit_t * high);
}

#ifdef ENABLE_TEST_HOOKS

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

//BigInt hooks
Js::JavascriptBigInt::AddDigit,
Js::JavascriptBigInt::SubDigit,
Js::JavascriptBigInt::MulDigit,

#define FLAG(type, name, description, defaultValue, ...) FLAG_##type##(name)
#define FLAGINCLUDE(name) \
IsEnabled##name##Flag, \
Expand Down
8 changes: 8 additions & 0 deletions bin/ChakraCore/TestHooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ struct TestHooks
SetEnableCheckMemoryLeakOutputPtr pfSetEnableCheckMemoryLeakOutput;
LogicalStringCompareImpl pfLogicalCompareStringImpl;

// Javasscript Bigint hooks
typedef digit_t(TESTHOOK_CALL *AddDigit)(digit_t a, digit_t b, digit_t* carry);
typedef digit_t(TESTHOOK_CALL *SubDigit)(digit_t a, digit_t b, digit_t* borrow);
typedef digit_t(TESTHOOK_CALL *MulDigit)(digit_t a, digit_t b, digit_t* high);
AddDigit pfAddDigit;
SubDigit pfSubDigit;
MulDigit pfMulDigit;

#define FLAG(type, name, description, defaultValue, ...) FLAG_##type##(name)
#define FLAG_String(name) \
bool (TESTHOOK_CALL *pfIsEnabled##name##Flag)(); \
Expand Down
84 changes: 84 additions & 0 deletions bin/NativeTests/JavascriptBigIntTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------

#include "stdafx.h"
#pragma warning(disable:26434) // Function definition hides non-virtual function in base class
#pragma warning(disable:26439) // Implicit noexcept
#pragma warning(disable:26451) // Arithmetic overflow
#pragma warning(disable:26495) // Uninitialized member variable
#include "catch.hpp"

#pragma warning(disable:4100) // unreferenced formal parameter
#pragma warning(disable:6387) // suppressing preFAST which raises warning for passing null to the JsRT APIs
#pragma warning(disable:6262) // CATCH is using stack variables to report errors, suppressing the preFAST warning.

namespace JavascriptBigIntTests
{
void Test_AddDigit(digit_t digit1, digit_t digit2, digit_t * carry, digit_t expectedResult, digit_t expectedCarry)
{
REQUIRE(g_testHooksLoaded);

digit_t res = g_testHooks.pfAddDigit(digit1, digit2, carry);

//test to check that the result from call to AddDigit is the expected value
REQUIRE(res == expectedResult);
REQUIRE(expectedCarry == *carry);
}

void Test_SubDigit(digit_t digit1, digit_t digit2, digit_t * borrow, digit_t expectedResult, digit_t expectedBorrow)
{
REQUIRE(g_testHooksLoaded);

digit_t res = g_testHooks.pfSubDigit(digit1, digit2, borrow);

//test to check that the result from call to SubtractDigit is the expected value
REQUIRE(res == expectedResult);
REQUIRE(*borrow == expectedBorrow);
}

void Test_MulDigit(digit_t digit1, digit_t digit2, digit_t * high, digit_t expectedResult, digit_t expectedHigh)
{
REQUIRE(g_testHooksLoaded);

digit_t res = g_testHooks.pfMulDigit(digit1, digit2, high);

//test to check that the result from call to SubtractDigit is the expected value
REQUIRE(res == expectedResult);
REQUIRE(*high == expectedHigh);
}

TEST_CASE("AddDigit", "[JavascriptBigIntTests]")
{
digit_t carry = 0;
Test_AddDigit(1, 2, &carry, 3, 0);

digit_t d1 = UINTPTR_MAX;
digit_t d2 = UINTPTR_MAX;
carry = 0;
Test_AddDigit(d1, d2, &carry, UINTPTR_MAX-1, 1);
}

TEST_CASE("SubDigit", "[JavascriptBigIntTests]")
{
digit_t borrow = 0;
Test_SubDigit(3, 2, &borrow, 1, 0);

digit_t d1 = 0;
digit_t d2 = 1;
borrow = 0;
Test_SubDigit(d1, d2, &borrow, UINTPTR_MAX, 1);
}

TEST_CASE("MulDigit", "[JavascriptBigIntTests]")
{
digit_t high = 0;
Test_MulDigit(3, 2, &high, 6, 0);

digit_t d1 = UINTPTR_MAX;
digit_t d2 = 2;
high = 0;
Test_MulDigit(d1, d2, &high, UINTPTR_MAX-1, 1);
}
}
1 change: 1 addition & 0 deletions bin/NativeTests/NativeTests.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<ClInclude Include="stdafx.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="JavascriptBigIntTests.cpp" />
<ClCompile Include="BigUIntTest.cpp" />
<ClCompile Include="CodexAssert.cpp" />
<ClCompile Include="CodexTests.cpp" />
Expand Down
3 changes: 3 additions & 0 deletions lib/Common/Core/CommonTypedefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,6 @@ namespace Js
{
typedef uint32 LocalFunctionId;
};

// digit_t represents a digit in bigint underline
typedef uintptr_t digit_t;
Loading

0 comments on commit 1f6d785

Please sign in to comment.