Skip to content

Commit e385504

Browse files
committed
Added FieldIndexSet
1 parent 52922f1 commit e385504

File tree

8 files changed

+62
-40
lines changed

8 files changed

+62
-40
lines changed

.vscode/launch.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
"name": "Run bench-compiler (.NET)",
6666
"program": "${workspaceFolder}/src/fable-standalone/test/bench-compiler/bin/Debug/netcoreapp3.1/bench-compiler.dll",
6767
// "args": ["${workspaceRoot}/tests/Main/Fable.Tests.fsproj", "out-tests"],
68-
"args": ["${workspaceRoot}/../fable-test/fable-test.fsproj", "out-test", "--typescript"],
68+
"args": ["${workspaceRoot}/../fable-test/fable-test.fsproj", "out-test", "--eraseUnions"],
6969
// "args": ["${workspaceRoot}/src/fable-library/Fable.Library.fsproj", "out-lib", "--typescript"],
7070
"cwd": "${workspaceFolder}/src/fable-standalone/test/bench-compiler"
7171
},

src/Fable.AST/Fable.fs

+7-2
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,11 @@ type GetKind =
273273
| ListTail
274274
| OptionValue
275275

276+
type SetKind =
277+
| ByKeySet of KeyKind
278+
| FieldIndexSet of string * int
279+
| ValueSet
280+
276281
type TestKind =
277282
| TypeTest of Type
278283
| OptionTest of isSome: bool
@@ -312,8 +317,8 @@ type Expr =
312317
// Getters, setters and bindings
313318
| Let of Ident * Expr * body: Expr
314319
| LetRec of bindings: (Ident * Expr) list * body: Expr
315-
| Get of Expr * GetKind * typ: Type * range: SourceLocation option
316-
| Set of Expr * key: KeyKind option * value: Expr * range: SourceLocation option
320+
| Get of Expr * kind: GetKind * typ: Type * range: SourceLocation option
321+
| Set of Expr * kind: SetKind * value: Expr * range: SourceLocation option
317322

318323
// Control flow
319324
| Sequential of Expr list

src/Fable.Transforms/FSharp2Fable.Util.fs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1327,7 +1327,7 @@ module Util =
13271327
let t = memb.CurriedParameterGroups.[0].[0].Type |> makeType Map.empty
13281328
let arg = callInfo.Args |> List.tryHead |> Option.defaultWith makeNull
13291329
let key = makeFieldKey name true t
1330-
Fable.Set(callee, Some key, arg, r)
1330+
Fable.Set(callee, Fable.ByKeySet key, arg, r)
13311331
else
13321332
getSimple callee name |> makeCall r typ callInfo
13331333

src/Fable.Transforms/FSharp2Fable.fs

