Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global scope is always assumed to be "window" #18

Open
niloc132 opened this issue Feb 12, 2019 · 4 comments
Open

Global scope is always assumed to be "window" #18

niloc132 opened this issue Feb 12, 2019 · 4 comments

Comments

@niloc132
Copy link
Contributor

ModelHelper.createGlobalJavaType always assumes that window is the correct name to use:

  public static Type createGlobalJavaType(String packagePrefix, String globalScopeClassName) {
    Type type = new Type(NAMESPACE);
    type.setName(globalScopeClassName);
    type.setPackageName(packagePrefix);
    type.setNativeNamespace(GLOBAL_NAMESPACE);
    type.addAnnotation(
        builder()
            .type(JS_TYPE)
            .isNativeAttribute(true)
            .namespaceAttribute("")
            .nameAttribute("window")
            .build());
    return type;
  }

However, this is only correct in a case like DomGlobal, which only makes sense in a normal window context. For classes like Global, this should instead be something like self, so that it works both in windows and in workers (as well as other non-browser JS contexts).

My proposal would be to change this to invoke nameAttribute("self"), since that seems as though it should be valid in window and worker contexts alike (see [1], [2]), but perhaps there is a better, more flexible solution?

[1] https://developer.mozilla.org/en-US/docs/Web/API/Window/self
[2] https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/self

In the mean time, objects in Global can't be used. Workarounds for others who find this:

  • For Symbol, make your own jstype to represent it, with a static function instead of a constructor to create it
  • For JSON, make your own JSONType with static methods instead of instance methods
  • For NaN/Infinity/undefined and other global methods, use your own static @JsProperty-annotated fields.

In all of these cases, JsPackage.GLOBAL should be used as the namespace to avoid needing to reference window.

@gkdn
Copy link
Member

gkdn commented Feb 12, 2019

goog.global is the replacement.
self unfortunately doesn't work in node.js environments. We added goog.global to GWT earlier so we could update the code generation. (https://github.com/gwtproject/gwt/blob/4825d47eee7505346de372a159db55b18b006a2a).

@vegegoku
Copy link
Contributor

the link seems to be broken.

@tbroyer
Copy link

tbroyer commented Feb 12, 2019

gwtproject/gwt@4825d47

@realityforge
Copy link
Contributor

I would love to see a java constant added for this name such as JsType.GLOBAL with a little comment to indicate what it does. Although then we have both JsType.GLOBAL and JsType.GLOBAL. Maybe we should use the unfortunate naming convention adopted by TC39 ala JsType.GLOBAL_THIS. See https://github.com/tc39/proposal-global and https://github.com/tc39/proposal-global/blob/master/NAMING.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants