Example #1
0
        protected JSExpression Translate_Stloc(ILExpression node, ILVariable variable)
        {
            if (node.Arguments[0].Code == ILCode.GetCallSite)
                DynamicCallSites.SetAlias(variable, (FieldReference)node.Arguments[0].Operand);

            // GetCallSite and CreateCallSite produce null expressions, so we want to ignore assignments containing them
            var value = TranslateNode(node.Arguments[0]);
            if ((value.IsNull) && !(value is JSUntranslatableExpression) && !(value is JSIgnoredMemberReference))
                return new JSNullExpression();

            var expectedType = node.ExpectedType ?? node.InferredType ?? variable.Type;
            if (!TypesAreAssignable(expectedType, value.GetExpectedType(TypeSystem)))
                value = Translate_Conv(value, expectedType);

            JSVariable jsv;

            if (RenamedVariables.TryGetValue(variable, out jsv))
                jsv = new JSIndirectVariable(Variables, jsv.Identifier, ThisMethodReference);
            else
                jsv = new JSIndirectVariable(Variables, variable.Name, ThisMethodReference);

            if (jsv.IsReference) {
                JSExpression materializedValue;
                if (!JSReferenceExpression.TryMaterialize(JSIL, value, out materializedValue))
                    Console.Error.WriteLine(String.Format("Cannot store a non-reference into variable {0}: {1}", jsv, value));
                else
                    value = materializedValue;
            }

            return new JSBinaryOperatorExpression(
                JSOperator.Assignment, jsv,
                value,
                value.GetExpectedType(TypeSystem)
            );
        }
Example #2
0
        protected JSExpression Translate_Ldloc(ILExpression node, ILVariable variable)
        {
            JSExpression result;
            JSVariable renamed;

            if (RenamedVariables.TryGetValue(variable, out renamed))
                result = new JSIndirectVariable(Variables, renamed.Identifier, ThisMethodReference);
            else
                result = new JSIndirectVariable(Variables, variable.Name, ThisMethodReference);

            var expectedType = node.ExpectedType ?? node.InferredType ?? variable.Type;
            if (!TypesAreAssignable(expectedType, variable.Type))
                result = Translate_Conv(result, expectedType);

            return result;
        }