Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Fable.Core/Fable.Core.Util.fs
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,27 @@ module Compiler =
/// In watch compilations, indicates if the file is being recompiled
/// not because of a direct change, but because a dependency has changed
let triggeredByDependency: bool = false

/// Indicates if code is running on .NET (not compiled by Fable)
let isDotnet: bool = true

/// Indicates if Fable is compiling to JavaScript
let isJavaScript: bool = false

/// Indicates if Fable is compiling to TypeScript
let isTypeScript: bool = false

/// Indicates if Fable is compiling to Python
let isPython: bool = false

/// Indicates if Fable is compiling to Dart
let isDart: bool = false

/// Indicates if Fable is compiling to Rust
let isRust: bool = false

/// Indicates if Fable is compiling to PHP
let isPhp: bool = false

/// Indicates if Fable is compiling to Erlang/BEAM
let isBeam: bool = false
24 changes: 24 additions & 0 deletions src/Fable.Transforms/Beam/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5478,6 +5478,30 @@ let tryCall
match info.CompiledName with
| "get_InvariantCulture" -> emitExpr r t [] "undefined" |> Some
| _ -> None
| "Fable.Core.Compiler" ->
match info.CompiledName with
| "version" -> makeStrConst Literals.VERSION |> Some
| "majorMinorVersion" ->
try
let m = System.Text.RegularExpressions.Regex.Match(Literals.VERSION, @"^\d+\.\d+")
float m.Value |> makeFloatConst |> Some
with _ ->
"Cannot parse compiler version"
|> addErrorAndReturnNull com ctx.InlinePath r
|> Some
| "debugMode" -> makeBoolConst com.Options.DebugMode |> Some
| "typedArrays" -> makeBoolConst com.Options.TypedArrays |> Some
| "extension" -> makeStrConst com.Options.FileExtension |> Some
| "triggeredByDependency" -> makeBoolConst com.Options.TriggeredByDependency |> Some
| "isDotnet" -> makeBoolConst false |> Some
| "isJavaScript" -> makeBoolConst (com.Options.Language = JavaScript) |> Some
| "isTypeScript" -> makeBoolConst (com.Options.Language = TypeScript) |> Some
| "isPython" -> makeBoolConst (com.Options.Language = Python) |> Some
| "isDart" -> makeBoolConst (com.Options.Language = Dart) |> Some
| "isRust" -> makeBoolConst (com.Options.Language = Rust) |> Some
| "isPhp" -> makeBoolConst (com.Options.Language = Php) |> Some
| "isBeam" -> makeBoolConst (com.Options.Language = Beam) |> Some
| _ -> None
| "Fable.Core.BeamInterop" ->
match info.CompiledName, args with
| Naming.StartsWith "import" suffix, _ ->
Expand Down
8 changes: 8 additions & 0 deletions src/Fable.Transforms/Dart/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,14 @@ let fableCoreLib (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Exp
| "typedArrays" -> makeBoolConst com.Options.TypedArrays |> Some
| "extension" -> makeStrConst com.Options.FileExtension |> Some
| "triggeredByDependency" -> makeBoolConst com.Options.TriggeredByDependency |> Some
| "isDotnet" -> makeBoolConst false |> Some
| "isJavaScript" -> makeBoolConst (com.Options.Language = JavaScript) |> Some
| "isTypeScript" -> makeBoolConst (com.Options.Language = TypeScript) |> Some
| "isPython" -> makeBoolConst (com.Options.Language = Python) |> Some
| "isDart" -> makeBoolConst (com.Options.Language = Dart) |> Some
| "isRust" -> makeBoolConst (com.Options.Language = Rust) |> Some
| "isPhp" -> makeBoolConst (com.Options.Language = Php) |> Some
| "isBeam" -> makeBoolConst (com.Options.Language = Beam) |> Some
| _ -> None
| Naming.StartsWith "Fable.Core.Dart" rest, _ ->
match rest with
Expand Down
8 changes: 8 additions & 0 deletions src/Fable.Transforms/Python/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,14 @@ let fableCoreLib (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Exp
| "debugMode" -> makeBoolConst com.Options.DebugMode |> Some
| "typedArrays" -> makeBoolConst com.Options.TypedArrays |> Some
| "extension" -> makeStrConst com.Options.FileExtension |> Some
| "isDotnet" -> makeBoolConst false |> Some
| "isJavaScript" -> makeBoolConst (com.Options.Language = JavaScript) |> Some
| "isTypeScript" -> makeBoolConst (com.Options.Language = TypeScript) |> Some
| "isPython" -> makeBoolConst (com.Options.Language = Python) |> Some
| "isDart" -> makeBoolConst (com.Options.Language = Dart) |> Some
| "isRust" -> makeBoolConst (com.Options.Language = Rust) |> Some
| "isPhp" -> makeBoolConst (com.Options.Language = Php) |> Some
| "isBeam" -> makeBoolConst (com.Options.Language = Beam) |> Some
| _ -> None
| "Fable.Core.Py", ("python" | "expr_python" as meth) ->
let isStatement = meth <> "expr_python"
Expand Down
8 changes: 8 additions & 0 deletions src/Fable.Transforms/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,14 @@ let fableCoreLib (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Exp
| "typedArrays" -> makeBoolConst com.Options.TypedArrays |> Some
| "extension" -> makeStrConst com.Options.FileExtension |> Some
| "triggeredByDependency" -> makeBoolConst com.Options.TriggeredByDependency |> Some
| "isDotnet" -> makeBoolConst false |> Some
| "isJavaScript" -> makeBoolConst (com.Options.Language = JavaScript) |> Some
| "isTypeScript" -> makeBoolConst (com.Options.Language = TypeScript) |> Some
| "isPython" -> makeBoolConst (com.Options.Language = Python) |> Some
| "isDart" -> makeBoolConst (com.Options.Language = Dart) |> Some
| "isRust" -> makeBoolConst (com.Options.Language = Rust) |> Some
| "isPhp" -> makeBoolConst (com.Options.Language = Php) |> Some
| "isBeam" -> makeBoolConst (com.Options.Language = Beam) |> Some
| _ -> None
| "Fable.Core.JS", ("js" | "expr_js" as meth) ->
let isStatement = meth <> "expr_js"
Expand Down
8 changes: 8 additions & 0 deletions src/Fable.Transforms/Rust/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,14 @@ let fableCoreLib (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Exp
| "debugMode" -> makeBoolConst com.Options.DebugMode |> Some
| "typedArrays" -> makeBoolConst com.Options.TypedArrays |> Some
| "extension" -> makeStrConst com.Options.FileExtension |> Some
| "isDotnet" -> makeBoolConst false |> Some
| "isJavaScript" -> makeBoolConst (com.Options.Language = JavaScript) |> Some
| "isTypeScript" -> makeBoolConst (com.Options.Language = TypeScript) |> Some
| "isPython" -> makeBoolConst (com.Options.Language = Python) |> Some
| "isDart" -> makeBoolConst (com.Options.Language = Dart) |> Some
| "isRust" -> makeBoolConst (com.Options.Language = Rust) |> Some
| "isPhp" -> makeBoolConst (com.Options.Language = Php) |> Some
| "isBeam" -> makeBoolConst (com.Options.Language = Beam) |> Some
| _ -> None
| "Fable.Core.RustInterop", "op_BangHat" -> List.tryHead args
| "Fable.Core.RustInterop", _ ->
Expand Down
Binary file modified src/fable-metadata/lib/Fable.Core.dll
Binary file not shown.
36 changes: 36 additions & 0 deletions tests/Beam/MiscTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Fable.Tests.Misc
#nowarn "40"

open System
open Fable.Core
open FSharp.UMX
open Util.Testing
open Util2.Extensions
Expand Down Expand Up @@ -1178,3 +1179,38 @@ let ``test Optimized assignment blocks inside try ... with work`` () =
try A.C.Helper.Add5(let mutable x = 2 in let mutable y = 3 in x + y)
with _ -> 1
equal 10 res

[<Fact>]
let ``test Compiler target flags have correct value per target`` () =
equal false Compiler.isJavaScript
equal false Compiler.isTypeScript
equal false Compiler.isPython
equal false Compiler.isDart
equal false Compiler.isRust
#if FABLE_COMPILER_BEAM
equal true Compiler.isBeam
#else
equal false Compiler.isBeam
#endif
#if FABLE_COMPILER
equal false Compiler.isDotnet
#else
equal true Compiler.isDotnet
#endif
equal false (Compiler.isJavaScript || Compiler.isTypeScript)

[<Fact>]
let ``test Compiler target flags eliminate dead branches`` () =
let target =
if Compiler.isJavaScript then "javascript"
elif Compiler.isTypeScript then "typescript"
elif Compiler.isPython then "python"
elif Compiler.isDart then "dart"
elif Compiler.isRust then "rust"
elif Compiler.isBeam then "beam"
else "dotnet"
#if FABLE_COMPILER_BEAM
equal "beam" target
#else
equal "dotnet" target
#endif
32 changes: 32 additions & 0 deletions tests/Dart/src/MiscTests.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Fable.Tests.Dart.Misc

open System
open Fable.Core
open Util

let increase (x: int ref) =
Expand Down Expand Up @@ -129,3 +130,34 @@ let tests() =
throwsError "This is invalid (Parameter 'arg')" (fun () ->
invalidArg "arg" "This is invalid"
)

testCase "Compiler target flags have correct value per target" <| fun () ->
equal false Compiler.isJavaScript
equal false Compiler.isTypeScript
equal false Compiler.isPython
#if FABLE_COMPILER_DART
equal true Compiler.isDart
#else
equal false Compiler.isDart
#endif
equal false Compiler.isRust
#if FABLE_COMPILER
equal false Compiler.isDotnet
#else
equal true Compiler.isDotnet
#endif
equal false (Compiler.isJavaScript || Compiler.isTypeScript)

testCase "Compiler target flags eliminate dead branches" <| fun () ->
let target =
if Compiler.isJavaScript then "javascript"
elif Compiler.isTypeScript then "typescript"
elif Compiler.isPython then "python"
elif Compiler.isDart then "dart"
elif Compiler.isRust then "rust"
else "dotnet"
#if FABLE_COMPILER_DART
equal "dart" target
#else
equal "dotnet" target
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module ConditionalTargetBranching

open Fable.Core

let target =
if Compiler.isJavaScript then "javascript"
elif Compiler.isPython then "python"
else "dotnet"

let targetFromFamily =
if Compiler.isJavaScript || Compiler.isTypeScript then "js-family"
else "other"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const target = "javascript";

export const targetFromFamily = "js-family";
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<RollForward>Major</RollForward>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
<Compile Include="ConditionalTargetBranching.fs" />
</ItemGroup>

<ItemGroup>
<!-- Reference the local build since Compiler.is* are not yet in a NuGet release -->
<Reference Include="Fable.Core">
<HintPath>../../../../../src/Fable.Core/bin/Release/netstandard2.0/Fable.Core.dll</HintPath>
</Reference>
</ItemGroup>

</Project>
41 changes: 41 additions & 0 deletions tests/Js/Main/MiscTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@

let mutable mutableValue = 0

let rec recursive1 = delay (fun () -> recursive2())

Check warning on line 382 in tests/Js/Main/MiscTests.fs

View workflow job for this annotation

GitHub Actions / build-javascript (ubuntu-latest)

This and other recursive references to the object(s) being defined will be checked for initialization-soundness at runtime through the use of a delayed reference. This is because you are defining one or more recursive objects, rather than recursive functions. This warning may be suppressed by using '#nowarn "40"' or '--nowarn:40'.

Check warning on line 382 in tests/Js/Main/MiscTests.fs

View workflow job for this annotation

GitHub Actions / build-javascript (windows-latest)

This and other recursive references to the object(s) being defined will be checked for initialization-soundness at runtime through the use of a delayed reference. This is because you are defining one or more recursive objects, rather than recursive functions. This warning may be suppressed by using '#nowarn "40"' or '--nowarn:40'.
and recursive2 =
mutableValue <- 5
fun () -> mutableValue <- mutableValue * 2
Expand Down Expand Up @@ -609,6 +609,47 @@
".js"
#endif
Compiler.extension.EndsWith(ext) |> equal true

testCase "isJavaScript || isTypeScript is true for JS family targets" <| fun _ ->
equal true (Compiler.isJavaScript || Compiler.isTypeScript)
equal false (Compiler.isPython || Compiler.isDart || Compiler.isRust || Compiler.isBeam)

testCase "Compiler target flags have correct value per target" <| fun _ ->
#if FABLE_COMPILER_JAVASCRIPT
equal true Compiler.isJavaScript
equal false Compiler.isTypeScript
#endif
#if FABLE_COMPILER_TYPESCRIPT
equal false Compiler.isJavaScript
equal true Compiler.isTypeScript
#endif
equal false Compiler.isPython
equal false Compiler.isDart
equal false Compiler.isRust
#if FABLE_COMPILER
equal false Compiler.isDotnet
#else
equal true Compiler.isDotnet
#endif
equal true (Compiler.isJavaScript || Compiler.isTypeScript)

testCase "Compiler target flags eliminate dead branches" <| fun _ ->
let target =
if Compiler.isJavaScript then "javascript"
elif Compiler.isTypeScript then "typescript"
elif Compiler.isPython then "python"
else "dotnet"
let family =
if Compiler.isJavaScript || Compiler.isTypeScript then "js-family"
else "other"
#if FABLE_COMPILER_JAVASCRIPT
equal "javascript" target
equal "js-family" family
#endif
#if FABLE_COMPILER_TYPESCRIPT
equal "typescript" target
equal "js-family" family
#endif
#endif

testCase "Values of autogenerated functions are not replaced by optimizations" <| fun () -> // See #1583
Expand Down Expand Up @@ -1170,7 +1211,7 @@
| 2 -> x <- "2"
| 3 | 4 -> x <- "3" // Multiple cases are allowed
// | 5 | 6 as j -> x <- string j // This prevents the optimization
| 4 -> x <- "4" // Unreachable cases are removed

Check warning on line 1214 in tests/Js/Main/MiscTests.fs

View workflow job for this annotation

GitHub Actions / build-javascript (ubuntu-latest)

This rule will never be matched

Check warning on line 1214 in tests/Js/Main/MiscTests.fs

View workflow job for this annotation

GitHub Actions / build-javascript (windows-latest)

This rule will never be matched
| _ -> x <- "?"
equal "3" x

Expand Down
23 changes: 23 additions & 0 deletions tests/Python/TestMisc.fs
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,29 @@
let ``test Can access extension for generated files`` () =
Compiler.extension.EndsWith(".py") |> equal true

[<Fact>]
let ``test Compiler target flags have correct value per target`` () =
equal false Compiler.isJavaScript
equal false Compiler.isTypeScript
equal true Compiler.isPython
equal false Compiler.isDart
equal false Compiler.isRust
#if FABLE_COMPILER
equal false Compiler.isDotnet
#else
equal true Compiler.isDotnet
#endif
equal false (Compiler.isJavaScript || Compiler.isTypeScript)

[<Fact>]
let ``test Compiler target flags eliminate dead branches`` () =
let target =
if Compiler.isJavaScript then "javascript"
elif Compiler.isTypeScript then "typescript"
elif Compiler.isPython then "python"
else "dotnet"
equal "python" target

#endif

[<Fact>]
Expand Down Expand Up @@ -1156,7 +1179,7 @@
| 2 -> x <- "2"
| 3 | 4 -> x <- "3" // Multiple cases are allowed
// | 5 | 6 as j -> x <- string j // This prevents the optimization
| 4 -> x <- "4" // Unreachable cases are removed

Check warning on line 1182 in tests/Python/TestMisc.fs

View workflow job for this annotation

GitHub Actions / build-python (ubuntu-latest, 3.14)

This rule will never be matched

Check warning on line 1182 in tests/Python/TestMisc.fs

View workflow job for this annotation

GitHub Actions / build-python (ubuntu-latest, 3.13)

This rule will never be matched

Check warning on line 1182 in tests/Python/TestMisc.fs

View workflow job for this annotation

GitHub Actions / build-python (ubuntu-latest, 3.12)

This rule will never be matched

Check warning on line 1182 in tests/Python/TestMisc.fs

View workflow job for this annotation

GitHub Actions / build-python (windows-latest, 3.13)

This rule will never be matched

Check warning on line 1182 in tests/Python/TestMisc.fs

View workflow job for this annotation

GitHub Actions / build-python (windows-latest, 3.12)

This rule will never be matched

Check warning on line 1182 in tests/Python/TestMisc.fs

View workflow job for this annotation

GitHub Actions / build-python (windows-latest, 3.14)

This rule will never be matched
| _ -> x <- "?"
equal "3" x

Expand Down
25 changes: 25 additions & 0 deletions tests/Rust/tests/src/MiscTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,31 @@
// let ``Can access extension for generated files`` () =
// Compiler.extension.EndsWith(".js") |> equal true

[<Fact>]
let ``Compiler target flags have correct value per target`` () =
equal false Compiler.isJavaScript
equal false Compiler.isTypeScript
equal false Compiler.isPython
equal false Compiler.isDart
equal true Compiler.isRust
#if FABLE_COMPILER
equal false Compiler.isDotnet
#else
equal true Compiler.isDotnet
#endif
equal false (Compiler.isJavaScript || Compiler.isTypeScript)

[<Fact>]
let ``Compiler target flags eliminate dead branches`` () =
let target =
if Compiler.isJavaScript then "javascript"
elif Compiler.isTypeScript then "typescript"
elif Compiler.isPython then "python"
elif Compiler.isDart then "dart"
elif Compiler.isRust then "rust"
else "dotnet"
equal "rust" target

#endif

[<Fact>]
Expand Down Expand Up @@ -1235,7 +1260,7 @@
| 2 -> x <- "2"
| 3 | 4 -> x <- "3" // Multiple cases are allowed
// | 5 | 6 as j -> x <- string j // This prevents the optimization
| 4 -> x <- "4" // Unreachable cases are removed

Check warning on line 1263 in tests/Rust/tests/src/MiscTests.fs

View workflow job for this annotation

GitHub Actions / analyzers (src/Fable.AST/Fable.AST.fsproj)

This rule will never be matched

Check warning on line 1263 in tests/Rust/tests/src/MiscTests.fs

View workflow job for this annotation

GitHub Actions / build-rust (no_std)

This rule will never be matched

Check warning on line 1263 in tests/Rust/tests/src/MiscTests.fs

View workflow job for this annotation

GitHub Actions / build-rust (threaded)

This rule will never be matched

Check warning on line 1263 in tests/Rust/tests/src/MiscTests.fs

View workflow job for this annotation

GitHub Actions / build-rust (default)

This rule will never be matched
| _ -> x <- "?"
equal "3" x

Expand Down
Loading