+22-11
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,11 @@ let private transformNewUnion com ctx r fsType (unionCase: FSharpUnionCase) (arg
5353
match com, fsType, unionCase with
5454
| ErasedUnion(kind, tdef, _genArgs) ->
5555
match kind, argExprs with
56-
| EraseKind.AsNamedTuple caseRule, [] -> transformStringEnum caseRule unionCase
57-
| EraseKind.AsNamedTuple _, _ -> (makeStrConst unionCase.Name)::argExprs |> Fable.NewTuple |> makeValue r
56+
// | EraseKind.AsNamedTuple caseRule, [] -> transformStringEnum caseRule unionCase
57+
| EraseKind.AsNamedTuple _, _ ->
58+
let caseTag = unionCaseTag tdef unionCase |> makeIntConst
59+
let caseName = makeStrConst unionCase.CompiledName
60+
caseTag::caseName::argExprs |> Fable.NewTuple |> makeValue r
5861
| EraseKind.AsValue, [arg] -> arg
5962
| EraseKind.AsValue, _ -> failwith "Shouldn't happen, error?"
6063
| EraseKind.AsTuple, _ -> Fable.NewTuple argExprs |> makeValue r
@@ -224,11 +227,14 @@ let private transformUnionCaseTest (com: IFableCompiler) (ctx: Context) r
224227
| ErasedUnion(kind, tdef, genArgs) ->
225228
match kind with
226229
| EraseKind.AsNamedTuple caseRule ->
227-
if unionCase.UnionCaseFields.Count = 0 then
228-
return makeEqOp r unionExpr (transformStringEnum caseRule unionCase) BinaryEqualStrict
229-
else
230-
let name = Fable.Get(unionExpr, Fable.TupleIndex(0), Fable.String, None)
231-
return makeEqOp r name (makeStrConst unionCase.Name) BinaryEqualStrict
230+
let tag1 = Fable.Get(unionExpr, Fable.TupleIndex(0), Fable.Number Int32, None)
231+
let tag2 = unionCaseTag tdef unionCase |> makeIntConst
232+
return makeEqOp r tag1 tag2 BinaryEqualStrict
233+
// if unionCase.UnionCaseFields.Count = 0 then
234+
// return makeEqOp r unionExpr (transformStringEnum caseRule unionCase) BinaryEqualStrict
235+
// else
236+
// let name = Fable.Get(unionExpr, Fable.TupleIndex(0), Fable.String, None)
237+
// return makeEqOp r name (makeStrConst unionCase.Name) BinaryEqualStrict
232238
| EraseKind.AsValue ->
233239
let fi = unionCase.UnionCaseFields.[0]
234240
let typ =
@@ -691,7 +697,7 @@ let private transformExpr (com: IFableCompiler) (ctx: Context) fsExpr =
691697
if unionCase.UnionCaseFields.Count = 0 then
692698
return "StringEnum types cannot have fields" |> addErrorAndReturnNull com ctx.InlinePath r
693699
else
694-
return getByIndex 1
700+
return getByIndex 2
695701
| OptionUnion t ->
696702
return Fable.Get(unionExpr, Fable.OptionValue, makeType ctx.GenericArgs t, r)
697703
| ListUnion t ->
@@ -711,14 +717,19 @@ let private transformExpr (com: IFableCompiler) (ctx: Context) fsExpr =
711717
return Fable.Get(unionExpr, kind, typ, r)
712718

713719
| BasicPatterns.FSharpFieldSet(callee, calleeType, field, value) ->
720+
let r = makeRangeFrom fsExpr
714721
let! callee = transformExprOpt com ctx callee
715722
let! value = transformExpr com ctx value
716723
let callee =
717724
match callee with
718725
| Some callee -> callee
719726
| None -> entityRef com (FsEnt calleeType.TypeDefinition)
720-
let field = FsField field :> Fable.Field |> Fable.FieldKey |> Some
721-
return Fable.Set(callee, field, value, makeRangeFrom fsExpr)
727+
if isErasedRecord com calleeType then
728+
let index = calleeType.TypeDefinition.FSharpFields |> Seq.findIndex (fun x -> x.Name = field.Name)
729+
return Fable.Set(callee, Fable.FieldIndexSet(field.Name, index + 1), value, r)
730+
else
731+
let key = FsField field :> Fable.Field |> Fable.FieldKey
732+
return Fable.Set(callee, Fable.ByKeySet key, value, r)
722733

723734
| BasicPatterns.UnionCaseTag(unionExpr, _unionType) ->
724735
let! unionExpr = transformExpr com ctx unionExpr
@@ -740,7 +751,7 @@ let private transformExpr (com: IFableCompiler) (ctx: Context) fsExpr =
740751
return makeCall r Fable.Unit info valToSet
741752
| _ ->
742753
let valToSet = makeValueFrom com ctx r valToSet
743-
return Fable.Set(valToSet, None, valueExpr, r)
754+
return Fable.Set(valToSet, Fable.ValueSet, valueExpr, r)
744755

745756
| BasicPatterns.NewArray(FableType com ctx elTyp, argExprs) ->
746757
let! argExprs = transformExprList com ctx argExprs

src/Fable.Transforms/Fable2Babel.fs

+9-7
Original file line numberDiff line numberDiff line change
@@ -1234,9 +1234,10 @@ module Util =
12341234
let value = com.TransformAsExpr(ctx, value) |> wrapIntExpression value.Type
12351235
let var =
12361236
match setKind with
1237-
| None -> var
1238-
| Some(Fable.FieldKey fi) -> get None var fi.Name
1239-
| Some(Fable.ExprKey(TransformExpr com ctx e)) -> getExpr None var e
1237+
| Fable.ValueSet -> var
1238+
| Fable.ByKeySet(Fable.FieldKey fi) -> get None var fi.Name
1239+
| Fable.ByKeySet(Fable.ExprKey(TransformExpr com ctx e)) -> getExpr None var e
1240+
| Fable.FieldIndexSet (_, index) -> getExpr None var (ofInt index)
12401241
assign range var value
12411242

12421243
let transformBindingExprBody (com: IBabelCompiler) ctx (var: Fable.Ident) (value: Fable.Expr) =
@@ -1641,10 +1642,11 @@ module Util =
16411642
| Fable.Set(TransformExpr com ctx expr, kind, value, _range) ->
16421643
let ret =
16431644
match kind with
1644-
| None -> Assign expr
1645-
| Some(Fable.ExprKey(TransformExpr com ctx prop)) -> getExpr None expr prop |> Assign
1646-
| Some(Fable.FieldKey fi) -> get None expr fi.Name |> Assign
1647-
com.TransformAsStatements(ctx, Some ret, value)
1645+
| Fable.ValueSet -> expr
1646+
| Fable.ByKeySet(Fable.ExprKey(TransformExpr com ctx prop)) -> getExpr None expr prop
1647+
| Fable.ByKeySet(Fable.FieldKey fi) -> get None expr fi.Name
1648+
| Fable.FieldIndexSet (_, index) -> getExpr None expr (ofInt index)
1649+
com.TransformAsStatements(ctx, Some (Assign ret), value)
16481650

16491651
| Fable.IfThenElse(guardExpr, thenExpr, elseExpr, r) ->
16501652
let asStatement =

src/Fable.Transforms/FableTransforms.fs

+7-7
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ let visit f e =
7171
IfThenElse(f cond, f thenExpr, f elseExpr, r)
7272
| Set(e, kind, v, r) ->
7373
match kind with
74-
| Some(ExprKey e2) ->
75-
Set(f e, Some(ExprKey(f e2)), f v, r)
76-
| Some(FieldKey _) | None -> Set(f e, kind, f v, r)
74+
| ByKeySet(ExprKey e2) ->
75+
Set(f e, ByKeySet(ExprKey(f e2)), f v, r)
76+
| _ -> Set(f e, kind, f v, r)
7777
| WhileLoop(e1, e2, r) -> WhileLoop(f e1, f e2, r)
7878
| ForLoop(i, e1, e2, e3, up, r) -> ForLoop(i, f e1, f e2, f e3, up, r)
7979
| TryCatch(body, catch, finalizer, r) ->
@@ -140,8 +140,8 @@ let getSubExpressions = function
140140
| IfThenElse(cond, thenExpr, elseExpr, _) -> [cond; thenExpr; elseExpr]
141141
| Set(e, kind, v, _) ->
142142
match kind with
143-
| Some(ExprKey e2) -> [e; e2; v]
144-
| Some(FieldKey _) | None -> [e; v]
143+
| ByKeySet(ExprKey e2) -> [e; e2; v]
144+
| _ -> [e; v]
145145
| WhileLoop(e1, e2, _) -> [e1; e2]
146146
| ForLoop(_, e1, e2, e3, _, _) -> [e1; e2; e3]
147147
| TryCatch(body, catch, finalizer, _) ->
@@ -558,9 +558,9 @@ module private Transforms =
558558
let uci = com.GetEntity(ent).UnionCases.[tag]
559559
let args = uncurryConsArgs args uci.UnionCaseFields
560560
Value(NewUnion(args, tag, ent, genArgs), r)
561-
| Set(e, Some(FieldKey fi), value, r) ->
561+
| Set(e, ByKeySet(FieldKey fi), value, r) ->
562562
let value = uncurryArgs com false [fi.FieldType] [value]
563-
Set(e, Some(FieldKey fi), List.head value, r)
563+
Set(e, ByKeySet(FieldKey fi), List.head value, r)
564564
| e -> e
565565

566566
let rec uncurryApplications (com: Compiler) e =

src/Fable.Transforms/Replacements.fs

+9-9
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ let makeRefFromMutableValue com ctx (value: Expr) =
384384
let getter = Delegate([], value, None)
385385
let setter =
386386
let v = makeUniqueIdent ctx Any "v"
387-
Delegate([v], Set(value, None, IdentExpr v, None), None)
387+
Delegate([v], Set(value, ValueSet, IdentExpr v, None), None)
388388
Helper.LibCall(com, "Types", "FSharpRef", t, [getter; setter], isJsConstructor=true)
389389

390390
let turnLastArgIntoRef com ctx args =
@@ -951,7 +951,7 @@ let makePojoFromLambda com arg =
951951
| Lambda(_, lambdaBody, _) ->
952952
(flattenSequential lambdaBody, Some []) ||> List.foldBack (fun statement acc ->
953953
match acc, statement with
954-
| Some acc, Set(_, Some(FieldKey fi), value, _) ->
954+
| Some acc, Set(_, ByKeySet(FieldKey fi), value, _) ->
955955
objValue (fi.Name, value)::acc |> Some
956956
| _ -> None)
957957
| _ -> None
@@ -1247,7 +1247,7 @@ let fableCoreLib (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Exp
12471247
| "op_Dynamic", [left; memb] ->
12481248
getExpr r t left memb |> Some
12491249
| "op_DynamicAssignment", [callee; prop; MaybeLambdaUncurriedAtCompileTime value] ->
1250-
Set(callee, Some(ExprKey prop), value, r) |> Some
1250+
Set(callee, ByKeySet(ExprKey prop), value, r) |> Some
12511251
| ("op_Dollar"|"createNew" as m), callee::args ->
12521252
let args = destructureTupleArgs args
12531253
if m = "createNew" then "new $0($1...)" else "$0($1...)"
@@ -1289,7 +1289,7 @@ let fableCoreLib (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Exp
12891289
| _ -> None
12901290

12911291
let getReference r t expr = get r t expr "contents"
1292-
let setReference r expr value = Set(expr, Some(ExprKey(makeStrConst "contents")), value, r)
1292+
let setReference r expr value = Set(expr, ByKeySet(ExprKey(makeStrConst "contents")), value, r)
12931293
let newReference com r t value = Helper.LibCall(com, "Types", "FSharpRef", t, [value], isJsConstructor=true, ?loc=r)
12941294

12951295
let references (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr option) (args: Expr list) =
@@ -1771,7 +1771,7 @@ let resizeArrays (com: ICompiler) (ctx: Context) r (t: Type) (i: CallInfo) (this
17711771
| ".ctor", _, args ->
17721772
Helper.GlobalCall("Array", t, args, memb="from", ?loc=r) |> Some
17731773
| "get_Item", Some ar, [idx] -> getExpr r t ar idx |> Some
1774-
| "set_Item", Some ar, [idx; value] -> Set(ar, Some(ExprKey idx), value, r) |> Some
1774+
| "set_Item", Some ar, [idx; value] -> Set(ar, ByKeySet(ExprKey idx), value, r) |> Some
17751775
| "Add", Some ar, [arg] ->
17761776
"void ($0)" |> emitJsExpr r t [Helper.InstanceCall(ar, "push", t, [arg])] |> Some
17771777
| "Remove", Some ar, [arg] ->
@@ -1859,7 +1859,7 @@ let arrays (com: ICompiler) (ctx: Context) r (t: Type) (i: CallInfo) (thisArg: E
18591859
match i.CompiledName, thisArg, args with
18601860
| "get_Length", Some arg, _ -> get r t arg "length" |> Some
18611861
| "get_Item", Some arg, [idx] -> getExpr r t arg idx |> Some
1862-
| "set_Item", Some arg, [idx; value] -> Set(arg, Some(ExprKey idx), value, r) |> Some
1862+
| "set_Item", Some arg, [idx; value] -> Set(arg, ByKeySet(ExprKey idx), value, r) |> Some
18631863
| "Copy", None, [source; target; count] ->
18641864
Helper.LibCall(com, "Array", "copyTo", t, [source; makeIntConst 0; target; makeIntConst 0; count], i.SignatureArgTypes, ?loc=r) |> Some
18651865
| "Copy", None, [source; sourceIndex; target; targetIndex; count] ->
@@ -1889,7 +1889,7 @@ let arrayModule (com: ICompiler) (ctx: Context) r (t: Type) (i: CallInfo) (_: Ex
18891889
| ("Length" | "Count"), [arg] -> get r t arg "length" |> Some
18901890
| "Item", [idx; ar] -> getExpr r t ar idx |> Some
18911891
| "Get", [ar; idx] -> getExpr r t ar idx |> Some
1892-
| "Set", [ar; idx; value] -> Set(ar, Some(ExprKey idx), value, r) |> Some
1892+
| "Set", [ar; idx; value] -> Set(ar, ByKeySet(ExprKey idx), value, r) |> Some
18931893
| "ZeroCreate", [count] -> createArray count None |> Some
18941894
| "Create", [count; value] -> createArray count (Some value) |> Some
18951895
| "Empty", _ ->
@@ -2273,7 +2273,7 @@ let intrinsicFunctions (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisAr
22732273
| "MakeDecimal", _, _ -> decimals com ctx r t i thisArg args
22742274
| "GetString", _, [ar; idx]
22752275
| "GetArray", _, [ar; idx] -> getExpr r t ar idx |> Some
2276-
| "SetArray", _, [ar; idx; value] -> Set(ar, Some(ExprKey idx), value, r) |> Some
2276+
| "SetArray", _, [ar; idx; value] -> Set(ar, ByKeySet(ExprKey idx), value, r) |> Some
22772277
| ("GetArraySlice" | "GetStringSlice"), None, [ar; lower; upper] ->
22782278
let upper =
22792279
match upper with
@@ -2645,7 +2645,7 @@ let timers (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr opti
26452645
match i.CompiledName, thisArg, args with
26462646
| ".ctor", _, _ -> Helper.LibCall(com, "Timer", "default", t, args, i.SignatureArgTypes, isJsConstructor=true, ?loc=r) |> Some
26472647
| Naming.StartsWith "get_" meth, Some x, _ -> get r t x meth |> Some
2648-
| Naming.StartsWith "set_" meth, Some x, [value] -> Set(x, Some(ExprKey(makeStrConst meth)), value, r) |> Some
2648+
| Naming.StartsWith "set_" meth, Some x, [value] -> Set(x, ByKeySet(ExprKey(makeStrConst meth)), value, r) |> Some
26492649
| meth, Some x, args -> Helper.InstanceCall(x, meth, t, args, i.SignatureArgTypes, ?loc=r) |> Some
26502650
| _ -> None
26512651

src/fable-standalone/test/bench-compiler/package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"private": true,
3-
"_type": "module",
3+
"type": "module",
44
"scripts": {
55
"build-cli": "dotnet run -c Release -p ../../../Fable.Cli -- bench-compiler.fsproj --outDir out-node",
66
"postbuild-cli": "npm run rollup-bundle",
@@ -44,9 +44,13 @@
4444
"build-tests-dotnet-ts": "dotnet run -c Release ../../../../tests/Main/Fable.Tests.fsproj out-tests --typescript",
4545
"build-tests-dotnet-opt": "dotnet run -c Release ../../../../tests/Main/Fable.Tests.fsproj out-tests --optimize",
4646
"build-tests-node": "node out-node/app.js ../../../../tests/Main/Fable.Tests.fsproj out-tests",
47-
"_pretests": "npm run build-tests-dotnet -- --eraseUnions",
4847
"tests": "npm run mocha -- out-tests -r esm --colors",
4948

49+
"prebuild-fable-library": "dotnet run -c Release ../../../fable-library/Fable.Library.fsproj ./out-lib --eraseUnions",
50+
"build-fable-library": "npm run tsc -- -p ../../../fable-library --outDir ./out-lib",
51+
"prebuild-tests": "git clean -fdx && npm run build-fable-library",
52+
"build-tests": "npm run build-tests-dotnet -- --eraseUnions",
53+
5054
"tsc": "node ../../../../node_modules/typescript/bin/tsc",
5155
"babel": "node ../../../../node_modules/@babel/cli/bin/babel",
5256
"mocha": "node ../../../../node_modules/mocha/bin/mocha",

0 commit comments

Comments
 (0)