summaryrefslogtreecommitdiff
path: root/test/plugins/ffi/builder.lua
blob: 47e39db3bce7bafa09660973fe74e731a3660561 (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
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 [[
    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 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);
]]