Home  >  Article  >  Backend Development  >  How do I use gopher-lua to define a Lua function that takes a predefined table as a parameter and that the Lua script can access within the function?

How do I use gopher-lua to define a Lua function that takes a predefined table as a parameter and that the Lua script can access within the function?

PHPz
PHPzforward
2024-02-09 20:57:08888browse

如何使用 gopher-lua 定义一个 Lua 函数,该函数有一个预定义的表作为参数,Lua 脚本可以在该函数中访问该表?

php editor Xiaoxin will introduce to you how to use gopher-lua to define a function with predefined table parameters in the Lua function, and access the table within the function. gopher-lua is a Lua interpreter implemented in Go language, which can embed and execute Lua scripts in Go programs. Through reasonable code design and use, we can easily achieve this goal. Next, we'll explain in detail how to do this.

Question content

I know how to define a Go function and make it global (Double example in the documentation). But what if the parameter of this function should be a predefined table?

function calling_this_function_would_be_required(predefined_table)
  print(predefined_table["something"])
end

IMAP server Dovecot does provide something like the above: https://doc.dovecot.org/configuration_manual/authentication/lua_based_authentication/#examples

I would also like to provide predefined functions with tables (or even user data). But I don't really know how to achieve this.

Making a table global is easy (L.SetGlobal(...)), but how do you add it to the expected function?

Add some functions in Go

func CallMe(L *lua.LState) {
    // How do I add a table as argument??
}

func Foo() {
    L := NewState()
    defer L.Close()

    t := L.NewTable()
    t.RawSetString("example", lua.LString("some_value"))

    // I do not want a global table. I would like an expected Lua function that has _this_ table as argument
    L.SetGlobal("predefined_table", t)

    // Not even sure with his...
    L.SetGlobal("calling_this_function_is_required", L.NewFunction©llMe)) 
}

It would be great if anyone could give me some inspiration :-) Thanks in advance

Solution

Based on @koyaanisqatsi's answer, I figured out how to make this work in Go.

Go code example:

<code>package main

import (
    "fmt"

    "github.com/yuin/gopher-lua"
)

type Person struct {
    Name       string
    GivenName  string
    Street     string
    PostalCode string
    City       string
}

func main() {
    p := &Person{
        Name:       "Mustermann",
        GivenName:  "Max",
        Street:     "Sackgasse 19",
        PostalCode: "36304",
        City:       "Alsfeld",
    }

    L := lua.NewState()
    defer L.Close()

    if err := L.DoFile("sample.lua"); err != nil {
        panic(err)
    }

    t := L.NewTable()
    t.RawSetString("name", lua.LString(p.Name))
    t.RawSetString("given_name", lua.LString(p.GivenName))
    t.RawSetString("street", lua.LString(p.Street))
    t.RawSetString("postal_code", lua.LString(p.PostalCode))
    t.RawSetString("city", lua.LString(p.City))

    if err := L.CallByParam(lua.P{
        Fn:      L.GetGlobal("call_me"),
        NRet:    1,
        Protect: true,
    }, t); err != nil {
        panic(err)
    }

    ret := L.Get(-1) // returned value
    L.Pop(1)         // remove received value

    fmt.Println("The result of the Lua function is:", ret)
}
</code>

sample.lua file:

<code>function call_me(tbl)
    print(tbl.name)
    print(tbl.given_name)
    print(tbl.street)
    print(tbl.postal_code)
    print(tbl.city)

    return 0
end
</code>

result:

Mustermann
Max
Sackgasse 19
36304
Alsfeld
The result of the Lua function is: 0

The above is the detailed content of How do I use gopher-lua to define a Lua function that takes a predefined table as a parameter and that the Lua script can access within the function?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete