Skip to content

Commit

Permalink
feat: add basic fiber functions
Browse files Browse the repository at this point in the history
  • Loading branch information
szkabaroli committed Jul 7, 2024
1 parent 4d627f5 commit af093e9
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 79 deletions.
41 changes: 27 additions & 14 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,40 @@ jobs:
runs-on: macos-14
steps:
- uses: actions/checkout@v4

- uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
targets: aarch64-apple-darwin,x86_64-apple-darwin,aarch64-apple-ios,aarch64-apple-ios-sim,x86_64-apple-ios
- run: brew install cbindgen

- name: Install cbindgen
run: brew install cbindgen

- name: Run cargo build for each target
- name: Run cargo build for each macos target
run: make macos

- name: Run cargo build for each ios target
run: make ios

- name: Generate header file
run: cbindgen --config cbindgen.toml --crate munchausen --output include/munchausen.h
run: cbindgen --config cbindgen.toml --crate fiber_ffi --output include/fiber_ffi.h

- name: Create binary framework
run: |
xcodebuild -create-xcframework \
-library libs/libmunchausen-macos.a \
-library libs/libfiber_ffi_macos.a \
-headers ./include/ \
-library libs/libfiber_ffi_ios_sim.a \
-headers ./include/ \
-output Munchausen.xcframework
-library libs/libfiber_ffi_ios.a \
-headers ./include/ \
-output FiberFFI.xcframework
- name: Create zipped framework
run: zip -r framework.zip FiberFFI.xcframework

- run: zip -r framework.zip Munchausen.xcframework
- run: openssl dgst -sha256 framework.zip
- name: Generate checksum
run: openssl dgst -sha256 framework.zip

- name: Create Release
id: create_release
Expand All @@ -57,10 +70,10 @@ jobs:
asset_name: framework.zip
asset_content_type: application/zip

- name: Invoke workflow without inputs
uses: peter-evans/repository-dispatch@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
repository: szkabaroli/SwiftRusty
event-type: new-release
client-payload: '{"ref": "${{ github.ref }}", "sha": "${{ github.sha }}"}'
#- name: Invoke workflow without inputs
# uses: peter-evans/repository-dispatch@v3
# with:
# token: ${{ secrets.GITHUB_TOKEN }}
# repository: szkabaroli/SwiftRusty
# event-type: new-release
# client-payload: '{"ref": "${{ github.ref }}", "sha": "${{ github.sha }}"}'
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "munchausen"
name = "fiber_ffi"
version = "0.12.0"
authors = []

