dart-lang/js-interop — pull request #91 by a14n (2013-06-24 20:51:35+00:00)
Title: FunctionProxy dart->javascript->dart round trip type error

I have a Dart function that receives calls from Javascript. The call are initiated in Javascript and include a callback. The dart function looks like this, e.g:

void dartFuncCalledFromJS(var anArg, js.FunctionProxy callback) {
callback(aResult);
}

That works fine if I initiate the call from javascript, including the callback.

I attempted to write a unit that to verify, from Dart, the operation of this code. This means that rather the normal call origination which is Javascript, the call to "dartFuncCalledFromJS" originates in a Dart unit test. From Dart, I can call "myFuncCalledFromJS" as follows:

callback(var result) { print("$result"); "
js.context.dartFuncCalledFromJS("aValue", new js.Callback.once(callback).toJs());

Note - the toJs() is optional, it runs the same with or without it. I added it so that I explicitly use a a type that matches the declaration of "dartFuncFromJS".

In this case, the call to "dartFuncCalledFromJS" will fail due to a type mismatch error. Keep in mind, that it works fine when the call originals from Javascript. The VM error is:

type '(List) => dynamic' is not a subtype of type 'FunctionProxy' of 'cb'

In the Dart debugger the callback type that I see in dartFuncCalledFromJS is:

Closure: (dynamic, List) => dynamic when called via dart->javascript->dart, fails

but is ....

js.FunctionProxy when called via javascript->dart, works

In the js-interop code it looked like a serialization problem related to the unusual round tripping of the FunctionProxy that is happening here.