Lua Notes

nldecl
Login

nldecl

Nelua can read C code to generate Nelua bindings appropriate for its use. This can be done at compile-time and in a convenient manner, similar to the facilities offered by Terra, Zig, and D's ImportC. Or indeed, similar to build-system binding generators like SWIG. A practical example is included in examples/snakesdl_nldecl.nelua, distributed with Nelua.

For example, this code

##[[
local nldecl = require 'nelua.plugins.nldecl'
local fs = require 'nelua.utils.fs'
if not fs.isfile('pcre-bindings.nelua') then
  nldecl.generate_bindings_file {
    output_file = 'pcre-bindings.nelua',
    parse_includes = {'<pcre.h>'},
    include_names = {'^PCRE_', '^pcre_'},
  }
end
cinclude '<pcre.h>'
linklib 'pcre'
]]
require 'pcre-bindings'

generates, pcre-bindings.nelua, with code like

global pcre_extra: type <cimport,nodecl> = @record{
  flags: culong,
  study_data: pointer,
  match_limit: culong,
  callout_data: pointer,
  tables: *cuchar,
  match_limit_recursion: culong,
  mark: **cuchar,
  executable_jit: pointer
}
global pcre_stack_guard: function(): cint <cimport,nodecl>
global function pcre_study(a1: *pcre, a2: cint, a3: *cstring): *pcre_extra <cimport,nodecl> end
global PCRE_MAJOR: cint <comptime> = 8
global PCRE_MINOR: cint <comptime> = 39
global PCRE_DATE: cint <comptime> = 1996

and so on. Even if a binding is relatively easy to do by hand, nldecl can make your program more responsive to changes in a C API, such as subtle type changes or additional struct members. nldecl's generate_bindings_file takes a Lua table which can contain quite a few options to control the generation of the bindings: