arnodel / golua

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

requiring luarocks modules #104

Closed amlwwalker closed 3 weeks ago

amlwwalker commented 4 months ago

I am trying to install and use luarocks modules (e.g cjson) - I'm failing lol... is there a way to do this?

I created a function to install modules from my Go app and I want to install them locally (working directory) so not to pollute the OS.

I have

// Function to install Lua modules using luarocks
func installLuaModule(module string) error {
    // Get the current working directory
    cwd, err := os.Getwd()
    if err != nil {
        return fmt.Errorf("failed to get current working directory: %w", err)
    }

    // Set LUA_PATH and LUA_CPATH to include the current working directory
    luarocksArgs := []string{"--tree=" + cwd, "install", module}

    // Run the luarocks command to install the module
    cmd := exec.Command("luarocks", luarocksArgs...)
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr

    if err := cmd.Run(); err != nil {
        return fmt.Errorf("failed to install Lua module: %w", err)
    }

    return nil
}

Now i want to execute a script that require's for instance cjson:

func executeLuaScript(filePath string) error {
    os.Setenv("GOVERSION", runtime.Version())
    os.Setenv("GOOS", runtime.GOOS)
    os.Setenv("GOARCH", runtime.GOARCH)
    cwd, err := os.Getwd()
    if err != nil {
        return fmt.Errorf("failed to get current working directory: %w", err)
    }
    luaPath := filepath.Join(cwd, "share", "lua", "5.4", "?.lua") + ";" + filepath.Join(cwd, "share", "lua", "5.4", "?", "init.lua")
    luaCPath := filepath.Join(cwd, "lib", "lua", "5.4", "?.so")

    //// Read the Lua script from the file
    script, err := os.ReadFile(filePath)
    if err != nil {
        return fmt.Errorf("failed to read Lua script: %w", err)
    }
    r := rt.New(os.Stdout) // Create runtime

    rawPackage := r.GlobalEnv().Get(rt.StringValue("package"))
    var pkgTable *rt.Table
    if rawPackage.Type() == rt.TableType {
        pkgTable = rawPackage.AsTable()
    } else {
        pkgTable = rt.NewTable()
        r.GlobalEnv().Set(rt.StringValue("package"), rt.TableValue(pkgTable))
    }

    // Set path and cpath in the package table
    pkgTable.Set(rt.StringValue("path"), rt.StringValue(luaPath))
    pkgTable.Set(rt.StringValue("cpath"), rt.StringValue(luaCPath))

    lib.LoadLibs(
        r,
        base.LibLoader,       // Load base lib (needed for print)
        packagelib.LibLoader, // Load package lib (needed for require)
        regexlib.LibLoader,   // Load our example lib
    )

    // Now compile and run the lua code
    chunk, err := r.CompileAndLoadLuaChunk("test", []byte(script), rt.TableValue(r.GlobalEnv()))
    if err != nil {
        fmt.Printf("error %s\n", err)
    }
    _, err = rt.Call1(r.MainThread(), rt.FunctionValue(chunk))
    if err != nil {
        fmt.Printf("error %s\n", err)
    }
    return nil
}

But I keep getting error error: test:1: could not find package 'cjson'

I tried a simpler script

print("Lua Version:", _VERSION)
print("System Information:")
print("Go Version:", os.getenv("GOVERSION"))
print("GOOS:", os.getenv("GOOS"))
print("GOARCH:", os.getenv("GOARCH"))
print("Luarocks Config:")
os.execute("luarocks config")

but now I get


Lua Version:    Golua 5.4
System Information:
error error: test:3: attempt to index a nil value

what am I doing wrong to get erorrs in both instances?

TorchedSammy commented 4 months ago

But I keep getting error error: test:1: could not find package 'cjson'

golua cannot use c written libraries.

I tried a simpler script but now I get Lua Version: Golua 5.4 System Information: error error: test:3: attempt to index a nil value

you did not load the oslib

arnodel commented 3 weeks ago

Golua runs on the Go runtime, I have no plan to support C libraries!