Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 365133

Summary: Add support for an internal rhino interpreter to run scripts within eclipse IDE
Product: [WebTools] JSDT Reporter: Christian Pontesegger <christian.pontesegger>
Component: GeneralAssignee: Project Inbox <jsdt.javascript-inbox>
Status: CLOSED WONTFIX QA Contact: Chris Jaun <cmjaun>
Severity: enhancement    
Priority: P3 CC: dhodnett, loskutov, Michael_Rennie, simon_kaegi
Version: unspecifiedKeywords: needinfo
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard:
Attachments:
Description Flags
Plugins supporting Rhino JS execution within eclipse
none
Updated plugins for JavaScript Support none

Description Christian Pontesegger CLA 2011-11-29 15:46:27 EST
Build Identifier: 

I am interested in having an internal rhino interpreter available within eclipse to run and debug scripts that run directly in the IDE.

Scripts should be able to make use of available Java classes within the IDE. This way the whole IDE would be scriptable.

Reproducible: Always
Comment 1 Christian Pontesegger CLA 2011-11-29 15:49:08 EST
As I am working on such a project already for some time, I'd like to present what I have:

[copy from wtp-dev mailing list]
I am a developer at Infineon Austria and working on eclipse plugins for internal use for over 3 years now. Part of this development was using JSDT and Rhino and integrate a JS/ECMA interpreter into the eclipse IDE. This results for example in a shell view capable of executing JS code on the fly. There is also a dedicated JS console and some UI stuff to execute *.js files directly within the IDE.
I implemented some extension mechanism to the engine to provide libraries written in Java to the JS engine. So the main focus is not supporting JS coding for websites, but JS to script the Eclipse IDE itself.
 
Currently I am looking for ways to make this public. Part of my thoughts is if this could fit to JSDT and if so, how. I am interested in your opinion.


[and here a more closer description]
To give you a better insight of what I've done:
In our company we have to access lots of measurement equipment for automated testing of our hardware products. We needed a versatile tool to combine different tasks. So we decided to go for an easy scripting language. As I wanted it to be integrated in Eclipse (to get all the cool bonus stuff like project management, svn support, the editor, ... for free) I had to find a way to make the scripts executable within the IDE.

As Rhino is already part of JSDT/WTP it was just a matter of replacing the Rhino classloader with one capable of using stuff from other bundles. This way it is possible to instantiate every java class that is exported by some bundle simply using something like:

var foo = new Packages.org.eclipse.whatever.MyClass();
foo.someMethod();

This gives you total control to do whatever you want - you could even program a new view in JS and fire it up in your running eclipse (still writing this code would not be fun). So I added some kind of extension mechanism to simplify JS programming a bit. Basically members of a class (which needs to extend a defined base class) are mapped to JS functions and can be used that way in the Rhino interpreter.

Then I added some UI stuff like a shell for interactive scripting, a quick launcher and a console. I experimented with a macro recorder that tracks shell input. This works quite nicely.
Building an Eclipse IDE macro recorder would be another story.
Right now there is no debugging support available. I know that there is already some UI stuff available to debug JS but I didn't have time yet to dig into this.

Currently the script engine works only with Rhino but it would be fun to integrate other interpreters too (as long as there is a native Java parser available) like jruby or jython.

