Esempio n. 1
0
/* new(int) in lua  */
static int newarray(lua_State *L)
{
	int i,n;
	size_t nbytes;
	NumArray *a;

	n = luaL_checkint(L,1);

	/* check 1st argument is integer and bigger than 1
	 * or it will show error message;
	 * */
	luaL_argcheck(L, n >= 1, 1, "invalid size");

	// call lua_newuserdata and give size of structure
	nbytes = sizeof(NumArray) + I_WORD(n -1)*sizeof(unsigned int);
	a = (NumArray *)lua_newuserdata(L,nbytes);
	a->size = n;
	for(i=0 ; i <= I_WORD(n-1) ; i++)
	{
		a->values[i] = 0; /* initial array */
	}

	// add metatable here
	luaL_getmetatable(L,"LuaBook.array");
	lua_setmetatable(L,-2);

	printf("open %s\n",__func__);
	return 1;
}
Esempio n. 2
0
static int newarray(lua_State *L) {
    int i, n;
    size_t nbytes;
    NumArray *a;
    n = luaL_checkint(L, 1);
    luaL_argcheck(L, n >= 1, 1, "invalid size");
    nbytes = sizeof(NumArray) + I_WORD(n - 1) * sizeof(unsigned int);
    a = (NumArray *)lua_newuserdata(L, nbytes);
    a->size = n;
    for (i = 0; i <= I_WORD(n - 1); ++i)
        a->values[i] = 0;
    luaL_getmetatable(L, "LuaBook.array");
    lua_setmetatable(L, -2);
    return 1;
}
Esempio n. 3
0
static unsigned int *getindex(lua_State *L, unsigned int *mask) {
    NumArray *a = checkarray(L);
    int index = luaL_checkint(L, 2) - 1;
    luaL_argcheck(L, 0 <= index && index < a->size, 2, "index out of range");
    *mask = I_BIT(index);
    return &a->values[I_WORD(index)];
}