mesecons icon indicating copy to clipboard operation
mesecons copied to clipboard

Add safe version of pcall and error function to luacontroller environment

Open rdococ opened this issue 5 years ago • 2 comments

Currently propagates all errors generated by the luacontroller code itself (e.g. string length overflow), which might not be the desired behavior, but can be fixed if necessary.

I'm not sure if anyone actually wants this feature, but it could be useful for certain forms of non-local control flow, and judging by the comment I saw by the safe_globals definition, it seems like there was some interest in it.

rdococ avatar May 02 '20 12:05 rdococ

I suggest you write safe_pcall something like this (I haven't tested the code):

local function finish_safe_pcall(...)
	local ok, err = ...

	if not ok and not debug.gethook() then
		error(err, 0)
	end

	return ...
end

local function safe_pcall(f, ...)
	return finish_safe_pcall(pcall(f, ...))
end

This handles nil return values correctly.

Calling error with 0 as its second argument throws the given message without modifying it.

TurkeyMcMac avatar Mar 09 '22 03:03 TurkeyMcMac

I support this! I would not like to manually check inputs for errors.

Bituvo avatar Nov 13 '23 23:11 Bituvo