-
Notifications
You must be signed in to change notification settings - Fork 326
Expand file tree
/
Copy pathReplacements.fs
More file actions
4016 lines (3626 loc) · 176 KB
/
Copy pathReplacements.fs
File metadata and controls
4016 lines (3626 loc) · 176 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
[<RequireQualifiedAccess>]
module Fable.Transforms.Py.Replacements
#nowarn "1182"
open System
open Fable
open Fable.AST
open Fable.AST.Fable
open Fable.Py
open Fable.Transforms
open Replacements.Util
type Context = FSharp2Fable.Context
type ICompiler = FSharp2Fable.IFableCompiler
type CallInfo = ReplaceCallInfo
let (|TypedArrayCompatible|_|) (com: Compiler) (arrayKind: ArrayKind) t =
match arrayKind, t with
| ResizeArray, _ -> None
| _, Number(kind, _) when com.Options.TypedArrays ->
match kind with
| Int8 -> Some "Int8ArrayCons"
| UInt8 -> Some "UInt8ArrayCons"
| Int16 -> Some "Int16ArrayCons"
| UInt16 -> Some "UInt16ArrayCons"
| Int32 -> Some "Int32ArrayCons"
| UInt32 -> Some "UInt32ArrayCons"
| Float32 -> Some "Float32ArrayCons"
| Float64 -> Some "Float64ArrayCons"
// Don't use typed array for int64 until we remove our int64 polyfill
// and use JS BigInt to represent int64
// | Int64 -> Some "BigInt64ArrayCons"
// | UInt64 -> Some "BigUint64ArrayCons"
| Int128
| UInt128
| Float16
| Int64
| UInt64
| BigInt
| Decimal
| NativeInt
| UNativeInt -> None
| _ -> None
let error com msg =
Helper.ConstructorCall(makeIdentExpr "Exception", Any, [ msg ])
/// Wraps String arguments with to_enumerable for Seq module compatibility.
/// Python strings don't implement IEnumerable_1, so they need to be wrapped when used as sequences.
let wrapStringToEnumerable com (arg: Expr) =
let rec getInnerType expr =
match expr with
| TypeCast(inner, _) -> getInnerType inner
| _ -> expr.Type
match getInnerType arg with
| String -> Helper.LibCall(com, "util", "to_enumerable", arg.Type, [ arg ])
| _ -> arg
/// Wraps IDictionary/Dictionary arguments with to_enumerable(dict.items()) for Seq module compatibility.
/// When iterating over a dictionary in F#, you get KeyValuePair items, but Python dict
/// iteration yields keys only. Calling .items() gives us the key-value pairs, and
/// to_enumerable wraps it as IEnumerable_1 for proper type compatibility.
/// Note: Only wraps direct dict args, not those already wrapped in TypeCast (handled by transformCast).
let wrapDictToItems com (arg: Expr) =
match arg.Type with
| DeclaredType(ent, [ keyType; valueType ]) when ent.FullName = Types.idictionary || ent.FullName = Types.dictionary ->
// First call .items() on the dict, then wrap with to_enumerable
let tupleType = Tuple([ keyType; valueType ], false)
let itemsReturnType = makeRuntimeType [ tupleType ] Types.ienumerableGeneric
let itemsCall = Helper.InstanceCall(arg, "items", itemsReturnType, [])
// Wrap with to_enumerable to get IEnumerable_1
Helper.LibCall(com, "util", "to_enumerable", itemsReturnType, [ itemsCall ])
| _ -> arg
let coreModFor =
function
| BclGuid -> "guid"
| BclDateTime -> "date"
| BclDateTimeOffset -> "date_offset"
| BclTimer -> "timer"
| BclTimeSpan -> "time_span"
| FSharpSet _ -> "set"
| FSharpMap _ -> "map"
| FSharpResult _ -> "result"
| FSharpChoice _ -> "choice"
| FSharpReference _ -> "core"
| BclHashSet _ -> "mutable_set"
| BclDictionary _ -> "mutable_map"
| BclKeyValuePair _
| BclDateOnly
| BclTimeOnly -> FableError "Cannot decide core module" |> raise
let makeDecimal com r t (x: decimal) =
let str = x.ToString(System.Globalization.CultureInfo.InvariantCulture)
Helper.LibCall(com, "decimal", "Decimal", t, [ makeStrConst str ], isConstructor = true, ?loc = r)
let makeDecimalFromExpr com r t (e: Expr) =
match e with
| Value(Fable.NumberConstant(NumberValue.Float32 x, _), _) -> makeDecimal com r t (decimal x)
| Value(Fable.NumberConstant(NumberValue.Float64 x, _), _) -> makeDecimal com r t (decimal x)
| Value(Fable.NumberConstant(NumberValue.Decimal x, _), _) -> makeDecimal com r t x
| _ -> Helper.LibCall(com, "decimal_", "create", t, [ e ], isConstructor = true, ?loc = r)
let makeAtomType genArg =
makeDeclaredType "Fable.Library.Python" [ genArg ] "Fable.Library.Python.Atom"
let createAtom com (value: Expr) =
let typ = value.Type
let atomType = makeAtomType typ
Helper.LibCall(com, "util", "create_atom", atomType, [ value ], [ typ ])
let getRefCell com r typ (expr: Expr) = getFieldWith r typ expr "contents"
let setRefCell com r (expr: Expr) (value: Expr) =
setExpr r expr (makeStrConst "contents") value
let makeRefCell com r genArg args =
let typ = makeFSharpCoreType [ genArg ] Types.refCell
Helper.LibCall(com, "core", "FSharpRef", typ, args, isConstructor = true, ?loc = r)
let makeRefCellFromValue com r (value: Expr) =
let typ = value.Type
makeRefCell com r typ [ value ]
let makeRefFromMutableValue com ctx r t (value: Expr) =
let getter = Delegate([], value, None, Tags.empty)
let setter =
let v = makeUniqueIdent com ctx t "v"
Delegate([ v ], Set(value, ValueSet, t, IdentExpr v, None), None, Tags.empty)
makeRefCell com r t [ getter; setter ]
let makeRefFromMutableField com ctx r t callee key =
let getter =
Delegate([], Get(callee, FieldInfo.Create(key, isMutable = true), t, r), None, Tags.empty)
let setter =
let v = makeUniqueIdent com ctx t "v"
Delegate([ v ], Set(callee, FieldSet(key), t, IdentExpr v, r), None, Tags.empty)
makeRefCell com r t [ getter; setter ]
// Mutable and public module values are compiled as functions, because
// values imported from ES2015 modules cannot be modified (see #986)
let makeRefFromMutableFunc com ctx r t (value: Expr) =
let getter =
let info = makeCallInfo None [] []
let value = makeCall r t info value
Delegate([], value, None, Tags.empty)
let setter =
let v = makeUniqueIdent com ctx t "v"
let args = [ IdentExpr v ]
let info = makeCallInfo None args [ t; Boolean ]
let value = makeCall r Unit info value
Delegate([ v ], value, None, Tags.empty)
makeRefCell com r t [ getter; setter ]
let makeEqOpStrict range left right op =
Operation(Binary(op, left, right), [ "strict" ], Boolean, range)
let getParseParams (kind: NumberKind) =
let isFloatOrDecimal, numberModule, unsigned, bitsize =
match kind with
| Int8 -> false, "Int32", false, 8
| UInt8 -> false, "Int32", true, 8
| Int16 -> false, "Int32", false, 16
| UInt16 -> false, "Int32", true, 16
| Int32 -> false, "Int32", false, 32
| UInt32 -> false, "Int32", true, 32
| Int64 -> false, "Long", false, 64
| UInt64 -> false, "Long", true, 64
| Float32 -> true, "Double", false, 32
| Float64 -> true, "Double", false, 64
| Decimal -> true, "Decimal", false, 128
| x -> FableError $"Unexpected kind in getParseParams: %A{x}" |> raise
isFloatOrDecimal, numberModule, unsigned, bitsize
/// Gets the type name for calling static parse methods on integer types
let getIntTypeName (kind: NumberKind) =
match kind with
| Int8 -> "int8"
| UInt8 -> "uint8"
| Int16 -> "int16"
| UInt16 -> "uint16"
| Int32 -> "int32"
| UInt32 -> "uint32"
| Int64 -> "int64"
| UInt64 -> "uint64"
| x -> FableError $"Unexpected kind in getIntTypeName: %A{x}" |> raise
let castBigIntMethod typeTo =
match typeTo with
| Number(kind, _) ->
match kind with
| Int8 -> "toSByte"
| Int16 -> "toInt16"
| Int32 -> "toInt32"
| Int64 -> "toInt64"
| UInt8 -> "toByte"
| UInt16 -> "toUInt16"
| UInt32 -> "toUInt32"
| UInt64 -> "toUInt64"
| Float32 -> "toSingle"
| Float64 -> "toDouble"
| Decimal -> "toDecimal"
| Int128
| UInt128
| Float16
| BigInt
| NativeInt
| UNativeInt -> FableError $"Unexpected BigInt/%A{kind} conversion" |> raise
| _ -> FableError $"Unexpected non-number type %A{typeTo}" |> raise
let kindIndex kind = // 0 1 2 3 4 5 6 7 8 9 10 11
match kind with // i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 dec big
| Int8 -> 0 // 0 i8 - - - - + + + + - - - +
| Int16 -> 1 // 1 i16 + - - - + + + + - - - +
| Int32 -> 2 // 2 i32 + + - - + + + + - - - +
| Int64 -> 3 // 3 i64 + + + - + + + + - - - +
| UInt8 -> 4 // 4 u8 + + + + - - - - - - - +
| UInt16 -> 5 // 5 u16 + + + + + - - - - - - +
| UInt32 -> 6 // 6 u32 + + + + + + - - - - - +
| UInt64 -> 7 // 7 u64 + + + + + + + - - - - +
| Float32 -> 8 // 8 f32 + + + + + + + + - - - +
| Float64 -> 9 // 9 f64 + + + + + + + + - - - +
| Decimal -> 10 // 10 dec + + + + + + + + - - - +
| BigInt -> 11 // 11 big + + + + + + + + + + + -
| Float16 -> FableError "Casting to/from float16 is unsupported" |> raise
| Int128
| UInt128 -> FableError "Casting to/from (u)int128 is unsupported" |> raise
| NativeInt
| UNativeInt -> FableError "Casting to/from (u)nativeint is unsupported" |> raise
let needToCast fromKind toKind =
let v = kindIndex fromKind // argument type (vertical)
let h = kindIndex toKind // return type (horizontal)
((v > h) || (v < 4 && h > 3)) && (h < 8) || (h <> v && (h = 11 || v = 11))
/// Conversions to floating point
let toFloat com (ctx: Context) r targetType (args: Expr list) : Expr =
match args.Head.Type with
| Char ->
//Helper.InstanceCall(args.Head, "charCodeAt", Int32.Number, [ makeIntConst 0 ])
Helper.LibCall(com, "char", "char_code_at", targetType, [ args.Head; makeIntConst 0 ])
| String ->
// Use parse_single for Float32, parse for Float64
let meth =
match targetType with
| Number(Float32, _) -> "parse_single"
| _ -> "parse"
Helper.LibCall(com, "double", meth, targetType, args)
| Number(kind, _) ->
match kind with
| BigInt -> Helper.LibCall(com, "big_int", castBigIntMethod targetType, targetType, args)
| Decimal -> Helper.LibCall(com, "decimal", "to_number", targetType, args)
| Int64
| UInt64 -> Helper.LibCall(com, "long", "to_number", targetType, args)
| _ -> TypeCast(args.Head, targetType)
| _ ->
addWarning com ctx.InlinePath r "Cannot make conversion because source type is unknown"
TypeCast(args.Head, targetType)
let toDecimal com (ctx: Context) r targetType (args: Expr list) : Expr =
match args.Head.Type with
| Char ->
//Helper.InstanceCall(args.Head, "charCodeAt", Int32.Number, [ makeIntConst 0 ])
Helper.LibCall(com, "char", "char_code_at", targetType, [ args.Head; makeIntConst 0 ])
|> makeDecimalFromExpr com r targetType
| String -> makeDecimalFromExpr com r targetType args.Head
| Number(kind, _) ->
match kind with
| Decimal -> args.Head
| BigInt -> Helper.LibCall(com, "big_int", castBigIntMethod targetType, targetType, args)
| _ -> makeDecimalFromExpr com r targetType args.Head
| _ ->
addWarning com ctx.InlinePath r "Cannot make conversion because source type is unknown"
TypeCast(args.Head, targetType)
let stringToInt com (_ctx: Context) r targetType (args: Expr list) : Expr =
let kind =
match targetType with
| Number(kind, _) -> kind
| x -> FableError $"Unexpected type in stringToInt: %A{x}" |> raise
let style = int System.Globalization.NumberStyles.Any
// Use the type's static parse method: e.g., int8.parse(string, style)
let typeName = getIntTypeName kind
let typeExpr = Helper.LibValue(com, "core", typeName, Any)
Helper.InstanceCall(typeExpr, "parse", targetType, [ args.Head; makeIntConst style ] @ args.Tail, ?loc = r)
let toLong com (ctx: Context) r (unsigned: bool) targetType (args: Expr list) : Expr =
let fromInteger kind arg =
let kind = makeIntConst (kindIndex kind)
Helper.LibCall(com, "long", "from_integer", targetType, [ arg; makeBoolConst unsigned; kind ])
let sourceType = args.Head.Type
match sourceType with
| Char ->
//Helper.InstanceCall(args.Head, "charCodeAt", Int32.Number, [ makeIntConst 0 ])
Helper.LibCall(com, "char", "char_code_at", targetType, [ args.Head; makeIntConst 0 ])
|> fromInteger UInt16
| String -> stringToInt com ctx r targetType args
| Number(kind, _) ->
match kind with
| Decimal -> Helper.LibCall(com, "decimal", "to_int", targetType, args)
| BigInt -> Helper.LibCall(com, "big_int", castBigIntMethod targetType, targetType, args)
| Int64
| UInt64 -> Helper.LibCall(com, "long", "from_value", targetType, args @ [ makeBoolConst unsigned ])
| Int8
| Int16
| Int32
| UInt8
| UInt16
| UInt32 as kind -> fromInteger kind args.Head
| Float32
| Float64 -> Helper.LibCall(com, "long", "from_number", targetType, args @ [ makeBoolConst unsigned ])
| Float16 -> FableError "Casting float16 to long is not supported" |> raise
| Int128
| UInt128 -> FableError "Casting (u)int128 to long is not supported" |> raise
| NativeInt
| UNativeInt -> FableError "Converting (u)nativeint to long is not supported" |> raise
| _ ->
addWarning com ctx.InlinePath r "Cannot make conversion because source type is unknown"
TypeCast(args.Head, targetType)
/// Conversion to integers (excluding longs and bigints)
let toInt com (ctx: Context) r targetType (args: Expr list) =
let sourceType = args.Head.Type
let emitCast typeTo arg =
match typeTo with
| Int8 -> Helper.LibCall(com, "core", "sbyte", targetType, [ arg ])
| Int16 -> Helper.LibCall(com, "core", "int16", targetType, [ arg ])
| Int32 -> Helper.LibCall(com, "core", "int32", targetType, [ arg ])
| UInt8 -> Helper.LibCall(com, "core", "byte", targetType, [ arg ])
| UInt16 -> Helper.LibCall(com, "core", "uint16", targetType, [ arg ])
| UInt32 -> Helper.LibCall(com, "core", "uint32", targetType, [ arg ])
| _ ->
// Use normal Python int for BigInt, NativeInt, UNativeInt
Helper.GlobalCall("int", targetType, [ arg ])
match sourceType, targetType with
| Char, Number(typeTo, _) ->
Helper.LibCall(com, "char", "char_code_at", targetType, [ args.Head; makeIntConst 0 ])
|> emitCast typeTo
| String, _ -> stringToInt com ctx r targetType args
| Number(BigInt, _), _ -> Helper.LibCall(com, "big_int", castBigIntMethod targetType, targetType, args)
| Number(typeFrom, _), Number(typeTo, _) ->
if needToCast typeFrom typeTo then
match typeFrom with
| Int64
| UInt64 -> Helper.LibCall(com, "core", "int32", targetType, args)
| Decimal -> Helper.LibCall(com, "Decimal", "to_int", targetType, args)
| _ -> args.Head
|> emitCast typeTo
else
TypeCast(args.Head, targetType)
| _ ->
addWarning com ctx.InlinePath r "Cannot make conversion because source type is unknown"
TypeCast(args.Head, targetType)
let toChar com (ctx: Context) r (arg: Expr) =
match arg.Type with
| Char
| String -> arg
| _ ->
let code = Helper.GlobalCall("int", Int32.Number, [ arg ])
Helper.GlobalCall("chr", Char, [ code ])
let toString com (ctx: Context) r (args: Expr list) =
match args with
| [] ->
"toString is called with empty args"
|> addErrorAndReturnNull com ctx.InlinePath r
| head :: tail ->
match head.Type with
| Char -> TypeCast(head, String)
| String -> head
| Builtin BclGuid when tail.IsEmpty -> Helper.GlobalCall("str", String, [ head ], ?loc = r)
| Builtin(BclGuid | BclTimeSpan as bt) -> Helper.LibCall(com, coreModFor bt, "to_string", String, args)
| Number(Int32, _) ->
let expr = Helper.LibCall(com, "core", "int32", head.Type, [ head ], ?loc = r)
Helper.InstanceCall(expr, "to_string", String, tail, ?loc = r)
| Number((Int8 | UInt8 | UInt16 | Int16 | UInt32 | Int64 | UInt64), _) ->
if tail.Length > 0 then
Helper.InstanceCall(head, "to_string", String, tail, ?loc = r)
else
Helper.GlobalCall("str", String, [ head ], ?loc = r)
| Number(BigInt, _) -> Helper.LibCall(com, "util", "int_to_string", String, args)
| Number(Decimal, _) -> Helper.LibCall(com, "decimal", "to_string", String, args)
| Number _ -> Helper.LibCall(com, "exceptions", "to_string", String, [ head ], ?loc = r)
| Array _
| List _ -> Helper.LibCall(com, "exceptions", "seq_to_string", String, [ head ], ?loc = r)
// | DeclaredType(ent, _) when ent.IsFSharpUnion || ent.IsFSharpRecord || ent.IsValueType ->
// Helper.InstanceCall(head, "toString", String, [], ?loc=r)
// | DeclaredType(ent, _) ->
| _ -> Helper.LibCall(com, "exceptions", "to_string", String, [ head ], ?loc = r)
let round com (args: Expr list) =
match args.Head.Type with
| Number(Decimal, _) ->
let n = Helper.LibCall(com, "decimal", "to_number", Float64.Number, [ args.Head ])
let rounded = Helper.LibCall(com, "util", "round", Float64.Number, [ n ])
rounded :: args.Tail
| Number((Float32 | Float64), _) ->
let rounded = Helper.LibCall(com, "util", "round", Float64.Number, [ args.Head ])
rounded :: args.Tail
| _ -> args
let toList com returnType expr =
Helper.LibCall(com, "list", "ofSeq", returnType, [ expr ])
let stringToCharArray t e =
let elementType = getElementType t
makeArrayFrom elementType e
let toSeq t (e: Expr) =
match e.Type with
// Convert to array to get 16-bit code units, see #1279
| String -> stringToCharArray t e
| _ -> TypeCast(e, t)
let applyOp (com: ICompiler) (ctx: Context) r t opName (args: Expr list) =
let unOp operator operand =
Operation(Unary(operator, operand), Tags.empty, t, r)
let binOp op left right =
Operation(Binary(op, left, right), Tags.empty, t, r)
let binOpChar op left right =
let toUInt16 e = toInt com ctx r UInt16.Number [ e ]
Operation(Binary(op, toUInt16 left, toUInt16 right), Tags.empty, UInt16.Number, r)
|> toChar com ctx r
let truncateUnsigned operation = // see #1550
match t with
| Number(UInt32, _) -> Operation(Binary(BinaryShiftRightZeroFill, operation, makeIntConst 0), Tags.empty, t, r)
| _ -> operation
let logicOp op left right =
Operation(Logical(op, left, right), Tags.empty, Boolean, r)
let nativeOp opName argTypes args =
match opName, args with
| Operators.addition, [ left; right ] ->
match argTypes with
| Char :: _ -> binOpChar BinaryPlus left right
| _ -> binOp BinaryPlus left right
| Operators.subtraction, [ left; right ] ->
match argTypes with
| Char :: _ -> binOpChar BinaryMinus left right
| _ -> binOp BinaryMinus left right
| Operators.multiply, [ left; right ] -> binOp BinaryMultiply left right
| (Operators.division | Operators.divideByInt), [ left; right ] -> binOp BinaryDivide left right
| Operators.modulus, [ left; right ] -> binOp BinaryModulus left right
| Operators.leftShift, [ left; right ] -> binOp BinaryShiftLeft left right |> truncateUnsigned // See #1530
| Operators.rightShift, [ left; right ] ->
match argTypes with
| Number(UInt32, _) :: _ -> binOp BinaryShiftRightZeroFill left right // See #646
| _ -> binOp BinaryShiftRightSignPropagating left right
| Operators.bitwiseAnd, [ left; right ] -> binOp BinaryAndBitwise left right |> truncateUnsigned
| Operators.bitwiseOr, [ left; right ] -> binOp BinaryOrBitwise left right |> truncateUnsigned
| Operators.exclusiveOr, [ left; right ] -> binOp BinaryXorBitwise left right |> truncateUnsigned
| Operators.booleanAnd, [ left; right ] -> logicOp LogicalAnd left right
| Operators.booleanOr, [ left; right ] -> logicOp LogicalOr left right
| Operators.logicalNot, [ operand ] -> unOp UnaryNotBitwise operand |> truncateUnsigned
| Operators.unaryNegation, [ operand ] ->
match argTypes with
| Number(Int8, _) :: _ -> Helper.LibCall(com, "int32", "op_unary_negation_int8", t, args, ?loc = r)
| Number(Int16, _) :: _ -> Helper.LibCall(com, "int32", "op_unary_negation_int16", t, args, ?loc = r)
| Number(Int32, _) :: _ -> Helper.LibCall(com, "int32", "op_unary_negation_int32", t, args, ?loc = r)
| _ -> unOp UnaryMinus operand
| Operators.unaryPlus, [ operand ] -> unOp UnaryPlus operand
| _ ->
$"Operator %s{opName} not found in %A{argTypes}"
|> addErrorAndReturnNull com ctx.InlinePath r
let argTypes = args |> List.map (fun a -> a.Type)
match argTypes with
| Number(Int64 | UInt64 | BigInt | Decimal as kind, _) :: _ ->
let modName, opName =
match kind, opName with
// | UInt64, Operators.rightShift -> "long", "op_RightShiftUnsigned" // See #1482
| Decimal, Operators.divideByInt -> "decimal", Operators.division
| Decimal, _ -> "decimal", opName
| BigInt, _ -> "big_int", opName
| _ -> "long", opName
Helper.LibCall(com, modName, opName, t, args, argTypes, ?loc = r)
| Builtin(BclDateTime | BclDateTimeOffset as bt) :: _ ->
Helper.LibCall(com, coreModFor bt, opName, t, args, argTypes, ?loc = r)
| Builtin(FSharpSet _) :: _ ->
let mangledName = Naming.buildNameWithoutSanitationFrom "FSharpSet" true opName ""
Helper.LibCall(com, "set", mangledName, t, args, argTypes, ?loc = r)
// | Builtin (FSharpMap _)::_ ->
// let mangledName = Naming.buildNameWithoutSanitationFrom "FSharpMap" true opName overloadSuffix.Value
// Helper.LibCall(com, "Map", mangledName, t, args, argTypes, ?loc=r)
| Builtin BclTimeSpan :: _ -> nativeOp opName argTypes args
| CustomOp com ctx r t opName args e -> e
| _ -> nativeOp opName argTypes args
let isCompatibleWithNativeComparison =
function
| Builtin(BclGuid | BclTimeSpan | BclTimeOnly)
| Boolean
| Char
| String
| Number _ -> true
// TODO: Non-record/union declared types without custom equality
// should be compatible with Py comparison
| _ -> false
// Overview of hash rules:
// * `hash`, `Unchecked.hash` first check if GetHashCode is implemented and then default to structural hash.
// * `.GetHashCode` called directly defaults to identity hash (for reference types except string) if not implemented.
// * `LanguagePrimitive.PhysicalHash` creates an identity hash no matter whether GetHashCode is implemented or not.
let identityHash com r (arg: Expr) =
let methodName =
match arg.Type with
// These are the same for identity/structural hashing
| Char
| String
| Builtin BclGuid -> "stringHash"
| Number((Decimal | BigInt | Int64 | UInt64), _) -> "safeHash"
| Number _
| Builtin BclTimeSpan -> "numberHash"
| List _ -> "safeHash"
| Tuple _ -> "arrayHash" // F# tuples must use structural hashing
// These are only used for structural hashing
// | Array _ -> "arrayHash"
// | Builtin (BclDateTime|BclDateTimeOffset) -> "dateHash"
| DeclaredType _ -> "safeHash"
| _ -> "identityHash"
Helper.LibCall(com, "Util", methodName, Int32.Number, [ arg ], ?loc = r)
let structuralHash (com: ICompiler) r (arg: Expr) =
let methodName =
match arg.Type with
| Char
| String
| Builtin BclGuid -> "stringHash"
| Number _
| Builtin BclTimeSpan -> "numberHash"
| List _ -> "safeHash"
// TODO: Get hash functions of the generic arguments
// for better performance when using tuples as map keys
| Tuple _
| Array _ -> "arrayHash"
| Builtin(BclDateTime | BclDateTimeOffset) -> "dateHash"
| DeclaredType(ent, _) ->
let ent = com.GetEntity(ent)
if not ent.IsInterface then
"safeHash"
else
"structuralHash"
| _ -> "structuralHash"
Helper.LibCall(com, "Util", methodName, Int32.Number, [ arg ], ?loc = r)
let rec equals (com: ICompiler) ctx r equal (left: Expr) (right: Expr) =
let is equal expr =
if equal then
expr
else
makeUnOp None Boolean expr UnaryNot
match left.Type with
| Number(Decimal, _) ->
Helper.LibCall(com, "decimal", "equals", Boolean, [ left; right ], ?loc = r)
|> is equal
| Number(BigInt, _) ->
Helper.LibCall(com, "big_int", "equals", Boolean, [ left; right ], ?loc = r)
|> is equal
| Builtin(BclGuid | BclTimeSpan)
| Boolean
| Char
| String
| Number _
| Nullable _
| MetaType ->
let op =
if equal then
BinaryEqual
else
BinaryUnequal
makeBinOp r Boolean left right op
| Builtin(BclDateTime | BclDateTimeOffset) ->
Helper.LibCall(com, "date", "equals", Boolean, [ left; right ], ?loc = r)
|> is equal
| Builtin(FSharpSet _ | FSharpMap _) -> Helper.InstanceCall(left, "Equals", Boolean, [ right ]) |> is equal
| DeclaredType _ ->
Helper.LibCall(com, "util", "equals", Boolean, [ left; right ], ?loc = r)
|> is equal
| Array(t, _) ->
let f = makeEqualityFunction com ctx t
Helper.LibCall(com, "array", "equals_with", Boolean, [ f; left; right ], ?loc = r)
|> is equal
| List _ ->
Helper.LibCall(com, "util", "equals", Boolean, [ left; right ], ?loc = r)
|> is equal
| MetaType ->
Helper.LibCall(com, "reflection", "equals", Boolean, [ left; right ], ?loc = r)
|> is equal
| Tuple _ ->
Helper.LibCall(com, "util", "equalArrays", Boolean, [ left; right ], ?loc = r)
|> is equal
| _ ->
Helper.LibCall(com, "util", "equals", Boolean, [ left; right ], ?loc = r)
|> is equal
/// Compare function that will call Util.compare or instance `CompareTo` as appropriate
and compare (com: ICompiler) ctx r (left: Expr) (right: Expr) =
let t = Int32.Number
match left.Type with
| Number(Decimal, _) -> Helper.LibCall(com, "decimal", "compare", t, [ left; right ], ?loc = r)
| Number(BigInt, _) -> Helper.LibCall(com, "big_int", "compare", t, [ left; right ], ?loc = r)
| Builtin(BclGuid | BclTimeSpan)
| Boolean
| Char
| String
| Number _ -> Helper.LibCall(com, "util", "comparePrimitives", t, [ left; right ], ?loc = r)
| Builtin(BclDateTime | BclDateTimeOffset) -> Helper.LibCall(com, "date", "compare", t, [ left; right ], ?loc = r)
| DeclaredType _ -> Helper.LibCall(com, "util", "compare", t, [ left; right ], ?loc = r)
| Array(genArg, _) ->
let f = makeComparerFunction com ctx genArg
// TODO: change to compareTo after main sync. See #2961
Helper.LibCall(com, "array", "compare_to", t, [ f; left; right ], ?loc = r)
| List _ -> Helper.LibCall(com, "util", "compare", t, [ left; right ], ?loc = r)
| Tuple _ -> Helper.LibCall(com, "util", "compareArrays", t, [ left; right ], ?loc = r)
| _ -> Helper.LibCall(com, "util", "compare", t, [ left; right ], ?loc = r)
/// Boolean comparison operators like <, >, <=, >=
and booleanCompare (com: ICompiler) ctx r (left: Expr) (right: Expr) op =
if isCompatibleWithNativeComparison left.Type then
makeEqOp r left right op
else
let comparison = compare com ctx r left right
makeEqOp r comparison (makeIntConst 0) op
and makeComparerFunction (com: ICompiler) ctx typArg =
let x = makeUniqueIdent com ctx typArg "x"
let y = makeUniqueIdent com ctx typArg "y"
let body = compare com ctx None (IdentExpr x) (IdentExpr y)
Delegate([ x; y ], body, None, Tags.empty)
and makeComparer (com: ICompiler) ctx typArg =
objExpr [ "Compare", makeComparerFunction com ctx typArg ]
and makeEqualityFunction (com: ICompiler) ctx typArg =
let x = makeUniqueIdent com ctx typArg "x"
let y = makeUniqueIdent com ctx typArg "y"
let body = equals com ctx None true (IdentExpr x) (IdentExpr y)
Delegate([ x; y ], body, None, Tags.empty)
let makeEqualityComparer (com: ICompiler) ctx typArg =
let x = makeUniqueIdent com ctx typArg "x"
let y = makeUniqueIdent com ctx typArg "y"
objExpr
[
"Equals", Delegate([ x; y ], equals com ctx None true (IdentExpr x) (IdentExpr y), None, Tags.empty)
"GetHashCode", Delegate([ x ], structuralHash com None (IdentExpr x), None, Tags.empty)
]
// TODO: Try to detect at compile-time if the object already implements `Compare`?
let inline makeComparerFromEqualityComparer e = e // leave it as is, if implementation supports it
// Helper.LibCall(com, "Util", "comparerFromEqualityComparer", Any, [e])
/// Adds comparer as last argument for set creator methods
let makeSet (com: ICompiler) ctx r t methName args genArg =
let args = args @ [ makeComparer com ctx genArg ]
Helper.LibCall(com, "set", Naming.lowerFirst methName, t, args, ?loc = r)
/// Adds comparer as last argument for map creator methods
let makeMap (com: ICompiler) ctx r t methName args genArg =
let args = args @ [ makeComparer com ctx genArg ]
Helper.LibCall(com, "map", Naming.lowerFirst methName, t, args, ?loc = r)
let makeDictionaryWithComparer com r t sourceSeq comparer =
Helper.LibCall(com, "mutable_map", "Dictionary", t, [ sourceSeq; comparer ], isConstructor = true, ?loc = r)
let makeDictionary (com: ICompiler) ctx r t sourceSeq =
match t with
| DeclaredType(_, [ key; _ ]) when not (isCompatibleWithNativeComparison key) ->
// makeComparer com ctx key
makeEqualityComparer com ctx key |> makeDictionaryWithComparer com r t sourceSeq
// Use make_dict which handles both Python iterables and Fable IEnumerable_1
| _ -> Helper.LibCall(com, "map_util", "make_dict", t, [ sourceSeq ], ?loc = r)
let makeHashSetWithComparer com r t sourceSeq comparer =
Helper.LibCall(com, "mutable_set", "HashSet", t, [ sourceSeq; comparer ], isConstructor = true, ?loc = r)
let makeHashSet (com: ICompiler) ctx r t sourceSeq =
match t with
| DeclaredType(_, [ key ]) when not (isCompatibleWithNativeComparison key) ->
// makeComparer com ctx key
makeEqualityComparer com ctx key |> makeHashSetWithComparer com r t sourceSeq
| _ -> Helper.GlobalCall("set", t, [ sourceSeq ], isConstructor = true, ?loc = r)
let rec getZero (com: ICompiler) ctx (t: Type) =
match t with
| Boolean -> makeBoolConst false
| Number(BigInt, _) as t -> Helper.LibCall(com, "big_int", "fromInt32", t, [ makeIntConst 0 ])
| Number(Decimal, _) as t -> makeIntConst 0 |> makeDecimalFromExpr com None t
| Number(kind, uom) -> NumberConstant(NumberValue.GetZero kind, uom) |> makeValue None
| Char
| String -> makeStrConst "" // TODO: Use null for string?
| Builtin BclTimeSpan -> Helper.LibCall(com, "time_span", "create", t, [ makeIntConst 0 ])
| Builtin BclDateTime as t -> Helper.LibCall(com, "date", "minValue", t, [])
| Builtin BclDateTimeOffset as t -> Helper.LibCall(com, "DateOffset", "minValue", t, [])
| Builtin(FSharpSet genArg) as t -> makeSet com ctx None t "Empty" [] genArg
| Builtin(BclKeyValuePair(k, v)) -> makeTuple None true [ getZero com ctx k; getZero com ctx v ]
| ListSingleton(CustomOp com ctx None t "get_Zero" [] e) -> e
| _ -> Value(Null Any, None) // null
let getOne (com: ICompiler) ctx (t: Type) =
match t with
| Boolean -> makeBoolConst true
| Number(kind, uom) -> NumberConstant(NumberValue.GetOne kind, uom) |> makeValue None
| ListSingleton(CustomOp com ctx None t "get_One" [] e) -> e
| _ -> makeIntConst 1
let makeAddFunction (com: ICompiler) ctx t =
let x = makeUniqueIdent com ctx t "x"
let y = makeUniqueIdent com ctx t "y"
let body = applyOp com ctx None t Operators.addition [ IdentExpr x; IdentExpr y ]
Delegate([ x; y ], body, None, Tags.empty)
let makeGenericAdder (com: ICompiler) ctx t =
objExpr
[
"GetZero", getZero com ctx t |> makeDelegate []
"Add", makeAddFunction com ctx t
]
let makeGenericAverager (com: ICompiler) ctx t =
let divideFn =
let x = makeUniqueIdent com ctx t "x"
let i = makeUniqueIdent com ctx (Int32.Number) "i"
let body = applyOp com ctx None t Operators.divideByInt [ IdentExpr x; IdentExpr i ]
Delegate([ x; i ], body, None, Tags.empty)
objExpr
[
"GetZero", getZero com ctx t |> makeDelegate []
"Add", makeAddFunction com ctx t
"DivideByInt", divideFn
]
let injectArg (com: ICompiler) (ctx: Context) r moduleName methName (genArgs: Type list) args =
let injectArgInner args (injectType, injectGenArgIndex) =
let fail () =
$"Cannot inject arg to %s{moduleName}.%s{methName} (genArgs %A{genArgs} - expected index %i{injectGenArgIndex})"
|> addError com ctx.InlinePath r
args
match List.tryItem injectGenArgIndex genArgs with
| None -> fail ()
| Some genArg ->
match injectType with
| Types.icomparerGeneric -> args @ [ makeComparer com ctx genArg ]
| Types.iequalityComparerGeneric -> args @ [ makeEqualityComparer com ctx genArg ]
| Types.arrayCons ->
match genArg with
// We don't have a module for ResizeArray so let's assume the kind is MutableArray
| TypedArrayCompatible com MutableArray consName ->
let cons = [ makeImportLib com Any consName "array_" ]
args @ cons
| _ ->
let cons = [ Expr.Value(ValueKind.NewOption(None, genArg, false), None) ]
args @ cons
| Types.adder -> args @ [ makeGenericAdder com ctx genArg ]
| Types.averager -> args @ [ makeGenericAverager com ctx genArg ]
| _ -> fail ()
Map.tryFind moduleName ReplacementsInject.fableReplacementsModules
|> Option.bind (Map.tryFind methName)
|> function
| None -> args
| Some injectInfo -> injectArgInner args injectInfo
let tryEntityIdent (com: Compiler) entFullName =
match entFullName with
| BuiltinDefinition BclDateOnly
| BuiltinDefinition BclDateTime
| BuiltinDefinition BclDateTimeOffset -> makeIdentExpr "Date" |> Some
| BuiltinDefinition BclTimer -> makeImportLib com Any "default" "Timer" |> Some
| BuiltinDefinition(FSharpReference _) -> makeImportLib com Any "FSharpRef" "Types" |> Some
| BuiltinDefinition(FSharpResult _) ->
// Import the underscore-prefixed base class (has cases() method), not the type alias
makeImportLib com Any "_FSharpResult_2" "Result" |> Some
| BuiltinDefinition(FSharpChoice genArgs) ->
// Import the underscore-prefixed base class (has cases() method), not the type alias
let membName = $"_FSharpChoice_%d{List.length genArgs}"
makeImportLib com Any membName "Choice" |> Some
// | BuiltinDefinition BclGuid -> jsTypeof "string" expr
// | BuiltinDefinition BclTimeSpan -> jsTypeof "number" expr
// | BuiltinDefinition BclHashSet _ -> fail "MutableSet" // TODO:
// | BuiltinDefinition BclDictionary _ -> fail "MutableMap" // TODO:
// | BuiltinDefinition BclKeyValuePair _ -> fail "KeyValuePair" // TODO:
// | BuiltinDefinition FSharpSet _ -> fail "Set" // TODO:
// | BuiltinDefinition FSharpMap _ -> fail "Map" // TODO:
| Types.matchFail -> makeImportLib com Any "MatchFailureException" "Types" |> Some
| Types.exception_ -> makeIdentExpr "Exception" |> Some
// | "System.Collections.Generic.KeyNotFoundException" ->
// makeImportLib com Any "KeyNotFoundException" "System.Collections.Generic" |> Some
| BuiltinSystemException entName -> makeImportLib com Any entName "System" |> Some
// | Naming.EndsWith "Exception" _ -> makeIdentExpr "Exception" |> Some
| _ -> None
let tryConstructor com (ent: Entity) =
if FSharp2Fable.Util.isReplacementCandidate ent.Ref then
tryEntityIdent com (ent.FullName |> Naming.toPythonNaming)
else
match FSharp2Fable.Util.tryEntityIdentMaybeGlobalOrImported com ent with
| Some(IdentExpr ident) when ent.IsFSharpUnion ->
// For F# union types, the base class is prefixed with underscore (_UnionName)
// This is needed for both reflection (base class has cases() method) and
// type annotations (self inside base class methods)
Some(IdentExpr { ident with Name = "_" + ident.Name })
| other -> other
let constructor com ent =
match tryConstructor com ent with
| Some e -> e
| None -> $"Cannot find %s{ent.FullName} constructor" |> addErrorAndReturnNull com [] None
let tryOp com r t op args =
Helper.LibCall(com, "option", "tryOp", t, op :: args, ?loc = r)
let tryCoreOp com r t coreModule coreMember args =
let op = Helper.LibValue(com, coreModule, coreMember, Any)
tryOp com r t op args
let emptyGuid com t =
Helper.LibCall(com, "guid", "parse", t, [ makeStrConst "00000000-0000-0000-0000-000000000000" ])
let rec defaultof com ctx r t =
match t with
| Nullable _ -> Value(Null t, r)
| Tuple(args, true) -> NewTuple(args |> List.map (defaultof com ctx r), true) |> makeValue None
| Boolean
| Number _
| Builtin BclTimeSpan
| Builtin BclDateTime
| Builtin BclDateTimeOffset -> getZero com ctx t
| Builtin BclGuid -> emptyGuid com t
| DeclaredType(ent, _) ->
let ent = com.GetEntity(ent)
// TODO: For BCL types we cannot access the constructor, raise error or warning?
if ent.IsValueType then
tryConstructor com ent
else
None
|> Option.map (fun e -> Helper.ConstructorCall(e, t, []))
|> Option.defaultWith (fun () -> Null t |> makeValue None)
// TODO: Fail (or raise warning) if this is an unresolved generic parameter?
| _ -> Null t |> makeValue None
let fableCoreLib (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr option) (args: Expr list) =
match i.DeclaringEntityFullName, i.CompiledName with
| _, UniversalFableCoreHelpers com ctx r t i args error expr -> Some expr
| "Fable.Core.Testing.Assert", _ ->
match i.CompiledName with
| "AreEqual" -> Helper.LibCall(com, "util", "assertEqual", t, args, ?loc = r) |> Some
| "NotEqual" -> Helper.LibCall(com, "util", "assertNotEqual", t, args, ?loc = r) |> Some
| _ -> None
| "Fable.Core.Reflection", meth -> Helper.LibCall(com, "reflection", meth, t, args, ?loc = r) |> Some
| "Fable.Core.Compiler", meth ->
match meth 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
| _ -> None
| "Fable.Core.Py", ("python" | "expr_python" as meth) ->
let isStatement = meth <> "expr_python"
match args with
| RequireStringConstOrTemplate com ctx r template :: _ -> emitTemplate r t [] isStatement template |> Some
| _ -> None
| "Fable.Core.PyInterop", _ ->
match i.CompiledName, args with
| Naming.StartsWith "import" suffix, _ ->
match suffix, args with
| "Member", [ RequireStringConst com ctx r path ] ->
makeImportUserGenerated r t Naming.placeholder path |> Some
| "Default", [ RequireStringConst com ctx r path ] -> makeImportUserGenerated r t "default" path |> Some
| "SideEffects", [ RequireStringConst com ctx r path ] -> makeImportUserGenerated r t "" path |> Some
| "All", [ RequireStringConst com ctx r path ] -> makeImportUserGenerated r t "*" path |> Some
| _, [ RequireStringConst com ctx r selector; RequireStringConst com ctx r path ] ->
makeImportUserGenerated r t selector path |> Some
| _ -> None
// Dynamic casting, erase
| "op_BangHat", [ arg ] -> Some arg
| "op_BangBang", [ arg ] ->
match arg, i.GenericArgs with
| IsNewAnonymousRecord(_, exprs, fieldNames, _, _, _), [ _; DeclaredType(ent, []) ] ->
let ent = com.GetEntity(ent)
if ent.IsInterface then
AnonRecords.fitsInInterface com r exprs fieldNames ent
|> function
| Error errors ->
errors
|> List.iter (fun (range, error) -> addWarning com ctx.InlinePath range error)
Some arg
| Ok() -> Some arg
else
Some arg
| _ -> Some arg
| "op_Dynamic", [ left; memb ] -> getExpr r t left memb |> Some
| "op_DynamicAssignment", [ callee; prop; MaybeLambdaUncurriedAtCompileTime value ] ->
setExpr r callee prop value |> Some
| ("op_Dollar" | "createNew" as m), callee :: args ->
let args = destructureTupleArgs args
if m = "createNew" then
"new $0($1...)"
else
"$0($1...)"
|> emitExpr r t (callee :: args)
|> Some
| Naming.StartsWith "emitPy" rest, [ args; macro ] ->
match macro with
| RequireStringConstOrTemplate com ctx r template ->
let args = destructureTupleArgs [ args ]
let isStatement = rest = "Statement"
emitTemplate r t args isStatement template |> Some
| "op_EqualsEqualsGreater", [ name; MaybeLambdaUncurriedAtCompileTime value ] ->
makeTuple r false [ name; value ] |> Some
| "createObj", _ -> Helper.LibCall(com, "util", "createObj", Any, args) |> withTag "pojo" |> Some
| "keyValueList", [ caseRule; keyValueList ] ->
// makePojo com ctx caseRule keyValueList
let args = [ keyValueList; caseRule ]
Helper.LibCall(com, "map_util", "keyValueList", Any, args)
|> withTag "pojo"
|> Some
| "createEmpty", _ -> typedObjExpr t [] |> Some
| _ -> None
| _ -> None
let refCells (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr option) (args: Expr list) =
match i.CompiledName, thisArg, args with
| "get_Value", Some callee, _ -> getRefCell com r t callee |> Some
| "set_Value", Some callee, [ value ] -> setRefCell com r callee value |> Some
| _ -> None
let getMangledNames (i: CallInfo) (thisArg: Expr option) =
let isStatic = Option.isNone thisArg
let pos = i.DeclaringEntityFullName.LastIndexOf('.')
let moduleName =
i.DeclaringEntityFullName.Substring(0, pos).Replace("Microsoft.", "")
let entityName =
i.DeclaringEntityFullName.Substring(pos + 1) |> Naming.cleanNameAsPyIdentifier