Fixes for interop with host

This commit is contained in:
2022-09-16 11:01:17 +02:00
parent 3c8bd5f7c5
commit f3f5b2acb0
9 changed files with 29 additions and 36 deletions

View File

@@ -49,7 +49,7 @@ impl BattleSide {
pub fn has_volatile(&self, script_name: &str) -> bool {
unsafe {
let script_name = CString::new(script_name).unwrap();
battleside_has_volatile(self.inner.reference, script_name.into_raw())
battleside_has_volatile(self.inner.reference, script_name.as_ptr())
}
}
@@ -66,7 +66,7 @@ impl BattleSide {
pub fn remove_volatile(&self, script: &dyn Script) {
unsafe {
let name = CString::new(script.get_name()).unwrap();
battleside_remove_volatile(self.inner.reference, name.into_raw());
battleside_remove_volatile(self.inner.reference, name.as_ptr());
}
}
@@ -77,7 +77,7 @@ impl BattleSide {
{
unsafe {
let script_name = CString::new(script_name).unwrap();
let s = battleside_get_volatile(self.inner.reference, script_name.into_raw()).val();
let s = battleside_get_volatile(self.inner.reference, script_name.as_ptr()).val();
if let Some(s) = s {
Some(s.as_any().downcast_ref().unwrap())
} else {

View File

@@ -207,7 +207,7 @@ impl Pokemon {
pub fn add_volatile_by_name(&self, script_name: &str) -> &dyn Script {
unsafe {
let ptr = CString::new(script_name).unwrap();
pokemon_add_volatile_by_name(self.inner.reference, ptr.into_raw())
pokemon_add_volatile_by_name(self.inner.reference, ptr.as_ptr())
.val()
.unwrap()
}
@@ -217,7 +217,7 @@ impl Pokemon {
pub fn remove_volatile(&self, script: &dyn Script) {
unsafe {
let name = CString::new(script.get_name()).unwrap();
pokemon_remove_volatile(self.inner.reference, name.into_raw());
pokemon_remove_volatile(self.inner.reference, name.as_ptr());
}
}
@@ -228,7 +228,7 @@ impl Pokemon {
{
unsafe {
let script_name = CString::new(script_name).unwrap();
let s = pokemon_get_volatile(self.inner.reference, script_name.into_raw()).val();
let s = pokemon_get_volatile(self.inner.reference, script_name.as_ptr()).val();
if let Some(s) = s {
Some(s.as_any().downcast_ref().unwrap())
} else {

View File

@@ -18,6 +18,7 @@ pub enum ScriptCategory {
Status,
Pokemon,
Battle,
Weather,
Side,
ItemBattleTrigger,
}

View File

@@ -2,35 +2,28 @@ use alloc::alloc::alloc;
use core::alloc::Layout;
#[cfg(not(feature = "mock_data"))]
use core::panic::PanicInfo;
use cstr_core::c_char;
#[cfg(feature = "mock_data")]
use cstr_core::{CStr, CString};
use cstr_core::CStr;
use cstr_core::{c_char, CString};
#[cfg(not(feature = "mock_data"))]
#[cfg(not(feature = "mock_data"))]
extern "wasm" {
fn _print(s: *const u8, len: usize);
fn _error(
message: *const u8,
message_len: usize,
file: *const u8,
file_len: usize,
line: u32,
position: u32,
);
fn _print(s: *const u8);
fn _error(message: *const u8, file: *const u8, line: u32, position: u32);
}
#[cfg(not(feature = "mock_data"))]
pub fn print_raw(s: &[c_char]) {
pub fn print_raw(s: CString) {
unsafe {
_print(s.as_ptr(), s.len());
_print(s.as_ptr());
}
}
#[cfg(feature = "mock_data")]
pub fn print_raw(s: &[u8]) {
pub fn print_raw(s: CString) {
unsafe {
println!("{}", CString::new(s).unwrap().into_string().unwrap());
println!("{}", s.into_string().unwrap());
}
}
@@ -52,25 +45,18 @@ macro_rules! dbg {
#[cfg(not(feature = "mock_data"))]
#[cfg(not(test))]
pub fn begin_panic_handler(panic_info: &PanicInfo<'_>) -> ! {
let msg = panic_info.message().unwrap().as_str().unwrap();
let msg = CString::new(panic_info.message().unwrap().as_str().unwrap()).unwrap();
let mut line = 0;
let mut position = 0;
let mut file = "";
let mut file = CString::default();
if let Some(s) = panic_info.location() {
line = s.line();
position = s.column();
file = s.file();
file = CString::new(s.file()).unwrap();
}
unsafe {
_error(
msg.as_ptr(),
msg.len(),
file.as_ptr(),
file.len(),
line,
position,
);
_error(msg.as_ptr(), file.as_ptr(), line, position);
}
loop {}
}
@@ -88,3 +74,9 @@ fn allocation_error_handler(layout: core::alloc::Layout) -> ! {
unsafe extern "wasm" fn allocate_mem(len: u32, align: u32) -> *mut u8 {
alloc(Layout::from_size_align(len as usize, align as usize).unwrap())
}
#[no_mangle]
#[cfg(not(feature = "mock_data"))]
unsafe extern "wasm" fn dealloc_cstring(ptr: *mut c_char) {
CString::from_raw(ptr);
}