Skip to content

Commit 846c77a

Browse files
committed
fix more cdata type related bugs
1 parent 2c43758 commit 846c77a

File tree

7 files changed

+86
-41
lines changed

7 files changed

+86
-41
lines changed

examples/projects/luajit/src/main.nlua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ end
77

88
print(assert(fs.get_current_directory()))
99

10-
for k, v in pairs((assert(fs.get_attributes("nlconfig.lua")))) do
10+
for k, v in pairs((assert(fs.get_attributes("README.md")))) do
1111
print(k, v)
1212
end

examples/projects/luajit/src/platforms/unix/filesystem.nlua

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,15 @@ do
118118
local ret = follow_link and stat_link(path, buff) or stat(path, buff)
119119

120120
if ret == 0 then
121+
local s = assert(buff[0])
122+
local xxx = s.st_atime
121123
return {
122-
last_accessed = tonumber(buff[0].st_atime),
123-
last_changed = tonumber(buff[0].st_ctime),
124-
last_modified = tonumber(buff[0].st_mtime),
125-
creation_time = tonumber(buff[0].st_mtime),
126-
size = tonumber(buff[0].st_size),
127-
type = bit.band(buff[0].st_mode, DIRECTORY) ~= 0 and "directory" or "file",
124+
last_accessed = tonumber(s.st_atime),
125+
last_changed = tonumber(s.st_ctime),
126+
last_modified = tonumber(s.st_mtime),
127+
creation_time = tonumber(s.st_mtime),
128+
size = tonumber(s.st_size),
129+
type = bit.band(s.st_mode, DIRECTORY) ~= 0 and "directory" or "file",
128130
}
129131
end
130132

@@ -166,7 +168,7 @@ do
166168

167169
local dot = string.byte(".")
168170

169-
local function is_dots(ptr: {[number] = number}) --FFIArray<|3, number|>)
171+
local function is_dots(ptr: ffi.new("char*"))
170172
if ptr[0] == dot then
171173
if ptr[1] == dot and ptr[2] == 0 then return true end
172174

@@ -186,7 +188,6 @@ do
186188

187189
while true do
188190
local dir_info = ffi.C.readdir(ptr)
189-
dir_info = dir_info
190191

191192
if dir_info == nil then break end
192193

examples/projects/luajit/src/platforms/windows/filesystem.nlua

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ do
5050
struct
5151
)
5252

53-
local function POSIX_TIME(time: number)
53+
local function POSIX_TIME(time: number): number
5454
return tonumber(time / 10000000 - 11644473600)
5555
end
5656

@@ -73,18 +73,20 @@ do
7373
temporary = 0x100, -- A file that is being used for temporary storage. File systems avoid writing data back to mass storage if sufficient cache memory is available, because typically, an application deletes a temporary file after the handle is closed. In that scenario, the system can entirely avoid writing the data. Otherwise, the data is written after the handle is closed.
7474
virtual = 0x10000, -- This value is reserved for system use.
7575
}
76+
local box = ffi.typeof("$[1]", struct)
7677

7778
function fs.get_attributes(path, follow_link)
78-
local info = ffi.new("$[1]", struct)
79+
local info = box()
7980

8081
if ffi.C.GetFileAttributesExA(path, 0, info) ~= 0 then
82+
local s = assert(info[0])
8183
return {
82-
creation_time = POSIX_TIME(info[0].ftCreationTime),
83-
last_accessed = POSIX_TIME(info[0].ftLastAccessTime),
84-
last_modified = POSIX_TIME(info[0].ftLastWriteTime),
84+
creation_time = POSIX_TIME(s.ftCreationTime),
85+
last_accessed = POSIX_TIME(s.ftLastAccessTime),
86+
last_modified = POSIX_TIME(s.ftLastWriteTime),
8587
last_changed = -1, -- last permission changes
86-
size = tonumber(info[0].nFileSize),
87-
type = bit.band(info[0].dwFileAttributes, flags.directory) == flags.directory and
88+
size = assert(tonumber(s.nFileSize)),
89+
type = bit.band(s.dwFileAttributes, flags.directory) == flags.directory and
8890
"directory" or
8991
"file",
9092
}
@@ -122,7 +124,7 @@ do
122124
)
123125
local dot = string.byte(".")
124126

125-
local function is_dots(ptr: {[number] = number}) -- todo: maybe FFIArray<|12, number|> should be ok to pass when the argument contract is FFIArray<|3, number|> , because it's at least 3 in length
127+
local function is_dots(ptr: ffi.new("const char *")) -- todo: maybe FFIArray<|12, number|> should be ok to pass when the argument contract is FFIArray<|3, number|> , because it's at least 3 in length
126128
if ptr[0] == dot then
127129
if ptr[1] == dot and ptr[2] == 0 then return true end
128130

@@ -133,13 +135,14 @@ do
133135
end
134136

