Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error handling with closures? #11

Closed
dnaeon opened this issue Jun 24, 2016 · 1 comment
Closed

Error handling with closures? #11

dnaeon opened this issue Jun 24, 2016 · 1 comment

Comments

@dnaeon
Copy link

dnaeon commented Jun 24, 2016

This one is related to #10 and how to handle errors from Go functions in Lua.

I have a program, which registers new Lua types from Go as providers, which return some result and an error.

As I don't want to do the error handling in Lua, I thought I'd use a closure and close the Go provider function where I could do the error handling and raise errors in Lua in case I have an error from my provider function. That way I only return the result from the Go function to Lua and do the error handling in the wrapper function.

What I've noticed though is that whenever I register my providers in Lua the wrapper function is always the same, resulting in overwriting the wrappers for previous providers.

Here's an example code:

package main

import (
    "fmt"

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

// provider type
type provider func(name string) (string, error)

// registry contains all registered providers
var registry = map[string]provider{
    "provider1": provider_1,
    "provider2": provider_2,
}

func provider_1(name string) (string, error) {
    return "provider_1 result", nil
}

func provider_2(name string) (string, error) {
    return "provider_2 results", nil
}

func main() {
    L := lua.NewState()
    defer L.Close()

    // Register the providers in Lua
    for typ, provider := range registry {
        wrapper := func(L *lua.LState) int {
            result, err := provider(L.CheckString(1))
            if err != nil {
                L.RaiseError(err.Error()) // Do the error handling here, so that we don't have to return the error to Lua
            }

            L.Push(luar.New(L, result))
            return 1 // Number of arguments
        }

        tbl := L.NewTable()
        tbl.RawSetH(lua.LString("new"), L.NewFunction(wrapper)) // New wrapper overwrites all previous ones
        L.SetGlobal(typ, tbl)

        fmt.Printf("type %s -> wrapper %s\n", typ, wrapper)
    }

    // Run some Lua code
    code := `
    print(provider1.new("some input for provider1"))
    print(provider2.new("some input for provider2"))
    `

    if err := L.DoString(code); err != nil {
        panic(err)
    }
}

And here's the output it generates.

type provider1 -> wrapper %!s(func(*lua.LState) int=0x401a90)
type provider2 -> wrapper %!s(func(*lua.LState) int=0x401a90)
provider_2 results
provider_2 results

As you can see the wrapper function is always the same one, but I'd expect them to be different on each iteration. As a result the last wrapper function overwrites all previous ones, and the returned result is always from the last registered provider (in this case provider2).

Is this something I've overlooked or a bug in gopher-luar?

Thanks,
Marin

@dnaeon
Copy link
Author

dnaeon commented Jun 24, 2016

Managed to get it working by fixing the closure with the following code.

package main

import (
    "fmt"

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

// provider type
type provider func(name string) (string, error)

// registry contains all registered providers
var registry = map[string]provider{
    "provider1": provider_1,
    "provider2": provider_2,
}

func provider_1(name string) (string, error) {
    return "provider_1 result", nil
}

func provider_2(name string) (string, error) {
    return "provider_2 results", nil
}

func main() {
    L := lua.NewState()
    defer L.Close()

    // Register the providers in Lua
    for typ, prov := range registry {
        wrapper := func(p provider) lua.LGFunction {
            return func(L *lua.LState) int {
                result, err := p(L.CheckString(1))
                if err != nil {
                    L.RaiseError(err.Error())
                }

                L.Push(luar.New(L, result))
                return 1
            }
        }

        tbl := L.NewTable()
        tbl.RawSetH(lua.LString("new"), L.NewFunction(wrapper(prov)))
        L.SetGlobal(typ, tbl)

        fmt.Printf("type %s -> wrapper %s\n", typ, wrapper)
    }

    // Run some Lua code
    code := `
    print(provider1.new("some input for provider1"))
    print(provider2.new("some input for provider2"))
    `

    if err := L.DoString(code); err != nil {
        panic(err)
    }
}

@dnaeon dnaeon closed this as completed Jun 24, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant