Skip to content

Commit 8e7cf7e

Browse files
committed
Expand battery example to be more explicit
1 parent 6297557 commit 8e7cf7e

1 file changed

Lines changed: 32 additions & 12 deletions

File tree

source

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87643,25 +87643,45 @@ interface <dfn>NavigatorOnLine</dfn> {
8764387643
<code>Navigator</code> object on which it is invoked. This has the following impact: <ref
8764487644
spec=BATTERY>
8764587645

87646-
<pre>&lt;!DOCTYPE html>
87646+
<pre>&lt;!-- outer.html -->
87647+
&lt;!DOCTYPE html>
8764787648
&lt;html lang="en">
87648-
&lt;title>Relevant Realm demo&lt;/title>
87649+
&lt;title>Relevant Realm demo: outer page&lt;/title>
87650+
&lt;script>
87651+
function doTest() {
87652+
const promise = navigator.getBattery().call(frames[0].navigator);
87653+
87654+
console.log(promise instanceof Promise); // logs false
87655+
console.log(promise instanceof frames[0].Promise); // logs true
87656+
87657+
frames[0].hello();
87658+
}
87659+
&lt;/script>
87660+
&lt;iframe src="inner.html" onload="doTest()">&lt;/iframe>
8764987661

87650-
&lt;iframe src="about:blank">&lt;/iframe>
87662+
&lt;!-- inner.html -->
87663+
&lt;!DOCTYPE html>
87664+
&lt;html lang="en">
87665+
&lt;title>Relevant Realm demo: inner page&lt;/title>
8765187666
&lt;script>
87652-
const promise = navigator.getBattery().call(frames[0].navigator);
87667+
function hello() {
87668+
const promise = navigator.getBattery();
8765387669

87654-
console.log(promise instanceof Promise); // logs false
87655-
console.log(promise instanceof frames[0].Promise); // logs true
87670+
console.log(promise instanceof Promise); // logs true
87671+
console.log(promise instanceof parent.Promise); // logs false
87672+
}
8765687673
&lt;/script></pre>
8765787674

8765887675
<p>If the algorithm for the <code data-x="dom-navigator-getBattery">getBattery()</code> method
87659-
had instead used the <span data-x="current Realm Record">current Realm</span>, the results would
87660-
be reversed. The downside of such an approach would be that all future calls to that frame's
87661-
<code data-x="dom-navigator-getBattery">getBattery()</code>, even ones from within the
87662-
<code>iframe</code> and without cross-frame shenanigans, would forever return <code
87663-
data-x="">Promise</code> objects from the outer realm. Since this is undesirable, the algorithm
87664-
instead uses the <span data-x="concept-relevant-realm">relevant Realm</span>.</p>
87676+
had instead used the <span data-x="current Realm Record">current Realm</span>, all the results
87677+
would be reversed. That is, after the first call to <code
87678+
data-x="dom-navigator-getBattery">getBattery()</code> in <code data-x="">outer.html</code>, the
87679+
<code>Navigator</code> object in <code data-x="">inner.html</code> would be permanently storing
87680+
a <code data-x="">Promise</code> object created in <code data-x="">outer.html</code>'s
87681+
<span>JavaScript realm</span>, and calls like that inside the <code data-x="">hello()</code>
87682+
function would thus return a promise from the "wrong" realm. Since this is undesirable, the
87683+
algorithm instead uses the <span data-x="concept-relevant-realm">relevant Realm</span>, giving
87684+
the sensible results indicated in the comments above.</p>
8766587685
</div>
8766687686

8766787687
<hr>

0 commit comments

Comments
 (0)