예제 #1
0
        public static void EmitYieldReturn(AbstractEmitterBlock block, IType returnType)
        {
            block.WriteReturn(true);

            if (returnType != null && returnType.Name == "IEnumerator")
            {
                block.Write("Bridge.Array.toEnumerator($yield)");
            }
            else
            {
                block.Write("Bridge.Array.toEnumerable($yield)");
            }

            block.WriteSemiColon();
            block.WriteNewLine();
        }
예제 #2
0
        public static void EmitYieldReturn(AbstractEmitterBlock block, IType returnType)
        {
            block.WriteReturn(true);

            if (returnType != null && returnType.Name == "IEnumerator")
            {
                block.Write("Bridge.Array.toEnumerator($yield)");
            }
            else
            {
                block.Write("Bridge.Array.toEnumerable($yield)");
            }

            block.WriteSemiColon();
            block.WriteNewLine();
        }
예제 #3
0
        public static void EmitYield(AbstractEmitterBlock block, IType returnType)
        {
            block.Write("var $yield = []");

            block.WriteSemiColon();
            block.WriteNewLine();
        }
예제 #4
0
        public static void EmitYield(AbstractEmitterBlock block, IType returnType)
        {
            block.Write("var $yield = []");

            block.WriteSemiColon();
            block.WriteNewLine();
        }
예제 #5
0
        public static string ReplaceInlineArgs(AbstractEmitterBlock block, string inline, Expression[] args)
        {
            var emitter = block.Emitter;

            inline = _formatArg.Replace(inline, delegate(Match m)
            {
                int count       = emitter.Writers.Count;
                string key      = m.Groups[2].Value;
                string modifier = m.Groups[1].Success ? m.Groups[4].Value : null;

                StringBuilder oldSb = emitter.Output;
                emitter.Output      = new StringBuilder();

                Expression expr = null;

                if (Regex.IsMatch(key, "^\\d+$"))
                {
                    expr = args.Skip(int.Parse(key)).FirstOrDefault();
                }
                else
                {
                    expr = args.FirstOrDefault(e => e.ToString() == key);
                }

                string s = "";
                if (expr != null)
                {
                    var writer = block.SaveWriter();
                    block.NewWriter();
                    expr.AcceptVisitor(emitter);
                    s = emitter.Output.ToString();
                    block.RestoreWriter(writer);

                    if (modifier == "raw")
                    {
                        s = s.Trim('"');
                    }
                }

                block.Write(block.WriteIndentToString(s));

                if (emitter.Writers.Count != count)
                {
                    block.PopWriter();
                }

                string replacement = emitter.Output.ToString();
                emitter.Output     = oldSb;

                return(replacement);
            });

            return(inline);
        }
예제 #6
0
        public static void EmitYieldReturn(AbstractEmitterBlock block, IType returnType)
        {
            block.WriteReturn(true);

            if (returnType != null && returnType.Name == "IEnumerator")
            {
                if (returnType.TypeArguments.Count > 0)
                {
                    block.Write(JS.Types.SYSTEM_ARRAY + "." + JS.Funcs.TO_ENUMERATOR + "(" + JS.Vars.YIELD + ", " + BridgeTypes.ToJsName(returnType.TypeArguments.First(), block.Emitter) + ")");
                }
                else
                {
                    block.Write(JS.Types.SYSTEM_ARRAY + "." + JS.Funcs.TO_ENUMERATOR + "(" + JS.Vars.YIELD + ")");
                }
            }
            else
            {
                block.Write(JS.Types.SYSTEM_ARRAY + "." + JS.Funcs.TO_ENUMERABLE + "(" + JS.Vars.YIELD + ")");
            }

            block.WriteSemiColon();
            block.WriteNewLine();
        }
