1
/* embed3.cpp A C++ embeded interpreter
3
This will register a C++ class with Lua, and then call a Lua function
4
passing C++ objects to this function.
8
/* Deal with Microsoft's attempt at deprecating C standard runtime functions */
9
#if !defined(SWIG_NO_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER) && !defined(_CRT_SECURE_NO_DEPRECATE)
10
# define _CRT_SECURE_NO_DEPRECATE
13
/* Deal with Microsoft's attempt at deprecating methods in the standard C++ library */
14
#if !defined(SWIG_NO_SCL_SECURE_NO_DEPRECATE) && defined(_MSC_VER) && !defined(_SCL_SECURE_NO_DEPRECATE)
15
# define _SCL_SECURE_NO_DEPRECATE
30
#include "swigluarun.h" // the SWIG external runtime
32
/* the SWIG wrappered library */
33
extern "C" int luaopen_example(lua_State*L);
38
// this code pushes a C++ pointer as well as the SWIG type onto the Lua stack
39
bool push_pointer(lua_State*L, void* ptr, const char* type_name, int owned = 0) {
40
// task 1: get the object 'type' which is registered with SWIG
41
// you need to call SWIG_TypeQuery() with the class name
42
// (normally, just look in the wrapper file to get this)
43
swig_type_info * pTypeInfo = SWIG_TypeQuery(L, type_name);
45
return false; // error
46
// task 2: push the pointer to the Lua stack
47
// this requires a pointer & the type
48
// the last param specifies if Lua is responsible for deleting the object
49
SWIG_NewPointerObj(L, ptr, pTypeInfo, owned);
53
/* This is an example of how to call the Lua function
55
its very tedious, but gives you an idea of the issues involed.
57
int call_onEvent(lua_State *L, Event e) {
60
push a, push b, call 'add' check & return res
62
top = lua_gettop(L); /* for later */
63
lua_pushstring(L, "onEvent"); /* function name */
64
lua_gettable(L, LUA_GLOBALSINDEX); /* function to be called */
65
if (!lua_isfunction(L, -1)) {
66
printf("[C++] error: cannot find function 'OnEvent'\n");
67
lua_settop(L, top); // reset
70
// push the event object
71
push_pointer(L, &e, "Event *", 0);
72
if (lua_pcall(L, 1, 0, 0) != 0) /* call function with 1 arguments and no result */
74
printf("[C++] error running function `OnEvent': %s\n", lua_tostring(L, -1));
75
lua_settop(L, top); // reset
78
lua_settop(L, top); /* reset stack */
83
int main(int argc, char* argv[]) {
84
printf("[C++] Welcome to the simple embedded Lua example v3\n");
85
printf("[C++] We are in C++\n");
86
printf("[C++] opening a Lua state & loading the libraries\n");
87
lua_State *L = lua_open();
91
printf("[C++] now loading the SWIG wrappered library\n");
93
printf("[C++] all looks ok\n");
95
printf("[C++] lets create an Engine and pass a pointer to Lua\n");
97
/* this code will pass a pointer into lua, but C++ still owns the object
98
this is a little tedious, to do, but lets do it
99
we need to pass the pointer (obviously), the type name
100
and a flag which states if Lua should delete the pointer once its finished with it
101
The type name is a class name string which is registered with SWIG
102
(normally, just look in the wrapper file to get this)
103
in this case we don't want Lua to delete the pointer so the ownership flag is 0
105
push_pointer(L,&engine,"Engine *",0);
106
lua_setglobal(L, "pEngine"); // set as global variable
108
printf("[C++] now lets load the file 'runme.lua'\n");
109
printf("[C++] any lua code in this file will be executed\n");
110
if (luaL_loadfile(L, "runme.lua") || lua_pcall(L, 0, 0, 0)) {
111
printf("[C++] ERROR: cannot run lua file: %s", lua_tostring(L, -1));
114
printf("[C++] We are now back in C++, all looks ok\n");
117
printf("[C++] Lets call the Lua function onEvent(e)\n");
118
printf("[C++] We will give it different events, as we wish\n");
119
printf("[C++] Starting with STARTUP\n");
121
ev.mType = Event::STARTUP;
123
printf("[C++] ok\n");
124
printf("[C++] now we will try MOUSEPRESS,KEYPRESS,MOUSEPRESS\n");
125
ev.mType = Event::MOUSEPRESS;
127
ev.mType = Event::KEYPRESS;
129
ev.mType = Event::MOUSEPRESS;
131
printf("[C++] ok\n");
132
printf("[C++] Finally we will SHUTDOWN\n");
133
ev.mType = Event::SHUTDOWN;
135
printf("[C++] ok\n");
138
printf("[C++] all finished, closing the lua state\n");