[DevTools] Added RemoteArray and RemoteFunction wrappers

RemoteArray wrapper incapsulates at and length methods. RemoteFunction incapsulates targetFunction method. Wrappers have createFromObject methods which check type of object.
In utilities.js method storeResultTo is added to Promise.prototype. This method allow to save promise result value to object[propertyName] and return it for following processing.

BUG=61643
R=pfeldman@chromium.org

Review URL: https://codereview.chromium.org/1325783003

git-svn-id: svn://svn.chromium.org/blink/trunk@201653 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent 113abf6c
...@@ -1499,3 +1499,16 @@ Promise.prototype.catchException = function(defaultValue) { ...@@ -1499,3 +1499,16 @@ Promise.prototype.catchException = function(defaultValue) {
return defaultValue; return defaultValue;
}); });
} }
/**
* @param {!Object} object
* @param {*} propertyName
* @param {T} result
* @return {T}
* @template T
*/
function storeResultTo(object, propertyName, result)
{
object[propertyName] = result;
return result;
}
...@@ -28,6 +28,11 @@ ...@@ -28,6 +28,11 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
/**
* @typedef {{object: ?WebInspector.RemoteObject, wasThrown: (boolean|undefined)}}
*/
WebInspector.CallFunctionResult;
/** /**
* This may not be an interface due to "instanceof WebInspector.RemoteObject" checks in the code. * This may not be an interface due to "instanceof WebInspector.RemoteObject" checks in the code.
* *
...@@ -174,7 +179,7 @@ WebInspector.RemoteObject.prototype = { ...@@ -174,7 +179,7 @@ WebInspector.RemoteObject.prototype = {
/** /**
* @param {function(this:Object, ...)} functionDeclaration * @param {function(this:Object, ...)} functionDeclaration
* @param {!Array.<!RuntimeAgent.CallArgument>=} args * @param {!Array<!RuntimeAgent.CallArgument>=} args
* @param {function(?WebInspector.RemoteObject, boolean=)=} callback * @param {function(?WebInspector.RemoteObject, boolean=)=} callback
*/ */
callFunction: function(functionDeclaration, args, callback) callFunction: function(functionDeclaration, args, callback)
...@@ -184,15 +189,15 @@ WebInspector.RemoteObject.prototype = { ...@@ -184,15 +189,15 @@ WebInspector.RemoteObject.prototype = {
/** /**
* @param {function(this:Object, ...)} functionDeclaration * @param {function(this:Object, ...)} functionDeclaration
* @param {!Array.<!RuntimeAgent.CallArgument>=} args * @param {!Array<!RuntimeAgent.CallArgument>=} args
* @return {!Promise.<!{object: ?WebInspector.RemoteObject, wasThrown: (boolean|undefined)}>} * @return {!Promise<!WebInspector.CallFunctionResult>}
*/ */
callFunctionPromise: function(functionDeclaration, args) callFunctionPromise: function(functionDeclaration, args)
{ {
return new Promise(promiseConstructor.bind(this)); return new Promise(promiseConstructor.bind(this));
/** /**
* @param {function(!{object: ?WebInspector.RemoteObject, wasThrown: (boolean|undefined)})} success * @param {function(!WebInspector.CallFunctionResult)} success
* @this {WebInspector.RemoteObject} * @this {WebInspector.RemoteObject}
*/ */
function promiseConstructor(success) function promiseConstructor(success)
...@@ -201,7 +206,7 @@ WebInspector.RemoteObject.prototype = { ...@@ -201,7 +206,7 @@ WebInspector.RemoteObject.prototype = {
} }
/** /**
* @param {function(!{object: ?WebInspector.RemoteObject, wasThrown: (boolean|undefined)})} callback * @param {function(!WebInspector.CallFunctionResult)} callback
* @param {?WebInspector.RemoteObject} object * @param {?WebInspector.RemoteObject} object
* @param {boolean=} wasThrown * @param {boolean=} wasThrown
*/ */
...@@ -215,7 +220,7 @@ WebInspector.RemoteObject.prototype = { ...@@ -215,7 +220,7 @@ WebInspector.RemoteObject.prototype = {
}, },
/** /**
* @param {function(this:Object)} functionDeclaration * @param {function(this:Object, ...)} functionDeclaration
* @param {!Array<!RuntimeAgent.CallArgument>|undefined} args * @param {!Array<!RuntimeAgent.CallArgument>|undefined} args
* @param {function(*)} callback * @param {function(*)} callback
*/ */
...@@ -225,7 +230,7 @@ WebInspector.RemoteObject.prototype = { ...@@ -225,7 +230,7 @@ WebInspector.RemoteObject.prototype = {
}, },
/** /**
* @param {function(this:Object)} functionDeclaration * @param {function(this:Object, ...)} functionDeclaration
* @param {!Array<!RuntimeAgent.CallArgument>|undefined} args * @param {!Array<!RuntimeAgent.CallArgument>|undefined} args
* @return {!Promise<*>} * @return {!Promise<*>}
*/ */
...@@ -1336,6 +1341,180 @@ WebInspector.LocalJSONObject.prototype = { ...@@ -1336,6 +1341,180 @@ WebInspector.LocalJSONObject.prototype = {
__proto__: WebInspector.RemoteObject.prototype __proto__: WebInspector.RemoteObject.prototype
} }
/**
* @constructor
* @param {!WebInspector.RemoteObject} object
*/
WebInspector.RemoteArray = function(object)
{
this._object = object;
};
/**
* @param {?WebInspector.RemoteObject} object
* @return {!WebInspector.RemoteArray}
*/
WebInspector.RemoteArray.objectAsArray = function(object)
{
if (!object || object.subtype !== "array")
throw new Error("Object is empty or not an array");
return new WebInspector.RemoteArray(object);
}
/**
* @param {!Array<!WebInspector.RemoteObject>} objects
* @return {!Promise<!WebInspector.RemoteArray>}
*/
WebInspector.RemoteArray.createFromRemoteObjects = function(objects)
{
if (!objects.length)
throw new Error("Input array is empty");
var objectArguments = [];
for (var i = 0; i < objects.length; ++i)
objectArguments.push(WebInspector.RemoteObject.toCallArgument(objects[i]))
return objects[0].callFunctionPromise(createArray, objectArguments).then(returnRemoteArray);
/**
* @return {!Array<*>}
*/
function createArray()
{
if (arguments.length > 1)
return new Array(arguments);
return [arguments[0]];
}
/**
* @param {!WebInspector.CallFunctionResult} result
* @return {!WebInspector.RemoteArray}
*/
function returnRemoteArray(result)
{
if (result.wasThrown || !result.object)
throw new Error("Call function throws exceptions or returns empty value");
return WebInspector.RemoteArray.objectAsArray(result.object);
}
}
WebInspector.RemoteArray.prototype = {
/**
* @param {number} index
* @return {!Promise<!WebInspector.RemoteObject>}
*/
at: function(index)
{
if (index < 0 || index > this._object.arrayLength())
throw new Error("Out of range");
return this._object.callFunctionPromise(at, [WebInspector.RemoteObject.toCallArgument(index)]).then(assertCallFunctionResult);
/**
* @suppressReceiverCheck
* @param {number} index
* @return {*}
* @this {!Object}
*/
function at(index)
{
return this[index];
}
/**
* @param {!WebInspector.CallFunctionResult} result
* @return {!WebInspector.RemoteObject}
*/
function assertCallFunctionResult(result)
{
if (result.wasThrown || !result.object)
throw new Error("Exception in callFunction or result value is empty");
return result.object;
}
},
/**
* @return {number}
*/
length: function()
{
return this._object.arrayLength();
},
/**
* @param {function(!WebInspector.RemoteObject):!Promise<T>} func
* @return {!Promise<!Array<T>>}
* @template T
*/
map: function(func)
{
var promises = [];
for (var i = 0; i < this.length(); ++i)
promises.push(this.at(i).then(func));
return Promise.all(promises);
},
/**
* @return {!WebInspector.RemoteObject}
*/
object: function()
{
return this._object;
}
}
/**
* @constructor
* @param {!WebInspector.RemoteObject} object
*/
WebInspector.RemoteFunction = function(object)
{
this._object = object;
}
/**
* @param {?WebInspector.RemoteObject} object
* @return {!WebInspector.RemoteFunction}
*/
WebInspector.RemoteFunction.objectAsFunction = function(object)
{
if (!object || object.type !== "function")
throw new Error("Object is empty or not a function");
return new WebInspector.RemoteFunction(object);
}
WebInspector.RemoteFunction.prototype = {
/**
* @return {!Promise<!WebInspector.RemoteObject>}
*/
targetFunction: function()
{
return this._object.getOwnPropertiesPromise().then(targetFunction.bind(this));
/**
* @param {!{properties: ?Array<!WebInspector.RemoteObjectProperty>, internalProperties: ?Array<!WebInspector.RemoteObjectProperty>}} ownProperties
* @return {!WebInspector.RemoteObject}
* @this {WebInspector.RemoteFunction}
*/
function targetFunction(ownProperties)
{
if (!ownProperties.internalProperties)
return this._object;
var internalProperties = ownProperties.internalProperties;
for (var property of internalProperties) {
if (property.name === "[[TargetFunction]]")
return property.value;
}
return this._object;
}
},
/**
* @return {!WebInspector.RemoteObject}
*/
object: function()
{
return this._object;
}
}
/** /**
* @constructor * @constructor
* @extends {WebInspector.LocalJSONObject} * @extends {WebInspector.LocalJSONObject}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment