summaryrefslogtreecommitdiff
path: root/worker.go
diff options
context:
space:
mode:
Diffstat (limited to 'worker.go')
-rw-r--r--worker.go25
1 files changed, 19 insertions, 6 deletions
diff --git a/worker.go b/worker.go
index b79f7e8..5d6eeb1 100644
--- a/worker.go
+++ b/worker.go
@@ -42,6 +42,7 @@ func HandleHTTPRequest(
return res
}
+// NewWorker creates a new instance of Worker type.
func NewWorker() *Worker {
return &Worker {
routes: make(map[string]LuaRef),
@@ -49,6 +50,11 @@ func NewWorker() *Worker {
}
}
+// Start starts the worker:
+// 1) creates a Lua context
+// 2) executes the filename in it
+// 3) initiates the the "luna" module so it's possible to call Go functions
+// from Lua
func (w *Worker) Start(filename string, module map[string]any) error {
w.mu.Lock()
defer w.mu.Unlock()
@@ -67,6 +73,7 @@ func (w *Worker) Start(filename string, module map[string]any) error {
return nil
}
+// Listen starts a goroutine listening/handling HTTP requests from the queue.
func (w *Worker) Listen(queue chan any) {
handle := func() {
defer w.lua.RestoreStackFunc()()
@@ -155,23 +162,33 @@ func (w *Worker) Listen(queue chan any) {
}
}
-func (w *Worker) Run(code string) error {
+// Eval evaluates the code in the Lua context.
+func (w *Worker) Eval(code string) error {
w.mu.Lock()
defer w.mu.Unlock()
defer w.lua.RestoreStackFunc()()
return w.lua.LoadString(code)
}
+// SetRoute sets a handler for the route.
func (w *Worker) SetRoute(route string, handler LuaRef) {
w.routes[route] = handler
}
+// Stop stops the worker closing the Lua context. TODO: stop Listen goroutine
+// as well.
func (w *Worker) Stop() {
w.mu.Lock()
defer w.mu.Unlock()
w.lua.Close()
}
+// HasSameLua checks if the Lua context belongs to the worker.
+func (w *Worker) HasSameLua(l *Lua) bool {
+ return w.lua == l
+}
+
+// initLunaModule registers the module in the Lua context.
func (w *Worker) initLunaModule(module map[string]any) {
var pushTable func(t map[string]any)
pushTable = func (t map[string]any) {
@@ -191,7 +208,7 @@ func (w *Worker) initLunaModule(module map[string]any) {
v, _ := v.(map[string]any)
pushTable(v)
default:
- // FIXME: more details
+ // FIXME: more details.
log.Fatal("unsupported module value type")
}
w.lua.SetTableItem(k)
@@ -200,7 +217,3 @@ func (w *Worker) initLunaModule(module map[string]any) {
pushTable(module)
w.lua.SetGlobal("luna")
}
-
-func (w *Worker) HasSameLua(l *Lua) bool {
- return w.lua == l
-}