Skip to content

Commit

Permalink
fix(*) openresty coroutines
Browse files Browse the repository at this point in the history
wherever coroutines are used, we now check on openresty, and if
using openresty, we fallback on the original Lua/LuaJIT coroutine
functions.

Fixes #265
  • Loading branch information
Tieske committed Jul 31, 2020
1 parent fa6f4e1 commit 120021c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
7 changes: 7 additions & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ unused_args = false
redefined = false
max_line_length = false

globals = {
"ngx",
"coroutine._wrap",
"coroutine._yield",
"coroutine._create",
"coroutine._resume",
}

not_globals = {
"string.len",
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
[#314](https://github.com/Tieske/Penlight/pull/314)
- Fix: `package.searchpath` (in module `pl.compat`)
[#328](https://github.com/Tieske/Penlight/pull/328)
- Fix: OpenResty coroutines, used by `dir.dirtree`, `pl.lexer`, `pl.permute`. If
available the original coroutine functions are now used [#329](https://github.com/Tieske/Penlight/pull/329)

## 1.7.0 (2019-10-14)

Expand Down
7 changes: 5 additions & 2 deletions lua/pl/dir.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ local sub = string.sub
local os,pcall,ipairs,pairs,require,setmetatable = os,pcall,ipairs,pairs,require,setmetatable
local remove = os.remove
local append = table.insert
local wrap = coroutine.wrap
local yield = coroutine.yield
local assert_arg,assert_string,raise = utils.assert_arg,utils.assert_string,utils.raise

-- check on OpenResty coroutine versions, and use originals if possible
local wrap = coroutine._wrap or coroutine.wrap
local yield = coroutine._yield or coroutine.yield


local dir = {}

local function makelist(l)
Expand Down
6 changes: 5 additions & 1 deletion lua/pl/lexer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@
-- See the Guide for further @{06-data.md.Lexical_Scanning|discussion}
-- @module pl.lexer

local yield,wrap = coroutine.yield,coroutine.wrap
local strfind = string.find
local strsub = string.sub
local append = table.insert

-- check on OpenResty coroutine versions, and use originals if possible
local wrap = coroutine._wrap or coroutine.wrap
local yield = coroutine._yield or coroutine.yield


local function assert_arg(idx,val,tp)
if type(val) ~= tp then
error("argument "..idx.." must be "..tp, 2)
Expand Down
11 changes: 7 additions & 4 deletions lua/pl/permute.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ local tablex = require 'pl.tablex'
local utils = require 'pl.utils'
local copy = tablex.deepcopy
local append = table.insert
local coroutine = coroutine
local resume = coroutine.resume
local assert_arg = utils.assert_arg

-- check on OpenResty coroutine versions, and use originals if possible
local co_create = coroutine._create or coroutine.create
local co_yield = coroutine._yield or coroutine.yield
local co_resume = coroutine._resume or coroutine.resume


local permute = {}

Expand Down Expand Up @@ -41,9 +44,9 @@ end
function permute.iter (a)
assert_arg(1,a,'table')
local n = #a
local co = coroutine.create(function () permgen(a, n, coroutine.yield) end)
local co = co_create(function () permgen(a, n, co_yield) end)
return function () -- iterator
local _, res = resume(co)
local _, res = co_resume(co)
return res
end
end
Expand Down

0 comments on commit 120021c

Please sign in to comment.