A cheap way to run a compiled function (keeping with your "test" function) in threadsafe manner:

Script script = ...; // you obtain this from somewhere, cache it, etc. It's shared Context cx = Context.enter(); // Consider using ContextFactory.call() instead
ScriptableObject scope = cx.initStandardObjects();
script.exec(cx, scope);
Object retval = ScriptableObject.callMethod(cx, scope, "test", args);
Context.exit();

There will be one class generated for your "test" function (as you compiled the script only once), so no permgen exhaustion - script.exec() will create new instances of that single class. You can also have a pre-created "standard" scope with initStandardObjects() run in it, seal it (so it's read-only) and set it as prototype of any per-request scopes, that way you can even avoid javing to run initStandardObjects() on a per request basis:

// On system initalization:
Context cx = Context.enter();
ScriptableObject standardScope = cx.initStandardObjects();
... you can even add some further proprietary "standard" objects and global functions here by executing a script into standardScope
standardScope.seal();
Context.exit();

// This is per request:
Script script = ...; // you obtain this from somewhere, cache it, etc. It's shared Context cx = Context.enter(); // Consider using ContextFactory.call() instead
> ScriptableObject scope = cx.newObject(standardScope);
> scope.setPrototype(standardScope);
script.exec(cx, scope);
Object retval = ScriptableObject.callMethod(cx, scope, "test", args);
Context.exit();

Note lines marked with ">" have changed. This is as efficient as it gets.

Attila.

--
home: http://www.szegedi.org
twitter: http://twitter.com/szegedi
weblog: http://constc.blogspot.com

On 2008.12.10., at 17:28, Johan Compagner wrote:

came pretty much to the same conclusion
I guess we just have to run in interpreted mode because the class
generations is really killing on the server for our customers
Some have hundreds if not thousands of functions (and those are per session)

So we get perm mem heap problems

I am going to profile a bit what the cost is running it in interpreted mode.

johan


On Wed, Dec 10, 2008 at 17:20, Attila Szegedi <[EMAIL PROTECTED]> wrote:

On 2008.12.10., at 16:35, Johan Compagner wrote:

also all our code are standalone functions like

function test()
{
// code
}

if i compile that as a script and say exec() then nothing happens because
it
doesnt exec the function but just the scripts (which doesnt do anything)


It does. It creates a function object and binds it to the property name
"test" within the scope.


besides that suddenly having arguments also is not possible for us because many of our customers use "arguments" we cant just change that. So using a
Script instead of a Function is just not possible

I am currently investigating why it is that function object cant just be
as
a script, why we really need to have suddenly a parent and prototype scope which a Script (which also can have the same function it it) doesnt seem
to
need


Here's a distinction:

As the JS spec does not define a runtime representation for the program, a script is not a first-class JS object. It is Rhino's implementation- specific representation of a JS program. There's no way to access or manipulate it
within the program itself.

A function, on the other hand, is a first-class object in JavaScript. I.e. it is a Scriptable, and it also has a prototype (the standard Function object, which is why you need to have a scope with standard objects in it to create a function). As such, function objects are bound to a scope, scripts are not. Function objects are really created when a program executes a function statement or a function expression; and as any other first- class JS object, function objects are mutable (sort of - you can add/remove arbitrary
properties to them, but you can't modify their code).

johan


Attila.

--
home: http://www.szegedi.org
twitter: http://twitter.com/szegedi
weblog: http://constc.blogspot.com

_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino

Reply via email to