Novell Home

Developer's Toolbox: Scripting Tools, Part 1

Novell Cool Solutions: Feature
By Richard Smith

Digg This - Slashdot This

Posted: 16 Nov 2004
 

Scripting Tools for Developers Series

This is the first in a series of articles that will introduce developers to some of the many scripting and embedding options available for Linux developers. Most of the tools discussed in this series of articles will be cross-platform capable, thus allowing a developer to build on any supported platform and use the resultant scripts on other supported platforms. Many of the tools covered will be very familiar to you: Perl, PHP, and Python to mention a few. These tools will offer a variety of uses, and the purpose behind this series to expose these many choices and provide some insights into how they might benefit the developer.

From scripting to manipulating the user environment to embedding a scripting language within your application, you'll find some tools and uses that maybe you had not considered before.

Look no further than Lua for a small, embeddable language

Quite often as a software developer you find yourself in need of the ability to add functionality to your application, either yourself or through your users. A good way of doing this is to embed scripting capability into your application. By allowing your users to extend the functionality of your application, you allow for customization by the user, rather than by yourself. The effects of this are that your application is much more flexible and useful and the users themselves can make these customizations.

This customization can range from user interface changes to integration with external systems. A scripting language can expose the functionality of your application to these types of extensions. In many cases these scripting languages are subsets of another programming language and allow users to make use of existing skills to, in essence, re-program your application to better suit their needs. Allowing this can be an important selling point for your application when compared to a competing application that does not offer this simple extensibility.

I've run across a particular language of this type called Lua. Here is what the About page at lua.org says about Lua...

Lua is a powerful light-weight programming language designed for extending applications. Lua is also frequently used as a general-purpose, stand-alone language. Lua is free software.

Lua combines simple procedural syntax with powerful data description constructs based on associative arrays and extensible semantics. Lua is dynamically typed, interpreted from bytecodes, and has automatic memory management with garbage collection, making it ideal for configuration, scripting, and rapid prototyping.

A fundamental concept in the design of Lua is to provide meta-mechanisms for implementing features, instead of providing a host of features directly in the language. For example, although Lua is not a pure object-oriented language, it does provide meta-mechanisms for implementing classes and inheritance. Lua's meta-mechanisms bring an economy of concepts and keep the language small, while allowing the semantics to be extended in unconventional ways. Extensible semantics is a distinguishing feature of Lua.

Lua is a language engine that you can embed into your application. This means that, besides syntax and semantics, Lua has an API that allows the application to exchange data with Lua programs and also to extend Lua with C functions. In this sense, Lua can be regarded as a language framework for building domain-specific languages. Lua is implemented as a small library of C functions, written in ANSI C, and compiles unmodified in all known platforms. The implementation goals are simplicity, efficiency, portability, and low embedding cost. The result is a fast language engine with small footprint, making it ideal in embedded systems too.

Lua is designed and implemented by a team at Tecgraf, the Computer Graphics Technology Group of PUC-Rio (the Pontifical Catholic University of Rio de Janeiro in Brazil). Tecgraf is a laboratory of the Department of Computer Science.

Lua means moon in Portuguese and is pronounced LOO-ah.

Now you might be thinking, so how does this help me? Well, just to illustrate that Lua can be used in many varied environments, let's examine a few diverse applications of Lua.

First Encounter

My first known encounter with Lua was as the scripting engine for a game I played, Baldur's Gate from Bioware. Baldur's Gate was a very popular computer role-playing game released five years ago and won many industry awards, including a number of "Game of the Year" awards. One of Baldur's Gate's strengths was the ability of players to be able to customize behavior of characters within the game by way of a scripting engine. I spent many hours fine-tuning scripts to improve my game play and there are literally thousands of scripts written by players available.

I've also run across Lua as the scripting engine in a couple of other games and it seems that Lua has gained a great deal of popularity among game developers due to its small size and flexible capabilities.

A good example of Lua's cross-platform capabilities is its use in the console game, Defender. Lua was used to script in game missions.

Not just for fun and games

Another interesting use for Lua I found while researching was as a part of the tool "Giggle" developed by Moving Picture Company. Giggle was used to create special effects for the movie "Troy" and MPC is expanding its use into other areas of their entertainment business. Now this isn't a use of Lua that is exposed to users, but does illustrate the power and versatility of Lua.

Lua is the scripting engine that drives the "Premake" build script generation tool. What Premake does is relieve you of the need to manage build scripts for multiple development environments by using a Lua definition file to create input files for GNU Make, Microsoft Visual Studio 6 and SharpDevelop.

