Jamie Jennings

lua-users home


After around 5 years away from the Lua community, I have returned and am happy about it!

Below is some work I did in 2009. The ideas remain important, in my opinion, but the code itself does not work with the latest versions of Lua.


My interest in Lua stems from a general interest in programming language design and implementation. For me, this is a diversion, a hobby, and an opportunity to stay connected with programming since I do not code at work.

The Darwin module system

During a summer vacation in June/July 2009, I wrote a first draft of the Darwin module system for Lua, which is an extension to the native Lua module system. Back in the early 1990's, I worked for a few years with [Jonathan Rees]. I had been programming in Lisp, and was fortunate enough to learn Scheme from Jonathan. The module system of [Scheme48] is tremendously useful because it provides a number of security guarantees. The Darwin module system is my own attempt to obtain some of the same guarantees in Lua. In particular:

Since I'm interested in Lua as an embedded language, I want strong guarantees about what capabilities are granted to user code, because the users will not be the developers of my projects; the users will be customizing my projects by injecting their own Lua code.

Relationship to the Lua module system

The Lua module system is extraordinarily flexible. Like Lua itself, the module implementation is fairly "exposed" and therefore can be extended to do all manner of strange and wonderous things. To obtain security guarantees, I wrote Darwin as a layer on top of (next to?) the Lua module system. The Lua module system is fully functioning when you use Darwin. Darwin merely adds an additional power: the power to create a run-time environment that has read-only access to an explicit set of modules and no more.

With Darwin, it is possible to do some things that are awkward (or impossible) without it, e.g.

Due credit

The main ideas in Darwin came from Scheme48, which in turn was influenced by ML. Having just now read Jonathan Rees's [PhD thesis] after writing Darwin, it seems that I've implemented in Lua a fair portion of his security kernel design. There are a few things I have yet to do to round out the features that I think are needed in Darwin, including:

Code and examples

Attached below is a tar/gzip file containing the source code and some tests. I've written some documentation, which is an informal paper describing the design, plus a short reference section:

Darwin is (C) 2009, Jamie Jennings, and will be released under the MIT open source license.

Darwin is pure Lua, and works with Lua 5.1.4. I don't know if it works with older versions.

Here is a file that I load every time I start Lua (after Darwin loads). It defines a set of structures for modules I use often. The way I have Darwin configured (which is the default), Darwin puts entries into package.preload for each declared structure. That way, my code can simply call require, e.g. require "list" or require "lanes". Darwin works fairly smoothly with the existing Lua package system.

structure.declare { name="list"; 

-- Just for demonstration purposes
structure.declare { name="pair"; 
		    signature={"cons", "car", "cdr", "isnull", "null"};

structure.declare { name="recordtype";
		    -- debug package is only needed because recordtype uses "strict"
		    open={"_G", "package"; "table", "string", "debug"};
		    environment=[[ require("recordtype"); return recordtype ]];

structure.declare { name="lprocess";
		    environment=[[ require "lprocess"; return lprocess ]];

structure.declare { name="queuestack";

structure.declare { name="points";
		    open={"_G", "math"};
		    objects={"point", "points"};

Note to MacOS and Windows users: You might need a small edit to _darwinpackage.lua_ in order for Darwin to load native (C/C++) libraries correctly. The change, and the reason for it, are explained in the documentation, under "Limitations". I'll create a work-around at some point.

RecentChanges · preferences
edit · history
Last edited February 13, 2016 12:48 pm GMT (diff)