Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions changelog.d/20260713_143000_st_stfn_ffi.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### Fixed

- `Data.Array.ST` FFI matches its `STFn` declarations again: the Lua module
exports `thawImpl`/`freezeImpl`/`toAssocArrayImpl` (previously only the
pre-`STFn` names `thaw`/`freeze`/`toAssocArray` existed, leaving the
declared foreigns unbound, so `thaw`, `freeze`, `withArray` and
`toAssocArray` crashed with a nil call), and `peekImpl` returns its result
directly instead of an inner thunk (previously `peek` produced a function
instead of the `Maybe`).
26 changes: 11 additions & 15 deletions src/Data/Array/ST.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@ local function move(a1, f, e, t, a2)
return a2
end

local function copyImpl(xs) return function() return move(xs, 1, #xs, 1, {}) end end
local function copyImpl(xs) return move(xs, 1, #xs, 1, {}) end

return {
new = (function() return {} end),
peekImpl = (function(just, nothing, i, xs)
return function()
if i >= 0 and i < #xs then
return just(xs[i + 1])
else
return nothing
end
if i >= 0 and i < #xs then
return just(xs[i + 1])
else
return nothing
end
end),
pokeImpl = (function(i, a, xs)
Expand Down Expand Up @@ -73,8 +71,8 @@ return {
end),
unsafeFreezeImpl = (function(xs) return xs end),
unsafeThawImpl = (function(xs) return xs end),
freeze = (copyImpl),
thaw = (copyImpl),
freezeImpl = (copyImpl),
thawImpl = (copyImpl),
sortByImpl = ((function()
local function rshift(x, by) return math.floor(x / 2 ^ by) end

Expand Down Expand Up @@ -119,12 +117,10 @@ return {
return xs
end
end)()),
toAssocArray = (function(xs)
return function()
local r = {}
for i = 1, #xs do r[i] = {index = i - 1, value = xs[i]} end
return r
end
toAssocArrayImpl = (function(xs)
local r = {}
for i = 1, #xs do r[i] = {index = i - 1, value = xs[i]} end
return r
end),
pushImpl = (function(a, xs)
xs[#xs + 1] = a
Expand Down
42 changes: 35 additions & 7 deletions test/regression/array_st.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,47 @@ local function check(name, cond, detail)
end
end

local function just(x) return {tag = "just", value = x} end
local nothing = {tag = "nothing"}

--------------------------------------------------------------------------------
-- Data/Array/ST.lua: move-based operations -----------------------------------

-- freeze (= copyImpl, the move-to-fresh-table path) is an independent copy.
-- The ST foreigns follow the STFn convention: an STFnN entry is an N-ary
-- function that performs the effect when called and returns the result
-- directly — no inner thunk, and the export carries the `*Impl` name the
-- PureScript side declares.

-- freezeImpl / thawImpl (the move-to-fresh-table path) are independent copies.
do
local xs = {1, 2, 3}
local copy = ST.freeze(xs)()
checkArray("freeze copies", copy, {1, 2, 3})
local copy = ST.freezeImpl(xs)
checkArray("freezeImpl copies", copy, {1, 2, 3})
copy[1] = 99
check("freeze is independent", xs[1] == 1, "source mutated to " .. tostring(xs[1]))
check("freezeImpl is independent", xs[1] == 1, "source mutated to " .. tostring(xs[1]))
end

do
local xs = {1, 2, 3}
local copy = ST.thawImpl(xs)
checkArray("thawImpl copies", copy, {1, 2, 3})
xs[1] = 99
check("thawImpl is independent", copy[1] == 1, "copy mutated to " .. tostring(copy[1]))
end

-- toAssocArrayImpl builds zero-based {index, value} records.
do
local r = ST.toAssocArrayImpl({"a", "b"})
check("toAssocArrayImpl builds records",
type(r) == "table" and #r == 2 and r[1].index == 0 and r[1].value == "a" and r[2].index == 1 and r[2].value == "b",
"got " .. tostring(r))
end

-- peekImpl returns the Maybe itself, not a thunk producing it.
do
local r = ST.peekImpl(just, nothing, 1, {10, 20, 30})
check("peekImpl returns the Maybe directly", type(r) == "table" and r.tag == "just" and r.value == 20, "got " .. type(r))
check("peekImpl out of range is nothing", ST.peekImpl(just, nothing, 9, {10}) == nothing, "expected nothing")
end

-- pushAllImpl appends and reports the new length.
Expand Down Expand Up @@ -156,9 +187,6 @@ end
--------------------------------------------------------------------------------
-- Data/Array.lua: unpack-based operations ------------------------------------

local function just(x) return {tag = "just", value = x} end
local nothing = {tag = "nothing"}

-- unconsImpl splits head / tail; the tail is `{ unpack(xs, 2) }`.
do
local function empty(_) return {tag = "empty"} end
Expand Down