public override void VisitSwitchStatement(SwitchStatement switchStatement) { var compiledExpr = CompileExpression(switchStatement.Expression, CompileExpressionFlags.ReturnValueIsImportant); _result.AddRange(compiledExpr.AdditionalStatements); var oldGotoCaseMap = _currentGotoCaseMap; var gotoCaseData = new GatherGotoCaseAndDefaultDataVisitor(_resolver, _nextLabelIndex).Process(switchStatement); _currentGotoCaseMap = gotoCaseData.Item2; var caseClauses = new List<JsSwitchSection>(); foreach (var section in switchStatement.SwitchSections) { SetRegion(section.GetRegion()); var values = new List<JsExpression>(); foreach (var v in section.CaseLabels) { if (v.Expression.IsNull) { values.Add(null); // Default } else { var rr = _resolver.Resolve(v.Expression); object value = rr.ConstantValue; if (rr is MemberResolveResult && ((MemberResolveResult)rr).Member is IField) { var sem = _metadataImporter.GetFieldSemantics((IField)((MemberResolveResult)rr).Member); if (sem.Type == FieldScriptSemantics.ImplType.Constant) value = sem.Value; } if (value == null) { values.Add(JsExpression.Null); } else if (value is string) { values.Add(JsExpression.String((string)value)); } else { values.Add(JsExpression.Number((int)Convert.ChangeType(value, typeof(int)))); } } } var ic = CreateInnerCompiler(); IList<JsStatement> statements; if (section.Statements.Count == 1 && section.Statements.First() is BlockStatement) { statements = ic.Compile(section.Statements.First()).Statements; } else { ic.VisitChildren(section); statements = ic._result; } if (gotoCaseData.Item1.ContainsKey(section)) statements = new[] { JsStatement.Label(gotoCaseData.Item1[section], statements[0]) }.Concat(statements.Skip(1)).ToList(); caseClauses.Add(JsStatement.SwitchSection(values, JsStatement.Block(statements))); } _result.Add(JsStatement.Switch(compiledExpr.Expression, caseClauses)); _currentGotoCaseMap = oldGotoCaseMap; }
public override void VisitSwitchStatement(SwitchStatement switchStatement) { var compiledExpr = CompileExpression(switchStatement.Expression, true); _result.AddRange(compiledExpr.AdditionalStatements); var oldGotoCaseMap = _currentGotoCaseMap; var gotoCaseData = new GatherGotoCaseAndDefaultDataVisitor(_resolver, _nextLabelIndex).Process(switchStatement); _currentGotoCaseMap = gotoCaseData.Item2; var caseClauses = new List<JsSwitchSection>(); foreach (var section in switchStatement.SwitchSections) { var values = new List<JsExpression>(); foreach (var v in section.CaseLabels) { if (v.Expression.IsNull) { values.Add(null); // Default } else { var rr = _resolver.Resolve(v.Expression); if (rr.ConstantValue == null) { values.Add(JsExpression.Null); } else if (rr.ConstantValue is string) { values.Add(JsExpression.String((string)rr.ConstantValue)); } else { values.Add(JsExpression.Number((int)Convert.ChangeType(rr.ConstantValue, typeof(int)))); } } } var statements = section.Statements.SelectMany(stmt => CreateInnerCompiler().Compile(stmt).Statements); if (gotoCaseData.Item1.ContainsKey(section)) statements = new[] { new JsLabelStatement(gotoCaseData.Item1[section]) }.Concat(statements); caseClauses.Add(new JsSwitchSection(values, new JsBlockStatement(statements))); } _result.Add(new JsSwitchStatement(compiledExpr.Expression, caseClauses)); _currentGotoCaseMap = oldGotoCaseMap; }