-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexample.cpp
executable file
·114 lines (88 loc) · 2.35 KB
/
example.cpp
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <cstring>
extern "C" {
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
#if defined(_WIN32) || defined(_WIN64)
# include <windows.h>
# define sleep(x) Sleep((DWORD)(x*1000))
#else
# include <unistd.h>
#endif
#include "pokemon.h"
using namespace std;
// Sleep function exported to Lua
static
int
Zzzzz(lua_State* L) {
(void)luaD_push_location(L, __FILE__, __LINE__);
sleep(lua_tointeger(L, 1));
(void)luaD_pop_location(L);
return 0;
}
static int s_Line = -1;
static
int
ResolveLocation( lua_State* L,
const lua_Debug* dbg,
const char** filePath,
int* line) {
assert(L);
assert(dbg);
assert(filePath);
assert(line);
if (dbg->source && strcmp(dbg->source, "EXAMPLE") == 0) {
*filePath = __FILE__;
*line = s_Line;
return PKMN_LC_RESOLVED;
}
return PKMN_LC_FALLBACK;
}
int
main(int argc, char** argv) {
// set up pokemon
int error = luaD_setup(&argc, argv);
if (error) {
fprintf(stderr, "Pokemon setup error %d\n", error);
return error;
}
// set location resolve callback
(void)luaD_set_location_callback(ResolveLocation);
// create Lua state and register packages
#if LUA_VERSION_NUM >= 502
lua_State* L = luaL_newstate();
#else
lua_State* L = lua_open();
#endif
// setup default libraries
luaL_openlibs(L);
// register a global C function with name 'sleep'
lua_pushcfunction(L, Zzzzz);
lua_setglobal(L, "sleep");
// register Lua state with the debugger
(void)luaD_register(L);
// register inline Lua code with the debugger
const char LuaCode[] = "dofile(\"" POKEMON_LUA_SOURCE_DIR "/example.lua\")"; s_Line = __LINE__ + 1;
// load inline code
error = luaL_loadbuffer(L, LuaCode, sizeof(LuaCode) - 1, "EXAMPLE");
if (error) {
fprintf(stderr, "luaL_loadbuffer error %d\n", error);
} else {
error = lua_pcall(L, 0, LUA_MULTRET, 0);
if (error) {
const char* errorMessage = lua_tostring(L, 1);
fprintf(stderr, "%s\n", errorMessage);
}
}
// unregister Lua state from the debugger
(void)luaD_unregister(L);
// destroy Lua state
lua_close(L);
// tear down pokemon
luaD_teardown();
return error;
}