cbuschka / beanshell2

Automatically exported from code.google.com/p/beanshell2
0 stars 0 forks source link

Concurrency isue: Defining class problem: Check: BeanShell cannot yet simultaneously define two or more dependant classes of the same name. #40

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Class comment in bsh.Interpreter states:

    In the above examples we showed a single interpreter instance, however 
    you may wish to use many instances, depending on the application and how
    you structure your scripts.  Interpreter instances are very light weight
    to create, however if you are going to execute the same script repeatedly
    and require maximum performance you should consider scripting the code as 
    a method and invoking the scripted method each time on the same interpreter
    instance (using eval()). 

But if you do so, and if you do it concurrently, and the block contains a class 
definition you will encounter an internal error:

Defining class problem: Check: BeanShell cannot yet simultaneously define two 
or more dependant classes of the same name. Attempt to define: X while 
defining: X
at bsh.BshClassManager.definingClass(Unknown Source)
at bsh.ClassGeneratorImpl.generateClassImpl(Unknown Source)
at bsh.ClassGeneratorImpl.generateClass(Unknown Source)
at bsh.BSHClassDeclaration.eval(Unknown Source)
at bsh.BSHBlock.evalBlock(Unknown Source)
at bsh.BSHBlock.eval(Unknown Source)
at bsh.BshMethod.invokeImpl(Unknown Source)
at bsh.BshMethod.invoke(Unknown Source)
at bsh.BshMethod.invoke(Unknown Source)
at bsh.This.invokeMethod(Unknown Source)
at bsh.This.invokeMethod(Unknown Source)

Original issue reported on code.google.com by pejob...@gmail.com on 20 Feb 2011 at 5:42

GoogleCodeExporter commented 9 years ago

Original comment by fschm...@gmail.com on 20 Feb 2011 at 10:10

GoogleCodeExporter commented 9 years ago
Added unit test "bsh.InterpreterConcurrencyTest".

Fix delayed because created class points to wrong namespace. Need to do some 
work on class creation process. That's also a chance to simplify it ;)

Code-pointers: 
 * Reflect.constructObject(..)
 * created class in turn calls ClassGeneratorUtil.initInstance(..) from generated constructor

Could be simplified by implementing an internal callback (interface). This 
removes some setting-field-by-reflection code and would allow to pass the 
correct namespace to the instance (callstack based).

Besides concurrency issues this would also speed up e.g. class generation calls 
in loops.

Original comment by pejob...@gmail.com on 20 Feb 2011 at 11:38

GoogleCodeExporter commented 9 years ago
* new marker interface "bsh.GeneratedClass" for generated classes 
* caching of generated class instance in "bhs.BSHClassDeclaration" 
* inject current namespace for new instances of generated classes in 
"bsh.BSHAllocationExpression"

> Need to do some work on class creation process.
> That's also a chance to simplify it ;)

I postponed this, i'll do it with an ASM update. It's a bigger change.

fixed with http://code.google.com/p/beanshell2/source/detail?r=61

Original comment by pejob...@gmail.com on 25 Feb 2011 at 5:12

GoogleCodeExporter commented 9 years ago
for "inject namespace" i added a (package private) "copy" method to 
"bsh.NameSpace"

Original comment by pejob...@gmail.com on 25 Feb 2011 at 5:13

GoogleCodeExporter commented 9 years ago

Original comment by pejob...@gmail.com on 25 Feb 2011 at 5:53

GoogleCodeExporter commented 9 years ago

Original comment by pejob...@gmail.com on 20 Oct 2011 at 7:12