private void ProcessNamespace(string ns) { if (processedNamespaces.Contains(ns)) { return; } processedNamespaces.Add(ns); var lastDotIndex = ns.LastIndexOf('.'); var parentNamespace = lastDotIndex != -1 ? ns.Substring(0, lastDotIndex) : null; if (parentNamespace != null) { ProcessNamespace(parentNamespace); } JsBinaryExpression result; if (lastDotIndex == -1) { result = Js.Assign(Js.Reference("window").Member(ns), Js.Object()); } else { result = Js.Assign(Js.Reference(ns), Js.Object()); } result.Right = Js.Binary(JsBinaryOperator.LogicalOr, result.Left, result.Right); body.Express(result); }
public override JsNode Visit(JsReturnStatement node) { escapeStatements.Add(new EscapeStatement { Type = Return }); return(Js.Return(Js.Object( Js.Item(EscapeTypeField, Js.Primitive(Return)), Js.Item(EscapeValueField, node.Expression != null ? node.Expression : Js.Null()), Js.Item(EscapeDepthField, Js.Primitive(loopDepth)) ).Compact())); }
public override JsNode Visit(JsBreakStatement node) { escapeStatements.Add(new EscapeStatement { Type = Break, Label = node.Label }); return(Js.Return(Js.Object( Js.Item(EscapeTypeField, Js.Primitive(Break)), Js.Item(EscapeLabelField, node.Label != null ? Js.Primitive(node.Label) : Js.Null()), Js.Item(EscapeDepthField, node.Label != null ? Js.Primitive(transformer.GetLabelDepth(node.Label)) : Js.Primitive(0)) ).Compact())); }
private JsStatement TransformBody(JsStatement body) { var block = body is JsBlockStatement ? (JsBlockStatement)body : Js.Block(body); if (!escapeStatements.Any()) { return(Js.Express(idioms.Wrap(block))); } else { if (!(block.Statements.Last() is JsReturnStatement)) { block.Return(Js.Object(Js.Item(EscapeTypeField, Js.Primitive(0))).Compact()); } var wrapped = idioms.Wrap(block); var outerBlock = new JsBlockStatement(); var loopResult = outerBlock.Local("$loopResult", wrapped); if (escapeStatements.Any(x => x.Type == Return)) { outerBlock.If(loopResult.GetReference().Member(EscapeTypeField).EqualTo(Js.Primitive(Return)), Js.Return(loopResult.GetReference().Member(EscapeValueField))); } if (escapeStatements.Any(x => x.Type == Continue)) { var escapes = escapeStatements.Where(x => x.Type == Continue).Distinct(); JsStatement ifTrue; if (!escapes.Any(x => x.Label != null)) { ifTrue = Js.Continue(); } else { ifTrue = Js.Switch( loopResult.GetReference().Member(EscapeLabelField), escapes .Where(x => x.Label == null) .Select(x => Js.Section(Js.Null()).Statement(Js.Continue())) .Concat(escapes .Where(x => x.Label != null && transformer.GetLabelDepth(x.Label) == loopDepth) .Select(x => Js.Section(Js.Primitive(x.Label)).Statement(Js.Continue(x.Label)))) .Concat(new[] { Js.Section(Js.DefaultLabel()).Statements(Js.Return(loopResult.GetReference())) }) .ToArray()); } outerBlock.If(loopResult.GetReference().Member(EscapeTypeField).EqualTo(Js.Primitive(Continue)), ifTrue); } if (escapeStatements.Any(x => x.Type == Break)) { var escapes = escapeStatements.Where(x => x.Type == Break).Distinct(); JsStatement ifTrue; if (!escapes.Any(x => x.Label != null)) { ifTrue = Js.Break(); } else { ifTrue = Js.Switch(loopResult.GetReference().Member(EscapeLabelField), escapes.Select(x => Js.Section(Js.Primitive(x.Label)).Statement(Js.Break(x.Label))).ToArray()); } outerBlock.If(loopResult.GetReference().Member(EscapeTypeField).EqualTo(Js.Primitive(Break)), ifTrue); } return(outerBlock); } }