Esempio n. 1
0
        // Parse a program. Initializes the parser, reads any number of
        // statements, and wraps them in a Program node.  Optionally takes a
        // `program` argument.  If present, the statements will be appended
        // to its body instead of creating a new node.
        void ParseTopLevel([NotNull] AstToplevel node)
        {
            var exports = new Dictionary <string, bool>();

            _canBeDirective = true;
            while (Type != TokenType.Eof)
            {
                var stmt = ParseStatement(true, true, exports);

                if (_canBeDirective)
                {
                    if (IsDirectiveCandidate(stmt))
                    {
                        if (IsUseStrictDirective(stmt))
                        {
                            node.HasUseStrictDirective = true;
                            _strict = true;
                            continue;
                        }
                    }
                    else
                    {
                        _canBeDirective = false;
                    }
                }
                node.Body.Add(stmt);
            }

            Next();
            node.End = _lastTokEnd;
        }
Esempio n. 2
0
        public AstToplevel Instrument(AstToplevel toplevel)
        {
            var tt = new InstrumentTreeTransformer(this);

            toplevel = (AstToplevel)tt.Transform(toplevel);
            return(toplevel);
        }
Esempio n. 3
0
    public void Mangle(AstToplevel topLevel)
    {
        var output = new OutputContext(_outputOptions);

        output.InitializeForFrequencyCounting();
        if (_options.FrequencyCounting)
        {
            topLevel.Print(output);
        }
        _options.Chars = output.FinishFrequencyCounting();
        Walk(topLevel);
        foreach (var s in _options.Reserved)
        {
            var idx = AstScope.Debase54(_options.Chars, s);
            if (idx >= 0)
            {
                _options.ReservedOrIdentifier.Add((uint)idx);
            }
        }
        foreach (var s in OutputContext.ReservedWords)
        {
            var idx = AstScope.Debase54(_options.Chars, s);
            if (idx >= 0)
            {
                _options.ReservedOrIdentifier.Add((uint)idx);
            }
        }
        for (var i = 0u; i < _toMangle.Count; i++)
        {
            _toMangle[i].Mangle(_options);
        }
    }
Esempio n. 4
0
        public void AddCountingHelpers(AstToplevel toplevel, string globalThis = "window")
        {
            var tla = Parser.Parse(
                $"var {StorageName}=new Uint32Array({LastIndex});{globalThis}.{StorageName}={StorageName};function {FncNameStatement}(i){{{StorageName}[i]++;}}function {FncNameCond}(r,i){{{StorageName}[i+(r?1:0)]++;return r}}");

            toplevel.Body.InsertRange(0, tla.Body);
        }
Esempio n. 5
0
        public static void AppendToplevelWithRename(AstToplevel main, AstToplevel add, string suffix, Action <AstToplevel>?beforeAdd = null)
        {
            if (main.Body.Count == 0)
            {
                beforeAdd?.Invoke(add);
                main.Body.AddRange(add.Body.AsSpan());
                main.Variables = add.Variables;
                main.Globals   = add.Globals;
                return;
            }
            var renameWalker = new ToplevelRenameWalker(main.Variables !, suffix);

            renameWalker.Walk(add);

            beforeAdd?.Invoke(add);
            main.Body.AddRange(add.Body.AsSpan());
            foreach (var(_, symbolDef) in add.Variables !)
            {
                main.Variables !.TryAdd(symbolDef.Name, symbolDef);
            }

            foreach (var(_, symbolDef) in add.Globals !)
            {
                main.Globals !.TryAdd(symbolDef.Name, symbolDef);
            }
        }
Esempio n. 6
0
 public override AstNode ShallowClone()
 {
     var res = new AstToplevel(Source, Start, End);
     res.Body.AddRange(Body.AsReadOnlySpan());
     res.HasUseStrictDirective = HasUseStrictDirective;
     return res;
 }
Esempio n. 7
0
        public void FigureOutScope(AstToplevel toplevel)
        {
            TreeWalker treeWalker = new SetupScopeChainingAndHandleDefinitionsTreeWalker(_options, toplevel);

            treeWalker.Walk(toplevel);
            treeWalker = new FindBackReferencesAndEvalTreeWalker(_options, toplevel);
            treeWalker.Walk(toplevel);
        }
Esempio n. 8
0
        public static void WrapByIIFE(AstToplevel topLevelAst)
        {
            var func = new AstFunction();

            func.ArgNames.Add(new AstSymbolFunarg("undefined"));
            func.HasUseStrictDirective = true;
            func.Body.TransferFrom(ref topLevelAst.Body);
            topLevelAst.Body.Add(new AstSimpleStatement(new AstUnaryPrefix(Operator.LogicalNot, new AstCall(func))));
        }
Esempio n. 9
0
 /// If there is global variable named `global`, then define it as `window`, because we are bundling for browser
 static void IfNeededPolyfillGlobal(AstToplevel topLevelAst)
 {
     if (topLevelAst.Globals !.ContainsKey("global"))
     {
         var astVar = new AstVar(topLevelAst);
         astVar.Definitions.Add(new AstVarDef(new AstSymbolVar("global"), new AstSymbolRef("window")));
         topLevelAst.Body.Insert(0) = astVar;
     }
 }
Esempio n. 10
0
    public void EmitVarDefinesTest()
    {
        var ast = Helpers.EmitVarDefines(new Dictionary <string, object> {
            { "DEBUG", true }, { "answer", 42 }
        });
        var toplevel = new AstToplevel();

        toplevel.Body.Add(ast);
        Assert.Equal("var DEBUG=true,answer=42", toplevel.PrintToString());
    }
Esempio n. 11
0
        public void Mangle(AstToplevel topLevel)
        {
            var output = new OutputContext();

            output.InitializeForFrequencyCounting();
            topLevel.Print(output);
            _options.Chars = output.FinishFrequencyCounting();
            Walk(topLevel);
            for (var i = 0u; i < _toMangle.Count; i++)
            {
                _toMangle[i].Mangle(_options);
            }
        }
Esempio n. 12
0
        public void ModifyBundle(string name, AstToplevel topLevelAst)
        {
            if (name != _mainJsBundleUrl)
            {
                return;
            }
            var srcToInject = InitG11n();

            if (srcToInject == "")
            {
                return;
            }
            topLevelAst.Body.InsertRange(0, Parser.Parse(srcToInject).Body);
        }
Esempio n. 13
0
 public static (AstToplevel toplevel, AstSymbolVar?symbol) IfPossibleEmitModuleExportsJsWrapper(
     AstToplevel toplevel,
     string?varName = null)
 {
     varName ??= "exports";
     if (toplevel.Globals !["module"].References.Count != 1)
 public FindBackReferencesAndEvalTreeWalker(ScopeOptions options, AstToplevel astToplevel)
 {
     _options             = options;
     _astToplevel         = astToplevel;
     _astToplevel.Globals = new Dictionary <string, SymbolDef>();
 }
 public SetupScopeChainingAndHandleDefinitionsTreeWalker(ScopeOptions options, AstToplevel astToplevel)
 {
     _options      = options;
     _currentScope = astToplevel.ParentScope = null;
     _defun        = null;
 }
Esempio n. 16
0
 public FindBackReferencesAndEvalTreeWalker(ScopeOptions options, AstToplevel astToplevel)
 {
     _options             = options;
     _astToplevel         = astToplevel;
     _astToplevel.Globals = new();
 }