Skip to content

Commit 7bcdec7

Browse files
Allow undefined in TypedArray.prototype.sort(comparefn) (#6842)
According to the spec, TypedArray.prototype.sort(comparefn) should allow comparefn to be undefined. In this case .sort(undefined) should be equivalent to .sort(). See https://tc39.es/ecma262/#sec-%typedarray%.prototype.sort Before it threw a TypeError. Fix #6503
1 parent f63de5d commit 7bcdec7

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

lib/Runtime/Library/TypedArray.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//-------------------------------------------------------------------------------------------------------
22
// Copyright (C) Microsoft. All rights reserved.
3-
// Copyright (c) 2021 ChakraCore Project Contributors. All rights reserved.
3+
// Copyright (c) 2022 ChakraCore Project Contributors. All rights reserved.
44
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
55
//-------------------------------------------------------------------------------------------------------
66
// Implementation for typed arrays based on ArrayBuffer.
@@ -2263,7 +2263,7 @@ namespace Js
22632263

22642264
RecyclableObject* compareFn = nullptr;
22652265

2266-
if (args.Info.Count > 1)
2266+
if (args.Info.Count > 1 && !JavascriptOperators::IsUndefined(args[1]))
22672267
{
22682268
if (!JavascriptConversion::IsCallable(args[1]))
22692269
{

test/typedarray/rlexe.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,4 +435,9 @@ Below test fails with difference in space. Investigate the cause and re-enable t
435435
<files>definitetypedarray.js</files>
436436
</default>
437437
</test>
438+
<test>
439+
<default>
440+
<files>sort.js</files>
441+
</default>
442+
</test>
438443
</regress-exe>

test/typedarray/sort.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//-------------------------------------------------------------------------------------------------------
2+
// Copyright (C) Microsoft. All rights reserved.
3+
// Copyright (c) 2022 ChakraCore Project Contributors. All rights reserved.
4+
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
5+
//-------------------------------------------------------------------------------------------------------
6+
7+
const array = new Uint32Array([3, 1, 2]);
8+
9+
// May not throw; See https://tc39.es/ecma262/#sec-%typedarray%.prototype.sort
10+
array.sort(undefined);
11+
12+
print("pass");

0 commit comments

Comments
 (0)