Skip to content

Commit

Permalink
Add Lua language support.
Browse files Browse the repository at this point in the history
Lua FFI has a few notable differrences from regular generated C headers:
* Custom prelude/epilogue with `ffi.cdef` directive.
* Constants defined using `static const` syntax instead of `#define`.
* Different comments format using `--` or `[[` instead of `/* */`.
* Doesn't support `#include`, `#ifdef` or other macros.

Lua FFI described in more details at http://luajit.org/ext_ffi.html

Other changes:
* Added const generics support for the structs.
* Added implementation for array of any const size `N`.
* Added `try_as_str` implementation for `str_ref`.
  • Loading branch information
bocharov committed Aug 18, 2023
1 parent 0c5829a commit 54cb34f
Show file tree
Hide file tree
Showing 23 changed files with 1,247 additions and 157 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/gh-pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,15 @@ jobs:
command: test
args: --features docs

- name: FFI test (C & C#?)
- name: Install LuaJIT and LuaRocks (Ubuntu)
run: sudo apt-get install -y luajit
if: matrix.os == 'ubuntu-latest'

- name: Install LuaJIT and LuaRocks (macOS)
run: brew install luajit
if: matrix.os == 'macos-latest'

- name: FFI test (C & C# & Lua?)
run: make -C ffi_tests
if: runner.os != 'Windows'
env:
Expand Down
46 changes: 46 additions & 0 deletions ffi_tests/generated.cffi
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ typedef struct AnUnusedStruct {
Wow_t are_you_still_there;
} AnUnusedStruct_t;

typedef struct {
float idx[3];
} float_3_array_t;

typedef struct {
size_t idx[5];
} size_5_array_t;

typedef struct ArraysStruct {
float_3_array_t floats;

size_5_array_t sizes;
} ArraysStruct_t;

#define FOO ...

typedef enum Bar {
Expand All @@ -36,6 +50,38 @@ typedef struct next_generation {
void * (*cb)(bool);
} next_generation_t;

typedef struct {
uint8_t idx[1];
} uint8_1_array_t;

typedef struct ConstGenericStruct_uint8_1 {
uint8_1_array_t data;
} ConstGenericStruct_uint8_1_t;

typedef struct {
uint8_t idx[2];
} uint8_2_array_t;

typedef struct ConstGenericStruct_uint8_2 {
uint8_2_array_t data;
} ConstGenericStruct_uint8_2_t;

typedef struct {
uint16_t idx[3];
} uint16_3_array_t;

typedef struct ConstGenericStruct_uint16_3 {
uint16_3_array_t data;
} ConstGenericStruct_uint16_3_t;

typedef struct SpecificConstGenericContainer {
ConstGenericStruct_uint8_1_t field1;

ConstGenericStruct_uint8_2_t field2;

ConstGenericStruct_uint16_3_t field3;
} SpecificConstGenericContainer_t;

typedef enum triforce {
TRIFORCE_DIN,
TRIFORCE_FARORE,
Expand Down
58 changes: 58 additions & 0 deletions ffi_tests/generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,25 @@ public unsafe struct AnUnusedStruct_t {
public Wow_t are_you_still_there;
}

[StructLayout(LayoutKind.Sequential, Size = 12)]
public unsafe struct float_3_array_t {
public float _0;
public float _1;
public float _2;
}

[StructLayout(LayoutKind.Sequential, Size = 40)]
public unsafe struct size_5_array_t {
public fixed UIntPtr arr[5];
}

[StructLayout(LayoutKind.Sequential, Size = 56)]
public unsafe struct ArraysStruct_t {
public float_3_array_t floats;

public size_5_array_t sizes;
}

public unsafe partial class Ffi {
public const Int32 FOO = 42;
}
Expand Down Expand Up @@ -66,6 +85,45 @@ public unsafe struct next_generation_t {
public void_ptr_bool_fptr cb;
}

[StructLayout(LayoutKind.Sequential, Size = 1)]
public unsafe struct uint8_1_array_t {
public fixed byte arr[1];
}

[StructLayout(LayoutKind.Sequential, Size = 1)]
public unsafe struct ConstGenericStruct_uint8_1_t {
public uint8_1_array_t data;
}

[StructLayout(LayoutKind.Sequential, Size = 2)]
public unsafe struct uint8_2_array_t {
public fixed byte arr[2];
}

[StructLayout(LayoutKind.Sequential, Size = 2)]
public unsafe struct ConstGenericStruct_uint8_2_t {
public uint8_2_array_t data;
}

[StructLayout(LayoutKind.Sequential, Size = 6)]
public unsafe struct uint16_3_array_t {
public fixed UInt16 arr[3];
}

[StructLayout(LayoutKind.Sequential, Size = 6)]
public unsafe struct ConstGenericStruct_uint16_3_t {
public uint16_3_array_t data;
}

[StructLayout(LayoutKind.Sequential, Size = 10)]
public unsafe struct SpecificConstGenericContainer_t {
public ConstGenericStruct_uint8_1_t field1;

public ConstGenericStruct_uint8_2_t field2;

public ConstGenericStruct_uint16_3_t field3;
}

/// <summary>
/// Hello, <c>World</c>!
/// </summary>
Expand Down
59 changes: 59 additions & 0 deletions ffi_tests/generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ typedef struct AnUnusedStruct {
Wow_t are_you_still_there;
} AnUnusedStruct_t;

typedef struct {
float idx[3];
} float_3_array_t;

typedef struct {
size_t idx[5];
} size_5_array_t;

/** <No documentation available> */
typedef struct ArraysStruct {
/** <No documentation available> */
float_3_array_t floats;

/** <No documentation available> */
size_5_array_t sizes;
} ArraysStruct_t;

/** <No documentation available> */
#define FOO ((int32_t) 42)

Expand Down Expand Up @@ -76,6 +93,48 @@ typedef struct next_generation {
void * (*cb)(bool);
} next_generation_t;

typedef struct {
uint8_t idx[1];
} uint8_1_array_t;

/** <No documentation available> */
typedef struct ConstGenericStruct_uint8_1 {
/** <No documentation available> */
uint8_1_array_t data;
} ConstGenericStruct_uint8_1_t;

typedef struct {
uint8_t idx[2];
} uint8_2_array_t;

/** <No documentation available> */
typedef struct ConstGenericStruct_uint8_2 {
/** <No documentation available> */
uint8_2_array_t data;
} ConstGenericStruct_uint8_2_t;

typedef struct {
uint16_t idx[3];
} uint16_3_array_t;

/** <No documentation available> */
typedef struct ConstGenericStruct_uint16_3 {
/** <No documentation available> */
uint16_3_array_t data;
} ConstGenericStruct_uint16_3_t;

/** <No documentation available> */
typedef struct SpecificConstGenericContainer {
/** <No documentation available> */
ConstGenericStruct_uint8_1_t field1;

/** <No documentation available> */
ConstGenericStruct_uint8_2_t field2;

/** <No documentation available> */
ConstGenericStruct_uint16_3_t field3;
} SpecificConstGenericContainer_t;

/** \brief
* Hello, `World`!
*/
Expand Down
Loading

0 comments on commit 54cb34f

Please sign in to comment.