Repository: servo/servo
File: resources/debugger.js (JavaScript)

Code before:
});

function handlePauseFrame(frame, is_breakpoint) {
    // Get the pipeline ID for this debuggee
    const pipelineId = debuggeesToPipelineIds.get(frame.script.global);
    if (!pipelineId) {
        console.error("[debugger] No pipeline ID for frame's global");
        return undefined;
    }

    // TODO: Some properties throw if terminated is true
    // TODO: Check if start line / column is correct or we need the proper breakpoint
    const result = {
        // TODO: arguments: frame.arguments,
        column: frame.script.startColumn,
        displayName: frame.script.displayName,
        line: frame.script.startLine,
        onStack: frame.onStack,
        oldest: frame.older == null,
        terminated: frame.terminated,
        type_: frame.type,
        url: frame.script.url,
    };

    // Notify devtools and enter pause loop. This blocks until Resume.
    pauseFrame(pipelineId, result, is_breakpoint);

    // <https://firefox-source-docs.mozilla.org/js/Debugger/Conventions.html#resumption-values>
    // Return undefined to continue execution normally after resume.
    return undefined;
}

addEventListener("setBreakpoint", event => {
    const {spidermonkeyId, scriptId, offset} = event;
    const script = sourceIdsToScripts.get(spidermonkeyId);
    const target = findScriptById(script, scriptId);
    if (target) {
        target.setBreakpoint(offset, {
            // <https://firefox-source-docs.mozilla.org/js/Debugger/Debugger.Script.html#setbreakpoint-offset-handler>
            // The hit handler receives a Debugger.Frame instance representing the currently executing stack frame.
            hit: (frame) => handlePauseFrame(frame, true)
        });
    }
});

// <https://firefox-source-docs.mozilla.org/js/Debugger/Debugger.Frame.html>
addEventListener("interrupt", event => {
    dbg.onEnterFrame = function(frame) {
        dbg.onEnterFrame = undefined;
        handlePauseFrame(frame, false);
    };

Review comment (bug):
as we discuseed, let's keep debugger.js as close to firefox as possible.
- pauseFrame should be PauseAndRespond
- handlePauseFrame should be HandlePauseAndRespond
- Add docs to PauseAndRespond

Code after:
});

function handlePauseAndRespond(frame, is_breakpoint) {
    // Get the pipeline ID for this debuggee
    const pipelineId = debuggeesToPipelineIds.get(frame.script.global);
    if (!pipelineId) {
        console.error("[debugger] No pipeline ID for frame's global");
        return undefined;
    }

    // TODO: Some properties throw if terminated is true
    const result = {
        // TODO: arguments: frame.arguments,
        column: frame.script.startColumn,
        displayName: frame.script.displayName,
        line: frame.script.startLine,
        onStack: frame.onStack,
        oldest: frame.older == null,
        terminated: frame.terminated,
        type_: frame.type,
        url: frame.script.url,
    };

    // Notify devtools and enter pause loop. This blocks until Resume.
    pauseAndRespond(pipelineId, result, is_breakpoint);

    // <https://firefox-source-docs.mozilla.org/js/Debugger/Conventions.html#resumption-values>
    // Return undefined to continue execution normally after resume.
    return undefined;
}

addEventListener("setBreakpoint", event => {
    const {spidermonkeyId, scriptId, offset} = event;
    const script = sourceIdsToScripts.get(spidermonkeyId);
    const target = findScriptById(script, scriptId);
    if (target) {
        target.setBreakpoint(offset, {
            // <https://firefox-source-docs.mozilla.org/js/Debugger/Debugger.Script.html#setbreakpoint-offset-handler>
            // The hit handler receives a Debugger.Frame instance representing the currently executing stack frame.
            hit: (frame) => handlePauseAndRespond(frame, true)
        });
    }
});

// <https://firefox-source-docs.mozilla.org/js/Debugger/Debugger.Frame.html>
addEventListener("interrupt", event => {
    dbg.onEnterFrame = function(frame) {
        dbg.onEnterFrame = undefined;
        handlePauseAndRespond(frame, false);
    };
});