I have a javascript API that takes a canvas context as an argument
The following
var context2dJs = new js.JsObject.fromBrowserObject(canvas.getContext('2d'));
throws Exception: Uncaught Error: object must be an Node, ArrayBuffer, Blob, ImageData, or IDBKeyRange
but the following works
var context2dJs = new js.JsObject.fromBrowserObject(canvas).callMethod('getContext', ['2d']);
However, designing a wrapper around this API, I'd like the dart API to be similar and have a CanvasRenderingContext parameter. How can I convert such dart parameter to its javascript equivalent?
Related
Is there any way to call a JavaScript function named call() (in a nested object) from Dart or do I have to wait for Dart 2.0 from which the special handling of call() might get removed?
I have a JS Proxy like:
#JS()
class SomethingFancy {
external String call();
}
But as call() can be used to turn an object into a function, it makes it impossible to access the function of the JS object.
If I could, I would change the name of the method in Dart, but that's not supported by package:js:
/// By default the dart name is used. It is not valid to specify a custom
/// [name] for class instance members.
The error I get is:
Uncaught Error: NoSuchMethodError: method not found: 'call$0' (J.getSomethingFancy$1$x(...).call$0 is not a function)
If the function didn't exist, the error would look like this:
Uncaught Error: NoSuchMethodError: method not found: 'callMe' (receiver.callMe is not a function)
Other functions on the same object work just fine.
You can prefix call with JS$:
#JS()
class SomethingFancy {
external String JS$call();
}
JS$ can be used as prefix to allow to access to JS names that conflicts with dart keywords.
I'm trying to write a library that will make it easer for dartisans to use the SoundCloud JavaScript SDK (http://developers.soundcloud.com/docs/api/sdks#javascript).
I'm using the 'dart:js' library, and
I'm only using one class to handle the proxy.
class SCproxy {
JsObject proxy = context['SC'];
String client_id;
SCproxy(this.client_id) {}
initialize() {
proxy.callMethod('initialize', [client_id]);
}
stream(String track_id){
var track = new JsObject(proxy.callMethod('stream',[track_id]));
print(track); // track should be the soundmanager2 object that we can call '.play' on.
}
The repo I'm hosting this from is (https://github.com/darkkiero/scproxy)
My problem occurs when I try to run my 'stream' method.
main() {
SCproxy SC = new SCproxy('Your SoundCloud API client_ID');
SC.initialize();
SC.stream('/tracks/111477464');
}
When I try to grab and use the soundmanager2 object returned by the javascript 'SC.stream' method, the dart editor gives me this exception :
Breaking on exception: type 'ScriptElement' is not a subtype of type 'JsFunction' of 'constructor'.
I am under the impression that I should be able to get the dart JsObject for the soundmanager2 object by collecting the callback of the 'SC.stream', But I'm not sure how.However I could be completely misusing 'dart:js' and that would be helpful information as well.
You don't seem to follow the SoundCloud JavaScript SDK documentation. Particularly for the stream method that takes a callback as parameter and doesn't return.
The following Dart code :
context['SC'].callMethod('stream', ['/tracks/293', (sound) {
sound.callMethod('play');
}]);
will do the same as this JS code :
SC.stream("/tracks/293", function(sound){
sound.play();
});
You can have a look at Using JavaScript from Dart for more explanations.
I have some confusion debugging some simple app that uses the Web Audio API.
In the developer console I can do something like this:
var ctx = new webkitAudioContext(),
osc = ctx.createOscillator();
osc.connect(ctx.destination);
osc.start(0);
Trying to get this to work with Dart yields the following errors when I try it like this:
AudioContext ctx = new AudioContext();
OscillatorNode osc = ctx.createOscillator();
osc.connect(ctx.destination);
osc.start(0);
//Dart2JS: Uncaught TypeError: Object #<OscillatorNode> has no method 'connect$1'
//DartVM: Class 'OscillatorNode' has no instance method 'connect' with matching
arguments. NoSuchMethodError: incorrect number of arguments passed to method
named connect' Receiver: Instance of 'OscillatorNode'
Stepping through I found that there are two kinds of implementations to the connect method. So I tried to add an extra second param and since I can not really wrap my head around why it needs an int named "output", thinking maybe it is for volume I decided on the value 1 but that yields:
//Dart2JS: Uncaught Error: IndexSizeError: DOM Exception 1 flexsynth.html_bootstrap.dart.js:8698 $.main flexsynth.html_bootstrap.dart.js:8698 $$._IsolateContext.eval$1flexsynth.html_bootstrap.dart.js:565 $.startRootIsolate flexsynth.html_bootstrap.dart.js:7181 (anonymous function)
//DartVM: "Dart_IntegerToInt64 expects argument 'integer' to be non-null."
Here is where I can't figure out what to do, I think the argument is not null, it is 1.
Googling the errors only leads me to the actual Dart source code.
Is there any place that explains how to work with the dart:web_audio? What am I doing wrong?
This is because the underlying implementation seems to require the parameter input, despite it being an optional parameter. This code will work:
AudioContext ctx = new AudioContext();
OscillatorNode osc = ctx.createOscillator();
osc.connect(ctx.destination, 0, 0);
osc.start(0);
This is a known bug, you can star it here: https://code.google.com/p/dart/issues/detail?id=6728
I have written a extension which makes use of js-ctypes. I have a function in the extension binded by js-ctypes with a C library function. Now, I want to pass some data loaded from the web page to this C library via Extension (js-ctypes). How do i do it ?
I came across - https://developer.mozilla.org/en-US/docs/Code_snippets/Interaction_between_privileged_and_non-privileged_pages .
I understood the part where messages i.e strings are passed but now how do I use it pass data to my extensions' js-ctypes binded function from the web page ?
Precisely I am allocating a array in my web page js script. populate some data. Now I need to send this array/data to the C library function i.e invoke C lib function with this data
Communicate with your extension, send data as JSON (sometimes arrays are wrong serialized).
For example you can send event:
var obj = JSON.stringify({arg1: 3, arg2: "some text"});
var event = new CustomEvent("YourAddonEvent", { detail: obj });
top.dispatchEvent(event);
In extension parse JSON and you will get object with arguments.
var args = JSON.parse(event.detail);
After load library and declare function, e.g:
var func = lib.declare("YourFuncName", ctypes.default_abi, ctypes.int, ctypes.int, ctypes.char.ptr, );
you can invoke your function:
var result = func(args.arg1, args.arg2);
when i call for search function like:
TwitterResponse<TwitterSearchResultCollection> result = TwitterSearch.Search("#hastag");
it throws exception like: Error converting value 0,148 to type 'Twitterizer.TwitterSearchResultCollection'. JsonParsingException.
im using the latest version of api. is there anything that i can do to solve it?