I am using C API embedded with Lua. My goal is that: pass an array of integers into Lua and calculate their factorials, then the results are passed back to C and printed out.
To realize the goal, my C code is:
#include <lua.h>
#include <lauxlib.h>
#include <stdlib.h>
#include <stdio.h>
int main(void){
int status, result, i;
double fac;
lua_State *L; // set Lua state
L = luaL_newstate();
luaL_openlibs(L);
status = luaL_loadfile(L, "factorial.lua"); // load the Lua script for factorial calculation
if (status) {
fprintf(stderr, "Couldn't load file: %s\n", lua_tostring(L, -1));
exit(1);
}
lua_newtable(L);
for (i = 1; i <= 10; i++) {
lua_pushnumber(L, i); /* Push the table index */
lua_pushnumber(L, i*2); /* Push the cell value */
lua_rawset(L, -3); /* Stores the pair in the table */
}
lua_setglobal(L, "foo");
result = lua_pcall(L, 0, LUA_MULTRET, 0);
if (result) {
fprintf(stderr, "Failed to run script: %s\n", lua_tostring(L, -1));
exit(1);
}
// the following loop is for factorial print-out
while (lua_next(L, -1) != 0) {
fac = lua_tonumber(L, -1);
printf("%.0f\n", fac);
lua_pop(L, 1);
}
lua_close(L);
return 0;
}
And my Lua script is like:
-- this is the function to calculate the factorial
function fact(n)
if n == 0 then
return 1
else
return n * fact(n-1)
end
end
io.write("We calculate the factorial of the following numbers: \n")
return_table = {}
for i = 1, #foo do
n = foo[i]
factorial_result = fact(n)
print(n)
table.insert(return_table, factorial_result)
end
io.write("Here we show the results: \n")
for i=1,10 do
return(return_table[i])
end
The compilation goes well but when I run it on terminal, I got:
We calculate the factorial of the following numbers:
2.0
4.0
6.0
8.0
10.0
12.0
14.0
16.0
18.0
20.0
Here we show the results:
Segmentation fault (core dumped)
I don't know why the result is like this. It seems that no problem with the passing from C to Lua but there is problem from Lua to C. Can anyone help me with this?
Your code has two problems:
return
statement inside a loop in your Lua code. Once a return statement is hit, your Lua script is done executing. It'll return that one value to the C code and it's going to be a number, not the table your C code is expecting.lua_next
without first pushing a starting key onto the stack.In your Lua code, change this:
for i=1,10 do
return(return_table[i])
end
To this:
return(return_table) -- don't need parentheses here, btw
In your C code, add lua_pushnil(L)
before your while loop.