NLua / NLua

Bridge between Lua and the .NET.
http://nlua.org
MIT License
2.05k stars 309 forks source link

Calling lua function error() #381

Open z3bu opened 4 years ago

z3bu commented 4 years ago

Hello,

I have been trying to trigger an error in c# functions I registered to NLua. (The plan is to handle exceptions with pcall). So far I tried to do it in two ways:

NLua.Lua lua = new NLua.Lua lua();
lua.DoString("error('test')");

gives

NLua.Exceptions.LuaScriptException
  HResult=0x80131500
  Message=[string "chunk"]:1: hi
  Source=
  StackTrace:
   at NLua.Lua.ThrowExceptionFromError(Int32 oldTop)
   at NLua.Lua.DoString(String chunk, String chunkName)
   at NSCT.Scripting.LuaExecutor.Execute(Object args) in C:\Users\3311mavi\source\NSCT\NSCT\Scripting\LuaExecutor.cs:line 677
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart(Object obj)

And

NLua.Lua lua = new NLua.Lua lua();
LuaFunction f = lua.GeFunction("error");
f.Call("42");

gives

NLua.Exceptions.LuaScriptException
  HResult=0x80131500
  Message=[string "chunk"]:1: hi
  Source=
  StackTrace:
   at NLua.Lua.ThrowExceptionFromError(Int32 oldTop)
   at NLua.Lua.DoString(String chunk, String chunkName)
   at NSCT.Scripting.LuaExecutor.Execute(Object args) in C:\Users\3311mavi\source\NSCT\NSCT\Scripting\LuaExecutor.cs:line 677
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart(Object obj)

Is this expected? Or is there a better way to trigger errors?

Using Nlua 1.4.32

z3bu commented 4 years ago

I ended up throwing LuaException in my registered function and check err.InnerException in the lua script.

I would like to reproduce this behaviour:

local status, err = pcall(function () error({code=121}) end) print(err.code)

Is there any possibilities?