Skip to content

Commit 9c7bb81

Browse files
committed
some changes
1 parent 5b2518e commit 9c7bb81

File tree

6 files changed

+49
-30
lines changed

6 files changed

+49
-30
lines changed

crates/byondapi-rs-test/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ fn test_block() {
126126
fn test_length_with_str(object: ByondValue) {
127127
setup_panic_handler();
128128

129-
Ok(byond_length(&object)?)
129+
Ok(object.builtin_length()?)
130130
}
131131
#[byondapi::bind]
132132
fn test_list_key_lookup(mut list: ByondValue) {
@@ -202,7 +202,7 @@ fn test_non_assoc_list(list: ByondValue) {
202202
fn test_list_read(list: ByondValue) {
203203
setup_panic_handler();
204204

205-
let map = list.get_list_keys()?;
205+
let map = list.get_list_values()?;
206206
let values = map
207207
.into_iter()
208208
.map(|item| item.get_string().unwrap())

crates/byondapi-rs/src/map.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,6 @@ pub fn byond_block(corner1: ByondXYZ, corner2: ByondXYZ) -> Result<Vec<ByondValu
7373
})
7474
}
7575

76-
/// Corresponds to [`dm::length`](https://www.byond.com/docs/ref/#/proc/length)
77-
/// Gives the length of a list or the length in bytes of a string.
78-
pub fn byond_length(src: &ByondValue) -> Result<ByondValue, Error> {
79-
let mut output = ByondValue::new();
80-
// Safety: src and output must be initialized, we take care of this.
81-
unsafe { map_byond_error!(byond().Byond_Length(&src.0, &mut output.0))? };
82-
Ok(output)
83-
}
84-
8576
/// Corresponds to the first variation of [`dm::locate(Type) in Container`](https://www.byond.com/docs/ref/#/proc/locate)
8677
/// Finds an object prototype or tag within the haystack, usually used for finding objects within a turf/area/etc
8778
pub fn byond_locatein(needle: &ByondValue, haystack: &ByondValue) -> Result<ByondValue, Error> {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use super::ByondValue;
2+
use crate::{static_global::byond, Error};
3+
4+
impl ByondValue {
5+
pub fn builtin_length(&self) -> Result<ByondValue, Error> {
6+
let mut result = ByondValue::new();
7+
unsafe {
8+
map_byond_error!(byond().Byond_Length(&self.0, &mut result.0))?;
9+
}
10+
Ok(result)
11+
}
12+
pub fn builtin_new(
13+
value_type: ByondValue,
14+
arglist: &[ByondValue],
15+
) -> Result<ByondValue, Error> {
16+
let mut result = ByondValue::new();
17+
unsafe {
18+
map_byond_error!(byond().Byond_New(
19+
&value_type.0,
20+
arglist.as_ptr().cast(),
21+
arglist.len() as u32,
22+
&mut result.0
23+
))?;
24+
}
25+
Ok(result)
26+
}
27+
28+
pub fn builtin_newarglist(
29+
value_type: ByondValue,
30+
arglist: ByondValue,
31+
) -> Result<ByondValue, Error> {
32+
if !arglist.is_list() {
33+
return Err(Error::NotAList(arglist));
34+
};
35+
let mut result = ByondValue::new();
36+
unsafe {
37+
map_byond_error!(byond().Byond_NewArglist(&value_type.0, &arglist.0, &mut result.0))?;
38+
}
39+
Ok(result)
40+
}
41+
}

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

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::ffi::CString;
33
use byondapi_sys::{u4c, ByondValueType, CByondValue};
44

55
use super::ByondValue;
6-
use crate::{map::byond_length, static_global::byond, Error};
6+
use crate::{static_global::byond, Error};
77

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

4037
thread_local! {
4138
static BUFFER: RefCell<Vec<u8>> = RefCell::new(Vec::with_capacity(1));
@@ -252,17 +249,6 @@ impl ByondValue {
252249
}
253250
}
254251

255-
/// # Builtins
256-
impl ByondValue {
257-
pub fn builtin_length(&self) -> Result<ByondValue, Error> {
258-
let mut result = ByondValue::new();
259-
unsafe {
260-
map_byond_error!(byond().Byond_Length(&self.0, &mut result.0))?;
261-
}
262-
Ok(result)
263-
}
264-
}
265-
266252
/// # Helpers
267253
impl ByondValue {
268254
/// Reads a number from a var. Fails if this isn't a ref type or this isn't a number.
@@ -302,7 +288,7 @@ impl ByondValue {
302288
if !self.is_list() {
303289
return Err(Error::NotAList(*self));
304290
}
305-
let len: f32 = byond_length(self)?.try_into()?;
291+
let len: f32 = self.builtin_length()?.try_into()?;
306292
Ok(ListIterator {
307293
value: self,
308294
len: len as u32,
@@ -315,7 +301,7 @@ impl ByondValue {
315301
if !self.is_list() {
316302
return Err(Error::NotAList(*self));
317303
}
318-
let len: f32 = byond_length(self)?.try_into()?;
304+
let len: f32 = self.builtin_length()?.try_into()?;
319305
Ok(ValueIterator {
320306
value: self,
321307
len: len as u32,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::{byond_string, static_global::byond, value::ByondValue, Error};
22
/// List stuff goes here, Keep in mind that all indexing method starts at zero instead of one like byondland
33
impl ByondValue {
4-
/// Gets an array of all the list keys, this means keys for assoc lists and values for regular lists
5-
pub fn get_list_keys(&self) -> Result<Vec<ByondValue>, Error> {
4+
/// Gets an array of all the list values, this means values for assoc lists and just items in the listfor regular lists
5+
pub fn get_list_values(&self) -> Result<Vec<ByondValue>, Error> {
66
use std::cell::RefCell;
77
if !self.is_list() {
88
return Err(Error::NotAList(*self));

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub struct ByondValue(pub CByondValue);
1212
/// It is safe to send ByondValue with ownership, but it is not safe to have references between threads.
1313
unsafe impl Send for ByondValue {}
1414

15+
pub mod builtins;
1516
pub mod constructors;
1617
pub mod conversion;
1718
pub mod functions;

0 commit comments

Comments
 (0)