blob: 9dcbe7ad5e170b425f8acf9c1593d00edd47eedb [file] [log] [blame]
/*
* weinre is available under *either* the terms of the modified BSD license *or* the
* MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
*
* Copyright (c) 2010, 2011 IBM Corporation
*/
requireClass ./Ex
requireClass ./Callback
//-----------------------------------------------------------------------------
class IDLTools
throw new Ex(arguments, "this class is not intended to be instantiated")
//-----------------------------------------------------------------------------
init
var IDLs = {}
IDLTools._idls = IDLs
//-----------------------------------------------------------------------------
static method addIDLs(idls)
idls.forEach(function(idl){
idl.interfaces.forEach(function(intf){
IDLs[intf.name] = intf
intf.module = idl.name
})
})
//-----------------------------------------------------------------------------
static method getIDL(name)
return IDLs[name]
//-----------------------------------------------------------------------------
static method getIDLsMatching(regex)
var results = []
for (var intfName in IDLs) {
var intf = IDLs[intfName]
if (intfName.match(regex)) {
results.push(intf)
}
}
return results
//-----------------------------------------------------------------------------
static method validateAgainstIDL(klass, interfaceName)
var intf = IDLTools.getIDL(interfaceName)
var messagePrefix = "IDL validation for " + interfaceName + ": "
if (null == intf) throw new Ex(arguments, messagePrefix + "idl not found: '" + interfaceName + "'")
var errors = []
// check that class implements all the methods
intf.methods.forEach(function(intfMethod) {
var classMethod = klass.prototype[intfMethod.name]
var printName = klass.name + "::" + intfMethod.name
if (null == classMethod) {
errors.push(messagePrefix + "method not implemented: '" + printName + "'")
return
}
if (classMethod.length != intfMethod.parameters.length) {
if (classMethod.length != intfMethod.parameters.length + 1) {
errors.push(messagePrefix + "wrong number of parameters: '" + printName + "'")
return
}
}
})
// check that class doesn't implement extra methods
for (var propertyName in klass.prototype) {
if (klass.prototype.hasOwnProperty(propertyName)) continue
if (propertyName.match(/^_.*/)) continue
var printName = klass.name + "::" + propertyName
if (!intf.methods[propertyName]) {
errors.push(messagePrefix + "method should not be implemented: '" + printName + "'")
continue
}
}
if (!errors.length) return
errors.forEach(function(error){
require("./Weinre").getClass().logError(error)
})
//-----------------------------------------------------------------------------
static method buildProxyForIDL(proxyObject, interfaceName)
var intf = IDLTools.getIDL(interfaceName)
var messagePrefix = "building proxy for IDL " + interfaceName + ": "
if (null == intf) throw new Ex(arguments, messagePrefix + "idl not found: '" + interfaceName + "'")
intf.methods.forEach(function(intfMethod) {
proxyObject[intfMethod.name] = getProxyMethod(intf, intfMethod)
})
//-----------------------------------------------------------------------------
function getProxyMethod(intf, method)
var result = function proxyMethod() {
var callbackId = null
var args = [].slice.call(arguments)
if (args.length > 0) {
if (typeof args[args.length-1] == "function") {
callbackId = Callback.register(args[args.length-1])
args = args.slice(0, args.length-1)
}
}
while (args.length < method.parameters.length) {
args.push(null)
}
args.push(callbackId)
this.__invoke(intf.name, method.name, args)
}
result.displayName = intf.name + "__" + method.name
return result