Skip to content

Commit bba7df4

Browse files
committed
Move List object to List module
1 parent c191f31 commit bba7df4

File tree

7 files changed

+490
-456
lines changed

7 files changed

+490
-456
lines changed

src/Fable.Transforms/Fable2Babel.fs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ module Util =
373373
makeNativeTypeAnnotation com ctx [genArg] "Array"
374374

375375
and makeListTypeAnnotation com ctx genArg =
376-
makeImportTypeAnnotation com ctx [genArg] "Types" "List"
376+
makeImportTypeAnnotation com ctx [genArg] "List" "List"
377377

378378
and makeUnionTypeAnnotation com ctx genArgs =
379379
List.map (typeAnnotation com ctx) genArgs
@@ -809,12 +809,12 @@ module Util =
809809
// Optimization for bundle size: compile list literals as List.ofArray
810810
| Replacements.ListLiteral(exprs, t) ->
811811
[|List.rev exprs |> makeArray com ctx|]
812-
|> coreLibCall com ctx r "Types" "newList"
812+
|> coreLibCall com ctx r "List" "newList"
813813
| Fable.NewList (headAndTail, _) ->
814814
match headAndTail with
815-
| None -> coreLibCall com ctx r "Types" "newList" [||]
815+
| None -> coreLibCall com ctx r "List" "newList" [||]
816816
| Some(TransformExpr com ctx head, TransformExpr com ctx tail) ->
817-
coreLibCall com ctx r "Types" "cons" [|head; tail|]
817+
coreLibCall com ctx r "List" "cons" [|head; tail|]
818818
| Fable.NewOption (value, t) ->
819819
match value with
820820
| Some (TransformExpr com ctx e) ->
@@ -1054,8 +1054,8 @@ module Util =
10541054
let expr = com.TransformAsExpr(ctx, fableExpr)
10551055
match getKind with
10561056
| Fable.ExprGet(TransformExpr com ctx prop) -> getExpr range expr prop
1057-
| Fable.ListHead -> get range expr "Head"
1058-
| Fable.ListTail -> get range expr "Tail"
1057+
| Fable.ListHead -> coreLibCall com ctx range "List" "head" [|expr|]
1058+
| Fable.ListTail -> coreLibCall com ctx range "List" "tail" [|expr|]
10591059
| Fable.FieldGet(fieldName,_,_) ->
10601060
let expr =
10611061
match fableExpr with
@@ -1151,7 +1151,7 @@ module Util =
11511151
| Fable.FunctionType _ -> jsTypeof "function" expr
11521152
| Fable.Array _ | Fable.Tuple _ ->
11531153
coreLibCall com ctx None "Util" "isArrayLike" [|com.TransformAsExpr(ctx, expr)|]
1154-
| Fable.List _ -> jsInstanceof (coreValue com ctx "Types" "List") expr
1154+
| Fable.List _ -> jsInstanceof (coreValue com ctx "List" "List") expr
11551155
| Replacements.Builtin kind ->
11561156
match kind with
11571157
| Replacements.BclGuid -> jsTypeof "string" expr
@@ -1212,7 +1212,7 @@ module Util =
12121212
let op = if nonEmpty then BinaryUnequal else BinaryEqual
12131213
upcast BinaryExpression(op, com.TransformAsExpr(ctx, expr), NullLiteral(), ?loc=range)
12141214
| Fable.ListTest nonEmpty ->
1215-
let expr = get range (com.TransformAsExpr(ctx, expr)) "IsEmpty"
1215+
let expr = coreLibCall com ctx range "List" "isEmpty" [|com.TransformAsExpr(ctx, expr)|]
12161216
if nonEmpty then upcast UnaryExpression(UnaryNot, expr, ?loc=range)
12171217
else expr
12181218
| Fable.UnionCaseTest(uci, ent) ->

src/Fable.Transforms/Replacements.fs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,8 @@ let identityHash r (arg: Expr) =
717717
Helper.CoreCall("Util", "structuralHash", Number Int32, [arg], ?loc=r)
718718
| DeclaredType(ent,_) when ent.IsFSharpUnion || ent.IsFSharpRecord || ent.IsValueType ->
719719
Helper.CoreCall("Util", "structuralHash", Number Int32, [arg], ?loc=r)
720+
| DeclaredType(ent,_) ->
721+
Helper.InstanceCall(arg, "GetHashCode", Number Int32, [], ?loc=r)
720722
| _ ->
721723
Helper.CoreCall("Util", "identityHash", Number Int32, [arg], ?loc=r)
722724

