plugin

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 14, 2023 License: BSD-3-Clause Imports: 37 Imported by: 5

README

plugin GoDoc

Usage

local plugin = require("plugin")
local time = require("time")

local plugin_body = [[
    local time = require("time")
    local i = 1
    while true do
        print(i)
        i = i + 1
        time.sleep(1)
    end
]]

-- plugin.do_string(body)
-- also you can use: plugin.do_file(filename)
local print_plugin = plugin.do_string(plugin_body)
print_plugin:run()
time.sleep(2)
print_plugin:stop()
time.sleep(1)

local running = print_plugin:is_running()
if running then error("already running") end
-- also you can get last error: print_plugin:error()

-- plugin.do_string_with_payload()
-- also you can use: plugin.do_file_with_payload(filename)
local job_body = [[
    print(payload)
]]
local print_plugin_with_payload = plugin.do_string_with_payload(plugin_body, "text of payload")
print_plugin_with_payload:run()
-- must print: "text of payload"
time.sleep(1)
local running = print_plugin:is_running()
if running then error("already running") end

Documentation

Overview

Package plugin implements run lua-code from lua-code.

Example (Package)

plugin.do_string(), plugin_ud:run(), plugin_ud:stop()

state := lua.NewState()
defer state.Close()
Preload(state)
time.Preload(state)
source := `
    local plugin = require("plugin")
    local time = require("time")

    local plugin_body = [[
        local doCh, doneCh = unpack(arg)
        local i = 1
        while doCh:receive() do
            print(i)
            doneCh:send(i)
            i = i + 1
        end
    ]]

    -- Make synchronization channels and fire up the plugin
    local doCh = channel.make(100)
    local doneCh = channel.make(100)
    local print_plugin = plugin.do_string(plugin_body, doCh, doneCh)
    print_plugin:run()

    -- Allow two iterations to proceed
    doCh:send(nil)
    local ok, got = doneCh:receive()
    assert(ok and got == 1, string.format("ok = %s; got = %s", ok, got))
    doCh:send(nil)
    ok, got = doneCh:receive()
    assert(ok and got == 2, string.format("ok = %s; got = %s", ok, got))

    -- Close the doCh and wait to ensure it's closed gracefully but stop just to be sure
    doCh:close()
    time.sleep(1)
    print_plugin:stop()
    time.sleep(1)

    -- Ensure it's not still running
    assert(not print_plugin:is_running(), "still running")
`
if err := state.DoString(source); err != nil {
	log.Fatal(err.Error())
}
Output:

1
2
Example (Using_like_goroutine)
state := lua.NewState()
defer state.Close()

Preload(state)

source := `
	local plugin = require 'plugin'

	function myfunc(...)
		print(table.concat({...}, ""))
	end

	local background_body = [[
		return pcall(unpack(arg))
	]]
	local background_plugin = plugin.do_string(background_body, myfunc, "Hello", " ", "World")
	background_plugin:run()
	local err = background_plugin:wait()
	assert(not err, err)
`
if err := state.DoString(source); err != nil {
	log.Fatal(err)
}
Output:

Hello World

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DoFile

func DoFile(L *lua.LState) int

DoFile lua plugin.do_file(filename) returns plugin_ud

func DoFileWithPayload

func DoFileWithPayload(L *lua.LState) int

DoFileWithPayload lua plugin.async() returns (plugin_ud, err)

func DoString

func DoString(L *lua.LState) int

DoString lua plugin.do_string(body) returns plugin_ud

func DoStringWithPayload

func DoStringWithPayload(L *lua.LState) int

DoStringWithPayload lua plugin.async() returns (plugin_ud, err)

func DoneChannel added in v0.3.0

func DoneChannel(L *lua.LState) int

DoneChannel lua plugin_ud:done_chan()

func Error

func Error(L *lua.LState) int

Error lua plugin_ud:error() returns err

func IsRunning

func IsRunning(L *lua.LState) int

IsRunning lua plugin_ud:is_running()

func Loader

func Loader(L *lua.LState) int

Loader is the module loader function.

func NewLuaPlugin added in v0.3.0

func NewLuaPlugin(L *lua.LState, n int) *luaPlugin

func NewPluginState

func NewPluginState() *lua.LState

NewPluginState return lua state

func Preload

func Preload(L *lua.LState)

Preload adds plugin to the given Lua state's package.preload table. After it has been preloaded, it can be loaded using require:

local plugin = require("plugin")

func PreloadAll added in v0.3.0

func PreloadAll(L *lua.LState)

PreloadAll preload all gopher lua packages - note it's needed here to prevent circular deps between plugin and libs

func Run

func Run(L *lua.LState) (nRet int)

Run lua plugin_ud:run()

func Stop

func Stop(L *lua.LState) int

Stop lua plugin_ud:stop()

func Wait added in v0.3.0

func Wait(L *lua.LState) int

Wait lua plugin_ud:wait()

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL