Global Variable of type Lua Table

Abierto
#129 1 comentario 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
22/100
Tipo de issue
Nueva funcionalidad
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
go, lua
Área
compilers

Línea de trabajo

The issue shows only user code: go_exposedLuaFunction builds a table with CreateTable and SetField, and UtilOpen registers it with lua.NewLibrary. Start by checking how the library exposes setting a global value from Go, and whether the state can carry a read-only metatable. Done would mean a Go-created global table is visible to a Lua script and can be made read-only, with a test covering both. The thread has one comment and no maintainer decision, so confirm the approach before writing code.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

I'm sorry for bothering you guys, but I'm have hard time trying to create a global variable on go-lua of type table.

I'd not any issue to return a table from a function call (GO Lang internal func, returning a table type variable.).

But my litle tool need to create a global variable of type table with system information fields.

And it would be nice if I was able to set it as read-only. Using pure LUA, I can set a metatable, but I never did such thing using API (On this case GO code).

The part of expose a GO Lang function to LUA script and return a table as result was prety easy!

Does anybody did something like that?


For those who need return tables from GO function to Lua script, below is my code! I hope it help!

Example:

// Exposed functions table
var goFuncsTable = []lua.RegistryFunction{
	{"AppEnvironment", func(l *lua.State) int { return go_exposedLuaFunction(l) } },
}

// Register functions 
func UtilOpen(l *lua.State) int {
	lua.NewLibrary(l, goFuncsTable)
	return 1
}

// Function implementation itself
func go_exposedLuaFunction(l *lua.State) int {

	l.CreateTable(0, 6)

	l.PushString("1.0.0")
	l.SetField(-2, "Version")

	l.PushString(BuildDate)
	l.SetField(-2, "BuildDate")

	l.PushString(BuildTime)
	l.SetField(-2, "BuildTime")

	l.PushString(runtime.GOOS)
	l.SetField(-2, "OS")

	l.PushString(runtime.GOARCH)
	l.SetField(-2, "Arch")

	l.PushInteger(runtime.GOMAXPROCS(0))
	l.SetField(-2, "CPUCores")

	return 1 // Returning one single table
}
Lenguaje dominante
Go
Estrellas
3.5k
Forks
217
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de Shopify/go-lua

Todos los issues de Shopify/go-lua

Issues similares

Más issues de Go

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.