What I'm trying to accomplish here is to share code between users (meaning we don't have to duplicate immutable objects like scripts, thereby saving memory).
Perhaps you can help by explaining how and when instances of JavaScriptInterpreter get created. Is it per sitemap?
With Rhino, script variables are stored in a "scope" object - which must be created on a per user basis. This is the "thrScope" variable you see in JavaScriptInterpreter.java. However, it's possible to share the built-in JavaScript objects, like String, Number, etc. as well as scripts loaded by the sitemap (system.js, etc) since these are immutable objects, among all the thrScope's. This shared scope is the "scope" field in JavaScriptInterpreter.java.
When you request the property of an object in JavaScript the interpreter performs the lookup in the following way:
Search the object itself Search the object's prototype chain Search the object's parent scope
The thrScope's parent scope is null, so it becomes the global variable scope for the user, but we set its prototype to be the shared "scope", so builtin objects are located in the shared scope.
I wanted to test if I could successfully compile scripts using the shared scope instead of in thrScope, and it does indeed seem to work.
Now my question:
I only want to compile and execute the set of scripts that apply to a specific application. I don't want them shared between different applications. I think I remember Ovidiu saying that each (sub-)sitemap should represent a single application. Does that make sense? If so, would each one its own instance of JavaScriptInterpreter?
Regards, Chris
Regards,
Chris
Vadim Gritsenko wrote:
Can somebody knowledgable comment on the following method? Local variable scope is not used...
private Script compileScript(Context cx, Scriptable scope, Source src) throws Exception { InputStream is = src.getInputStream(); Reader reader = new BufferedReader(new InputStreamReader(is)); // FIXME: scope or this.scope? Script compiledScript = cx.compileReader(this.scope, reader, src.getURI(), 1, null); return compiledScript; }
Thanks, Vadim