Palomino - Lua Scripting

©2004,2008  Jim E. Brooks   http://www.palomino3d.org


Contents


Overview

[2008/05]

The Lua interpreter is integrated with the simulator's source code. The simulator has a set of C functions to interface with Lua scripts.

The lua module consists of:

palomino/src/lua_lang  # the Lua interpreter
palomino/src/lua_bind  # the simulator's C interface for Lua scripts
palomino/scripts       # Lua scripts (*.lua files)

Lua/C++ Binding Reference

[2008/05]

See the lua_bind namespace in doxygen.


Lua Execution

[2008/05]

The Lua interpreter stores its state in a lua_State struct. This means that every time the simulator's C++ code calls Lua and Lua returns back, Lua remembers all of its objects, global vars, etc.

The C++ simulator calls Lua at certain points:

In turn, Lua can call a set of C functions exported to Lua by the C++ simulator.


Lua Class

[2008/05]

This C++ class abstracts the Lua interpreter. Lua::RunScript() has the ability to cache bytecode in case the same Lua script is re-executed [reference].


C++ Classes as Lua Objects

[2008/05]

To avoid C++ new/delete problems, instances of C++ classes are created as reference-counted pointers (osg::Referenced). C++ classes are exported to Lua as metatables. The "__gc" method is called by Lua's garbage-collector. __gc() is implemented by a C function that calls ref_ptr::unref() to decrement the reference-count. C++ delete is not called explicitly thus avoiding problems if the simulator or OSG is still using a C++ object. This is implemented by allocating a Lua "userdata" object. Then C++ "placement new" is used to construct an osg::ref_ptr at that memory location.


Last modified: Mon May 26 18:28:31 EDT 2008