pkulchenko / wxlua

wxlua: Lua bindings for wxWidgets cross-platform GUI toolkit; supports Lua 5.1, 5.2, 5.3, 5.4, LuaJIT and wxWidgets 3.x
306 stars 59 forks source link

wxDir:Exists and wxDir:GetAllFiles compile but fail at runtime #66

Closed decuant closed 4 years ago

decuant commented 4 years ago

Hello,

I'm using wxDir and I'm getting this strange error with Exists and GetAllFiles.

C:\Program Files (x86)\ZeroBrane\bin\lua53.exe: C:\USR_2\LUA\WMOQuery\compile.lua:65: wxLua: Expected a 'string' or 'wxString' for parameter 1, but got a 'userdata'.

Function called: 'GetAllFiles(wxDir, string, number)'
01. wxDir::GetAllFiles(string [, string, number]) - static
stack traceback:
    [C]: in method 'GetAllFiles'

I reverted my source code to use Open instead of Exists and GetFirst/GetNext instead of GetAllFiles, but wanted to point the issue out.

Below is code failing (actually lines commented out), if can be of help:

local function ProcessDirectory(inPathname)
    trace.newLine("ProcessDirectory [" .. inPathname .. "]")

    local dir = wx.wxDir()

    if not dir:Open(inPathname) then

        trace.line("Cannot open directory [" .. inPathname .. "]")
        return
    end

--  local _, tFilenames = dir:GetAllFiles("*.json", wx.wxDIR_FILES) 

--  if tFilenames then

--      for _, sFilename in ipairs(tFilenames) do

--          ProcessFile(sFilename)
--      end
--  end

--  local _, tDirectories = dir:GetAllFiles("*.*", wx.wxDIR_DIRS)

--  if tDirectories then

--      for _, sDirectory in ipairs(tDirectories) do

--          ProcessDirectory(sDirectory)
--      end
--  end

    local _, sFilename = dir:GetFirst("*.json", wx.wxDIR_FILES)

    while sFilename and 0 < #sFilename do

        ProcessFile(inPathname .. "\\" .. sFilename)

        _, sFilename = dir:GetNext()
    end

    local _, sDirectory = dir:GetFirst("*", wx.wxDIR_DIRS)

    while sDirectory and 0 < #sDirectory do

        ProcessDirectory(inPathname .. "\\" .. sDirectory)

        _, sDirectory = dir:GetNext()
    end

    dir:Close()
end

Thank you

Antonio

pkulchenko commented 4 years ago

@decuant, both Exists and GetAllFiles are static functions, so you'd need to call them as wxDir.Exists() or dir.Exists() (assuming dir = wx.wxDir()). Notice . instead of :, as the static functions are not methods, so they don't take the object as the first parameter.

Also, for GetAllFiles, it expects the path, the filespec, and the flags, so in your case the call should be dir.GetAllFiles(inPathname, "*.json", wx.wxDIR_FILES). The directory referenced by dir doesn't need to be open for these methods to work, as they take the directory name as the parameter.

  1. wxDir::GetAllFiles(string [, string, number]) - static

Notice that it references the fact that the function is static in the description, although it's easy to miss.

I'm closing this, but let me know if this still doesn't work for you.