Thing is, context.compileString() returns an
org.mozilla.javascript.InterpretedFunction (which implements
org.mozilla.javascript.Script, among others). As such, the runtime
treats it as a JS function, not as a plain Java object. Generally,
Rhino's underlying Java implementation classes aren't meant to be
directly used from scripting. I'd suggest you write a host object with
an "include" function to include/import further scripts instead of
directly manipulating org.mozilla.javascript.* objects from script.
Attila.
On 2009.05.16., at 15:46, Joran wrote:
Hi,
I've read through all the discussions and articles on contexts and
scopes that I could get my hands on (most of these at least three
times), but am still struggling to compile and exec a script with JS
in the Rhino shell.
I've tried something as simple as this:
js> var context = org.mozilla.javascript.Context.getCurrentContext();
js> var script = context.compileString('var a = true;', 'test', 0,
null);
js> var scope = context.initStandardObjects(null, true);
js> var result = script.exec(context, scope);
But it returns "TypeError: Cannot find default value for object.".
I'm trying to get Atilla's vanilla scope trick working. I've got the
Rhino shell starting up the Simple webserver. I'd like each HTTP
request to create a clean scope, exec a pre-compiled script in it, put
some extra request-specific variables in it, call a function in the
new scope, and then return the result back to Simple.
(The reason for trying this is that I was using a global scope across
all threads, but instantiating these globally defined JS functions
from the threads would attach the instances to the global scope
causing memory usage to keep growing and seemingly keeping these
instances out of reach of the garbage collector. I'm hoping a clean
scope per thread can avoid this.)
Your help and advice would be MUCH appreciated. Thank you.
Joran Greef
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino