Rework of FFI, adding a value identifier, so we can keep knowledge of data even when data moves.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-10-08 13:15:04 +02:00
parent 84ddf0307d
commit 41b40ef98e
38 changed files with 582 additions and 230 deletions

View File

@@ -1,12 +1,12 @@
use crate::defines::LevelInt;
use crate::ffi::{BorrowedPtr, ExternPointer, OwnedPtr};
use crate::ffi::{BorrowedPtr, ExternPointer, IdentifiablePointer, OwnedPtr};
use crate::static_data::{GrowthRate, GrowthRateLibrary};
use std::ffi::{c_char, CStr};
use std::ptr::drop_in_place;
#[no_mangle]
extern "C" fn growth_rate_library_new(capacity: usize) -> OwnedPtr<GrowthRateLibrary> {
Box::into_raw(Box::new(GrowthRateLibrary::new(capacity)))
extern "C" fn growth_rate_library_new(capacity: usize) -> IdentifiablePointer<GrowthRateLibrary> {
Box::new(GrowthRateLibrary::new(capacity)).into()
}
#[no_mangle]

View File

@@ -1,11 +1,11 @@
use crate::defines::LevelInt;
use crate::ffi::{ExternPointer, OwnedPtr};
use crate::ffi::{ExternPointer, IdentifiablePointer, OwnedPtr};
use crate::static_data::LibrarySettings;
use std::ptr::drop_in_place;
#[no_mangle]
extern "C" fn library_settings_new(max_level: LevelInt) -> OwnedPtr<LibrarySettings> {
Box::into_raw(Box::new(LibrarySettings::new(max_level)))
extern "C" fn library_settings_new(max_level: LevelInt) -> IdentifiablePointer<LibrarySettings> {
Box::new(LibrarySettings::new(max_level)).into()
}
#[no_mangle]

View File

@@ -4,7 +4,7 @@ mod nature_library;
mod static_data;
mod type_library;
use crate::ffi::{BorrowedPtr, OwnedPtr};
use crate::ffi::{BorrowedPtr, IdentifiablePointer, OwnedPtr};
use crate::static_data::*;
use std::ffi::{c_char, CStr};
use std::ptr::drop_in_place;
@@ -14,8 +14,8 @@ macro_rules! library_interface {
($library_type:ty, $return_type:ty) => {
paste::paste! {
#[no_mangle]
extern "C" fn [< $library_type:snake _new >](capacity: usize) -> OwnedPtr<$library_type> {
Box::into_raw(Box::new($library_type::new(capacity)))
extern "C" fn [< $library_type:snake _new >](capacity: usize) -> IdentifiablePointer<$library_type> {
Box::new($library_type::new(capacity)).into()
}
#[no_mangle]
@@ -24,7 +24,7 @@ macro_rules! library_interface {
}
#[no_mangle]
unsafe extern "C" fn [< $library_type:snake _add >](ptr: OwnedPtr<$library_type>, key: BorrowedPtr<c_char>, value: OwnedPtr<$return_type>) {
unsafe extern "C" fn [< $library_type:snake _add >](ptr: OwnedPtr<$library_type>, key: BorrowedPtr<c_char>, value: OwnedPtr<Arc<$return_type>>) {
let lib = ptr.as_mut().unwrap();
lib.add(&CStr::from_ptr(key).into(), *Box::from_raw(value));
}
@@ -36,13 +36,13 @@ macro_rules! library_interface {
}
#[no_mangle]
unsafe extern "C" fn [< $library_type:snake _get >](ptr: OwnedPtr<$library_type>, key: BorrowedPtr<c_char>) -> BorrowedPtr<$return_type> {
unsafe extern "C" fn [< $library_type:snake _get >](ptr: OwnedPtr<$library_type>, key: BorrowedPtr<c_char>) -> IdentifiablePointer<Arc<$return_type>> {
let lib = ptr.as_mut().unwrap();
let v = lib.get(&CStr::from_ptr(key).into());
if let Some(value) = v {
Arc::as_ptr(value)
value.clone().into()
} else {
std::ptr::null()
IdentifiablePointer::none()
}
}

View File

@@ -1,12 +1,12 @@
use crate::ffi::{BorrowedPtr, ExternPointer, OwnedPtr};
use crate::ffi::{BorrowedPtr, ExternPointer, IdentifiablePointer, OwnedPtr};
use crate::static_data::{Nature, NatureLibrary};
use std::ffi::{c_char, CStr, CString};
use std::ptr::drop_in_place;
use std::sync::Arc;
#[no_mangle]
extern "C" fn nature_library_new(capacity: usize) -> OwnedPtr<NatureLibrary> {
Box::into_raw(Box::new(NatureLibrary::new(capacity)))
extern "C" fn nature_library_new(capacity: usize) -> IdentifiablePointer<NatureLibrary> {
Box::new(NatureLibrary::new(capacity)).into()
}
#[no_mangle]
@@ -18,31 +18,30 @@ unsafe extern "C" fn nature_library_drop(ptr: OwnedPtr<NatureLibrary>) {
unsafe extern "C" fn nature_library_load_nature(
ptr: ExternPointer<NatureLibrary>,
name: BorrowedPtr<c_char>,
nature: OwnedPtr<Nature>,
nature: OwnedPtr<Arc<Nature>>,
) {
ptr.as_mut()
.load_nature(CStr::from_ptr(name).into(), *Box::from_raw(nature))
.load_nature(CStr::from_ptr(name).into(), nature.as_ref().unwrap().clone())
}
#[no_mangle]
unsafe extern "C" fn nature_library_get_nature(
ptr: ExternPointer<NatureLibrary>,
name: BorrowedPtr<c_char>,
) -> BorrowedPtr<Nature> {
) -> IdentifiablePointer<Arc<Nature>> {
if let Some(nature) = ptr.as_ref().get_nature(&CStr::from_ptr(name).into()) {
Arc::into_raw(nature.clone())
nature.clone().into()
} else {
std::ptr::null()
IdentifiablePointer::none()
}
}
#[no_mangle]
unsafe extern "C" fn nature_library_get_nature_name(
ptr: ExternPointer<NatureLibrary>,
nature: BorrowedPtr<Nature>,
nature: BorrowedPtr<Arc<Nature>>,
) -> OwnedPtr<c_char> {
let arc = Arc::from_raw(nature);
CString::new(ptr.as_ref().get_nature_name(&arc).str())
CString::new(ptr.as_ref().get_nature_name(nature.as_ref().unwrap()).str())
.unwrap()
.into_raw()
}

View File

@@ -1,4 +1,4 @@
use crate::ffi::{BorrowedPtr, ExternPointer, OwnedPtr};
use crate::ffi::{ExternPointer, IdentifiablePointer, OwnedPtr};
use crate::static_data::{
AbilityLibrary, GrowthRateLibrary, ItemLibrary, LibrarySettings, MoveLibrary, NatureLibrary, SpeciesLibrary,
StaticData, TypeLibrary,
@@ -6,8 +6,8 @@ use crate::static_data::{
use std::ptr::drop_in_place;
#[no_mangle]
unsafe extern "C" fn static_data_new(settings: OwnedPtr<LibrarySettings>) -> OwnedPtr<StaticData> {
Box::into_raw(Box::new(StaticData::new(*Box::from_raw(settings))))
unsafe extern "C" fn static_data_new(settings: OwnedPtr<LibrarySettings>) -> IdentifiablePointer<StaticData> {
Box::new(StaticData::new(*Box::from_raw(settings))).into()
}
#[no_mangle]
@@ -16,41 +16,43 @@ unsafe extern "C" fn static_data_drop(ptr: OwnedPtr<StaticData>) {
}
#[no_mangle]
unsafe extern "C" fn static_data_settings(data: ExternPointer<StaticData>) -> BorrowedPtr<LibrarySettings> {
data.as_mut().settings() as *const LibrarySettings
unsafe extern "C" fn static_data_settings(data: ExternPointer<StaticData>) -> IdentifiablePointer<LibrarySettings> {
(data.as_mut().settings() as *const LibrarySettings).into()
}
#[no_mangle]
unsafe extern "C" fn static_data_species(data: ExternPointer<StaticData>) -> BorrowedPtr<SpeciesLibrary> {
data.as_mut().species_mut() as *mut SpeciesLibrary
unsafe extern "C" fn static_data_species(data: ExternPointer<StaticData>) -> IdentifiablePointer<SpeciesLibrary> {
(data.as_mut().species_mut() as *const SpeciesLibrary).into()
}
#[no_mangle]
unsafe extern "C" fn static_data_moves(data: ExternPointer<StaticData>) -> BorrowedPtr<MoveLibrary> {
data.as_mut().moves_mut() as *mut MoveLibrary
unsafe extern "C" fn static_data_moves(data: ExternPointer<StaticData>) -> IdentifiablePointer<MoveLibrary> {
(data.as_mut().moves_mut() as *const MoveLibrary).into()
}
#[no_mangle]
unsafe extern "C" fn static_data_items(data: ExternPointer<StaticData>) -> BorrowedPtr<ItemLibrary> {
data.as_mut().items_mut() as *mut ItemLibrary
unsafe extern "C" fn static_data_items(data: ExternPointer<StaticData>) -> IdentifiablePointer<ItemLibrary> {
(data.as_mut().items_mut() as *const ItemLibrary).into()
}
#[no_mangle]
unsafe extern "C" fn static_data_growth_rates(data: ExternPointer<StaticData>) -> BorrowedPtr<GrowthRateLibrary> {
data.as_mut().growth_rates_mut() as *mut GrowthRateLibrary
unsafe extern "C" fn static_data_growth_rates(
data: ExternPointer<StaticData>,
) -> IdentifiablePointer<GrowthRateLibrary> {
(data.as_mut().growth_rates_mut() as *const GrowthRateLibrary).into()
}
#[no_mangle]
unsafe extern "C" fn static_data_types(data: ExternPointer<StaticData>) -> BorrowedPtr<TypeLibrary> {
data.as_mut().types_mut() as *mut TypeLibrary
unsafe extern "C" fn static_data_types(data: ExternPointer<StaticData>) -> IdentifiablePointer<TypeLibrary> {
(data.as_mut().types_mut() as *const TypeLibrary).into()
}
#[no_mangle]
unsafe extern "C" fn static_data_natures(data: ExternPointer<StaticData>) -> BorrowedPtr<NatureLibrary> {
data.as_mut().natures_mut() as *mut NatureLibrary
unsafe extern "C" fn static_data_natures(data: ExternPointer<StaticData>) -> IdentifiablePointer<NatureLibrary> {
(data.as_mut().natures_mut() as *const NatureLibrary).into()
}
#[no_mangle]
unsafe extern "C" fn static_data_abilities(data: ExternPointer<StaticData>) -> BorrowedPtr<AbilityLibrary> {
data.as_mut().abilities_mut() as *mut AbilityLibrary
unsafe extern "C" fn static_data_abilities(data: ExternPointer<StaticData>) -> IdentifiablePointer<AbilityLibrary> {
(data.as_mut().abilities_mut() as *const AbilityLibrary).into()
}

View File

@@ -1,11 +1,11 @@
use crate::ffi::{BorrowedPtr, ExternPointer, OwnedPtr};
use crate::ffi::{BorrowedPtr, ExternPointer, IdentifiablePointer, OwnedPtr};
use crate::static_data::{TypeIdentifier, TypeLibrary};
use std::ffi::{c_char, CStr, CString};
use std::ptr::drop_in_place;
#[no_mangle]
extern "C" fn type_library_new(capacity: usize) -> OwnedPtr<TypeLibrary> {
Box::into_raw(Box::new(TypeLibrary::new(capacity)))
extern "C" fn type_library_new(capacity: usize) -> IdentifiablePointer<TypeLibrary> {
Box::new(TypeLibrary::new(capacity)).into()
}
#[no_mangle]