Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FFI support for lune #243

Draft
wants to merge 84 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
84 commits
Select commit Hold shift + click to select a range
af08c59
Add lune-std-ffi crate (#243)
qwreey Aug 21, 2024
8c38aef
Implement struct and arr (#243)
qwreey Aug 23, 2024
b36948c
Implement carr, cstruct, ctype and cptr (#243)
qwreey Aug 24, 2024
6f131e9
Implement chelper, ffiref, ffibox (#243)
qwreey Aug 24, 2024
26706d9
Improve ctype export and Refactor (#243)
qwreey Aug 25, 2024
d60a1b9
Use CType<T> instead of CType (#243)
qwreey Aug 25, 2024
6d0db93
Implement ctype casting (#243)
qwreey Aug 26, 2024
48d2db4
Implement luavalue conversion (#243)
qwreey Aug 27, 2024
3ccb072
Export ctypes and implement signedness (#243)
qwreey Aug 27, 2024
b54ea51
Implement boundary check (#243)
qwreey Aug 28, 2024
dd6a386
Improve conversion performance by caching dyn handle on subtype (#243)
qwreey Aug 30, 2024
94d8d07
Add is_integer (#243)
qwreey Sep 2, 2024
e23aaef
Improve ffiref (#243)
qwreey Sep 2, 2024
c656a48
Add tests for ffi (#243)
qwreey Sep 6, 2024
11bf0b6
Remove unused codes and strange safety features (#243)
qwreey Sep 6, 2024
4d0fd9d
Implement callable and closure (#243)
qwreey Oct 12, 2024
7ce5be2
Add uninit ref and Implement Callable (#243)
qwreey Oct 14, 2024
46dd185
Implement call (#243)
qwreey Oct 14, 2024
7d4e4a2
Refactor type-define macro rules (#243)
qwreey Oct 16, 2024
f094f2b
Remove static ctype reduce duplicated code (#243)
qwreey Oct 16, 2024
95258e1
Fix pretty-prints for type-define (#243)
qwreey Oct 16, 2024
f27bed5
chore(types): updated ffi types
CompeyDev Oct 16, 2024
b03b485
chore(types): fix `CType:cast` to be properly typed
CompeyDev Oct 16, 2024
d27fba8
chore(types): remove unnecessary generic usage
CompeyDev Oct 16, 2024
991ae5a
chore(types): fix incorrect function signatures
CompeyDev Oct 16, 2024
133abb5
chore(types): add accidently removed leading pipe to `CTypes`
CompeyDev Oct 16, 2024
ba074d9
Reducing and organizing duplicated codes (#243)
qwreey Oct 16, 2024
58add58
Renaming methods (#243)
qwreey Oct 17, 2024
bf33afd
Merge branch 'lune-org:main' into ffi
qwreey Oct 17, 2024
a2a8176
Merge pull request #1 from 0x5eal/chore/ffi-types
qwreey Oct 17, 2024
da30dfb
Refactor and documenting code structure (#243)
qwreey Oct 17, 2024
e19d974
Implement ClosureData (#243)
qwreey Oct 17, 2024
7ee757a
Provide ptr conversion and test case (#243)
qwreey Oct 17, 2024
27e250d
Implememt CVoid, StringifyData and CopyData (#243)
qwreey Oct 18, 2024
a67661a
Void type support in call (#243)
qwreey Oct 19, 2024
b31f814
Move c ABI related object and functions into ffi.c (#243)
qwreey Oct 21, 2024
658b5ef
Move fixed size types into ffi, Fix test cases (#243)
qwreey Oct 21, 2024
5002088
Fix closure error (#243)
qwreey Oct 21, 2024
72fac28
Add Moonwave annotation partially (#243)
qwreey Oct 21, 2024
12bf3bd
Add gitignore for core dump file, Callable optimization (#243)
qwreey Oct 21, 2024
410489a
Merge branch 'lune-org:main' into ffi
qwreey Oct 21, 2024
706efaf
Fix formatting (#243)
qwreey Oct 21, 2024
144f49a
More moonwave docs (#243)
qwreey Oct 21, 2024
d42bfc9
Add benchmark tests/ffi/benchmark/external_call (#243)
qwreey Oct 22, 2024
ddf0c4c
Typescript lint error fix (#243)
qwreey Oct 22, 2024
886d555
Fix assertion (#243)
qwreey Oct 22, 2024
b442ba7
Add windows benchmark result (#243)
qwreey Oct 22, 2024
83be2bc
Remove callable boundary check (#243)
qwreey Oct 23, 2024
34a5b39
Add linguist ignore for tests/ffi (#243)
qwreey Oct 23, 2024
154c68a
Add Data:copyFrom, tests and annotation (#243)
qwreey Oct 24, 2024
14d2b60
Fix casting and add casting test (#243)
qwreey Oct 24, 2024
00319f0
Use camel case naming on ffi test (#243)
qwreey Oct 24, 2024
90c0987
Restruct tests/ffi and add std-ffi tests (#243)
qwreey Oct 24, 2024
80a7497
Fix benchmark code and add C level result (#243)
qwreey Oct 24, 2024
2a9664a
Add runtime flag for allowing unsafe libraries (#243)
qwreey Oct 31, 2024
de7029a
Remove debug exports (#243)
qwreey Nov 2, 2024
37b5557
Add missing types (#243)
qwreey Nov 2, 2024
4cc2698
Add offset option in cast method (#243)
qwreey Nov 2, 2024
b95266e
type plus
Nov 2, 2024
7856764
fix
Nov 5, 2024
c20ef95
fix
Nov 5, 2024
208bfa1
fix
Nov 5, 2024
8b2fdbc
move fixme
Nov 5, 2024
a05c598
moonwave annotation fix
Nov 6, 2024
a525faa
moonwave annotation fix
Nov 6, 2024
1e69c0a
Merge pull request #2 from kimpure/ffi
qwreey Nov 7, 2024
9bca7b8
Fix annotation (#243)
qwreey Nov 7, 2024
4b2277f
Fix error messages (#243)
qwreey Nov 8, 2024
a655a4d
Improve code quality (#243)
qwreey Nov 8, 2024
3a6f3bd
Add free function (#243)
qwreey Nov 8, 2024
4588581
Merge branch 'lune-org:main' into ffi
qwreey Nov 8, 2024
c457737
Fix dereference ref flags (#243)
qwreey Nov 8, 2024
809fd56
Add free test (#243)
qwreey Nov 9, 2024
230632b
Fix unsafe library error, add --unsafe repl option (#243)
qwreey Nov 9, 2024
b191218
Throw out of index error in struct:field method (#243)
qwreey Nov 9, 2024
902f5f9
Add and organize ffi tests (#243)
qwreey Nov 9, 2024
b503cc9
Fix boundary check logic (#243)
qwreey Nov 9, 2024
c3f255d
Improve code quality (#243)
qwreey Nov 9, 2024
7a7ccf7
More pretty-prints (#243)
qwreey Nov 9, 2024
461ab26
Update ffi README files (#243)
qwreey Nov 9, 2024
627c2c9
Update readRef method to allow reuse RefData (#243)
qwreey Nov 12, 2024
e8cc2dc
Add readString, writeString method (#243)
qwreey Nov 12, 2024
7a51bc9
Fix read/writeString and add test (#243)
qwreey Nov 12, 2024
8f248ff
Chnage naming naturally (#243)
qwreey Nov 13, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@

# Ensure all txt files within tests use LF
tests/**/*.txt eol=lf

# Remove test c and typescript from language list
tests/ffi/**/*.c linguist-vendored
tests/ffi/**/*.ts linguist-vendored
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ luneDocs.json
luneTypes.d.luau

# Files generated by runtime or build scripts

scripts/brick_color.rs
scripts/font_enum_map.rs
scripts/physical_properties_enum_map.rs

# Files generated by tests

/tests/ffi/**/*.so

# Core dump file

/core
115 changes: 113 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ members = [
"crates/lune-std-serde",
"crates/lune-std-stdio",
"crates/lune-std-task",
"crates/lune-std-ffi",
"crates/lune-utils",
"crates/mlua-luau-scheduler",
]
Expand Down
24 changes: 24 additions & 0 deletions crates/lune-std-ffi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "lune-std-ffi"
version = "0.1.1"
edition = "2021"
license = "MPL-2.0"
repository = "https://github.com/lune-org/lune"
description = "Lune standard library - FFI"

[lib]
path = "src/lib.rs"

[lints]
workspace = true

[dependencies]
mlua = { version = "0.9.9", features = ["luau"] }
mlua-sys = { version = "0.6.2", features = ["luau"] }
num = "0.4.3"
dlopen2 = "0.7.0"
libc = "0.2.162"

libffi = "3.2.0"

lune-utils = { version = "0.1.3", path = "../lune-utils" }
146 changes: 146 additions & 0 deletions crates/lune-std-ffi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<!-- markdownlint-disable MD033 -->

# `lune-std-ffi`

## Tests & Benchmarks

See [tests/ffi](../../tests/ffi/README.md)

## TODO

- [CString](./src/c/string_info.rs)
- Add buffer as owned data support
- Add math operation for numeric types
> Provide related methods: `CTypeInfo:add(target, from1, from2, ...)` and `:sub` `:mul` `:div` `:mod` `:pow` `:max` `:min` `:gt` `:lt`
> Luau cannot handle f64, i64 or i128, so we should provide math operation for it
- Add bit operation for box/ref
> Luau only supports 32bit bit operations
- Add wchar and wstring support
> For windows API support
- Add varargs support
- Array argument in cfn
- [More box/ref methods](./src/data/helper.rs)
- writeString
- readString
- writeBase64
- readBase64

## Code structure

### /c

Define C-ABI type information and provide conversion and casting

**Structs:** C ABI type informations

- [**Struct `CArrInfo`:**](./src/c/arr_info.rs) Represents C Array type
- [**Struct `CPtrInfo`:**](./src/c/ptr_info.rs) Represents C Pointer type
- [**Struct `CFnInfo`:**](./src/c/fn_info.rs) Represents C Function signature
> provide `CallableData` and `ClosureData` creator
- [**Struct `CStructInfo`:**](./src/c/struct_info.rs) Represents C Struct type
- [**Struct `CTypeInfo<T>`:**](./src/c/type_info.rs) Represents C type, extended in `/c/types`

<details><summary><a href="./src/c/helper.rs"><strong>Mod <code>helper.rs</code>:</strong></a> C ABI type helper</summary>

- **Function `get_conv`, `get_conv_list`:**
get `FfiConvert` from userdata (CStruct, CArr, CPtr, CTypes)
- **Function `get_middle_type`, `get_middle_type_list`:**
get **`libffi::middle::Type`:** from userdata (CFn, CStruct, CArr, CPtr, CTypes)
- **Function `get_size`:**
get size from userdata
- **Function `has_void`:**
check table has void type
- **Function `stringify`:**
stringify any type userdata
- **Function `get_name`:**
get type name from ctype userdata, used for pretty-print
- **Function `is_ctype`:** check userdata is ctype
- **Mod `method_provider`:** provide common userdata method implements

</details>

#### /c/types

Export fixed-size source time known types and non-fixed compile time known types
mod.rs implememts type-casting for all CTypes

<details><summary><a href="./src/c/types/mod.rs"><strong>Mod <code>ctype_helper</code>:</strong></a> CTypeInfo helper</summary>

- **Function `get_conv`:**
get `FfiConvert` from ctype userdata, used for struct and array conversion
- **Function `get_middle_type`:**
get **`libffi::middle::Type`:** from ctype userdata
- **Function `get_size`:**
get size from ctype userdata
- **Function `get_name`:**
get type name from ctype userdata, used for pretty-print
- **Function `is_ctype`:** check userdata is ctype

</details>

---

### /data

**Structs:** Provide memory userdata

- [**Struct `BoxData`:**](./src/data/box_data/mod.rs) A heap allocated memory with user definable lifetime
- [**Struct `LibData`:**](./src/data/lib_data.rs) A dynamic opened library
- [**Struct `RefData`:**](./src/data/ref_data/mod.rs) A reference that can be used for receiving return data from external function or pass pointer arguments

**Structs:** Provide function(pointer) userdata

- [**Struct `CallableData`:**](./src/data/callable_data.rs) A callable function, which can be created from function pointer
- [**Struct `ClosureData`:**](./src/data/closure_data.rs) A closure pointer, which can be created from lua function and can be used for callback

---

### /ffi

**Traits:** Provide ABI shared common type information trait

- **Trait `FfiSize`**
- **Trait `FfiSignedness`**

<ul><li><details><summary><strong>Trait <code>FfiConvert</code>:</strong> Provide methods for read LuaValue from FfiData or write LuaValue into FfiData</summary>

- **Method `value_into_data`:** set data with lua value
- **Method `value_from_data`:** get lua value from data
- **Method `copy_data`:** copy sized data into another data
- **Method `stringify_data`:** stringify data with specific type

</details></li></ul>

**Structs:** Provide call information

- **Struct `FfiArg`:** Used for argument boundary checking and callback argument ref flag
- **Struct `FfiResult`:** Used for result boundary checking

<details><summary><strong>Trait <code>FfiData</code>:</strong> Provide common data handle, including methods below</summary>

- **Method `check_inner_boundary`:** check boundary with offset and size
- **Method `get_inner_pointer`:** returns raw pointer `*mut ()`
- **Method `is_writable`**
- **Method `is_readable`**
- **Method `copy_from`** copy data from another data

</details>

> Note: `GetFfiData` trait in `data/mod.rs` provides `(LuaValue | LuaAnyUserData).get_data_handle() -> FfiData` method

**Mods:** Provide common helper functions

- [**Mod `association.rs`:**](./src/ffi/association.rs) GC utility, used for inner, ret and arg type holding in subtype
- [**Mod `bit_mask.rs`:**](./src/ffi/bit_mask.rs) u8 bitfield helper
- [**Mod `cast.rs`:**](./src/ffi/cast.rs) num cast library wrapper
- **Function `num_cast<From, Into>(from: FfiData, from: FfiData)`:**
Cast number type value inno another number type

<ul><li><details><summary><a href="./src/c/struct_info.rs"><strong>Mod <code>libffi_helper.rs</code>:</strong></a> libffi library helper</summary>

- **Const `FFI_STATUS_NAMES`:** Stringify `ffi_status`
- **Function `get_ensured_size`:** Returns ensured size of `ffi_type`
- **Const `SIZE_OF_POINTER`:** Platform specific pointer size (Compile time known)
- **Function `ffi_status_assert`:** Convert `ffi_status` to `LuaResult<()>`

</details></li></ul>
Loading