@@ -188,10 +188,29 @@ local maybe_quote do
188188 end
189189end
190190
191- -- A pcall relative that can be yielded over in PUC 5.1
191+ -- A pcall-alike function that can be yielded over even in PUC 5.1
192192local yieldable_pcall
193- -- See if pcall can be yielded over
194- if coroutine.wrap (function () return pcall (coroutine.yield , true ) end )() then
193+ --[[ If pcall can already yield, then we want to use that.
194+
195+ However, we can't do the feature check straight away, Openresty breaks
196+ coroutine.wrap in some contexts. See #98
197+ Openresty nominally only supports LuaJIT, which always supports a yieldable
198+ pcall, so we short-circuit the feature check by checking if the 'ngx' library
199+ is loaded, plus that jit.version_num indicates LuaJIT 2.0.
200+ This combination ensures that we don't take the wrong branch if:
201+ - lua-http is being used to mock the openresty environment
202+ - openresty is compiled with something other than LuaJIT
203+ ]]
204+ if (
205+ package.loaded .ngx
206+ and type (package.loaded .jit ) == " table"
207+ and type (package.loaded .jit .version_num ) == " number"
208+ and package.loaded .jit .version_num >= 20000
209+ )
210+ -- See if pcall can be yielded over
211+ or coroutine.wrap (function ()
212+ return pcall (coroutine.yield , true ) end
213+ )() then
195214 yieldable_pcall = pcall
196215else
197216 local function handle_resume (co , ok , ...)
0 commit comments