Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Embedding Lua in Composite OS

Embedding Lua in Composite OS

Chris Anderson and Terry Knowlton

Project for Advanced Operating and Disitributed Systems at George Washington University (CS 6907.83)

http://faculty.cs.gwu.edu/~timwood/wiki/doku.php/teaching:s2013:advos

Chris Anderson

May 02, 2013
Tweet

Other Decks in Research

Transcript

  1. We basically took the Lua source in its entirety and

    added it to Composite, for completeness and ease of upgrading later on.
  2. And after some makefile magic, we managed to output a

    lua_lang.o file that contained the statically compiled Lua libraries, ready to be embedded into C & Composite.
  3. First, just made a dummy component that included the Lua

    headers and initialized the Lua stack.
  4. lua_State *L = luaL_newstate(); printc("Initialized Lua state”); luaL_dostring(L, "return 'Executing

    Lua inline'"); const char *str = lua_tostring(L, -1); printc("Lua returned value is %s", str); === Initialized Lua state === Lua returned value is ‘Executing Lua inline’
  5. // tests loading configuration from a file // loads the

    file, then executes it, which converts the variables // in the file to lua parsable data if (luaL_loadfile(L, "config.txt") || lua_pcall(L, 0, 0, 0)){ printc("error %s", lua_tostring(L, -1)); } lua_getglobal(L, "width"); int width = lua_tonumber(L, -1); printc("Width is %d\n", width); // config.txt -- sample config width = 200 height = 500
  6. -- file 'foo.lua' function fib(n) if n < 2 then

    return n else return fib(n-1) + fib(n-2) end end
  7. if (luaL_loadfile(L, "foo.lua") || lua_pcall(L, 0, 0, 0)){ // if

    either call fails, will return non-zero error code and print // it out here printc("error %s", lua_tostring(L, -1)); } // loads the fib function lua_getglobal(L, "fib"); // pushes param onto lua stack int fib_num = 20 lua_pushnumber(L,fib_num); // executes function into lua state lua_pcall(L,1,1,0); int result = (int)lua_tonumber(L, -1); printc("Fib of %d is %d", fib_num, result); === Fib of 20 is 6765
  8. list = nil function linked_list(n) for i=1,n do list =

    {next = list, value = 'foobar'} end end function clear_list() list = nil end [ 1068.108374] Create 40000 nodes [ 1068.108374] Loop 1 [ 1068.108379] Garbage 2816kb [ 1068.108383] Loop 2 [ 1068.130174] Garbage 4893kb [ 1068.152191] Loop 3 [ 1068.152196] Garbage 2816kb [ 1068.163271] Loop 4 [ 1068.163275] Garbage 5283kb
  9. Powerful and easy to use through the C API. Lua

    is dynamically typed so a mix of data types can be stored in the same table. These tables exist within the Lua state.
  10. // Store a string value in the specified table void

    luakv_putstring_bytable (char *key, char *value, char *table_name) { lua_getglobal(L, table_name); if(!lua_istable(L, -1)) { create_tablespace(table_name); } lua_pushstring(L, key); lua_pushstring(L, value); lua_settable(L, -3); } // Creates a table on the lua_State object for general use. void create_tablespace (char *table_name) { // Create the new table and assign it to global name *table_name lua_newtable(L); lua_setglobal(L, table_name); // Put the newly created table on top of the stack lua_getglobal(L, table_name); }
  11. === Trying out some keyval operations === Starting lua keyvalue

    storage test. New Lua state has been set. === tablespace didn't exist so try to create === Attempting to create new lua table with table_name=DEFAULT === trying to getString before fields are entered. received (null) === Put string of value1 into the default table at key1 === Retrieved the value at key1, return was: value1 === Put number value of 2 into the default table at key2 === pull from the table key2: 2 === Attempting to create new lua table with table_name=TABLE2 === Placed key3=value3 and key4=4 into TABLE2 === attempting to retrieve TABLE2 values from DEFAULT table === should have no values returned, retrieved key3=(null) and key4=0 === should now have returned values from table 2 key3=value3 and key4=4 === stored key3=default_value3 in default table === Pulled key3 from both tables. DEFAULT=default_value3 and TABLE2=value3 === lua_state has been closed
  12. Export Lua as a usable component, using the cbuf or

    torrent interface to deal with sharing memory Create a framework to load and parse configuration files Continue testing Lua functions and third party libraries for compatibility