public static void EmitInsideNamespace(this JavascriptFormatter formatter, TypeDefinition typedef, bool isTopLevel, Action <bool> inner)
        {
            var fullNamespace = DefinitelyTypedUtilities.GetFullNamespace(typedef);

            foreach (var part in fullNamespace)
            {
                if (isTopLevel)
                {
                    formatter.WriteRaw("export");
                    formatter.Space();
                    formatter.WriteRaw("declare");
                    formatter.Space();
                    isTopLevel = false;
                }

                formatter.WriteRaw("namespace");
                formatter.Space();
                formatter.Identifier(part);
                formatter.Space();
                formatter.OpenBrace();
            }

            inner(isTopLevel);

            foreach (var part in fullNamespace)
            {
                formatter.CloseBrace();
            }
        }
Esempio n. 2
0
        public void VisitNode(JSBlockStatement block, bool includeBraces)
        {
            if (includeBraces)
            {
                Output.OpenBrace();
            }

            for (var i = 0; i < block.Statements.Count; i++)
            {
                Visit(block.Statements[i]);
            }

            if (includeBraces)
            {
                Output.CloseBrace();
            }
        }
Esempio n. 3
0
        protected void TranslateInterface(DecompilerContext context, JavascriptFormatter output, TypeDefinition iface)
        {
            output.Identifier("JSIL.MakeInterface", null);
            output.LPar();
            output.NewLine();

            output.Value(Util.EscapeIdentifier(iface.FullName, EscapingMode.String));
            output.Comma();

            output.OpenBracket();
            output.CommaSeparatedList(
                (from p in iface.GenericParameters select p.Name), ListValueType.Primitive
            );
            output.CloseBracket();
            output.Comma();

            output.OpenBrace();

            bool isFirst = true;
            foreach (var m in iface.Methods) {
                var methodInfo = TypeInfoProvider.GetMethod(m);
                if ((methodInfo != null) && methodInfo.IsIgnored)
                    continue;

                if (!isFirst) {
                    output.Comma();
                    output.NewLine();
                }

                output.Value(Util.EscapeIdentifier(m.Name));
                output.Token(": ");
                output.Identifier("Function");

                isFirst = false;
            }

            foreach (var p in iface.Properties) {
                var propertyInfo = TypeInfoProvider.GetProperty(p);
                if ((propertyInfo != null) && propertyInfo.IsIgnored)
                    continue;

                if (!isFirst) {
                    output.Comma();
                    output.NewLine();
                }

                output.Value(Util.EscapeIdentifier(p.Name));
                output.Token(": ");
                output.Identifier("Property");

                isFirst = false;
            }

            output.NewLine();
            output.CloseBrace(false);

            output.RPar();
            output.Semicolon();
            output.NewLine();
        }
Esempio n. 4
0
        protected void TranslateEnum(DecompilerContext context, JavascriptFormatter output, TypeDefinition enm)
        {
            output.Identifier("JSIL.MakeEnum", null);
            output.LPar();
            output.NewLine();

            output.Value(Util.EscapeIdentifier(enm.FullName, EscapingMode.String));
            output.Comma();
            output.OpenBrace();

            var typeInformation = TypeInfoProvider.GetTypeInformation(enm);
            if (typeInformation == null)
                throw new InvalidOperationException();

            bool isFirst = true;
            foreach (var em in typeInformation.EnumMembers.Values) {
                if (!isFirst) {
                    output.Comma();
                    output.NewLine();
                }

                output.Identifier(em.Name);
                output.Token(": ");
                output.Value(em.Value);

                isFirst = false;
            }

            output.NewLine();
            output.CloseBrace(false);
            output.Comma();
            output.Value(typeInformation.IsFlagsEnum);
            output.NewLine();

            output.RPar();
            output.Semicolon();
            output.NewLine();
        }
