<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=969786

Debugger.Source.prototype.introductionScript and .introductionOffset should
behave when 'eval' is called with no scripted frames active at all.
-->
<head>
  <meta charset="utf-8">
  <title>Debugger.Source.prototype.introductionScript with no caller</title>
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
</head>
<body>
<pre id="test">
<script>
"use strict";

const {addDebuggerToGlobal} = ChromeUtils.importESModule("resource://gre/modules/jsdebugger.sys.mjs");
addDebuggerToGlobal(globalThis);

window.onload = function() {
  SimpleTest.waitForExplicitFinish();

  let dbg, iframeDO, doc;

  // Create an iframe to debug.
  const iframe = document.createElement("iframe");
  iframe.src = "data:text/html,<div>Hi!</div>";
  iframe.onload = onLoadHandler;
  document.body.appendChild(iframe);

  function onLoadHandler() {
    // Now that the iframe's window has been created, we can add
    // it as a debuggee.
    dbg = new Debugger();
    iframeDO = dbg.addDebuggee(iframe.contentWindow);

    doc = iframe.contentWindow.document;
    const script = doc.createElement("script");
    script.text = "setTimeout(eval.bind(null, 'debugger;'), 0);";
    dbg.onDebuggerStatement = timerHandler;
    doc.body.appendChild(script);
  }

  function timerHandler(frame) {
    // The top stack frame's source should have an undefined
    // introduction script and introduction offset.
    const source = frame.script.source;
    ok(source.introductionScript === undefined,
       "setTimeout eval introductionScript is undefined");
    ok(source.introductionOffset === undefined,
       "setTimeout eval introductionOffset is undefined");

    // Check that the above isn't just some quirk of iframes, or the
    // browser milieu destroying information: an eval script should indeed
    // have proper introduction information.
    const script2 = doc.createElement("script");
    script2.text = "eval('debugger;');";
    iframeDO.makeDebuggeeValue(script2);

    dbg.onDebuggerStatement = evalHandler;
    doc.body.appendChild(script2);
  }

  function evalHandler(frame) {
    // The top stack frame's source should be introduced by the script that
    // called eval.
    const source = frame.script.source;
    const frame2 = frame.older;
    const frame3 = frame2.older;

    ok(source.introductionType === "eval",
       "top frame's source was introduced by 'eval'");
    ok(source.introductionScript === frame2.script,
       "eval frame's introduction script is the older frame's script");
    ok(source.introductionOffset === frame2.offset,
       "eval frame's introduction offset is current offset in older frame");

    // The frame that called eval, in turn, was introduced at the call that
    // inserted the script element into the document.
    ok(frame2.script.source.introductionType === "injectedScript",
       "older frame has no introduction type");
    ok(frame2.script.source.introductionScript === frame3.script,
       "older frame has introduction script");
    ok(frame2.script.source.introductionOffset === frame3.offset,
       "older frame has introduction offset");

    SimpleTest.finish();
  }
};
</script>
</pre>
</body>
</html>
