LUA EXTENDING 1 References Programming in LUA 2
- Slides: 19
LUA EXTENDING 1. References: “Programming in LUA, 2 nd ed. ”, Roberto Lerusalmschy Chapters 24 -28 2. “Lua Reference Manual” (included in Lua installation or online)
Overview � � Write a module (in C) that can be run in Lua “world” C “world” �Lua ○ Makes heavy use of a virtual stack ○ No Lua/C types ○ Lua does all the memory management without our help. �Python: ○ Reference counting (helping Python with memory mgmt) ○ A ton of conversion functions ○ Many Py_xyz/C types. � We’ll explore most of the techniques needed to embed a Lua interpreter in ssuge.
Font / Color conventions -- lua code… print(“Lua code”) -- lua comment // C code printf(“%s”, “C-code”); // C comment #define LS lua_State * L int lua_gettop(lua_State * L); int lua_gettop(LS); // short-hand.
Simple Example � We’ll create a module (dll in windows): �A variable �A function �A simple class � The module itself is useless, but it illustrates �Converting between Lua and C data �Using things in a different “universe” � Simpler than the lab I had planned �Embedding lua in ssuge �I’ll save that for Lab 1 in ETGG 3802
Step 1: Create the project � [Do it together] � Remember to remove the release and x 64 � In the real lab, I don’t want you to include all lua. c files in the project! � Test program v 1. 0 -- test 01. lua local m = require “test“ -- [discuss search] [Do it!] � What we just did: � � Created a new (empty) module on the Lua Stack and returned it to the lua interpreter (lua_State*)
Step 1, cont. Run it! � Sources of errors: � �Make sure the. dll is in same folder as. lua �… Visual C++ debugger doesn’t work! � Lua doesn’t really have a debugger � �OK, it sort of does, but… �…it can’t inspect C code. � My (bad) solution: Lots of printf statements!
Lua State object � The environment � When running Lua on the command line �There is one lua_State * object �It is passed to all C code run through the command line interpreter. � Later (in ssuge, next semester), we'll create a single state (the "Lua World") lua_State * lua. L_newstate();
Using the documentation � Your best friend in this lab… �https: //www. lua. org/manual/5. 3/ �(Scroll down a bit to the Index) # items popped from stack # items pushed onto stack One of these: • “-” never raises an error • “m” could raise out-of-mem error • “e” could raise an error • “v” could raise an error on purpose
Lua Stack � The way data is exchanged between C and Lua. � Indices: � 1 to n: first, second, …, last �-1 to –n: last, next-to-last, …, first �“meta” (virtual) indices – not really on stack. ○ Globals ○ Registry ○…
Step 2: test program, v 2. 0 � Requires us to add (C) function(s) and add them to the module -- test 02. lua local m = require “test" print(m. adder(5, 3)) print(m. adder(5. 2, 3. 1)) print(m. adder(“abc”, 5)) � [Do it!] -- 8. 3 -- error
Functions to manipulate the stack // Gets the top (i. e. the number // of things on the stack. ) int lua_gettop(LS); // Removes elements (or add’s nils) // to resize the stack void lua_settop(LS, int num); // Pushes a copy of the indexed value // to the top of the stack void lua_pushvalue(LS, int index);
Functions to manipulate the stack, cont. // Removes the indexed value and // collapses the “hole”. int lua_remove(LS, int index); // Moves the top element to the given // index. void lua_insert(LS, int index); // Pops the top value and replaces the // value at the given index with it void lua_replace(LS, int index);
Functions to get values � int lua_isnumber(LS, int index); � int lua_tointeger(LS, int index); � double lua_tonumber(LS, int index); � int lua_isstring(LS, int index) � char * lua_tostring(LS, int index); � Another method: �int lua. L_checkint(LS, int index); // error-check �char * lua. L_checkstring(LS, int index)
Functions to add values to the stack � lua_pushinteger(LS, int val); � lua_pushnumber(LS, double val); � lua_pushstring(LS, char * val); �…
A handy debugging tool void stack_dump(lua_State * L) { int i; char type_str[256], value_str[256]; printf("======n|STACK DUMPn======n"); for (i=1; i<=lua_gettop(L); i++) { value_str[0] = '