Skip to content

Commit bf9984a

Browse files
committed
Remove conversion for variants.
1 parent 6681a16 commit bf9984a

16 files changed

+69
-589
lines changed

jscomp/gentype/Converter.ml

+15-206
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ type t =
88
| OptionC of t
99
| PromiseC of t
1010
| TupleC of t list
11-
| VariantC of variantC
1211

1312
and groupedArgConverter =
1413
| ArgConverter of t
@@ -23,17 +22,6 @@ and functionC = {
2322
uncurried: bool;
2423
}
2524

26-
and variantC = {
27-
hash: int;
28-
noPayloads: case list;
29-
withPayloads: withPayload list;
30-
polymorphic: bool;
31-
unboxed: bool;
32-
useVariantTables: bool;
33-
}
34-
35-
and withPayload = {case: case; inlineRecord: bool; argConverters: t list}
36-
3725
let rec toString converter =
3826
match converter with
3927
| ArrayC c -> "array(" ^ toString c ^ ")"
@@ -66,20 +54,6 @@ let rec toString converter =
6654
| PromiseC c -> "promise(" ^ toString c ^ ")"
6755
| TupleC innerTypesC ->
6856
"[" ^ (innerTypesC |> List.map toString |> String.concat ", ") ^ "]"
69-
| VariantC {noPayloads; withPayloads} ->
70-
"variant("
71-
^ ((noPayloads |> List.map labelJSToString)
72-
@ (withPayloads
73-
|> List.map (fun {case; inlineRecord; argConverters} ->
74-
(case |> labelJSToString)
75-
^ (match inlineRecord with
76-
| true -> " inlineRecord "
77-
| false -> "")
78-
^ ":" ^ "{"
79-
^ (argConverters |> List.map toString |> String.concat ", ")
80-
^ "}"))
81-
|> String.concat ", ")
82-
^ ")"
8357

