lua-users home
lua-l archive

Re: Exit one Lua state from a (C) API function

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Thu, Feb 25, 2021 at 9:58 PM bel wrote:
> It cannot be just a luaL_error call, since this will be caught by a pcall in the Lua code.
Could a pcall wrapper be a good solution for you? Something like (not tested):
```
local propagate = {}
local oldpcall = pcall
pcall = function(f)
 local res = table.pack(oldpcall(f)) -- [1]
 -- If the "Propagate" request is found, it will forward the error to
the upper level
 if not res[1] and res[2] == propagate then
 error(res[2])
 end
 -- Otherwise, it behaves like the standard pcall
 return table.unpack(res)
end
```
then, a exit_to_c function can be defined as
```
local function exit_to_c()
 error(propagate)
end
```
(probably you need to wrap xpcall too)
Mimmo Mane
[1] Pack/unpack can be avoided with something like
local function result_check(f, ...)
 return (function(a, ...)
 if a == 'default' then
 return 'default', 1, 2, 3
 end
 return a, ...
 end)(f(...))
end
print(result_check(function() return 'foo','bar' end))
print(result_check(function() return 'default','bar' end))

AltStyle によって変換されたページ (->オリジナル) /