金庆的专栏

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  423 随笔 :: 0 文章 :: 454 评论 :: 0 Trackbacks
# 测试 tolua 例子 TestErrorStack

(金庆的专栏 2020.9)

## Error1

1. 点击 "Error1" 按钮
1. c# showStack.PCall()
1. lua ShowStack()
1. c# Test1()
1. c# try { show.PCall() }
1. lua Show() error('this is error')                

```
LuaException: TestErrorStack:2: this is error
stack traceback:
    [C]: in function 'error'
    TestErrorStack:2: in function <TestErrorStack:1>
    [C]: in function 'Test1'
    TestErrorStack:6: in function <TestErrorStack:5>
LuaInterface.LuaState:PCall(Int32, Int32) (at Assets/ToLua/Core/LuaState.cs:758)
LuaInterface.LuaFunction:PCall() (at Assets/ToLua/Core/LuaFunction.cs:96)
TestLuaStack:Test1(IntPtr) (at Assets/ToLua/Examples/TestErrorStack/TestLuaStack.cs:27)
LuaInterface.LuaDLL:lua_pcall(IntPtr, Int32, Int32, Int32)
LuaInterface.LuaState:PCall(Int32, Int32) (at Assets/ToLua/Core/LuaState.cs:755)
LuaInterface.LuaFunction:PCall() (at Assets/ToLua/Core/LuaFunction.cs:96)
TestLuaStack:OnGUI() (at Assets/ToLua/Examples/TestErrorStack/TestLuaStack.cs:375)
```

lua Show() 中抛 error, 在 C# 中 try-catch 得到,
通过 toluaL_exception() 返回 Lua 调用者 ShowStack(),
ShowStack() 中止执行,传递异常到 c# 调用者 OnGUI(),
OnGUI()中断执行,打印错误信息。

## Instantiate Error

1. "Instantiate Error"
1. c# GetFunction("Instantiate").PCall()
1. lua Instantiate()
1. c# UnityEngine.Object.Instantiate(obj)
1. c# TestInstantiate.Awake()
1. c# try { GetFunction("Show").PCall() }
1. lua Show()
1. c# state.ThrowLuaException(e)

```
LuaException: TestErrorStack:2: this is error
stack traceback:
    [C]: in function 'error'
    TestErrorStack:2: in function <TestErrorStack:1>
    [C]: in function 'Instantiate'
    TestErrorStack:12: in function <TestErrorStack:11>
LuaInterface.LuaState:PCall(Int32, Int32) (at Assets/ToLua/Core/LuaState.cs:758)
LuaInterface.LuaFunction:PCall() (at Assets/ToLua/Core/LuaFunction.cs:96)
TestInstantiate:Awake() (at Assets/ToLua/Examples/TestErrorStack/TestInstantiate.cs:15)
UnityEngine.Object:Instantiate(Object)
UnityEngine_ObjectWrap:Instantiate(IntPtr) (at Assets/ToLua/BaseType/UnityEngine_ObjectWrap.cs:203)
LuaInterface.LuaDLL:lua_pcall(IntPtr, Int32, Int32, Int32)
LuaInterface.LuaState:PCall(Int32, Int32) (at Assets/ToLua/Core/LuaState.cs:755)
LuaInterface.LuaFunction:PCall() (at Assets/ToLua/Core/LuaFunction.cs:96)
TestLuaStack:OnGUI() (at Assets/ToLua/Examples/TestErrorStack/TestLuaStack.cs:391)
```
在 lua 中实例化对象,Awake() 中向lua抛异常:state.ThrowLuaException(e)。
中止了 lua 调用和 OnGUI(), 但是新对象的 Start() 成功调用了。
因为 Awake() 中 catch 了异常,按执行成功处理。
如果 Awake() 中不 catch, Awake() 执行异常,也不会有 Start() 调用,但是lua Instantiate() 执行会成功,打印出对象名。

## Check Error

1. "Check Error"
1. c# GetFunction("TestRay").PCall()
1. lua TestRay() return Vector3.zero
1. c# CheckRay();  //返回值出错

```
LuaException: bad argument #2 (Ray expected, got Vector3)
LuaInterface.LuaDLL:luaL_argerror(IntPtr, Int32, String) (at Assets/ToLua/Core/LuaDLL.cs:692)
LuaInterface.LuaDLL:luaL_typerror(IntPtr, Int32, String, String) (at Assets/ToLua/Core/LuaDLL.cs:706)
LuaInterface.LuaStatePtr:LuaTypeError(Int32, String, String) (at Assets/ToLua/Core/LuaStatePtr.cs:534)
LuaInterface.LuaState:CheckRay(Int32) (at Assets/ToLua/Core/LuaState.cs:1505)
LuaInterface.LuaFunction:CheckRay() (at Assets/ToLua/Core/LuaFunction.cs:781)
TestLuaStack:OnGUI() (at Assets/ToLua/Examples/TestErrorStack/TestLuaStack.cs:403)
```

