wiki:Packages/LUA

Version 4 (modified by Scotty Smith, on Aug 23, 2011 at 1:06:14 AM) (diff)

Lua

Lua is an open source scripting language whose main purpose is being embedded in C applications. More can be learned by viewing their website at http://www.lua.org .

Using Lua in RTEMS

For now, you can find the lua rtems files at http://gwu.webhop.net/~cssmith/GSOC/index.html

Calling Lua Functions in C

This operates exactly as it would in the host operating system. <tt>

#include <rtems/lua.h> #include <rtems/lualib.h> #include <rtems/lauxlib.h> This is the reference to the lua interpreter lua_State* L;

rtems_task Init(

rtems_task_argument ignored

) {

L = lua_open(); luaL_openlibs(L);

Test of basic lua functionality lua_getglobal(L, "print"); lua_pushstring(L, "Hello, Lua!"); lua_call(L, 1, 0); lua_close(L);

}

</tt>= Spawning a Lua Shell =

<tt>_Lua_Main(int argc, char argv)</tt> is a standard <tt>C</tt> main function. Lua spawns a shell whenever it is called without a script as an argument. Therefore, the end user can spawn a lua shell by modifying the code below: <tt>

#include <rtems/lua.h> #include <rtems/lualib.h> #include <rtems/lauxlib.h>

rtems_task Init(

rtems_task_argument ignored

) {

_Lua_spawn_shell();

}

</tt>

Spawning a RTEMS Shell

This creates a lua shell that allows for the execution of RTEMS commands. <tt>

#include <rtems/lua.h> #include <rtems/lualib.h> #include <rtems/lauxlib.h>

rtems_task Init(

rtems_task_argument ignored

) {

_Lua_spawn_RTEMS_shell();

}

</tt>

Executing a Standalone Lua Script

Since <tt>_Lua_Main(int argc, char argv)</tt> is a standard <tt>C</tt> main function, executing lua scripts are as simple as notifying the interpreter where the file is located.

Note: This currently has only been tested with the IMFS? in RTEMS. Technically speaking, it should work agnostic to the underlying filesystem.

<tt>

#include <rtems/lua.h> #include <rtems/lualib.h> #include <rtems/lauxlib.h>

rtems_task Init(

rtems_task_argument ignored

) {

char program_args = NULL; /*

Setup arguments for the lua script, if any

*/ _Lua_run_script("path-to-program.lua", num_arguments, program_args);

}

</tt>