Пример #1
0
        private static void GenerateAssemblyInitializer(Context context, OutputBuffer output, string directory)
        {
            var assemblyName = Path.GetFileName(directory.TrimEnd(Path.DirectorySeparatorChar));

            output.AppendLine($"namespace {assemblyName}");
            output.AppendLine("{");

            output.IncreaseIndent();
            output.AppendLine("public static class WasmWranglerAssemblyInitializer");
            output.AppendLine("{");

            output.IncreaseIndent();
            output.AppendLine("public static void Initialize()");
            output.AppendLine("{");

            output.IncreaseIndent();
            context.Wrappers.Sort();

            foreach (var wrapper in context.Wrappers)
            {
                output.AppendLine($"{wrapper}.Initialize();");
            }

            output.DecreaseIndent();

            output.AppendLine("}");
            output.DecreaseIndent();

            output.AppendLine("}");
            output.DecreaseIndent();

            output.AppendLine("}");
            output.AppendLine();
        }
Пример #2
0
        private static void GenerateGlobal(Context context, OutputBuffer output, ClassDeclarationSyntax @class)
        {
            output.AppendLine($"public static partial class {@class.Identifier}");
            output.AppendLine("{");
            output.AppendLine("\tprivate static JSObject? __js;");
            output.AppendLine();
            output.AppendLine("\tprivate static JSObject _js");
            output.AppendLine("\t{");
            output.AppendLine("\t\tget");
            output.AppendLine("\t\t{");
            output.AppendLine("\t\t\tif (__js == null)");
            output.AppendLine($"\t\t\t\t__js = (JSObject)Runtime.GetGlobalObject(nameof({@class.Identifier}));");
            output.AppendLine();
            output.AppendLine("\t\t\treturn __js;");
            output.AppendLine("\t\t}");
            output.AppendLine("\t}");
            output.AppendLine();

            output.IncreaseIndent();

            foreach (var member in @class.Members)
            {
                GenerateInterfaceMember(context, output, member, true);
            }

            output.DecreaseIndent();

            output.AppendLine("}");
            output.AppendLine();
        }
Пример #3
0
        private static void GenerateSyntaxNodes(Context context, OutputBuffer output, IEnumerable <SyntaxNode> nodes)
        {
            foreach (var node in nodes)
            {
                switch (node)
                {
                case ClassDeclarationSyntax classDeclarationSyntax:
                    GenerateClass(context, output, classDeclarationSyntax);
                    break;

                case NamespaceDeclarationSyntax namespaceDeclarationSyntax:
                    output.AppendLine();
                    output.AppendLine($"namespace {namespaceDeclarationSyntax.Name}");
                    output.AppendLine("{");
                    output.IncreaseIndent();

                    context.CurrentNamespace = namespaceDeclarationSyntax.Name.ToString();

                    GenerateSyntaxNodes(context, output, namespaceDeclarationSyntax.Members);

                    context.CurrentNamespace = "";

                    output.DecreaseIndent();
                    output.AppendLine("}");
                    break;

                //case InterfaceDeclarationSyntax interfaceDeclarationSyntax:
                //    GenerateInterface(context, output, interfaceDeclarationSyntax);
                //    break;

                case UsingDirectiveSyntax usingDirectiveSyntax:
                    output.AppendLine(usingDirectiveSyntax.ToString());
                    break;

                default:
                    throw new InvalidOperationException(CreateErrorMessage(node, $"{node.Kind()} was not expected."));
                }
            }
        }
Пример #4
0
        private static void GenerateWrapper(Context context, OutputBuffer output, ClassDeclarationSyntax @class, List <string> implements)
        {
            context.Wrappers.Add(context.CurrentNamespace + "." + @class.Identifier.ToString());

            output.Append($"public partial class {@class.Identifier}");

            if (@class.BaseList != null || implements.Any())
            {
                output.Append(" : ");

                if (@class.BaseList != null)
                {
                    output.Append(string.Join(", ", @class.BaseList.Types.Select(x => x.ToString())));
                }

                if (implements.Any())
                {
                    // If we already output the BaseList we need to add a ,
                    if (@class.BaseList != null)
                    {
                        output.Append(", ");
                    }

                    output.Append(string.Join(", ", implements));
                }
            }

            output.AppendLine();
            output.AppendLine("{");

            output.Append("\tinternal static ");

            if (@class.BaseList != null)
            {
                output.Append("new ");
            }

            output.AppendLine($"void Initialize() {{ JSObjectWrapperFactory.RegisterFactory(typeof({@class.Identifier}), x => new {@class.Identifier}(x)); }}");
            output.AppendLine();

            if (@class.BaseList == null)
            {
                output.AppendLine("\tprotected readonly JSObject _js;");
                output.AppendLine();
                output.AppendLine($"\tinternal {@class.Identifier}(object obj)");
                output.AppendLine("\t{");
                output.AppendLine("\t\tif (!(obj is JSObject))");
                output.AppendLine("\t\t\tthrow new WasmWranglerException($\"Expected {nameof(obj)} to be an instance of JSObject.\");");
                output.AppendLine();
                output.AppendLine("\t\t_js = (JSObject)obj;");
                output.AppendLine("\t}");
            }
            else
            {
                output.AppendLine($"\tinternal {@class.Identifier}(object obj) : base(obj) {{ }}");
            }

            output.AppendLine();
            output.IncreaseIndent();

            foreach (var member in @class.Members)
            {
                GenerateInterfaceMember(context, output, member, false);
            }

            output.DecreaseIndent();

            output.AppendLine("}");
            output.AppendLine();
        }