135137
local INVALID_FILE = ffi.cast("void *", 0xFFFFFFFFFFFFFFFFULL)
138+
local box = ffi.typeof("$[1]", struct)
136139

137140
function fs.get_files(path)
138141
if path == "" then path = "." end
139142

140143
if path:sub(-1) ~= "/" then path = path .. "/" end
141144

142-
local data = ffi.new("$[1]", struct)
145+
local data = box()
143146
local handle = ffi.C.FindFirstFileA(path .. "*", data)
144147

145148
if handle == nil then return nil, last_error() end
@@ -170,7 +173,7 @@ do
170173
]])
171174

172175
function fs.set_current_directory(path)
173-
if ffi.C.chdir(path) == 0 then return true end
176+
if ffi.C.SetCurrentDirectoryA(path) == 0 then return true end
174177

175178
return nil, last_error()
176179
end

nattlua/c_declarations/main.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ local function gen(parser, ...)
6262
error("expected ctype or value to fill $ or ? at argument #" .. i, 2)
6363
end
6464

65+
if ct.Type == "union" then
66+
local u = {}
67+
68+
for i, obj in ipairs(ct:GetData()) do
69+
u[i] = assert(obj:Get(LString("T")))
70+
end
71+
72+
ct = Union(u)
73+
end
74+
6575
if ct.Type == "table" then ct = assert(ct:Get(LString("T"))) end
6676

6777
table.insert(new, ct)

test/tests/nattlua/analyzer/complex/ljsocket.nlua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1400,7 +1400,7 @@ do
14001400
address_len: nil | number
14011401
)
14021402
size = size or 64000
1403-
local buff = ffi.new("char[?]", size) as any
1403+
local buff = ffi.new("char[?]", size as number)
14041404

14051405
if self.on_receive then return self:on_receive(buff, size, flags) end
14061406

test/tests/nattlua/c_declarations/typed_ffi.lua

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -524,24 +524,60 @@ analyze[=[
524524
end
525525
]=]
526526
analyze[=[
527-
ffi.cdef[[void FormatMessageA(void*);]]
528-
local a = ffi.C.FormatMessageA
529-
ffi.cdef[[int GetLastError();]]
530-
attest.equal(ffi.C.FormatMessageA, a)
527+
ffi.cdef[[void FormatMessageA(void*);]]
528+
local a = ffi.C.FormatMessageA
529+
ffi.cdef[[int GetLastError();]]
530+
attest.equal(ffi.C.FormatMessageA, a)
531531
]=]
532532
analyze[[
533-
local function foo(x: ffi.typeof_arg("void*")) end
534-
local y = _ as TCData<|{["s_addr"] = number}|>
535-
foo(y)
533+
local function foo(x: ffi.typeof_arg("void*")) end
534+
local y = _ as TCData<|{["s_addr"] = number}|>
535+
foo(y)
536536
]]
537537
analyze[=[
538-
ffi.cdef([[
539-
struct wwww {
540-
int ai_flags;
541-
int ai_family;
542-
};
543-
]])
544-
local res = ffi.new("struct wwww[1]", {{ai_flags = 0}})
545-
attest.equal(res[0].ai_flags, _ as 0)
546-
attest.equal(res[0].ai_family, _ as number)
538+
ffi.cdef([[
539+
struct wwww {
540+
int ai_flags;
541+
int ai_family;
542+
};
543+
]])
544+
local res = ffi.new("struct wwww[1]", {{ai_flags = 0}})
545+
attest.equal(res[0].ai_flags, _ as 0)
546+
attest.equal(res[0].ai_family, _ as number)
547+
]=]
548+
analyze[=[
549+
local stat_struct
550+
local OSX = ffi.os == "OSX"
551+
local X64 = ffi.arch == "x64"
552+
553+
if OSX then
554+
stat_struct = ffi.typeof([[
555+
struct {
556+
uint32_t st_dev;
557+
uint16_t st_mode;
558+
}
559+
]])
560+
else
561+
if X64 then
562+
stat_struct = ffi.typeof([[
563+
struct {
564+
uint64_t st_dev;
565+
uint64_t st_ino;
566+
}
567+
]])
568+
else
569+
stat_struct = ffi.typeof([[
570+
struct {
571+
uint64_t st_dev;
572+
uint32_t __st_ino;
573+
}
574+
]])
575+
end
576+
end
577+
578+
local statbox = ffi.typeof("$[1]", stat_struct)
579+
local buff = statbox()
580+
local s = assert(buff[0])
581+
attest.equal(s.st_dev, _ as number)
582+
547583
]=]

test/tests/nattlua/project.lua

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
--PENDING_TEST
2-
do
3-
return
4-
end
5-
61
local nl = require("nattlua")
72
local builder = assert(
83
nl.File(

0 commit comments

Comments
 (0)