| 1 | = Lua = |
| 2 | |
| 3 | |
| 4 | 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 . |
| 5 | = Using Lua in RTEMS = |
| 6 | |
| 7 | The Lua library will be included in the RTEMS distribution shortly. Until then, you can find them at http://www.coderedonly.com/GSOC/lua/index.html |
| 8 | = Calling Lua Functions in C = |
| 9 | |
| 10 | This operates exactly as it would in the host operating system. |
| 11 | <tt> |
| 12 | #include <rtems/lua.h> |
| 13 | #include <rtems/lualib.h> |
| 14 | #include <rtems/lauxlib.h> |
| 15 | //This is the reference to the lua interpreter |
| 16 | lua_State* L; |
| 17 | |
| 18 | rtems_task Init( |
| 19 | rtems_task_argument ignored |
| 20 | ) |
| 21 | { |
| 22 | L = lua_open(); |
| 23 | luaL_openlibs(L); |
| 24 | |
| 25 | //Test of basic lua functionality |
| 26 | lua_getglobal(L, "print"); |
| 27 | lua_pushstring(L, "Hello, Lua!"); |
| 28 | lua_call(L, 1, 0); |
| 29 | lua_close(L); |
| 30 | } |
| 31 | </tt>= Spawning a Lua Shell = |
| 32 | |
| 33 | <tt>_Lua_Main(int argc, char** argv)</tt> is a standard <tt>C</tt> main function. Lua spawns a shell whenever |
| 34 | it is called without a script as an argument. Therefore, the end user can spawn a lua shell by modifying the |
| 35 | code below: |
| 36 | <tt> |
| 37 | #include <rtems/lua.h> |
| 38 | #include <rtems/lualib.h> |
| 39 | #include <rtems/lauxlib.h> |
| 40 | |
| 41 | rtems_task Init( |
| 42 | rtems_task_argument ignored |
| 43 | ) |
| 44 | { |
| 45 | char** program = calloc (2, sizeof(char*)); |
| 46 | |
| 47 | //program[0] is the string name of the executing executable. |
| 48 | program[0] = "''program.exe''"; |
| 49 | |
| 50 | _Lua_main(1, program); |
| 51 | } |
| 52 | </tt> |
| 53 | = Executing a Standalone Lua Script = |
| 54 | |
| 55 | Since <tt>_Lua_Main(int argc, char** argv)</tt> is a standard <tt>C</tt> main function, executing lua scripts are |
| 56 | as simple as notifying the interpreter where the file is located. |
| 57 | |
| 58 | ''Note:'' This currently has only been tested with the [wiki:File_Systems#IMFS_File_System_ IMFS] in RTEMS. Technically speaking, it should work |
| 59 | agnostic to the underlying filesystem. |
| 60 | |
| 61 | <tt> |
| 62 | #include <rtems/lua.h> |
| 63 | #include <rtems/lualib.h> |
| 64 | #include <rtems/lauxlib.h> |
| 65 | |
| 66 | rtems_task Init( |
| 67 | rtems_task_argument ignored |
| 68 | ) |
| 69 | { |
| 70 | char** program = calloc (2, sizeof(char*)); |
| 71 | |
| 72 | //program[0] is the string name of the executing executable. |
| 73 | program[0] = "program.exe"; |
| 74 | |
| 75 | //program[1] is the path to the lua script to execute |
| 76 | program[1] = "lua_script.lua"; |
| 77 | |
| 78 | _Lua_main(2, program); |
| 79 | } |
| 80 | </tt> |