예제 #7
0
        public static string GetInlineInit(Expression item, AbstractEmitterBlock block)
        {
            Expression expr = null;

            if (item is NamedExpression)
            {
                var namedExpression = (NamedExpression)item;
                expr = namedExpression.Expression;
            }
            else if (item is NamedArgumentExpression)
            {
                var namedArgumentExpression = (NamedArgumentExpression)item;
                expr = namedArgumentExpression.Expression;
            }

            var    rr         = block.Emitter.Resolver.ResolveNode(item, block.Emitter);
            string inlineCode = null;

            if (expr != null && rr is MemberResolveResult)
            {
                var member = ((MemberResolveResult)rr).Member;

                if (member is IProperty)
                {
                    var setter = ((IProperty)member).Setter;
                    if (setter != null)
                    {
                        inlineCode = block.Emitter.GetInline(setter);
                    }
                }
                else
                {
                    inlineCode = block.Emitter.GetInline(member);
                }

                if (inlineCode != null)
                {
                    bool oldIsAssignment = block.Emitter.IsAssignment;
                    bool oldUnary        = block.Emitter.IsUnaryAccessor;
                    var  oldWriter       = block.SaveWriter();
                    block.NewWriter();
                    block.Emitter.IsAssignment    = true;
                    block.Emitter.IsUnaryAccessor = false;

                    bool hasThis = inlineCode.Contains("{this}");
                    if (inlineCode.StartsWith("<self>"))
                    {
                        hasThis    = true;
                        inlineCode = inlineCode.Substring(6);
                    }

                    if (hasThis)
                    {
                        inlineCode = inlineCode.Replace("{this}", "this");

                        if (member is IProperty)
                        {
                            var argsInfo = new ArgumentsInfo(block.Emitter, item, rr);
                            argsInfo.ArgumentsExpressions = new Expression[] { expr };
                            argsInfo.ArgumentsNames       = new string[] { "value" };
                            argsInfo.ThisArgument         = "this";
                            argsInfo.NamedExpressions     = argsInfo.CreateNamedExpressions(argsInfo.ArgumentsNames, argsInfo.ArgumentsExpressions);

                            inlineCode = inlineCode.Replace("{0}", "[[0]]");
                            new InlineArgumentsBlock(block.Emitter, argsInfo, inlineCode).Emit();
                            inlineCode = block.Emitter.Output.ToString();
                            inlineCode = inlineCode.Replace("[[0]]", "{0}");
                        }
                    }
                    else
                    {
                        if (member.SymbolKind == SymbolKind.Property)
                        {
                            var count = block.Emitter.Writers.Count;
                            block.PushWriter("this." + inlineCode);

                            expr.AcceptVisitor(block.Emitter);

                            if (block.Emitter.Writers.Count > count)
                            {
                                inlineCode = block.PopWriter(true);
                            }
                        }
                        else
                        {
                            block.Write("this." + inlineCode);
                        }
                    }

                    block.Emitter.IsAssignment    = oldIsAssignment;
                    block.Emitter.IsUnaryAccessor = oldUnary;
                    block.RestoreWriter(oldWriter);
                }
            }

            if (inlineCode != null && !inlineCode.Trim().EndsWith(";"))
            {
                inlineCode += ";";
            }

            return(inlineCode);
        }
예제 #8
0
        public static string GetInlineInit(KeyValuePair <IMember, ResolveResult> item, AbstractEmitterBlock block)
        {
            string inlineCode = null;
            var    member     = item.Key;

            if (member is IProperty)
            {
                var setter = ((IProperty)member).Setter;
                if (setter != null)
                {
                    inlineCode = block.Emitter.GetInline(setter);
                }
            }
            else
            {
                inlineCode = block.Emitter.GetInline(member);
            }

            if (inlineCode != null)
            {
                bool oldIsAssignment = block.Emitter.IsAssignment;
                bool oldUnary        = block.Emitter.IsUnaryAccessor;
                var  oldWriter       = block.SaveWriter();
                block.NewWriter();
                block.Emitter.IsAssignment    = true;
                block.Emitter.IsUnaryAccessor = false;

                bool hasThis = Helpers.HasThis(inlineCode);
                inlineCode = Helpers.ConvertTokens(block.Emitter, inlineCode, member);
                if (inlineCode.StartsWith("<self>"))
                {
                    hasThis    = true;
                    inlineCode = inlineCode.Substring(6);
                }

                if (hasThis)
                {
                    inlineCode = inlineCode.Replace("{this}", "this");

                    if (member is IProperty)
                    {
                        inlineCode = inlineCode.Replace("{0}", "[[0]]");

                        AttributeCreateBlock.WriteResolveResult(item.Value, block);
                        var value = block.Emitter.Output.ToString();
                        inlineCode = inlineCode.Replace("{value}", value);
                        inlineCode = inlineCode.Replace("[[0]]", "{0}");
                    }
                }
                else
                {
                    if (member.SymbolKind == SymbolKind.Property)
                    {
                        var count = block.Emitter.Writers.Count;
                        block.PushWriter("this." + inlineCode);
                        AttributeCreateBlock.WriteResolveResult(item.Value, block);

                        if (block.Emitter.Writers.Count > count)
                        {
                            inlineCode = block.PopWriter(true);
                        }
                    }
                    else
                    {
                        block.Write("this." + inlineCode);
                    }
                }

                block.Emitter.IsAssignment    = oldIsAssignment;
                block.Emitter.IsUnaryAccessor = oldUnary;
                block.RestoreWriter(oldWriter);
            }

            if (inlineCode != null && !inlineCode.Trim().EndsWith(";"))
            {
                inlineCode += ";";
            }

            return(inlineCode);
        }
