Code Monkey home page Code Monkey logo

ffi-to-lls's Introduction

FFI-to-LLS

Automatically turns LuaJIT FFI declarations into valid Lua-Language-Server meta files.

Dependencies

  • LuaJIT
  • LLVM (for libclang)
  • C compiler (to compile utilities.c)

Usage

First, compile utilities.c

cc $(llvm-config --cflags) utilities.c -shared -o utilities.so -L$(llvm-config --libdir) -lclang

Then, run the script:

luajit ffi-to-lls.lua <input defs.h> [-o <output file>] [--remove-prefix <prefix>] [--no-auxiliary-types] [--module-name <name>]

Options

  • <input defs.h>: The header file containing the FFI declarations.
  • -o <output file>: The output file to write the LLS meta file to. If not specified, the output will be written to stdout.
  • --remove-prefix <prefix>: Remove the specified prefix from the function names in the output. This is useful when the FFI declarations are prefixed with a common string, such as lua_ or lj_.
  • --no-auxiliary-types: Do not generate auxiliary types such as c.pointer<T>. This is useful when the generated file is used with another file that already defines these types.
  • --module-name <name>: The name of the module to be usede in generation

Example

Given the a definitions file such as:

struct MyStruct {
  int a;
  int b;
};

struct MyStruct *mylib_MyStruct_create(int a, int b);

void mylib_MyStruct_free(struct MyStruct *s);

We can generate definitions with

luajit ffi-to-lls.lua mylib.h -o mylib.lua --remove-prefix mylib_

A file like this would be generated:

---Automatically generated bindings generated by ffi-to-lls.lua (https://github.com/Frityet/ffi-to-lls/)
---You can edit this file!
---The following prefix was removed: `mylib_`
---Lua language server will autocomplete both with and without the prefix.
---@meta mylib

---You may remove this, or generate this file again with --no-auxiliary-types, to supress redefinition warnings
---@class c.pointer<T> : { [integer] : T }, ffi.cdata*
---@class mylib
local mylib = {}

---@class MyStruct
---@field a integer
---@field b integer

---@param a integer
---@param b integer
---@return c.pointer<MyStruct>
function mylib.mylib_MyStruct_create(a, b) end
mylib.MyStruct_create = mylib.mylib_MyStruct_create

---@param s c.pointer<MyStruct>
---@return nil
function mylib.mylib_MyStruct_free(s) end
mylib.MyStruct_free = mylib.mylib_MyStruct_free

return mylib

ffi-to-lls's People

Contributors

frityet avatar

Stargazers

Brynne Taylor avatar Michael Martin avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.