aboutsummaryrefslogtreecommitdiff
path: root/deep_test/deep_test.c
blob: a48ecb2757e3e649db23fca7640abcf71c2d40d6 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

#include "deep.h"

#if (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC)
#define LANES_API __declspec(dllexport)
#else
#define LANES_API
#endif // (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC)

// ################################################################################################

static int deep_tostring(lua_State* L)
{
	lua_pushliteral( L, "I am a deep_test");
	return 1;
}

// ################################################################################################

static luaL_Reg const deep_mt[] =
{
	{ "__tostring", deep_tostring},
	{ NULL, NULL }
};

// ################################################################################################

static void* deep_test_id( lua_State* L, enum eDeepOp op_)
{
	switch( op_)
	{
		case eDO_new:
		{
			void* allocUD;
			lua_Alloc allocF = lua_getallocf( L, &allocUD);
			void* deep_test = allocF( allocUD, NULL, 0, sizeof(void*));
			return deep_test;
		}

		case eDO_delete:
		{
			void* allocUD;
			lua_Alloc allocF = lua_getallocf( L, &allocUD);
			void* deep_test = lua_touserdata( L, 1);
			allocF( allocUD, deep_test, sizeof(void*), 0);
			return NULL;
		}

		case eDO_metatable:
		{
			lua_newtable( L);
			luaL_setfuncs( L, deep_mt, 0);
			luaG_pushdeepversion( L);
			return NULL;
		}

		case eDO_module:
		return "deep_test";

		default:
		{
			return NULL;
		}
	}
}

// ################################################################################################

int luaD_new_deep(lua_State* L)
{
	return luaG_newdeepuserdata( L, deep_test_id);
}

// ################################################################################################

static luaL_Reg const deep_module[] =
{
	{ "new_deep", luaD_new_deep},
	{ NULL, NULL}
};

// ################################################################################################

extern int __declspec(dllexport) luaopen_deep_test(lua_State* L)
{
	luaL_newlib( L, deep_module);
	return 1;
}