Skip to content

Commit 621dea3

Browse files
authored
feat(all): add Compiler.is* target detection flags for conditional branching (#4692)
1 parent f5723a2 commit 621dea3

15 files changed

Lines changed: 272 additions & 0 deletions

File tree

src/Fable.Core/Fable.Core.Util.fs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,27 @@ module Compiler =
6969
/// In watch compilations, indicates if the file is being recompiled
7070
/// not because of a direct change, but because a dependency has changed
7171
let triggeredByDependency: bool = false
72+
73+
/// Indicates if code is running on .NET (not compiled by Fable)
74+
let isDotnet: bool = true
75+
76+
/// Indicates if Fable is compiling to JavaScript
77+
let isJavaScript: bool = false
78+
79+
/// Indicates if Fable is compiling to TypeScript
80+
let isTypeScript: bool = false
81+
82+
/// Indicates if Fable is compiling to Python
83+
let isPython: bool = false
84+
85+
/// Indicates if Fable is compiling to Dart
86+
let isDart: bool = false
87+
88+
/// Indicates if Fable is compiling to Rust
89+
let isRust: bool = false
90+
91+
/// Indicates if Fable is compiling to PHP
92+
let isPhp: bool = false
93+
94+
/// Indicates if Fable is compiling to Erlang/BEAM
95+
let isBeam: bool = false

src/Fable.Transforms/Beam/Replacements.fs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5478,6 +5478,30 @@ let tryCall
54785478
match info.CompiledName with
54795479
| "get_InvariantCulture" -> emitExpr r t [] "undefined" |> Some
54805480
| _ -> None
5481+
| "Fable.Core.Compiler" ->
5482+
match info.CompiledName with
5483+
| "version" -> makeStrConst Literals.VERSION |> Some
5484+
| "majorMinorVersion" ->
5485+
try
5486+
let m = System.Text.RegularExpressions.Regex.Match(Literals.VERSION, @"^\d+\.\d+")
5487+
float m.Value |> makeFloatConst |> Some
5488+
with _ ->
5489+
"Cannot parse compiler version"
5490+
|> addErrorAndReturnNull com ctx.InlinePath r
5491+
|> Some
5492+
| "debugMode" -> makeBoolConst com.Options.DebugMode |> Some
5493+
| "typedArrays" -> makeBoolConst com.Options.TypedArrays |> Some
5494+
| "extension" -> makeStrConst com.Options.FileExtension |> Some
5495+
| "triggeredByDependency" -> makeBoolConst com.Options.TriggeredByDependency |> Some
5496+
| "isDotnet" -> makeBoolConst false |> Some
5497+
| "isJavaScript" -> makeBoolConst (com.Options.Language = JavaScript) |> Some
5498+
| "isTypeScript" -> makeBoolConst (com.Options.Language = TypeScript) |> Some
5499+
| "isPython" -> makeBoolConst (com.Options.Language = Python) |> Some
5500+
| "isDart" -> makeBoolConst (com.Options.Language = Dart) |> Some
5501+
| "isRust" -> makeBoolConst (com.Options.Language = Rust) |> Some
5502+
| "isPhp" -> makeBoolConst (com.Options.Language = Php) |> Some
5503+
| "isBeam" -> makeBoolConst (com.Options.Language = Beam) |> Some
5504+
| _ -> None
54815505
| "Fable.Core.BeamInterop" ->
54825506
match info.CompiledName, args with
54835507
| Naming.StartsWith "import" suffix, _ ->

src/Fable.Transforms/Dart/Replacements.fs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,14 @@ let fableCoreLib (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Exp
732732
| "typedArrays" -> makeBoolConst com.Options.TypedArrays |> Some
733733
| "extension" -> makeStrConst com.Options.FileExtension |> Some
734734
| "triggeredByDependency" -> makeBoolConst com.Options.TriggeredByDependency |> Some
735+
| "isDotnet" -> makeBoolConst false |> Some
736+
| "isJavaScript" -> makeBoolConst (com.Options.Language = JavaScript) |> Some
737+
| "isTypeScript" -> makeBoolConst (com.Options.Language = TypeScript) |> Some
738+
| "isPython" -> makeBoolConst (com.Options.Language = Python) |> Some
739+
| "isDart" -> makeBoolConst (com.Options.Language = Dart) |> Some
740+
| "isRust" -> makeBoolConst (com.Options.Language = Rust) |> Some
741+
| "isPhp" -> makeBoolConst (com.Options.Language = Php) |> Some
742+
| "isBeam" -> makeBoolConst (com.Options.Language = Beam) |> Some
735743
| _ -> None
736744
| Naming.StartsWith "Fable.Core.Dart" rest, _ ->
737745
match rest with

src/Fable.Transforms/Python/Replacements.fs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,14 @@ let fableCoreLib (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Exp
954954
| "debugMode" -> makeBoolConst com.Options.DebugMode |> Some
955955
| "typedArrays" -> makeBoolConst com.Options.TypedArrays |> Some
956956
| "extension" -> makeStrConst com.Options.FileExtension |> Some
957+
| "isDotnet" -> makeBoolConst false |> Some
958+
| "isJavaScript" -> makeBoolConst (com.Options.Language = JavaScript) |> Some
959+
| "isTypeScript" -> makeBoolConst (com.Options.Language = TypeScript) |> Some
960+
| "isPython" -> makeBoolConst (com.Options.Language = Python) |> Some
961+
| "isDart" -> makeBoolConst (com.Options.Language = Dart) |> Some
962+
| "isRust" -> makeBoolConst (com.Options.Language = Rust) |> Some
963+
| "isPhp" -> makeBoolConst (com.Options.Language = Php) |> Some
964+
| "isBeam" -> makeBoolConst (com.Options.Language = Beam) |> Some
957965
| _ -> None
958966
| "Fable.Core.Py", ("python" | "expr_python" as meth) ->
959967
let isStatement = meth <> "expr_python"

src/Fable.Transforms/Replacements.fs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,14 @@ let fableCoreLib (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Exp
10891089
| "typedArrays" -> makeBoolConst com.Options.TypedArrays |> Some
10901090
| "extension" -> makeStrConst com.Options.FileExtension |> Some
10911091
| "triggeredByDependency" -> makeBoolConst com.Options.TriggeredByDependency |> Some
1092+
| "isDotnet" -> makeBoolConst false |> Some
1093+
| "isJavaScript" -> makeBoolConst (com.Options.Language = JavaScript) |> Some
1094+
| "isTypeScript" -> makeBoolConst (com.Options.Language = TypeScript) |> Some
1095+
| "isPython" -> makeBoolConst (com.Options.Language = Python) |> Some
1096+
| "isDart" -> makeBoolConst (com.Options.Language = Dart) |> Some
1097+
| "isRust" -> makeBoolConst (com.Options.Language = Rust) |> Some
1098+
| "isPhp" -> makeBoolConst (com.Options.Language = Php) |> Some
1099+
| "isBeam" -> makeBoolConst (com.Options.Language = Beam) |> Some
10921100
| _ -> None
10931101
| "Fable.Core.JS", ("js" | "expr_js" as meth) ->
10941102
let isStatement = meth <> "expr_js"

src/Fable.Transforms/Rust/Replacements.fs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,14 @@ let fableCoreLib (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Exp
741741
| "debugMode" -> makeBoolConst com.Options.DebugMode |> Some
742742
| "typedArrays" -> makeBoolConst com.Options.TypedArrays |> Some
743743
| "extension" -> makeStrConst com.Options.FileExtension |> Some
744+
| "isDotnet" -> makeBoolConst false |> Some
745+
| "isJavaScript" -> makeBoolConst (com.Options.Language = JavaScript) |> Some
746+
| "isTypeScript" -> makeBoolConst (com.Options.Language = TypeScript) |> Some
747+
| "isPython" -> makeBoolConst (com.Options.Language = Python) |> Some
748+
| "isDart" -> makeBoolConst (com.Options.Language = Dart) |> Some
749+
| "isRust" -> makeBoolConst (com.Options.Language = Rust) |> Some
750+
| "isPhp" -> makeBoolConst (com.Options.Language = Php) |> Some
751+
| "isBeam" -> makeBoolConst (com.Options.Language = Beam) |> Some
744752
| _ -> None
745753
| "Fable.Core.RustInterop", "op_BangHat" -> List.tryHead args
746754
| "Fable.Core.RustInterop", _ ->
-206 KB
Binary file not shown.

tests/Beam/MiscTests.fs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Fable.Tests.Misc
33
#nowarn "40"
44

55
open System
6+
open Fable.Core
67
open FSharp.UMX
78
open Util.Testing
89
open Util2.Extensions
@@ -1178,3 +1179,38 @@ let ``test Optimized assignment blocks inside try ... with work`` () =
11781179
try A.C.Helper.Add5(let mutable x = 2 in let mutable y = 3 in x + y)
11791180
with _ -> 1
11801181
equal 10 res
1182+
1183+
[<Fact>]
1184+
let ``test Compiler target flags have correct value per target`` () =
1185+
equal false Compiler.isJavaScript
1186+
equal false Compiler.isTypeScript
1187+
equal false Compiler.isPython
1188+
equal false Compiler.isDart
1189+
equal false Compiler.isRust
1190+
#if FABLE_COMPILER_BEAM
1191+
equal true Compiler.isBeam
1192+
#else
1193+
equal false Compiler.isBeam
1194+
#endif
1195+
#if FABLE_COMPILER
1196+
equal false Compiler.isDotnet
1197+
#else
1198+
equal true Compiler.isDotnet
1199+
#endif
1200+
equal false (Compiler.isJavaScript || Compiler.isTypeScript)
1201+
1202+
[<Fact>]
1203+
let ``test Compiler target flags eliminate dead branches`` () =
1204+
let target =
1205+
if Compiler.isJavaScript then "javascript"
1206+
elif Compiler.isTypeScript then "typescript"
1207+
elif Compiler.isPython then "python"
1208+
elif Compiler.isDart then "dart"
1209+
elif Compiler.isRust then "rust"
1210+
elif Compiler.isBeam then "beam"
1211+
else "dotnet"
1212+
#if FABLE_COMPILER_BEAM
1213+
equal "beam" target
1214+
#else
1215+
equal "dotnet" target
1216+
#endif

tests/Dart/src/MiscTests.fs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module Fable.Tests.Dart.Misc
22

33
open System
4+
open Fable.Core
45
open Util
56

67
let increase (x: int ref) =
@@ -129,3 +130,34 @@ let tests() =
129130
throwsError "This is invalid (Parameter 'arg')" (fun () ->
130131
invalidArg "arg" "This is invalid"
131132
)
133+
134+
testCase "Compiler target flags have correct value per target" <| fun () ->
135+
equal false Compiler.isJavaScript
136+
equal false Compiler.isTypeScript
137+
equal false Compiler.isPython
138+
#if FABLE_COMPILER_DART
139+
equal true Compiler.isDart
140+
#else
141+
equal false Compiler.isDart
142+
#endif
143+
equal false Compiler.isRust
144+
#if FABLE_COMPILER
145+
equal false Compiler.isDotnet
146+
#else
147+
equal true Compiler.isDotnet
148+
#endif
149+
equal false (Compiler.isJavaScript || Compiler.isTypeScript)
150+
151+
testCase "Compiler target flags eliminate dead branches" <| fun () ->
152+
let target =
153+
if Compiler.isJavaScript then "javascript"
154+
elif Compiler.isTypeScript then "typescript"
155+
elif Compiler.isPython then "python"
156+
elif Compiler.isDart then "dart"
157+
elif Compiler.isRust then "rust"
158+
else "dotnet"
159+
#if FABLE_COMPILER_DART
160+
equal "dart" target
161+
#else
162+
equal "dotnet" target
163+
#endif
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module ConditionalTargetBranching
2+
3+
open Fable.Core
4+
5+
let target =
6+
if Compiler.isJavaScript then "javascript"
7+
elif Compiler.isPython then "python"
8+
else "dotnet"
9+
10+
let targetFromFamily =
11+
if Compiler.isJavaScript || Compiler.isTypeScript then "js-family"
12+
else "other"

0 commit comments

Comments
 (0)