GWT JSNI Problems with onready and ontimeout

  • 1
  • Problem
  • Updated 5 years ago
  • Solved
In attempting to add callbacks to onready and ontimeout, I am receiving:

com.google.gwt.core.client.JavaScriptException: (String): soundManager: Function object expected for onready

It appears that there is some issue with onready's check of the callback via oMethod instanceof Function. Since GWT does some translation of object through JSNI, I believe it changes the function in some fundamental way such that instance of fails, despite the fact that the callback appears to be a function in the debugger. I have tried passing functions as statements and after assignment to variables both as literals and via Function constructor.

I have tried everything I can think of and am considering removing the checks that throw the exception from the API. Any advice would be appreciated.
Photo of Chris

Chris

  • 5 Posts
  • 0 Reply Likes
  • about to mod the api

Posted 5 years ago

  • 1
Photo of Chris

Chris

  • 5 Posts
  • 0 Reply Likes
Additional info: on troubleshooting in Google Chrome, I did a typeof(oMethod) in the console when a breakpoint was reached at line 1017:

if (oMethod && oMethod instanceof Function)

I get back: "function". Just to see what "Function" was, I did a typeof(Function) and got back "function." Why does it fail?

I think instanceof is sketchy and have found that it can be dependant on the scope where an object was defined. Maybe a fix would be:

if (oMethod && (oMethod instanceof Function || typeof(oMethod) === 'function')

I haven't tested this, but it seems like it might help to bulletproof the the method. you might even be able to drop the instanceof. I think it would throw an exception if oMethod is undefined.
Photo of Chris

Chris

  • 5 Posts
  • 0 Reply Likes
Heres some more stuff:


Function.constructor
function Function() { [native code] }

oMethod.constructor
function Function() { [native code] }

Function.toLocaleString()
"function Function() { [native code] }"

oMethod.toLocaleString()
"function (){
__gwt_makeJavaInvoke(0)(callback, 720898);
}"
Photo of Scott

Scott, Official Rep

  • 3873 Posts
  • 253 Reply Likes
To be safer, I'll use typeof oMethod === 'function' as that should be more liberal in terms of accepting function objects. That should also be safe against the case where oMethod is undefined, as typeof oMethod === 'undefined' can be used without throwing an error. (Referencing oMethod.something would barf, if oMethod itself was undefined.)

Just checked in this change on the V2.97a.20120513+DEV branch - all methods that checked instanceof function are now using typeof x === 'function'. Let me know if that fixes things.
Photo of Chris

Chris

  • 5 Posts
  • 0 Reply Likes
Just did a quick check on the onready method. Works great! Thanks for the quick turnaround!
Photo of Scott

Scott, Official Rep

  • 3873 Posts
  • 253 Reply Likes
Excellent, thanks for the report. I'll have this included with the next official release.
Photo of elementz

elementz

  • 8 Posts
  • 0 Reply Likes
This just came up in my new gwt-soundmanager2 wrapper. https://github.com/rcaloras/gwt-sound...

Much thanks for this fix Scott, and thank you Chris for posting it!
Photo of Scott

Scott, Official Rep

  • 3873 Posts
  • 253 Reply Likes
This has been fixed with the V2.97a.20120527 release. Thanks for the report!