On Aug 26, 1:37 am, Attila Szegedi <[EMAIL PROTECTED]> wrote:
> On Aug 25, 2008, at 1:50 AM, Nathan wrote:
>
>
>
>
>
> > Hi,
>
> > I've just upgraded from 1.5R4 to 1.7R1 and some scripts that used to
> > compile now no longer compile.
>
> > My code is pretty much as follows:
>
> >    context.setGeneratingDebug( true );
> >    context.setOptimizationLevel( 0 );
> >    this.script = context.compileReader( reader, fileName, 1, null );
>
> > Then I call a function multiple times on the same script, throwing the
> > scope away after each execution:
>
> >    Scriptable scope = new ImporterTopLevel();
> >    this.script.exec( context, scope );
> >    Object functionObject = scope.get( functionName, scope );
> >    if ( functionObject instanceof Function )
> >    {
> >        Function function = (Function)functionObject;
> >        function.call( context, scope, null, args );
> >    }
>
> > The first question is whether this usage is acceptable.  I want to
> > reuse the compiled code to save perm gen space and compilation time.
> > I need to throw away the scope between executions to save heap space
> > between function calls.
>
> Hm... an initStandardObjects() in every created scope might be in  
> order. *Or* running it in a single scope that's then being set as the  
> prototype of the newly created scopes. Other than that, yes, it's fine.
>
> Attila.
>
> --
> home:http://www.szegedi.org
> weblog:http://constc.blogspot.com- Hide quoted text -
>
> - Show quoted text -

Thanks for your reply.  I build the scope for each function call as
follows.  The Rhino source code shows that the ImporterTopLevel
constructor initialises the standard objects.

    Scriptable scope = new ImporterTopLevel( context );

    for ( final String objectName : myBuiltinObjects.keySet() )
    {
        final Object globalObject =
myBuiltinObjects.get( objectName );
        final Scriptable jsGlobalObject =
            Context.toObject( globalObject, scope );
        scope.put( objectName, scope, jsGlobalObject );
    }

    synchronized ( myScript )
    {
        myScript.exec( context, scope );
    }

    Object functionObject = scope.get( functionName, scope );
    if ( functionObject != null && functionObject instanceof
Function )
    {
        // Call the JavaScript event function
        Function function = (Function)functionObject;
        function.call( context, scope, null, args );
    }

The scope then gets thrown away after the function is called.  This
code could be called by multiple threads at the same time.  I assume
that the exec() call is the only one that would need to be
synchronized.  It then populates the scope with a new instance of the
function objects each time, so I don't see any concurrency issues
there.  Is this assumption correct, or do I need more synchronized
blocks to ensure thread safety?
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino

Reply via email to