Using the `lume` library is giving unexpected results

Hi there! First-time poster, medium-time lurker. Your decision to use Lua is making me take a serious look at switching from Tiddlywiki to Silverbullet v2 – as a heavy user of TW I’ve written some complicated queries and filters that would make much more sense in a programming language like Lua.

I’m very familiar with Lua and have used it a lot with Love2D. My favorite library to use is rxi/lume. It’s a kind of utility belt with a lot of handy functions.

I imported a small slice of the library because I wanted to use lume.concat to merge two arrays together. I should be able to do this:

local things = lume.concat({1, 2}, {3, 4})
print(things) -- {1, 2, 3, 4}

But instead what I’m getting is {{1, 2}, {3, 4}}.

Here’s my small slice of lume:

-- priority: 50
lume = lume or {}

local getiter = function(x)
  if lume.isarray(x) then
    return ipairs
  elseif type(x) == "table" then
    return pairs
  end
  error("expected table", 3)
end

function lume.isarray(x)
  return type(x) == "table" and x[1] ~= nil
end

function lume.concat(...)
  local rtn = {}
  for i = 1, select("#", ...) do
    local t = select(i, ...)
    if t ~= nil then
      local iter = getiter(t)
      for _, v in iter(t) do
        rtn[#rtn + 1] = v
      end
    end
  end
  return rtn
end

This feels like a bug but I wanted to check here before filing it. Anyone else come across something like this? Is it maybe the use of select("#", ...)? I did a quick scan of the silverbullet codebase and didn’t see any uses of select in a vararg context.

EDIT: To unblock myself I wrote a much simpler array merge function that only works on two arrays:

local function mergeArrays(one, two)
  local result = {}
  for i,v in ipairs(one) do
    result[#result + 1] = v
  end
  for i,v in ipairs(two) do
    result[#result + 1] = v
  end
  return result
end

No warranties for performance, sanity, etc, etc… :smile:

1 Like

There’s likely a subtle bug in our Lua implementation, created a Github issue to investigate: Using the `lume` library is giving unexpected results · Issue #1420 · silverbulletmd/silverbullet · GitHub

Should be fixed now, it was indeed a poor implementation of variadic argument handling: Fixes #1420 · silverbulletmd/silverbullet@19618b9 · GitHub

For those interested — this is a part that I had delegated for an AI coding agent to implement for me. It seemed to work, but when you dig into it, it generated quite a mess (as you can see in the commit). Cleaned this up now. No more AI in the SB code base from my end.

2 Likes