github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/lib/base/pcall.go (about) 1 package base 2 3 import ( 4 rt "github.com/arnodel/golua/runtime" 5 ) 6 7 func pcall(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { 8 var err error 9 if err = c.Check1Arg(); err != nil { 10 return nil, err 11 } 12 next := c.Next() 13 res := rt.NewTerminationWith(c, 0, true) 14 _, err = t.CallContext(rt.RuntimeContextDef{}, func() error { 15 return rt.Call(t, c.Arg(0), c.Etc(), res) 16 }) 17 if err != nil { 18 t.Push1(next, rt.BoolValue(false)) 19 t.Push1(next, rt.ErrorValue(err)) 20 } else { 21 t.Push1(next, rt.BoolValue(true)) 22 t.Push(next, res.Etc()...) 23 } 24 return next, nil 25 } 26 27 func xpcall(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { 28 var err error 29 if err = c.CheckNArgs(2); err != nil { 30 return nil, err 31 } 32 var msgHandler rt.Callable 33 if !c.Arg(1).IsNil() { 34 msgHandler, err = c.CallableArg(1) 35 if err != nil { 36 return nil, err 37 } 38 } 39 next := c.Next() 40 res := rt.NewTerminationWith(c, 0, true) 41 42 _, err = t.CallContext(rt.RuntimeContextDef{ 43 MessageHandler: msgHandler, 44 }, func() error { 45 return rt.Call(t, c.Arg(0), c.Etc(), res) 46 }) 47 if err != nil { 48 t.Push1(next, rt.BoolValue(false)) 49 t.Push1(next, rt.ErrorValue(err)) 50 } else { 51 t.Push1(next, rt.BoolValue(true)) 52 t.Push(next, res.Etc()...) 53 } 54 return next, nil 55 }