Skip to content

Commit a64e54b

Browse files
authored
bugfix(#29): Fix Windows compilation error due to signedness of constants (#41)
This is a fix for: #29 The existing code assumed that the Wolfram LibraryLink constants would have the same size and signedness on all platforms. This assumption was not correct on Windows targeting MSVC, where constants that would be u32 on other platforms would be i32. See also: * rust-lang/rust-bindgen#1244 * rust-lang/rust-bindgen#1361
1 parent a531693 commit a64e54b

File tree

2 files changed

+27
-29
lines changed

2 files changed

+27
-29
lines changed

wolfram-library-link/src/macro_utils.rs

+14-17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use std::{
2-
os::raw::{c_int, c_uint},
3-
path::PathBuf,
4-
};
1+
use std::{os::raw::c_int, path::PathBuf};
52

63
use wstp::{self, Link};
74

@@ -22,19 +19,19 @@ use crate::{
2219
// TODO: Make this module public somewhere and document these error code in #[export(..)]
2320
// and Overview.md.
2421
mod error_code {
25-
use std::os::raw::c_uint;
22+
use std::os::raw::c_int;
2623

2724
// Chosen arbitrarily. Avoids clashing with `LIBRARY_FUNCTION_ERROR` and related
2825
// error codes.
29-
const OFFSET: c_uint = 1000;
26+
const OFFSET: c_int = 1000;
3027

3128
/// A call to [initialize()][crate::initialize] failed.
32-
pub const FAILED_TO_INIT: c_uint = OFFSET + 1;
29+
pub const FAILED_TO_INIT: c_int = OFFSET + 1;
3330

3431
/// The library code panicked.
3532
//
3633
// TODO: Wherever this code is set, also set a $LastError-like variable.
37-
pub const FAILED_WITH_PANIC: c_uint = OFFSET + 2;
34+
pub const FAILED_WITH_PANIC: c_int = OFFSET + 2;
3835
}
3936

4037
//==================
@@ -47,7 +44,7 @@ unsafe fn call_wstp_link_wolfram_library_function<
4744
libdata: sys::WolframLibraryData,
4845
mut unsafe_link: wstp::sys::WSLINK,
4946
function: F,
50-
) -> c_uint {
47+
) -> c_int {
5148
// Initialize the library.
5249
if crate::initialize(libdata).is_err() {
5350
return error_code::FAILED_TO_INIT;
@@ -61,14 +58,14 @@ unsafe fn call_wstp_link_wolfram_library_function<
6158
}));
6259

