Ejemplo n.º 1
0
        // [TestCase]
        // FIXME: Cloning scripts doesn't work
        public void ExecuteCrossCompartment()
        {
            using (var tc = new TestContext()) {
                tc.Context.ReportUncaughtExceptions = false;

                var scriptRoot = new Rooted <JSScriptPtr>(tc);

                var options = new JSCompileOptions();
                options.canLazilyParse = false;
                options.defineOnScope  = false;
                options.sourceIsLazy   = false;
                options.noScriptRval   = true;

                Assert.IsTrue(JSAPI.CompileScript(
                                  tc, tc.Global,
                                  "function fn() { return global_v; }; global_v = 3;",
                                  options, scriptRoot
                                  ));

                if (tc.Context.Exception.IsPending)
                {
                    throw tc.Context.Exception.GetManaged();
                }

                tc.Leave();

                using (var tc2 = new TestContext(tc.Runtime)) {
                    var eres = (bool)JSAPI.CloneAndExecuteScript(tc2, tc2.Global, scriptRoot);
                    if (tc2.Context.Exception.IsPending)
                    {
                        throw tc2.Context.Exception.GetManaged();
                    }

                    Assert.IsTrue(eres);

                    Assert.AreEqual(3, tc2.Global["global_v"].ToManaged(tc2));

                    tc2.Global["global_v"] = new JS.Value(5);

                    var invokeResult = tc2.Global["fn"].InvokeFunction(tc2, JSHandleObject.Zero);
                    Assert.AreEqual(5, invokeResult.Value.ToManaged(tc2));
                }
            }
        }