aarzilli / golua

Go bindings for Lua C API - in progress
MIT License
645 stars 161 forks source link

Why use unsafe_pcall/unsafe_xpcall instead of pcall/xpcall #95

Closed lx-world closed 3 years ago

lx-world commented 3 years ago

Why use unsafe_pcall/unsafe_xpcall instead of pcall/xpcall

lx-world commented 3 years ago

They are only safe to be called from Lua code that never calls back to Go , why?

aarzilli commented 3 years ago

Because:

Lua's exceptions are incompatible with Go, golua works around this incompatibility by setting up protected execution environments in lua.State.DoString, lua.State.DoFile and lua.State.Call and turning every exception into a Go panic.

lx-world commented 3 years ago

Because:

Lua's exceptions are incompatible with Go, golua works around this incompatibility by setting up protected execution environments in lua.State.DoString, lua.State.DoFile and lua.State.Call and turning every exception into a Go panic.

Thank you very much

lx-world commented 3 years ago

A third-party library is called in Lua, such as protoc.lua. If pcall is used in this library, golua will report an error and cannot find pcall. How to solve this?

And , How to understand They are only safe to be called from Lua code that never calls back to Go?

lx-world commented 3 years ago

I use like this, example: pcall=unsafe_pcall

Will there be any problems or risks in such use?

aarzilli commented 3 years ago

Will there be any problems or risks in such use?

No.

lx-world commented 3 years ago

Does that mean that I cannot call any third-party libraries with pcall and xpcall?

And, If the third-party library used uses pcall/xpcall, the program will not start. Is there a solution for this?

aarzilli commented 3 years ago

Does that mean that I cannot call any third-party libraries with pcall and xpcall?

You can but only if there is never any Go frame on the stack between the point where an error is raised and the point where pcall is used.

If the third-party library used uses pcall/xpcall, the program will not start. Is there a solution for this?

Comment out the code that hides pcall, xpcall?

lx-world commented 3 years ago

Does that mean that I cannot call any third-party libraries with pcall and xpcall?

You can but only if there is never any Go frame on the stack between the point where an error is raised and the point where pcall is used.

If the third-party library used uses pcall/xpcall, the program will not start. Is there a solution for this?

Comment out the code that hides pcall, xpcall?

Thank you for your answer. Is there any risk in commenting out pcall/xpcall?

aarzilli commented 3 years ago

Is there any risk in commenting out pcall/xpcall?

Do you mean commenting out the code that hides pcall and xpcall? Yes, all the lua code will have to be audited to make sure that it doesn't use errors + pcall/xpcall in a way that can crash the go runtime.