private JsExpression CompileImportedTypeCheckCode(IType type, ref JsExpression @this, IRuntimeContext context, bool isTypeIs)
        {
            var def = type.GetDefinition();

            if (def == null)
            {
                return(null);
            }
            var ia = AttributeReader.ReadAttribute <ImportedAttribute>(def);

            if (ia == null || string.IsNullOrEmpty(ia.TypeCheckCode))
            {
                return(null);
            }

            // Can ignore errors here because they are caught by the metadata importer
            var method    = MetadataUtils.CreateTypeCheckMethod(type, _compilation);
            var tokens    = InlineCodeMethodCompiler.Tokenize(method, ia.TypeCheckCode, _ => {});
            int thisCount = tokens.Count(t => t.Type == InlineCodeToken.TokenType.This);

            if (!isTypeIs || thisCount > 0)
            {
                @this = context.EnsureCanBeEvaluatedMultipleTimes(@this, new JsExpression[0]);
            }
            return(JsExpression.LogicalAnd(
                       ReferenceNotEquals(@this, JsExpression.Null, context),
                       InlineCodeMethodCompiler.CompileExpressionInlineCodeMethodInvocation(method, tokens, @this, EmptyList <JsExpression> .Instance, n => { var t = ReflectionHelper.ParseReflectionName(n).Resolve(_compilation); return t.Kind == TypeKind.Unknown ? JsExpression.Null : InstantiateType(t, context); }, t => InstantiateTypeForUseAsTypeArgumentInInlineCode(t, context), _ => {})));
        }
Ejemplo n.º 2
0
        private JsExpression GenerateStructEqualsMethod(ITypeDefinition type, string typeVariableName)
        {
            var o     = JsExpression.Identifier("o");
            var parts = new List <JsExpression>();

            foreach (var f in type.Fields.Where(f => !f.IsStatic))
            {
                var expr = GenerateFieldCompare(f, o);
                if (expr != null)
                {
                    parts.Add(expr);
                }
            }

            JsExpression typeCompare = JsExpression.Invocation(JsExpression.Member(_systemScript, "isInstanceOfType"), o, JsExpression.Identifier(typeVariableName));

            if (parts.Count == 0)
            {
                return(JsExpression.FunctionDefinition(new[] { "o" }, JsStatement.Return(typeCompare)));
            }
            else
            {
                return(JsExpression.FunctionDefinition(new[] { "o" }, JsStatement.Block(
                                                           JsStatement.If(JsExpression.LogicalNot(typeCompare),
                                                                          JsStatement.Return(JsExpression.False),
                                                                          null
                                                                          ),
                                                           JsStatement.Return(parts.Aggregate((old, p) => old == null ? p : JsExpression.LogicalAnd(old, p)))
                                                           )));
            }
        }