## Push Error

1. "Push Error"
1. c# func.Push(Instance);

```
14:04:31.371-157: Type TestLuaStack not wrap to lua, push as UnityEngine.MonoBehaviour, the warning is only raised once
UnityEngine.Debug:LogWarning(Object)
LuaInterface.Debugger:LogWarning(String)
LuaInterface.Debugger:LogWarning(String, Object, Object)
LuaInterface.LuaState:GetMissMetaReference(Type) (at Assets/ToLua/Core/LuaState.cs:1917)
LuaInterface.LuaStatic:GetMissMetaReference(IntPtr, Type) (at Assets/ToLua/Core/LuaStatic.cs:39)
LuaInterface.ToLua:LoadPreType(IntPtr, Type) (at Assets/ToLua/Core/ToLua.cs:2608)
LuaInterface.ToLua:PushUserObject(IntPtr, Object) (at Assets/ToLua/Core/ToLua.cs:2622)
LuaInterface.ToLua:Push(IntPtr, Object) (at Assets/ToLua/Core/ToLua.cs:2636)
LuaInterface.LuaState:Push(Object) (at Assets/ToLua/Core/LuaState.cs:1378)
LuaInterface.LuaFunction:Push(Object) (at Assets/ToLua/Core/LuaFunction.cs:465)
TestLuaStack:OnGUI() (at Assets/ToLua/Examples/TestErrorStack/TestLuaStack.cs:412)
```

## LuaPushError

1. "LuaPushError"
1. c# GetFunction("PushLuaError").PCall()
1. lua PushLuaError()
1. lua TestStack.PushLuaError()
1. c# PushLuaError()
1. c# try { testRay.Push(Instance); }

仅是警告,没有异常

## Check Error

1. "Check Error"
1. c# GetFunction("Test5").PCall()
1. lua Test5()
1. lua TestStack.Test5()
1. c# Test5()
1. c# GetFunction("Test4").PCall()
1. lua TestStack.Test4()
1. c# Test4()
1. c# try { show.PCall() }

```
LuaException: TestErrorStack:2: this is error
stack traceback:
    [C]: in function 'error'
    TestErrorStack:2: in function <TestErrorStack:1>
    [C]: in function 'Test4'
    TestErrorStack:30: in function <TestErrorStack:29>
    [C]: in function 'Test5'
    TestErrorStack:34: in function <TestErrorStack:33>
LuaInterface.LuaState:PCall(Int32, Int32) (at Assets/ToLua/Core/LuaState.cs:758)
LuaInterface.LuaFunction:PCall() (at Assets/ToLua/Core/LuaFunction.cs:96)
TestLuaStack:Test4(IntPtr) (at Assets/ToLua/Examples/TestErrorStack/TestLuaStack.cs:85)
LuaInterface.LuaDLL:lua_pcall(IntPtr, Int32, Int32, Int32)
LuaInterface.LuaState:PCall(Int32, Int32) (at Assets/ToLua/Core/LuaState.cs:755)
LuaInterface.LuaFunction:PCall() (at Assets/ToLua/Core/LuaFunction.cs:96)
TestLuaStack:Test5(IntPtr) (at Assets/ToLua/Examples/TestErrorStack/TestLuaStack.cs:102)
LuaInterface.LuaDLL:lua_pcall(IntPtr, Int32, Int32, Int32)
LuaInterface.LuaState:PCall(Int32, Int32) (at Assets/ToLua/Core/LuaState.cs:755)
LuaInterface.LuaFunction:PCall() (at Assets/ToLua/Core/LuaFunction.cs:96)
TestLuaStack:OnGUI() (at Assets/ToLua/Examples/TestErrorStack/TestLuaStack.cs:434)
```

## Test Resume

1. "Test Resume"
1. c# GetFunction("Test6").PCall()
1. lua Test6()
1. lua 协程中调用 TestStack.Test6(go)
1. c# toluaL_exception()

lua coroutine resume() 返回 false, 不会有错误。

## out of bound

1. "out of bound"
1. c# GetFunction("TestOutOfBound").PCall()
1. c# TestOutOfBound()
1. c# toluaL_exception(L, e)

```
LuaException: Transform child out of bounds
stack traceback:
    [C]: at 0x613c2af0
TestLuaStack:TestOutOfBound(IntPtr) (at Assets/ToLua/Examples/TestErrorStack/TestLuaStack.cs:136)
LuaInterface.LuaDLL:lua_pcall(IntPtr, Int32, Int32, Int32)
LuaInterface.LuaState:PCall(Int32, Int32) (at Assets/ToLua/Core/LuaState.cs:755)
LuaInterface.LuaFunction:PCall() (at Assets/ToLua/Core/LuaFunction.cs:96)
TestLuaStack:OnGUI() (at Assets/ToLua/Examples/TestErrorStack/TestLuaStack.cs:454)
```

