Esempio n. 1
0
        public Program Simplify(SimplifierContext ctxt)
        {
            var subCtxt = ctxt.InFreshStatements();

            Body.Simplify(subCtxt, EvalTimes.Bottom, false);
            return(new Program(Loc, new Statements(subCtxt.Statements)));
        }
Esempio n. 2
0
        static int Main(string[] args)
        {
            try
            {
                if (args.Length < 1)
                {
                    Usage();
                    return(1);
                }
                var infile = Path.GetFullPath(args[0]);

                var outfile  = default(string);
                var compress = false;
                var reformat = false;
                var strict   = false;

                for (var i = 1; i < args.Length; i++)
                {
                    if (args[i].Equals("-o", StringComparison.Ordinal))
                    {
                        if (++i < args.Length)
                        {
                            outfile = Path.GetFullPath(args[i]);
                            if (infile.Equals(outfile, StringComparison.OrdinalIgnoreCase))
                            {
                                Usage();
                                return(1);
                            }
                        }
                        else
                        {
                            Usage();
                            return(1);
                        }
                    }
                    else if (args[i].Equals("-c", StringComparison.Ordinal))
                    {
                        compress = true;
                    }
                    else if (args[i].Equals("-f", StringComparison.Ordinal))
                    {
                        reformat = true;
                    }
                    else if (args[i].Equals("-s", StringComparison.Ordinal))
                    {
                        strict = true;
                    }
                    else
                    {
                        Usage();
                        return(1);
                    }
                }

                var program = JST.Program.FromFile(infile, strict);

                if (compress)
                {
                    var ctxt = new JST.SimplifierContext(true, false, new JST.NameSupply(), null);
                    program = program.Simplify(ctxt);
                }

                if (outfile != null)
                {
                    if (File.Exists(outfile))
                    {
                        File.SetAttributes(outfile, FileAttributes.Normal);
                    }
                    var outdir = Path.GetDirectoryName(outfile);
                    if (!string.IsNullOrEmpty(outdir) && !Directory.Exists(outdir))
                    {
                        Directory.CreateDirectory(outdir);
                    }

                    if (compress || reformat)
                    {
                        program.ToFile(outfile, !compress);
                    }
                    else
                    {
                        File.Copy(infile, outfile, true);
                        File.SetAttributes(outfile, FileAttributes.Normal);
                    }
                }
                return(0);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: jsv.exe: " + e.Message);
                return(1);
            }
        }
Esempio n. 3
0
        static int Main(string[] args)
        {
            try
            {
                if (args.Length < 1)
                {
                    Usage();
                    return 1;
                }
                var infile = Path.GetFullPath(args[0]);

                var outfile = default(string);
                var compress = false;
                var reformat = false;
                var strict = false;

                for (var i = 1; i < args.Length; i++)
                {
                    if (args[i].Equals("-o", StringComparison.Ordinal))
                    {
                        if (++i < args.Length)
                        {
                            outfile = Path.GetFullPath(args[i]);
                            if (infile.Equals(outfile, StringComparison.OrdinalIgnoreCase))
                            {
                                Usage();
                                return 1;
                            }
                        }
                        else
                        {
                            Usage();
                            return 1;
                        }
                    }
                    else if (args[i].Equals("-c", StringComparison.Ordinal))
                        compress = true;
                    else if (args[i].Equals("-f", StringComparison.Ordinal))
                        reformat = true;
                    else if (args[i].Equals("-s", StringComparison.Ordinal))
                        strict = true;
                    else
                    {
                        Usage();
                        return 1;
                    }
                }

                var program = JST.Program.FromFile(infile, strict);

                if (compress)
                {
                    var ctxt = new JST.SimplifierContext(true, false, new JST.NameSupply(), null);
                    program = program.Simplify(ctxt);
                }

                if (outfile != null)
                {
                    if (File.Exists(outfile))
                        File.SetAttributes(outfile, FileAttributes.Normal);
                    var outdir = Path.GetDirectoryName(outfile);
                    if (!string.IsNullOrEmpty(outdir) && !Directory.Exists(outdir))
                        Directory.CreateDirectory(outdir);

                    if (compress || reformat)
                        program.ToFile(outfile, !compress);
                    else
                    {
                        File.Copy(infile, outfile, true);
                        File.SetAttributes(outfile, FileAttributes.Normal);
                    }
                }
                return 0;
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: jsv.exe: " + e.Message);
                return 1;
            }
        }
Esempio n. 4
0
        private JST.FunctionExpression MethodImpl(CST.CSTWriter trace)
        {
            if (trace != null)
                trace.Trace
                    ("Original IL method",
                     w =>
                     {
                         methEnv.Method.AppendDefinition(w);
                         w.EndLine();
                     });

            var func = ImportedMethod(trace) ?? NormalMethod(trace);

            // Simplify
            var simpCtxt = new JST.SimplifierContext(false, env.DebugMode, simpNameSupply, IsValue);
            func = (JST.FunctionExpression)func.Simplify(simpCtxt, EvalTimes.Bottom);
            if (trace != null)
                trace.Trace
                    ("After JavaScript simplification",
                     w =>
                     {
                         func.Append(w);
                         w.EndLine();
                     });

            if (env.DebugMode)
            {
                // Add debugging assistance
                var l = 0;
                Func<int> nextLine = () => { return l++; };
                var lineCountIds = new Set<JST.Identifier>();
                var debugStmnts = WithLineCounts(func.Body, nextLine, 0, lineCountIds);
                lineCountIds.Add(Constants.DebugCurrentLine);
                var debugBody = new Seq<JST.Statement>();
                debugBody.Add
                    (new JST.VariableStatement(lineCountIds.Select(id => new JST.VariableDeclaration(id)).ToSeq()));
                foreach (var s in debugStmnts.Body)
                    debugBody.Add(s);
                var exId = simpNameSupply.GenSym();
                var funcBody = new Seq<JST.Statement>();
#if !JSCRIPT_IS_CORRECT
                funcBody.Add(JST.Statement.Var(exId));
#endif
                funcBody.Add
                    (new JST.TryStatement
                         (new JST.Statements(debugBody),
                          new JST.CatchClause
                              (exId,
                               new JST.Statements
                                   (JST.Statement.DotCall(rootId.ToE(), Constants.RootDebugger, exId.ToE()),
                                    new JST.ThrowStatement(exId.ToE())))));
                func = new JST.FunctionExpression(func.Name, func.Parameters, new JST.Statements(funcBody));
            }

            if (trace != null)
                trace.Trace
                    ("Final JavaScript method",
                     w =>
                     {
                         func.Append(w);
                         w.EndLine();
                     });

            return func;
        }