diff --git a/changelog.d/20260713_143000_st_stfn_ffi.md b/changelog.d/20260713_143000_st_stfn_ffi.md new file mode 100644 index 0000000..4f67b2a --- /dev/null +++ b/changelog.d/20260713_143000_st_stfn_ffi.md @@ -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`). diff --git a/src/Data/Array/ST.lua b/src/Data/Array/ST.lua index e67655f..c92ddf3 100644 --- a/src/Data/Array/ST.lua +++ b/src/Data/Array/ST.lua @@ -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) @@ -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 @@ -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 diff --git a/test/regression/array_st.lua b/test/regression/array_st.lua index 6b1ddf9..0172e65 100644 --- a/test/regression/array_st.lua +++ b/test/regression/array_st.lua @@ -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. @@ -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