Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] add replacements for System.Numerics Vector3 / Quaternion #1638

Open
wants to merge 25 commits into
base: nagareyama
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
e1a48c6
add replacements for System.Numerics Vector3 / Quaternion
0x53A Nov 14, 2018
e074968
Merge branch 'master' into replacement-system-numerics
0x53A Nov 14, 2018
33979c4
v3 / q use public fields, not properties
0x53A Nov 14, 2018
04f4fc6
add transforms for ILFieldGet / ILFieldSet
0x53A Nov 14, 2018
38fc343
ILFieldGet / Set
0x53A Nov 14, 2018
e5cf894
ILFieldGet
0x53A Nov 15, 2018
64b2b57
add (hopefully) correct transforms for ILFieldGet / ILFieldSet
0x53A Nov 16, 2018
7b472ed
oops
0x53A Nov 16, 2018
d402839
add tests for field read / write
0x53A Nov 16, 2018
d8387b8
merge from master
0x53A Dec 21, 2018
6b0db68
minor comment
0x53A Dec 21, 2018
31fbbbc
adapt to rename fable-core => fable-library
0x53A Dec 21, 2018
8271537
implement some more members of Vector3
0x53A Dec 21, 2018
7dc6c31
wip comments
0x53A Dec 21, 2018
c3733e5
merge from upstream/master
0x53A Apr 30, 2019
c6a5b3a
fable-library was moved
0x53A Apr 30, 2019
1e3a33d
Vector3.ToString
0x53A May 7, 2019
ee4e1d2
Merge remote-tracking branch 'upstream/master' into replacement-syste…
0x53A Aug 5, 2019
2cef223
Fable.Tests: add PackageReference to System.Numerics.Vectors
0x53A Aug 5, 2019
444a9c6
add support for nested "testList", fixes https://github.com/fable-com…
0x53A Aug 5, 2019
250c63e
add equality tests
0x53A Aug 5, 2019
0d4cfc1
override equality and comparison for Vector3
0x53A Aug 5, 2019
1b928ca
split file into Vector3.fs and Quaternion.fs
0x53A Aug 5, 2019
5c27ea2
add missing cases to transformTypeInfo
0x53A Aug 5, 2019
50aa0a9
Revert "split file into Vector3.fs and Quaternion.fs"
0x53A Aug 5, 2019
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
22 changes: 20 additions & 2 deletions src/Fable.Transforms/FSharp2Fable.fs
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@ let private transformExpr (com: IFableCompiler) (ctx: Context) fsExpr =
| _ -> return failwith "makeFunctionArgs returns args with different length"

// Getters and Setters

| BasicPatterns.AnonRecordGet(callee, calleeType, fieldIndex) ->
let! callee = transformExpr com ctx callee
let fieldName = calleeType.AnonRecordTypeDetails.SortedFieldNames.[fieldIndex]
Expand Down Expand Up @@ -746,6 +747,7 @@ let private transformExpr (com: IFableCompiler) (ctx: Context) fsExpr =
let typ = makeType com ctx.GenericArgs fsExpr.Type
return Fable.DecisionTreeSuccess(targetIndex, boundValues, typ)

// read a static field (Example: Decimal.Zero)
| BasicPatterns.ILFieldGet(None, ownerTyp, fieldName) ->
let ownerTyp = makeType com ctx.GenericArgs ownerTyp
let typ = makeType com ctx.GenericArgs fsExpr.Type
Expand All @@ -755,6 +757,23 @@ let private transformExpr (com: IFableCompiler) (ctx: Context) fsExpr =
return sprintf "Cannot compile ILFieldGet(%A, %s)" ownerTyp fieldName
|> addErrorAndReturnNull com ctx.InlinePath (makeRangeFrom fsExpr)

// read a member field (Example: Vector3.X)
| BasicPatterns.ILFieldGet(Some callee, calleeType, fieldName)
when (Replacements.isILMemberFieldSupported (makeType com ctx.GenericArgs calleeType) fieldName) ->
let! callee = transformExpr com ctx callee
let typ = makeType com ctx.GenericArgs fsExpr.Type
let kind = Fable.FieldGet(fieldName, true, typ)
let typ = makeType com ctx.GenericArgs fsExpr.Type
return Fable.Get(callee, kind, typ, makeRangeFrom fsExpr)

// write a member field (example: Vector3.X)
| BasicPatterns.ILFieldSet(Some callee, calleeType, fieldName, value)
when (Replacements.isILMemberFieldSupported (makeType com ctx.GenericArgs calleeType) fieldName) ->
let! callee = transformExpr com ctx callee
let! value = transformExpr com ctx value
let typ = makeType com ctx.GenericArgs fsExpr.Type
return Fable.Set(callee, Fable.FieldSet(fieldName, typ), value, makeRangeFrom fsExpr)

| BasicPatterns.Quote _ ->
return "Quotes are not currently supported by Fable"
|> addErrorAndReturnNull com ctx.InlinePath (makeRangeFrom fsExpr)
Expand All @@ -763,8 +782,7 @@ let private transformExpr (com: IFableCompiler) (ctx: Context) fsExpr =
| BasicPatterns.AddressOf(expr) ->
let! expr = transformExpr com ctx expr
return expr

