vox-core
Version:
Runtime de aplicaciones multiplataforma
126 lines (113 loc) • 2.94 kB
JavaScript
var consts= core.org.voxsoftware.Lpr.Constants;
/**
* Representa un método estático o no estático de .NET
* @memberof VW.Clr
* @constructor
* @name Function
* @param {Object} options
* @protected
*/
var Func= module.exports= function(args){
if(args.static){
this.typename= args.typename;
this.static=true;
}
this.type= args.type;
this.manager= this.type.manager;
this.method= true;
this.name= args.name
this.index= args.index
}
/**
* Obtener una función que permite invocar el método que representa
* <br>Esta función es llamada automáticamente al ejecutar
* {@link VW.Clr.Type.method} o {@link VW.Clr.Type.staticMethod}
* y su valor se coloca una propiedad
* dentro del objeto si es estático o del prototipo
* si es de instancia
* @name func
* @func
* @memberof VW.Clr.Function.prototype
* @return {Function}
* @example
* ClrType.staticMethod("AnyMethod") // Carga el método en el tipo
* var funcClr= ClrType.m.AnyMethod
* var task= funcClr.func()(arg1,arg2,...);
* task.then(function(){
* //OK
* }, function(er){
* // Error
* })
* @example
* ClrType.staticMethod("AnyMethod") // Carga el método en el tipo
* var task= ClrType.AnyMethod(arg1,arg2,...);
* task.then(function(){
* //OK
* }, function(er){
* // Error
* })
*/
Func.prototype.func= function(){
var self= this;
var f= function(){
var native= self.nativeJS!==undefined?self.nativeJS:!!this.native
return self.invoke(this, arguments, native);
}
return f;
}
/**
* Ejecutar el método .NET representado.
* <br>Este método es asíncrono
* @name invoke
* @func
* @memberof VW.Clr.Function.prototype
* @param {VW.Clr.Type} obj Nulo si es un método estático
* @param {Array} args Un array con los argumentos a pasar en la invocación del método
* @param {Boolean} [native] Si es `true`, el resultado es convertido a un objeto JavaScript plano o a su valor primitivo
* @return {VW.Task} Tarea asíncrona. Al finalizar devuelve el resultado de la invocación
* @example
* ClrType.staticMethod("AnyMethod") // Carga el método en el tipo
* var task= ClrType.m.AnyMethod.invoke(null, [arg1, ...]);
* task.then(function(value){
* //OK
* console.log(value)
* }, function(er){
* // Error
* })
*/
Func.prototype.invoke= function(obj, args, native){
var ipc= this.manager.ipc;
var id= (++ipc.commandid);
var opt={};
opt.c= consts.InvokeMethod;
if(this.static){
opt.typename= this.typename;
}else{
opt.objectid=obj.CSid;
}
if(this.index)
opt.nameindex= this.index+1
else
opt.name=this.name;
opt.arguments= ipc.processArgs(args);
opt.commandid= id;
if(native){
opt.command="get";
}
//ipc.setCallback(callback,id);
ipc.setObject(obj,id);
var task= ipc.send(opt);
var scope= obj?obj.scope:this.type.scope
if(scope){
this.type.scope=null
task.beforeExpose(function(){
if(task.result){
if(task.result.CSid){
scope.include(task.result)
task.result.scope=scope
}
}
})
}
return task
}