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,5 +1,6 @@
use crate::ffi::{ExternPointer, OwnedPtr};
use crate::static_data::EffectParameter;
use crate::StringKey;
use std::ffi::{c_char, CStr, CString};
use std::ptr::drop_in_place;
@@ -8,32 +9,31 @@ mod form;
mod growth_rate;
mod item;
mod learnable_moves;
mod libraries;
mod move_data;
mod nature;
mod species;
mod statistic_set;
mod libraries;
#[no_mangle]
extern "C" fn effect_parameter_new_bool(value: u8) -> OwnedPtr<EffectParameter> {
Box::into_raw(Box::new(EffectParameter::Bool(value == 1)))
Box::into_raw(Box::new((value == 1).into()))
}
#[no_mangle]
extern "C" fn effect_parameter_new_int(value: i64) -> OwnedPtr<EffectParameter> {
Box::into_raw(Box::new(EffectParameter::Int(value)))
Box::into_raw(Box::new(value.into()))
}
#[no_mangle]
extern "C" fn effect_parameter_new_float(value: f32) -> OwnedPtr<EffectParameter> {
Box::into_raw(Box::new(EffectParameter::Float(value)))
Box::into_raw(Box::new(value.into()))
}
#[no_mangle]
unsafe extern "C" fn effect_parameter_new_string(value: *const c_char) -> OwnedPtr<EffectParameter> {
Box::into_raw(Box::new(EffectParameter::String(
CStr::from_ptr(value).to_str().unwrap().into(),
)))
let sk: StringKey = CStr::from_ptr(value).to_str().unwrap().into();
Box::into_raw(Box::new(sk.into()))
}
#[no_mangle]
@@ -44,17 +44,17 @@ unsafe extern "C" fn effect_parameter_drop(ptr: OwnedPtr<EffectParameter>) {
#[no_mangle]
extern "C" fn effect_parameter_get_type(ptr: ExternPointer<EffectParameter>) -> u8 {
match ptr.as_ref() {
EffectParameter::Bool(_) => 0,
EffectParameter::Int(_) => 1,
EffectParameter::Float(_) => 2,
EffectParameter::String(_) => 3,
EffectParameter::Bool(_, _) => 0,
EffectParameter::Int(_, _) => 1,
EffectParameter::Float(_, _) => 2,
EffectParameter::String(_, _) => 3,
}
}
#[no_mangle]
extern "C" fn effect_parameter_get_as_bool(ptr: ExternPointer<EffectParameter>) -> u8 {
let p = ptr.as_ref();
if let EffectParameter::Bool(b) = p {
if let EffectParameter::Bool(_, b) = p {
return if *b { 1 } else { 0 };
}
panic!("Unexpected effect parameter. Expected bool, was: {}", p);
@@ -63,7 +63,7 @@ extern "C" fn effect_parameter_get_as_bool(ptr: ExternPointer<EffectParameter>)
#[no_mangle]
extern "C" fn effect_parameter_get_as_int(ptr: ExternPointer<EffectParameter>) -> i64 {
let p = ptr.as_ref();
if let EffectParameter::Int(b) = p {
if let EffectParameter::Int(_, b) = p {
return *b;
}
panic!("Unexpected effect parameter. Expected int, was: {}", p);
@@ -72,7 +72,7 @@ extern "C" fn effect_parameter_get_as_int(ptr: ExternPointer<EffectParameter>) -
#[no_mangle]
extern "C" fn effect_parameter_get_as_float(ptr: ExternPointer<EffectParameter>) -> f32 {
let p = ptr.as_ref();
if let EffectParameter::Float(b) = p {
if let EffectParameter::Float(_, b) = p {
return *b;
}
panic!("Unexpected effect parameter. Expected float, was: {}", p);
@@ -81,7 +81,7 @@ extern "C" fn effect_parameter_get_as_float(ptr: ExternPointer<EffectParameter>)
#[no_mangle]
extern "C" fn effect_parameter_get_as_string(ptr: ExternPointer<EffectParameter>) -> OwnedPtr<c_char> {
let p = ptr.as_ref();
if let EffectParameter::String(b) = p {
if let EffectParameter::String(_, b) = p {
return CString::new(b.str().to_string()).unwrap().into_raw();
}
panic!("Unexpected effect parameter. Expected string, was: {}", p);