• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang luar.Init函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/stevedonovan/luar.Init函数的典型用法代码示例。如果您正苦于以下问题:Golang Init函数的具体用法?Golang Init怎么用?Golang Init使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了Init函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: ExampleMap

func ExampleMap() {
	const code = `
print(#M)
print(M.one)
print(M.two)
print(M.three)
`

	L := luar.Init()
	defer L.Close()

	M := luar.Map{
		"one":   "ein",
		"two":   "zwei",
		"three": "drei",
	}

	luar.Register(L, "", luar.Map{
		"M":     M,
		"print": fmt.Println,
	})

	err := L.DoString(code)
	if err != nil {
		fmt.Println("error", err.Error())
	}
	// Output:
	// 3
	// ein
	// zwei
	// drei
}
开发者ID:stevedonovan,项目名称:luar,代码行数:32,代码来源:example_test.go


示例2: main

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)
		}
	}
}
开发者ID:sanyaade-embedded-systems,项目名称:luar,代码行数:31,代码来源:luar2.go


示例3: main

func main() {
	L := luar.Init()
	defer L.Close()

	M := luar.Map{
		"one":   "ein",
		"two":   "zwei",
		"three": "drei",
	}

	S := []string{"alfred", "alice", "bob", "frodo"}

	ST := &MyStruct{"Dolly", 46}

	luar.Register(L, "", luar.Map{
		"M":  M,
		"S":  S,
		"ST": ST,
	})

	err := L.DoString(code)
	if err != nil {
		fmt.Println("error", err.Error())
	}

}
开发者ID:EncoreJiang,项目名称:luar,代码行数:26,代码来源:map.go


示例4: main

func main() {
	lua := `
    fn = function(obj)
      slice = obj.GetSlice()
      print(type(slice), #slice)
      obj = slice[1]  -- slice[2] will raise a 'index out of range' error
      -- obj.Naam = 'howzit'  -- will raise a 'no field' error
      name = obj.GetName() 
      return name
    end
  `
	L := luar.Init()
	defer L.Close()

	L.DoString(lua)

	luafn := luar.NewLuaObjectFromName(L, "fn")
	gobj := NewStructWithSlice("string")
	res, err := luafn.Call(gobj)
	if err != nil {
		fmt.Println("error!", err)
	} else {
		fmt.Println("result", res)
	}
}
开发者ID:EncoreJiang,项目名称:luar,代码行数:25,代码来源:slice.go


示例5: Example_pointers

func Example_pointers() {
	const test = `
-- Pointers to structs and structs within pointers are automatically dereferenced.
local t = newRef()
Print(t.Index, t.Number, t.Title)
`

	type Ref struct {
		Index  int
		Number *int
		Title  *string
	}

	newRef := func() *Ref {
		n := new(int)
		*n = 10
		t := new(string)
		*t = "foo"
		return &Ref{Index: 17, Number: n, Title: t}
	}

	L := luar.Init()
	defer L.Close()

	luar.Register(L, "", luar.Map{
		"Print":  fmt.Println,
		"newRef": newRef,
	})

	L.DoString(test)
	// Output:
	// 17 10 foo
}
开发者ID:stevedonovan,项目名称:luar,代码行数:33,代码来源:example_test.go


示例6: main

func main() {
	L := luar.Init()
	defer L.Close()

	L.GetGlobal("print")
	print := luar.NewLuaObject(L, -1)
	print.Call("one two", 12)

	L.GetGlobal("package")
	pack := luar.NewLuaObject(L, -1)
	fmt.Println(pack.Get("path"))

	/*
	   L.GetGlobal("string")
	   strtab := luar.NewLuaObject(L,-1)
	   iter := strtab.Iter()
	   for iter.Next() {
	       fmt.Println(iter.Key,iter.Value)
	   }
	*/

	gsub := luar.NewLuaObjectFromName(L, "string.gsub")
	res, err := gsub.Call("hello $NAME go $HOME", "%$(%u+)", luar.Map{
		"NAME": "Dolly",
		"HOME": "where you belong",
	})
	if res == nil {
		fmt.Println("error", err)
	} else {
		fmt.Println("result", res)
	}

}
开发者ID:sanyaade-embedded-systems,项目名称:luar,代码行数:33,代码来源:luar3.go


示例7: initLua

