Beispiel #1
0
        public void Initialize()
        {
            _context = new GlobalContext();
            _context.ActivateInCurrentThread();

            try
            {
                using (var file = new FileStream(JsFunfuzzScriptPath, FileMode.Open))
                    using (var fileReader = new StreamReader(file))
                    {
                        _module = new Module(fileReader.ReadToEnd());
                    }
            }
            catch
            {
                _context.Deactivate();
                throw;
            }

            _output       = new StringBuilder();
            _oldErrOutput = Console.Error;
            _oldOutput    = Console.Out;
            Console.SetOut(new StringWriter(_output));
            Console.SetError(new StringWriter(_output));
        }
Beispiel #2
0
        public void Initialize()
        {
            _context = new GlobalContext();
            _context.ActivateInCurrentThread();

            try
            {
                using (var file = new FileStream(_typescriptServicesPath, FileMode.Open))
                    using (var fileReader = new StreamReader(file))
                    {
                        _module = new Module(fileReader.ReadToEnd());
                    }

                _module.Context.DefineVariable("window").Assign(_module.Context.ThisBind);
                _module.Run();
            }
            catch
            {
                _context.Deactivate();
                throw;
            }

            _output       = new StringBuilder();
            _oldErrOutput = Console.Error;
            _oldOutput    = Console.Out;
            Console.SetOut(new StringWriter(_output));
            Console.SetError(new StringWriter(_output));
        }
Beispiel #3
0
        public void Initialize()
        {
            using (var file = new FileStream(UglifyJsScriptPath, FileMode.Open))
                using (var fileReader = new StreamReader(file))
                {
                    _context = new GlobalContext();
                    _context.ActivateInCurrentThread();
                    _module = new Module(fileReader.ReadToEnd());
                }

            _module.Run();
        }
Beispiel #4
0
        /*      Global Context
         *      ┌───────────
         *      │  <Object, Number, Math, Function etc.>
         *      │  <Global functions like parseInt(...) and isNaN(...)>
         *      │  <Global constants like undefined and NaN>
         *      │
         *      │  new Context(), new Module().Context
         *      │  ┌───────────
         *      │  │ <Global variables of the script. Smthg like "window.my_variable">
         *      │  │
         *      │  │ Contexts of nested functions
         *      │  │ ┌───────────
         *      │  │ │ <Parameters and local variables of a function>
         *      │  │ │
         *      │  │ │ Contexts of functions inside other functions
         *      │  │ │ ┌───────────
         *      │  │ │ │ ...
         *      │  │ │ └───────────
         *      │  │ └───────────
         *      │  └───────────
         *      └───────────
         *
         *      Before version 2.4 was the one Global Context in all cases.
         *      Since version 2.4 you can create addition global contexts for isolation scripts from each other.
         *
         *      New created Global Context can be activated in one or more thread, after that all new instances
         *      of Context will be linked to this Global Context.
         *      Also you can directly link Context to new Global Context without activation.
         *      Just specify it when creating by pass it into constructor new Context(my_global_context);
         */

        public override void Run()
        {
            var firstGlobalContext  = new GlobalContext("First global context");
            var secondGlobalContext = new GlobalContext("Second global context");

            firstGlobalContext.ActivateInCurrentThread();
            var firstContext = new Context();

            firstGlobalContext.Deactivate();

            var secondContext = new Context(secondGlobalContext);

            firstContext.Eval(@"
Object.my_secret_field = 'my secret value';
console.log(Object.my_secret_field); // Output: my secret value
");

            secondContext.Eval(@"
console.log(Object.my_secret_field); // Output: undefined
");
        }
Beispiel #5
0
        protected void RunFile(string fileName)
        {
            string code;

            using (var f = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                using (var sr = new StreamReader(f))
                    code = sr.ReadToEnd();

            var globalContext = new GlobalContext();

            try
            {
                globalContext.ActivateInCurrentThread();

                var output    = new StringBuilder();
                var oldOutput = Console.Out;
                Console.SetOut(new StringWriter(output));
                var    pass = true;
                Module module;
                if (!string.IsNullOrEmpty(_sta))
                {
                    module = new Module(fileName, _sta);
                    module.Run();
                }
                else
                {
                    module = new Module(fileName, "");
                }

                module.ModuleResolversChain.Add(new MyTestModuleResolver());

                var preambleEnd     = 0;
                var preambleEndTemp = 0;
                do
                {
                    preambleEnd = preambleEndTemp;
                    try
                    {
                        preambleEndTemp = Parser.SkipComment(code, preambleEndTemp, true);
                    }
                    catch
                    {
                        break;
                    }
                }while (preambleEnd < preambleEndTemp);

                if (code.IndexOf("@negative") != -1)
                {
                    System.Diagnostics.Debugger.Break();
                }

                var negative = code.IndexOf("* @negative", 0, preambleEnd) != -1;
                var strict   = code.IndexOf("* @onlyStrict", 0, preambleEnd) != -1;

                try
                {
                    try
                    {
                        module.Context.Eval(code, !strict);
                    }
                    finally
                    {
                        pass ^= negative;
                    }
                }
                catch (JSException e)
                {
                    pass = negative;
                    if (!pass)
                    {
                        output.Append(e);
                    }
                }
                catch (Exception e)
                {
                    System.Diagnostics.Debugger.Break();
                    output.Append(e);
                    pass = false;
                }
                finally
                {
                    Console.SetOut(oldOutput);
                }

                Assert.IsTrue(pass, output.ToString());
                Assert.AreEqual(string.Empty, output.ToString().Trim());
            }
            finally
            {
                globalContext.Deactivate();
            }
        }
Beispiel #6
0
        protected void RunFile(string fileName)
        {
            string code;

            using (var f = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                using (var sr = new StreamReader(f))
                    code = sr.ReadToEnd();

            var globalContext = new GlobalContext();

            try
            {
                globalContext.ActivateInCurrentThread();

                var negative  = false;
                var output    = new StringBuilder();
                var oldOutput = Console.Out;
                Console.SetOut(new StringWriter(output));
                var    pass = true;
                Module module;
                var    moduleName = fileName.Split(new[] { '/', '\\' }).Last();
                if (!string.IsNullOrEmpty(_sta))
                {
                    module = new Module(moduleName, _sta);
                    module.Run();

                    negative = code.IndexOf("@negative") != -1;
                }
                else
                {
                    module = new Module(moduleName, "");
                }

                try
                {
                    try
                    {
                        module.Context.Eval(code, true);
                    }
                    finally
                    {
                        pass ^= negative;
                    }
                }
                catch (JSException e)
                {
                    pass = negative;
                    if (!pass)
                    {
                        Console.WriteLine(e.Message);
                    }
                }
                catch (Exception)
                {
                    System.Diagnostics.Debugger.Break();
                    pass = false;
                }
                finally
                {
                    Module.RemoveFromModuleCache(moduleName);
                    Console.SetOut(oldOutput);
                }

                Assert.IsTrue(pass, output.ToString());
                Assert.AreEqual(string.Empty, output.ToString().Trim());
            }
            finally
            {
                globalContext.Deactivate();
            }
        }