Details
-
Improvement
-
Status: Open
-
Minor
-
Resolution: Unresolved
-
Scripting JavaScript 3.1.4
-
None
-
None
Description
The current SlingContext implementation extends org.mozilla.javascript.Context to introduce useful new global functions that are not part of the ECMAScript standard. This is achieved by overriding the initStandardObject() method using the now-deprecated default Context() constructor.
The Context() constructor was deprecated due to its reliance on a global static singleton context factory (see Rhino Context.java). This creates a dependency on a global state and introduces limitations. Specifically, the current approach injects SlingContextFactory as the default global factory of the Rhino engine during initialization (lines #48–#57). However, this approach has a significant caveat: the Sling JavaScript Engine fails if a global factory has already been set.
// conditionally setup the global ContextFactory to be ours. If // a global context factory has already been set, we have lost // and cannot set this one. public static void setup(ScopeProvider sp, int languageVersion) { // TODO what do we do in the other case? debugger won't work if (!hasExplicitGlobal()) { initGlobal(new SlingContextFactory(sp, Context.isValidLanguageVersion(languageVersion) ? languageVersion : Context.VERSION_DEFAULT)); } }
To future-proof the Apache Sling Scripting JavaScript module and eliminate these concerns, an improvement could be made by migrating to the Context(ContextFactory factory) constructor. This approach avoids relying on global variable-based injection and aligns better with modern practices, addressing the issues highlighted in the code comments.
Attachments
Issue Links
- links to