Re: JavaScriptException during JSNI call: Invoking an instance method on a null instance
Thanks ChrisK, You saved my day. On Tuesday, October 23, 2012 3:23:21 PM UTC+2, ChrisK wrote: There were a few problems here. My first example calls a zero args function but is not formatted correctly. If you call something like this: th...@com.mine.PlayerImpl::callback(); It won't work. It needs the method signature and parameters separated (no args signature doesn't imply no args last time I checked in GWT): th...@com.mine.PlayerImpl::callback()(); Notice the additional set of brackets. Very briefly mentioned on https://developers.google.com/web-toolkit/doc/latest/DevGuideCodingBasicsJSNI . For the final code, I ended up passing this as a parameter from Java and also wrapping the JS callback function so it ends up looking like this: public PlayerImpl() { initialiseCallbacks(this); } private native void initialiseCallbacks(PlayerImpl impl) /*-{ $wnd.js_callback = $entry(function() { im...@com.mine.PlayerImpl::callback()(); }); } I hope that helps. On Monday, 22 October 2012 15:23:43 UTC+1, Kartik Kaipa wrote: Hey did you finally get this working. I am doing the exact same thing I pass in the instance of the object as a parameter in the function method Could you post the working code that you have. Would really apreciate it. Thanks, Kartik On Tuesday, March 23, 2010 7:31:24 PM UTC+5:30, Olivier Monaco wrote: ChrisK, For instance method, you need to reference the method from the instance like this.@ Here is my exact code that compile: private native void installListener() /*-{ var callback = $entry(this. @net.antidot.gwtcommon.mvp.share.client.channel.HTML5MessageChannel::process(Lnet/ antidot/gwtcommon/mvp/share/client/channel/HTML5MessageEvent;)); $wnd.addEventListener(message, (function(callback, channel) { return function(event) { callback.call(channel, event); }; })(callback, this), false);; }-*/; Sorry, I forgot that... Olivier On 23 mar, 13:58, ChrisK cknow...@gmail.com wrote: Actually, Olivier's methods work in development mode but don't seem to compile with this error: Cannot make an unqualified reference to the instance method callback I've tried the pre-assignment of this to var that and also an argument to the JSNI method passing in my instance. I've tried both of these with Olivier's first method as well as the second to avoid leaks. All with the same result - working in development mode but not when I compile. It looks to me as though the compiler doesn't understand the call method or the closure correctly. It looks at though it's trying to parse the @mypackage.MyClass::MyMethod() and seeing it's an instance method so failed (instead of looking at subsequent JavaScript). Of course I could quite easily be doing something wrong. Thomas - your method compiles fine so I guess I'll use that for now. On Mar 23, 10:07 am, ChrisK cknow...@gmail.com wrote: Ahhh ok. Sorry for the simple questions but I'm just getting my head around all this. I also have some other methods with arguments and have those working thanks to both of you. On Mar 23, 9:56 am, Olivier Monaco olivier.mon...@free.fr wrote: ChrisK, On 23 mar, 10:32, ChrisK cknow...@gmail.com wrote: Thomas - your solution worked from 6 worked but at first I didn't include both pairs of brackets after the function call MyMethod but it turns out they are required. With only one set (i.e. no arguments), it just doesn't work. I thought that was legal but maybe not. Remember that @mypackage.MyClass::MyMethod() is a reference to the function, not a call. The parenthesis allow you to give the full signature of the method. Java allows two methods to have the same name but different parameters. GWT needs the full signature to find which method you want to reference, even is there is only one method with this name. The second parenthesis are to call the method. If you forgot then, you just obtain the function object and do nothing with it... Olivier a\@a -- You received this message because you are subscribed to the Google Groups Google Web Toolkit group. To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscr...@googlegroups.com. To post to this group, send email to google-web-toolkit@googlegroups.com. Visit this group at http://groups.google.com/group/google-web-toolkit. For more options, visit https://groups.google.com/groups/opt_out.
Re: JavaScriptException during JSNI call: Invoking an instance method on a null instance
Thanks ChrisK,, You've done a smart job.. It is working. On Tuesday, October 23, 2012 6:53:21 PM UTC+5:30, ChrisK wrote: There were a few problems here. My first example calls a zero args function but is not formatted correctly. If you call something like this: th...@com.mine.PlayerImpl::callback(); It won't work. It needs the method signature and parameters separated (no args signature doesn't imply no args last time I checked in GWT): th...@com.mine.PlayerImpl::callback()(); Notice the additional set of brackets. Very briefly mentioned on https://developers.google.com/web-toolkit/doc/latest/DevGuideCodingBasicsJSNI . For the final code, I ended up passing this as a parameter from Java and also wrapping the JS callback function so it ends up looking like this: public PlayerImpl() { initialiseCallbacks(this); } private native void initialiseCallbacks(PlayerImpl impl) /*-{ $wnd.js_callback = $entry(function() { im...@com.mine.PlayerImpl::callback()(); }); } I hope that helps. On Monday, 22 October 2012 15:23:43 UTC+1, Kartik Kaipa wrote: Hey did you finally get this working. I am doing the exact same thing I pass in the instance of the object as a parameter in the function method Could you post the working code that you have. Would really apreciate it. Thanks, Kartik On Tuesday, March 23, 2010 7:31:24 PM UTC+5:30, Olivier Monaco wrote: ChrisK, For instance method, you need to reference the method from the instance like this.@ Here is my exact code that compile: private native void installListener() /*-{ var callback = $entry(this. @net.antidot.gwtcommon.mvp.share.client.channel.HTML5MessageChannel::process(Lnet/ antidot/gwtcommon/mvp/share/client/channel/HTML5MessageEvent;)); $wnd.addEventListener(message, (function(callback, channel) { return function(event) { callback.call(channel, event); }; })(callback, this), false);; }-*/; Sorry, I forgot that... Olivier On 23 mar, 13:58, ChrisK cknow...@gmail.com wrote: Actually, Olivier's methods work in development mode but don't seem to compile with this error: Cannot make an unqualified reference to the instance method callback I've tried the pre-assignment of this to var that and also an argument to the JSNI method passing in my instance. I've tried both of these with Olivier's first method as well as the second to avoid leaks. All with the same result - working in development mode but not when I compile. It looks to me as though the compiler doesn't understand the call method or the closure correctly. It looks at though it's trying to parse the @mypackage.MyClass::MyMethod() and seeing it's an instance method so failed (instead of looking at subsequent JavaScript). Of course I could quite easily be doing something wrong. Thomas - your method compiles fine so I guess I'll use that for now. On Mar 23, 10:07 am, ChrisK cknow...@gmail.com wrote: Ahhh ok. Sorry for the simple questions but I'm just getting my head around all this. I also have some other methods with arguments and have those working thanks to both of you. On Mar 23, 9:56 am, Olivier Monaco olivier.mon...@free.fr wrote: ChrisK, On 23 mar, 10:32, ChrisK cknow...@gmail.com wrote: Thomas - your solution worked from 6 worked but at first I didn't include both pairs of brackets after the function call MyMethod but it turns out they are required. With only one set (i.e. no arguments), it just doesn't work. I thought that was legal but maybe not. Remember that @mypackage.MyClass::MyMethod() is a reference to the function, not a call. The parenthesis allow you to give the full signature of the method. Java allows two methods to have the same name but different parameters. GWT needs the full signature to find which method you want to reference, even is there is only one method with this name. The second parenthesis are to call the method. If you forgot then, you just obtain the function object and do nothing with it... Olivier a\@a -- You received this message because you are subscribed to the Google Groups Google Web Toolkit group. To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscr...@googlegroups.com. To post to this group, send email to google-web-toolkit@googlegroups.com. Visit this group at http://groups.google.com/group/google-web-toolkit?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Re: JavaScriptException during JSNI call: Invoking an instance method on a null instance
There were a few problems here. My first example calls a zero args function but is not formatted correctly. If you call something like this: th...@com.mine.PlayerImpl::callback(); It won't work. It needs the method signature and parameters separated (no args signature doesn't imply no args last time I checked in GWT): th...@com.mine.PlayerImpl::callback()(); Notice the additional set of brackets. Very briefly mentioned on https://developers.google.com/web-toolkit/doc/latest/DevGuideCodingBasicsJSNI. For the final code, I ended up passing this as a parameter from Java and also wrapping the JS callback function so it ends up looking like this: public PlayerImpl() { initialiseCallbacks(this); } private native void initialiseCallbacks(PlayerImpl impl) /*-{ $wnd.js_callback = $entry(function() { im...@com.mine.PlayerImpl::callback()(); }); } I hope that helps. On Monday, 22 October 2012 15:23:43 UTC+1, Kartik Kaipa wrote: Hey did you finally get this working. I am doing the exact same thing I pass in the instance of the object as a parameter in the function method Could you post the working code that you have. Would really apreciate it. Thanks, Kartik On Tuesday, March 23, 2010 7:31:24 PM UTC+5:30, Olivier Monaco wrote: ChrisK, For instance method, you need to reference the method from the instance like this.@ Here is my exact code that compile: private native void installListener() /*-{ var callback = $entry(this. @net.antidot.gwtcommon.mvp.share.client.channel.HTML5MessageChannel::process(Lnet/ antidot/gwtcommon/mvp/share/client/channel/HTML5MessageEvent;)); $wnd.addEventListener(message, (function(callback, channel) { return function(event) { callback.call(channel, event); }; })(callback, this), false);; }-*/; Sorry, I forgot that... Olivier On 23 mar, 13:58, ChrisK cknow...@gmail.com wrote: Actually, Olivier's methods work in development mode but don't seem to compile with this error: Cannot make an unqualified reference to the instance method callback I've tried the pre-assignment of this to var that and also an argument to the JSNI method passing in my instance. I've tried both of these with Olivier's first method as well as the second to avoid leaks. All with the same result - working in development mode but not when I compile. It looks to me as though the compiler doesn't understand the call method or the closure correctly. It looks at though it's trying to parse the @mypackage.MyClass::MyMethod() and seeing it's an instance method so failed (instead of looking at subsequent JavaScript). Of course I could quite easily be doing something wrong. Thomas - your method compiles fine so I guess I'll use that for now. On Mar 23, 10:07 am, ChrisK cknow...@gmail.com wrote: Ahhh ok. Sorry for the simple questions but I'm just getting my head around all this. I also have some other methods with arguments and have those working thanks to both of you. On Mar 23, 9:56 am, Olivier Monaco olivier.mon...@free.fr wrote: ChrisK, On 23 mar, 10:32, ChrisK cknow...@gmail.com wrote: Thomas - your solution worked from 6 worked but at first I didn't include both pairs of brackets after the function call MyMethod but it turns out they are required. With only one set (i.e. no arguments), it just doesn't work. I thought that was legal but maybe not. Remember that @mypackage.MyClass::MyMethod() is a reference to the function, not a call. The parenthesis allow you to give the full signature of the method. Java allows two methods to have the same name but different parameters. GWT needs the full signature to find which method you want to reference, even is there is only one method with this name. The second parenthesis are to call the method. If you forgot then, you just obtain the function object and do nothing with it... Olivier a\@a -- You received this message because you are subscribed to the Google Groups Google Web Toolkit group. To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/dkFgPXKZEGoJ. To post to this group, send email to google-web-toolkit@googlegroups.com. To unsubscribe from this group, send email to google-web-toolkit+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
Re: JavaScriptException during JSNI call: Invoking an instance method on a null instance
Hey did you finally get this working. I am doing the exact same thing I pass in the instance of the object as a parameter in the function method Could you post the working code that you have. Would really apreciate it. Thanks, Kartik On Tuesday, March 23, 2010 7:31:24 PM UTC+5:30, Olivier Monaco wrote: ChrisK, For instance method, you need to reference the method from the instance like this.@ Here is my exact code that compile: private native void installListener() /*-{ var callback = $entry(this. @net.antidot.gwtcommon.mvp.share.client.channel.HTML5MessageChannel::process(Lnet/ antidot/gwtcommon/mvp/share/client/channel/HTML5MessageEvent;)); $wnd.addEventListener(message, (function(callback, channel) { return function(event) { callback.call(channel, event); }; })(callback, this), false);; }-*/; Sorry, I forgot that... Olivier On 23 mar, 13:58, ChrisK cknow...@gmail.com wrote: Actually, Olivier's methods work in development mode but don't seem to compile with this error: Cannot make an unqualified reference to the instance method callback I've tried the pre-assignment of this to var that and also an argument to the JSNI method passing in my instance. I've tried both of these with Olivier's first method as well as the second to avoid leaks. All with the same result - working in development mode but not when I compile. It looks to me as though the compiler doesn't understand the call method or the closure correctly. It looks at though it's trying to parse the @mypackage.MyClass::MyMethod() and seeing it's an instance method so failed (instead of looking at subsequent JavaScript). Of course I could quite easily be doing something wrong. Thomas - your method compiles fine so I guess I'll use that for now. On Mar 23, 10:07 am, ChrisK cknow...@gmail.com wrote: Ahhh ok. Sorry for the simple questions but I'm just getting my head around all this. I also have some other methods with arguments and have those working thanks to both of you. On Mar 23, 9:56 am, Olivier Monaco olivier.mon...@free.fr wrote: ChrisK, On 23 mar, 10:32, ChrisK cknow...@gmail.com wrote: Thomas - your solution worked from 6 worked but at first I didn't include both pairs of brackets after the function call MyMethod but it turns out they are required. With only one set (i.e. no arguments), it just doesn't work. I thought that was legal but maybe not. Remember that @mypackage.MyClass::MyMethod() is a reference to the function, not a call. The parenthesis allow you to give the full signature of the method. Java allows two methods to have the same name but different parameters. GWT needs the full signature to find which method you want to reference, even is there is only one method with this name. The second parenthesis are to call the method. If you forgot then, you just obtain the function object and do nothing with it... Olivier a\@a -- You received this message because you are subscribed to the Google Groups Google Web Toolkit group. To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/fYxsGBUXHN0J. To post to this group, send email to google-web-toolkit@googlegroups.com. To unsubscribe from this group, send email to google-web-toolkit+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
Re: JavaScriptException during JSNI call: Invoking an instance method on a null instance
Hi Thomas, The problem with memory leaks in browser is that it's sometimes hard to understand where they come from. Here, we are writting a little piece of code that will be transformed (replacement of @...()) and incorporated into a more complexe code. I don't want to look at the generated code to check if leaks are possibile. So, I always use the syntax I give so that I'm sure there will not be any leak... It's some type of discipline. Using a static method to inject the code seems to me being the same thing... Olivier On 23 mar, 00:07, Thomas Broyer t.bro...@gmail.com wrote: On 22 mar, 23:42, Olivier Monaco olivier.mon...@free.fr wrote: The call method of a JavaScript function allows you to call it with a differente this. Then, using $wnd.js_callback.call(otherObject), the this variable reference otherObject. So the glue is: var that = this; $wnd.js_callback = function() { $entry(@mypackage.MyClass::MyMethod()).call(that); } When js_callback will be called, MyMethod is called on that, the JavaScript object representing the MyClass instance. Finally, to avoid memory leaks of closure: $wnd.js_callback = (function(obj) { return function() { $entry(@mypackage.MyClass::MyMethod()).call(obj); }; })(this); ... or more simply: var that = this; $wnd.js_callback = $entry(function() { that. @mypackage.MyClass::MyMethod()(); }); (this is what I should have written previously, sorry for the buggy code) As for the leak, are you sure there really is one once the code is translated into JS? would it be better if the inject method were a static one with the instance being passed as an argument (no use of the thiskeyword within JSNI)? -- You received this message because you are subscribed to the Google Groups Google Web Toolkit group. To post to this group, send email to google-web-tool...@googlegroups.com. To unsubscribe from this group, send email to google-web-toolkit+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
Re: JavaScriptException during JSNI call: Invoking an instance method on a null instance
Hi, thanks for the answers again! Ok, I have some working code now! I think my issues were a combined not understand this became the $wnd object when written how I had it, plus not understanding what $entry actually does. Having said that the docs on $entry could be better than the few sentences given. Incidentally, I had some similar code where I had tried wrapping in function() { } but hadn't gotten that working. Olivier - your solutions worked first time for me. Thomas - your solution worked from 6 worked but at first I didn't include both pairs of brackets after the function call MyMethod but it turns out they are required. With only one set (i.e. no arguments), it just doesn't work. I thought that was legal but maybe not. Now I just have to decide which method is best. I think I prefer passing in the instance as a parameter of the inject method - I just prefer the argument to be strongly typed on the Java side (being primarily a Java developer). Olivier's code to work against memory leaks also seems sensible to me. I have a static method which does both of these now and works fine and think I understand the area a lot better thanks to you two. It does seem like a lot to write just to get a callback though! Maybe some decent examples like this would be useful in the GWT documentation pages? On Mar 23, 8:30 am, Olivier Monaco olivier.mon...@free.fr wrote: Hi Thomas, The problem with memory leaks in browser is that it's sometimes hard to understand where they come from. Here, we are writting a little piece of code that will be transformed (replacement of @...()) and incorporated into a more complexe code. I don't want to look at the generated code to check if leaks are possibile. So, I always use the syntax I give so that I'm sure there will not be any leak... It's some type of discipline. Using a static method to inject the code seems to me being the same thing... Olivier On 23 mar, 00:07, Thomas Broyer t.bro...@gmail.com wrote: On 22 mar, 23:42, Olivier Monaco olivier.mon...@free.fr wrote: The call method of a JavaScript function allows you to call it with a differente this. Then, using $wnd.js_callback.call(otherObject), the this variable reference otherObject. So the glue is: var that = this; $wnd.js_callback = function() { $entry(@mypackage.MyClass::MyMethod()).call(that); } When js_callback will be called, MyMethod is called on that, the JavaScript object representing the MyClass instance. Finally, to avoid memory leaks of closure: $wnd.js_callback = (function(obj) { return function() { $entry(@mypackage.MyClass::MyMethod()).call(obj); }; })(this); ... or more simply: var that = this; $wnd.js_callback = $entry(function() { that. @mypackage.MyClass::MyMethod()(); }); (this is what I should have written previously, sorry for the buggy code) As for the leak, are you sure there really is one once the code is translated into JS? would it be better if the inject method were a static one with the instance being passed as an argument (no use of the thiskeyword within JSNI)? -- You received this message because you are subscribed to the Google Groups Google Web Toolkit group. To post to this group, send email to google-web-tool...@googlegroups.com. To unsubscribe from this group, send email to google-web-toolkit+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
Re: JavaScriptException during JSNI call: Invoking an instance method on a null instance
ChrisK, On 23 mar, 10:32, ChrisK cknow...@gmail.com wrote: Thomas - your solution worked from 6 worked but at first I didn't include both pairs of brackets after the function call MyMethod but it turns out they are required. With only one set (i.e. no arguments), it just doesn't work. I thought that was legal but maybe not. Remember that @mypackage.MyClass::MyMethod() is a reference to the function, not a call. The parenthesis allow you to give the full signature of the method. Java allows two methods to have the same name but different parameters. GWT needs the full signature to find which method you want to reference, even is there is only one method with this name. The second parenthesis are to call the method. If you forgot then, you just obtain the function object and do nothing with it... Olivier -- You received this message because you are subscribed to the Google Groups Google Web Toolkit group. To post to this group, send email to google-web-tool...@googlegroups.com. To unsubscribe from this group, send email to google-web-toolkit+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
Re: JavaScriptException during JSNI call: Invoking an instance method on a null instance
Ahhh ok. Sorry for the simple questions but I'm just getting my head around all this. I also have some other methods with arguments and have those working thanks to both of you. On Mar 23, 9:56 am, Olivier Monaco olivier.mon...@free.fr wrote: ChrisK, On 23 mar, 10:32, ChrisK cknow...@gmail.com wrote: Thomas - your solution worked from 6 worked but at first I didn't include both pairs of brackets after the function call MyMethod but it turns out they are required. With only one set (i.e. no arguments), it just doesn't work. I thought that was legal but maybe not. Remember that @mypackage.MyClass::MyMethod() is a reference to the function, not a call. The parenthesis allow you to give the full signature of the method. Java allows two methods to have the same name but different parameters. GWT needs the full signature to find which method you want to reference, even is there is only one method with this name. The second parenthesis are to call the method. If you forgot then, you just obtain the function object and do nothing with it... Olivier -- You received this message because you are subscribed to the Google Groups Google Web Toolkit group. To post to this group, send email to google-web-tool...@googlegroups.com. To unsubscribe from this group, send email to google-web-toolkit+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
Re: JavaScriptException during JSNI call: Invoking an instance method on a null instance
Actually, Olivier's methods work in development mode but don't seem to compile with this error: Cannot make an unqualified reference to the instance method callback I've tried the pre-assignment of this to var that and also an argument to the JSNI method passing in my instance. I've tried both of these with Olivier's first method as well as the second to avoid leaks. All with the same result - working in development mode but not when I compile. It looks to me as though the compiler doesn't understand the call method or the closure correctly. It looks at though it's trying to parse the @mypackage.MyClass::MyMethod() and seeing it's an instance method so failed (instead of looking at subsequent JavaScript). Of course I could quite easily be doing something wrong. Thomas - your method compiles fine so I guess I'll use that for now. On Mar 23, 10:07 am, ChrisK cknow...@gmail.com wrote: Ahhh ok. Sorry for the simple questions but I'm just getting my head around all this. I also have some other methods with arguments and have those working thanks to both of you. On Mar 23, 9:56 am, Olivier Monaco olivier.mon...@free.fr wrote: ChrisK, On 23 mar, 10:32, ChrisK cknow...@gmail.com wrote: Thomas - your solution worked from 6 worked but at first I didn't include both pairs of brackets after the function call MyMethod but it turns out they are required. With only one set (i.e. no arguments), it just doesn't work. I thought that was legal but maybe not. Remember that @mypackage.MyClass::MyMethod() is a reference to the function, not a call. The parenthesis allow you to give the full signature of the method. Java allows two methods to have the same name but different parameters. GWT needs the full signature to find which method you want to reference, even is there is only one method with this name. The second parenthesis are to call the method. If you forgot then, you just obtain the function object and do nothing with it... Olivier -- You received this message because you are subscribed to the Google Groups Google Web Toolkit group. To post to this group, send email to google-web-tool...@googlegroups.com. To unsubscribe from this group, send email to google-web-toolkit+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
Re: JavaScriptException during JSNI call: Invoking an instance method on a null instance
ChrisK, For instance method, you need to reference the method from the instance like th...@ Here is my exact code that compile: private native void installListener() /*-{ var callback = $entry(this. @net.antidot.gwtcommon.mvp.share.client.channel.HTML5MessageChannel::process(Lnet/ antidot/gwtcommon/mvp/share/client/channel/HTML5MessageEvent;)); $wnd.addEventListener(message, (function(callback, channel) { return function(event) { callback.call(channel, event); }; })(callback, this), false);; }-*/; Sorry, I forgot that... Olivier On 23 mar, 13:58, ChrisK cknow...@gmail.com wrote: Actually, Olivier's methods work in development mode but don't seem to compile with this error: Cannot make an unqualified reference to the instance method callback I've tried the pre-assignment of this to var that and also an argument to the JSNI method passing in my instance. I've tried both of these with Olivier's first method as well as the second to avoid leaks. All with the same result - working in development mode but not when I compile. It looks to me as though the compiler doesn't understand the call method or the closure correctly. It looks at though it's trying to parse the @mypackage.MyClass::MyMethod() and seeing it's an instance method so failed (instead of looking at subsequent JavaScript). Of course I could quite easily be doing something wrong. Thomas - your method compiles fine so I guess I'll use that for now. On Mar 23, 10:07 am, ChrisK cknow...@gmail.com wrote: Ahhh ok. Sorry for the simple questions but I'm just getting my head around all this. I also have some other methods with arguments and have those working thanks to both of you. On Mar 23, 9:56 am, Olivier Monaco olivier.mon...@free.fr wrote: ChrisK, On 23 mar, 10:32, ChrisK cknow...@gmail.com wrote: Thomas - your solution worked from 6 worked but at first I didn't include both pairs of brackets after the function call MyMethod but it turns out they are required. With only one set (i.e. no arguments), it just doesn't work. I thought that was legal but maybe not. Remember that @mypackage.MyClass::MyMethod() is a reference to the function, not a call. The parenthesis allow you to give the full signature of the method. Java allows two methods to have the same name but different parameters. GWT needs the full signature to find which method you want to reference, even is there is only one method with this name. The second parenthesis are to call the method. If you forgot then, you just obtain the function object and do nothing with it... Olivier a...@a -- You received this message because you are subscribed to the Google Groups Google Web Toolkit group. To post to this group, send email to google-web-tool...@googlegroups.com. To unsubscribe from this group, send email to google-web-toolkit+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
Re: JavaScriptException during JSNI call: Invoking an instance method on a null instance
My above code got a little mangled along the way, the $entry part has (this, followed by a full stop, followed by @com.mine.PlayerImpl::callback()); -- You received this message because you are subscribed to the Google Groups Google Web Toolkit group. To post to this group, send email to google-web-tool...@googlegroups.com. To unsubscribe from this group, send email to google-web-toolkit+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
Re: JavaScriptException during JSNI call: Invoking an instance method on a null instance
On Mar 22, 4:23 pm, ChrisK cknow...@gmail.com wrote: My above code got a little mangled along the way, the $entry part has (this, followed by a full stop, followed by @com.mine.PlayerImpl::callback()); And that's the problem: this is a special keyword and will evaluate to the this context of the calling plain JS code, whereas you'd want it to be some PlayerImpl instance. There are several ways to fix this: // evaluates this right now var that = this; // then uses that $wnd.js_callback = $entry(t...@com.mine.playerimpl::callback()); or pass the instance as an argument to the method and then use its name. But there's another bug in your code: there's no this in a static method! -- You received this message because you are subscribed to the Google Groups Google Web Toolkit group. To post to this group, send email to google-web-tool...@googlegroups.com. To unsubscribe from this group, send email to google-web-toolkit+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
Re: JavaScriptException during JSNI call: Invoking an instance method on a null instance
Thanks for the reply! I'm not sure I understand why this evaluates to the plain JS context. I was going off the documentation here which seems to suggest you can use this in this way: http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsJSNI.html#methods-fields Or perhaps you just can't use it for $entry type JSNI? Having said that, I've updated to a non-static method - silly me. I did actually try that first but only pasted in my last attempt at getting some working code. I have tried both of the below now with the same stack trace as a result. Note, there are not actually any spaces before the .@ in my code I'm just working around Google Groups mangling it. private native void initialiseCallbacks() /*-{ var that = this; $wnd.js_callback = $entry(that @com.mine.playerimpl::callback()); }-*/; and private native void initialiseCallbacks(PlayerImpl pl) /*-{ $wnd.js_callback = $entry(pl @com.mine.playerimpl::callback()); }-*/; On Mar 22, 5:17 pm, Thomas Broyer t.bro...@gmail.com wrote: On Mar 22, 4:23 pm, ChrisK cknow...@gmail.com wrote: My above code got a little mangled along the way, the $entry part has (this, followed by a full stop, followed by @com.mine.PlayerImpl::callback()); And that's the problem: this is a special keyword and will evaluate to the this context of the calling plain JS code, whereas you'd want it to be some PlayerImpl instance. There are several ways to fix this: // evaluates this right now var that = this; // then uses that $wnd.js_callback = $entry(t...@com.mine.playerimpl::callback()); or pass the instance as an argument to the method and then use its name. But there's another bug in your code: there's no this in a static method! -- You received this message because you are subscribed to the Google Groups Google Web Toolkit group. To post to this group, send email to google-web-tool...@googlegroups.com. To unsubscribe from this group, send email to google-web-toolkit+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
Re: JavaScriptException during JSNI call: Invoking an instance method on a null instance
Hi ChrisK, You need to understand how JavaScript works. Like in many languages, the this keyword means the current object. The JavaScript language allows you to manipulate code as a function. This function can be affected to any variable or object member, like: $wnd.js_callback = function() { ... } Then, you can call your code like this: $wnd.js_callback() In the function body, the this variable is $wnd. If you assign the same function to the member of another object, the this variable will be equal to the new object. From JSNI, @mypackage.MyClass::MyMethod() references the JavaScript function corresponding to the MyMethod method of mypackage.MyClass. This method can be static or not, the reference is the same (no need of this before @... just to obtain the JavaScript function). Calling $entry just add a function wrapper around it but it stay a function. When you assign this function to $wnd.js_callback, the this variable inside of the function will be $wnd. If MyMethod is static, it's okay. If MyMethod is not static, GWT will probably cast the $wnd to a MyClass... it's bad... The call method of a JavaScript function allows you to call it with a differente this. Then, using $wnd.js_callback.call(otherObject), the this variable reference otherObject. So the glue is: var that = this; $wnd.js_callback = function() { $entry(@mypackage.MyClass::MyMethod()).call(that); } When js_callback will be called, MyMethod is called on that, the JavaScript object representing the MyClass instance. Finally, to avoid memory leaks of closure: $wnd.js_callback = (function(obj) { return function() { $entry(@mypackage.MyClass::MyMethod()).call(obj); }; })(this); And remember, for static methods it's just: $wnd.js_callback = $entry(...); Good luck, Olivier On 22 mar, 20:16, ChrisK cknow...@gmail.com wrote: Thanks for the reply! I'm not sure I understand why this evaluates to the plain JS context. I was going off the documentation here which seems to suggest you can use this in this way:http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsJSNI... Or perhaps you just can't use it for $entry type JSNI? Having said that, I've updated to a non-static method - silly me. I did actually try that first but only pasted in my last attempt at getting some working code. I have tried both of the below now with the same stack trace as a result. Note, there are not actually any spaces before the .@ in my code I'm just working around Google Groups mangling it. private native void initialiseCallbacks() /*-{ var that = this; $wnd.js_callback = $entry(that @com.mine.playerimpl::callback()); }-*/; and private native void initialiseCallbacks(PlayerImpl pl) /*-{ $wnd.js_callback = $entry(pl @com.mine.playerimpl::callback()); }-*/; On Mar 22, 5:17 pm, Thomas Broyer t.bro...@gmail.com wrote: On Mar 22, 4:23 pm, ChrisK cknow...@gmail.com wrote: My above code got a little mangled along the way, the $entry part has (this, followed by a full stop, followed by @com.mine.PlayerImpl::callback()); And that's the problem: this is a special keyword and will evaluate to the this context of the calling plain JS code, whereas you'd want it to be some PlayerImpl instance. There are several ways to fix this: // evaluates this right now var that = this; // then uses that $wnd.js_callback = $entry(t...@com.mine.playerimpl::callback()); or pass the instance as an argument to the method and then use its name. But there's another bug in your code: there's no this in a static method! -- You received this message because you are subscribed to the Google Groups Google Web Toolkit group. To post to this group, send email to google-web-tool...@googlegroups.com. To unsubscribe from this group, send email to google-web-toolkit+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
Re: JavaScriptException during JSNI call: Invoking an instance method on a null instance
On 22 mar, 23:42, Olivier Monaco olivier.mon...@free.fr wrote: The call method of a JavaScript function allows you to call it with a differente this. Then, using $wnd.js_callback.call(otherObject), the this variable reference otherObject. So the glue is: var that = this; $wnd.js_callback = function() { $entry(@mypackage.MyClass::MyMethod()).call(that); } When js_callback will be called, MyMethod is called on that, the JavaScript object representing the MyClass instance. Finally, to avoid memory leaks of closure: $wnd.js_callback = (function(obj) { return function() { $entry(@mypackage.MyClass::MyMethod()).call(obj); }; })(this); ... or more simply: var that = this; $wnd.js_callback = $entry(function() { that. @mypackage.MyClass::MyMethod()(); }); (this is what I should have written previously, sorry for the buggy code) As for the leak, are you sure there really is one once the code is translated into JS? would it be better if the inject method were a static one with the instance being passed as an argument (no use of the thiskeyword within JSNI)? -- You received this message because you are subscribed to the Google Groups Google Web Toolkit group. To post to this group, send email to google-web-tool...@googlegroups.com. To unsubscribe from this group, send email to google-web-toolkit+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.