For those of you who have started poking around, this is an example of one of my more technical posts. I am, after all, a programmer. So if this one's not for you, stop by the RSS Feed or enter your email in the box on the right to keep updated.
Embeddable scripting can be a great way to allow users to extend your core functionality. Some game companies also use scripting languages to allow their designers to modify functionality without interacting with the programmers. Scripting could even allow you to pare back features and provide advanced functionality accessible only through scripts, getting your software released faster (just iterate quickly).
At Sharendipity, we use embedded scripts to access functionality that isn't yet accessible through the UI. The demo below is a port of some of the code that we used to do this. I originally wrote it for BarCamp Madison. In the end, we didn't schedule in the session so I thought I'd post it here. A while ago, I also wrote a post about how we moved from the Jython project for integrating Python into Java to the Rhino project, a JavaScript interpreter and compiler. If you're interested in the differences between the two, that's a good place to go.
The demo shows how to integrate both the Jython and Rhino projects into a Java application. A small window will pop up where you can select the scripting language, enter a script (try 'print "test"' as an example), and execute the script. It does this by pre-compiling the scripts into Java byte code which it then uses for execution. It's possible to run both Python and JavaScript in interpreted mode using each library but we needed them to run a bit faster.
There is also a bit of code in there that shows how to secure script access to the rest of the application in Rhino. It's possible to do this in Jython through the ClassLoader architecture but it gets a bit complicated. Since we're sticking with Rhino for the time being I haven't explored it further.
Demo code: Download BCMad2.zip (Creative Commons Unported 3.0 License). The file contains an Eclipse project, source code, and the libraries required to run it. So you either need Eclipse or you'll have to build it on your own.
In the future, I'll talk about designing a scripting interface for your application. In the meantime, if you have any issues with the demo, let me know in the comments.





Comments