Esempio n. 5
0
        protected void Translate(DecompilerContext context, AssemblyDefinition assembly, Stream outputStream)
        {
            bool stubbed = IsStubbed(assembly);

            var initializer = new List<Action>();
            var tw = new StreamWriter(outputStream, Encoding.ASCII);
            var formatter = new JavascriptFormatter(tw, this.TypeInfoProvider, assembly);

            var version = Assembly.GetExecutingAssembly().GetName().Version;
            formatter.Comment(
                "Generated by JSIL v{0}.{1}.{2} build {3}. See http://jsil.org/ for more information.",
                version.Major, version.Minor, version.Build, version.Revision
            );
            formatter.NewLine();

            if (stubbed) {
                formatter.Comment("Generating type stubs only");
                formatter.NewLine();
            }

            formatter.DeclareAssembly();

            var sealedTypes = new HashSet<TypeDefinition>();

            // Important to clear this because types with the exact same full names can be defined in multiple assemblies
            DeclaredTypes.Clear();

            foreach (var module in assembly.Modules)
                TranslateModule(context, formatter, module, initializer, sealedTypes, stubbed);

            if (sealedTypes.Count > 0) {
                var groups = (from st in sealedTypes
                              let parent = JavascriptFormatter.GetParent(st, null)
                              group st by parent);

                initializer.Add(() => {
                    foreach (var g in groups) {
                        formatter.Identifier("JSIL.SealTypes", null);
                        formatter.LPar();

                        formatter.Identifier(formatter.PrivateToken);
                        formatter.Comma();
                        formatter.Value(g.Key);
                        formatter.Comma();

                        formatter.NewLine();

                        formatter.CommaSeparatedList(
                            (from typedef in g select typedef.Name), ListValueType.Primitive
                        );

                        formatter.NewLine();
                        formatter.RPar();
                        formatter.Semicolon();
                    }
                });
            }

            foreach (var init in initializer) {
                formatter.Identifier("JSIL.QueueInitializer", null);
                formatter.LPar();
                formatter.OpenFunction(null, null);

                init();

                formatter.CloseBrace(false);
                formatter.RPar();
                formatter.Semicolon();
            }

            tw.Flush();
        }
Esempio n. 6
0
        public void EmitInterfaceDefinition(
            DecompilerContext context, IAstEmitter astEmitter, TypeDefinition iface
            )
        {
            Formatter.Identifier("JSIL.MakeInterface", EscapingMode.None);
            Formatter.LPar();
            Formatter.NewLine();

            Formatter.Value(Util.DemangleCecilTypeName(iface.FullName));
            Formatter.Comma();

            Formatter.Value(iface.IsPublic);
            Formatter.Comma();

            Formatter.OpenBracket();
            WriteGenericParameterNames(iface.GenericParameters);
            Formatter.CloseBracket();

            Formatter.Comma();
            Formatter.OpenFunction(null, (f) =>
                                   f.Identifier("$")
                                   );

            var refContext = new TypeReferenceContext {
                EnclosingType = iface,
                DefiningType  = iface
            };

            bool isFirst = true;

            foreach (var methodGroup in iface.Methods.GroupBy(md => md.Name))
            {
                foreach (var m in methodGroup)
                {
                    if (Translator.ShouldSkipMember(m))
                    {
                        continue;
                    }

                    var methodInfo = _TypeInfoProvider.GetMethod(m);

                    if ((methodInfo == null) || methodInfo.IsIgnored)
                    {
                        continue;
                    }

                    Formatter.Identifier("$", EscapingMode.None);
                    Formatter.Dot();
                    Formatter.Identifier("Method", EscapingMode.None);
                    Formatter.LPar();

                    Formatter.WriteRaw("{}");
                    Formatter.Comma();

                    Formatter.Value(Util.EscapeIdentifier(m.Name, EscapingMode.String));
                    Formatter.Comma();

                    Formatter.MethodSignature(m, methodInfo.Signature, refContext);

                    Formatter.RPar();
                    Formatter.Semicolon(true);
                }
            }

            foreach (var p in iface.Properties)
            {
                var propertyInfo = _TypeInfoProvider.GetProperty(p);
                if ((propertyInfo != null) && propertyInfo.IsIgnored)
                {
                    continue;
                }

                Formatter.Identifier("$", EscapingMode.None);
                Formatter.Dot();
                Formatter.Identifier("Property", EscapingMode.None);
                Formatter.LPar();

                Formatter.WriteRaw("{}");
                Formatter.Comma();

                Formatter.Value(Util.EscapeIdentifier(p.Name, EscapingMode.String));

                Formatter.RPar();
                Formatter.Semicolon(true);
            }

            Formatter.CloseBrace(false);

            Formatter.Comma();

            refContext = new TypeReferenceContext {
                EnclosingType = iface.DeclaringType,
                DefiningType  = iface
            };

            Formatter.OpenBracket();
            foreach (var i in iface.Interfaces)
            {
                if (!isFirst)
                {
                    Formatter.Comma();
                }

                Formatter.TypeReference(i, refContext);

                isFirst = false;
            }
            Formatter.CloseBracket();

            Formatter.RPar();

            EmitCustomAttributes(context, iface, iface, astEmitter);

            Formatter.Semicolon();
            Formatter.NewLine();
        }
Esempio n. 7
0
 public void EmitFooter()
 {
     if (_referenceOverrides != null)
     {
         Formatter.CloseBrace();
         Formatter.RPar();
         Formatter.LPar();
         Formatter.RPar();
         Formatter.Semicolon();
     }
 }