Scott Yuan created SLING-12516:
----------------------------------

             Summary: Improving SlingContext by Replacing Deprecated Context() 
API with Context(ContextFactory)
                 Key: SLING-12516
                 URL: https://issues.apache.org/jira/browse/SLING-12516
             Project: Sling
          Issue Type: Improvement
          Components: Scripting
    Affects Versions: Scripting JavaScript 3.1.4
            Reporter: Scott Yuan


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.

{code:java}
    // 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));
        }
    }
{code}

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.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to