From aa8b2e2bbadb1196fdd45d1d60c079ca3ff673aa Mon Sep 17 00:00:00 2001 From: Katherine Kiefer Date: Fri, 15 Dec 2023 09:35:26 +1100 Subject: [PATCH] removes type check traits, add enum for valuetype, bump version --- crates/byondapi-rs-test/Cargo.toml | 3 +- crates/byondapi-rs-test/src/lib.rs | 76 +++++--------------- crates/byondapi-rs/Cargo.toml | 4 +- crates/byondapi-rs/src/lib.rs | 1 - crates/byondapi-rs/src/prelude.rs | 14 +--- crates/byondapi-rs/src/static_global.rs | 6 +- crates/byondapi-rs/src/typecheck_trait.rs | 21 ------ crates/byondapi-rs/src/value/constructors.rs | 24 +++---- crates/byondapi-rs/src/value/functions.rs | 2 +- crates/byondapi-rs/src/value/list.rs | 5 +- crates/byondapi-rs/src/value/mod.rs | 21 +++--- crates/byondapi-rs/src/value/pointer.rs | 2 +- crates/byondapi-rs/src/value/types.rs | 62 ++++++++++++++++ crates/byondapi-sys/Cargo.toml | 2 +- 14 files changed, 112 insertions(+), 131 deletions(-) delete mode 100644 crates/byondapi-rs/src/typecheck_trait.rs create mode 100644 crates/byondapi-rs/src/value/types.rs diff --git a/crates/byondapi-rs-test/Cargo.toml b/crates/byondapi-rs-test/Cargo.toml index 5f390ad..ac7cab0 100644 --- a/crates/byondapi-rs-test/Cargo.toml +++ b/crates/byondapi-rs-test/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "byondapi-test" -version = "0.1.0" +version = "0.0.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -10,6 +10,5 @@ crate-type = ["cdylib"] [dependencies] byondapi = { path = "../byondapi-rs" } -byondapi-sys = { path = "../byondapi-sys" } tempfile = "3.8.1" cargo_metadata = "0.18.1" diff --git a/crates/byondapi-rs-test/src/lib.rs b/crates/byondapi-rs-test/src/lib.rs index d6234bd..4752434 100644 --- a/crates/byondapi-rs-test/src/lib.rs +++ b/crates/byondapi-rs-test/src/lib.rs @@ -1,12 +1,6 @@ #![allow(clippy::missing_safety_doc)] -use byondapi::{ - byond_string, - map::{byond_block, byond_length, ByondXYZ}, - parse_args, - typecheck_trait::ByondTypeCheck, - value::{pointer::ByondValuePointer, ByondValue}, -}; +use byondapi::{byond_string, map::*, parse_args, prelude::*}; #[allow(dead_code)] fn write_log>(x: T) { @@ -21,16 +15,13 @@ fn setup_panic_handler() { } #[no_mangle] -pub unsafe extern "C" fn test_connection( - _argc: byondapi_sys::u4c, - _argv: *mut ByondValue, -) -> ByondValue { +pub unsafe extern "C" fn test_connection(_argc: u4c, _argv: *mut ByondValue) -> ByondValue { setup_panic_handler(); ByondValue::new_num(69.0) } #[no_mangle] -pub unsafe extern "C" fn test_args(argc: byondapi_sys::u4c, argv: *mut ByondValue) -> ByondValue { +pub unsafe extern "C" fn test_args(argc: u4c, argv: *mut ByondValue) -> ByondValue { setup_panic_handler(); let args = parse_args(argc, argv); assert_eq!(args.len(), 1); @@ -38,7 +29,7 @@ pub unsafe extern "C" fn test_args(argc: byondapi_sys::u4c, argv: *mut ByondValu } #[no_mangle] -pub unsafe extern "C" fn test_ptr(argc: byondapi_sys::u4c, argv: *mut ByondValue) -> ByondValue { +pub unsafe extern "C" fn test_ptr(argc: u4c, argv: *mut ByondValue) -> ByondValue { setup_panic_handler(); let args = parse_args(argc, argv); let pointer = match ByondValuePointer::new(args[0].clone()) { @@ -64,10 +55,7 @@ pub unsafe extern "C" fn test_ptr(argc: byondapi_sys::u4c, argv: *mut ByondValue } #[no_mangle] -pub unsafe extern "C" fn test_proc_call( - argc: byondapi_sys::u4c, - argv: *mut ByondValue, -) -> ByondValue { +pub unsafe extern "C" fn test_proc_call(argc: u4c, argv: *mut ByondValue) -> ByondValue { setup_panic_handler(); let args = parse_args(argc, argv); @@ -80,10 +68,7 @@ pub unsafe extern "C" fn test_proc_call( } #[no_mangle] -pub unsafe extern "C" fn test_readwrite_var( - argc: byondapi_sys::u4c, - argv: *mut ByondValue, -) -> ByondValue { +pub unsafe extern "C" fn test_readwrite_var(argc: u4c, argv: *mut ByondValue) -> ByondValue { setup_panic_handler(); let args = parse_args(argc, argv); let object = &args[0]; @@ -100,10 +85,7 @@ pub unsafe extern "C" fn test_readwrite_var( } } #[no_mangle] -pub unsafe extern "C" fn test_list_push( - argc: byondapi_sys::u4c, - argv: *mut ByondValue, -) -> ByondValue { +pub unsafe extern "C" fn test_list_push(argc: u4c, argv: *mut ByondValue) -> ByondValue { setup_panic_handler(); let args = parse_args(argc, argv); @@ -118,10 +100,7 @@ pub unsafe extern "C" fn test_list_push( } #[no_mangle] -pub unsafe extern "C" fn test_list_double( - argc: byondapi_sys::u4c, - argv: *mut ByondValue, -) -> ByondValue { +pub unsafe extern "C" fn test_list_double(argc: u4c, argv: *mut ByondValue) -> ByondValue { setup_panic_handler(); let args = parse_args(argc, argv); @@ -137,10 +116,7 @@ pub unsafe extern "C" fn test_list_double( } #[no_mangle] -pub unsafe extern "C" fn test_list_index( - argc: byondapi_sys::u4c, - argv: *mut ByondValue, -) -> ByondValue { +pub unsafe extern "C" fn test_list_index(argc: u4c, argv: *mut ByondValue) -> ByondValue { setup_panic_handler(); let args = parse_args(argc, argv); @@ -150,10 +126,7 @@ pub unsafe extern "C" fn test_list_index( } #[no_mangle] -pub unsafe extern "C" fn test_list_pop( - argc: byondapi_sys::u4c, - argv: *mut ByondValue, -) -> ByondValue { +pub unsafe extern "C" fn test_list_pop(argc: u4c, argv: *mut ByondValue) -> ByondValue { setup_panic_handler(); let args = parse_args(argc, argv); @@ -174,10 +147,7 @@ pub unsafe extern "C" fn test_list_pop( } #[no_mangle] -pub unsafe extern "C" fn test_length_with_list( - argc: byondapi_sys::u4c, - argv: *mut ByondValue, -) -> ByondValue { +pub unsafe extern "C" fn test_length_with_list(argc: u4c, argv: *mut ByondValue) -> ByondValue { setup_panic_handler(); let args = parse_args(argc, argv); @@ -189,7 +159,7 @@ pub unsafe extern "C" fn test_length_with_list( } } #[no_mangle] -pub unsafe extern "C" fn test_block(argc: byondapi_sys::u4c, argv: *mut ByondValue) -> ByondValue { +pub unsafe extern "C" fn test_block(argc: u4c, argv: *mut ByondValue) -> ByondValue { setup_panic_handler(); let _args = parse_args(argc, argv); @@ -211,10 +181,7 @@ pub unsafe extern "C" fn test_block(argc: byondapi_sys::u4c, argv: *mut ByondVal } #[no_mangle] -pub unsafe extern "C" fn test_length_with_str( - argc: byondapi_sys::u4c, - argv: *mut ByondValue, -) -> ByondValue { +pub unsafe extern "C" fn test_length_with_str(argc: u4c, argv: *mut ByondValue) -> ByondValue { setup_panic_handler(); let args = parse_args(argc, argv); @@ -224,10 +191,7 @@ pub unsafe extern "C" fn test_length_with_str( } } #[no_mangle] -pub unsafe extern "C" fn test_list_key_lookup( - argc: byondapi_sys::u4c, - argv: *mut ByondValue, -) -> ByondValue { +pub unsafe extern "C" fn test_list_key_lookup(argc: u4c, argv: *mut ByondValue) -> ByondValue { setup_panic_handler(); let args = parse_args(argc, argv); @@ -278,7 +242,7 @@ pub unsafe extern "C" fn test_list_key_lookup( } #[no_mangle] -pub unsafe extern "C" fn test_ref(argc: byondapi_sys::u4c, argv: *mut ByondValue) -> ByondValue { +pub unsafe extern "C" fn test_ref(argc: u4c, argv: *mut ByondValue) -> ByondValue { setup_panic_handler(); let args = parse_args(argc, argv); @@ -290,10 +254,7 @@ pub unsafe extern "C" fn test_ref(argc: byondapi_sys::u4c, argv: *mut ByondValue } #[no_mangle] -pub unsafe extern "C" fn test_non_assoc_list( - argc: byondapi_sys::u4c, - argv: *mut ByondValue, -) -> ByondValue { +pub unsafe extern "C" fn test_non_assoc_list(argc: u4c, argv: *mut ByondValue) -> ByondValue { setup_panic_handler(); let args = parse_args(argc, argv); let list = args.get(0).unwrap(); @@ -318,10 +279,7 @@ pub unsafe extern "C" fn test_non_assoc_list( } #[no_mangle] -pub unsafe extern "C" fn test_list_read( - argc: byondapi_sys::u4c, - argv: *mut ByondValue, -) -> ByondValue { +pub unsafe extern "C" fn test_list_read(argc: u4c, argv: *mut ByondValue) -> ByondValue { setup_panic_handler(); let args = parse_args(argc, argv); let list = args.get(0).unwrap(); diff --git a/crates/byondapi-rs/Cargo.toml b/crates/byondapi-rs/Cargo.toml index 0b823e2..f5ee154 100644 --- a/crates/byondapi-rs/Cargo.toml +++ b/crates/byondapi-rs/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "byondapi" -version = "0.3.0" +version = "0.3.1" authors = ["tigercat2000 "] edition = "2021" description = "Idiomatic Rust bindings for BYONDAPI" @@ -13,7 +13,7 @@ exclude = [".vscode/*"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -byondapi-sys = { path = "../byondapi-sys", version = "0.11.0" } +byondapi-sys = { path = "../byondapi-sys" } lazy_static = "1.4.0" libloading = "0.8.1" walkdir = "2.4.0" diff --git a/crates/byondapi-rs/src/lib.rs b/crates/byondapi-rs/src/lib.rs index d8671a2..ad1cfbe 100644 --- a/crates/byondapi-rs/src/lib.rs +++ b/crates/byondapi-rs/src/lib.rs @@ -11,7 +11,6 @@ pub use error::Error; pub mod byond_string; pub mod global_call; pub mod prelude; -pub mod typecheck_trait; pub mod value; use crate::value::ByondValue; diff --git a/crates/byondapi-rs/src/prelude.rs b/crates/byondapi-rs/src/prelude.rs index e698d61..cda26c4 100644 --- a/crates/byondapi-rs/src/prelude.rs +++ b/crates/byondapi-rs/src/prelude.rs @@ -14,21 +14,11 @@ pub use crate::sys::s2cMIN; pub use crate::sys::s4c; pub use crate::sys::s4cMAX; pub use crate::sys::s4cMIN; +pub use crate::sys::s8c; pub use crate::sys::u1c; -// pub use crate::sys::u1cMAX; -// pub use crate::sys::u1cMIN; pub use crate::sys::u2c; -// pub use crate::sys::u2cMAX; -// pub use crate::sys::u2cMIN; pub use crate::sys::u4c; -// pub use crate::sys::u4cMAX; -// pub use crate::sys::u4cMIN; pub use crate::sys::u8c; -// pub use crate::sys::u8cMAX; -// pub use crate::sys::u8cMIN; -pub use crate::sys::s8c; -// pub use crate::sys::s8cMAX; -// pub use crate::sys::s8cMIN; // Other types pub use byondapi_sys::u4cOrPointer; @@ -38,4 +28,6 @@ pub use byondapi_sys::CByondValue as InternalByondValue; // As well as our own types. pub use crate::byond_string; +pub use crate::value::pointer::ByondValuePointer; +pub use crate::value::types::ValueType; pub use crate::value::ByondValue; diff --git a/crates/byondapi-rs/src/static_global.rs b/crates/byondapi-rs/src/static_global.rs index 796fcee..cc23355 100644 --- a/crates/byondapi-rs/src/static_global.rs +++ b/crates/byondapi-rs/src/static_global.rs @@ -10,7 +10,7 @@ fn init_lib() -> byondapi_sys::ByondApi { Ok(lib) => lib, Err(e) => { let message = format!( - "byondcore.dll is not loaded into the process as expected: {:#?}", + "byondcore is not loaded into the process as expected: {:#?}", e ); crate::error::crash_logging::log_to_file(&message); @@ -32,7 +32,7 @@ fn init_lib() -> byondapi_sys::ByondApi { match unsafe { byondapi_sys::ByondApi::init_from_library(library) } { Err(e) => { let message = format!( - "byondcore.dll is not loaded into the process as expected: {:#?}", + "byondcore is not loaded into the process as expected: {:#?}", e ); crate::error::crash_logging::log_to_file(&message); @@ -42,6 +42,8 @@ fn init_lib() -> byondapi_sys::ByondApi { } } +///Initialises the byond lib, and calls relevant init functions defined by inventory. +///Or returns a reference to the lib if already initialised. #[inline(always)] pub fn byond() -> &'static byondapi_sys::ByondApi { BYOND.get_or_init(init_lib) diff --git a/crates/byondapi-rs/src/typecheck_trait.rs b/crates/byondapi-rs/src/typecheck_trait.rs deleted file mode 100644 index 7514f2e..0000000 --- a/crates/byondapi-rs/src/typecheck_trait.rs +++ /dev/null @@ -1,21 +0,0 @@ -//! This trait allows checking what a [`crate::value::ByondValue`] actually represents before doing something with it -use byondapi_sys::ByondValueType; - -/// This trait allows checking what a [`crate::value::ByondValue`] actually represents before doing something with it -pub trait ByondTypeCheck { - /// This gets the actual type. - fn get_type(&self) -> ByondValueType; - - /// Check if this is null. - fn is_null(&self) -> bool; - /// Check if this is a number. - fn is_num(&self) -> bool; - /// Check if this is a string. - fn is_str(&self) -> bool; - /// Check if this is a list. - fn is_list(&self) -> bool; - /// Check if this is a pointer. - fn is_ptr(&self) -> bool; - //// Check if this is true-ish. - fn is_true(&self) -> bool; -} diff --git a/crates/byondapi-rs/src/value/constructors.rs b/crates/byondapi-rs/src/value/constructors.rs index 1460b38..4b2e004 100644 --- a/crates/byondapi-rs/src/value/constructors.rs +++ b/crates/byondapi-rs/src/value/constructors.rs @@ -1,8 +1,8 @@ use std::ffi::CString; -use byondapi_sys::{u4c, ByondValueType, CByondValue}; +use byondapi_sys::{u4c, CByondValue}; -use super::ByondValue; +use super::{types::ValueType, ByondValue}; use crate::{static_global::byond, Error}; impl Default for ByondValue { @@ -19,7 +19,7 @@ impl ByondValue { pub fn null() -> Self { Self(CByondValue { - type_: 0, + type_: ValueType::Null as u8, junk1: 0, junk2: 0, junk3: 0, @@ -27,9 +27,9 @@ impl ByondValue { }) } - pub fn new_ref(typ: ByondValueType, ptr: u4c) -> Self { + pub fn new_ref(typ: ValueType, ptr: u4c) -> Self { Self(CByondValue { - type_: typ, + type_: typ as u8, junk1: 0, junk2: 0, junk3: 0, @@ -39,7 +39,7 @@ impl ByondValue { pub fn new_num(f: f32) -> Self { Self(CByondValue { - type_: 0x2A, + type_: ValueType::Number as u8, junk1: 0, junk2: 0, junk3: 0, @@ -49,7 +49,7 @@ impl ByondValue { pub fn new_global_ref() -> Self { Self(CByondValue { - type_: 0x0E, + type_: ValueType::World as u8, junk1: 0, junk2: 0, junk3: 0, @@ -64,7 +64,7 @@ impl ByondValue { return Err(Error::UnableToCreateString); } Ok(Self(CByondValue { - type_: 0x06, + type_: ValueType::String as u8, junk1: 0, junk2: 0, junk3: 0, @@ -80,11 +80,3 @@ impl ByondValue { Ok(new_self) } } - -impl<'a> ByondValue { - /// # Safety - /// The [`CByondValue`] must be initialized. - pub unsafe fn from_ref(s: &'a CByondValue) -> &'a Self { - unsafe { std::mem::transmute(s) } - } -} diff --git a/crates/byondapi-rs/src/value/functions.rs b/crates/byondapi-rs/src/value/functions.rs index 0d68523..25e6f04 100644 --- a/crates/byondapi-rs/src/value/functions.rs +++ b/crates/byondapi-rs/src/value/functions.rs @@ -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, typecheck_trait::ByondTypeCheck, Error}; +use crate::{map::byond_length, static_global::byond, Error}; /// # Compatibility with the C++ API impl ByondValue { diff --git a/crates/byondapi-rs/src/value/list.rs b/crates/byondapi-rs/src/value/list.rs index 905c095..863cc6d 100644 --- a/crates/byondapi-rs/src/value/list.rs +++ b/crates/byondapi-rs/src/value/list.rs @@ -1,7 +1,4 @@ -use crate::{ - byond_string_internal, static_global::byond, typecheck_trait::ByondTypeCheck, - value::ByondValue, Error, -}; +use crate::{byond_string_internal, 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 elements, this means keys for assoc lists and values for regular lists diff --git a/crates/byondapi-rs/src/value/mod.rs b/crates/byondapi-rs/src/value/mod.rs index ca0f883..b62fd71 100644 --- a/crates/byondapi-rs/src/value/mod.rs +++ b/crates/byondapi-rs/src/value/mod.rs @@ -2,7 +2,7 @@ use byondapi_sys::{ByondValueType, CByondValue}; -use crate::{static_global::byond, typecheck_trait::ByondTypeCheck}; +use crate::static_global::byond; /// [Newtype](https://doc.rust-lang.org/rust-by-example/generics/new_types.html) pattern over [`CByondValue`] #[repr(transparent)] @@ -18,45 +18,46 @@ pub mod functions; pub mod list; pub mod pointer; pub mod trait_impls; +pub mod types; -/// FIXME: Use a Byond_IsPtr here instead of checking the type by hand +/// TODO: Use a Byond_IsPtr here instead of checking the type by hand fn is_pointer_shim(value: &ByondValue) -> bool { let type_ = value.get_type(); type_ == 0x3C } // Typechecking -impl ByondTypeCheck for ByondValue { - fn get_type(&self) -> ByondValueType { +impl ByondValue { + pub fn get_type(&self) -> ByondValueType { // Safety: This operation only fails if our CByondValue is invalid, which cannot happen. unsafe { byond().ByondValue_Type(&self.0) } } - fn is_null(&self) -> bool { + pub fn is_null(&self) -> bool { // Safety: This operation only fails if our CByondValue is invalid, which cannot happen. unsafe { byond().ByondValue_IsNull(&self.0) } } - fn is_num(&self) -> bool { + pub fn is_num(&self) -> bool { // Safety: This operation only fails if our CByondValue is invalid, which cannot happen. unsafe { byond().ByondValue_IsNum(&self.0) } } - fn is_str(&self) -> bool { + pub fn is_str(&self) -> bool { // Safety: This operation only fails if our CByondValue is invalid, which cannot happen. unsafe { byond().ByondValue_IsStr(&self.0) } } - fn is_list(&self) -> bool { + pub fn is_list(&self) -> bool { // Safety: This operation only fails if our CByondValue is invalid, which cannot happen. unsafe { byond().ByondValue_IsList(&self.0) } } - fn is_ptr(&self) -> bool { + pub fn is_ptr(&self) -> bool { is_pointer_shim(self) } - fn is_true(&self) -> bool { + pub fn is_true(&self) -> bool { // Safety: This operation only fails if our CByondValue is invalid, which cannot happen. unsafe { byond().ByondValue_IsTrue(&self.0) } } diff --git a/crates/byondapi-rs/src/value/pointer.rs b/crates/byondapi-rs/src/value/pointer.rs index 83fd15d..3c8e4eb 100644 --- a/crates/byondapi-rs/src/value/pointer.rs +++ b/crates/byondapi-rs/src/value/pointer.rs @@ -1,5 +1,5 @@ use super::ByondValue; -use crate::{static_global::byond, typecheck_trait::ByondTypeCheck, Error}; +use crate::{static_global::byond, Error}; #[repr(transparent)] pub struct ByondValuePointer(pub ByondValue); diff --git a/crates/byondapi-rs/src/value/types.rs b/crates/byondapi-rs/src/value/types.rs new file mode 100644 index 0000000..9a23a3f --- /dev/null +++ b/crates/byondapi-rs/src/value/types.rs @@ -0,0 +1,62 @@ +///Type enum for the [`byondapi_sys::ByondValueType`] field of [`byondapi_sys::CByondValue`], copied from auxtools +#[repr(u8)] +#[derive(Copy, Clone, Debug)] +#[non_exhaustive] +pub enum ValueType { + Null = 0x00, + Turf = 0x01, + Obj = 0x02, + Mob = 0x03, + Area = 0x04, + Client = 0x05, + String = 0x06, + + MobTypepath = 0x08, + ObjTypepath = 0x09, + TurfTypepath = 0x0A, + AreaTypepath = 0x0B, + Resource = 0x0C, + Image = 0x0D, + World = 0x0E, + + // Lists + List = 0x0F, + ArgList = 0x10, + MobContents = 0x17, + TurfContents = 0x18, + AreaContents = 0x19, + WorldContents = 0x1A, + ObjContents = 0x1C, + MobVars = 0x2C, + ObjVars = 0x2D, + TurfVars = 0x2E, + AreaVars = 0x2F, + ClientVars = 0x30, + Vars = 0x31, + MobOverlays = 0x32, + MobUnderlays = 0x33, + ObjOverlays = 0x34, + ObjUnderlays = 0x35, + TurfOverlays = 0x36, + TurfUnderlays = 0x37, + AreaOverlays = 0x38, + AreaUnderlays = 0x39, + ImageOverlays = 0x40, + ImageUnderlays = 0x41, + ImageVars = 0x42, + TurfVisContents = 0x4B, + ObjVisContents = 0x4C, + MobVisContents = 0x4D, + TurfVisLocs = 0x4E, + ObjVisLocs = 0x4F, + MobVisLocs = 0x50, + WorldVars = 0x51, + GlobalVars = 0x52, + ImageVisContents = 0x54, + + Datum = 0x21, + SaveFile = 0x23, + + Number = 0x2A, + Appearance = 0x3A, +} diff --git a/crates/byondapi-sys/Cargo.toml b/crates/byondapi-sys/Cargo.toml index 8c01a21..e39e663 100644 --- a/crates/byondapi-sys/Cargo.toml +++ b/crates/byondapi-sys/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "byondapi-sys" -version = "0.11.0" +version = "0.11.1" authors = ["tigercat2000 "] edition = "2021" description = "Raw bindgen bindings for byondapi"