## TestArgError

1. "TestArgError"
1. c# GetFunction("Test8").PCall()
1. lua Test8()
1. lua TestArgError()
1. c# TestArgError()
1. c# toluaL_exception(L, e)

```
LuaException: TestErrorStack:69: bad argument #1 to 'TestArgError' (number expected, got string)
stack traceback:
    [C]: in function 'TestArgError'
    TestErrorStack:69: in function <TestErrorStack:68>
LuaInterface.LuaDLL:luaL_argerror(IntPtr, Int32, String) (at Assets/ToLua/Core/LuaDLL.cs:692)
LuaInterface.LuaDLL:luaL_typerror(IntPtr, Int32, String, String) (at Assets/ToLua/Core/LuaDLL.cs:706)
TestLuaStack:TestArgError(IntPtr) (at Assets/ToLua/Examples/TestErrorStack/TestLuaStack.cs:151)
LuaInterface.LuaDLL:lua_pcall(IntPtr, Int32, Int32, Int32)
LuaInterface.LuaState:PCall(Int32, Int32) (at Assets/ToLua/Core/LuaState.cs:755)
LuaInterface.LuaFunction:PCall() (at Assets/ToLua/Core/LuaFunction.cs:96)
TestLuaStack:OnGUI() (at Assets/ToLua/Examples/TestErrorStack/TestLuaStack.cs:463)
```

## TestFuncDispose

1. "TestFuncDispose"
1. c# func.Dispose();
1. c# func.PCall();

```
LuaException: LuaFunction has been disposed
LuaInterface.LuaFunction:BeginPCall() (at Assets/ToLua/Core/LuaFunction.cs:73)
TestLuaStack:OnGUI() (at Assets/ToLua/Examples/TestErrorStack/TestLuaStack.cs:473)
```

## SendMessage

1. "SendMessage"
1. c# gameObject.SendMessage("OnSendMsg");
1. c# OnSendMsg()
1. c# try { GetFunction("TestStack.Test6").PCall() }
1. c# Test6()
1. c# toluaL_exception(L, e);

```
LuaException: this a lua exception
stack traceback:
    [C]: at 0x613c2af0
    [C]: in function 'TestArgError'
    TestErrorStack:69: in function <TestErrorStack:68>
TestLuaStack:Test6(IntPtr) (at Assets/ToLua/Examples/TestErrorStack/TestLuaStack.cs:122)
LuaInterface.LuaDLL:lua_pcall(IntPtr, Int32, Int32, Int32)
LuaInterface.LuaState:PCall(Int32, Int32) (at Assets/ToLua/Core/LuaState.cs:755)
LuaInterface.LuaFunction:PCall() (at Assets/ToLua/Core/LuaFunction.cs:96)
TestLuaStack:OnSendMsg() (at Assets/ToLua/Examples/TestErrorStack/TestLuaStack.cs:277)
UnityEngine.GameObject:SendMessage(String)
TestLuaStack:OnGUI() (at Assets/ToLua/Examples/TestErrorStack/TestLuaStack.cs:481)
```

SendMessage() 等效于直接调用?

## SendMessageInLua

1. "SendMessageInLua"
1. c# GetFunction("SendMsgError").PCall()
1. lua SendMsgError(go)
1. lua go:SendMessage("OnSendMsg");

```
LuaException: this a lua exception
stack traceback:
    [C]: at 0x613c2af0
    [C]: in function 'SendMessage'
    TestErrorStack:38: in function <TestErrorStack:37>
    [C]: in function 'TestArgError'
    TestErrorStack:69: in function <TestErrorStack:68>
TestLuaStack:Test6(IntPtr) (at Assets/ToLua/Examples/TestErrorStack/TestLuaStack.cs:122)
LuaInterface.LuaDLL:lua_pcall(IntPtr, Int32, Int32, Int32)
LuaInterface.LuaState:PCall(Int32, Int32) (at Assets/ToLua/Core/LuaState.cs:755)
LuaInterface.LuaFunction:PCall() (at Assets/ToLua/Core/LuaFunction.cs:96)
TestLuaStack:OnSendMsg() (at Assets/ToLua/Examples/TestErrorStack/TestLuaStack.cs:277)
UnityEngine.GameObject:SendMessage(String)
UnityEngine_GameObjectWrap:SendMessage(IntPtr) (at Assets/Source/Generate/UnityEngine_GameObjectWrap.cs:657)
LuaInterface.LuaDLL:lua_pcall(IntPtr, Int32, Int32, Int32)
LuaInterface.LuaState:PCall(Int32, Int32) (at Assets/ToLua/Core/LuaState.cs:755)
LuaInterface.LuaFunction:PCall() (at Assets/ToLua/Core/LuaFunction.cs:96)
TestLuaStack:OnGUI() (at Assets/ToLua/Examples/TestErrorStack/TestLuaStack.cs:488)
```

