JsInterop receipts for simple JSNI code ?

301 views
Skip to first unread message

Hristo Stoyanov

unread,
May 24, 2016, 7:34:17 PM5/24/16
to GWT Users
Does any one know (/has easy receipts for) how to replace the following JSNI code with JsInterop code :


public static native String getJSVariableAsString(String jsVarName) /*-{
        return $wnd[jsVarName].toString();
}-*/;

private static native boolean isJSVariableDefined(String jsVarName)/*-{
        return !!$wnd[jsVarName];
}-*/;

Jens

unread,
May 24, 2016, 8:11:39 PM5/24/16
to GWT Users
You still need to use JSNI for accessing these properties. Depending on the API you want to build you could define @JsOverlay methods inside @JsType(native = true) classes and let them delegate to a JSNI based utility class.

-- J.

Hristo Stoyanov

unread,
May 25, 2016, 3:21:26 PM5/25/16
to GWT Users
Jens,
Thanks, so apparently JsInterop cannot be a complete replacement of JSNI?

I was hoping to be able to wrap in @JsType(native = true) something like Object.keys(window)

Hristo Stoyanov

unread,
May 25, 2016, 3:35:07 PM5/25/16
to GWT Users
Actually, it might be possible to do it with JsInterop only:

class Globals {

        @JsMethod(namespace=GLOBAL)
        public native Object eval(String expresion);
        
        @JsOverlay
        public native boolean isVariableDefined(String varName){
             return Boolean.TRUE.equals(eval("!!window['"+varName+"']"));
        }

}

I guess, you can also use JSON.safeEval() ... but we dont know if will survive  in J2CL.

Paul Stockley

unread,
May 25, 2016, 4:30:05 PM5/25/16
to GWT Users
Eval is really slow. I would use JSNI. Eventually I think J2CL will have a way to execute javascript. Just isolate the JSNI in a helper class so it can easily be replaced.

Goktug Gokdogan

unread,
May 26, 2016, 6:35:36 PM5/26/16
to google-web-toolkit
JsInterop will provide some base classes for stuff that are not possible with JsInterop annotations.

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-tool...@googlegroups.com.
To post to this group, send email to google-we...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

Paul Stockley

unread,
May 27, 2016, 9:02:50 AM5/27/16
to GWT Users
Will this be for J2CL only?


On Thursday, May 26, 2016 at 6:35:36 PM UTC-4, Goktug Gokdogan wrote:
JsInterop will provide some base classes for stuff that are not possible with JsInterop annotations.
On Wed, May 25, 2016 at 1:30 PM, Paul Stockley <pstoc...@gmail.com> wrote:
Eval is really slow. I would use JSNI. Eventually I think J2CL will have a way to execute javascript. Just isolate the JSNI in a helper class so it can easily be replaced.


On Wednesday, May 25, 2016 at 3:35:07 PM UTC-4, Hristo Stoyanov wrote:
Actually, it might be possible to do it with JsInterop only:

class Globals {

        @JsMethod(namespace=GLOBAL)
        public native Object eval(String expresion);
        
        @JsOverlay
        public native boolean isVariableDefined(String varName){
             return Boolean.TRUE.equals(eval("!!window['"+varName+"']"));
        }

}

I guess, you can also use JSON.safeEval() ... but we dont know if will survive  in J2CL.


On Wednesday, May 25, 2016 at 12:21:26 PM UTC-7, Hristo Stoyanov wrote:
Jens,
Thanks, so apparently JsInterop cannot be a complete replacement of JSNI?

I was hoping to be able to wrap in @JsType(native = true) something like Object.keys(window)

On Tuesday, May 24, 2016 at 5:11:39 PM UTC-7, Jens wrote:
You still need to use JSNI for accessing these properties. Depending on the API you want to build you could define @JsOverlay methods inside @JsType(native = true) classes and let them delegate to a JSNI based utility class.

-- J.

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsub...@googlegroups.com.

Goktug Gokdogan

unread,
Jun 25, 2016, 2:23:56 AM6/25/16
to google-web-toolkit
Sorry I didn't see this question earlier. It will work with both GWT and J2CL; that will be one of the main benefits.

On Fri, May 27, 2016 at 6:02 AM, Paul Stockley <pstoc...@gmail.com> wrote:
Will this be for J2CL only?

On Thursday, May 26, 2016 at 6:35:36 PM UTC-4, Goktug Gokdogan wrote:
JsInterop will provide some base classes for stuff that are not possible with JsInterop annotations.
On Wed, May 25, 2016 at 1:30 PM, Paul Stockley <pstoc...@gmail.com> wrote:
Eval is really slow. I would use JSNI. Eventually I think J2CL will have a way to execute javascript. Just isolate the JSNI in a helper class so it can easily be replaced.


On Wednesday, May 25, 2016 at 3:35:07 PM UTC-4, Hristo Stoyanov wrote:
Actually, it might be possible to do it with JsInterop only:

class Globals {

        @JsMethod(namespace=GLOBAL)
        public native Object eval(String expresion);
        
        @JsOverlay
        public native boolean isVariableDefined(String varName){
             return Boolean.TRUE.equals(eval("!!window['"+varName+"']"));
        }

}

I guess, you can also use JSON.safeEval() ... but we dont know if will survive  in J2CL.


On Wednesday, May 25, 2016 at 12:21:26 PM UTC-7, Hristo Stoyanov wrote:
Jens,
Thanks, so apparently JsInterop cannot be a complete replacement of JSNI?

I was hoping to be able to wrap in @JsType(native = true) something like Object.keys(window)

On Tuesday, May 24, 2016 at 5:11:39 PM UTC-7, Jens wrote:
You still need to use JSNI for accessing these properties. Depending on the API you want to build you could define @JsOverlay methods inside @JsType(native = true) classes and let them delegate to a JSNI based utility class.

-- J.

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-tool...@googlegroups.com.
To post to this group, send email to google-we...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-tool...@googlegroups.com.

Thomas Broyer

unread,
Jun 25, 2016, 4:10:17 AM6/25/16
to GWT Users
Reply all
Reply to author
Forward
0 new messages