if I read from the FAQ (can I compile my beanshell scripts)

There is currently no full bytecode compiler for BeanShell scripts, although one may exist in the future. In the mean time, there are ways to compile BeanShell scripts to Java classes that use the interpreter at runtime and also ways to get BeanShell to pre-parse scripted methods for performance.

is there any example of how do do that so to enforce "compilation trust" in a unit test instead of bouncing at runtime. The release note does not give so much of details and the api is not very verbose.

BeanShell scripts can generate classes which are implemented by the interpreter at runtime. These classes can be saved to regular dot class (.class) files and in that way provide a concrete API for your scripts. Please see the User Manual (This feature is new: TODO) or release notes for the 2.x release for instructions on this. Release Notes

BeanShell scripts can also benefit from simple pre-parsing of the script. Upon parsing, BeanShell compiles the script into an internal representation (not bytecode, but fairly efficient). An upcoming release of BeanShell will offer a formal API for this, however you can get this benefit now using scripted methods and objects, as opposed to simpl unstructured scripts:

thanks
Frederic



Alexey Zinger wrote:
>From the manual's Parsing and Performance section:

The first time a script is read or sourced into an interpreter, BeanShell uses the parser to parse the script internally to an AST. The AST consists of Java object representations of all of the language structures and objects. The AST consists of Java classes, but is not the same as compiled Java code. When the script is "executed" BeanShell steps through each element of the AST and tells it to perform whatever it does (e.g. a variable assignment, for-loop, etc.). This execution of the ASTs is generally much faster than the original parsing of the text of the method. It is really only limited by the speed of the application calls that it is making, the speed of the Java reflection API, and the efficiency of the implementation of the structures in BeanShell.
When parsing "line by line" through a BeanShell script the ASTs are routinely executed and then thrown away. However the case of a BeanShell method declaration is different. A BeanShell method is parsed only once: when it is declared in the script. It is then stored in the namespace like any variable. Successive invocations of the method execute the ASTs again, but do not re-parse the original text.
This means that successive calls to the same scripted method are as fast as possible - much faster than re-parsing the script each time. You can use this to your advantage when running the same script many times simply by wrapping your code in the form of a BeanShell scripted method and executing the method repeatedly, rather than sourcing the script repeatedly.

Frederic Tardif <frederic.tardif@gmail.com> wrote:
not sure to exatly understand the twist...

indead how does beanshell work to compile the script?

Is he only doing compilation once you invoke a method?

Is he then doing some kind of byte fiddling (with cglib's kind of framework) so to be able to classload the code?

why he is some time able to invoke a method even without some java based imports on the top of the script?

why isnt'it needed to re-throw exceptions out of a bean-shell script's method???

..

lots of questions, but I think I miss a basic puzzle piece in beanshell's underneath....

Thanks
Frederic

On 3/2/07, Alexey Zinger < inline_four@yahoo.com> wrote:
I'm shooting from the hip here, not having looked at the code, so I might be off base.  But if I remember correctly, BeanShell does a kind of pseudo-compilation of functions, as opposed to evaluating statements outside of function blocks.  So with that in mind, it may be possible to write a tester that encloses the scripts in question in a special function and looking at the resulting instruction tree.


Frederic Tardif <frederic.tardif@gmail.com > wrote:
I have a bunch of beanshell and I would simply like to do a dummy junit test that "compile" each of them to ensure that they are valid.

Eval of the interpreter seems to do some syntaxic check but no real compilation verification.

Is there any way of doing this?

Any idea?

Thanks a lot -------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV_______________________________________________
Beanshell-users mailing list
Beanshell-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/beanshell-users



Alexey
2001 Honda CBR600F4i (CCS)
1992 Kawasaki EX500
http://azinger.blogspot.com
http://bsheet.sourceforge.net
http://wcollage.sourceforge.net

Sucker-punch spam with award-winning protection.
Try the free Yahoo! Mail Beta.

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV_______________________________________________
Beanshell-users mailing list
Beanshell-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/beanshell-users



Alexey
2001 Honda CBR600F4i (CCS)
1992 Kawasaki EX500
http://azinger.blogspot.com
http://bsheet.sourceforge.net
http://wcollage.sourceforge.net


Bored stiff? Loosen up...
Download and play hundreds of games for free on Yahoo! Games.