GOLANG expanded use of LUA

https://www.cnblogs.com/zhangboyu/p/7686209.html

 

Foreword

Recently lua need to expand the project, found on github with a golang written in lua virtual machine, called gopher-lua. Use and found good, take this to others.

type of data

lua data types and data types in the correspondence between the author golang has been described in the document, it is worth noting that begins with L type, the name of the type begins with the LT.
data conversion golang of the data in the lua it must be converted to the beginning of the L type:

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

Lua data conversion in the data golang the project provides functions ToInt, CheckString like to convert, but this is a must know in advance the type of, I do not know if it is necessary to determine the type:

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

Here you can also be conveniently used typecast gopher-luar.

golang and lua function call each other

Golang functions must be converted to func (L * lua.State) int lua in this form to injection, int return parameter representative of the number of returned parameters.

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

Call golang function in lua, lua script, you must first define the function, and then call CallByParam call:

//先获取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

table about lua is a very powerful thing, project table also provides support for a number of ways such as to obtain a field, add a field. It is recommended to use gluamapper, you can convert tabl to structure golang in or map [string ] interface {} type, where an example of using provided:

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)

Module load and use

Lua project provides basic module, call OpenLibs can load these modules, including io, math, os, debug and so on. If you want your own load parameters can be used SkipOpenLibs skip.
If you want to develop your own library, the document also made description:

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)
 
Category:  golang

Foreword

Recently lua need to expand the project, found on github with a golang written in lua virtual machine, called gopher-lua. Use and found good, take this to others.

type of data

lua data types and data types in the correspondence between the author golang has been described in the document, it is worth noting that begins with L type, the name of the type begins with the LT.
data conversion golang of the data in the lua it must be converted to the beginning of the L type:

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

Lua data conversion in the data golang the project provides functions ToInt, CheckString like to convert, but this is a must know in advance the type of, I do not know if it is necessary to determine the type:

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

Here you can also be conveniently used typecast gopher-luar.

golang and lua function call each other

Golang functions must be converted to func (L * lua.State) int lua in this form to injection, int return parameter representative of the number of returned parameters.

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

Call golang function in lua, lua script, you must first define the function, and then call CallByParam call:

//先获取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

table about lua is a very powerful thing, project table also provides support for a number of ways such as to obtain a field, add a field. It is recommended to use gluamapper, you can convert tabl to structure golang in or map [string ] interface {} type, where an example of using provided:

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)

Module load and use

Lua project provides basic module, call OpenLibs can load these modules, including io, math, os, debug and so on. If you want your own load parameters can be used SkipOpenLibs skip.
If you want to develop your own library, the document also made description:

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)

Guess you like

Origin www.cnblogs.com/dhcn/p/11243428.html