Ejemplo n.º 1
0
func (app *App) openScript() {
	s := new(script)
	s.app = app

	s.chunks = make(map[string]struct{})

	app.script = s

	l := lua.NewState()

	l.OpenBase()
	l.OpenLibs()
	l.OpenMath()
	l.OpenString()
	l.OpenTable()
	l.OpenPackage()

	l.OpenCJson()
	l.OpenCMsgpack()
	l.OpenStruct()

	l.Register("error", luaErrorHandler)

	s.l = l
	s.c = newClient(app)
	s.c.db = nil

	w := new(luaWriter)
	w.l = l
	s.c.resp = w

	l.NewTable()
	l.PushString("call")
	l.PushGoFunction(luaCall)
	l.SetTable(-3)

	l.PushString("pcall")
	l.PushGoFunction(luaPCall)
	l.SetTable(-3)

	l.PushString("sha1hex")
	l.PushGoFunction(luaSha1Hex)
	l.SetTable(-3)

	l.PushString("error_reply")
	l.PushGoFunction(luaErrorReply)
	l.SetTable(-3)

	l.PushString("status_reply")
	l.PushGoFunction(luaStatusReply)
	l.SetTable(-3)

	l.SetGlobal("ledis")

	setMapState(l, s)
}
Ejemplo n.º 2
0
func TestLuaWriter(t *testing.T) {
	l := lua.NewState()

	l.OpenBase()

	testLuaWriter.l = l

	l.Register("WriteError", testLuaWriteError)

	str := `
        WriteError() 
        `

	err := l.DoString(str)

	if err == nil {
		t.Fatal("must error")
	}

	l.Register("WriteArray", testLuaWriteArray)

	str = `
        local  a = WriteArray()

        if #a ~= 2 then
            error("len a must 2")
        elseif a[1] ~= "1" then 
            error("a[1] must 1")
        elseif #a[2] ~= 2 then 
            error("len a[2] must 2")
        elseif a[2][1] ~= 10 then
            error("a[2][1] must 10")
        elseif a[2][2] ~= "11" then
            error("a[2][2] must 11") 
        end
        `

	err = l.DoString(str)
	if err != nil {
		t.Fatal(err)
	}

	l.Close()
}