Skip to content
New issue

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

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

Already on GitHub? # to your account

System.getProperty doesn't emit goog.require #1

Closed
niloc132 opened this issue Jun 19, 2018 · 8 comments
Closed

System.getProperty doesn't emit goog.require #1

niloc132 opened this issue Jun 19, 2018 · 8 comments

Comments

@niloc132
Copy link
Contributor

Observed in jsinterop-base, where jsinterop.js contains the following:

/**
 * This file provides the @defines for jsinterop configuration options.
 * See InternalPreconditions.java for details.
 */

goog.provide('jsinterop');

// Note that disabling checking only disables it for production.

/** @define {string} */
goog.define('jsinterop.checks', 'DISABLED');

Then, InternalPreconditions.java uses this:

private static final boolean IS_TYPE_CHECKED = 
                getProperty("jsinterop.checks").equals("ENABLED");

Currently, System.getProperty is implemented with JsMethod for J2CL (and a magic method for gwt2):

  @JsMethod(name = "$getDefine", namespace = "nativebootstrap.Util")
  public static native String getProperty(String key);

The Util.$getDefine method only calls goog.getObjectByName with the parameter, not a compiletime string, and closure-compiler doesn't seem to be able to detect this anyway.

The result is a runtime NPE in both BUNDLE and ADVANCED compilation modes, unless jsinterop.js is manually added as an --entry_point, or a new define is added to the build directly (and in that case, --define isn't used by BUNDLE, by design).

Historically, it looks like J2CL used to have a pass which would examine System.getProperty calls and do something with them - perhaps the same idea could be used again to emit goog.require calls to correctly require that non-native.js files are loaded.

This might be worth generalizing, so that non-native.js files can be depended on in general? It would add an extra step to use a system property which isn't ideal, but does prevent the need for j2cl to recognize specific pieces of code to be transpiled.

This would also allow jre.js to keep working if classMetadata and checkedMode were moved - while there is a goog.provide('jre.checks') in that file, nothing actually ever depends on jre.checks, so could stop working if the file were split into one file per provide.

@gkdn
Copy link
Member

gkdn commented Jun 20, 2018

I think the best way forward is to have goog.getDefine and have the tooling handle this but earlier they were hesitant on that.

@concavelenz
Copy link
Contributor

What would "goog.getDefine" be?

@gkdn
Copy link
Member

gkdn commented Oct 17, 2018

It would be an official version of J2CL's nativebootstrap.Util.$getDefine; a utility that returns the value of a property defined with @define.

See the current code for uncompiled mode here (minus the string coercion):

static $getDefine(name, opt_defaultValue) {

And the pass for compiled mode:
https://github.com/google/closure-compiler/blob/85d4241e6488ffe6e38496b3dc66c02a825f7ded/src/com/google/javascript/jscomp/J2clUtilGetDefineRewriterPass.java#L23

We are missing an automatic edge between the class that uses getDefine and the class that defines it.

@concavelenz
Copy link
Contributor

This is unlikely to be a thing in Closure Library. Continuing to expose define values as globals is not something we want to do. global namespaces are slowly being turned down (nothing new should use goog.provide or goog.module.declareLegacyNamespace). Going forward, you should expect @define values to be module local and to access them you will need to import the module that defines them. Any solution you build around @define should keep that model in mind.

@gkdn
Copy link
Member

gkdn commented Oct 23, 2018

The last time we discussed about @define couple of years back, the semantics around module @define wasn't clear (how it is set, how it is accessed etc.). Is there more clarification around it? Also what is the plan for existing stuff (DEBUG, COMPILED etc.)?

@jDramaix
Copy link
Member

@concavelenz @gkdn any update?

@gkdn
Copy link
Member

gkdn commented Nov 16, 2018

This doesn't need an update, could stay as a known issue.

@gkdn
Copy link
Member

gkdn commented Jun 13, 2020

Since there is not much we can do here I'm closing the issue.
#76 tracks the documentation/example/macro.

@gkdn gkdn closed this as completed Jun 13, 2020
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants