Welcome to mirror list, hosted at ThFree Co, Russian Federation.

lua_mtutil.c « src - github.com/windirstat/lua-winreg.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5f6bea2d1fe402ca1014e4ee0e9226e5914364ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
/* push metatable to stack, create if not yet reg, the metatable of the table is its self */
int lua_opentablemt(lua_State *L, const char * libname, const luaL_reg * reg, int upval){
	/*STACK:...<upvals>#*/
	if(luaL_newmetatable(L, libname)){
	/*STACK:...<upvals><tablemt>#*/
		luaL_openlib(L, NULL, reg, upval);
		lua_pushliteral(L, "__index");
	/*STACK:...<tablemt><"__index">#*/
		lua_pushvalue(L, -2);
	/*STACK:...<tablemt><"__index"><tablemt>#*/
		lua_rawset(L, -3);
	/*STACK:...<tablemt>*/
		return 1;//created
	}
	return 0;//opened, all ready created
}

/* new user data with metamethods table */
void * lua_newuserdatamt(lua_State *L, size_t cdata, const char * mtname, const luaL_reg * mtreg){
	void* pdata = lua_newuserdata(L, cdata);/*STACK:...<udata># */
	lua_opentablemt(L, mtname, mtreg, 0);	/*STACK:...<udata><tablemt># */
	lua_setmetatable(L, -2);				/*STACK:...<udata># */
	return pdata;
}

/* new user data with metamethods table and upvalues */
void * lua_newuserdatamtuv(lua_State *L, size_t cdata, const char * mtname, const luaL_reg * mtreg, int upval){
	void* pdata;
	/*STACK:...<upvals>#*/
	lua_opentablemt(L, mtname, mtreg, upval);
	/*STACK:...<tablemt>#*/
	pdata = lua_newuserdata(L, cdata);
	/*STACK:...<tablemt><udata>#*/
	lua_insert(L,-2);
	/*STACK:...<udata><tablemt>#*/
	lua_setmetatable(L, -2);
	/*STACK:...<udata>#*/
	return pdata;
}