Skip to content

Commit dc4b07e

Browse files
author
Maxime Mangel
authored
Merge pull request #3715 from chkn/add-system-array-resize
[JS] Add support for System.Array.Resize
2 parents 88003be + 2d4720d commit dc4b07e

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed

src/Fable.Cli/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
103103
* `DateTime.TryParse`
104104
* `DateTime.SpecifyKind`
105105

106+
#### JavaScript
107+
108+
* [GH-3715](https://github.com/fable-compiler/Fable/pull/3715) Add support for System.Array.Resize (by @chkn)
109+
106110
## 4.9.0 - 2023-12-14
107111

108112
### Fixed

src/Fable.Transforms/Replacements.fs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3369,6 +3369,22 @@ let arrays
33693369
)
33703370
|> Some
33713371
| "GetEnumerator", Some arg, _ -> getEnumerator com r t arg |> Some
3372+
| "Resize", None, args ->
3373+
let args =
3374+
args @ [ getZero com ctx (List.head i.GenericArgs) ]
3375+
|> injectArg com ctx r "Array" "resize" i.GenericArgs
3376+
3377+
Helper.LibCall(
3378+
com,
3379+
"Array",
3380+
"resize",
3381+
Unit,
3382+
args,
3383+
i.SignatureArgTypes,
3384+
genArgs = i.GenericArgs,
3385+
?loc = r
3386+
)
3387+
|> Some
33723388
| _ -> None
33733389

33743390
let arrayModule

src/Fable.Transforms/ReplacementsInject.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ let fableReplacementsModules =
5050
"insertAt", (Types.arrayCons, 0)
5151
"insertManyAt", (Types.arrayCons, 0)
5252
"updateAt", (Types.arrayCons, 0)
53+
"resize", (Types.arrayCons, 0)
5354
]
5455
"List",
5556
Map

src/fable-library/Array.fs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,3 +1405,29 @@ let updateAt
14051405
xs.[i]
14061406

14071407
target
1408+
1409+
let resize
1410+
(xs: byref<'T[]>)
1411+
(newSize: int)
1412+
([<OptionalArgument>] zero: 'T option)
1413+
([<OptionalArgument; Inject>] cons: Cons<'T>)
1414+
: unit
1415+
=
1416+
if newSize < 0 then
1417+
invalidArg "newSize" "The input must be non-negative."
1418+
1419+
let len = xs.Length
1420+
1421+
if newSize < len then
1422+
xs <- subArrayImpl xs 0 newSize
1423+
1424+
elif newSize > len then
1425+
let target = allocateArrayFromCons cons newSize
1426+
copyTo xs 0 target 0 len
1427+
1428+
xs <-
1429+
fillImpl
1430+
target
1431+
(defaultArg zero Unchecked.defaultof<_>)
1432+
len
1433+
(newSize - len)

tests/Js/Main/ArrayTests.fs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,4 +1190,13 @@ let tests =
11901190
equal c1 true
11911191
equal c2 -1
11921192
equal c3 1
1193+
1194+
testCase "Array.Resize works" <| fun () ->
1195+
let mutable xs = [|1; 2; 3; 4; 5|]
1196+
Array.Resize(&xs, 3)
1197+
xs |> equal [|1; 2; 3|]
1198+
Array.Resize(&xs, 7)
1199+
xs |> equal [|1; 2; 3; 0; 0; 0; 0|]
1200+
Array.Resize(&xs, 0)
1201+
xs |> equal [||]
11931202
]

0 commit comments

Comments
 (0)