Skip to content

Commit

Permalink
no uninit
Browse files Browse the repository at this point in the history
  • Loading branch information
jupyterkat committed Oct 20, 2023
1 parent b6bf018 commit 2971228
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 27 deletions.
10 changes: 6 additions & 4 deletions crates/byondapi-rs-test/dm_project/dm_project.dme
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@
if(O.name != ret)
throw EXCEPTION("Call proc failed, expected rust to return 'test name' but got '[ret]'")

/datum/data
var/name = "test name"

/test/proc/test_readwrite_var()
var/obj/O = new()
O.name = "test name"
var/datum/data/stub = new()

var/ret = call_ext("byondapi_test.dll", "byond:test_readwrite_var")(O)
var/ret = call_ext("byondapi_test.dll", "byond:test_readwrite_var")(stub)

if(O.name != ret)
if(stub.name != ret)
throw EXCEPTION("Call proc failed, expected rust to return 'test name' but got '[ret]'")

/test/proc/test_list_push()
Expand Down
4 changes: 2 additions & 2 deletions crates/byondapi-rs-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ pub unsafe extern "C" fn test_readwrite_var(
let args = parse_args(argc, argv);
let object = &args[0];

match object.read_var("name") {
Ok(res) => res,
match object.read_string("name") {
Ok(res) => res.try_into().unwrap(),
Err(e) => format!("{:#?}", e).try_into().unwrap(),
}
}
Expand Down
1 change: 0 additions & 1 deletion crates/byondapi-rs-test/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use std::{
use tempfile::TempDir;

#[test]
#[ignore]
#[cfg(windows)]
fn test_byondapi_with_dreamdaemon() {
let dll = build_dylib();
Expand Down
5 changes: 1 addition & 4 deletions crates/byondapi-rs/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub enum Error {
UnknownByondError,
/// Thrown by us when we know this call will panic internally because of the version
NotAvailableForThisByondVersion,
/// Thrown by us when we know this type does not have a ref
/// Thrown by us when we know this type does not have a refnumber
NotReferencable,
/// Thrown by us when we know this type is not a list, and we're expecting one
NotAList,
Expand All @@ -27,8 +27,6 @@ pub enum Error {
NotANum,
/// Thrown by us when we know this type is not a pointer, and we're expecting one
NotAPtr,
/// Thrown by us when we know this type is not a ref, and we're expecting one
NotARef,
/// Thrown by [`crate::byond_string::str_id_of_cstr`] when the string doesn't exist in
/// byondland
NonExistentString,
Expand Down Expand Up @@ -61,7 +59,6 @@ impl std::fmt::Display for Error {
Self::NotAString => write!(f, "Value is not a string"),
Self::NotANum => write!(f, "Value is not a number"),
Self::NotAPtr => write!(f, "Value is not a pointer"),
Self::NotARef => write!(f, "Value is not a ref"),
Self::NonExistentString => write!(f, "String id not found"),
}
}
Expand Down
10 changes: 2 additions & 8 deletions crates/byondapi-rs/src/value/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,7 @@ impl ByondValue {

/// Try to get a [`CString`] or fail if this isn't a string type
pub fn get_cstring(&self) -> Result<CString, Error> {
if self.is_str() {
let ptr = unsafe { byond().ByondValue_GetStr(&self.0) };
let cstr = unsafe { CStr::from_ptr(ptr) };
Ok(cstr.to_owned())
} else {
Err(Error::NotAString)
}
self.get_cstr().map(|cstr| cstr.to_owned())
}

/// Try to get a [`CStr`] or fail if this isn't a string type
Expand Down Expand Up @@ -116,7 +110,7 @@ impl ByondValue {
/// Read a variable through the ref. Fails if this isn't a ref type.
pub fn read_var<T: Into<Vec<u8>>>(&self, name: T) -> Result<ByondValue, Error> {
if self.is_num() || self.is_str() || self.is_ptr() || self.is_null() || self.is_list() {
return Err(Error::NotARef);
return Err(Error::NotReferencable);
}
let c_string = CString::new(name).unwrap();
let c_str = c_string.as_c_str();
Expand Down
13 changes: 5 additions & 8 deletions crates/byondapi-rs/src/value/trait_impls.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
use super::ByondValue;
use crate::static_global::byond;
use std::{fmt::Debug, mem::MaybeUninit};
use std::fmt::Debug;

// Memory handling
impl Clone for ByondValue {
fn clone(&self) -> Self {
let mut new_inner = MaybeUninit::uninit();
let mut cloned_value = ByondValue::null();

let new_inner = unsafe {
// Safety: new_inner is going to an initialization function, it will only write to the pointer.
byond().ByondValue_CopyFrom(new_inner.as_mut_ptr(), &self.0);
// Safety: ByondValue_Init will have initialized the new_inner.
new_inner.assume_init()
unsafe {
byond().ByondValue_CopyFrom(&mut cloned_value.0, &self.0);
};

Self(new_inner)
cloned_value
}
}

Expand Down

0 comments on commit 2971228

Please sign in to comment.