Having two instances of Context Factory under the same JVM

44 views
Skip to first unread message

Pedro Chaves

unread,
Feb 20, 2017, 12:04:52 PM2/20/17
to mozilla-rhino

Hello,

I'm using Rhino to compile and execute javascript functions. In order to constrain the execution time of scripts, I created a custom ContextFactory where am able to set the max cpu time allowed and the number of instructions before calling observeInstructionCount, similarly to the example described in ContextFactory.

The following code is a sample of the method I use to compile and execute the functions.

CustomFactory factory = new CustomFactory().setMaxCpuTime(300L);
if (!ContextFactory.hasExplicitGlobal()) {
            ContextFactory.initGlobal(factory );
}
Context context = factory.enterContext();
Scriptable Scope = context.initStandardObjects();
Function function = context.compileFunction(scope, script, "<func>", 0, null);
(...)
function.call(context, scope, scope, args);

Every thing works as excepted, the scripts that exceed the maximum allowed cpu time throw an Error. My problem is that I want to have two instances of my custom factory under the same JVM, one that will allow the scripts to run for a little longer than the other. But because of the call ContextFactory.initGlobal(factory) every Context will inherit the same ContextFactory. Even if I create a new custom factory with a different allowed cpu time, when I call factory.enterContext() the context will inherit the factory that was passed to initGlobal.

Is there any way to have two instances of ContextFactory (with different properties) under the same JVM?


Regards,

Pedro Chaves

ian

unread,
Apr 29, 2017, 8:11:59 PM4/29/17
to mozilla-rhino
Hello Pedro,

What describes seems inconsistent with what the API promises.

Have you attempted not setting a global ContextFactory, and thereafter always creating a context via factoryOne.enterContext() and factoryTwo.enterContext(), you'll have two different contexts from two different factories. 

However, please note that a Context is associated with the Thread you're running in. You may be seeing odd behavior if you're starting two Contexts on the same Thread.

Let us know how this goes!

Reply all
Reply to author
Forward
0 new messages