summaryrefslogtreecommitdiff
path: root/test/plugins/ffi/builder.lua
blob: ebbfab551ceb7d4758bb77f97c4bda25042b1289 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
local ffi = require 'plugins.ffi'
local util = require 'utility'
rawset(_G, 'TEST', true)

local function removeEmpty(lines)
    local removeLines = {}
    for i, v in ipairs(lines) do
        if v ~= '\n' then
            removeLines[#removeLines+1] = v:gsub('^%s+', '')
        end
    end
    return removeLines
end

local function formatLines(lines)
    if not lines or #lines == 0 then
        return {}
    end
    table.remove(lines, 1)
    return removeEmpty(lines)
end

---@param str string
local function splitLines(str)
    local lines = {}
    local i = 1
    for line in str:gmatch("[^\r\n]+") do
        lines[i] = line
        i = i + 1
    end
    return lines
end

function TEST(wanted)
    wanted = removeEmpty(splitLines(wanted))
    return function (script)
        local lines = formatLines(ffi.compileCodes({ script }))
        assert(util.equal(wanted, lines), util.dump(lines))
    end
end

TEST [[
    ---@alias ffi.namespace*.EVP_MD ffi.namespace*.struct@env_md_st

    ---@return ffi.namespace*.EVP_MD
    function m.EVP_md5() end
]] [[
    typedef struct env_md_st EVP_MD;
    const EVP_MD *EVP_md5(void);
]]

TEST [[
    ---@class ffi.namespace*.struct@a
    ---@field _in integer
]] [[
    struct a {
        int in;
    };
]]

TEST [[
---@param _in integer
function m.test(_in) end
]] [[
    void test(int in);
]]

TEST [[
    ---@alias ffi.namespace*.ENGINE ffi.namespace*.struct@engine_st
    ---@alias ffi.namespace*.ENGINE1 ffi.namespace*.enum@engine_st1
]] [[
    typedef struct engine_st ENGINE;
    typedef enum engine_st1 ENGINE1;
]]

TEST [[
    ---@param a integer[][]
    function m.test(a) end
]] [[
    void test(int a[][]);
]]

TEST [[
    ---@class ffi.namespace*.struct@A
    ---@field b integer[]
    ---@field c integer[]
]] [[
    struct A {
        int b[5];
        int c[];
    };
]]

TEST [[
    m.B = 5
    m.A = 0
    m.D = 7
    m.C = 6
]] [[
    enum {
        A,
        B=5,
        C,
        D,
    };
]]

TEST [[
    m.B = 2
    m.A = 1
    m.C = 5
    ---@alias ffi.namespace*.enum@a 1 | 2 | 'B' | 'A' | 5 | 'C'
]] [[
    enum a {
       A = 1,
       B = 2,
       C = A|B+2,
    };
]]

TEST [[
    ---@param a boolean
    ---@param b boolean
    ---@param c integer
    ---@param d integer
    function m.test(a, b, c, d) end
]] [[
    void test(bool a, _Bool b, size_t c, ssize_t d);
]]

TEST [[
    ---@param a integer
    ---@param b integer
    ---@param c integer
    ---@param d integer
    function m.test(a, b, c, d) end
]] [[
    void test(int8_t a, int16_t b, int32_t c, int64_t d);
]]

TEST [[
    ---@param a integer
    ---@param b integer
    ---@param c integer
    ---@param d integer
    function m.test(a, b, c, d) end
]] [[
    void test(uint8_t a, uint16_t b, uint32_t c, uint64_t d);
]]

TEST [[
    ---@param a integer
    ---@param b integer
    ---@param c integer
    ---@param d integer
    function m.test(a, b, c, d) end
]] [[
    void test(unsigned char a, unsigned short b, unsigned long c, unsigned int d);
]]

TEST [[
    ---@param a integer
    ---@param b integer
    ---@param c integer
    ---@param d integer
    function m.test(a, b, c, d) end
]] [[
    void test(unsigned char a, unsigned short b, unsigned long c, unsigned int d);
]]

TEST [[
    ---@param a integer
    ---@param b integer
    ---@param c integer
    ---@param d integer
    function m.test(a, b, c, d) end
]] [[
    void test(signed char a, signed short b, signed long c, signed int d);
]]

TEST [[
    ---@param a integer
    ---@param b integer
    ---@param c integer
    ---@param d integer
    function m.test(a, b, c, d) end
]] [[
    void test(char a, short b, long c, int d);
]]

TEST [[
    ---@param a number
    ---@param b number
    ---@param c integer
    ---@param d integer
    function m.test(a, b, c, d) end
]] [[
    void test(float a, double b, int8_t c, uint8_t d);
]]

TEST [[
    ---@alias ffi.namespace*.H ffi.namespace*.void

    function m.test() end
]] [[
    typedef void H;

    H test();
]]

TEST [[
    ---@class ffi.namespace*.a

    ---@param a ffi.namespace*.a
    function m.test(a) end
]] [[
    typedef struct {} a;

    void test(a* a);
]]

TEST [[
    ---@class ffi.namespace*.struct@a
    ---@field a integer
    ---@field b ffi.namespace*.char*

    ---@param a ffi.namespace*.struct@a
    function m.test(a) end
]] [[
    struct a {int a;char* b;};

    void test(struct a* a);
]]