Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Next #20

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open

Next #20

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 73 additions & 53 deletions InjectedScript/DebuggerScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,6 @@ DebuggerScript.getAfterCompileScript = function(eventData)
return DebuggerScript._formatScript(eventData.script_.script_);
}

DebuggerScript.getWorkerScripts = function()
{
var result = [];
var scripts = Debug.scripts();
for (var i = 0; i < scripts.length; ++i) {
var script = scripts[i];
// Workers don't share same V8 heap now so there is no need to complicate stuff with
// the context id like we do to discriminate between scripts from different pages.
// However we need to filter out v8 native scripts.
if (script.context_data && script.context_data === "worker")
result.push(DebuggerScript._formatScript(script));
}
return result;
}

DebuggerScript.getFunctionScopes = function(fun)
{
var mirror = MakeMirror(fun);
Expand All @@ -89,6 +74,31 @@ DebuggerScript.getFunctionScopes = function(fun)
return result;
}

DebuggerScript.getGeneratorObjectDetails = function(object)
{
var mirror = MakeMirror(object, true /* transient */);
if (!mirror.isGenerator())
return null;
var funcMirror = mirror.func();
if (!funcMirror.resolved())
return null;
var result = {
"function": funcMirror.value(),
"functionName": DebuggerScript._displayFunctionName(funcMirror) || "",
"status": mirror.status()
};
var script = funcMirror.script();
var location = mirror.sourceLocation() || funcMirror.sourceLocation();
if (script && location) {
result["location"] = {
"scriptId": String(script.id()),
"lineNumber": location.line,
"columnNumber": location.column
};
}
return result;
}

DebuggerScript.getCollectionEntries = function(object)
{
var mirror = MakeMirror(object, true /* transient */);
Expand All @@ -103,20 +113,6 @@ DebuggerScript.getCollectionEntries = function(object)
}
}

DebuggerScript.getInternalProperties = function(value)
{
var properties = ObjectMirror.GetInternalProperties(value);
var result = [];
for (var i = 0; i < properties.length; i++) {
var mirror = properties[i];
result.push({
name: mirror.name(),
value: mirror.value().value()
});
}
return result;
}

DebuggerScript.setFunctionVariableValue = function(functionValue, scopeIndex, variableName, newValue)
{
var mirror = MakeMirror(functionValue);
Expand All @@ -134,24 +130,24 @@ DebuggerScript._setScopeVariableValue = function(scopeHolder, scopeIndex, variab
return undefined;
}

DebuggerScript.getScripts = function(contextData)
DebuggerScript.getScripts = function(contextGroupId)
{
var result = [];

if (!contextData)
return result;
var comma = contextData.indexOf(",");
if (comma === -1)
return result;
// Context data is a string in the following format:
// ("page"|"injected")","<page id>
var idSuffix = contextData.substring(comma); // including the comma

var scripts = Debug.scripts();
var contextDataPrefix = null;
if (contextGroupId)
contextDataPrefix = contextGroupId + ",";
for (var i = 0; i < scripts.length; ++i) {
var script = scripts[i];
if (script.context_data && script.context_data.lastIndexOf(idSuffix) != -1)
result.push(DebuggerScript._formatScript(script));
if (contextDataPrefix) {
if (!script.context_data)
continue;
// Context data is a string in the following format:
// <id>","("page"|"injected"|"worker")
if (script.context_data.indexOf(contextDataPrefix) !== 0)
continue;
}
result.push(DebuggerScript._formatScript(script));
}
return result;
}
Expand Down Expand Up @@ -183,7 +179,8 @@ DebuggerScript._formatScript = function(script)
startColumn: script.column_offset,
endLine: endLine,
endColumn: endColumn,
isContentScript: !!script.context_data && script.context_data.indexOf("injected") == 0
isContentScript: !!script.context_data && script.context_data.endsWith(",injected"),
isInternalScript: script.is_debugger_script
};
}

Expand Down Expand Up @@ -276,6 +273,11 @@ DebuggerScript.stepOutOfFunction = function(execState, callFrame)
execState.prepareStep(Debug.StepAction.StepOut, 1);
}

DebuggerScript.clearStepping = function()
{
Debug.clearStepping();
}

