|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Collections.Immutable; |
| 7 | +using System.Linq; |
| 8 | +using System.Text; |
| 9 | +using Microsoft.CodeAnalysis; |
| 10 | +using Microsoft.CodeAnalysis.CSharp; |
| 11 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 12 | +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; |
| 13 | + |
| 14 | + |
| 15 | +namespace JavaScript.MarshalerGenerator |
| 16 | +{ |
| 17 | + internal class CommonJSMethodGenerator |
| 18 | + { |
| 19 | + public StringBuilder prolog; |
| 20 | + public MethodDeclarationSyntax MethodSyntax; |
| 21 | + public TypeDeclarationSyntax TypeSyntax; |
| 22 | + public AttributeSyntax AttributeSyntax; |
| 23 | + public IMethodSymbol MethodSymbol; |
| 24 | + public IMethodSymbol AttributeSymbol; |
| 25 | + public AttributeData JSAttributeData; |
| 26 | + public JSMarshalerSig[] ParemeterSignatures; |
| 27 | + public JSMarshalerSig ReturnSignature; |
| 28 | + public MarshalerSelector MarshalerSelector; |
| 29 | + public string BoundFunctionName; |
| 30 | + public string AssemblyName; |
| 31 | + |
| 32 | + public ITypeSymbol ReturnType => MethodSymbol.ReturnType; |
| 33 | + public TypeSyntax ReturnTypeSyntax => ReturnType.AsTypeSyntax(); |
| 34 | + public string MethodName => MethodSymbol.Name; |
| 35 | + public bool HasCustomMarshalers => JSAttributeData.ConstructorArguments.Length > 1; |
| 36 | + public bool IsVoidMethod => ReturnType.SpecialType == SpecialType.System_Void; |
| 37 | + |
| 38 | + public void SelectMarshalers(Compilation compilation) |
| 39 | + { |
| 40 | + JSMarshalerMetadata[] customMarshalers = null; |
| 41 | + if (HasCustomMarshalers) |
| 42 | + { |
| 43 | + ImmutableArray<TypedConstant> marshalerTypes = JSAttributeData.ConstructorArguments[1].Values; |
| 44 | + customMarshalers = marshalerTypes.Select(mt => ExtractMarshalerMeta(compilation, mt)).ToArray(); |
| 45 | + } |
| 46 | + |
| 47 | + MarshalerSelector = new MarshalerSelector(compilation); |
| 48 | + ReturnSignature = MarshalerSelector.GetArgumentSignature(prolog, customMarshalers, MethodSymbol.ReturnType); |
| 49 | + for (int i = 0; i < MethodSymbol.Parameters.Length; i++) |
| 50 | + { |
| 51 | + IParameterSymbol arg = MethodSymbol.Parameters[i]; |
| 52 | + ParemeterSignatures[i] = MarshalerSelector.GetArgumentSignature(prolog, customMarshalers, arg.Type); |
| 53 | + } |
| 54 | + AssemblyName = compilation.AssemblyName; |
| 55 | + } |
| 56 | + |
| 57 | + protected ArgumentSyntax CreateMarshallersSyntax() |
| 58 | + { |
| 59 | + ArgumentSyntax marshallersArg; |
| 60 | + List<ITypeSymbol> marshalersTypes = HasCustomMarshalers |
| 61 | + ? JSAttributeData.ConstructorArguments[1].Values.Select(a => (ITypeSymbol)a.Value).ToList() |
| 62 | + : new List<ITypeSymbol?>(); |
| 63 | + |
| 64 | + if (ReturnSignature.IsAuto) |
| 65 | + { |
| 66 | + marshalersTypes.Add(ReturnSignature.MarshalerType); |
| 67 | + } |
| 68 | + marshalersTypes.AddRange(ParemeterSignatures.Where(s => s.IsAuto).Select(s => s.MarshalerType)); |
| 69 | + |
| 70 | + if (marshalersTypes.Count > 0) |
| 71 | + { |
| 72 | + var marshalerInstances = marshalersTypes.Distinct(SymbolEqualityComparer.Default).Cast<ITypeSymbol>().Select(t => |
| 73 | + { |
| 74 | + return ObjectCreationExpression(t.AsTypeSyntax()).WithArgumentList(ArgumentList()); |
| 75 | + }); |
| 76 | + marshallersArg = Argument(ImplicitArrayCreationExpression(InitializerExpression(SyntaxKind.ArrayInitializerExpression, SeparatedList<ExpressionSyntax>(marshalerInstances)))); |
| 77 | + } |
| 78 | + else |
| 79 | + { |
| 80 | + marshallersArg = Argument(LiteralExpression(SyntaxKind.NullLiteralExpression)); |
| 81 | + } |
| 82 | + |
| 83 | + return marshallersArg; |
| 84 | + } |
| 85 | + |
| 86 | + protected static TypeDeclarationSyntax CreateTypeDeclarationWithoutTrivia(TypeDeclarationSyntax typeDeclaration) |
| 87 | + { |
| 88 | + var mods = AddToModifiers(StripTriviaFromModifiers(typeDeclaration.Modifiers), SyntaxKind.UnsafeKeyword); |
| 89 | + return TypeDeclaration(typeDeclaration.Kind(), typeDeclaration.Identifier) |
| 90 | + .WithModifiers(mods); |
| 91 | + } |
| 92 | + |
| 93 | + protected static SyntaxTokenList AddToModifiers(SyntaxTokenList modifiers, SyntaxKind modifierToAdd) |
| 94 | + { |
| 95 | + if (modifiers.IndexOf(modifierToAdd) >= 0) |
| 96 | + return modifiers; |
| 97 | + |
| 98 | + int idx = modifiers.IndexOf(SyntaxKind.PartialKeyword); |
| 99 | + return idx >= 0 |
| 100 | + ? modifiers.Insert(idx, Token(modifierToAdd)) |
| 101 | + : modifiers.Add(Token(modifierToAdd)); |
| 102 | + } |
| 103 | + |
| 104 | + protected static SyntaxTokenList StripTriviaFromModifiers(SyntaxTokenList tokenList) |
| 105 | + { |
| 106 | + SyntaxToken[] strippedTokens = new SyntaxToken[tokenList.Count]; |
| 107 | + for (int i = 0; i < tokenList.Count; i++) |
| 108 | + { |
| 109 | + strippedTokens[i] = tokenList[i].WithoutTrivia(); |
| 110 | + } |
| 111 | + return new SyntaxTokenList(strippedTokens); |
| 112 | + } |
| 113 | + |
| 114 | + protected JSMarshalerMetadata ExtractMarshalerMeta(Compilation compilation, TypedConstant mt) |
| 115 | + { |
| 116 | + try |
| 117 | + { |
| 118 | + INamedTypeSymbol? marshalerType = compilation.GetTypeByMetadataName(mt.Value.ToString()); |
| 119 | + ITypeSymbol marshaledType = marshalerType.BaseType.TypeArguments[0]; |
| 120 | + |
| 121 | + var hasAfterJs = marshalerType.GetMembers("AfterToJavaScript").Length > 0; |
| 122 | + |
| 123 | + return new JSMarshalerMetadata |
| 124 | + { |
| 125 | + MarshalerType = marshalerType, |
| 126 | + MarshaledType = marshaledType, |
| 127 | + ToManagedMethod = "MarshalToManaged", |
| 128 | + ToJsMethod = "MarshalToJs", |
| 129 | + AfterToJsMethod = hasAfterJs ? "AfterMarshalToJs" : null, |
| 130 | + }; |
| 131 | + } |
| 132 | + catch (Exception ex) |
| 133 | + { |
| 134 | + prolog.AppendLine($"Failed when processing {mt.Value} \n" + ex.Message); |
| 135 | + return null; |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments