cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Rob Berens" <rber...@osirion.nl>
Subject Re: FlowScript: Enforcing exlplicit declaration blocks dynamic loading
Date Mon, 31 Oct 2005 13:56:19 GMT
Sylvain Wallez wrote:
> Rob Berens wrote:
> > In 2.1.6. explicit declaration of variables in global scope was enforced
> > (fix 25951).
> > This was implemented by locking the scope after the main part of the
main
> > script has been loaded and processed.
> > In our situation we have one generic script loaded form the sitemap
once,
> > that determines the name of the script to be loaded based on the request
and
> > than loads that script. Simplified it's like this.
> > The sitemap loads the script main.js:
> >
> > main.js:
> >
> > function loadScript() {
> >     var scriptURI = "determineScriptURIFromRequest";
> >     cocoon.load(scriptURI);
> > }
> >
> >
> > When a request comes in, the sitemap calls the function loadScript().
Let's
> > say loadScript determines that it needs to load myScript.js.
> >
> >
> > myScript.js:
> >
> > var myVar = "myValue"; // This results in an error when loaded from
main.js.
> >
> > "doSomething";
> >
>
> There's a big problem with this approach: since the actual script to
> load is defined by the currently executing request, what happens when
> different scripts are loaded? You fill the global (and session-bound)
> scope with request-dependent items.
>
> To solve your issue, what we need is to extend cocoon.load() so that it
> accepts a second parameter, which will be the scope used to load the
script.

I see the problem. After some investigation I came to the conclusion that
the only thing I really need is to dynamicallye load JavaScript objects. So
the myScript.js in the previous example actually is more like a the
myObject.js below:

function myObject() {                // at this point the current
implementation throws the exception
     // constructor for myObject
}

myObject.prototype.myMethod = function() {
    // implementation of myMethod
}
......


When the put method in FOM_CocoonJavaScriptInterpreter.ThreadScope is
changed into:


    public void put(String name, Scriptable start, Object value) {
            //Allow setting values to existing variables, or if this is a
            //java class (used by importClass & importPackage)
            // Also allow function definitions
            if (this.locked && !has(name, start) && !(value instanceof
NativeJavaClass) &&
                !(value instanceof org.mozilla.javascript.Function))
{        // or maybe InterpretedFunction
                // Need to wrap into a runtime exception as Scriptable.put
has no throws clause...
                throw new WrappedException (new
JavaScriptException("Implicit declaration of global variable '" + name +
                  "' forbidden. Please ensure all variables are explicitely
declared with the 'var' keyword"));
            }
            this.useSession = true;
            super.put(name, start, value);
        }

thus also allowing for new function declarations my problem is solved.
I know there is still a certain danger, as you may load two different
scripts having the same function name, but
I think it is acceptable to say that this is up to the designer of the
scripts. Defining different objects with the same name is bad design.

WDYT.

Rob Berens
Osirion B.V.
Gagelveld 41
6596 CC  Milsbeek
The Netherlands
Tel: +31 (0)485-54 02 03
Fax: +31 (0)485-54 02 04
E-mail: rberens@osirion.nl




Mime
View raw message