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
|
/*
* Copyright 2023 jacqueline <me@jacqueline.id.au>
*
* SPDX-License-Identifier: GPL-3.0-only
*/
#include "lua/lua_thread.hpp"
#include <iostream>
#include <memory>
#include "esp_heap_caps.h"
#include "esp_log.h"
#include "lua.hpp"
#include "events/event_queue.hpp"
#include "lua/bridge.hpp"
#include "memory_resource.hpp"
#include "system_fsm/service_locator.hpp"
#include "ui/ui_events.hpp"
namespace lua {
[[maybe_unused]] static constexpr char kTag[] = "lua";
class Allocator {
public:
Allocator() : total_allocated_(0) {}
auto alloc(void* ptr, size_t osize, size_t nsize) -> void* {
total_allocated_ = total_allocated_ - osize + nsize;
// ESP_LOGI(kTag, "lua realloc -> %u KiB", total_allocated_ / 1024);
if (nsize == 0) {
heap_caps_free(ptr);
return NULL;
} else {
return heap_caps_realloc(ptr, nsize, MALLOC_CAP_SPIRAM);
}
}
private:
size_t total_allocated_;
};
static auto lua_alloc(void* ud,
void* ptr,
size_t osize,
size_t nsize) -> void* {
Allocator* instance = reinterpret_cast<Allocator*>(ud);
return instance->alloc(ptr, osize, nsize);
}
static int lua_panic(lua_State* L) {
ESP_LOGE(kTag, "!! PANIC !! %s", lua_tostring(L, -1));
return 0;
}
auto LuaThread::Start(system_fsm::ServiceLocator& services) -> LuaThread* {
auto alloc = std::make_unique<Allocator>();
lua_State* state = lua_newstate(lua_alloc, alloc.get());
if (!state) {
return nullptr;
}
luaL_openlibs(state);
lua_atpanic(state, lua_panic);
return new LuaThread(alloc, state);
}
LuaThread::LuaThread(std::unique_ptr<Allocator>& alloc, lua_State* state)
: alloc_(std::move(alloc)), state_(state) {}
LuaThread::~LuaThread() {
lua_close(state_);
}
auto LuaThread::RunScript(const std::string& path) -> bool {
int res = luaL_loadfilex(state_, path.c_str(), NULL);
if (res != LUA_OK) {
return false;
}
CallProtected(state_, 0, 0);
return true;
}
auto LuaThread::RunString(const std::string& script) -> bool {
int res = luaL_loadstring(state_, script.c_str());
if (res != LUA_OK) {
return false;
}
CallProtected(state_, 0, 0);
return true;
}
auto LuaThread::DumpStack() -> void {
int top = lua_gettop(state_);
std::cout << "stack size: " << top << std::endl;
for (size_t i = 1; i <= top; i++) {
std::cout << "[" << i << "]\t" << luaL_typename(state_, i);
switch (lua_type(state_, i)) {
case LUA_TNUMBER:
std::cout << "\t(";
if (lua_isinteger(state_, i)) {
std::cout << lua_tointeger(state_, i);
} else {
std::cout << lua_tonumber(state_, i);
}
std::cout << ")";
break;
case LUA_TSTRING:
std::cout << "\t('" << lua_tostring(state_, i) << "')";
break;
case LUA_TBOOLEAN:
std::cout << "\t(" << lua_toboolean(state_, i) << ")";
break;
case LUA_TNIL:
// Value is implied.
break;
case LUA_TTABLE:
lua_pushnil(state_);
while (lua_next(state_, i) != 0) {
// Keys
std::cout << std::endl << "\t\t" << luaL_typename(state_, -2);
if (lua_type(state_, -2) == LUA_TSTRING) {
std::cout << "\t(" << lua_tostring(state_, -2) << ")";
} else if (lua_type(state_, -2) == LUA_TNUMBER) {
std::cout << "\t(" << lua_tonumber(state_, -2) << ")";
}
// Values
std::cout << "\t\t" << luaL_typename(state_, -1);
if (lua_type(state_, -1) == LUA_TSTRING) {
std::cout << "\t(" << lua_tostring(state_, -1) << ")";
} else if (lua_type(state_, -1) == LUA_TNUMBER) {
std::cout << "\t(" << lua_tonumber(state_, -1) << ")";
}
// Pop the value; we don't care about it. Leave the key on the stack
// for the next call to lua_next.
lua_pop(state_, 1);
}
break;
default:
std::cout << "\t(" << lua_topointer(state_, i) << ")";
break;
}
std::cout << std::endl;
}
}
static int msg_handler(lua_State* L) {
if (!lua_isstring(L, 1)) {
return 1;
}
const char* msg = lua_tostring(L, 1);
if (msg == NULL) { /* is error object not a string? */
if (luaL_callmeta(L, 1, "__tostring") && /* does it have a metamethod */
lua_type(L, -1) == LUA_TSTRING) /* that produces a string? */
return 1; /* that is the message */
else
msg = lua_pushfstring(L, "(error object is a %s value)",
luaL_typename(L, 1));
}
/* append a standard traceback */
luaL_traceback(L, L, msg, 1);
return 1;
}
auto CallProtected(lua_State* s, int nargs, int nresults) -> int {
int base = lua_gettop(s) - nargs;
// Place our message handler under the function to be called.
lua_pushcfunction(s, msg_handler);
lua_insert(s, base);
// Invoke the function.
int ret = lua_pcall(s, nargs, nresults, base);
if (ret != LUA_OK) {
events::Ui().Dispatch(ui::OnLuaError{.message = lua_tostring(s, -1)});
}
// Clean up our message handler
lua_remove(s, base);
return ret;
}
} // namespace lua
|