Lua Server Pages

Lua Server Pages, a Lua scripting engine plug-in that blows away any other approach to embedded web development, offers a dramatic short cut to traditional C server pages. The Lua Server Pages plug-in (included in the Barracuda Application Server) makes developing applications extremely fast, simple, and safe. Notably, it isn't limited to developing web applications, but can also be used to provide high-level socket APIs that make developing secure socket applications very easy. We also provide HTTP client libraries, SMTP (mail) libraries, etc. In fact, we estimate that up to 80% of a modern embedded system could be implemented using Lua Server Pages, ensuring dramatically reduced development time.

How Lua Works

Lua is a scripting language and includes a compiler that compiles the scripts on the fly to a register-based instruction set. The instruction set is then executed by the virtual machine. The Lua compiler and the virtual machine are implemented in C code and can run in virtually any device with sufficient memory. The Barracuda Application Server, the Lua Server Pages plug-in, and all plug-ins required by the Lua Server Pages use roughly 600 kbytes of ROM. We recommend a minimum of 500 kbytes RAM for running your scripts.

Lua is the only module in the Barracuda Application Server not developed by Real Time Logic. Lua is licensed under the terms of the MIT license and can therefore be used in any commercial product without any restrictions. See the Lua license for more information.

Customers Discover the Benefits of Lua Server Pages

We have customers using the Lua Server Pages plug-in in everything from deeply embedded monolithic systems, such as in Xilinx FPGAs, to high-level host systems, including Linux and Windows. The Lua virtual machine can be compiled for any microprocessor architecture, including CPUs embedded inside FPGAs such as an 80MHz ARM processor that is sufficient to run the Lua virtual machine. The web page you are reading now is managed by Lua Server Pages.

Customer Testimonial:

"It was great that you drove the point across regarding the benefits of using Lua. I think that most embedded people, such as myself, would initially gravitate towards C code and CSP because it's a familiar environment and disregard Lua without realizing the huge benefits that can be gained with only a little bit of an upfront cost of having to learn the Lua language. BTW, you definitely made us think about Lua a whole lot more and not just as a server-side scripting language but as a scripting language that we could potentially use throughout our entire project." - Honeywell employee attending our three day training.

Lua Server Pages Advantages for Embedded Web Development

Lua is not your average scripting language. Lua is small, fast, portable, and embeddable. These cornerstones of the language make it well-suited for embedded devices, web applications, and protocol stacks.

Lua Book
The Lua Book
  • Lua Server Pages are designed to be embedded.
  • Lua Server Pages use one of the fastest scripting languages available.
  • Lua Server Pages are small.
  • Lua Server Pages are easy to learn.
  • Lua Server Pages are designed to help you cut development cost and time to market.

See our whitepaper Lua fast-tracks embedded web application development for more information on how Lua simplifies and speeds up the development process.

Lua Server Pages Development Process

The Lua Server Pages development cycle is a straight-forward, three-step process. Recompiling a modified Lua Server Page and executing the Lua Server Page is as simple as refreshing the browser window. When the browser sends the request, the server loads, compiles, and executes the new script. The figure to the right illustrates this process.

The Lua Server Pages plug-in comprises the Lua engine and interface logic to the Barracuda Application Server's C API. The interface logic is referred to as a "Lua binding" and makes it possible for Lua scripts to call C functions. We provide bindings to most of the C APIs in the Barracuda Application Server, including plug-ins such as the WebDAV Server. The Lua Server Pages plug-in is designed to be extended and lets you add your own device management APIs. We also provide an online C parser that automatically generates Lua bindings for your C device management APIs.

Lua Server Pages require that you provide some start-up code in C, but no C code is required when assembling web-server components. All components, including opening server listen ports, managing SSL certificates and so on, can be dynamically managed from within a Lua script. Lua also makes it possible to dynamically add and remove server components in a running system. For example, you can dynamically change the listen port numbers without restarting the server.

Refresh Browser

Lua Server Pages Development Process

A question that is frequently asked by our customers is how to access their own proprietary C functions from Lua code. Lua to C/C++ access is called a Lua binding and can be created manually or by using our automatic Lua Binding Generator. See the online tutorials and how to interface Lua to existing C/C++ code for more information.

A Look at the Design of Lua

Roberto Ierusalimschy, the designer of Lua, explains the benefit in using Lua on small resource constrained devices in the following video:

Lua Example: The Eight-Queen Puzzle

The following example is from the Lua book. You may view the excerpt from the Lua book by clicking the Lua book image above and by navigating to Amazon. Click the "Look inside" link and navigate to chapter two.

You can execute the following example on our online demo server. The goal is to put eight queens in a chessboard so that no queen can attack another one.

<pre><?lsp -- Run as LSP page

-- The following fix makes the example print to the HTTP response buffer
local io={write=function(...) response:write(...) end}

local N = 8

-- check whether position (n,c) is free from attacks
local function isplaceok(a, n, c)
    for i = 1, n - 1 do     -- for each queen already placed
        if(a[i] == c) or             -- same column?
            (a[i] - i == c - n) or   -- samn diagonal?
            (a[i] + i == c + n) then -- same diagonal?
            return false
    return true -- no attacks, place is OK

-- print a board
local function printsolution(a)
    for i = 1, N do
        for j = 1, N do
            io.write(a[i] == j and "X" or "-", " ")
    -- not needed

-- add to board 'a' all queens from 'n' to 'N'
local function addqueen(a, n)
    if n > N then   -- all queens have been placed?
        return true -- indicate that this was a success
    else    -- try to place the n-th queen
        for c = 1, N do
            if isplaceok(a, n, c) then
                a[n] = c    -- place n-th queen at column 'c'
                local ret = addqueen(a, n + 1)
                if ret == true then -- addqueen returns true if
                    return true     -- it found a solution. if that
                end                 -- is the case, our work here 
            end                     -- is done.

-- run the program
addqueen({}, 1)