Skip to content

Commit fefc048

Browse files
committed
Array-based List type
1 parent 8441abe commit fefc048

File tree

3 files changed

+435
-369
lines changed

3 files changed

+435
-369
lines changed

src/Fable.Transforms/Fable2Babel.fs

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ module Annotation =
438438
makeNativeTypeAnnotation com ctx [genArg] "Array"
439439

440440
let makeListTypeAnnotation com ctx genArg =
441-
makeImportTypeAnnotation com ctx [genArg] "Types" "List"
441+
makeImportTypeAnnotation com ctx [genArg] "List" "List"
442442

443443
let makeUnionTypeAnnotation com ctx genArgs =
444444
List.map (typeAnnotation com ctx) genArgs
@@ -663,12 +663,6 @@ module Util =
663663
| [] -> expr
664664
| m::ms -> get None expr m |> getParts ms
665665

666-
let makeList com ctx r headAndTail =
667-
match headAndTail with
668-
| None -> [||]
669-
| Some(TransformExpr com ctx head, TransformExpr com ctx tail) -> [|head; tail|]
670-
|> libConsCall com ctx r "Types" "List"
671-
672666
let makeArray (com: IBabelCompiler) ctx exprs =
673667
List.mapToArray (fun e -> com.TransformAsExpr(ctx, e)) exprs
674668
|> ArrayExpression :> Expression
@@ -931,27 +925,39 @@ module Util =
931925
| Fable.NewArray (values, typ) -> makeTypedArray com ctx typ values
932926
| Fable.NewArrayFrom (size, typ) -> makeTypedAllocatedFrom com ctx typ size
933927
| Fable.NewTuple vals -> makeArray com ctx vals
934-
| Fable.NewList (headAndTail, _) when List.contains "FABLE_LIBRARY" com.Options.Define ->
935-
makeList com ctx r headAndTail
928+
// | Fable.NewList (headAndTail, _) when List.contains "FABLE_LIBRARY" com.Options.Define ->
929+
// makeList com ctx r headAndTail
936930
// Optimization for bundle size: compile list literals as List.ofArray
931+
| Replacements.ListLiteral(exprs, t) ->
932+
[|List.rev exprs |> makeArray com ctx|]
933+
|> libCall com ctx r "List" "newList"
934+
// match exprs with
935+
// | [] -> libCall com ctx r "List" "empty" [||]
936+
// | [TransformExpr com ctx expr] -> libCall com ctx r "List" "singleton" [|expr|]
937+
// | exprs -> [|makeArray com ctx exprs|] |> libCall com ctx r "List" "ofArray"
937938
| Fable.NewList (headAndTail, _) ->
938-
let rec getItems acc = function
939-
| None -> List.rev acc, None
940-
| Some(head, Fable.Value(Fable.NewList(tail, _),_)) -> getItems (head::acc) tail
941-
| Some(head, tail) -> List.rev (head::acc), Some tail
942-
match getItems [] headAndTail with
943-
| [], None ->
944-
libCall com ctx r "List" "empty" [||]
945-
| [TransformExpr com ctx expr], None ->
946-
libCall com ctx r "List" "singleton" [|expr|]
947-
| exprs, None ->
948-
[|makeArray com ctx exprs|]
949-
|> libCall com ctx r "List" "ofArray"
950-
| [TransformExpr com ctx head], Some(TransformExpr com ctx tail) ->
939+
match headAndTail with
940+
| None -> libCall com ctx r "List" "empty" [||]
941+
| Some(TransformExpr com ctx head, TransformExpr com ctx tail) ->
951942
libCall com ctx r "List" "cons" [|head; tail|]
952-
| exprs, Some(TransformExpr com ctx tail) ->
953-
[|makeArray com ctx exprs; tail|]
954-
|> libCall com ctx r "List" "ofArrayWithTail"
943+
944+
// let rec getItems acc = function
945+
// | None -> List.rev acc, None
946+
// | Some(head, Fable.Value(Fable.NewList(tail, _),_)) -> getItems (head::acc) tail
947+
// | Some(head, tail) -> List.rev (head::acc), Some tail
948+
// match getItems [] headAndTail with
949+
// | [], None ->
950+
// libCall com ctx r "List" "empty" [||]
951+
// | [TransformExpr com ctx expr], None ->
952+
// libCall com ctx r "List" "singleton" [|expr|]
953+
// | exprs, None ->
954+
// [|makeArray com ctx exprs|]
955+
// |> libCall com ctx r "List" "ofArray"
956+
// | [TransformExpr com ctx head], Some(TransformExpr com ctx tail) ->
957+
// libCall com ctx r "List" "cons" [|head; tail|]
958+
// | exprs, Some(TransformExpr com ctx tail) ->
959+
// [|makeArray com ctx exprs; tail|]
960+
// |> libCall com ctx r "List" "ofArrayWithTail"
955961
| Fable.NewOption (value, t) ->
956962
match value with
957963
| Some (TransformExpr com ctx e) ->
@@ -1205,10 +1211,12 @@ module Util =
12051211
| Fable.FieldKey field -> get range expr field.Name
12061212

12071213
| Fable.ListHead ->
1208-
get range (com.TransformAsExpr(ctx, fableExpr)) "head"
1214+
// get range (com.TransformAsExpr(ctx, fableExpr)) "head"
1215+
libCall com ctx range "List" "head" [|com.TransformAsExpr(ctx, fableExpr)|]
12091216

12101217
| Fable.ListTail ->
1211-
get range (com.TransformAsExpr(ctx, fableExpr)) "tail"
1218+
// get range (com.TransformAsExpr(ctx, fableExpr)) "tail"
1219+
libCall com ctx range "List" "tail" [|com.TransformAsExpr(ctx, fableExpr)|]
12121220

12131221
| Fable.TupleIndex index ->
12141222
match fableExpr with
@@ -1271,9 +1279,9 @@ module Util =
12711279
let op = if nonEmpty then BinaryUnequal else BinaryEqual
12721280
upcast BinaryExpression(op, com.TransformAsExpr(ctx, expr), NullLiteral(), ?loc=range)
12731281
| Fable.ListTest nonEmpty ->
1274-
let expr = com.TransformAsExpr(ctx, expr)
1275-
let op = if nonEmpty then BinaryUnequal else BinaryEqual
1276-
upcast BinaryExpression(op, get None expr "tail", NullLiteral(), ?loc=range)
1282+
// let expr = get range (com.TransformAsExpr(ctx, expr)) "IsEmpty"
1283+
let expr = libCall com ctx range "List" "isEmpty" [|com.TransformAsExpr(ctx, expr)|]
1284+
if nonEmpty then upcast UnaryExpression(UnaryNot, expr, ?loc=range) else expr
12771285
| Fable.UnionCaseTest tag ->
12781286
let expected = ofInt tag
12791287
let actual = com.TransformAsExpr(ctx, expr) |> getUnionExprTag None

0 commit comments

Comments
 (0)