示例#1
0
func main() {
	L := luar.Init()
	defer L.Close()

	// arbitrary Go functions can be registered
	// to be callable from Lua
	luar.Register(L, "", luar.Map{
		"GoFun": GoFun,
	})

	res := L.DoString(code)
	if !res {
		fmt.Println("Error:", L.ToString(-1))
		os.Exit(1)
	}

	res = L.DoString(setup)
	if !res {
		fmt.Println("Error:", L.ToString(-1))
		os.Exit(1)
	} else {
		// there will be a table on the stack!
		fmt.Println("table?", L.IsTable(-1))
		v := luar.CopyTableToMap(L, nil, -1)
		fmt.Println("returned map", v)
		m := v.(map[string]interface{})
		for k, v := range m {
			fmt.Println(k, v)
		}
	}
}
示例#2
0
// Read configuration in Lua format.
//
// WARNING: Deprecated.
func ExampleCopyTableToMap() {
	L := luar.Init()
	defer L.Close()

	err := L.DoString(config)
	if err != nil {
		log.Fatal(err)
	}

	// There should be a table on the Lua stack.
	if !L.IsTable(-1) {
		log.Fatal("no table on stack")
	}

	v := luar.CopyTableToMap(L, nil, -1)
	// Extract table from the returned interface.
	m := v.(map[string]interface{})
	marked := m["marked"].([]interface{})
	options := m["options"].(map[string]interface{})

	fmt.Printf("%#v\n", m["baggins"])
	fmt.Printf("%#v\n", m["name"])
	fmt.Printf("%#v\n", len(marked))
	fmt.Printf("%.1f\n", marked[0])
	fmt.Printf("%.1f\n", marked[1])
	fmt.Printf("%#v\n", options["leave"])
	// Output:
	// true
	// "dumbo"
	// 2
	// 1.0
	// 2.0
	// true
}
示例#3
0
// Construct a new LuaConfig given the lua state and file name.
// Returns *LuaConfig, nil on success and nil, error on error.
// Expects that the file provided is a lua script that will return a
// table of strings to values, for example:
// config = {
//   key = "value",
//   boolean = false,
// }
// return config
//
func NewLuaConfig(lua *lua.State, file string) (*LuaConfig, error) {
	lc := &LuaConfig{file: file}

	if err := lua.DoFile(file); err != nil {
		return nil, fmt.Errorf("NewLuaConfig: Can't load %s: %s", file, err)
	} else {
		m := luar.CopyTableToMap(lua, nil, -1)
		lc.conf = m.(map[string]interface{})
	}

	return lc, nil
}