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] Emit type #2899

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/Fable.Transforms/Dart/Dart.fs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Type =

| Generic of name: string
| TypeReference of Ident * generics: Type list
| EmitType of value: string * generics: Type list
| Function of argTypes: Type list * returnType: Type

type Ident =
Expand Down
29 changes: 29 additions & 0 deletions src/Fable.Transforms/Dart/DartPrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,33 @@ module PrinterExtensions =
printer.Print(s)
printSeparator |> Option.iter (fun f -> f printer)

member printer.PrintEmitType(value: string, genArgs: Type list) =
let printSegment (printer: Printer) (value: string) segmentStart segmentEnd =
let segmentLength = segmentEnd - segmentStart
if segmentLength > 0 then
let segment = value.Substring(segmentStart, segmentLength)
printer.Print(segment)

let matches = Regex.Matches(value, @"\$\d+")
if matches.Count > 0 then
for i = 0 to matches.Count - 1 do
let m = matches[i]
let segmentStart =
if i > 0 then matches[i-1].Index + matches[i-1].Length
else 0

printSegment printer value segmentStart m.Index

let argIndex = int m.Value[1..]
match List.tryItem argIndex genArgs with
| Some t -> printer.PrintType(t)
| None -> ()

let lastMatch = matches[matches.Count - 1]
printSegment printer value (lastMatch.Index + lastMatch.Length) value.Length
else
printSegment printer value 0 value.Length

// TODO: Most of this code matches BabelPrinter.PrintEmitExpression, can we refactor it?
member printer.PrintEmitExpression(value: string, args: Expression list) =
let inline replace pattern (f: Match -> string) input =
Expand Down Expand Up @@ -233,6 +260,8 @@ module PrinterExtensions =
| TypeReference(ref, gen) ->
printer.PrintIdent(ref)
printer.PrintList("<", ", ", ">", gen, printer.PrintType, skipIfEmpty=true)
| EmitType(macro, gen) ->
printer.PrintEmitType(macro, gen)
| Function(argTypes, returnType) ->
printer.PrintType(returnType)
printer.Print(" ")
Expand Down
25 changes: 18 additions & 7 deletions src/Fable.Transforms/Dart/Fable2Dart.fs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ module Util =
let tup = List.length genArgs |> getTupleTypeIdent com ctx
TypeReference(tup, transformGenArgs com ctx genArgs)

let (|EmitAttribute|_|) (ent: Fable.Entity) =
ent.Attributes |> Seq.tryPick (fun att ->
if att.Entity.FullName.StartsWith(Atts.emit) then
match att.ConstructorArgs with
| [:? string as macro] -> Some macro
| _ -> None
else None)

let transformDeclaredTypeIgnoreMeasure ignoreMeasure (com: IDartCompiler) ctx (entRef: Fable.EntityRef) genArgs =
match entRef.FullName with
| "System.Enum" -> Integer |> Some
Expand All @@ -175,10 +183,11 @@ module Util =
// We use `dynamic` for now because there doesn't seem to be a type that catches all errors in Dart
| Naming.EndsWith "Exception" _ -> Dynamic |> Some
| _ ->
let ent = com.GetEntity(entRef)
if ignoreMeasure && ent.IsMeasure then
None
else
match com.GetEntity(entRef) with
| ent when ignoreMeasure && ent.IsMeasure -> None
| EmitAttribute(macro) ->
EmitType(macro, transformGenArgs com ctx genArgs) |> Some
| ent ->
let genArgs = transformGenArgs com ctx genArgs
TypeReference(getEntityIdent com ctx ent, genArgs) |> Some

Expand Down Expand Up @@ -2121,10 +2130,12 @@ module Util =

| Fable.ClassDeclaration decl ->
let entRef = decl.Entity
let ent = com.GetEntity(entRef)
if ent.IsInterface then
match com.GetEntity(entRef) with
// Ignore declaration for classes with Emit attribute
| EmitAttribute _ -> []
| ent when ent.IsInterface ->
transformInterfaceDeclaration com ctx decl ent
else
| ent ->
let instanceMethods =
decl.AttachedMembers |> List.choose (fun memb ->
match memb.Name with
Expand Down