Skip to content

Commit

Permalink
Add ability to create lua states with context (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrNavaStar authored Nov 10, 2024
1 parent 1841fa0 commit 99a25f0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
3 changes: 3 additions & 0 deletions lua/golua.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package lua
import "C"

import (
"context"
"reflect"
"sync"
"unsafe"
Expand Down Expand Up @@ -50,6 +51,8 @@ type State struct {

// User defined hook function
hookFn HookFunction

ctx context.Context
}

var goStates map[uintptr]*State
Expand Down
15 changes: 14 additions & 1 deletion lua/lauxlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ package lua
//#include <stdlib.h>
//#include "golua.h"
import "C"
import "unsafe"
import (
"context"
"unsafe"
)

type LuaError struct {
code int
Expand Down Expand Up @@ -192,6 +195,16 @@ func NewState() *State {
return L
}

func NewStateWithContext(ctx context.Context) *State {
L := NewState()
L.ctx = ctx
return L
}

func (L *State) Context() context.Context {
return L.ctx
}

// luaL_openlibs
func (L *State) OpenLibs() {
C.luaL_openlibs(L.s)
Expand Down
4 changes: 2 additions & 2 deletions lua/lua.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type LuaStackEntry struct {
}

func newState(L *C.lua_State) *State {
newstate := &State{L, 0, make([]interface{}, 0, 8), make([]uint, 0, 8), nil, nil}
newstate := &State{L, 0, make([]interface{}, 0, 8), make([]uint, 0, 8), nil, nil, nil}
registerGoState(newstate)
C.clua_setgostate(L, C.size_t(newstate.Index))
C.clua_initstate(L)
Expand Down Expand Up @@ -354,7 +354,7 @@ func (L *State) NewThread() *State {
//TODO: should have same lists as parent
// but may complicate gc
s := C.lua_newthread(L.s)
return &State{s, 0, nil, nil, nil, nil}
return &State{s, 0, nil, nil, nil, nil, nil}
}

// lua_next
Expand Down

0 comments on commit 99a25f0

Please sign in to comment.