func (PM *PluginManager) initLua() {
	PM.L = luar.Init()
	luar.RawRegister(PM.L, "", luar.Map{
		"RegisterCommand": func(L *lua.State) int {
			name := L.ToString(1)
			fn := luar.NewLuaObject(L, 2)
			PM.Commands[name] = Plugin{name, fn}
			log.Printf("    %-10s command\n", name)
			return 0
		},
		"RegisterEvent": func(L *lua.State) int {
			name := L.ToString(1)
			event := L.ToString(2)
			fn := luar.NewLuaObject(L, 3)
			if _, ok := PM.Events[event]; !ok {
				PM.Events[event] = make(map[string]Plugin)
			}
			PM.Events[event][name] = Plugin{name, fn}
			log.Printf("    %-10s event\n", name)
			return 0
		},
	})

	luar.Register(PM.L, "go", luar.Map{
		"Split":  strings.Split,
		"SplitN": strings.SplitN,
		"PrintTable": func(table interface{}) {
			log.Printf("%#v\n", table)
		},
	})
}
开发者ID:patterns,项目名称:gomero,代码行数:31,代码来源:plugin.go


示例8: ExampleCopyTableToMap

// 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
}
开发者ID:stevedonovan,项目名称:luar,代码行数:37,代码来源:example_test.go


示例9: ExampleLuaTableIter_Next

func ExampleLuaTableIter_Next() {
	const code = `
return {
  foo = 17,
  bar = 18,
}
`

	L := luar.Init()
	defer L.Close()

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

	lo := luar.NewLuaObject(L, -1)

	iter := lo.Iter()
	keys := []string{}
	values := map[string]float64{}
	for iter.Next() {
		k := iter.Key.(string)
		keys = append(keys, k)
		values[k] = iter.Value.(float64)
	}
	sort.Strings(keys)

	for _, v := range keys {
		fmt.Println(v, values[v])
	}
	// Output:
	// bar 18
	// foo 17
}
开发者ID:stevedonovan,项目名称:luar,代码行数:35,代码来源:example_test.go


示例10: main

func main() {
	L := luar.Init()
	defer L.Close()

	L.GetGlobal("print")
	print := luar.NewLuaObject(L, -1)
	print.Call("one two", 12)

	L.GetGlobal("package")
	pack := luar.NewLuaObject(L, -1)
	fmt.Println(pack.Get("path"))

	lcopy := luar.NewLuaObjectFromValue

	gsub := luar.NewLuaObjectFromName(L, "string.gsub")

	rmap := lcopy(L, luar.Map{
		"NAME": "Dolly",
		"HOME": "where you belong",
	})

	res, err := gsub.Call("hello $NAME go $HOME", "%$(%u+)", rmap)
	if res == nil {
		fmt.Println("error", err)
	} else {
		fmt.Println("result", res)
	}

}
开发者ID:EncoreJiang,项目名称:luar,代码行数:29,代码来源:luar3.go


示例11: LuaInit

// TODO: better error handling
func LuaInit() *lua.State {
	L := luar.Init()
	//L.AtPanic(LuaAtPanic)

	L.OpenLibs()
	L.DoString("math.randomseed( os.time() )")

	return L
}
开发者ID:stephenbalaban,项目名称:goland,代码行数:10,代码来源:lua.go


示例12: NewConf

func NewConf() *Conf {
	c := Conf{
		l: luar.Init(),
	}

	c.registerCommands()

	return &c
}
开发者ID:jeromer,项目名称:haiconf,代码行数:9,代码来源:main.go


示例13: ExampleMakeChan

func ExampleMakeChan() {
	L1 := luar.Init()
	defer L1.Close()
	L2 := luar.Init()
	defer L2.Close()

	luar.MakeChan(L1)
	L1.SetGlobal("c")
	L1.GetGlobal("c")
	c := luar.LuaToGo(L1, nil, -1)

	luar.Register(L2, "", luar.Map{
		"c":     c,
		"Print": fmt.Println,
	})

	const code1 = `
c.send(17)
`

	const code2 = `
v = c.recv()
Print(v)
`

	var wg sync.WaitGroup
	wg.Add(1)
	go func() {
		err := L1.DoString(code1)
		if err != nil {
			fmt.Println(err)
		}
		wg.Done()
	}()

	err := L2.DoString(code2)
	if err != nil {
		fmt.Println(err)
	}
	wg.Wait()
	// Output:
	// 17
}
开发者ID:stevedonovan,项目名称:luar,代码行数:43,代码来源:example_test.go


