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
13 changes: 12 additions & 1 deletion src/Fable.Transforms/Python/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3967,7 +3967,8 @@ let fsharpType com methName (r: SourceLocation option) t (i: CallInfo) (args: Ex
|> Some
// Prevent name clash with FSharpValue.GetRecordFields
| "GetRecordFields" ->
Helper.LibCall(com, "Reflection", "get_record_elements", t, args, i.SignatureArgTypes, ?loc = r)
// Drop the trailing `allowAccessToPrivateRepresentation` flag (no meaning in Python)
Helper.LibCall(com, "Reflection", "get_record_elements", t, List.truncate 1 args, i.SignatureArgTypes, ?loc = r)
|> Some
| "GetUnionCases"
| "GetTupleElements"
Expand All @@ -3992,6 +3993,16 @@ let fsharpValue com methName (r: SourceLocation option) t (i: CallInfo) (args: E
| "MakeUnion"
| "MakeRecord"
| "MakeTuple" ->
// Drop the trailing `allowAccessToPrivateRepresentation` flag, a .NET-only
// concept the runtime helpers don't accept.
let args =
match methName with
| "GetRecordFields" -> List.truncate 1 args
| "GetUnionFields"
| "MakeUnion"
| "MakeRecord" -> List.truncate 2 args
| _ -> args

Helper.LibCall(com, "Reflection", Naming.lowerFirst methName, t, args, i.SignatureArgTypes, ?loc = r)
|> Some
| "GetExceptionFields" -> None // TODO!!!
Expand Down
13 changes: 12 additions & 1 deletion src/Fable.Transforms/Replacements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4154,7 +4154,8 @@ let fsharpType com (ctx: Context) methName (r: SourceLocation option) t (i: Call
|> Some
// Prevent name clash with FSharpValue.GetRecordFields
| "GetRecordFields" ->
Helper.LibCall(com, "Reflection", "getRecordElements", t, args, i.SignatureArgTypes, ?loc = r)
// Drop the trailing `allowAccessToPrivateRepresentation` flag (no meaning in JS/TS)
Helper.LibCall(com, "Reflection", "getRecordElements", t, List.truncate 1 args, i.SignatureArgTypes, ?loc = r)
|> Some
| "GetUnionCases"
| "GetTupleElements"
Expand Down Expand Up @@ -4185,6 +4186,16 @@ let fsharpValue com methName (r: SourceLocation option) t (i: CallInfo) (args: E
| "MakeUnion"
| "MakeRecord"
| "MakeTuple" ->
// Drop the trailing `allowAccessToPrivateRepresentation` flag, a .NET-only
// concept the runtime helpers don't accept.
let args =
match methName with
| "GetRecordFields" -> List.truncate 1 args
| "GetUnionFields"
| "MakeUnion"
| "MakeRecord" -> List.truncate 2 args
| _ -> args

Helper.LibCall(com, "Reflection", Naming.lowerFirst methName, t, args, i.SignatureArgTypes, ?loc = r)
|> Some
| "GetExceptionFields" -> None // TODO!!!
Expand Down
18 changes: 18 additions & 0 deletions tests/Js/Main/ReflectionTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,24 @@ let reflectionTests = [
let all = isRecord && matchRecordFields && matchIndividualRecordFields && canMakeSameRecord
all |> equal true

testCase "Reflection functions accept allowAccessToPrivateRepresentation" <| fun () ->
let recordType = typeof<TestRecord>
let record = { String = "a"; Int = 1 }

FSharpType.GetRecordFields(recordType, allowAccessToPrivateRepresentation = true).Length |> equal 2

let values = FSharpValue.GetRecordFields(record, allowAccessToPrivateRepresentation = true)
let rebuilt =
FSharpValue.MakeRecord(recordType, values, allowAccessToPrivateRepresentation = true) :?> TestRecord
rebuilt |> equal record

let unionType = typeof<TestUnion>
let intCase = FSharpType.GetUnionCases(unionType).[1]
let u = FSharpValue.MakeUnion(intCase, [| box 5 |], allowAccessToPrivateRepresentation = true) :?> TestUnion
let info, fields = FSharpValue.GetUnionFields(u, unionType, allowAccessToPrivateRepresentation = true)
info.Name |> equal "IntCase"
fields.[0] |> equal (box 5)

testCase "PropertyInfo.GetValue works" <| fun () ->
let value: obj = { Firstname = "Maxime"; Age = 12 } :> obj

Expand Down
19 changes: 19 additions & 0 deletions tests/Python/TestReflection.fs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,25 @@ let ``test Reflection Array`` () =
typeof<bool> = elType |> equal false
liType.GetElementType() |> equal null

[<Fact>]
let ``test reflection functions accept allowAccessToPrivateRepresentation`` () =
let recordType = typeof<MyRecord>
let record = { String = "a"; Int = 1 }

FSharpType.GetRecordFields(recordType, allowAccessToPrivateRepresentation = true).Length |> equal 2

let values = FSharpValue.GetRecordFields(record, allowAccessToPrivateRepresentation = true)
let rebuilt =
FSharpValue.MakeRecord(recordType, values, allowAccessToPrivateRepresentation = true) :?> MyRecord
rebuilt |> equal record

let unionType = typeof<MyUnion>
let intCase = FSharpType.GetUnionCases(unionType).[1]
let u = FSharpValue.MakeUnion(intCase, [| box 5 |], allowAccessToPrivateRepresentation = true) :?> MyUnion
let info, fields = FSharpValue.GetUnionFields(u, unionType, allowAccessToPrivateRepresentation = true)
info.Name |> equal "IntCase"
fields.[0] |> equal (box 5)

#if FABLE_COMPILER
[<Fact>]
let ``test FSharp.Reflection Record`` () =
Expand Down
Loading