// | BasicPatterns.ILFieldSet _

// | BasicPatterns.AddressSet _
// | BasicPatterns.ILAsm _
| expr ->
Expand Down
4 changes: 4 additions & 0 deletions src/Fable.Transforms/Fable2Babel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,8 @@ module Util =
| Replacements.BuiltinEntity kind ->
match kind with
| Replacements.BclGuid
| Replacements.BclVector3
| Replacements.BclQuaternion
| Replacements.BclTimeSpan
| Replacements.BclDateTime
| Replacements.BclDateTimeOffset
Expand Down Expand Up @@ -870,6 +872,8 @@ module Util =
| Replacements.BclUInt64 -> jsInstanceof (coreValue com ctx "Long" "default") expr
| Replacements.BclDecimal -> jsInstanceof (coreValue com ctx "Decimal" "default") expr
| Replacements.BclBigInt -> coreLibCall com ctx None "BigInt" "isBigInt" [|com.TransformAsExpr(ctx, expr)|]
| Replacements.BclQuaternion -> jsInstanceof (coreValue com ctx "Quaternion" "default") expr
| Replacements.BclVector3 -> jsInstanceof (coreValue com ctx "Vector3" "default") expr
| Replacements.BclHashSet _
| Replacements.BclDictionary _
| Replacements.FSharpSet _
Expand Down
17 changes: 17 additions & 0 deletions src/Fable.Transforms/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ type BuiltinType =
| BclUInt64
| BclDecimal
| BclBigInt
| BclQuaternion
| BclVector3
| BclHashSet of Type
| BclDictionary of key:Type * value:Type
| FSharpSet of Type
Expand All @@ -151,6 +153,8 @@ let (|BuiltinEntity|_|) (ent: FSharpEntity, genArgs) =
| Some Types.decimal, _
| Some "Microsoft.FSharp.Core.decimal`1", _ -> Some BclDecimal
| Some Types.bigint, _ -> Some BclBigInt
| Some Types.quaternion, _ -> Some BclQuaternion
| Some Types.vector3, _ -> Some BclVector3
| Some Types.fsharpSet, [t] -> Some(FSharpSet(t))
| Some Types.fsharpMap, [k;v] -> Some(FSharpMap(k,v))
| Some Types.hashset, [t] -> Some(BclHashSet(t))
Expand Down Expand Up @@ -254,6 +258,8 @@ let coreModFor = function
| BclDecimal -> "Decimal"
| BclBigInt -> "BigInt"
| BclTimeSpan -> "TimeSpan"
| BclQuaternion _ -> "Quaternion"
| BclVector3 _ -> "Vector3"
| FSharpSet _ -> "Set"
| FSharpMap _ -> "Map"
| FSharpResult _ -> "Option"
Expand Down Expand Up @@ -2718,6 +2724,7 @@ let partialApplyAtRuntime t arity (fn: Expr) (args: Expr list) =
let args = NewArray(ArrayValues args, Any) |> makeValue None
Helper.CoreCall("Util", "partialApply", t, [makeIntConst arity; fn; args])

/// try to map a static field (e.g. Decimal.Zero)
let tryField returnTyp ownerTyp fieldName =
match ownerTyp, fieldName with
| Builtin BclDecimal, _ ->
Expand All @@ -2736,6 +2743,14 @@ let tryField returnTyp ownerTyp fieldName =
| _ -> None
| _ -> None

// this is queried when an IL member field is read / written.
// at the moment member fields are only supported for Vector3 / Quaternion, all other supported BCL types expose properties instead of fields.
let isILMemberFieldSupported ownerTyp _fieldName =
match ownerTyp with
| Builtin BclQuaternion
| Builtin BclVector3 -> true
| _ -> false

let private replacedModules =
dict [
"System.Math", operators
Expand Down Expand Up @@ -2844,6 +2859,8 @@ let private replacedModules =
"Microsoft.FSharp.Control.ObservableModule", observable
Types.type_, types
"System.Reflection.TypeInfo", types
"System.Numerics.Vector3", bclType
"System.Numerics.Quaternion", bclType
]

let tryCall (com: ICompiler) (ctx: Context) r t (info: CallInfo) (thisArg: Expr option) (args: Expr list) =
Expand Down
2 changes: 2 additions & 0 deletions src/Fable.Transforms/Transforms.Util.fs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ module Types =
let [<Literal>] float64 = "System.Double"
let [<Literal>] decimal = "System.Decimal"
let [<Literal>] bigint = "System.Numerics.BigInteger"
let [<Literal>] quaternion = "System.Numerics.Quaternion"
let [<Literal>] vector3 = "System.Numerics.Vector3"
let [<Literal>] regex = "System.Text.RegularExpressions.Regex"
let [<Literal>] unit = "Microsoft.FSharp.Core.Unit"
let [<Literal>] option = "Microsoft.FSharp.Core.FSharpOption`1"
Expand Down
1 change: 1 addition & 0 deletions src/fable-library/Fable.Library.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="System.Numerics.fs" />
<Compile Include="Global.fs" />
<!-- <Compile Include="BigInt/n.fsi" /> -->
<Compile Include="BigInt/n.fs" />
Expand Down
Loading