I'm trying to create an userdata with Lua C API with a metatable associated where I will collect a matrix.
What I can't get is how to set every component of the initialized matrix to zero.
I compile my Lua module C code as I described here.
The C code I have is the following:
#include "lauxlib.h"
#include "lua.h"
typedef struct {
LUA_NUMBER data[1][1];
int row;
int col;
}matrix;
// Create a matrix full of zeros
static int lb_newmatrix(lua_State *L)
{
// Variable declarations
int i,j;
matrix *temp;
// Input checks
if (lua_gettop(L)!=2)
{
lua_pushstring(L,"\n Two input required");
lua_error(L);
}
//--> Check I° index m riga
luaL_checktype(L,1,LUA_TNUMBER);
if (lua_tonumber(L,1)<0)
{
lua_pushstring(L,"\nRow number must be positive");
lua_error(L);
}
//--> Check II° index n colonna
luaL_checktype(L,2,LUA_TNUMBER);
if (lua_tonumber(L,2)<0)
{
lua_pushstring(L,"\nColumn number must be positive");
lua_error(L);
}
// Computation of memory allocation
int m = lua_tonumber(L,1);
int n = lua_tonumber(L,2);
size_t nbyte = 2*sizeof(int)+sizeof(LUA_NUMBER)*m*n;
size_t nbyte2 = sizeof(matrix)+sizeof(LUA_NUMBER)*(m*n-1);
// Memory allocation
temp = (matrix *)lua_newuserdata(L,nbyte);
// Matrix dimension setting
temp->row = m;
temp->col = n;
// Matrix inizialization
/* PROBLEM HERE */
for (i=1;i==m;i++)
{
for(j=1;j==n;j++)
{
temp->data[i][j] = 0;
}
}
//-------------------------------
// If I de-comment these line,
// the matrix is written but
// element with equal sum indices
// rewrite!!!
//-------------------------------
// temp->data[1][1] = nbyte;
// temp->data[1][2] = nbyte2;
// temp->data[1][3] = 13;
// temp->data[2][1] = nbyte2;
// temp->data[2][2] = 22;
// temp->data[2][3] = 23;
// temp->data[3][1] = 31;
// temp->data[3][2] = 32;
// temp->data[3][3] = 33;
// Link the userdata to the metatable "basic"
luaL_getmetatable(L,"basic");
lua_setmetatable(L,-2);
return 1;
}
static int lb_index(lua_State *L)
{
/* Check input Numbers */
if (lua_gettop(L)>3)
{
lua_pushstring(L,"\nOnly two inputs are needed:\n1) Point\n2) N° row\n3) N° col");
lua_error(L);
}
/* Check if the first input is userdata basic */
matrix *temp = (matrix *)luaL_checkudata(L,1,"basic");
/* I° index check ROW */
luaL_checktype(L,2,LUA_TNUMBER);
if (lua_tointeger(L,2)<0||lua_tointeger(L,2)>temp->row)
{
lua_pushstring(L,"\n First index should be 1 to n");
lua_error(L);
}
/* II° index check COLUMN */
luaL_checktype(L,3,LUA_TNUMBER);
if (lua_tointeger(L,3)<0||lua_tointeger(L,3)>temp->col)
{
lua_pushstring(L,"\n Second index should be 1 to m");
lua_error(L);
}
int row = lua_tointeger(L,2);
int col = lua_tointeger(L,3);
/* Insert the index value of userdata on top of the stack */
lua_pushnumber(L,temp->data[row][col]);
return 1;
}
/**********************
* MODULE DECLARATION *
**********************/
static const struct luaL_Reg LuaBasic_f [] = {//
{"NewMatrix",lb_newmatrix},
{ "__index", lb_index},
{ NULL, NULL}};
static const struct luaL_Reg LuaBasic_m [] = {//
{ NULL, NULL}};
LUA_API int luaopen_LuaBasic(lua_State *L)
{
/* Insert basic metatable "basic" into the stack */
luaL_newmetatable(L,"basic");
/* Copy the "basic" metatable
and push it into the stack */
lua_pushvalue(L,-1);
/* basic["__index"] = basic */
lua_setfield(L,-2,"__index");
/* register all the function
into LuaBasic_m into the
basic table metatable */
luaL_setfuncs(L,LuaBasic_m,0);
luaL_newlib(L,LuaBasic_f);
return 1;
}
The related Lua Code is the following:
lb = require("LuaBasic")
A = lb.NewMatrix(3,3)
print(A)
print("--------------")
print(lb.__index(A,1,1))
print(lb.__index(A,1,2))
print(lb.__index(A,1,3))
print(lb.__index(A,2,1))
print(lb.__index(A,2,2))
print(lb.__index(A,2,3))
print(lb.__index(A,3,1))
print(lb.__index(A,3,2))
print(lb.__index(A,3,3))
print("--------------")
print("row = "..lb.GetRow(A))
print("col = "..lb.GetCol(A))
and the output I get is:
userdata: 007C2940
--------------
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
--------------
row = 3
col = 3
I don't understand why I can't write the zero values into initialized matrix.
What do I do wrong?