Esempio n. 1
0
        public static BoundProgram Perform(BoundProgram program, IList<BoundExpression> resultExpressions)
        {
            // If the last statement of the program is a return, we don't
            // have to rewrite the program.

            var body = program.Body;
            if (
                body.Body.Nodes.Count > 0 &&
                body.Body.Nodes[body.Body.Nodes.Count - 1] is BoundReturn
            )
                return program;

            // If we don't have any result expressions, we only have to
            // insert a return statement.

            if (resultExpressions == null || resultExpressions.Count == 0)
            {
                // Create a new nodes list with the added return statement.

                var nodes = new ReadOnlyArray<BoundStatement>.Builder();

                nodes.AddRange(body.Body.Nodes);
                nodes.Add(new BoundReturn(
                    null,
                    SourceLocation.Missing
                ));

                // Return the updated program.

                return program.Update(
                    body.Update(
                        body.Body.Update(
                            body.Body.Temporaries,
                            nodes.ToReadOnly(),
                            body.Body.Location
                            ),
                        body.Closure,
                        body.ScopedClosure,
                        body.Arguments,
                        body.Locals,
                        body.MappedArguments,
                        body.Flags,
                        body.TypeManager
                    )
                );
            }

            // Otherwise, we need to do a full rewrite.

            return program.Update(
                (BoundBody)new Rewriter(resultExpressions, body.TypeManager).Visit(program.Body)
            );
        }
Esempio n. 2
0
        public JsMain BuildMainMethod(BoundProgram program)
        {
            if (program == null)
                throw new ArgumentNullException("program");

            var method = _scriptBuilder.CreateFunction(typeof(JsMain), MainMethodName, null);

            _scope = new Scope(
                this,
                method.GetILBuilder(),
                false,
                program.Body,
                null,
                _scriptBuilder,
                null
            );

            _scope.EmitLocals(program.Body.TypeManager);

            if (program.Body.Closure != null)
                EmitClosureSetup(program.Body);

            EmitInitializeArguments(program.Body);

            EmitStatements(program.Body.Body);

            // Ensure that we return something.

            EmitReturn(new BoundReturn(null, SourceLocation.Missing));

            // Emit the exceptional return block if we need it.

            EmitExceptionalReturn();

            _scriptBuilder.Commit();

            return (JsMain)Delegate.CreateDelegate(typeof(JsMain), method.Method);
        }
Esempio n. 3
0
        private void PrintBound(BoundProgram program)
        {
            var functions = FunctionGatherer.Gather(program.Body);

            var bodies = new List<BoundBody> { program.Body };
            bodies.AddRange(functions.Select(p => p.Body));

            using (var stream = File.Create("Bound Dump.txt"))
            using (var writer = new StreamWriter(stream, Encoding.UTF8))
            {
                for (int i = 0; i < bodies.Count; i++)
                {
                    if (i == 0)
                        writer.WriteLine("Program:");
                    else
                        writer.WriteLine("Function:");

                    writer.WriteLine();
                    new BoundTreePrettyPrintVisitor(writer).Visit(bodies[i]);
                    writer.WriteLine();
                }
            }
        }
Esempio n. 4
0
        private void EnsureGlobalsDeclared(BoundProgram program)
        {
            var scope = Global.GlobalScope;

            foreach (var local in program.Body.Locals)
            {
                if (
                    local.IsDeclared &&
                    !scope.HasOwnProperty(local.Name)
                )
                    scope.DefineProperty(local.Name, JsUndefined.Instance, PropertyAttributes.DontEnum);
            }
        }
Esempio n. 5
0
 public static IList<BoundExpression> Perform(BoundProgram node)
 {
     return Perform(node.Body, null);
 }
Esempio n. 6
0
 public static BoundProgram Perform(BoundProgram node)
 {
     return node.Update(Perform(node.Body));
 }
Esempio n. 7
0
 public static void Perform(BoundProgram node)
 {
     Perform(node.Body);
 }