-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
49 lines (38 loc) · 1.05 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"fmt"
"log"
lua "github.com/yuin/gopher-lua"
)
// Define a Go function that will be callable from Lua
func HelloGo(L *lua.LState) int {
fmt.Println("Hello from Go!")
return 0 // Number of return values
}
// Define a Go function that returns an integer
func HelloGoNumber(L *lua.LState) int {
// Return an integer value to Lua
L.Push(lua.LNumber(42))
return 1 // Number of return values
}
func main() {
// Create a new Lua state
L := lua.NewState()
defer L.Close()
// Register the Go function in Lua
L.SetGlobal("HelloGo", L.NewFunction(HelloGo))
// Register our other func
L.SetGlobal("HelloGoNumber", L.NewFunction(HelloGoNumber))
// Set up a custom Lua environment with restricted functions
L.SetGlobal("io", L.NewFunction(sandboxed))
// Set up a custom Lua environment with restricted functions
L.SetGlobal("os", L.NewFunction(sandboxed))
// Run the Lua script
if err := L.DoFile("example.lua"); err != nil {
log.Fatal(err)
}
}
func sandboxed(L *lua.LState) int {
L.Push(lua.LString("not allowed"))
return 1
}