Skip to content

Commit

Permalink
some changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jupyterkat committed May 9, 2024
1 parent 5b2518e commit 9c7bb81
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 30 deletions.
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 @@ -126,7 +126,7 @@ fn test_block() {
fn test_length_with_str(object: ByondValue) {
setup_panic_handler();

Ok(byond_length(&object)?)
Ok(object.builtin_length()?)
}
#[byondapi::bind]
fn test_list_key_lookup(mut list: ByondValue) {
Expand Down Expand Up @@ -202,7 +202,7 @@ fn test_non_assoc_list(list: ByondValue) {
fn test_list_read(list: ByondValue) {
setup_panic_handler();

let map = list.get_list_keys()?;
let map = list.get_list_values()?;
let values = map
.into_iter()
.map(|item| item.get_string().unwrap())
Expand Down
9 changes: 0 additions & 9 deletions crates/byondapi-rs/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,6 @@ pub fn byond_block(corner1: ByondXYZ, corner2: ByondXYZ) -> Result<Vec<ByondValu
})
}

/// Corresponds to [`dm::length`](https://www.byond.com/docs/ref/#/proc/length)
/// Gives the length of a list or the length in bytes of a string.
pub fn byond_length(src: &ByondValue) -> Result<ByondValue, Error> {
let mut output = ByondValue::new();
// Safety: src and output must be initialized, we take care of this.
unsafe { map_byond_error!(byond().Byond_Length(&src.0, &mut output.0))? };
Ok(output)
}

/// Corresponds to the first variation of [`dm::locate(Type) in Container`](https://www.byond.com/docs/ref/#/proc/locate)
/// Finds an object prototype or tag within the haystack, usually used for finding objects within a turf/area/etc
pub fn byond_locatein(needle: &ByondValue, haystack: &ByondValue) -> Result<ByondValue, Error> {
Expand Down
41 changes: 41 additions & 0 deletions crates/byondapi-rs/src/value/builtins.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use super::ByondValue;
use crate::{static_global::byond, Error};

impl ByondValue {
pub fn builtin_length(&self) -> Result<ByondValue, Error> {
let mut result = ByondValue::new();
unsafe {
map_byond_error!(byond().Byond_Length(&self.0, &mut result.0))?;
}
Ok(result)
}
pub fn builtin_new(
value_type: ByondValue,
arglist: &[ByondValue],
) -> Result<ByondValue, Error> {
let mut result = ByondValue::new();
unsafe {
map_byond_error!(byond().Byond_New(
&value_type.0,
arglist.as_ptr().cast(),
arglist.len() as u32,
&mut result.0
))?;
}
Ok(result)
}

pub fn builtin_newarglist(
value_type: ByondValue,
arglist: ByondValue,
) -> Result<ByondValue, Error> {
if !arglist.is_list() {
return Err(Error::NotAList(arglist));
};
let mut result = ByondValue::new();
unsafe {
map_byond_error!(byond().Byond_NewArglist(&value_type.0, &arglist.0, &mut result.0))?;
}
Ok(result)
}
}
20 changes: 3 additions & 17 deletions crates/byondapi-rs/src/value/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::ffi::CString;
use byondapi_sys::{u4c, ByondValueType, CByondValue};

use super::ByondValue;
use crate::{map::byond_length, static_global::byond, Error};
use crate::{static_global::byond, Error};

/// # Compatibility with the C++ API
impl ByondValue {
Expand Down Expand Up @@ -33,9 +33,6 @@ impl ByondValue {
/// Try to get a [`CString`] or fail if this isn't a string type
pub fn get_cstring(&self) -> Result<CString, Error> {
use std::cell::RefCell;
if !self.is_str() {
return Err(Error::NotAString(*self));
}

thread_local! {
static BUFFER: RefCell<Vec<u8>> = RefCell::new(Vec::with_capacity(1));
Expand Down Expand Up @@ -252,17 +249,6 @@ impl ByondValue {
}
}

/// # Builtins
impl ByondValue {
pub fn builtin_length(&self) -> Result<ByondValue, Error> {
let mut result = ByondValue::new();
unsafe {
map_byond_error!(byond().Byond_Length(&self.0, &mut result.0))?;
}
Ok(result)
}
}

/// # Helpers
impl ByondValue {
/// Reads a number from a var. Fails if this isn't a ref type or this isn't a number.
Expand Down Expand Up @@ -302,7 +288,7 @@ impl ByondValue {
if !self.is_list() {
return Err(Error::NotAList(*self));
}
let len: f32 = byond_length(self)?.try_into()?;
let len: f32 = self.builtin_length()?.try_into()?;
Ok(ListIterator {
value: self,
len: len as u32,
Expand All @@ -315,7 +301,7 @@ impl ByondValue {
if !self.is_list() {
return Err(Error::NotAList(*self));
}
let len: f32 = byond_length(self)?.try_into()?;
let len: f32 = self.builtin_length()?.try_into()?;
Ok(ValueIterator {
value: self,
len: len as u32,
Expand Down
4 changes: 2 additions & 2 deletions crates/byondapi-rs/src/value/list.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{byond_string, static_global::byond, 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 keys, this means keys for assoc lists and values for regular lists
pub fn get_list_keys(&self) -> Result<Vec<ByondValue>, Error> {
/// Gets an array of all the list values, this means values for assoc lists and just items in the listfor regular lists
pub fn get_list_values(&self) -> Result<Vec<ByondValue>, Error> {
use std::cell::RefCell;
if !self.is_list() {
return Err(Error::NotAList(*self));
Expand Down
1 change: 1 addition & 0 deletions crates/byondapi-rs/src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct ByondValue(pub CByondValue);
/// It is safe to send ByondValue with ownership, but it is not safe to have references between threads.
unsafe impl Send for ByondValue {}

pub mod builtins;
pub mod constructors;
pub mod conversion;
pub mod functions;
Expand Down

0 comments on commit 9c7bb81

Please sign in to comment.