和 "SendMessage" 差不多一样。

## AddComponent

1. "AddComponent"
1. c# GetFunction("TestAddComponent").PCall()
1. c# TestAddComponent()
1. c# try { go.AddComponent<TestInstantiate2>(); }
1. c# TestInstantiate2.Awake()
1. c# state.ThrowLuaException(e);

```
Exception: Instantiate exception 2
LuaInterface.LuaStatePtr.ThrowLuaException (System.Exception e) (at Assets/ToLua/Core/LuaStatePtr.cs:607)
TestInstantiate2.Awake () (at Assets/ToLua/Examples/TestErrorStack/TestInstantiate2.cs:16)
UnityEngine.GameObject:AddComponent()
TestLuaStack:TestAddComponent(IntPtr) (at Assets/ToLua/Examples/TestErrorStack/TestLuaStack.cs:237)
LuaInterface.LuaDLL:lua_pcall(IntPtr, Int32, Int32, Int32)
LuaInterface.LuaState:PCall(Int32, Int32) (at Assets/ToLua/Core/LuaState.cs:755)
LuaInterface.LuaFunction:PCall() (at Assets/ToLua/Core/LuaFunction.cs:96)
TestLuaStack:OnGUI() (at Assets/ToLua/Examples/TestErrorStack/TestLuaStack.cs:498)
```

## TableGetSet

1. "TableGetSet"

## TestTableInCo

1. "TestTableInCo"
1. c# GetFunction("TestCoTable").PCall()
1. lua TestCoTable()
1. lua 运行协程 TestCo(...)
1. lua TestTableInCo(...)
1. c# TestTableInCo()

## Instantiate2 Error

1. "Instantiate2 Error"
1. c# GetFunction("Instantiate").PCall() with go2
1. lua Instantiate()
1. lua UnityEngine.Object.Instantiate(obj)
1. c# TestInstantiate2:Awake()

```
LuaException: Instantiate exception 2
stack traceback:
    [C]: in function 'Instantiate'
    TestErrorStack:13: in function <TestErrorStack:11>
    [C]: in function 'TestArgError'
    TestErrorStack:69: in function <TestErrorStack:68>
TestInstantiate2:Awake() (at Assets/ToLua/Examples/TestErrorStack/TestInstantiate2.cs:11)
UnityEngine.Object:Instantiate(Object)
UnityEngine_ObjectWrap:Instantiate(IntPtr) (at Assets/ToLua/BaseType/UnityEngine_ObjectWrap.cs:203)
LuaInterface.LuaDLL:lua_pcall(IntPtr, Int32, Int32, Int32)
LuaInterface.LuaState:PCall(Int32, Int32) (at Assets/ToLua/Core/LuaState.cs:755)
LuaInterface.LuaFunction:PCall() (at Assets/ToLua/Core/LuaFunction.cs:96)
TestLuaStack:OnGUI() (at Assets/ToLua/Examples/TestErrorStack/TestLuaStack.cs:573)
```

## Instantiate3 Error

1. "Instantiate3 Error"
1. c# UnityEngine.Object.Instantiate(go2);
1. c# TestInstantiate2.Awake()

```
Exception: Instantiate exception 2
LuaInterface.LuaStatePtr.ThrowLuaException (System.Exception e) (at Assets/ToLua/Core/LuaStatePtr.cs:607)
TestInstantiate2.Awake () (at Assets/ToLua/Examples/TestErrorStack/TestInstantiate2.cs:16)
UnityEngine.Object:Instantiate(GameObject)
TestLuaStack:OnGUI() (at Assets/ToLua/Examples/TestErrorStack/TestLuaStack.cs:580)
```

## TestCycle

1. "TestCycle"
1. c# GetFunction("TestCycle").PCall()
1. c# TestCycle()

测试递归调用 luaFunction

## TestNull

1. "TestNull"
1. c# StartCoroutine(TestCo(action));

c# 中创建协程

## TestMulti

1. "TestMulti"
1. c# GetFunction("TestMulStack").PCall()
1. c# TestMulStack()
1. c# try { TestMul0(); }
1. c# TestMul1()
1. throw
posted on 2020-09-03 16:29 金庆 阅读(692) 评论(0)  编辑 收藏 引用 所属分类: 2. 网游开发

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理