8458
let typeGetConverterNormalized ~config ~inline ~lookupId ~typeNameIsInterface
8559
type0 =
@@ -186,77 +160,33 @@ let typeGetConverterNormalized ~config ~inline ~lookupId ~typeNameIsInterface
186160
(TupleC innerConversions, Tuple normalizedList)
187161
| TypeVar _ -> (IdentC, normalized_)
188162
| Variant variant ->
189-
let allowUnboxed = not variant.polymorphic in
190-
let withPayloads, normalized, unboxed =
191-
match
192-
variant.payloads
193-
|> List.map (fun {case; inlineRecord; numArgs; t} ->
194-
(case, inlineRecord, numArgs, t |> visit ~visited))
195-
with
196-
| [] when allowUnboxed -> ([], normalized_, variant.unboxed)
197-
| [(case, inlineRecord, numArgs, (converter, tNormalized))]
198-
when allowUnboxed ->
199-
let unboxed = tNormalized |> expandOneLevel |> typeIsObject in
163+
let ordinaryVariant = not variant.polymorphic in
164+
let withPayloadConverted =
165+
variant.payloads
166+
|> List.map (fun (payload : payload) ->
167+
{payload with t = snd (payload.t |> visit ~visited)})
168+
in
169+
let normalized =
170+
match withPayloadConverted with
171+
| [] when ordinaryVariant -> normalized_
172+
| [payload] when ordinaryVariant ->
173+
let unboxed = payload.t |> expandOneLevel |> typeIsObject in
200174
let normalized =
201175
Variant
202176
{
203177
variant with
204-
payloads = [{case; inlineRecord; numArgs; t = tNormalized}];
178+
payloads = [payload];
205179
unboxed =
206180
(match unboxed with
207181
| true -> true
208182
| false -> variant.unboxed);
209183
}
210184
in
211-
let argConverters =
212-
match converter with
213-
| TupleC converters when numArgs > 1 -> converters
214-
| _ -> [converter]
215-
in
216-
([{argConverters; case; inlineRecord}], normalized, unboxed)
185+
normalized
217186
| withPayloadConverted ->
218-
let withPayloadNormalized =
219-
withPayloadConverted
220-
|> List.map (fun (case, inlineRecord, numArgs, (_, tNormalized)) ->
221-
{case; inlineRecord; numArgs; t = tNormalized})
222-
in
223-
let normalized =
224-
Variant {variant with payloads = withPayloadNormalized}
225-
in
226-
( withPayloadConverted
227-
|> List.map (fun (case, inlineRecord, numArgs, (converter, _)) ->
228-
let argConverters =
229-
match converter with
230-
| TupleC converters when numArgs > 1 -> converters
231-
| _ -> [converter]
232-
in
233-
{argConverters; case; inlineRecord}),
234-
normalized,
235-
variant.unboxed )
236-
in
237-
let noPayloads = variant.noPayloads in
238-
let useVariantTables =
239-
if variant.bsStringOrInt then false
240-
else if variant.polymorphic then
241-
noPayloads
242-
|> List.exists (fun {label; labelJS} -> labelJS <> StringLabel label)
243-
|| withPayloads
244-
|> List.exists (fun {case = {label; labelJS}} ->
245-
labelJS <> StringLabel label)
246-
else true
247-
in
248-
let converter =
249-
VariantC
250-
{
251-
hash = variant.hash;
252-
noPayloads;
253-
withPayloads;
254-
polymorphic = variant.polymorphic;
255-
unboxed;
256-
useVariantTables;
257-
}
187+
Variant {variant with payloads = withPayloadConverted}
258188
in
259-
(converter, normalized)
189+
(IdentC, normalized)
260190
and argTypeToGroupedArgConverter ~visited {aName; aType} =
261191
match aType with
262192
| GroupOfLabeledArgs fields ->
@@ -322,13 +252,6 @@ let rec converterIsIdentity ~config ~toJS converter =
322252
| PromiseC c -> c |> converterIsIdentity ~config ~toJS
323253
| TupleC innerTypesC ->
324254
innerTypesC |> List.for_all (converterIsIdentity ~config ~toJS)
325-
| VariantC {withPayloads; useVariantTables} ->
326-
if not useVariantTables then
327-
withPayloads
328-
|> List.for_all (fun {argConverters} ->
329-
argConverters
330-
|> List.for_all (fun c -> c |> converterIsIdentity ~config ~toJS))
331-
else false
332255