So this stuff is used within my company. I already clarified that I am free to publish the source if I find someone interested in it.
Comment 2 Nitin Dahyabhai CLA 2011-12-12 09:35:05 EST
(In reply to comment #1)
> new view in JS and fire it up in your running eclipse (still writing this code
> would not be fun). So I added some kind of extension mechanism to simplify JS
> programming a bit. Basically members of a class (which needs to extend a
> defined base class) are mapped to JS functions and can be used that way in the
> Rhino interpreter.

The best thing would be to combine this with a mechanism that exposes those classes to Content Assist, although that would tricky, for starters, since there's nothing to "index" as we would source files or metadata files.

> Currently the script engine works only with Rhino but it would be fun to
> integrate other interpreters too (as long as there is a native Java parser
> available) like jruby or jython.

This is what has me thinking about whether all of this would fit in JSDT--unless this is speculation and there aren't other implementations yet.

Michael, can you comment from the point of view of our Debug lead?
Comment 3 Michael Rennie CLA 2011-12-12 13:29:27 EST
(In reply to comment #2)

> Michael, can you comment from the point of view of our Debug lead?

We already have support for debugging Rhino, although we use our own custom wire protocol (http://wiki.eclipse.org/JSDT/Debug/Rhino/Rhino_Debug_Wire_Protocol). It sounds like we could quite easily extend the current Rhino debugging support to hook into (or be hooked into) the proposed support - and it might also fix our current lack of support for debugging Java classes in the JSDT debugger...

Christian, here is a link to the wiki with a bunch of information about Rhino debugging in JSDT: http://wiki.eclipse.org/JSDT/Debug/Rhino.

Would it be possible to review the code and / or get a bit more information about how you run the classes / JS files in Rhino? It might give me a better understanding of how we could hook the two together.

Simon might also have some thoughts here as he started working on support for deploying bundles of JS code to Eclipse.
Comment 4 Christian Pontesegger CLA 2012-01-18 08:49:33 EST
(In reply to comment #3)
> Would it be possible to review the code and / or get a bit more information
> about how you run the classes / JS files in Rhino? It might give me a better
> understanding of how we could hook the two together.

Sorry for the delay. I am currently negotiating with our legal department to get clearance for releasing code. Hope to get it next week, so stay tuned.
Comment 5 Christian Pontesegger CLA 2012-02-06 03:32:53 EST
Created attachment 210554 [details]
Plugins supporting Rhino JS execution within eclipse

Finally I am able to show something:


Getting started
===============

I added 4 plug-ins that you can try out for yourself. I was hosting them within an Eclipse 3.7 environment with JSDT support.

After starting you will have a new view available: JavaScript -> JavaScript Shell.
This will give you an interactive shell where you can execute JS commands.
Consider changing Preferences -> JavaScript -> Shell: set all the outputs to Shell (which means the interactive shell view).

A print("Hello world") should give you some output in a console view. Doing something like "38 + 4" will show you the results right in the shell.
You may access native java objects with something like

new Packages.java.io.File("~/test")

See Rhino documentation on this. With the classloader used for Rhino you may access classes within other bundles too.

JS file now have an additional run target to execute code directly within eclipse.



Plug-in content
===============

I had to rip the code out of a bigger existing project, so plug-in separation is far from perfect.
com.example.script ... holds generic classes for scripting support. Might be interesting to have other interpreters (Jython) too in the future
com.example.javascript ... Rhino specific stuff.
com.example.javascript.ui ... shell view and run targets for JS 

com.example.javascript.modules.tools ... gotta explain this one:
For our purpose JS is nice, but with the pure JS engine you cannot do lots of things. In most cases you need to work with native Java classes. Unfortunately the syntax for such JS code is quite nasty. So I added a simple plug-in mechanism for JS functions. I use it to hide complex Java code behind a wrapper that can be easily called by JS code. So when you load the tools module (loadModule("Tools");) you magically have new functions available: sleep() and showInfoDialog("Title", "Text").

Let me know what you think of it.
Comment 6 Christian Pontesegger CLA 2012-03-29 04:22:05 EDT
Created attachment 213319 [details]
Updated plugins for JavaScript Support

I see no activity on this for quite some time. So I uploaded a reworked version of the JS support. 

Basically I removed the module loading stuff from the core plugins and put it into a separate plug-in - therefore it could be removed easily as it might be quite specific to my personal needs.

If you want modules to work you need to add a line to your java code right after creating an instance of RhinoScriptEngine:

com.example.javascript.modules.EnvironmentModule.bootstrap(<RhinoScriptEngine>);
Comment 7 Michael Rennie CLA 2012-03-29 12:07:01 EDT
(In reply to comment #6)
> Created attachment 213319 [details]
> Updated plugins for JavaScript Support
> 
> I see no activity on this for quite some time. So I uploaded a reworked version
> of the JS support. 
> 
> Basically I removed the module loading stuff from the core plugins and put it
> into a separate plug-in - therefore it could be removed easily as it might be
> quite specific to my personal needs.
> 
> If you want modules to work you need to add a line to your java code right
> after creating an instance of RhinoScriptEngine:
> 
> com.example.javascript.modules.EnvironmentModule.bootstrap(<RhinoScriptEngine>);

Sorry Christian, it was on my plan to look at this much sooner, but I have been swamped. I promise to look at it ASAP :)
Comment 8 Christian Pontesegger CLA 2013-07-31 06:58:17 EDT
I moved the project sources to github:
https://github.com/Pontesegger/EScript

In the meantime it also supports Jython and JRuby, so it may be grown out of JSDT by now.

There is also a project homepage available at
http://codeandme.blogspot.de/p/escript-scripting-power-for-eclipse.html
Comment 9 Michael Rennie CLA 2013-08-01 12:37:25 EDT
(In reply to comment #8)
> I moved the project sources to github:
> https://github.com/Pontesegger/EScript
> 
> In the meantime it also supports Jython and JRuby, so it may be grown out of
> JSDT by now.
> 
> There is also a project homepage available at
> http://codeandme.blogspot.de/p/escript-scripting-power-for-eclipse.html

Thanks for the update Christian! I just tried it now in Eclipse 4.4 and I like the JavaScript Shell, I think we could definitely hook that up to our Rhino debugging - currently we use an IO console, but this view is much nicer. It would also be very cool to hook it up with the editor kind of like how the Scrapbook works in JDT.

When trying it out I did notice that it does not seem allow most of the stock Rhino commands (unless I am messing something up)? For example entering help() returns 'ReferenceError: "help" is not defined. (#1)'  - the same thing happens for most of the other commands.

Some other nits:

1. Ctrl+C does not seem to work in the command result area 
2. the scroll lock and clear toolbar buttons should probably use the platform text clear and scroll lock icons rather than text
3. the single line combo / command enter area can get a bit small if you are entering in some rather large commands
Comment 10 Christian Pontesegger CLA 2013-08-02 02:27:48 EDT
(In reply to comment #9)
> Thanks for the update Christian! I just tried it now in Eclipse 4.4 and I
> like the JavaScript Shell, I think we could definitely hook that up to our
> Rhino debugging - currently we use an IO console, but this view is much
> nicer. It would also be very cool to hook it up with the editor kind of like
> how the Scrapbook works in JDT.

Basic debugging support was added just recently by one of our trainees and is available on github (link from comment 8). It works quite nicely using the Wire Protocol to communicate with a local thread instead of a new Process. However we had to use several internal class references to make it work.

> When trying it out I did notice that it does not seem allow most of the
> stock Rhino commands (unless I am messing something up)? For example
> entering help() returns 'ReferenceError: "help" is not defined. (#1)'  - the
> same thing happens for most of the other commands.

This is by design as I am not loading the shell extensions into the context when initializing the engine. See RhinoScriptEngine.setupEngine() for that. 
Instead of mContext.initStandardObjects() you would need something like New ImporterTopLevel(getContext()) and I guess some more stuff to get your initial scope right.


> Some other nits:
> 
> 1. Ctrl+C does not seem to work in the command result area

I guess you did not test the version available on GitHub? Should be better right now (although not completely fixed yet as the shell view automatically focuses on the input textbox on keypressed events)

> 2. the scroll lock and clear toolbar buttons should probably use the
> platform text clear and scroll lock icons rather than text

scroll lock got removed as it rendered to be quite useless

> 3. the single line combo / command enter area can get a bit small if you are
> entering in some rather large commands

true. You can already use CTRL-return to type multiline text, although you would just see the last line as the text field does not expand.
Comment 11 Christian Pontesegger CLA 2013-10-25 10:23:44 EDT
This contribution evolved a bit over the last years. Finally we've made it and were accepted as an incubator project within e4 (e4/scripting). The project is called EASE.

interested persons may check out the wiki:
http://wiki.eclipse.org/E4/Scripting

I guess this bug can be closed right now.
Comment 12 Christian Pontesegger CLA 2015-01-14 06:06:01 EST
Finally this proposal ended up as an Eclipse project:
eclipse.org/ease