better API, put them in global name space

This commit is contained in:
Ryan Voots 2016-04-24 13:36:53 -07:00
parent e15650cb1f
commit 0dd833306d

View file

@ -3,54 +3,11 @@
#include "lua.hpp"
#include "log.h"
// CREATE ARRAY TYPE AND EXPOSE matrixpanel TO LUA.
// metatable method for handling "array[index]"
/* static int matrixpanel_index (lua_State* L) {
CRGB** parray = (CRGB **) luaL_checkudata(L, 1, "matrixpanel");
int index = luaL_checkint(L, 2);
lua_pushnumber(L, (*parray)[index-1]);
return 1;
}
// metatable method for handle "array[index] = value"
static int matrixpanel_newindex (lua_State* L) {
CRGB** parray = (CRGB **) luaL_checkudata(L, 1, "matrixpanel");
int index = luaL_checkint(L, 2);
int value = luaL_checkint(L, 3);
(*parray)[index-1] = value;
return 0;
} */
int8_t matrixpanel_userdata = 0; // used just to have some userdata
static int matrixpanel_swapbuffer(lua_State *L) {
matrixpanel.swapBuffers(true);
return 0;
}
void pushPanel (lua_State *L)
{
int8_t *pi = (int8_t *)lua_newuserdata(L, sizeof(int8_t));
*pi = matrixpanel_userdata;
luaL_getmetatable(L, "matrixpanel");
lua_setmetatable(L, -2);
}
static int matrixpanel_new (lua_State *L) {
pushPanel(L);
return 1;
}
static int matrixpanel_gc (lua_State *L) {
output_log("dropped matrixpanel\n");
return 0;
}
static int matrixpanel_tostring (lua_State *L) {
lua_pushfstring(L, "matrixpanel string");
return 1;
}
static int matrixpanel_fillscreen (lua_State *L) {
int c = luaL_checkint(L, 1);
matrixpanel.fillScreen(c);
@ -326,7 +283,6 @@ static int matrixpanel_gettextbounds (lua_State *L) {
}
static const struct luaL_Reg matrixpanel_methods[] = {
{ "new", matrixpanel_new },
{ "swapBuffer", matrixpanel_swapbuffer },
{ "fillScreen", matrixpanel_fillscreen },
{ "drawPixel", matrixpanel_drawpixel },
@ -364,28 +320,7 @@ static const struct luaL_Reg matrixpanel_methods[] = {
{NULL, NULL}
};
static const struct luaL_Reg matrixpanel_metamethods[] = {
{ "__gc", matrixpanel_gc },
{ "__tostring", matrixpanel_tostring },
{NULL, NULL}
};
int luaopen_matrixpanel (lua_State* L) {
lua_newtable(L);
luaL_setfuncs(L, matrixpanel_methods, 0);
lua_setglobal(L, "matrixpanel");
luaL_newmetatable(L, "matrixpanel"); /* create metatable for matrixpanel,
add it to the Lua registry */
luaL_setfuncs(L, matrixpanel_methods, 0);
lua_pushliteral(L, "__index");
lua_pushvalue(L, -3); /* dup methods table*/
lua_rawset(L, -3); /* metatable.__index = methods */
lua_pushliteral(L, "__metatable");
lua_pushvalue(L, -3); /* dup methods table*/
lua_rawset(L, -3); /* hide metatable:
metatable.__metatable = methods */
lua_pop(L, 1); /* drop metatable */
return 1;
luaL_newlib(L, matrixpanel_methods);
return 0;
}