Skip to content

Commit

Permalink
dev: fix stop app error
Browse files Browse the repository at this point in the history
  • Loading branch information
wwhai committed Mar 8, 2023
1 parent 3e8eb48 commit 84e20b7
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 15 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ _release*
_build*
lua-log*
*.txt
plugin/http_server/www/*
dist/
plugin/http_server/www/*
dist/
apps/*.lua
6 changes: 3 additions & 3 deletions appstack/appstack_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ func (as *AppStack) StartApp(uuid string) error {
app.AppState = 1
err := app.VM().CallByParam(lua.P{
Fn: app.GetMainFunc(),
NRet: 1,
Protect: true, // If ``Protect`` is false,
// GopherLua will panic instead of returning an ``error`` value.
NRet: 1,
Protect: true, // If ``Protect`` is false,
// GopherLua will panic instead of returning an ``error`` value.
Handler: &lua.LFunction{
GFunction: func(*lua.LState) int {
return 0
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions test/appstack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ func Test_appStack(t *testing.T) {
engine.Start()
as := appstack.NewAppStack(engine)
err := as.LoadApp(typex.NewApplication("test-uuid-1", "test-name",
"1.0.1", "helloworld_1.0.0.lua"))
"1.0.1", "./apps/hello_world.lua"))
if err != nil {
t.Fatal(err)
}
t.Log(as)
time.Sleep(30 * time.Second)
time.Sleep(10 * time.Second)
engine.Stop()
}
73 changes: 72 additions & 1 deletion test/calllua_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package test

import (
"bufio"
"fmt"
"os"
"reflect"
"testing"
"time"

"github.com/yuin/gopher-lua"
parse "github.com/yuin/gopher-lua/parse"
)

// global
Expand Down Expand Up @@ -176,6 +179,74 @@ func Test_loop_close(t *testing.T) {
}()
time.Sleep(2 * time.Second)
luaVM.Close()
luaVM.po
time.Sleep(3 * time.Second)
}

// CompileLua reads the passed lua file from disk and compiles it.
func CompileLua(filePath string) (*lua.FunctionProto, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
reader := bufio.NewReader(file)
chunk, err := parse.Parse(reader, filePath)
if err != nil {
return nil, err
}
proto, err := lua.Compile(chunk, filePath)
if err != nil {
return nil, err
}
return proto, nil
}

// DoCompiledFile takes a FunctionProto, as returned by CompileLua, and runs it in the LState. It is equivalent
// to calling DoFile on the LState with the original source file.
func DoCompiledFile(L *lua.LState, proto *lua.FunctionProto) error {
lfunc := L.NewFunctionFromProto(proto)
L.Push(lfunc)
return L.PCall(0, lua.MultRet, nil)
}

// Example shows how to share the compiled byte code from a lua script between multiple VMs.
func TestCompileLua(t *testing.T) {
codeToShare, _ := CompileLua("lua/_exit.lua")
t.Log(codeToShare.Code)
// a := lua.NewState()
// b := lua.NewState()
// c := lua.NewState()
// DoCompiledFile(a, codeToShare)
// DoCompiledFile(b, codeToShare)
// DoCompiledFile(c, codeToShare)
}

func Test_Stack_order(t *testing.T) {
var s1 = `
A=1
B=2
function __f1()
end
function __f2()
end
function __f3()
end
`
var luaVM = lua.NewState()

err1 := luaVM.DoString(s1)
if err1 != nil {
panic(err1)
}
luaVM.G.Global.ForEach(func(l1, l2 lua.LValue) {

if l2.Type() == lua.LTFunction {
if l1.String()[:3] == "__f" {
fc := l2.(*lua.LFunction)
t.Log(fc.Proto)
}

}
})
luaVM.Close()
}
2 changes: 2 additions & 0 deletions test/lua/_exit.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
function _exit() end
_exit()
4 changes: 2 additions & 2 deletions typex/version.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// Warning:
// This file is generated by go compiler, don't change it!!!
// Generated Time: "2023-03-08 11:14:02"
// Generated Time: "2023-03-08 17:23:43"
// Build on: 5.18.17-amd64-desktop-community-hwe
//
package typex
Expand All @@ -13,6 +13,6 @@ type Version struct {

var DefaultVersion = Version{
Version: `v0.4.3`,
ReleaseTime: "2023-03-08 11:14:02",
ReleaseTime: "2023-03-08 17:23:43",
}

19 changes: 16 additions & 3 deletions typex/xappstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package typex

import (
"context"
"fmt"
"log"

lua "github.com/yuin/gopher-lua"
)
Expand Down Expand Up @@ -67,16 +67,29 @@ func (app *Application) VM() *lua.LState {
/*
*
* 释放资源,这里是个问题,因为多线程突然 vm.Close 中断lua虚拟机的时候,会引发panic
* 这里是个野路子办法,直接把进程给救活,实际上到这里已经挂了。
* 这里是个野路子办法,直接把进程给救活,实际上到这里已经挂了。已经给作者提了Issue,等他后期解决
* https://github.com/yuin/gopher-lua/discussions/430
*/
func (app *Application) Stop() {
defer func() {
if err := recover(); err != nil {
fmt.Println("app stop:", app.UUID, ", with recover error: ", err)
log.Println("[gopher-lua] app stop:", app.UUID, ", with recover error: ", err)
}
}()
t1 := app.vm.GetTop()
log.Println(t1)
app.vm.DoString(`function __1() end __1()`)
t2 := app.vm.GetTop()
log.Println(t2)
app.vm.DoString(`function __2() end __2()`)
t3 := app.vm.GetTop()
log.Println(t3)
app.vm.Pop(0)
app.cancel()
app.vm.Close()
app.cancel()
app.vm.GetTop()

}

/*
Expand Down
2 changes: 1 addition & 1 deletion utils/banner.b
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
|
|* Welcome to RULEX framework world <'_'>
|* Version: v0.4.3-d5df539dbd1a97d
|* Build at: 2023-03-08 11:14:02
|* Build at: 2023-03-08 17:23:43
|* Document: https://rulex.pages.dev
|
2 changes: 1 addition & 1 deletion utils/uuid_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ func RuleUuid() string {

// MakeUUID
func MakeUUID(prefix string) string {
return prefix + ":" + strings.Replace(uuid.NewString(), "-", "", -1)
return prefix + strings.Replace(uuid.NewString(), "-", "", -1)
}

0 comments on commit 84e20b7

Please sign in to comment.