@@ -454,6 +454,7 @@ module private AstPass =
454454 let fableCoreLib com ( i : Fable.ApplyInfo ) =
455455 let destruct = function
456456 | Fable.Value( Fable.TupleConst exprs) -> exprs
457+ | expr when expr.Type = Fable.Unit -> []
457458 | expr -> [ expr]
458459 match i.methodName with
459460 | Naming.StartsWith " import" _ ->
@@ -681,7 +682,7 @@ module private AstPass =
681682 | " toSingle" | " toDouble" | " toDecimal" -> toFloat com info args |> Some
682683 | " toChar" -> toChar com info args.Head |> Some
683684 | " toString" -> toString com info args.Head |> Some
684- | " toEnum" -> args.Head |> Some
685+ | " toEnum" -> args.Head |> Some
685686 | " createDictionary" ->
686687 GlobalCall( " Map" , None, true , args) |> makeCall r typ |> Some
687688 | " createSet" ->
@@ -1022,17 +1023,13 @@ module private AstPass =
10221023
10231024 let options ( com : ICompiler ) ( i : Fable.ApplyInfo ) =
10241025 // Prevent functions being run twice, see #198
1025- let wrapInLet f expr =
1026- let ident = com.GetUniqueVar() |> makeIdent
1027- [
1028- Fable.VarDeclaration( ident, expr, false )
1029- f( Fable.Value( Fable.IdentValue ident))
1030- ]
1031- |> fun exprs -> Fable.Sequential( exprs, i.range)
1032- let toArray r optExpr =
1033- // "$0 != null ? [$0]: []"
1034- let makeArray exprs = Fable.ArrayConst( Fable.ArrayValues exprs, genArg i.returnType) |> Fable.Value
1035- Fable.IfThenElse( makeEqOp r [ optExpr; Fable.Value Fable.Null] BinaryUnequal, makeArray [ optExpr], makeArray [], r)
1026+ let runIfSome r expr defValue f =
1027+ CoreLibCall( " Util" , Some " defaultArg" , false , [ expr; defValue; f])
1028+ |> makeCall r Fable.Any
1029+ let toArray r arg =
1030+ let ident = makeIdent " x"
1031+ makeLambdaExpr [ ident] ( makeArray Fable.Any [ Fable.IdentValue ident |> Fable.Value])
1032+ |> runIfSome r arg ( makeArray Fable.Any [])
10361033 let getCallee () = match i.callee with Some c -> c | None -> i.args.Head
10371034 match i.methodName with
10381035 | " none" -> Fable.Null |> Fable.Value |> Some
@@ -1049,35 +1046,25 @@ module private AstPass =
10491046 | " map" | " bind" ->
10501047 // emit i "$1 != null ? $0($1) : $1" i.args |> Some
10511048 let f , arg = i.args.Head, i.args.Tail.Head
1052- arg |> wrapInLet ( fun e ->
1053- Fable.IfThenElse(
1054- makeEqOp i.range [ e; Fable.Value Fable.Null] BinaryUnequal,
1055- Fable.Apply( f, [ e], Fable.ApplyMeth, Fable.Any, i.range),
1056- e, i.range))
1057- |> Some
1049+ runIfSome i.range arg ( Fable.Value Fable.Null) f |> Some
10581050 | " filter" ->
1059- // emit i "$1 != null && $0($1) ? $1 : null" i.args |> Some
1060- let f , arg = i.args.Head, i.args.Tail.Head
1061- arg |> wrapInLet ( fun e ->
1062- let cond =
1063- [ makeEqOp i.range [ e; Fable.Value Fable.Null] BinaryUnequal
1064- Fable.Apply( f, [ e], Fable.ApplyMeth, Fable.Any, i.range) ]
1065- |> makeLogOp i.range <| LogicalAnd
1066- Fable.IfThenElse( cond, e, Fable.Value Fable.Null, i.range))
1051+ let filter , arg = i.args.Head, i.args.Tail.Head
1052+ " x => $0(x) ? x : null"
1053+ |> makeEmit None Fable.Any [ filter]
1054+ |> runIfSome i.range arg ( Fable.Value Fable.Null)
10671055 |> Some
1068- | " toArray" -> toArray i.range i.args.Head |> Some
1056+ | " toArray" ->
1057+ toArray i.range i.args.Head |> Some
10691058 | " foldBack" ->
1070- let opt = wrapInLet ( fun e -> toArray i.range e ) i.args.Tail.Head
1059+ let opt = toArray None i.args.Tail.Head
10711060 let args = i.args.Head:: opt:: i.args.Tail.Tail
1072- CoreLibCall( " Seq" , Some " foldBack" , false , deleg com i args)
1073- |> makeCall i.range i.returnType |> Some
1061+ ccall com i " Seq" " foldBack" ( deleg com i args) |> Some
10741062 | meth ->
10751063 let args =
10761064 let args = List.rev i.args
1077- wrapInLet ( fun e -> toArray i.range e) args.Head
1078- |> fun argsHead -> List.rev ( argsHead:: args.Tail)
1079- CoreLibCall( " Seq" , Some meth, false , deleg com i args)
1080- |> makeCall i.range i.returnType |> Some
1065+ let opt = toArray None args.Head
1066+ List.rev ( opt:: args.Tail)
1067+ ccall com i " Seq" meth ( deleg com i args) |> Some
10811068
10821069 let timeSpans com ( i : Fable.ApplyInfo ) =
10831070 // let callee = match i.callee with Some c -> c | None -> i.args.Head
0 commit comments