Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: lune-org/lune
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: main
Choose a base ref
...
head repository: qwreey/lune
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: ffi
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.

Commits on Oct 13, 2024

  1. Add lune-std-ffi crate (#243)

    qwreey committed Oct 13, 2024
    Copy the full SHA
    af08c59 View commit details
  2. Implement struct and arr (#243)

    struct.size: Returns a non-zero actual size
    rewrite use super:: => use crate::
    qwreey committed Oct 13, 2024
    Copy the full SHA
    8c38aef View commit details
  3. Copy the full SHA
    b36948c View commit details
  4. Copy the full SHA
    6f131e9 View commit details
  5. Copy the full SHA
    26706d9 View commit details
  6. Copy the full SHA
    d60a1b9 View commit details
  7. Implement ctype casting (#243)

    qwreey committed Oct 13, 2024
    Copy the full SHA
    6d0db93 View commit details
  8. Copy the full SHA
    48d2db4 View commit details
  9. Copy the full SHA
    3ccb072 View commit details
  10. Copy the full SHA
    b54ea51 View commit details
  11. Copy the full SHA
    dd6a386 View commit details
  12. Add is_integer (#243)

    qwreey committed Oct 13, 2024
    Copy the full SHA
    94d8d07 View commit details
  13. Improve ffiref (#243)

    qwreey committed Oct 13, 2024
    Copy the full SHA
    e23aaef View commit details
  14. Add tests for ffi (#243)

    qwreey committed Oct 13, 2024
    Copy the full SHA
    c656a48 View commit details
  15. Copy the full SHA
    11bf0b6 View commit details
  16. Copy the full SHA
    4d0fd9d View commit details

Commits on Oct 14, 2024

  1. Copy the full SHA
    7ce5be2 View commit details
  2. Implement call (#243)

    qwreey committed Oct 14, 2024
    Copy the full SHA
    46dd185 View commit details

Commits on Oct 16, 2024

  1. Copy the full SHA
    7d4e4a2 View commit details
  2. Copy the full SHA
    f094f2b View commit details
  3. Copy the full SHA
    95258e1 View commit details
  4. Copy the full SHA
    f27bed5 View commit details
  5. Copy the full SHA
    b03b485 View commit details
  6. Copy the full SHA
    d27fba8 View commit details
  7. chore(types): fix incorrect function signatures

    * Fixes incorrect function signatures for callables.
    * Now correctly returns the main module.
    CompeyDev authored Oct 16, 2024
    Copy the full SHA
    991ae5a View commit details
  8. Copy the full SHA
    133abb5 View commit details
  9. Copy the full SHA
    ba074d9 View commit details

Commits on Oct 17, 2024

  1. Renaming methods (#243)

    qwreey committed Oct 17, 2024
    Copy the full SHA
    58add58 View commit details
  2. Copy the full SHA
    bf33afd View commit details
  3. Merge pull request #1 from 0x5eal/chore/ffi-types

    Update Luau types
    qwreey authored Oct 17, 2024
    Copy the full SHA
    a2a8176 View commit details
  4. Copy the full SHA
    da30dfb View commit details
  5. Implement ClosureData (#243)

    qwreey committed Oct 17, 2024
    Copy the full SHA
    e19d974 View commit details
  6. Copy the full SHA
    7ee757a View commit details

Commits on Oct 18, 2024

  1. Copy the full SHA
    27e250d View commit details

Commits on Oct 19, 2024

  1. Copy the full SHA
    a67661a View commit details

Commits on Oct 21, 2024

  1. Copy the full SHA
    b31f814 View commit details
  2. Copy the full SHA
    658b5ef View commit details
  3. Fix closure error (#243)

    qwreey committed Oct 21, 2024
    Copy the full SHA
    5002088 View commit details
  4. Copy the full SHA
    72fac28 View commit details
  5. Copy the full SHA
    12bf3bd View commit details
  6. Copy the full SHA
    410489a View commit details
  7. Fix formatting (#243)

    qwreey committed Oct 21, 2024
    Copy the full SHA
    706efaf View commit details
  8. More moonwave docs (#243)

    qwreey committed Oct 21, 2024
    Copy the full SHA
    144f49a View commit details

Commits on Oct 22, 2024

  1. Copy the full SHA
    d42bfc9 View commit details
  2. Copy the full SHA
    ddf0c4c View commit details
  3. Fix assertion (#243)

    qwreey committed Oct 22, 2024
    Copy the full SHA
    886d555 View commit details
  4. Copy the full SHA
    b442ba7 View commit details

Commits on Oct 23, 2024

  1. Copy the full SHA
    83be2bc View commit details
  2. Copy the full SHA
    34a5b39 View commit details

Commits on Oct 24, 2024

  1. Copy the full SHA
    154c68a View commit details
Showing with 6,684 additions and 8 deletions.
  1. +4 −0 .gitattributes
  2. +9 −0 .gitignore
  3. +113 −2 Cargo.lock
  4. +1 −0 Cargo.toml
  5. +24 −0 crates/lune-std-ffi/Cargo.toml
  6. +146 −0 crates/lune-std-ffi/README.md
  7. +180 −0 crates/lune-std-ffi/src/c/arr_info.rs
  8. +218 −0 crates/lune-std-ffi/src/c/fn_info.rs
  9. +318 −0 crates/lune-std-ffi/src/c/helper.rs
  10. +52 −0 crates/lune-std-ffi/src/c/mod.rs
  11. +192 −0 crates/lune-std-ffi/src/c/ptr_info.rs
  12. +13 −0 crates/lune-std-ffi/src/c/string_info.rs
  13. +215 −0 crates/lune-std-ffi/src/c/struct_info.rs
  14. +142 −0 crates/lune-std-ffi/src/c/type_info.rs
  15. +90 −0 crates/lune-std-ffi/src/c/types/f32.rs
  16. +90 −0 crates/lune-std-ffi/src/c/types/f64.rs
  17. +90 −0 crates/lune-std-ffi/src/c/types/i128.rs
  18. +90 −0 crates/lune-std-ffi/src/c/types/i16.rs
  19. +90 −0 crates/lune-std-ffi/src/c/types/i32.rs
  20. +90 −0 crates/lune-std-ffi/src/c/types/i64.rs
  21. +82 −0 crates/lune-std-ffi/src/c/types/i8.rs
  22. +90 −0 crates/lune-std-ffi/src/c/types/isize.rs
  23. +203 −0 crates/lune-std-ffi/src/c/types/mod.rs
  24. +90 −0 crates/lune-std-ffi/src/c/types/u128.rs
  25. +91 −0 crates/lune-std-ffi/src/c/types/u16.rs
  26. +90 −0 crates/lune-std-ffi/src/c/types/u32.rs
  27. +90 −0 crates/lune-std-ffi/src/c/types/u64.rs
  28. +85 −0 crates/lune-std-ffi/src/c/types/u8.rs
  29. +90 −0 crates/lune-std-ffi/src/c/types/usize.rs
  30. +42 −0 crates/lune-std-ffi/src/c/void_info.rs
  31. +13 −0 crates/lune-std-ffi/src/data/box_data/flag.rs
  32. +166 −0 crates/lune-std-ffi/src/data/box_data/mod.rs
  33. +191 −0 crates/lune-std-ffi/src/data/callable_data.rs
  34. +161 −0 crates/lune-std-ffi/src/data/closure_data.rs
  35. +103 −0 crates/lune-std-ffi/src/data/helper.rs
  36. +68 −0 crates/lune-std-ffi/src/data/lib_data.rs
  37. +68 −0 crates/lune-std-ffi/src/data/mod.rs
  38. +99 −0 crates/lune-std-ffi/src/data/ref_data/bounds.rs
  39. +23 −0 crates/lune-std-ffi/src/data/ref_data/flag.rs
  40. +204 −0 crates/lune-std-ffi/src/data/ref_data/mod.rs
  41. +52 −0 crates/lune-std-ffi/src/ffi/association.rs
  42. +31 −0 crates/lune-std-ffi/src/ffi/bit_field.rs
  43. +36 −0 crates/lune-std-ffi/src/ffi/cast.rs
  44. +43 −0 crates/lune-std-ffi/src/ffi/libffi_helper.rs
  45. +129 −0 crates/lune-std-ffi/src/ffi/mod.rs
  46. +39 −0 crates/lune-std-ffi/src/lib.rs
  47. +3 −0 crates/lune-std/Cargo.toml
  48. +2 −0 crates/lune-std/src/lib.rs
  49. +37 −2 crates/lune-std/src/library.rs
  50. +26 −0 crates/lune-std/src/unsafe_library.rs
  51. +2 −0 crates/lune/Cargo.toml
  52. +6 −2 crates/lune/src/cli/repl.rs
  53. +6 −1 crates/lune/src/cli/run.rs
  54. +11 −0 crates/lune/src/rt/runtime.rs
  55. +29 −1 crates/lune/src/tests.rs
  56. +113 −0 tests/ffi/README.md
  57. +24 −0 tests/ffi/benchmark/external_call/deno.ts
  58. +31 −0 tests/ffi/benchmark/external_call/init.luau
  59. +18 −0 tests/ffi/benchmark/external_call/lib.c
  60. +24 −0 tests/ffi/benchmark/external_call/luajit.lua
  61. +13 −0 tests/ffi/cast.luau
  62. +13 −0 tests/ffi/external_closure/callClosure.luau
  63. +15 −0 tests/ffi/external_closure/callClosureWithPointer.luau
  64. +11 −0 tests/ffi/external_closure/callHelloWorld.luau
  65. +17 −0 tests/ffi/external_closure/lib.c
  66. +9 −0 tests/ffi/external_math/addInt.luau
  67. +7 −0 tests/ffi/external_math/lib.c
  68. +8 −0 tests/ffi/external_math/mulInt.luau
  69. +7 −0 tests/ffi/external_pointer/lib.c
  70. +8 −0 tests/ffi/external_pointer/pointerRead.luau
  71. +9 −0 tests/ffi/external_pointer/pointerWrite.luau
  72. +5 −0 tests/ffi/external_print/helloWorld.luau
  73. +5 −0 tests/ffi/external_print/lib.c
  74. +12 −0 tests/ffi/external_struct/ab.luau
  75. +14 −0 tests/ffi/external_struct/lib.c
  76. +18 −0 tests/ffi/free.luau
  77. +7 −0 tests/ffi/isInteger.luau
  78. +6 −0 tests/ffi/pretty_print/arr.luau
  79. +4 −0 tests/ffi/pretty_print/box.luau
  80. +13 −0 tests/ffi/pretty_print/fn.luau
  81. +6 −0 tests/ffi/pretty_print/ptr.luau
  82. +8 −0 tests/ffi/pretty_print/struct.luau
  83. +6 −0 tests/ffi/pretty_print/type.luau
  84. +51 −0 tests/ffi/readBoundary.luau
  85. +34 −0 tests/ffi/stringReadWrite.luau
  86. +19 −0 tests/ffi/types/arr.luau
  87. +31 −0 tests/ffi/types/ptr.luau
  88. +20 −0 tests/ffi/types/struct.luau
  89. +38 −0 tests/ffi/utils/callableWrapper.luau
  90. +25 −0 tests/ffi/utils/compile.luau
  91. +13 −0 tests/ffi/utils/libSuffix.ts
  92. +27 −0 tests/ffi/utils/proc_clock/deno.ts
  93. +46 −0 tests/ffi/utils/proc_clock/init.luau
  94. +12 −0 tests/ffi/utils/proc_clock/lib.c
  95. +45 −0 tests/ffi/writeBoundary.luau
  96. +1,224 −0 types/ffi.luau
4 changes: 4 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -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
@@ -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
@@ -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",
]
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