// Returns array in form:
// [ 0, <v8_result_report> ] in case of success
// or [ 1, <general_error_message>, <compiler_message>, <line_number>, <column_number> ] in case of compile error, numbers are 1-based.
Expand All @@ -296,7 +298,7 @@ DebuggerScript.liveEditScriptSource = function(scriptId, newSource, preview)
var changeLog = [];
try {
var result = Debug.LiveEdit.SetScriptSource(scriptToEdit, newSource, preview, changeLog);
return [0, result];
return [0, result.stack_modified];
} catch (e) {
if (e instanceof Debug.LiveEdit.Failure && "details" in e) {
var details = e.details;
Expand Down Expand Up @@ -357,6 +359,17 @@ DebuggerScript.isEvalCompilation = function(eventData)
return (script.compilationType() === Debug.ScriptCompilationType.Eval);
}

DebuggerScript._displayFunctionName = function(funcMirror)
{
if (!funcMirror.resolved())
return undefined
var displayName;
var valueMirror = funcMirror.property("displayName").value();
if (valueMirror && valueMirror.isString())
displayName = valueMirror.value();
return displayName || funcMirror.name() || funcMirror.inferredName();
}

// NOTE: This function is performance critical, as it can be run on every
// statement that generates an async event (like addEventListener) to support
// asynchronous call stacks. Thus, when possible, initialize the data lazily.
Expand Down Expand Up @@ -458,14 +471,19 @@ DebuggerScript._frameMirrorToJSCallFrame = function(frameMirror, callerFrame, sc

function functionName()
{
var func = ensureFuncMirror();
if (!func.resolved())
return undefined;
var displayName;
var valueMirror = func.property("displayName").value();
if (valueMirror && valueMirror.isString())
displayName = valueMirror.value();
return displayName || func.name() || func.inferredName();
return DebuggerScript._displayFunctionName(ensureFuncMirror());
}

function functionLine()
{
var location = ensureFuncMirror().sourceLocation();
return location ? location.line : 0;
}

function functionColumn()
{
var location = ensureFuncMirror().sourceLocation();
return location ? location.column : 0;
}

function evaluate(expression, scopeExtension)
Expand All @@ -475,7 +493,7 @@ DebuggerScript._frameMirrorToJSCallFrame = function(frameMirror, callerFrame, sc

function restart()
{
return Debug.LiveEdit.RestartFrame(frameMirror);
return frameMirror.restart();
}

function setVariableValue(scopeNumber, variableName, newValue)
Expand Down Expand Up @@ -511,6 +529,8 @@ DebuggerScript._frameMirrorToJSCallFrame = function(frameMirror, callerFrame, sc
"column": column,
"scriptName": scriptName,
"functionName": functionName,
"functionLine": functionLine,
"functionColumn": functionColumn,
"thisObject": thisObject,
"scopeChain": lazyScopeChain,
"scopeType": lazyScopeTypes,
Expand Down
85 changes: 78 additions & 7 deletions InjectedScript/InjectedScriptHost.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,104 @@
"use strict";

(function (binding, DebuggerScript) {
function InjectedScriptHost() {}

var lastBoundObjectId = 0;
var idToWrappedObject = new Map();
var idToObjectGroupName = new Map();
var nameToObjectGroup = new Map();

function InjectedScriptHost() {
}

InjectedScriptHost.prototype = binding.InjectedScriptHost;

InjectedScriptHost.prototype.bind = function(value, groupName) {
if (lastBoundObjectId <= 0)
lastBoundObjectId = 1;

var id = lastBoundObjectId++;
idToWrappedObject.set(id, value);

if (id < 0) return;
if (groupName == null) return id;

idToObjectGroupName.set(id, groupName);

if (!nameToObjectGroup.has(groupName))
nameToObjectGroup.set(groupName, [id]);
else
nameToObjectGroup.get(groupName).push(id);

return id;
};

InjectedScriptHost.prototype.unbind = function(id) {
idToWrappedObject.delete(id);
idToObjectGroupName.delete(id);
};

InjectedScriptHost.prototype.releaseObject = function(objectId) {
var parsedObjectId;
try {
parsedObjectId = JSON.parse(objectId);
} catch (e) { return; }

this.unbind(parsedObjectId.id);
};

InjectedScriptHost.prototype.releaseObjectGroup = function(groupName) {
if (!groupName) return;

var group = nameToObjectGroup.get(groupName);
if (!group) return;

group.forEach(function(id) {
this.unbind(id);
}, this);

nameToObjectGroup.delete(groupName);
};

InjectedScriptHost.prototype.objectForId = function(id) {
if (!Number(id)) return;
return idToWrappedObject.get(id);
};

InjectedScriptHost.prototype.idToObjectGroupName = function(id) {
if (!Number(id)) return;
return idToObjectGroupName.get(id) || '';
}

InjectedScriptHost.prototype.isHTMLAllCollection = function(object) {
//We don't have `all` collection in NodeJS
return false;
};

InjectedScriptHost.prototype.isDOMWrapper = function(object) {
return false;
};

InjectedScriptHost.prototype.suppressWarningsAndCallFunction = function(func, receiver, args) {
return this.callFunction(func, receiver, args);
};

InjectedScriptHost.prototype.functionDetails = function(fun) {
var details = this.functionDetailsWithoutScopes(fun);
var scopes = DebuggerScript.getFunctionScopes(fun);

if (scopes && scopes.length) {
details.rawScopes = scopes;
}

return details;
};

InjectedScriptHost.prototype.getInternalProperties = function(value) {
return DebuggerScript.getInternalProperties(value);
InjectedScriptHost.prototype.generatorObjectDetails = function(object) {
return DebuggerScript.getGeneratorObjectDetails(object);
};


InjectedScriptHost.prototype.collectionEntries = function(object) {
return DebuggerScript.getCollectionEntries(object);
};

return new InjectedScriptHost();
});
Loading