forked from OneLoneCoder/Javidx9
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OneLoneCoder_EmbeddingLua_Part1.cpp
200 lines (162 loc) · 5.98 KB
/
OneLoneCoder_EmbeddingLua_Part1.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
Embedding Lua in C++ Part #1
"Stupid 3D printer, I didn't want a nest..." - javidx9
License (OLC-3)
~~~~~~~~~~~~~~~
Copyright 2018-2019 OneLoneCoder.com
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions or derivations of source code must retain the above
copyright notice, this list of conditions and the following disclaimer.
2. Redistributions or derivative works in binary form must reproduce
the above copyright notice. This list of conditions and the following
disclaimer must be reproduced in the documentation and/or other
materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Relevant Video: https://youtu.be/4l5HdmPoynw
Links
~~~~~
YouTube: https://www.youtube.com/javidx9
https://www.youtube.com/javidx9extra
Discord: https://discord.gg/WhwHUMV
Twitter: https://www.twitter.com/javidx9
Twitch: https://www.twitch.tv/javidx9
GitHub: https://www.github.com/onelonecoder
Patreon: https://www.patreon.com/javidx9
Homepage: https://www.onelonecoder.com
Author
~~~~~~
David Barr, aka javidx9, ©OneLoneCoder 2019
*/
#include <iostream>
#include <string>
// include Lua, assumes it is local to this file
extern "C"
{
#include "lua535/include/lua.h"
#include "lua535/include/lauxlib.h"
#include "lua535/include/lualib.h"
}
// Link to lua library
#ifdef _WIN32
#pragma comment(lib, "lua535/liblua53.a")
#endif
// Little error checking utility function
bool CheckLua(lua_State *L, int r)
{
if (r != LUA_OK)
{
std::string errormsg = lua_tostring(L, -1);
std::cout << errormsg << std::endl;
return false;
}
return true;
}
int lua_HostFunction(lua_State *L)
{
float a = (float)lua_tonumber(L, 1);
float b = (float)lua_tonumber(L, 2);
std::cout << "[CPP S4] HostFunction(" << a << ", " << b << ") called from Lua" << std::endl;
float c = a * b;
lua_pushnumber(L, c);
return 1;
}
// NOTE !!!
// YOU WILL NEED THE "EmbeddingLua1.lua" FILE FROM GITHUB REPO
int main()
{
struct Player
{
std::string title;
std::string name;
std::string family;
int level;
} player;
// Create Lua State
lua_State *L = luaL_newstate();
// Add standard libraries to Lua Virtual Machine
luaL_openlibs(L);
// Register our C++ Function in the global Lua space
lua_register(L, "HostFunction", lua_HostFunction);
// Load and parse the Lua File
if(CheckLua(L, luaL_dofile(L, "EmbeddingLua1.lua")))
{
// Stage 1: Just read simple variables
std::cout << "[CPP] Stage 1 - Read Simple Variables" << std::endl;
lua_getglobal(L, "a");
if (lua_isnumber(L, -1)) std::cout << "[CPP S1] a = " << (int)lua_tointeger(L, -1) << std::endl;
lua_getglobal(L, "b");
if (lua_isnumber(L, -1)) std::cout << "[CPP S1] b = " << (int)lua_tointeger(L, -1) << std::endl;
lua_getglobal(L, "c");
if (lua_isnumber(L, -1)) std::cout << "[CPP S1] c = " << (int)lua_tointeger(L, -1) << std::endl;
lua_getglobal(L, "d");
if (lua_isstring(L, -1)) std::cout << "[CPP S1] d = " << lua_tostring(L, -1) << std::endl << std::endl;
// Stage 2: Read Table Object
std::cout << "[CPP] Stage 2 - Read Table (Key/Value pairs)" << std::endl;
lua_getglobal(L, "player");
if (lua_istable(L, -1))
{
lua_pushstring(L, "Name");
lua_gettable(L, -2);
player.name = lua_tostring(L, -1);
lua_pop(L, 1);
lua_pushstring(L, "Family");
lua_gettable(L, -2);
player.family = lua_tostring(L, -1);
lua_pop(L, 1);
lua_pushstring(L, "Title");
lua_gettable(L, -2);
player.title = lua_tostring(L, -1);
lua_pop(L, 1);
lua_pushstring(L, "Level");
lua_gettable(L, -2);
player.level = (int)lua_tointeger(L, -1);
lua_pop(L, 1);
}
std::cout << "[CPP S2] " << player.title << " " << player.name << " of " << player.family << " [Lvl: " << player.level << "]" << std::endl << std::endl;
// Stage 3: Call Lua Function
std::cout << "[CPP] Stage 3 - Call Lua Function" << std::endl;
lua_getglobal(L, "CalledFromCPP1");
if (lua_isfunction(L, -1))
{
lua_pushnumber(L, 5.0f);
lua_pushnumber(L, 6.0f);
lua_pushstring(L, "Bwa ha ha!");
std::cout << "[CPP S3] Calling 'CalledFromCPP1' in lua script" << std::endl;
if (CheckLua(L, lua_pcall(L, 3, 1, 0)))
{
std::cout << "[CPP S3] 'CalledFromCPP1' returned " << (float)lua_tonumber(L, -1) << std::endl << std::endl;
}
}
// Stage 4: Call Lua Function, which calls C++ Function
std::cout << "[CPP] Stage 4 - Call Lua Function, whcih in turn calls C++ Function" << std::endl;
lua_getglobal(L, "CalledFromCPP2");
if (lua_isfunction(L, -1))
{
lua_pushnumber(L, 5.0f);
lua_pushnumber(L, 6.0f);
std::cout << "[CPP S4] Calling 'CalledFromCPP2' in lua script" << std::endl;
if (CheckLua(L, lua_pcall(L, 2, 1, 0)))
{
std::cout << "[CPP S4] 'CalledFromCPP2' returned " << (float)lua_tonumber(L, -1) << std::endl << std::endl;
}
}
}
system("pause");
lua_close(L);
return 0;
}