-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlua_runtime.c
executable file
·172 lines (143 loc) · 3.8 KB
/
lua_runtime.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <camlib.h>
#include <app.h>
#include "fuji_lua.h"
static char error_buffer[512] = {0};
static struct CamLuaTasks {
int tasks;
struct lua_State *L[MAX_LUA_CONCURRENT];
int state[MAX_LUA_CONCURRENT];
}lua_tasks = {0};
const char *cam_lua_get_error(void) {
return error_buffer;
}
int lua_script_run_loop(int id) {
if (id >= MAX_LUA_CONCURRENT || id < 0) {
snprintf(error_buffer, sizeof(error_buffer), "Script: out of bounds ID: %d\n", id);
return 1;
}
struct lua_State *L = lua_tasks.L[(int)id];
if (L == NULL) return -1;
lua_getglobal(L, "eventLoop");
if (lua_isfunction(L, -1)) {
if (lua_pcall(L, 0, 0, 0) != LUA_OK) {
snprintf(error_buffer, sizeof(error_buffer), "Failed to run Lua: %s", lua_tostring(L, -1));
return -1;
}
} else {
snprintf(error_buffer, sizeof(error_buffer), "eventLoop is not a function: %s", lua_tostring(L, -1));
return -1;
}
return 0;
}
static int get_task_id(lua_State *L) {
for (int i = 0; i < lua_tasks.tasks; i++) {
if (L == lua_tasks.L[i]) return i;
}
return -1;
}
static int lua_script_print(lua_State *L) {
const char *str = luaL_checkstring(L, 1);
app_print("%s", str);
return 1;
}
static int lua_script_toast(lua_State *L) {
const char *str = luaL_checkstring(L, 1);
app_print("%s", str);
return 1;
}
static int lua_script_set_status(lua_State *L) {
const char *str = luaL_checkstring(L, 1);
//ui_send_text("status", (char *)str);
return 1;
}
static int mylua_itoa(lua_State* L) {
int number = (int)luaL_checkinteger(L, 1);
char buffer[20];
snprintf(buffer, sizeof(buffer), "%d", number);
lua_pushstring(L, buffer);
return 1;
}
int lua_mark_dead(lua_State *L) {
int id = get_task_id(L);
if (id == -1) abort();
lua_tasks.state[id] = 1;
return 1;
}
lua_State *cam_lua_state(void) {
lua_State *L = luaL_newstate();
luaopen_base(L);
luaL_requiref(L, "json", luaopen_cjson, 1);
luaL_requiref(L, "ptp", luaopen_ptp, 1);
luaL_requiref(L, "fuji", luaopen_fuji, 1);
lua_register(L, "print", lua_script_print);
lua_register(L, "exit", lua_mark_dead);
lua_register(L, "setStatusText", lua_script_set_status);
return L;
}
int cam_run_lua_script(const char *buffer) {
lua_State *L = cam_lua_state();
if (L == NULL) {
snprintf(error_buffer, sizeof(error_buffer), "Failed to init lua");
return -1;
}
if (luaL_loadbuffer(L, buffer, strlen(buffer), "script") != LUA_OK) {
snprintf(error_buffer, sizeof(error_buffer), "Failed to run Lua: %s", lua_tostring(L, -1));
lua_close(L);
return -1;
}
if (lua_pcall(L, 0, 0, 0) != LUA_OK) {
snprintf(error_buffer, sizeof(error_buffer), "Failed to run Lua: %s", lua_tostring(L, -1));
lua_close(L);
return -1;
}
lua_close(L);
return 0;
}
lua_State *cam_new_task(int *id) {
(*id) = -1;
if (lua_tasks.tasks >= MAX_LUA_CONCURRENT) {
for (int i = 0; i < lua_tasks.tasks; i++) {
if (lua_tasks.state[i] == 1) {
(*id) = i;
lua_close(lua_tasks.L[i]);
lua_tasks.L[i] = 0;
break;
}
}
if ((*id) == -1) {
return NULL;
}
} else {
(*id) = lua_tasks.tasks;
lua_tasks.tasks++;
}
lua_State *L = cam_lua_state();
lua_tasks.L[(*id)] = L;
lua_tasks.state[(*id)] = 0;
return L;
}
int cam_run_lua_script_async(const char *buffer) {
int id = 0;
lua_State *L = cam_new_task(&id);
if (L == NULL) {
snprintf(error_buffer, sizeof(error_buffer), "Reached max concurrent Lua tasks");
return -1;
}
if (luaL_loadbuffer(L, buffer, strlen(buffer), "script") != LUA_OK) {
snprintf(error_buffer, sizeof(error_buffer), "Failed to run Lua: %s", lua_tostring(L, -1));
lua_close(L);
return -1;
}
if (lua_pcall(L, 0, 0, 0) != LUA_OK) {
snprintf(error_buffer, sizeof(error_buffer), "Failed to run Lua: %s", lua_tostring(L, -1));
lua_close(L);
return -1;
}
return id;
}