Hardware Design: SIE
Sign in or create your account | Project List | Help
Hardware Design: SIE Git Source Tree
Root/
| 1 | Examples from http://www.wellho.net/mouth/1844_Calling-functions-in-C-from-your-Lua-script-a-first-HowTo.html |
| 2 | |
| 3 | |
| 4 | The code is very similar, but you'll notice six additions - in the Lua: |
| 5 | |
| 6 | a) We have added a parameter to the call to dothis in the Lua |
| 7 | b) We have added an assignment to collect a return value from the C |
| 8 | |
| 9 | summat = cstuff.dothis(value) |
| 10 | |
| 11 | c) We have printed out these values |
| 12 | print ("Values in Lua now",value, summat) |
| 13 | |
| 14 | |
| 15 | and in the C: |
| 16 | |
| 17 | d) We have used lua_tonumber to collect a numeric value that was passed from the Lua to the C; you'll note that all such numbers are treated as doubles. The parameter "1" indicates 1 down - i.e. top of - the state stack. |
| 18 | |
| 19 | double trouble = lua_tonumber(L, 1); |
| 20 | |
| 21 | e) We have performed a calculation (representative of something being done in the C code) and pushed the result of this back onto the Lua state stack so that it can be picked up once we have returned from the C to the Lua |
| 22 | |
| 23 | lua_pushnumber(L, 16.0 - trouble); |
| 24 | |
| 25 | f) We have changed return 0 into return 1 to indicate that one result is being returned. |
| 26 | |
| 27 | return 1; } |
| 28 | |
| 29 | |
| 30 |
Branches:
master
