Skip to content

Commit f2440c8

Browse files
committed
config: make coro_table functions public
Also made waker_lookup public as coro_table_get, since the Lua API implementation will need to make use of it.
1 parent 4ebd828 commit f2440c8

File tree

2 files changed

+55
-53
lines changed

2 files changed

+55
-53
lines changed

include/config/vm.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ struct wrap *config_vm_get_wrap(struct config_vm *vm);
2626
void config_vm_set_wrap(struct config_vm *vm, struct wrap *wrap);
2727
void config_vm_set_profile(struct config_vm *vm, const char *profile);
2828

29+
void config_vm_coro_add(lua_State *L, struct config_vm_waker *waker);
30+
void config_vm_coro_del(lua_State *L);
31+
struct config_vm_waker *config_vm_coro_get(lua_State *L);
2932
struct config_vm_waker *config_vm_create_waker(lua_State *L, config_vm_waker_destroy_func_t destroy,
3033
void *data);
3134
int config_vm_exec_bcode(struct config_vm *vm, const unsigned char *bc, size_t bc_size,

waywall/config/vm.c

Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ static const struct {
3030
static constexpr int MAX_INSTRUCTIONS = 50000000;
3131

3232
static void waker_destroy(struct config_vm_waker *waker);
33-
static struct config_vm_waker *waker_lookup(lua_State *L);
3433

3534
static void
3635
on_debug_hook(lua_State *L, struct lua_Debug *dbg) {
@@ -43,47 +42,19 @@ on_lua_panic(lua_State *L) {
4342
return 0;
4443
}
4544

46-
static void
47-
coro_table_add(lua_State *L, struct config_vm_waker *waker) {
48-
ssize_t stack_start = lua_gettop(L);
49-
50-
lua_pushlightuserdata(L, (void *)&REG_KEYS.coroutines); // stack: n+1
51-
lua_rawget(L, LUA_REGISTRYINDEX); // stack: n+1
52-
lua_pushthread(L); // stack: n+2
53-
lua_pushlightuserdata(L, (void *)waker); // stack: n+3
54-
lua_rawset(L, -3); // stack: n+1
55-
56-
lua_pop(L, 1); // stack: n
57-
ww_assert(lua_gettop(L) == stack_start);
58-
}
59-
60-
static void
61-
coro_table_del(lua_State *L) {
62-
ssize_t stack_start = lua_gettop(L);
63-
64-
lua_pushlightuserdata(L, (void *)&REG_KEYS.coroutines); // stack: n+1
65-
lua_rawget(L, LUA_REGISTRYINDEX); // stack: n+1
66-
lua_pushthread(L); // stack: n+2
67-
lua_pushnil(L); // stack: n+3
68-
lua_rawset(L, -3); // stack: n+1
69-
70-
lua_pop(L, 1); // stack: n
71-
ww_assert(lua_gettop(L) == stack_start);
72-
}
73-
7445
static void
7546
process_yield(lua_State *L) {
7647
// Ensure that this coroutine's yield was valid (i.e. it came from a waywall Lua API function
7748
// and was not from a user call to coroutine.yield).
7849
//
7950
// If the yield was invalid, the coroutine will not have any associated waker and will never be
8051
// able to resume. It should be destroyed immediately.
81-
if (waker_lookup(L)) {
52+
if (config_vm_coro_get(L)) {
8253
return;
8354
}
8455

8556
ww_log(LOG_ERROR, "invalid yield from coroutine %p", L);
86-
coro_table_del(L);
57+
config_vm_coro_del(L);
8758
}
8859

8960
static void *
@@ -114,30 +85,13 @@ registry_set(lua_State *L, const char *key, void *value) {
11485

11586
static void
11687
waker_destroy(struct config_vm_waker *waker) {
117-
coro_table_del(waker->L);
88+
config_vm_coro_del(waker->L);
11889
waker->destroy(waker, waker->data);
11990

12091
wl_list_remove(&waker->link);
12192
free(waker);
12293
}
12394

124-
static struct config_vm_waker *
125-
waker_lookup(lua_State *L) {
126-
ssize_t stack_start = lua_gettop(L);
127-
128-
lua_pushlightuserdata(L, (void *)&REG_KEYS.coroutines); // stack: n+1
129-
lua_rawget(L, LUA_REGISTRYINDEX); // stack: n+1
130-
lua_pushthread(L); // stack: n+2
131-
lua_rawget(L, -2); // stack: n+2
132-
133-
struct config_vm_waker *waker = lua_touserdata(L, -1);
134-
135-
lua_pop(L, 2); // stack: 0
136-
ww_assert(lua_gettop(L) == stack_start);
137-
138-
return waker;
139-
}
140-
14195
struct config_vm *
14296
config_vm_create() {
14397
struct config_vm *vm = zalloc(1, sizeof(*vm));
@@ -223,6 +177,51 @@ config_vm_set_wrap(struct config_vm *vm, struct wrap *wrap) {
223177
config_vm_signal_event(vm, "load");
224178
}
225179

180+
void
181+
config_vm_coro_add(lua_State *L, struct config_vm_waker *waker) {
182+
ssize_t stack_start = lua_gettop(L);
183+
184+
lua_pushlightuserdata(L, (void *)&REG_KEYS.coroutines); // stack: n+1
185+
lua_rawget(L, LUA_REGISTRYINDEX); // stack: n+1
186+
lua_pushthread(L); // stack: n+2
187+
lua_pushlightuserdata(L, (void *)waker); // stack: n+3
188+
lua_rawset(L, -3); // stack: n+1
189+
190+
lua_pop(L, 1); // stack: n
191+
ww_assert(lua_gettop(L) == stack_start);
192+
}
193+
194+
void
195+
config_vm_coro_del(lua_State *L) {
196+
ssize_t stack_start = lua_gettop(L);
197+
198+
lua_pushlightuserdata(L, (void *)&REG_KEYS.coroutines); // stack: n+1
199+
lua_rawget(L, LUA_REGISTRYINDEX); // stack: n+1
200+
lua_pushthread(L); // stack: n+2
201+
lua_pushnil(L); // stack: n+3
202+
lua_rawset(L, -3); // stack: n+1
203+
204+
lua_pop(L, 1); // stack: n
205+
ww_assert(lua_gettop(L) == stack_start);
206+
}
207+
208+
struct config_vm_waker *
209+
config_vm_coro_get(lua_State *L) {
210+
ssize_t stack_start = lua_gettop(L);
211+
212+
lua_pushlightuserdata(L, (void *)&REG_KEYS.coroutines); // stack: n+1
213+
lua_rawget(L, LUA_REGISTRYINDEX); // stack: n+1
214+
lua_pushthread(L); // stack: n+2
215+
lua_rawget(L, -2); // stack: n+2
216+
217+
struct config_vm_waker *waker = lua_touserdata(L, -1);
218+
219+
lua_pop(L, 2); // stack: 0
220+
ww_assert(lua_gettop(L) == stack_start);
221+
222+
return waker;
223+
}
224+
226225
struct config_vm_waker *
227226
config_vm_create_waker(lua_State *L, config_vm_waker_destroy_func_t destroy, void *data) {
228227
struct config_vm *vm = config_vm_from(L);
@@ -232,7 +231,7 @@ config_vm_create_waker(lua_State *L, config_vm_waker_destroy_func_t destroy, voi
232231
waker->destroy = destroy;
233232
waker->data = data;
234233

235-
coro_table_add(L, waker);
234+
config_vm_coro_add(L, waker);
236235

237236
wl_list_insert(&vm->wakers, &waker->link);
238237

@@ -359,7 +358,7 @@ config_vm_try_action(struct config_vm *vm, size_t index) {
359358
// Create a new coroutine (Lua thread) and place it in the global coroutines table so that
360359
// it does not get garbage collected.
361360
lua_State *coro = lua_newthread(vm->L); // stack: 1
362-
coro_table_add(coro, nullptr);
361+
config_vm_coro_add(coro, nullptr);
363362

364363
// Retrieve the given action function from the actions table.
365364
lua_pushlightuserdata(coro, (void *)&REG_KEYS.actions); // stack: 1
@@ -383,13 +382,13 @@ config_vm_try_action(struct config_vm *vm, size_t index) {
383382
}
384383
consumed = (!lua_isboolean(coro, -1) || lua_toboolean(coro, -1));
385384

386-
coro_table_del(coro);
385+
config_vm_coro_del(coro);
387386
break;
388387
default:
389388
// The coroutine failed and threw an error. Remove it from the global table and log the
390389
// error.
391390
ww_log(LOG_ERROR, "failed to start action: '%s'", lua_tostring(coro, -1));
392-
coro_table_del(coro);
391+
config_vm_coro_del(coro);
393392
break;
394393
}
395394

0 commit comments

Comments
 (0)