cross-compartment.js (2695B)
1 var g = newGlobal(); 2 g.mainGlobal = this; 3 4 if (typeof isSameCompartment !== "function") { 5 var isSameCompartment = SpecialPowers.Cu.getJSTestingFunctions().isSameCompartment; 6 } 7 8 var next = async function*(){}.prototype.next; 9 10 var f = g.eval(`(async function*() { 11 var x = yield {message: "yield"}; 12 13 // Input completion values are correctly wrapped into |f|'s compartment. 14 assertEq(isSameCompartment(x, mainGlobal), true); 15 assertEq(x.message, "continue"); 16 17 return {message: "return"}; 18 })`); 19 20 var it = f(); 21 22 // The async iterator is same-compartment with |f|. 23 assertEq(isSameCompartment(it, f), true); 24 25 var p1 = next.call(it, {message: "initial yield"}); 26 27 // The promise object is same-compartment with |f|. 28 assertEq(isSameCompartment(p1, f), true); 29 30 // Note: This doesn't follow the spec, which requires that only |p1 instanceof Promise| is true. 31 assertEq(p1 instanceof Promise || p1 instanceof g.Promise, true); 32 33 p1.then(v => { 34 // The iterator result object is same-compartment with |f|. 35 assertEq(isSameCompartment(v, f), true); 36 assertEq(v.done, false); 37 38 assertEq(isSameCompartment(v.value, f), true); 39 assertEq(v.value.message, "yield"); 40 }); 41 42 var p2 = next.call(it, {message: "continue"}); 43 44 // The promise object is same-compartment with |f|. 45 assertEq(isSameCompartment(p2, f), true); 46 47 // Note: This doesn't follow the spec, which requires that only |p2 instanceof Promise| is true. 48 assertEq(p2 instanceof Promise || p2 instanceof g.Promise, true); 49 50 p2.then(v => { 51 // The iterator result object is same-compartment with |f|. 52 assertEq(isSameCompartment(v, f), true); 53 assertEq(v.done, true); 54 55 assertEq(isSameCompartment(v.value, f), true); 56 assertEq(v.value.message, "return"); 57 }); 58 59 var p3 = next.call(it, {message: "already finished"}); 60 61 // The promise object is same-compartment with |f|. 62 assertEq(isSameCompartment(p3, f), true); 63 64 // Note: This doesn't follow the spec, which requires that only |p3 instanceof Promise| is true. 65 assertEq(p3 instanceof Promise || p3 instanceof g.Promise, true); 66 67 p3.then(v => { 68 // The iterator result object is same-compartment with |f|. 69 assertEq(isSameCompartment(v, f), true); 70 assertEq(v.done, true); 71 assertEq(v.value, undefined); 72 }); 73 74 var p4 = next.call({}, {message: "bad |this| argument"}); 75 76 // The promise object is same-compartment with |next|. 77 assertEq(isSameCompartment(p4, next), true); 78 79 // Only in this case we're following the spec and are creating the promise object 80 // in the correct realm. 81 assertEq(p4 instanceof Promise, true); 82 83 p4.then(() => { 84 throw new Error("expected a TypeError"); 85 }, e => { 86 assertEq(e instanceof TypeError, true); 87 }); 88 89 if (typeof reportCompare === "function") 90 reportCompare(0, 0);