Subj : Re: Sealing Scopes To : vige46 From : Igor Bukanov Date : Fri Oct 08 2004 12:40 am Miro wrote: > Hi, > > I'd like to use one top ScriptableObject among several threads as > described in http://www.mozilla.org/rhino/scopes.html > I'd like to combine it with sealing the top scriptable object: > >
> Context tempContext=Context.enter();
> sharedScope=new ImporterTopLevel(tempContext,true);
> //BLOCK1: ....some operations:
> tempContext.evaluateString(sharedScope,"importPackage(Packages.java.util);",null,0,null);
> sharedScope.put("a",sharedScope,new Integer(10));
> //seal object then
> sharedScope.sealObject();
> Context.exit();
>
>
> In another thread, I'd like to use this sharedScope as a prototype for
> another( local) scope:
>
> > Context context; > context=Context.enter(); > > Scriptable scope=new ImporterTopLevel(context,true); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > > scope.setPrototype(sharedScope); > scope.setParentScope(null); >> > Everything works fine( I have access to propertie 'a' defined in > BLOCK1), untill I execute JavaScript command, which defines new > property > >
> Object returnValue=context.evaluateString(scope, "var l = new > LinkedList();", null, 0, null); >> > When calling this, I get org.mozilla.javascript.EvaluatorException: > Cannot remove a property from a sealed object: XML. The underlined line creates a sealed object containing another copy of the standard objects. If you replace that by Scriptable scope = cx.newObject(sharedScope); then it should work. Also you have to always use the pattern: contextInstance = Context.enter(); try { .... } finally { Context.exit(); } or you get a resource leak. Regards, Igor .