Skip to content

Commit 75f263a

Browse files
committed
Add example of using like goroutine.
1 parent 198e4f8 commit 75f263a

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

plugin/example_test.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package plugin
22

33
import (
4-
"log"
5-
64
time "github.com/vadv/gopher-lua-libs/time"
5+
"log"
76

87
lua "github.com/yuin/gopher-lua"
98
)
109

1110
// plugin.do_string(), plugin_ud:run(), plugin_ud:stop()
1211
func Example_package() {
1312
state := lua.NewState()
13+
defer state.Close()
1414
Preload(state)
1515
time.Preload(state)
1616
source := `
@@ -57,3 +57,31 @@ func Example_package() {
5757
// 1
5858
// 2
5959
}
60+
61+
func Example_using_like_goroutine() {
62+
state := lua.NewState()
63+
defer state.Close()
64+
65+
Preload(state)
66+
67+
source := `
68+
local plugin = require 'plugin'
69+
70+
function myfunc(...)
71+
print(table.concat({...}, ""))
72+
end
73+
74+
local background_body = [[
75+
return pcall(unpack(arg))
76+
]]
77+
local background_plugin = plugin.do_string(background_body, myfunc, "Hello", " ", "World")
78+
background_plugin:run()
79+
local err = background_plugin:wait()
80+
assert(not err, err)
81+
`
82+
if err := state.DoString(source); err != nil {
83+
log.Fatal(err)
84+
}
85+
// Output:
86+
// Hello World
87+
}

plugin/test/test_api.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ end
208208

209209
function TestPassingFunction(t)
210210
local plugin_body = [[
211-
pcall(unpack(arg))
211+
return pcall(unpack(arg))
212212
]]
213213

214214
function sendMe(ic, oc)

0 commit comments

Comments
 (0)