Golang中使用lua进行扩展

前言

最近在项目中须要使用lua进行扩展,发现github上有一个用golang编写的lua虚拟机,名字叫作gopher-lua.使用后发现还不错,借此分享给你们.git

数据类型

lua中的数据类型与golang中的数据类型对应关系做者已经在文档中说明,值得注意的是类型是以L开头的,类型的名称是以LT开头的.
golang中的数据转换为lua中的数据就必须转换为L开头的类型:github

str := "hello"
num := 10
L.LString(str)
L.LNumber(float64(num))

lua中的数据转换为golang中的数据,项目提供了ToInt,CheckString之类的函数来进行转换,可是这都是必须提早知道类型的,若是不知道就必须进行类型判断:golang

value := L.Get(1)
switch value.Type() {
case lua.LTString:
case lua.LTTable:
....
}

这里还能够使用gopher-luar来方便的进行类型转换.app

golang和lua互相调用函数

golang中的函数必须转换为func(L *lua.State) int这种形式才能注入lua中,返回参数的int表明了返回参数的个数.函数

func hello(L *lua.State) int {
     //将返回参数压入栈中
     L.Push(lua.LString("hello"))
     //返回参数为1个
     return 1
}
//注入lua中
L.SetGlobal("hello", L.NewFunction(hello))

在golang中调用lua函数,lua脚本中需先定义这个函数,而后调用CallByParam进行调用:lua

//先获取lua中定义的函数
fn := L.GetGlobal("hello")
if err := L.CallByParam(lua.P{
    Fn: fn,
    NRet: 1,
    Protect: true,
    }, lua.LNumber(10)); err != nil {
    panic(err)
}
//这里获取函数返回值
ret := L.Get(-1)

Table

关于lua中的table是一个很强大的东西,项目对table也提供了不少方法的支持好比获取一个字段,添加一个字段.这里推荐使用gluamapper,能够将tabl转换为golang中的结构体或者map[string]interface{}类型,这里使用了做者提供的例子:debug

type Role struct {
    Name string
}

type Person struct {
    Name      string
    Age       int
    WorkPlace string
    Role      []*Role
}

L := lua.NewState()
if err := L.DoString(`
person = {
  name = "Michel",
  age  = "31", -- weakly input
  work_place = "San Jose",
  role = {
    {
      name = "Administrator"
    },
    {
      name = "Operator"
    }
  }
}
`); err != nil {
    panic(err)
}
var person Person
if err := gluamapper.Map(L.GetGlobal("person").(*lua.LTable), &person); err != nil {
    panic(err)
}
fmt.Printf("%s %d", person.Name, person.Age)

模块的加载与使用

项目中提供了lua基本模块,调用OpenLibs就能够加载这些模块,其中包括io,math,os,debug等.若是想本身加载能够使用SkipOpenLibs参数跳过.
若是想开发本身的库,文档中也作出了说明:code

func Loader(L *lua.LState) int {
    //注册模块中的导出函数
    mod := L.SetFuncs(L.NewTable(), exports)
    L.Push(mod)
    return 1
}

var exports = map[string]lua.LGFunction{
    "myfunc": myfunc,
}

func myfunc(L *lua.LState) int {
    return 0
}
//这里就能够加载mymodule模块
L.PreloadModule("mymodule", mymodule.Loader)

结语

固然这里只简单介绍了几个基本的用法,项目还有一些不支持的地方,好比:package.loadlib.更多的地方等待读者本身去探索,后面将会提供源代码分析的文章.ip

相关文章
相关标签/搜索