diff options
| author | chai <chaifix@163.com> | 2019-09-23 08:00:18 +0800 |
|---|---|---|
| committer | chai <chaifix@163.com> | 2019-09-23 08:00:18 +0800 |
| commit | 0382bd8a03b82b9c154d896e819ee7fed24025eb (patch) | |
| tree | 5e398fb2cc0e94046cfeee1f556a3a089b419570 | |
| parent | 695f88366e507032a3a9e1eb747cc48610a4cbe1 (diff) | |
*枚举
| -rw-r--r-- | src/02-enum/enum.lua | 18 | ||||
| -rw-r--r-- | src/04-thread/main.cpp | 78 | ||||
| -rw-r--r-- | src/04-thread/test.lua | 11 | ||||
| -rw-r--r-- | src/configure.h | 2 |
4 files changed, 98 insertions, 11 deletions
diff --git a/src/02-enum/enum.lua b/src/02-enum/enum.lua index 9c64c1f..b0e5abd 100644 --- a/src/02-enum/enum.lua +++ b/src/02-enum/enum.lua @@ -1,24 +1,22 @@ -local function makeEnum(enumtable) - if enumtable == nil or type(enumtable) ~= "table" then +local function enum(tbl) + if tbl == nil or type(tbl) ~= "table" then return nil end - enumtable.__index = enumtable - enumtable.__newindex = function() + tbl.__index = tbl + tbl.__newindex = function() print("can not modify enum") end - local e = {} - setmetatable(e, enumtable) - return e + return setmetatable({}, tbl) end -local function main() - local mode = makeEnum({ +function main() + local mode = enum { SinglePlayer = 1, TwoPlayers= 2, ThreePlayers= 3, FourPlayers= 4, PVCom= 5, - }) + } mode.SinglePlayer = 2 print(mode.SinglePlayer) end diff --git a/src/04-thread/main.cpp b/src/04-thread/main.cpp index e69de29..56b5066 100644 --- a/src/04-thread/main.cpp +++ b/src/04-thread/main.cpp @@ -0,0 +1,78 @@ +#include "../configure.h" +#if BUILD_TEST == TEST_4 + +#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; +} + +luaL_reg fns[] = { + {"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 f(int& a) +{ + a = 20; + return a; +} + +int foo(int a, int b) +{ + return a + b; +} + +int main(int args, char* argv[]) +{ + int a = 10; + int r = foo(a, f(a)); + printf("%d\n", r); + + lua_State* L = luaL_newstate(); + openlibs(L); + + luaL_dofile(L, "04-thread/test.lua"); + + lua_close(L); + return 0; +} + +#endif
\ No newline at end of file diff --git a/src/04-thread/test.lua b/src/04-thread/test.lua index e69de29..ec22646 100644 --- a/src/04-thread/test.lua +++ b/src/04-thread/test.lua @@ -0,0 +1,11 @@ + +local function main() + local a = 1 + print(debug.getlocal(1, 1)) +end + + +local suc, rval = xpcall(main, debug.traceback) +if not suc then + print(rval) +end diff --git a/src/configure.h b/src/configure.h index bc107e1..6289d19 100644 --- a/src/configure.h +++ b/src/configure.h @@ -13,7 +13,7 @@ #define TEST_10 10 -#define BUILD_TEST TEST_1 +#define BUILD_TEST TEST_2 //----------------------------------------------------------------------------------------------------- |