@@ -1838,30 +1840,20 @@ let arrayModule (com: ICompiler) (ctx: Context) r (t: Type) (i: CallInfo) (_: Ex
18381840
Helper.CoreCall("Array", meth, t, args, i.SignatureArgTypes, ?loc=r) |> Some
18391841

18401842
let lists (com: ICompiler) (ctx: Context) r (t: Type) (i: CallInfo) (thisArg: Expr option) (args: Expr list) =
1843+
let meth = Naming.removeGetSetPrefix i.CompiledName |> Naming.lowerFirst
18411844
match i.CompiledName, thisArg, args with
1842-
// Use methods for Head and Tail (instead of Get(ListHead) for example) to check for empty lists
1843-
| ("get_Head" | "get_Tail" | "get_Length" | "get_IsEmpty"), Some callee, _ ->
1844-
let meth = Naming.removeGetSetPrefix i.CompiledName
1845-
get r t callee meth |> Some
1846-
| "get_Item", Some callee, _ ->
1847-
let meth = Naming.removeGetSetPrefix i.CompiledName
1848-
Helper.InstanceCall(callee, meth, t, args, i.SignatureArgTypes, ?loc=r) |> Some
1849-
| "GetSlice", Some x, _ ->
1850-
let meth = Naming.lowerFirst i.CompiledName
1851-
let args = match args with [ExprType Unit] -> [x] | args -> args @ [x]
1845+
| ("get_Head" | "get_Tail" | "get_Length" | "get_IsEmpty"), Some x, _ ->
1846+
Helper.CoreCall("List", meth, t, [x], i.SignatureArgTypes, ?loc=r) |> Some
1847+
| ("get_Item" | "GetSlice"), Some x, _ ->
1848+
Helper.CoreCall("List", meth, t, args @ [x], i.SignatureArgTypes, ?loc=r) |> Some
1849+
| ("get_Empty" | "Cons"), None, _ ->
18521850
Helper.CoreCall("List", meth, t, args, i.SignatureArgTypes, ?loc=r) |> Some
1853-
| "get_Empty", None, _ -> NewList(None, (genArg com ctx r 0 i.GenericArgs)) |> makeValue r |> Some
1854-
| "Cons", None, [h;t] -> NewList(Some(h,t), (genArg com ctx r 0 i.GenericArgs)) |> makeValue r |> Some
18551851
| ("GetHashCode" | "Equals" | "CompareTo"), Some callee, _ ->
18561852
Helper.InstanceCall(callee, i.CompiledName, t, args, i.SignatureArgTypes, ?loc=r) |> Some
18571853
| _ -> None
18581854

18591855
let listModule (com: ICompiler) (ctx: Context) r (t: Type) (i: CallInfo) (_: Expr option) (args: Expr list) =
18601856
match i.CompiledName, args with
1861-
| "IsEmpty", [x] -> Test(x, ListTest false, r) |> Some
1862-
| "Empty", _ -> NewList(None, (genArg com ctx r 0 i.GenericArgs)) |> makeValue r |> Some
1863-
| "Singleton", [x] ->
1864-
NewList(Some(x, Value(NewList(None, t), None)), (genArg com ctx r 0 i.GenericArgs)) |> makeValue r |> Some
18651857
// Use a cast to give it better chances of optimization (e.g. converting list
18661858
// literals to arrays) after the beta reduction pass
18671859
| "ToSeq", [x] -> toSeq t x |> Some

src/fable-library/Array.fs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ module Helpers =
123123
open Helpers
124124

125125
let private indexNotFoundMsg = "An index satisfying the predicate was not found in the collection."
126-
let inline indexNotFound() = failwith indexNotFoundMsg
127126

128127
// Pay attention when benchmarking to append and filter functions below
129128
// if implementing via native JS array .concat() and .filter() do not fall behind due to js-native transitions.
@@ -478,15 +477,15 @@ let partition (f: 'T -> bool) (source: 'T[]) ([<Inject>] cons: IArrayCons<'T>) =
478477
let find (predicate: 'T -> bool) (array: 'T[]): 'T =
479478
match findImpl predicate array with
480479
| Some res -> res
481-
| None -> indexNotFound()
480+
| None -> failwith indexNotFoundMsg
482481

483482
let tryFind (predicate: 'T -> bool) (array: 'T[]): 'T option =
484483
findImpl predicate array
485484

486485
let findIndex (predicate: 'T -> bool) (array: 'T[]): int =
487486
match findIndexImpl predicate array with
488487
| index when index > -1 -> index
489-
| _ -> indexNotFound()
488+
| _ -> failwith indexNotFoundMsg
490489

491490
let tryFindIndex (predicate: 'T -> bool) (array: 'T[]): int option =
492491
match findIndexImpl predicate array with
@@ -496,7 +495,7 @@ let tryFindIndex (predicate: 'T -> bool) (array: 'T[]): int option =
496495
let pick chooser (array: _[]) =
497496
let rec loop i =
498497
if i >= array.Length then
499-
indexNotFound()
498+
failwith indexNotFoundMsg
500499
else
501500
match chooser array.[i] with
502501
| None -> loop(i+1)
@@ -513,7 +512,7 @@ let tryPick chooser (array: _[]) =
513512

514513
let findBack predicate (array: _[]) =
515514
let rec loop i =
516-
if i < 0 then indexNotFound()
515+
if i < 0 then failwith indexNotFoundMsg
517516
elif predicate array.[i] then array.[i]
518517
else loop (i - 1)
519518
loop (array.Length - 1)
@@ -534,7 +533,7 @@ let findLastIndex predicate (array: _[]) =
534533

535534
let findIndexBack predicate (array: _[]) =
536535
let rec loop i =
537-
if i < 0 then indexNotFound()
536+
if i < 0 then failwith indexNotFoundMsg
538537
elif predicate array.[i] then i
539538
else loop (i - 1)
540539
loop (array.Length - 1)

0 commit comments

Comments
 (0)