lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hello,
I have compiled luaffi module from https://github.com/jmckaskill/luaffi
and when I try to run example from http://luajit.org/ext_ffi_tutorial.html
I get error on this line:

    local buflen = ffi.new("unsigned long[1]", n)        -- this is line 14

~/tmp/progr/source/LUA/git/luaffi_my $ lua zlibtest.lua
Uncompressed size:      4000
lua: unable to convert argument 2 from lua<number> to cdata<unsigned int[1]>
stack traceback:
        [C]: in function 'new'
        zlibtest.lua:14: in function 'compress'
        zlibtest.lua:31: in main chunk
        [C]: ?

Does this example work for you?
When I run provided test.lua from distribution it is OK.

~/tmp/progr/source/LUA/git/luaffi_my $ LD_LIBRARY_PATH=./ lua test.lua
Running test
Test PASSED

Any idea what maybe wrong and how to fix it?
I am using Lua 5.1.4 on unix (Debian) system.

Martin

Bellow is included script just for reference
=== zlibtest.lua ====
local ffi = require("ffi")
ffi.cdef[[
 unsigned long compressBound(unsigned long sourceLen);
 int compress2(uint8_t *dest, unsigned long *destLen,
               const uint8_t *source, unsigned long sourceLen, int level);
 int uncompress(uint8_t *dest, unsigned long *destLen,
                const uint8_t *source, unsigned long sourceLen);
]]
local zlib = ffi.load(ffi.os == "Windows" and "zlib1" or "z")

local function compress(txt)
    local n = zlib.compressBound(#txt)
    local buf = ffi.new("uint8_t[?]", n)
    local buflen = ffi.new("unsigned long[1]", n)        -- this is line 14
    local res = zlib.compress2(buf, buflen, txt, #txt, 9)
    assert(res == 0)
    return ffi.string(buf, buflen[0])
end

local function uncompress(comp, n)
    local buf = ffi.new("uint8_t[?]", n)
    local buflen = ffi.new("unsigned long[1]", n)
    local res = zlib.uncompress(buf, buflen, comp, #comp)
    assert(res == 0)
    return ffi.string(buf, buflen[0])
end

-- Simple test code.
local txt = string.rep("abcd", 1000)
print("Uncompressed size: ", #txt)
local c = compress(txt)
print("Compressed size: ", #c)
local txt2 = uncompress(c, #txt)
assert(txt2 == txt)
=== end of zlibtest.lua ===