Skip to content

Commit 606f4ea

Browse files
authored
update to 1621 (#9)
* export this as well * global_ref * use oncelock to not overflow tls storage * some other changes * update to 1621
1 parent d8b203a commit 606f4ea

File tree

14 files changed

+670
-29
lines changed

14 files changed

+670
-29
lines changed

crates/byondapi-rs/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ walkdir = "2.4.0"
2020
inventory = "0.3.13"
2121

2222
[features]
23-
default = ["byond-515-1620"]
24-
byond-515-1620 = []
23+
default = ["byond-515-1621"]
24+
byond-515-1621 = []

crates/byondapi-rs/src/lib.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ pub use error::Error;
1010

1111
pub mod byond_string;
1212
pub mod global_call;
13-
pub mod list;
1413
pub mod prelude;
1514
pub mod typecheck_trait;
1615
pub mod value;
@@ -50,10 +49,15 @@ pub struct InitFunc(pub fn() -> ());
5049
#[macro_export]
5150
macro_rules! byond_string {
5251
($s:literal) => {{
53-
thread_local! {
54-
static STRING_ID: ::std::cell::OnceCell<u32> = ::std::cell::OnceCell::new();
55-
};
56-
STRING_ID
57-
.with(|cell| *cell.get_or_init(|| ::byondapi::byond_string::str_id_of($s).unwrap()))
52+
static STRING_ID: ::std::sync::OnceLock<u32> = ::std::sync::OnceLock::new();
53+
*STRING_ID.get_or_init(|| ::byondapi::byond_string::str_id_of($s).unwrap())
54+
}};
55+
}
56+
57+
#[macro_export]
58+
macro_rules! byond_string_internal {
59+
($s:literal) => {{
60+
static STRING_ID: ::std::sync::OnceLock<u32> = ::std::sync::OnceLock::new();
61+
*STRING_ID.get_or_init(|| crate::byond_string::str_id_of($s).unwrap())
5862
}};
5963
}

crates/byondapi-rs/src/map.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ pub fn byond_block(corner1: ByondXYZ, corner2: ByondXYZ) -> Result<Vec<ByondValu
5656
))?
5757
};
5858

59-
println!("len after second block is {len}");
6059
// Safety: buffer should be written to at this point
6160
unsafe { buff.set_len(len as usize) };
6261
Ok(std::mem::take(buff))

crates/byondapi-rs/src/prelude.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,5 @@ pub use byondapi_sys::ByondValueType as InternalByondValueType;
3737
pub use byondapi_sys::CByondValue as InternalByondValue;
3838

3939
// As well as our own types.
40+
pub use crate::byond_string;
4041
pub use crate::value::ByondValue;

crates/byondapi-rs/src/typecheck_trait.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ pub trait ByondTypeCheck {
1717
/// Check if this is a pointer.
1818
fn is_ptr(&self) -> bool;
1919
//// Check if this is true-ish.
20-
//fn is_true(&self) -> bool;
20+
fn is_true(&self) -> bool;
2121
}

crates/byondapi-rs/src/value/constructors.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ impl ByondValue {
4747
})
4848
}
4949

50+
pub fn new_global_ref() -> Self {
51+
Self(CByondValue {
52+
type_: 0x0E,
53+
junk1: 0,
54+
junk2: 0,
55+
junk3: 0,
56+
data: byondapi_sys::ByondValueData { ref_: 1 },
57+
})
58+
}
59+
5060
pub fn new_str<S: Into<Vec<u8>>>(s: S) -> Result<Self, Error> {
5161
let c_str = CString::new(s.into()).unwrap();
5262
let str_id = unsafe { byond().Byond_AddGetStrId(c_str.as_ptr()) };

crates/byondapi-rs/src/value/functions.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,22 @@ impl ByondValue {
235235
}
236236
}
237237

238-
/// # List operations by key instead of indices (why are they even here lumlum?????)
239-
impl ByondValue {}
238+
/// # Refcount operations
239+
impl ByondValue {
240+
pub fn increment_ref(&mut self) {
241+
unsafe { byond().ByondValue_IncRef(&self.0) }
242+
}
243+
244+
pub fn decrement_ref(&mut self) {
245+
unsafe { byond().ByondValue_DecRef(&self.0) }
246+
}
247+
248+
pub fn get_refcount(&self) -> Result<u32, Error> {
249+
let mut result = 0u32;
250+
unsafe { map_byond_error!(byond().Byond_Refcount(&self.0, &mut result))? };
251+
Ok(result)
252+
}
253+
}
240254

241255
/// # Builtins
242256
impl ByondValue {

crates/byondapi-rs/src/list.rs renamed to crates/byondapi-rs/src/value/list.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
use crate::{static_global::byond, typecheck_trait::ByondTypeCheck, value::ByondValue, Error};
1+
use crate::{
2+
byond_string_internal, static_global::byond, typecheck_trait::ByondTypeCheck,
3+
value::ByondValue, Error,
4+
};
25
/// List stuff goes here, Keep in mind that all indexing method starts at zero instead of one like byondland
36
impl ByondValue {
4-
/// Gets an array of all the list elements, this includes both keys and values for assoc lists, in an arbitrary order
7+
/// Gets an array of all the list elements, this means keys for assoc lists and values for regular lists
58
pub fn get_list(&self) -> Result<Vec<ByondValue>, Error> {
69
use std::cell::RefCell;
710
if !self.is_list() {
@@ -103,7 +106,7 @@ impl ByondValue {
103106
if !self.is_list() {
104107
return Err(Error::NotAList);
105108
}
106-
self.call("Add", &[value])?;
109+
self.call_id(byond_string_internal!("Add"), &[value])?;
107110
Ok(())
108111
}
109112

@@ -117,7 +120,7 @@ impl ByondValue {
117120
return Ok(None);
118121
}
119122
let value = self.read_list_index(len as f32)?;
120-
self.call("Remove", &[value])?;
123+
self.call_id(byond_string_internal!("Remove"), &[value])?;
121124
Ok(Some(value))
122125
}
123126
}

crates/byondapi-rs/src/value/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ unsafe impl Send for ByondValue {}
1515
pub mod constructors;
1616
pub mod conversion;
1717
pub mod functions;
18+
pub mod list;
1819
pub mod pointer;
1920
pub mod trait_impls;
2021

@@ -55,8 +56,8 @@ impl ByondTypeCheck for ByondValue {
5556
is_pointer_shim(self)
5657
}
5758

58-
// fn is_true(&self) -> bool {
59-
// // Safety: This operation only fails if our CByondValue is invalid, which cannot happen.
60-
// unsafe { byond().ByondValue_IsTrue(&self.0) }
61-
// }
59+
fn is_true(&self) -> bool {
60+
// Safety: This operation only fails if our CByondValue is invalid, which cannot happen.
61+
unsafe { byond().ByondValue_IsTrue(&self.0) }
62+
}
6263
}

crates/byondapi-sys/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ rustc_version = "0.4.0"
2525
walkdir = "2.4.0"
2626

2727
[features]
28-
default = ["byond-515-1620"]
29-
byond-515-1620 = []
28+
default = ["byond-515-1621"]
29+
byond-515-1621 = []

0 commit comments

Comments
 (0)