Strict Standards: Declaration of Doku_Renderer_metadata::table_open() should be compatible with Doku_Renderer::table_open($maxcols = NULL, $numrows = NULL, $pos = NULL) in D:\www\www865\dokuwiki\inc\parser\metadata.php on line 24 Strict Standards: Declaration of Doku_Renderer_metadata::table_close() should be compatible with Doku_Renderer::table_close($pos = NULL) in D:\www\www865\dokuwiki\inc\parser\metadata.php on line 24

Usage of Lua in a Lab Device

Some code snippets document a real implementation of an embedded/extended Lua into a Qt application.

The following function is called when the user presses the „init“ button on the GUI. It calls the Lua VM with the init command string (“cube.init()“)

void MainWindow::pushButtonInitPressed(void)
  LuaVM->run("ok, result = cube.init()");

The LuaVM receives a string to execute. The string must be a valid lua chunk. The function creates and initializes the Lua VM and executes the function dostring() to process the lua chunk. At the end with lua_close() the allocated memory is freed.

bool LuaVM::run(QString luaCommand)
    L = lua_open();  /* create state */
    lua_gc(L, LUA_GCSTOP, 0);  /* stop garbadge collector during initialization */
    luaL_openlibs(L);  /* open libraries */
    lua_gc(L, LUA_GCRESTART, 0); /* restart garbadge collector */
    dostring(L, qPrintable(luaCommand));

C++ implementation of the Lua function cube.init()

static int cube_init(lua_State *L)
  e_ERROR err = ERROR_OK;
  err = cube->init();
  lua_pushinteger(L, err);
  return 1;

Table with all Lua functions and the corresponding implementations in C++.

static const struct luaL_reg cube_functions[] = {
  { "init", cube_init },
  { NULL, NULL }

This function connects the Lua function cube.init() with its implementation in C++.

int luaopen_cube (lua_State *L)
  luaL_openlib(L,"cube", cube_functions,0);
  return 1;
lua_lab_device.txt · Zuletzt geändert: 2015/02/03 10:01 von Claus Kühnel
Falls nicht anders bezeichnet, ist der Inhalt dieses Wikis unter der folgenden Lizenz veröffentlicht:CC Attribution-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki