Exemplo n.º 1
0
func ExamTable() {
	vm := lua.NewVM()
	defer vm.Close()
	vm.Openlibs()

	result := vm.EvalString("T = {1,2,3}; return T")
	fmt.Println("result:", result[0])

	t := result[0].(*lua.Table)
	for i := 1; i <= t.Getn(); i++ {
		fmt.Printf("t[%v]=%v\n", i, t.Get(i))
	}
	ok, err := t.Set("a", "value_a")
	fmt.Println("set a result:", ok, err)
	fmt.Printf("t.a=%v\n", t.Get("a"))

	ok, err = t.Set(nil, "null")
	fmt.Println("set nil result:", ok, err)

	vm.EvalString("print('in lua:', T.a)")

	t.Release()

	ok, err = t.Set("b", "value_b")
	fmt.Println("set result:", ok, err)
}
Exemplo n.º 2
0
func ExamFunction() {
	vm := lua.NewVM()
	defer vm.Close()
	vm.Openlibs()

	result := vm.EvalString("return function(a, b) return a+b end")

	fn := result[0].(*lua.Function)
	result, err := fn.Call(1, 2)
	fmt.Println("call result:", result, err)
}
Exemplo n.º 3
0
func Test1() {
	vm := lua.NewVM()
	defer vm.Close()
	vm.Openlibs()
	var EB = func(buf io.Reader) {
		_, err := vm.EvalBufferWithError(buf, 0)
		if err != nil {
			fmt.Println("> EB", err)
		}
	}
	var ES = func(s string) {
		_, err := vm.EvalStringWithError(s, 0)
		if err != nil {
			fmt.Println("> ES", err)
		}
	}

	f, err := os.Open("baselib.lua")
	if err != nil {
		f, err = os.Open("example/baselib.lua")
		if err != nil {
			fmt.Println(err)
		}
	}
	defer f.Close()
	EB(f)

	ES(`
		bin = pack.Pack(1, 2, {a=1,b=2,c=3, 1, 2})
		print('#bin', #bin)
		print(ToString({pack.Unpack(bin)}))
	`)

	ES(`
		bin = pack.Pack('key', {a=1})
		print('#bin', #bin)
		print(ToString({pack.Unpack(bin)}))
	`)

	ES(`
		bin = pack.Pack({a={b={c={d={e='depth'}}}}})
		print('#bin', #bin)
		print(ToString({pack.Unpack(bin)}))
	`)

	result, err := vm.EvalStringWithError("return 1,2,3,'aaa'", 0)
	fmt.Println(result, err)
}
Exemplo n.º 4
0
func ExamInterface() {
	vm := lua.NewVM()
	defer vm.Close()
	vm.Openlibs()

	CallMe := func(a ...interface{}) []interface{} {
		return a
	}
	vm.AddFunc("CallMe", CallMe)

	result, err := vm.EvalStringWithError(`
		return CallMe(1, true, 'abc', nil)
	`)
	fmt.Println("err:", err)
	fmt.Println("result:", result)
}
Exemplo n.º 5
0
func ExamVararg() {
	vm := lua.NewVM()
	defer vm.Close()
	vm.Openlibs()

	SumAll := func(init int, values ...int) int {
		for i := 0; i < len(values); i++ {
			init += values[i]
		}
		return init
	}
	vm.AddFunc("SumAll", SumAll)

	result, err := vm.EvalStringWithError("return SumAll(1000,2,3,4,5,6)")
	fmt.Println("err:", err)
	fmt.Println("result:", result)
}
Exemplo n.º 6
0
func Test() {
	vm := lua.NewVM()
	vm.Openlibs()

	vm.AddStructs(allMyStruct{})
	vm.AddFunc("test.NewDoublePoint", NewDoublePoint)

	begin := time.Now()
	_, err := vm.EvalStringWithError(`
		doublePoint = test.NewDoublePoint()
		for i=0, 1000000 do
			doublePoint.P1_X = doublePoint.P2_X
		end
	`)
	end := time.Now()
	fmt.Println(err, end.Sub(begin))
}
Exemplo n.º 7
0
func Test1() {
	vm := lua.NewVM()
	defer vm.Close()

	vm.Openlibs()

	vm.AddStructList(allMyStruct{})

	vm.AddFunc("NewPoint", NewPoint)
	vm.AddFunc("NewDoublePoint", NewDoublePoint)
	vm.AddFunc("NewIntSlice", NewIntSlice)
	vm.AddFunc("NewStrIntMap", NewStrIntMap)
	vm.AddFunc("go.golang", GetHello)

	_, err := vm.AddFunc("go.golang.GetHello", GetHello)
	fmt.Println("AddFunc", err)

	var ES = func(s string) {
		_, err := vm.EvalStringWithError(s, 0)
		if err != nil {
			fmt.Println("> ES", err)
		}
	}
	var EB = func(buf io.Reader) {
		_, err := vm.EvalBufferWithError(buf, 0)
		if err != nil {
			fmt.Println("> EB", err)
		}
	}

	ES("pt = NewPoint(1, 2)")

	ES("print(pt.X, pt.Y)")

	ES("print(pt.SumXY)")

	// string reader
	EB(strings.NewReader("print(pt:SumXY())"))

	// file reader
	f, err := os.Open("baselib.lua")
	if err != nil {
		fmt.Println(err)
	} else {
		defer f.Close()
		EB(f)
	}

	ES("map = NewStrIntMap(); print('map[a]', #map, map['c'], map['x'])")
	ES("map['c']=nil; print('map[c]', map['c'])")
	ES("map['c']=4; print('map[c]', map['c'])")
	ES("map[1]=4")
	ES("map['1']='4'")

	ES("slice = NewIntSlice(); print('slice[0]', #slice, slice[0])")
	ES("slice[0]=100; print(slice[0])")
	ES("slice[-1]=200")
	ES("slice['a']=200")

	ES("dp = NewDoublePoint(); print('dp.P1.X', dp.P1_X)")
	ES("dp.P1_X = 100; print('dp.P1.X', dp.P1_X)")
	ES("dp.P1_X = '100'")
	ES("dp.P1_K = 100")

	ok, err := vm.AddFunc("", WrongRawFunc1)
	fmt.Println("WrongRawFunc1", ok, err)

	ok, err = vm.AddFunc("", WrongRawFunc2)
	fmt.Println("WrongRawFunc2", ok, err)

	ok, err = vm.AddFunc("", WrongRawFunc3)
	fmt.Println("WrongRawFunc3", ok, err)

	ES("print(go.golang.GetHello())")
	ES("print(go.golang())")

	ES("for _, k in ipairs(golang.Keys(map)) do print(k) end")
	ES("print(golang.HasKey(map, 1))")
	ES("print(golang.HasKey(map, 'k'))")
	ES("print(golang.HasKey(map, 'a'))")
}