aboutsummaryrefslogtreecommitdiff
path: root/test/functional/lua/ffi_spec.lua
blob: 3a37b18cd1bd64da32ed2938302594a160bfd30e (plain) (blame)
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
local helpers = require('test.functional.helpers')(after_each)
local eq = helpers.eq
local exec_lua = helpers.exec_lua
local clear = helpers.clear

before_each(clear)

describe('ffi.cdef', function()
  it('can use Neovim core functions', function()
    if not exec_lua("return pcall(require, 'ffi')") then
      pending('missing LuaJIT FFI')
    end

    eq(12, exec_lua[[
      local ffi = require('ffi')

      ffi.cdef('int curwin_col_off(void);')

      vim.cmd('set number numberwidth=4 signcolumn=yes:4')

      return ffi.C.curwin_col_off()
    ]])

    eq(20, exec_lua[=[
      local ffi = require('ffi')

      ffi.cdef[[
        typedef struct window_S win_T;
        typedef struct {} stl_hlrec_t;
        typedef struct {} StlClickRecord;
        typedef struct {} statuscol_T;
        typedef struct {} Error;

        win_T *find_window_by_handle(int Window, Error *err);

        int build_stl_str_hl(
          win_T *wp,
          char *out,
          size_t outlen,
          char *fmt,
          char *opt_name,
          int opt_scope,
          int fillchar,
          int maxwidth,
          stl_hlrec_t **hltab,
          StlClickRecord **tabtab,
          statuscol_T *scp
        );
      ]]

      return ffi.C.build_stl_str_hl(
        ffi.C.find_window_by_handle(0, ffi.new('Error')),
        ffi.new('char[1024]'),
        1024,
        ffi.cast('char*', 'StatusLineOfLength20'),
        nil,
        0,
        0,
        0,
        nil,
        nil,
        nil
      )
    ]=])

    -- Check that extern symbols are exported and accessible
    eq(true, exec_lua[[
      local ffi = require('ffi')

      ffi.cdef('uint64_t display_tick;')

      return ffi.C.display_tick >= 0
    ]])
  end)
end)