333256
let rec apply ~config ~converter ~indent ~nameGen ~toJS ~variantTables value =
334257
match converter with
@@ -470,120 +393,6 @@ let rec apply ~config ~converter ~indent ~nameGen ~toJS ~variantTables value =
470393
|> apply ~config ~converter:c ~indent ~nameGen ~toJS ~variantTables)
471394
|> String.concat ", ")
472395
^ "]"
473-
| VariantC {noPayloads = [case]; withPayloads = []; polymorphic} -> (
474-
match toJS with
475-
| true -> case |> labelJSToString
476-
| false -> case.label |> Runtime.emitVariantLabel ~polymorphic)
477-
| VariantC variantC -> (
478-
if variantC.noPayloads <> [] && variantC.useVariantTables then
479-
Hashtbl.replace variantTables (variantC.hash, toJS) variantC;
480-
let convertToString =
481-
match
482-
(not toJS)
483-
&& variantC.noPayloads
484-
|> List.exists (fun {labelJS} ->
485-
labelJS = BoolLabel true || labelJS = BoolLabel false)
486-
with
487-
| true -> ".toString()"
488-
| false -> ""
489-
in
490-
let table = variantC.hash |> variantTable ~toJS in
491-
let accessTable v =
492-
match not variantC.useVariantTables with
493-
| true -> v
494-
| false -> table ^ EmitText.array [v ^ convertToString]
495-
in
496-
let convertVariantPayloadToJS ~indent ~argConverters x =
497-
match argConverters with
498-
| [converter] ->
499-
x |> apply ~config ~converter ~indent ~nameGen ~toJS ~variantTables
500-
| _ ->
501-
argConverters
502-
|> List.mapi (fun i converter ->
503-
x
504-
|> Runtime.accessVariant ~index:i
505-
|> apply ~config ~converter ~indent ~nameGen ~toJS ~variantTables)
506-
|> EmitText.array
507-
in
508-
let convertVariantPayloadToRE ~indent ~argConverters x =
509-
match argConverters with
510-
| [converter] ->
511-
[x |> apply ~config ~converter ~indent ~nameGen ~toJS ~variantTables]
512-
| _ ->
513-
argConverters
514-
|> List.mapi (fun i converter ->
515-
x
516-
|> EmitText.arrayAccess ~index:i
517-
|> apply ~config ~converter ~indent ~nameGen ~toJS ~variantTables)
518-
in
519-
match variantC.withPayloads with
520-
| [] -> value |> accessTable
521-
| [{case; inlineRecord; argConverters}] when variantC.unboxed -> (
522-
let casesWithPayload ~indent =
523-
if toJS then
524-
value
525-
|> Runtime.emitVariantGetPayload ~inlineRecord
526-
~numArgs:(argConverters |> List.length)
527-
~polymorphic:variantC.polymorphic
528-
|> convertVariantPayloadToJS ~argConverters ~indent
529-
else
530-
value
531-
|> convertVariantPayloadToRE ~argConverters ~indent
532-
|> Runtime.emitVariantWithPayload ~inlineRecord ~label:case.label
533-
~polymorphic:variantC.polymorphic
534-
in
535-
match variantC.noPayloads = [] with
536-
| true -> casesWithPayload ~indent
537-
| false ->
538-
EmitText.ifThenElse ~indent
539-
(fun ~indent:_ -> value |> EmitText.typeOfObject)
540-
casesWithPayload
541-
(fun ~indent:_ -> value |> accessTable))
542-
| _ :: _ -> (
543-
let convertCaseWithPayload ~indent ~inlineRecord ~argConverters case =
544-
if toJS then
545-
value
546-
|> Runtime.emitVariantGetPayload ~inlineRecord
547-
~numArgs:(argConverters |> List.length)
548-
~polymorphic:variantC.polymorphic
549-
|> convertVariantPayloadToJS ~argConverters ~indent
550-
|> Runtime.emitJSVariantWithPayload ~label:(case |> labelJSToString)
551-
~polymorphic:variantC.polymorphic
552-
else
553-
value
554-
|> Runtime.emitJSVariantGetPayload ~polymorphic:variantC.polymorphic
555-
|> convertVariantPayloadToRE ~argConverters ~indent
556-
|> Runtime.emitVariantWithPayload ~inlineRecord ~label:case.label
557-
~polymorphic:variantC.polymorphic
558-
in
559-
let switchCases ~indent =
560-
variantC.withPayloads
561-
|> List.map (fun {case; inlineRecord; argConverters} ->
562-
( (match toJS with
563-
| true ->
564-
case.label
565-
|> Runtime.emitVariantLabel ~polymorphic:variantC.polymorphic
566-
| false -> case |> labelJSToString),
567-
case
568-
|> convertCaseWithPayload ~indent ~inlineRecord ~argConverters
569-
))
570-
in
571-
let casesWithPayload ~indent =
572-
value
573-
|> (let open Runtime in
574-
(match toJS with
575-
| true -> emitVariantGetLabel
576-
| false -> emitJSVariantGetLabel)
577-
~polymorphic:variantC.polymorphic)
578-
|> EmitText.switch ~indent ~cases:(switchCases ~indent)
579-
in
580-
match variantC.noPayloads = [] with
581-
| true -> casesWithPayload ~indent
582-
| false ->
583-
EmitText.ifThenElse ~indent
584-
(fun ~indent:_ -> value |> EmitText.typeOfObject)
585-
casesWithPayload
586-
(fun ~indent:_ -> value |> accessTable)))
587396

588397
let toJS ~config ~converter ~indent ~nameGen ~variantTables value =
589398
value |> apply ~config ~converter ~indent ~nameGen ~variantTables ~toJS:true

jscomp/gentype/EmitJs.ml

-35
Original file line numberDiff line numberDiff line change
@@ -433,40 +433,6 @@ let emitRequires ~importedValueOrComponent ~early ~config ~requires emitters =
433433
~moduleName)
434434
requires emitters
435435