6360
match result {
64-
Ok(()) => LIBRARY_NO_ERROR,
61+
Ok(()) => LIBRARY_NO_ERROR as c_int,
6562
// Try to fail gracefully by writing the panic message as a Failure[..] object to
6663
// be returned, but if that fails, just return LIBRARY_FUNCTION_ERROR.
6764
Err(panic) => match write_panic_failure_to_link(link, panic) {
68-
Ok(()) => LIBRARY_NO_ERROR,
65+
Ok(()) => LIBRARY_NO_ERROR as c_int,
6966
Err(_wstp_err) => {
7067
// println!("PANIC ERROR: {}", _wstp_err);
71-
sys::LIBRARY_FUNCTION_ERROR // +1
68+
sys::LIBRARY_FUNCTION_ERROR as c_int // +1
7269
},
7370
},
7471
}
@@ -114,7 +111,7 @@ pub unsafe fn call_native_wolfram_library_function<'a, F: NativeFunction<'a>>(
114111
argc: sys::mint,
115112
res: MArgument,
116113
func: F,
117-
) -> c_uint {
114+
) -> c_int {
118115
use std::panic::AssertUnwindSafe;
119116

120117
// Initialize the library.
@@ -124,7 +121,7 @@ pub unsafe fn call_native_wolfram_library_function<'a, F: NativeFunction<'a>>(
124121

125122
let argc = match usize::try_from(argc) {
126123
Ok(argc) => argc,
127-
Err(_) => return sys::LIBRARY_FUNCTION_ERROR,
124+
Err(_) => return sys::LIBRARY_FUNCTION_ERROR as c_int,
128125
};
129126

130127
// FIXME: This isn't safe! 'a could be 'static, and then the user could store the
@@ -138,7 +135,7 @@ pub unsafe fn call_native_wolfram_library_function<'a, F: NativeFunction<'a>>(
138135
return error_code::FAILED_WITH_PANIC;
139136
};
140137

141-
sys::LIBRARY_NO_ERROR
138+
sys::LIBRARY_NO_ERROR as c_int
142139
}
143140

144141
pub unsafe fn call_wstp_wolfram_library_function<
@@ -147,7 +144,7 @@ pub unsafe fn call_wstp_wolfram_library_function<
147144
libdata: sys::WolframLibraryData,
148145
unsafe_link: wstp::sys::WSLINK,
149146
func: F,
150-
) -> c_uint {
147+
) -> c_int {
151148
call_wstp_link_wolfram_library_function(
152149
libdata,
153150
unsafe_link,
@@ -204,7 +201,7 @@ inventory::collect!(LibraryLinkFunction);
204201
pub unsafe fn load_library_functions_impl(
205202
lib_data: sys::WolframLibraryData,
206203
raw_link: wstp::sys::WSLINK,
207-
) -> c_uint {
204+
) -> c_int {
208205
call_wstp_link_wolfram_library_function(lib_data, raw_link, |link: &mut Link| {
209206
let arg_count: usize =
210207
link.test_head("List").expect("expected 'List' expression");

wolfram-library-link/src/numeric_array.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -172,16 +172,16 @@ pub enum NumericArrayDataType {
172172
#[repr(u32)]
173173
#[allow(missing_docs)]
174174
pub enum NumericArrayConvertMethod {
175-
Cast = MNumericArray_Convert_Cast,
176-
Check = MNumericArray_Convert_Check,
177-
Coerce = MNumericArray_Convert_Coerce,
178-
Round = MNumericArray_Convert_Round,
179-
Scale = MNumericArray_Convert_Scale,
180-
ClipAndCast = MNumericArray_Convert_Clip_Cast,
181-
ClipAndCheck = MNumericArray_Convert_Clip_Check,
182-
ClipAndCoerce = MNumericArray_Convert_Clip_Coerce,
183-
ClipAndRound = MNumericArray_Convert_Clip_Round,
184-
ClipAndScale = MNumericArray_Convert_Clip_Scale,
175+
Cast = MNumericArray_Convert_Cast as u32,
176+
Check = MNumericArray_Convert_Check as u32,
177+
Coerce = MNumericArray_Convert_Coerce as u32,
178+
Round = MNumericArray_Convert_Round as u32,
179+
Scale = MNumericArray_Convert_Scale as u32,
180+
ClipAndCast = MNumericArray_Convert_Clip_Cast as u32,
181+
ClipAndCheck = MNumericArray_Convert_Clip_Check as u32,
182+
ClipAndCoerce = MNumericArray_Convert_Clip_Coerce as u32,
183+
ClipAndRound = MNumericArray_Convert_Clip_Round as u32,
184+
ClipAndScale = MNumericArray_Convert_Clip_Scale as u32,
185185
}
186186

187187
/// Data array borrowed from a [`NumericArray`].
@@ -540,6 +540,7 @@ impl<T> NumericArray<T> {
540540
#[allow(missing_docs)]
541541
pub fn data_type(&self) -> NumericArrayDataType {
542542
let value: sys::numericarray_data_t = self.data_type_raw();
543+
let value: u32 = value as u32;
543544

544545
NumericArrayDataType::try_from(value)
545546
.expect("NumericArray tensor property type is value is not a known NumericArrayDataType variant")
@@ -828,7 +829,7 @@ fn copy_from_slice_uninit<T>(src: &[T], dest: &mut [MaybeUninit<T>]) {
828829
impl NumericArrayDataType {
829830
#[allow(missing_docs)]
830831
pub fn as_raw(self) -> sys::numericarray_data_t {
831-
self as u32
832+
self as sys::numericarray_data_t
832833
}
833834

834835
/// Get the string name of this type, suitable for use in
@@ -860,7 +861,7 @@ impl NumericArrayDataType {
860861
impl NumericArrayConvertMethod {
861862
#[allow(missing_docs)]
862863
pub fn as_raw(self) -> sys::numericarray_convert_method_t {
863-
self as u32
864+
self as sys::numericarray_convert_method_t
864865
}
865866
}
866867

0 commit comments

Comments
 (0)