wiki:Packages/LUA
Notice: We have migrated to GitLab launching 2024-05-01 see here: https://gitlab.rtems.org/

Version 1 (modified by Scotty Smith, on 08/02/11 at 23:51:15) (diff)

Created page with "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 Lu..."

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

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

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

) {

char program = calloc (2, sizeof(char*));

program[0] is the string name of the executing executable. program[0] = "program.exe";

_Lua_main(1, program);

}

</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 = calloc (2, sizeof(char*));

program[0] is the string name of the executing executable. program[0] = "program.exe";

program[1] is the path to the lua script to execute program[1] = "lua_script.lua";

_Lua_main(2, program);

}

</tt>