Implement coroutine handling

This commit is contained in:
Krzosa Karol
2024-08-14 12:41:09 +02:00
parent f38c20b975
commit 225d1ffc06
7 changed files with 45 additions and 62 deletions

View File

@@ -324,10 +324,24 @@ function ApplyRules(s)
return nil return nil
end end
Coroutines = {}
function AddCo(f)
local i = #Coroutines + 1
Coroutines[i] = coroutine.create(f)
return Coroutines[i]
end
function OnUpdate() function OnUpdate()
-- @todo: implement coroutine dispatch here local new_co_list = {}
-- probably also want constatnly rerunning jobs with a set wake up time for key, co in pairs(Coroutines) do
-- @warning: make sure to not rewrite the thread list when rerunning this file local status = coroutine.status(co)
if status ~= "dead" then
coroutine.resume(co)
new_co_list[#new_co_list + 1] = co
end
end
Coroutines = new_co_list
end end
)=="; )==";

View File

@@ -40,6 +40,7 @@ struct Event {
}; };
Array<Event> EventPlayback; Array<Event> EventPlayback;
// :Event
void Serialize(Serializer *s, Event *e) { void Serialize(Serializer *s, Event *e) {
SerializeBegin(s); SerializeBegin(s);
Serialize(s, "kind", &e->kind); Serialize(s, "kind", &e->kind);
@@ -80,12 +81,3 @@ bool WaitForEvents = true;
#define Mouse(x) (event.kind == EVENT_MOUSE_##x) #define Mouse(x) (event.kind == EVENT_MOUSE_##x)
#define MousePress() (Mouse(LEFT) || Mouse(RIGHT) || Mouse(MIDDLE)) #define MousePress() (Mouse(LEFT) || Mouse(RIGHT) || Mouse(MIDDLE))
#define MouseUp() (Mouse(LEFT_UP) || Mouse(RIGHT_UP) || Mouse(MIDDLE_UP)) #define MouseUp() (Mouse(LEFT_UP) || Mouse(RIGHT_UP) || Mouse(MIDDLE_UP))
void ParseEvents(Array<Event> *events, Buffer *buffer) {
Serializer s = {NULL, GetString(buffer), false};
for (; s.string.len;) {
Event event = {};
Serialize(&s, &event);
Add(events, event);
}
}

View File

@@ -213,10 +213,24 @@ function ApplyRules(s)
return nil return nil
end end
Coroutines = {}
function AddCo(f)
local i = #Coroutines + 1
Coroutines[i] = coroutine.create(f)
return i
end
function OnUpdate() function OnUpdate()
-- @todo: implement coroutine dispatch here local new_co_list = {}
-- probably also want constatnly rerunning jobs with a set wake up time for key, co in pairs(Coroutines) do
-- @warning: make sure to not rewrite the thread list when rerunning this file local status = coroutine.status(co)
if status ~= "dead" then
coroutine.resume(co)
new_co_list[#new_co_list + 1] = co
end
end
Coroutines = new_co_list
end end

View File

@@ -329,7 +329,7 @@ Vec2 GetVec2(const char *name) {
return result; return result;
} }
// #Play { {kind = 10, key = 1073741902, xmouse = 612, ymouse = 357, xwindow = 1280, ywindow = 720 } } // :Event
int Lua_Play(lua_State *L) { int Lua_Play(lua_State *L) {
if (!lua_istable(L, -1)) luaL_error(LuaState, "expected a table of events"); if (!lua_istable(L, -1)) luaL_error(LuaState, "expected a table of events");

View File

@@ -1,17 +1,9 @@
struct Serializer { struct Serializer {
Buffer *buffer; // for writing Buffer *buffer; // for writing
String16 string; // for reading
bool is_writing;
}; };
void Serialize(Serializer *s, String name, Int *datum) { void Serialize(Serializer *s, String name, Int *datum) {
if (s->is_writing) { IKnowWhatImDoing_Appendf(s->buffer, "%.*s = %lld, ", FmtString(name), (long long)*datum);
IKnowWhatImDoing_Appendf(s->buffer, "%.*s = %lld, ", FmtString(name), (long long)*datum);
} else {
s->string = Skip(s->string, name.len + 3);
*datum = SkipNumber(&s->string);
s->string = Skip(s->string, 2);
}
} }
void Serialize(Serializer *s, String name, uint32_t *datum) { void Serialize(Serializer *s, String name, uint32_t *datum) {
@@ -33,48 +25,18 @@ void Serialize(Serializer *s, String name, int16_t *datum) {
} }
void Serialize(Serializer *s, String name, Vec2 *datum) { void Serialize(Serializer *s, String name, Vec2 *datum) {
if (s->is_writing) { IKnowWhatImDoing_Appendf(s->buffer, "%.*s = {%lld, %lld}, ", FmtString(name), (long long)datum->x, (long long)datum->y);
IKnowWhatImDoing_Appendf(s->buffer, "%.*s = {%lld, %lld}, ", FmtString(name), (long long)datum->x, (long long)datum->y);
} else {
s->string = Skip(s->string, name.len + 3 + 1);
datum->x = (float)SkipNumber(&s->string);
s->string = Skip(s->string, 2);
datum->y = (float)SkipNumber(&s->string);
s->string = Skip(s->string, 1 + 2);
}
} }
void Serialize(Serializer *s, String name, char **text) { void Serialize(Serializer *s, String name, char **text) {
if (s->is_writing) { String str = *text;
String str = *text; IKnowWhatImDoing_Appendf(s->buffer, "%.*s = \"(%.*s)\", ", FmtString(name), FmtString(str));
IKnowWhatImDoing_Appendf(s->buffer, "%.*s = \"(%.*s)\", ", FmtString(name), FmtString(str));
} else {
s->string = Skip(s->string, name.len + 3 + 2);
String16 str = SkipUntil(&s->string, L")\"");
if (str.len) {
Scratch scratch;
String s8 = ToString(scratch, str);
*text = Intern(&GlobalInternTable, s8).data;
}
s->string = Skip(s->string, 2 + 2);
}
} }
void SerializeBegin(Serializer *s) { void SerializeBegin(Serializer *s) {
if (s->is_writing) { IKnowWhatImDoing_Appendf(s->buffer, "{");
IKnowWhatImDoing_Appendf(s->buffer, "{");
} else {
SkipWhitespace(&s->string);
Assert(s->string[0] == '{');
s->string = Skip(s->string, 1);
}
} }
void SerializeEnd(Serializer *s) { void SerializeEnd(Serializer *s) {
if (s->is_writing) { IKnowWhatImDoing_Appendf(s->buffer, "},\n");
IKnowWhatImDoing_Appendf(s->buffer, "},\n");
} else {
Assert(s->string[0] == '}');
s->string = Skip(s->string, 3);
}
} }

View File

@@ -301,7 +301,7 @@ int main(int argc, char **argv)
WindowOpenBufferView(window, it); WindowOpenBufferView(window, it);
} }
Serializer ser = {EventBuffer, {}, true}; Serializer ser = {EventBuffer};
while (AppIsRunning) { while (AppIsRunning) {
FrameID += 1; FrameID += 1;

View File

@@ -10,6 +10,7 @@
- Fuzzy search buffer which uses titlebar as query! - Fuzzy search buffer which uses titlebar as query!
- Check if file exists in ApplyRules otherwise return null - Check if file exists in ApplyRules otherwise return null
- Gotos, jumping between views should preserve cursors - Gotos, jumping between views should preserve cursors
- ReportWarning should be signaled visibly but you should be able to do things! no focus switching
- OnWindowCommand allow config user to overwrite the WindowCommand keybinding, introduce his own - OnWindowCommand allow config user to overwrite the WindowCommand keybinding, introduce his own