forked from YunWGui/cpp_study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathluajit.cpp
More file actions
146 lines (118 loc) · 2.75 KB
/
luajit.cpp
File metadata and controls
146 lines (118 loc) · 2.75 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
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
// Copyright (c) 2020 by Chrono
//
// git clone git@github.com:openresty/luajit2.git
// make && make install
//
// git clone git@github.com:vinniefalco/LuaBridge.git
//
// g++ luajit.cpp -std=c++11 -I../common -I/usr/local/include/luajit-2.1 -lluajit-5.1 -ldl -o a.out;./a.out
// g++ luajit.cpp -std=c++11 -I../common -I/usr/local/include/luajit-2.1 -lluajit-5.1 -ldl -O0 -g -o a.out
//
// g++ luajit.cpp -std=c++11 -I/usr/local/include/luajit-2.1 /usr/local/lib/libluajit-5.1.a -ldl -o a.out;./a.out
// g++ luajit.cpp -std=c++11 -I/usr/local/include/luajit-2.1 -L/usr/local/lib -Wl,-rpath,/usr/local/lib -lluajit-5.1 -ldl -o a.out;./a.out
//
#include <cassert>
#include <iostream>
#include <memory>
extern "C" {
#include <luajit.h>
#include <lualib.h>
#include <lauxlib.h>
}
#include <LuaBridge/LuaBridge.h>
//namespace lua = luabridge;
using namespace luabridge;
using namespace std;
#if 0
class MyLuaState final
{
public:
using lua_state_t = decltype(luaL_newstate());
private:
lua_state_t L = nullptr;
public:
MyLuaState()
{
L = luaL_newstate();
assert(L);
luaL_openlibs(L);
}
~MyLuaState()
{
lua_close(L);
}
public:
operator lua_state_t() const
{
return L;
}
int dofile(const char* filename) const
{
return luaL_dofile(L, filename);
}
public:
MyLuaState(const MyLuaState&) = delete;
MyLuaState& operator=(const MyLuaState&) = delete;
};
#endif
//using MyLuaState = std::shared_ptr<lua_State>;
auto make_luavm = []()
{
std::shared_ptr<lua_State> vm(
luaL_newstate(), lua_close
);
assert(vm);
luaL_openlibs(vm.get());
return vm;
};
#define L vm.get()
void case1()
{
//MyLuaState L;
auto vm = make_luavm();
auto package = getGlobal(L, "package");
string path = package["path"];
string cpath = package["cpath"];
cout << path << endl;
cout << cpath << endl;
}
void case2()
{
//MyLuaState L;
auto vm = make_luavm();
int status;
status = luaL_dostring(L, "print('hello lua')");
status = luaL_dofile(L, "./embedded.lua");
status = luaL_dofile(L, "./shared.lua");
//L.dofile("./embedded.lua");
}
void case3()
{
//MyLuaState L;
auto vm = make_luavm();
int status;
string chunk = R"(
function say(s)
print(s)
end
function add(a, b)
return a + b
end
)";
status = luaL_dostring(L, chunk.c_str());
assert(status == 0);
auto f1 = getGlobal(L, "say");
f1("say something");
auto f2 = getGlobal(L, "add");
auto v = f2(10, 20);
assert(v == 30);
//cout << v << endl;
//L.dofile("./embedded.lua");
}
int main()
{
case1();
case2();
case3();
cout << "luajit demo" << endl;
}