Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update to 1621 #9

Merged
merged 5 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/byondapi-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ walkdir = "2.4.0"
inventory = "0.3.13"

[features]
default = ["byond-515-1620"]
byond-515-1620 = []
default = ["byond-515-1621"]
byond-515-1621 = []
16 changes: 10 additions & 6 deletions crates/byondapi-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pub use error::Error;

pub mod byond_string;
pub mod global_call;
pub mod list;
pub mod prelude;
pub mod typecheck_trait;
pub mod value;
Expand Down Expand Up @@ -50,10 +49,15 @@ pub struct InitFunc(pub fn() -> ());
#[macro_export]
macro_rules! byond_string {
($s:literal) => {{
thread_local! {
static STRING_ID: ::std::cell::OnceCell<u32> = ::std::cell::OnceCell::new();
};
STRING_ID
.with(|cell| *cell.get_or_init(|| ::byondapi::byond_string::str_id_of($s).unwrap()))
static STRING_ID: ::std::sync::OnceLock<u32> = ::std::sync::OnceLock::new();
*STRING_ID.get_or_init(|| ::byondapi::byond_string::str_id_of($s).unwrap())
}};
}

#[macro_export]
macro_rules! byond_string_internal {
($s:literal) => {{
static STRING_ID: ::std::sync::OnceLock<u32> = ::std::sync::OnceLock::new();
*STRING_ID.get_or_init(|| crate::byond_string::str_id_of($s).unwrap())
}};
}
1 change: 0 additions & 1 deletion crates/byondapi-rs/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ pub fn byond_block(corner1: ByondXYZ, corner2: ByondXYZ) -> Result<Vec<ByondValu
))?
};

println!("len after second block is {len}");
// Safety: buffer should be written to at this point
unsafe { buff.set_len(len as usize) };
Ok(std::mem::take(buff))
Expand Down
1 change: 1 addition & 0 deletions crates/byondapi-rs/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ pub use byondapi_sys::ByondValueType as InternalByondValueType;
pub use byondapi_sys::CByondValue as InternalByondValue;

// As well as our own types.
pub use crate::byond_string;
pub use crate::value::ByondValue;
2 changes: 1 addition & 1 deletion crates/byondapi-rs/src/typecheck_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ pub trait ByondTypeCheck {
/// Check if this is a pointer.
fn is_ptr(&self) -> bool;
//// Check if this is true-ish.
//fn is_true(&self) -> bool;
fn is_true(&self) -> bool;
}
10 changes: 10 additions & 0 deletions crates/byondapi-rs/src/value/constructors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ impl ByondValue {
})
}

pub fn new_global_ref() -> Self {
Self(CByondValue {
type_: 0x0E,
junk1: 0,
junk2: 0,
junk3: 0,
data: byondapi_sys::ByondValueData { ref_: 1 },
})
}

pub fn new_str<S: Into<Vec<u8>>>(s: S) -> Result<Self, Error> {
let c_str = CString::new(s.into()).unwrap();
let str_id = unsafe { byond().Byond_AddGetStrId(c_str.as_ptr()) };
Expand Down
18 changes: 16 additions & 2 deletions crates/byondapi-rs/src/value/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,22 @@ impl ByondValue {
}
}

/// # List operations by key instead of indices (why are they even here lumlum?????)
impl ByondValue {}
/// # Refcount operations
impl ByondValue {
pub fn increment_ref(&mut self) {
unsafe { byond().ByondValue_IncRef(&self.0) }
}

pub fn decrement_ref(&mut self) {
unsafe { byond().ByondValue_DecRef(&self.0) }
}

pub fn get_refcount(&self) -> Result<u32, Error> {
let mut result = 0u32;
unsafe { map_byond_error!(byond().Byond_Refcount(&self.0, &mut result))? };
Ok(result)
}
}

/// # Builtins
impl ByondValue {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::{static_global::byond, typecheck_trait::ByondTypeCheck, value::ByondValue, Error};
use crate::{
byond_string_internal, static_global::byond, typecheck_trait::ByondTypeCheck,
value::ByondValue, Error,
};
/// List stuff goes here, Keep in mind that all indexing method starts at zero instead of one like byondland
impl ByondValue {
/// Gets an array of all the list elements, this includes both keys and values for assoc lists, in an arbitrary order
/// Gets an array of all the list elements, this means keys for assoc lists and values for regular lists
pub fn get_list(&self) -> Result<Vec<ByondValue>, Error> {
use std::cell::RefCell;
if !self.is_list() {
Expand Down Expand Up @@ -103,7 +106,7 @@ impl ByondValue {
if !self.is_list() {
return Err(Error::NotAList);
}
self.call("Add", &[value])?;
self.call_id(byond_string_internal!("Add"), &[value])?;
Ok(())
}

Expand All @@ -117,7 +120,7 @@ impl ByondValue {
return Ok(None);
}
let value = self.read_list_index(len as f32)?;
self.call("Remove", &[value])?;
self.call_id(byond_string_internal!("Remove"), &[value])?;
Ok(Some(value))
}
}
9 changes: 5 additions & 4 deletions crates/byondapi-rs/src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ unsafe impl Send for ByondValue {}
pub mod constructors;
pub mod conversion;
pub mod functions;
pub mod list;
pub mod pointer;
pub mod trait_impls;

Expand Down Expand Up @@ -55,8 +56,8 @@ impl ByondTypeCheck for ByondValue {
is_pointer_shim(self)
}

// fn is_true(&self) -> bool {
// // Safety: This operation only fails if our CByondValue is invalid, which cannot happen.
// unsafe { byond().ByondValue_IsTrue(&self.0) }
// }
fn is_true(&self) -> bool {
// Safety: This operation only fails if our CByondValue is invalid, which cannot happen.
unsafe { byond().ByondValue_IsTrue(&self.0) }
}
}
4 changes: 2 additions & 2 deletions crates/byondapi-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ rustc_version = "0.4.0"
walkdir = "2.4.0"

[features]
default = ["byond-515-1620"]
byond-515-1620 = []
default = ["byond-515-1621"]
byond-515-1621 = []
Loading
Loading