And finally another cross-platform implementation of Lua is in the ELinks browser. ELinks is a text mode web browser that supports HTTP, FTP, and most important web standards. Lua is used in ELinks to change the behavior of the browser itself, which is not to be confused with how JavaScript, VBscript, etc., are used embedded within web pages to affect how the page is rendered and handled. Lua is not embedded into the web pages, but instead is actually embedded into the browser itself, thus affecting the browser in the way the user wishes.

So where are the tools for using Lua?

There are a number of tools available for use with Lua, from IDE's to add-on libraries of functions to wrappers for embedding Lua in your application.

One that I found interesting and useful is wxLua. wxLua brings Lua and wxWidgets together to create an extension to Lua for building graphical user interfaces for your applications. wxWidgets is a GUI framework that is available across a number of platforms. Developers can use it to easily build familiar looking interfaces for applications using C++. This means that to create applications on a variety of platforms a developer will need to make minimal changes to source code, mainly just to account for differences in the underlying GUI of the platform.

Below is an example of the output of a simple wxLua application. One thing that this illustrates is that while Lua's main appeal might be as an embedded tool, it can also be used as a stand-alone scripting tool. This also allows it to integrate with other tools and libraries to provide a lot of power with minimal time and resource expenditure.

Just to make the point, this is one of the sample applications that installs with wxLua and doesn't require much code. Here is the source code for this application?

-----------------------------------------------------------------------
-- Name:        minimal.wx.lua
-- Purpose:     'Minimal' wxLua sample
-- Author:      J Winwood
-- Modified by:
-- Created:     16/11/2001
-- RCS-ID:      $Id:  $
-- Copyright:   (c) 2001 J Winwood. All rights reserved.
-- Licence:     wxWindows licence
-----------------------------------------------------------------------

frame = nil

function main()

-- create the frame window
    frame = wx.wxFrame( wx.wxNull, 
                        -1, 
                        "Minimal Demo", 
                        wx.wxPoint(-1, -1), 
                        wx.wxSize(450, 450), 
                        wx.wxDEFAULT_FRAME_STYLE )

-- [[
-- paint event handler
    function Paint(event)
-- create the paint DC
        local dc = wx.wxPaintDC(frame)			
-- call some drawing functions
        dc:DrawRectangle(10, 10, 300, 300);
        dc:DrawRoundedRectangle(20, 20, 280, 280, 20);
        dc:DrawEllipse(30, 30, 260, 260);
        dc:DrawText("A test string", 50, 150);
-- the paint DC will be destroyed by the garbage collector,
-- however on Windows 9x/Me this may be too late (DC's are precious resource)
-- so delete it here
        dc:Delete()
    end

-- connect the paint event handler with the paint event
    frame:ConnectEvent(wx.wxEVT_PAINT, Paint)        
                                      
-- create a simple file menu
    local fileMenu = wx.wxMenu()
    fileMenu:Append(wx.wxID_EXIT, 
                    "&Exit", 
                    "Quit the program")

-- create a simple help menu
    local helpMenu = wx.wxMenu()
    helpMenu:Append(wx.wxID_ABOUT, 
                    "&About", 
                    "About the wxLua Minimal Application")
    
-- create a menu bar and append the file and help menus
    local menuBar = wx.wxMenuBar()
    menuBar:Append(fileMenu, "&File")
    menuBar:Append(helpMenu, "&Help")
 
-- insert the menu bar into the frame
    frame:SetMenuBar(menuBar)

-- create a simple status bar
	frame:CreateStatusBar(1)
	frame:SetStatusText("Welcome to wxLua.")

-- connect the selection event of the exit menu item to an
-- event handler that closes the window
    frame:ConnectEvent(wx.wxID_EXIT, 
				       wx.wxEVT_COMMAND_MENU_SELECTED, 
                       function (event)
                          frame:Close(TRUE)
                       end )

-- connect the selection event of the about menu item
    frame:ConnectEvent(wx.wxID_ABOUT, 
                       wx.wxEVT_COMMAND_MENU_SELECTED, 
                       function (event)
                          wx.wxMessageBox( 'This is the "About" dialog of the Minimal wxLua sample.', 
                                           "About wxLua", 
                                           wx.wxOK + wx.wxICON_INFORMATION, 
                                           frame )
                       end )
    
-- show the frame window
    frame:Show(wx.TRUE)
end

main()

Conclusions

If you are looking for a small, lightweight scripting engine, either for general use or embedding, Lua is a very viable option. Perhaps not as well known as some of the more popular scripting tools like PHP and Python, it still offers advantages over them and could very well become a valuable tool in your ever growing toolbox of development resources.

Over the next few months I'll be looking at other scripting options to help you determine which might fill a need for you.

Resources


Novell Cool Solutions (corporate web communities) are produced by WebWise Solutions. www.webwiseone.com

© 2014 Novell