436-
let emitVariantTables ~emitters variantTables =
437-
let typeAnnotation = ": { [key: string]: any }" in
438-
let emitTable ~table ~toJS (variantC : Converter.variantC) =
439-
"const " ^ table ^ typeAnnotation ^ " = {"
440-
^ (variantC.noPayloads
441-
|> List.map (fun case ->
442-
let js = case |> labelJSToString ~alwaysQuotes:(not toJS) in
443-
let re =
444-
case.label
445-
|> Runtime.emitVariantLabel ~polymorphic:variantC.polymorphic
446-
in
447-
match toJS with
448-
| true -> (re |> EmitText.quotesIfRequired) ^ ": " ^ js
449-
| false -> js ^ ": " ^ re)
450-
|> String.concat ", ")
451-
^ "};"
452-
in
453-
Hashtbl.fold
454-
(fun (_, toJS) variantC l -> (variantC, toJS) :: l)
455-
variantTables []
456-
|> List.sort (fun (variantC1, toJS1) (variantC2, toJS2) ->
457-
let n = compare variantC1.Converter.hash variantC2.hash in
458-
match n <> 0 with
459-
| true -> n
460-
| false -> compare toJS2 toJS1)
461-
|> List.fold_left
462-
(fun emitters (variantC, toJS) ->
463-
variantC
464-
|> emitTable
465-
~table:(variantC.Converter.hash |> variantTable ~toJS)
466-
~toJS
467-
|> Emitters.requireEarly ~emitters)
468-
emitters
469-
470436
let typeGetInlined ~config ~exportTypeMap type_ =
471437
type_
472438
|> Converter.typeGetNormalized ~config ~inline:true
@@ -762,7 +728,6 @@ let emitTranslationAsString ~config ~fileName ~inputCmtTranslateTypeDeclarations
762728
| false -> env
763729
in
764730
let finalEnv = env in
765-
let emitters = variantTables |> emitVariantTables ~emitters in
766731
let emitters =
767732
moduleItemsEmitter
768733
|> ExportModule.emitAllModuleItems ~config ~emitters ~fileName

jscomp/gentype/EmitText.ml

-21
Original file line numberDiff line numberDiff line change
@@ -40,32 +40,11 @@ let funDef ~bodyArgs ~functionName ~funParams ~indent ~mkBody ~typeVars =
4040
^ genericsString ~typeVars ^ (funParams |> parens) ^ " {"
4141
^ (bodyArgs |> mkBody) ^ Indent.break ~indent ^ "}"
4242

43-
let ifThenElse ~indent if_ then_ else_ =
44-
let indent1 = indent |> Indent.more in
45-
if_ ~indent:indent1 ^ Indent.break ~indent ^ "? " ^ then_ ~indent:indent1
46-
^ Indent.break ~indent ^ ": " ^ else_ ~indent:indent1
47-
4843
let newNameGen () = Hashtbl.create 1
4944
let quotes x = "\"" ^ x ^ "\""
5045

51-
let quotesIfRequired x =
52-
match String.length x > 0 && (x.[0] [@doesNotRaise]) = '"' with
53-
| true -> x
54-
| false -> quotes x
55-
5646
let resultName ~nameGen = "result" |> name ~nameGen
5747

58-
let switch ~indent ~cases expr =
59-
let lastCase = (cases |> List.length) - 1 in
60-
cases
61-
|> List.mapi (fun i (label, code) ->
62-
if i = lastCase then code
63-
else
64-
expr ^ "===" ^ label ^ Indent.break ~indent ^ "? " ^ code
65-
^ Indent.break ~indent ^ ": ")
66-
|> String.concat ""
67-
68-
let typeOfObject x = "typeof(" ^ x ^ ")" ^ " === " ^ "'object'"
6948
let addComment ~comment x = "\n/* " ^ comment ^ " */\n " ^ x
7049
let arrayAccess ~index value = value ^ "[" ^ string_of_int index ^ "]"
7150
let fieldAccess ~label value = value ^ "." ^ label

0 commit comments

Comments
 (0)