arnodel / golua

A Lua compiler / runtime in Go
Apache License 2.0
93 stars 10 forks source link

Embedding quickstart example does not compile #92

Closed TheCount closed 1 year ago

TheCount commented 1 year ago

The embedding example from the README does not compile. When I try to embed that code snippet in a program like this:

package main

import (
    "fmt"
    "os"

    rt "github.com/arnodel/golua/runtime"
)

func main() {
    // First we obtain a new Lua runtime which outputs to stdout
    r := rt.New(os.Stdout)

    // This is the chunk we want to run.  It returns an adding function.
    source := []byte(`return function(x, y) return x + y end`)

    // Compile the chunk. Note that compiling doesn't require a runtime.
    chunk, _ := rt.CompileAndLoadLuaChunk("test", source, r.GlobalEnv())

    // Run the chunk in the runtime's main thread.  Its output is the Lua adding
    // function.
    f, _ := rt.Call1(r.MainThread(), chunk)

    // Now, run the Lua function in the main thread.
    sum, _ := rt.Call1(r.MainThread(), f, rt.Int(40), rt.Int(2))

    // --> 42
    fmt.Println(sum)
}

I get compiler errors:

./embed.go:18:17: undefined: runtime.CompileAndLoadLuaChunk
./embed.go:25:43: undefined: runtime.Int

With the "obvious" fixes it works again:

package main

import (
    "fmt"
    "os"

    rt "github.com/arnodel/golua/runtime"
)

func main() {
    // First we obtain a new Lua runtime which outputs to stdout
    r := rt.New(os.Stdout)

    // This is the chunk we want to run.  It returns an adding function.
    source := []byte(`return function(x, y) return x + y end`)

    // Compile the chunk. Note that compiling requires a runtime.
    chunk, _ := r.CompileAndLoadLuaChunk(
        "test", source, rt.TableValue(r.GlobalEnv()))

    // Run the chunk in the runtime's main thread.  Its output is the Lua adding
    // function.
    f, _ := rt.Call1(r.MainThread(), rt.FunctionValue(chunk))

    // Now, run the Lua function in the main thread.
    sum, _ := rt.Call1(r.MainThread(), f, rt.IntValue(40), rt.IntValue(2))

    // --> 42
    fmt.Println(sum.AsInt())
}

correctly prints 42.

Therefore I'd like to suggest to update the README accordingly.

arnodel commented 1 year ago

Thank you for reporting this. I'll fix it ASAP.