js/jsd/test/jsd-test.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/jsd/test/jsd-test.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,117 @@
     1.4 +const Cc = SpecialPowers.Cc;
     1.5 +const Ci = SpecialPowers.Ci;
     1.6 +const RETURN_CONTINUE = Ci.jsdIExecutionHook.RETURN_CONTINUE;
     1.7 +const DebuggerService = Cc["@mozilla.org/js/jsd/debugger-service;1"];
     1.8 +
     1.9 +var jsd = Cc['@mozilla.org/js/jsd/debugger-service;1']
    1.10 +          .getService(Ci.jsdIDebuggerService);
    1.11 +var jsdOnAtStart = false;
    1.12 +
    1.13 +function setupJSD(test) {
    1.14 +  netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    1.15 +  jsdOnAtStart = jsd.isOn;
    1.16 +  if (jsdOnAtStart) {
    1.17 +      runTest();
    1.18 +  } else {
    1.19 +      jsd.asyncOn({ onDebuggerActivated: function() { runTest(); } });
    1.20 +  }
    1.21 +}
    1.22 +
    1.23 +// Ugly workaround: when you turn the debugger on, it will only see scripts
    1.24 +// compiled after that point. And it may be turned on asynchronously. So
    1.25 +// we put the debugged code into a separate script that gets loaded after
    1.26 +// the debugger is on.
    1.27 +function loadScript(url, element) {
    1.28 +    var script = document.createElement('script');
    1.29 +    script.type = 'text/javascript';
    1.30 +    script.src = url;
    1.31 +    script.defer = false;
    1.32 +    element.appendChild(script);
    1.33 +}
    1.34 +
    1.35 +function findScriptByFunction(name) {
    1.36 +    var script;
    1.37 +    jsd.enumerateScripts({ enumerateScript:
    1.38 +                           function(script_) {
    1.39 +                               if (script_.functionName === name) {
    1.40 +                                   script = script_;
    1.41 +                               }
    1.42 +                           }
    1.43 +                         });
    1.44 +
    1.45 +    if (typeof(script) === "undefined") {
    1.46 +        throw("Cannot find function named '" + name + "'");
    1.47 +    }
    1.48 +
    1.49 +    return script;
    1.50 +}
    1.51 +
    1.52 +// Pass in a JSD script
    1.53 +function breakOnAllLines(script) {
    1.54 +    // Map each line to a PC, and collect that set of PCs (removing
    1.55 +    // duplicates.)
    1.56 +    var pcs = {};
    1.57 +    for (i = 0; i < script.lineExtent; i++) {
    1.58 +        var jsdLine = script.baseLineNumber + i;
    1.59 +        var pc = script.lineToPc(jsdLine, Ci.jsdIScript.PCMAP_SOURCETEXT);
    1.60 +        pcs[pc] = 1;
    1.61 +    }
    1.62 +        
    1.63 +    // Set a breakpoint on each of those PCs.
    1.64 +    for (pc in pcs) {
    1.65 +        try {
    1.66 +            script.setBreakpoint(pc);
    1.67 +        } catch(e) {
    1.68 +            alert("Error setting breakpoint: " + e);
    1.69 +        }
    1.70 +    }
    1.71 +}
    1.72 +
    1.73 +// Set a breakpoint on a script, where lineno is relative to the beginning
    1.74 +// of the script (NOT the absolute line number within the file).
    1.75 +function breakOnLine(script, lineno) {
    1.76 +    breakOnAbsoluteLine(script, script.baseLineNumber + lineno);
    1.77 +}
    1.78 +
    1.79 +function breakOnAbsoluteLine(script, lineno) {
    1.80 +    var pc = script.lineToPc(lineno, Ci.jsdIScript.PCMAP_SOURCETEXT);
    1.81 +    script.setBreakpoint(pc);
    1.82 +}
    1.83 +
    1.84 +function loadPage(page) {
    1.85 +    var url;
    1.86 +    if (page.match(/^\w+:/)) {
    1.87 +        // Full URI, so just use it
    1.88 +        url = page;
    1.89 +    } else {
    1.90 +        // Treat as relative to previous page
    1.91 +        url = document.location.href.replace(/\/[^\/]*$/, "/" + page);
    1.92 +    }
    1.93 +
    1.94 +    dump("Switching to URL " + url + "\n");
    1.95 +
    1.96 +    gURLBar.value = url;
    1.97 +    gURLBar.handleCommand();
    1.98 +}
    1.99 +
   1.100 +function breakpointObserver(lines, interesting, callback) {
   1.101 +    jsd.breakpointHook = { onExecute: function(frame, type, rv) {
   1.102 +        breakpoints_hit.push(frame.line);
   1.103 +        if (frame.line in interesting) {
   1.104 +            return callback(frame, type, breakpoints_hit);
   1.105 +        } else {
   1.106 +            return RETURN_CONTINUE;
   1.107 +        }
   1.108 +    } };
   1.109 +}
   1.110 +
   1.111 +function dumpStack(frame, msg) {
   1.112 +    dump(msg + ":\n");
   1.113 +    while(frame) {
   1.114 +        var callee = frame.callee;
   1.115 +        if (callee !== null)
   1.116 +          callee = callee.jsClassName;
   1.117 +        dump("  " + frame.script.fileName + ":" + frame.line + " func=" + frame.script.functionName + " ffunc=" + frame.functionName + " callee=" + callee + " pc=" + frame.pc + "\n");
   1.118 +        frame = frame.callingFrame;
   1.119 +    }
   1.120 +}

mercurial