예제 #9
0
        public static void WriteResolveResult(ResolveResult rr, AbstractEmitterBlock block)
        {
            if (rr is ConversionResolveResult)
            {
                rr = ((ConversionResolveResult)rr).Input;
            }

            if (rr is TypeOfResolveResult)
            {
                block.Write(BridgeTypes.ToJsName(((TypeOfResolveResult)rr).ReferencedType, block.Emitter));
            }
            else if (rr is ArrayCreateResolveResult)
            {
                TypeSystemAstBuilder typeBuilder =
                    new TypeSystemAstBuilder(new CSharpResolver(block.Emitter.Resolver.Compilation));
                var expression = typeBuilder.ConvertConstantValue(rr) as ArrayCreateExpression;
                new ArrayCreateBlock(block.Emitter, expression, (ArrayCreateResolveResult)rr).Emit();
            }
            else if (rr is MemberResolveResult)
            {
                var mrr = (MemberResolveResult)rr;

                if (mrr.IsCompileTimeConstant && mrr.Member.DeclaringType.Kind == TypeKind.Enum)
                {
                    var typeDef = mrr.Member.DeclaringType as DefaultResolvedTypeDefinition;

                    if (typeDef != null)
                    {
                        var enumMode = Helpers.EnumEmitMode(typeDef);

                        if ((block.Emitter.Validator.IsExternalType(typeDef) && enumMode == -1) || enumMode == 2)
                        {
                            block.WriteScript(mrr.ConstantValue);

                            return;
                        }

                        if (enumMode >= 3 && enumMode < 7)
                        {
                            string enumStringName = mrr.Member.Name;
                            var    attr           = Helpers.GetInheritedAttribute(mrr.Member,
                                                                                  Translator.Bridge_ASSEMBLY + ".NameAttribute");

                            if (attr != null)
                            {
                                enumStringName = block.Emitter.GetEntityName(mrr.Member);
                            }
                            else
                            {
                                switch (enumMode)
                                {
                                case 3:
                                    enumStringName =
                                        Object.Net.Utilities.StringUtils.ToLowerCamelCase(mrr.Member.Name);
                                    break;

                                case 4:
                                    break;

                                case 5:
                                    enumStringName = enumStringName.ToLowerInvariant();
                                    break;

                                case 6:
                                    enumStringName = enumStringName.ToUpperInvariant();
                                    break;
                                }
                            }

                            block.WriteScript(enumStringName);
                        }
                        else
                        {
                            block.WriteScript(rr.ConstantValue);
                        }
                    }
                }
                else
                {
                    block.WriteScript(rr.ConstantValue);
                }
            }
            else
            {
                block.WriteScript(rr.ConstantValue);
            }
        }
예제 #10
0
        public static string ReplaceInlineArgs(AbstractEmitterBlock block, string inline, Expression[] args)
        {
            var emitter = block.Emitter;
            inline = _formatArg.Replace(inline, delegate(Match m)
            {
                int count = emitter.Writers.Count;
                string key = m.Groups[2].Value;
                string modifier = m.Groups[1].Success ? m.Groups[4].Value : null;

                StringBuilder oldSb = emitter.Output;
                emitter.Output = new StringBuilder();

                Expression expr = null;

                if (Regex.IsMatch(key, "^\\d+$"))
                {
                    expr = args.Skip(int.Parse(key)).FirstOrDefault();
                }
                else
                {
                    expr = args.FirstOrDefault(e => e.ToString() == key);
                }

                string s = "";
                if (expr != null)
                {
                    var writer = block.SaveWriter();
                    block.NewWriter();
                    expr.AcceptVisitor(emitter);
                    s = emitter.Output.ToString();
                    block.RestoreWriter(writer);

                    if (modifier == "raw")
                    {
                        s = s.Trim('"');
                    }
                }

                block.Write(block.WriteIndentToString(s));

                if (emitter.Writers.Count != count)
                {
                    block.PopWriter();
                }

                string replacement = emitter.Output.ToString();
                emitter.Output = oldSb;

                return replacement;
            });

            return inline;
        }