2014-02-27 12:57:06 -07:00
|
|
|
ffi = require 'ffi'
|
|
|
|
|
|
|
|
-- load neovim shared library
|
|
|
|
libnvim = ffi.load './build/src/libnvim-test.so'
|
|
|
|
|
2014-03-04 07:26:37 -07:00
|
|
|
-- Luajit ffi parser doesn't understand preprocessor directives, so
|
|
|
|
-- this helper function removes common directives before passing it the to ffi.
|
|
|
|
-- It will return a pointer to the library table, emulating 'requires'
|
2014-02-27 12:57:06 -07:00
|
|
|
cimport = (path) ->
|
2014-03-04 07:26:37 -07:00
|
|
|
header_file = io.open path, 'rb'
|
|
|
|
|
|
|
|
if not header_file
|
|
|
|
error "cannot find #{path}"
|
|
|
|
|
|
|
|
header = header_file\read '*a'
|
|
|
|
header_file.close!
|
|
|
|
header = string.gsub header, '#include[^\n]*\n', ''
|
|
|
|
header = string.gsub header, '#ifndef[^\n]*\n', ''
|
|
|
|
header = string.gsub header, '#define[^\n]*\n', ''
|
|
|
|
header = string.gsub header, '#endif[^\n]*\n', ''
|
|
|
|
ffi.cdef header
|
2014-02-27 12:57:06 -07:00
|
|
|
|
|
|
|
return libnvim
|
|
|
|
|
2014-03-04 07:26:37 -07:00
|
|
|
cimport './src/types.h'
|
|
|
|
|
2014-02-27 12:57:06 -07:00
|
|
|
-- take a pointer to a C-allocated string and return an interned
|
|
|
|
-- version while also freeing the memory
|
|
|
|
internalize = (cdata) ->
|
|
|
|
ffi.gc cdata, ffi.C.free
|
|
|
|
return ffi.string cdata
|
|
|
|
|
2014-03-06 08:49:00 -07:00
|
|
|
cstr = ffi.typeof 'char[?]'
|
|
|
|
|
|
|
|
to_cstr = (string) ->
|
|
|
|
cstr (string.len string) + 1, string
|
|
|
|
|
2014-02-27 12:57:06 -07:00
|
|
|
return {
|
|
|
|
cimport: cimport
|
|
|
|
internalize: internalize
|
|
|
|
eq: (expected, actual) -> assert.are.same expected, actual
|
|
|
|
ffi: ffi
|
2014-03-04 07:26:37 -07:00
|
|
|
lib: libnvim
|
2014-03-06 08:49:00 -07:00
|
|
|
cstr: cstr
|
|
|
|
to_cstr: to_cstr
|
2014-02-27 12:57:06 -07:00
|
|
|
}
|