Expand Down
22 changes: 11 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ macos:
@cargo build --release --lib --target x86_64-apple-darwin
#@cargo +nightly build -Z build-std - release - lib - target aarch64-apple-ios-macabi
#@cargo +nightly build -Z build-std - release - lib - target x86_64-apple-ios-macabi
@$(RM) -rf libs/libmunchausen-macos.a
@$(RM) -rf libs/libfiber_ffi_macos.a
#@$(RM) -rf libs/libmunchausen-maccatalyst.a
@lipo -create -o libs/libmunchausen-macos.a \
target/aarch64-apple-darwin/release/libmunchausen.a \
target/x86_64-apple-darwin/release/libmunchausen.a
@lipo -create -o libs/libfiber_ffi_macos.a \
target/aarch64-apple-darwin/release/libfiber_ffi.a \
target/x86_64-apple-darwin/release/libfiber_ffi.a
#@lipo -create -output libs/libmunchausen-maccatalyst.a \
# target/aarch64-apple-ios-macabi/release/libmunchausen.a \
# target/x86_64-apple-ios-macabi/release/libmunchausen.a
ios:
@cargo build - release - lib - target aarch64-apple-ios
@cargo build - release - lib - target aarch64-apple-ios-sim
@cargo build - release - lib - target x86_64-apple-ios
@cargo build --release --lib --target aarch64-apple-ios
@cargo build --release --lib --target aarch64-apple-ios-sim
@cargo build --release --lib --target x86_64-apple-ios
@$(RM) -rf libs/libmunchausen-ios.a
@$(RM) -rf libs/libmunchausen-ios-sim.a
# @cp target/aarch64-apple-ios/release/libmunchausen.a libs/libmunchausen-ios.a
# @lipo -create -output libs/libmunchausen-ios-sim.a \
target/aarch64-apple-ios-sim/release/libmunchausen.a \
target/x86_64-apple-ios/release/libmunchausen.a
@cp target/aarch64-apple-ios/release/libfiber_ffi.a libs/libfiber_ffi_ios.a
@lipo -create -output libs/libfiber_ffi_ios_sim.a \
target/aarch64-apple-ios-sim/release/libfiber_ffi.a \
target/x86_64-apple-ios/release/libfiber_ffi.a
4 changes: 2 additions & 2 deletions include/module.modulemap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Munchausen {
header "munchausen.h"
module FiberFFI {
header "fiber_ffi.h"
export *
}
112 changes: 63 additions & 49 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,59 +1,73 @@
use std::ffi;

type Callback = unsafe extern "C" fn(data: *mut u8, len: usize) -> ffi::c_int;

#[no_mangle]
pub extern "C" fn test(cb: Callback) {
let mut vec = vec![1, 2, 3, 4];
vec.shrink_to_fit();
assert!(vec.len() == vec.capacity());
macro_rules! multiconst {
($typename:ident, [$($(#[$attr:meta])* $rawname:ident = $value:expr;)*]) => {
$(
$(#[$attr])*
pub const $rawname: $typename = $value;
)*
}
}

let res = unsafe { cb(vec.as_mut_ptr(), vec.len() as usize) };
#[allow(non_camel_case_types)]
pub type fx_handle_t = u32;
#[allow(non_camel_case_types)]
pub type fx_status_t = i32;
#[allow(non_camel_case_types)]
pub type fx_obj_type_t = u32;
#[allow(non_camel_case_types)]
pub type fx_rights_t = u32;

println!("{:?}", cb);
println!("{}", res);
#[repr(C)]
#[allow(non_camel_case_types)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct fx_handle_info_t {
pub handle: fx_handle_t,
pub ty: fx_obj_type_t,
pub rights: fx_rights_t,
pub unused: u32,
}

#[no_mangle]
pub extern "C" fn rust_munchausen_numbers() -> *mut [i32; 4] {
// Pre-caching the power for all of the digits; 0⁰ is initially in the cache array.
let mut cache = [0; 10];
let mut index = 0;
let mut munchausen_num: [i32; 4] = [0; 4];
let munchausen_num_ptr: *mut [i32; 4] = &mut munchausen_num;

for n in 1..=9 {
cache[n] = (n as i32).pow(n as u32);
}
multiconst!(fx_status_t, [
FX_OK = 0;
]);

// Searching for Munchausen numbers iterating through a long range containing all of them.
for n in 0..500000000 {
if is_munchausen_number(n, &cache) {
munchausen_num[index] = n;
index += 1;
}
}
multiconst!(fx_obj_type_t, [
FX_OBJ_TYPE_NONE = 0;
FX_OBJ_TYPE_PROCESS = 1;
FX_OBJ_TYPE_CHANNEL = 4;
FX_OBJ_TYPE_EVENT = 5;
FX_OBJ_TYPE_PORT = 6;
FX_OBJ_TYPE_JOB = 17;
]);

munchausen_num_ptr
#[no_mangle]
pub extern "C" fn fx_port_create(options: u32, out: *mut fx_handle_t) -> fx_status_t {
FX_OK
}

fn is_munchausen_number(number: i32, cache: &[i32; 10]) -> bool {
let mut current_number = number;
let mut sum = 0;

// The calculation details: Do until we go through all of the digits.
while current_number > 0 {
// Take the last digit of a number.
let digit = current_number % 10;
// Add the cached power of the digit to the overall sum.
sum += cache[digit as usize];

if sum > number {
return false;
}
// "Cut" the last digit
current_number /= 10;
}
#[no_mangle]
pub extern "C" fn fx_channel_read(
handle: fx_handle_t,
options: u32,
bytes: *mut u8,
handles: *mut fx_handle_t,
num_bytes: u32,
num_handles: u32,
actual_bytes: *mut u32,
actual_handles: *mut u32,
) -> fx_status_t {
FX_OK
}

number == sum
#[no_mangle]
pub extern "C" fn fx_channel_read_etc(
handle: fx_handle_t,
options: u32,
bytes: *mut u8,
handles: *mut fx_handle_info_t,
num_bytes: u32,
num_handles: u32,
actual_bytes: *mut u32,
actual_handles: *mut u32,
) -> fx_status_t {
FX_OK
}

0 comments on commit af093e9

Please sign in to comment.