javascript - DartObject not unwrapped when passed into JsObject.callMethod -
i'm writing dart wrapper js library, hitting on problem:
i'm calling js method mediastream
(dart)object parameter. problem is, inside js lib, parameter dartobject
, , causes error because it's not mediastream
anymore.
jsobject.jsify
works on maps , lists, there way jsify object js usage?
after few time think problem, solution mirror api. it's possible create jsobject, approach has been create "binding" object make interface between js , dart. mirror api able analyze object instance create dynamicly binding. hope usefull
the dart file :
import 'dart:js'; import 'dart:mirrors'; typedef dynamic oncall(list); class varargsfunction extends function { oncall _oncall; varargsfunction(this._oncall); call() => _oncall([]); nosuchmethod(invocation invocation) { final arguments = invocation.positionalarguments; return _oncall(arguments); } } jsobject tojsobject(var inst) { instancemirror im = reflect(inst); classmirror classmirror = im.type; jsobject jsobj = new jsobject(context["object"]); classmirror.declarations.values.where((dm) => dm methodmirror && dm.isregularmethod).foreach((methodmirror method) { jsobj[mirrorsystem.getname(method.simplename)] = new varargsfunction((args) { return im.invoke(method.simplename, args).reflectee; }); }); return jsobj; } class testclass { string str; testclass(this.str); string getstr() { return this.str; } string tostring() { return '[myclass instance str: "${this.str}"]'; } jsobject tojs() { var jsobj = new jsobject(context['object']); jsobj["getstr"] = this.getstr; return jsobj; } } void main() { jsobject jsinst = context['jsinst']; print(jsinst); testclass tmp = new testclass(jsinst.callmethod("getmyattr")); tojsobject(tmp); jsinst.callmethod("printdartobj", [tojsobject(tmp)]); }
the js file :
function myjsclass() { this.my_attr = "salut"; } myjsclass.prototype.getmyattr = function() { return this.my_attr; } myjsclass.prototype.printdartobj = function(obj) { console.log(obj.tostring()); console.log(obj.getstr()); } var jsinst = new myjsclass(); console.log(jsinst);
the output :
[object object]
[myclass instance str: "salut"]
salut
Comments
Post a Comment