GopherLua

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2020 License: Apache-2.0 Imports: 2 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

GopherLua 🚜

lua为go增加动态化能力,go为lua提供功能拓展

项目依托gopher-luago平台的lua解释器
进行拓展和封装,主要是提供一个方向/思路,并尝试实现

go拓展库

  • 数据库连接查询库module_db
  • 网络请求库module_http
安装(Installation)
go get github.com/lisabiya/GopherLua
简单示例--http请求(simple Example httpRequest)
import (
	"fmt"
	"github.com/lisabiya/GopherLua"
	"github.com/lisabiya/GopherLua/module_http"
)

func main() {
	gopherLua := GopherLua.NewState()
	gopherLua.Register(module_http.ModuleHttp{})
	err := gopherLua.DoString(
		`
    --引用声明模块
	local http = httpRequest.new()
    local code, response =  http:End({
        get = "https://www.wanandroid.com/hotkey/json",
        query = "nihao",
    })
    --调用函数  
    print(code,response)
`)
	if err != nil {
		fmt.Println(err.Error())
	}
}
性能(performance)
  • 在postman测试脚本中,mysql请求数据列表20ms,1000请求下,内存基本稳定10M以内无变化
  • 当然这只是浅略的测试,目前正打算逐步加入正式项目中实践。(队友听了想打人😀)




添加自定义拓展(Add custom extension)

主要是提供一种思路,需要优化改进的地方还有很多

  • 参考 module_http,module_db
示例一 简单声明拓展函数
  • go声明函数

const metatableName = "request_metatable"

func RegisterFuction(L *lua.LState) {
	mt := L.NewTypeMetatable(metatableName)
	//声明全局对象
	L.SetGlobal("httprequest", mt)
	//添加拓展函数
	L.SetField(mt, "get", L.NewFunction(getSimple))
}

func getSimple(L *lua.LState) int {
	var request = L.CheckTable(1)
	var requestMap, ok = goTool.TransLuaValue2Map(request).(map[string]interface{})
	if ok {
		_, body, _ := gorequest.New().
			Get(requestMap["url"].(string)).
			Query(requestMap["query"]).End()
		L.Push(lua.LNumber(0))
		L.Push(lua.LString(body))
		return 2
	} else {
		L.Push(lua.LNumber(1))
		L.Push(lua.LString("参数转map失败"))
		return 2

	}
}

func main() {
	gopherLua := GopherLua.NewState()
	RegisterFuction(gopherLua.State)
	err := gopherLua.DoFile("test/test.lua")//test.lua 为项目所在文件位置
	if err != nil {
		fmt.Println(err.Error())
	}
}
  • lua中调用 test.lua
--直接引用声明模块
local code, response = httprequest.get(
        { url = "https://www.wanandroid.com/hotkey/json" })
--调用函数
print(code, response)
示例二 声明模块
  • go声明函数
const metatableName = "request_metatable"

type CustomModule struct {
}

func (CustomModule) RegisterType(L *lua.LState) {
	mt := L.NewTypeMetatable(metatableName)
	//声明全局对象
	L.SetGlobal("httprequest", mt)
	//添加拓展函数
	L.SetField(mt, "get", L.NewFunction(getSimple))
}

func (CustomModule) Close() {

}

func getSimple(L *lua.LState) int {
	var request = L.CheckTable(1)
	var requestMap, ok = goTool.TransLuaValue2Map(request).(map[string]interface{})
	if ok {
		_, body, _ := gorequest.New().
			Get(requestMap["url"].(string)).
			Query(requestMap["query"]).End()
		L.Push(lua.LNumber(0))
		L.Push(lua.LString(body))
		return 2
	} else {
		L.Push(lua.LNumber(1))
		L.Push(lua.LString("参数转map失败"))
		return 2

	}
}

func main() {
	gopherLua := GopherLua.NewState()
	gopherLua.Register(CustomModule{})
	err := gopherLua.DoFile("test/test.lua")
	if err != nil {
		fmt.Println(err.Error())
	}
	err = gopherLua.ExecuteFunc("TestHttp", 1, lua.LString("测试参数"))
	if err != nil {
		fmt.Println(err.Error())
	}
	fmt.Println(goTool.TransLuaValue2Map(gopherLua.State.Get(-1)))
}
  • lua中调用 test.lua
--直接引用声明模块
function TestHttp(params)
    local code, response = httprequest.get(
            { url = "https://www.wanandroid.com/hotkey/json" })
    --调用函数
    return { code = code, response = response, params = params }
end

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type InterfaceModule

type InterfaceModule interface {
	RegisterType(L *lua.LState)
	Close()
}

type Lua

type Lua struct {
	State *lua.LState
	// contains filtered or unexported fields
}

func NewState

func NewState() *Lua

func (*Lua) Close

func (instance *Lua) Close()

func (*Lua) DoFile

func (instance *Lua) DoFile(path string) error

func (*Lua) DoString

func (instance *Lua) DoString(source string) error

func (*Lua) ExecuteFunc

func (instance *Lua) ExecuteFunc(funcName string, returnParamsCount int, args ...lua.LValue) error

func (*Lua) GetAndPop added in v1.0.2

func (instance *Lua) GetAndPop(idx int) lua.LValue

获取栈中指定返回值,并pop出去,节约内存

func (*Lua) Register

func (instance *Lua) Register(module ...InterfaceModule)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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