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
|
#include "../configure.h"
#if BUILD_TEST == TEST_1
#include <windows.h>
#include <time.h>
#include <conio.h>
static int l_GetTime(lua_State* L)
{
float second = (double)clock() / CLOCKS_PER_SEC;
lua_pushnumber(L, second);
return 1;
}
static int l_Sleep(lua_State* L)
{
float t = lua_tonumber(L, 1);
Sleep(t * 1000);
return 0;
}
static int l_Kbhit(lua_State* L)
{
int c = _kbhit();
lua_pushboolean(L, c);
return 1;
}
static int l_GetChar(lua_State* L)
{
char str[2] = { 0,0 };
str[0] = _getch();
lua_pushstring(L, str);
return 1;
}
static int __gc(lua_State* L)
{
return 0;
}
static int l_createObj(lua_State* L)
{
int* u = (int*)lua_newuserdata(L, sizeof(int));
lua_newtable(L);
lua_pushstring(L, "__gc");
lua_pushcfunction(L, __gc);
lua_rawset(L, -3);
lua_setmetatable(L, -2);
return 1;
}
luaL_reg fns[] = {
{ "createObj", l_createObj },
{"GetTime", l_GetTime } ,
{"Sleep", l_Sleep } ,
{"Kbhit", l_Kbhit },
{"GetChar", l_GetChar },
{0, 0}
};
void openlibs(lua_State* L)
{
luaL_openlibs(L);
///luaL_register(L, NULL, fns);
luax_registerglobal(L, fns);
}
int main(int args, char* argv[])
{
lua_State* L = luaL_newstate();
openlibs(L);
luaL_dofile(L, "01-coroutine/test.lua");
lua_close(L);
return 0;
}
#endif
|