Skip to content

Commit 4175757

Browse files
committed
fix more issues with cdata types
1 parent 846c77a commit 4175757

File tree

2 files changed

+19
-25
lines changed

2 files changed

+19
-25
lines changed

nattlua/definitions/lua/luajit.nlua

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ function TCData<|T: ConstructorArgument, ...: ...any|>
5656
return {
5757
T = T,
5858
@MetaTable = self,
59+
@TypeOverride = "cdata",
5960
__is_cdata_number = type(T) == "number",
6061
__tostring = function(self: self)
6162
return "cdata<" .. tostring(T) .. (is_pointer and " *" or "") .. ">"
@@ -84,18 +85,21 @@ function TCData<|T: ConstructorArgument, ...: ...any|>
8485

8586
return TCData<|obj|>
8687
end,
87-
__le = function <|self: self, other: self | number | any|>
88+
__le = function <|a: any, b: number | any|>
8889
return boolean
8990
end,
90-
__lt = function <|self: self, other: self | number | any|>
91+
__lt = function <|a: any, b: number | any|>
9192
return boolean
9293
end,
93-
__add = function <|self: self, other: self | number | any|>
94+
__add = function <|a: any, b: number | any|>
9495
return TCData<|T|>
9596
end,
96-
__sub = function <|self: self, other: self | number | any|>
97-
return TCData<|T|>
97+
__sub = function <|a: any, b: number | any|>
98+
if type(b) == "number" then return TCData<|T|> end
99+
100+
return number
98101
end,
102+
__newindex = function <|self: self, key: string | number, value: any|> end,
99103
}
100104
end
101105

@@ -104,6 +108,7 @@ function TCType<|T: ConstructorArgument|>
104108
local type is_pointer = type(T) == "table" and keysof<|T|> == number
105109
return {
106110
T = T,
111+
@TypeOverride = "cdata",
107112
@MetaTable = self,
108113
__is_pointer = is_pointer,
109114
__tostring = function <|self: self|>
@@ -127,6 +132,11 @@ function TCType<|T: ConstructorArgument|>
127132
__sub = function <|self: self, other: self | number|>
128133
return TCType<|T|>
129134
end,
135+
-- TODO, __newindex is assigned and used while other fields are being assigned
136+
-- so it has to be done at the end to prevent ie __add = from erroring
137+
__newindex = function <|self: self, key: any, value: any|>
138+
error<|"ctype does not support newindex", 2|>
139+
end,
130140
}
131141
end
132142

@@ -154,8 +164,7 @@ function FFIArray<|size: number, T: any|>
154164
return {[0 .. size - 1] = T}
155165
end
156166

157-
type cdata = FFIPointer<|number|>
158-
type cdata.@TypeOverride = "cdata"
167+
type cdata = TCData<|any|>
159168
type Modules["ffi"] = {
160169
errno = function=(nil | number)>(number),
161170
os = "Windows" | "Linux" | "OSX" | "BSD" | "POSIX" | "Other",
@@ -323,8 +332,8 @@ analyzer function ffi.load(lib: string, global: boolean | nil)
323332
return cdecl_parser.load(lib)
324333
end
325334

326-
analyzer function ffi.gc(ctype: ctype, callback: Function): ctype
327-
return ctype
335+
analyzer function ffi.gc(obj: cdata, callback: Function)
336+
return obj
328337
end
329338

330339
function FFIType<|str: string|>

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

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
do
2-
return
3-
end -- PENDING_TEST
41
local ffi = require("ffi")
52
local _M = {}
63
ffi.cdef[[
@@ -62,7 +59,7 @@ local gbuf = ffi.new("char [?]", gbuf_n)
6259
local function buf_grow(len: ffi.typeof_arg("int"), nokeep: boolean | nil)
6360
if len > gbuf_n then
6461
gbuf_n = len
65-
local newbuf = ffi.new("char [?]", gbuf_n)
62+
local newbuf = ffi.new("char [?]", gbuf_n as number)
6663

6764
if not nokeep then ffi.copy(newbuf, gbuf, gbuf_n) end
6865

@@ -81,10 +78,6 @@ function _M.replace(automate: ac_t, str: string, replacement: string, ignore_cas
8178
compare_str_p = ffi.new("const char *", compare_str)
8279
end
8380

84-
if not compare_str_p then error("uh oh") end
85-
86-
if not str_p then error("uh oh") end
87-
8881
buf_grow(#str, true)
8982
-- BUG HERE!
9083
-- we cannot rely on (gbuf_p - gbuf) to calculate length. it seems like pointer arithmetic triggers bug of luajit
@@ -96,8 +89,6 @@ function _M.replace(automate: ac_t, str: string, replacement: string, ignore_cas
9689
local gbuf_p = ffi.new("char *", gbuf)
9790

9891
while true do
99-
if _ as boolean then break end
100-
10192
local r = ac.ac_match(automate, compare_str_p, str_n)
10293

10394
if r.match_begin >= 0 then
@@ -165,16 +156,10 @@ function _M.mask(automate: ac_t, str: string, p: string, ignore_case: boolean)
165156
compare_str_p = ffi.new("const char *", compare_str)
166157
end
167158

168-
if not compare_str_p then error("uh oh") end
169-
170-
if not str_p then error("uh oh") end
171-
172159
buf_grow(#str, true)
173160
local gbuf_p = gbuf
174161

175162
while true do
176-
if _ as boolean then break end
177-
178163
local r = ac.ac_match(automate, compare_str_p, str_n)
179164

180165
if r.match_begin >= 0 then

0 commit comments

Comments
 (0)