generated from mcpplibs/templates
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.cpp
More file actions
120 lines (102 loc) · 3.35 KB
/
function.cpp
File metadata and controls
120 lines (102 loc) · 3.35 KB
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
import std;
import mcpplibs.capi.lua;
namespace lua = mcpplibs::capi::lua;
static int cpp_add(lua::State* L) {
auto a = lua::tonumber(L, 1);
auto b = lua::tonumber(L, 2);
lua::pushnumber(L, a + b);
return 1;
}
static int cpp_greet(lua::State* L) {
auto* name = lua::tostring(L, 1);
lua::pushfstring(L, "hello, %s!", name);
return 1;
}
static int cpp_range(lua::State* L) {
auto start = lua::tointeger(L, 1);
auto stop = lua::tointeger(L, 2);
lua::newtable(L);
for (lua::Integer i { start }; i <= stop; i++) {
lua::pushinteger(L, i);
lua::rawseti(L, -2, i - start + 1);
}
return 1;
}
static int cpp_counter(lua::State* L) {
auto count = lua::tointeger(L, lua::upvalueindex(1));
count++;
lua::pushinteger(L, count);
lua::copy(L, -1, lua::upvalueindex(1));
return 1;
}
int main() {
std::println("=== mcpplibs.capi.lua function example ===\n");
auto* L = lua::L_newstate();
lua::L_openlibs(L);
// --- Register simple functions ---
std::println("[1] Register C++ functions:");
lua::register_func(L, "cpp_add", cpp_add);
lua::register_func(L, "cpp_greet", cpp_greet);
lua::register_func(L, "cpp_range", cpp_range);
lua::L_dostring(L,
"print(' cpp_add(10, 20) = ' .. cpp_add(10, 20))\n"
"print(' cpp_greet(\"mcpp\") = ' .. cpp_greet('mcpp'))\n"
);
// --- Return table from C++ function ---
std::println("[2] Return table from C++:");
lua::L_dostring(L,
"local nums = cpp_range(1, 5)\n"
"local parts = {}\n"
"for _, v in ipairs(nums) do parts[#parts+1] = tostring(v) end\n"
"print(' cpp_range(1, 5) = {' .. table.concat(parts, ', ') .. '}')\n"
);
// --- Closure with upvalue ---
std::println("[3] Closure with upvalue:");
lua::pushinteger(L, 0);
lua::pushcclosure(L, cpp_counter, 1);
lua::setglobal(L, "counter");
lua::L_dostring(L,
"print(' counter() = ' .. counter())\n"
"print(' counter() = ' .. counter())\n"
"print(' counter() = ' .. counter())\n"
);
// --- Register a module with luaL_setfuncs ---
std::println("[4] Register module with L_setfuncs:");
static const lua::L_Reg mymath[] = {
{"square", [](lua::State* L) -> int {
auto x = lua::tonumber(L, 1);
lua::pushnumber(L, x * x);
return 1;
}},
{"cube", [](lua::State* L) -> int {
auto x = lua::tonumber(L, 1);
lua::pushnumber(L, x * x * x);
return 1;
}},
{nullptr, nullptr}
};
lua::newtable(L);
lua::L_setfuncs(L, mymath, 0);
lua::setglobal(L, "mymath");
lua::L_dostring(L,
"print(' mymath.square(5) = ' .. mymath.square(5))\n"
"print(' mymath.cube(3) = ' .. mymath.cube(3))\n"
);
// --- Call Lua function from C++ ---
std::println("[5] Call Lua function from C++:");
lua::L_dostring(L,
"function fibonacci(n)\n"
" if n <= 1 then return n end\n"
" return fibonacci(n-1) + fibonacci(n-2)\n"
"end\n"
);
lua::getglobal(L, "fibonacci");
lua::pushinteger(L, 10);
if (lua::pcall(L, 1, 1, 0) == lua::OK) {
std::println(" fibonacci(10) = {}", lua::tointeger(L, -1));
}
lua::pop(L, 1);
lua::close(L);
std::println("\ndone.");
return 0;
}