示例14: mkstate

// mkstate constructs a new lua state with the appropriate functions binded
// for use in scripts.
// it also binds functions used for actions, which should probably go somewhere else.
func mkstate() *lua.State {
	L := luar.Init()

	luar.Register(L, "", luar.Map{
		"connect": lconnect,
		"http":    lhttp,
	})

	return L
}
开发者ID:hackerlist,项目名称:monty,代码行数:13,代码来源:lua.go


示例15: InitPlugin

func (r Runtime) InitPlugin(name, source string, implements func(string)) error {
	context := luar.Init()
	luar.Register(context, "", luar.Map{
		"implements": func(interfaceName string) {
			implements(interfaceName)
		},
	})
	context.DoString(source)
	r.plugins[name] = context
	return nil
}
开发者ID:progrium,项目名称:go-plugins-lua,代码行数:11,代码来源:luaplugins.go


示例16: main

func main() {
	L := luar.Init()
	defer L.Close()

	luar.Register(L, "", luar.Map{
		"Print": fmt.Println,
		"MSG":   "hello", // can also register constants
	})

	L.DoString(test)

}
开发者ID:EncoreJiang,项目名称:luar,代码行数:12,代码来源:example.go


示例17: main

func main() {
	whj := "wang::hai::jun"
	fmt.Println(strings.Split(whj, "::"))
	TestCall("111")
	L := luar.Init()
	defer L.Close()
	M := luar.Map{
		"one":   "ein",
		"two":   "zwei",
		"three": "drei",
	}

	S := []string{"alfred", "alice", "bob", "frodo"}

	ST := &MyStruct{"Dolly", 46}

	luar.Register(L, "", luar.Map{
		"Print":    fmt.Println,
		"testcall": TestCall,
		"print":    fmt.Println,
		"MSG":      "hello", // can also register constants
		"M":        M,
		"S":        S,
		"ST":       ST,
	})

	//L.DoString(test)
	L.DoString(code)
	L.GetGlobal("print")
	print := luar.NewLuaObject(L, -1)
	print.Call("one two", 12)

	L.GetGlobal("package")
	pack := luar.NewLuaObject(L, -1)
	fmt.Println(pack.Get("path"))

	lcopy := luar.NewLuaObjectFromValue

	gsub := luar.NewLuaObjectFromName(L, "string.gsub")

	rmap := lcopy(L, luar.Map{
		"NAME": "Dolly",
		"HOME": "where you belong",
	})

	res, err := gsub.Call("hello $NAME go $HOME", "%$(%u+)", rmap)
	if res == nil {
		fmt.Println("error", err)
	} else {
		fmt.Println("\033[0;31mresult\033[0m", res)
	}

}
开发者ID:yc7369,项目名称:gostudy,代码行数:53,代码来源:luatest.go


示例18: Init

func (luaipt *LuaIpt) Init(path string) error {
	luaipt.state = luar.Init()
	luaipt.Bind("Debugf", log.Debugf)
	luaipt.Bind("Debug", log.Debug)
	luaipt.Bind("Messagef", log.Messagef)
	luaipt.Bind("Message", log.Message)
	luaipt.Bind("Warningf", log.Warningf)
	luaipt.Bind("Warning", log.Warning)
	luaipt.Bind("Errorf", log.Errorf)
	luaipt.Bind("Error", log.Error)
	luaipt.path = path
	return nil
}
开发者ID:stallman-cui,项目名称:ghoko,代码行数:13,代码来源:lua.go


示例19: main

func main() {
	L := luar.Init()
	defer L.Close()

	luar.Register(L, "", luar.Map{
		"newT": newT,
	})

	res := L.DoString(setup)
	if res != nil {
		fmt.Println("Error:", res)
	}

}
开发者ID:EncoreJiang,项目名称:luar,代码行数:14,代码来源:ptr-elem.go


示例20: main

func main() {
	L := luar.Init()
	defer L.Close()

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

	res := L.DoString(setup)
	if res != nil {
		fmt.Println("Error:", res)
	}

}
开发者ID:kdar,项目名称:luar,代码行数:16,代码来源:struct.go



注:本文中的github.com/stevedonovan/luar.Init函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang luar.Register函数代码示例发布时间:2022-05-28
下一篇:
Golang lua51.State类代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap