Skip to content

Commit

Permalink
fix i8/u8 usage for arm64 compatibility
Browse files Browse the repository at this point in the history
similar to:
sportsball-ai/blackmagic-raw-rs#3

> Apparently c_char is aliased to u8 on linux/arm64, whereas it's i8 on linux/amd64.
> Some context here: rust-lang/rust#60226
> And see c_char_definition here: https://doc.rust-lang.org/src/std/os/raw/mod.rs.html#103
  • Loading branch information
jbielick committed Oct 6, 2022
1 parent 7f91673 commit 9f5fd5d
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 6 deletions.
5 changes: 3 additions & 2 deletions ruby-parser-cpp/src/decoder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#[allow(unused_imports)]
use std::os::raw::{c_char};
use crate::{blob_type, bytes::ByteListBlob, string::StringBlob};
use lib_ruby_parser::source::{DecoderResult, InputError};

Expand Down Expand Up @@ -73,7 +74,7 @@ pub extern "C" fn lib_ruby_parser__test__always_ok_decoder(output: *const u8) ->
drop(String::from(encoding));
drop(Vec::<u8>::from(input));
// and return given output
let output = unsafe { std::ffi::CStr::from_ptr(state as *const i8) }
let output = unsafe { std::ffi::CStr::from_ptr(state as *const c_char) }
.to_str()
.unwrap();
DecoderResultBlob::from(DecoderResult::Ok(output.as_bytes().to_vec()))
Expand All @@ -97,7 +98,7 @@ pub extern "C" fn lib_ruby_parser__test__always_err_decoder(output: *const u8) -
drop(String::from(encoding));
drop(Vec::<u8>::from(input));
// and return given output
let output = unsafe { std::ffi::CStr::from_ptr(state as *const i8) }
let output = unsafe { std::ffi::CStr::from_ptr(state as *const c_char) }
.to_str()
.unwrap();
DecoderResultBlob::from(DecoderResult::Err(InputError::DecodingError(
Expand Down
3 changes: 2 additions & 1 deletion ruby-parser-cpp/src/diagnostic.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::os::raw::{c_char};
use crate::blob_type;
#[allow(unused_imports)]
use crate::message::DiagnosticMessageBlob;
Expand Down Expand Up @@ -42,7 +43,7 @@ pub extern "C" fn LIB_RUBY_PARSER_drop_diagnostic_list(diagnostic_list: *mut Vec
pub extern "C" fn LIB_RUBY_PARSER_render_diagnostic(
diagnostic: *const Diagnostic,
input: *const DecodedInput,
) -> *mut i8 {
) -> *mut c_char {
let diagnostic = unsafe { diagnostic.as_ref().unwrap() };
let input = unsafe { input.as_ref().unwrap() };
let rendered = diagnostic.render(input).unwrap();
Expand Down
3 changes: 2 additions & 1 deletion ruby-parser-cpp/src/loc.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::os::raw::{c_char};
use crate::blob_type;
use lib_ruby_parser::source::DecodedInput;
#[allow(unused_imports)]
Expand Down Expand Up @@ -27,7 +28,7 @@ pub extern "C" fn lib_ruby_parser__test__make_some_loc(begin: usize, end: usize)
pub extern "C" fn LIB_RUBY_PARSER_loc_source(
loc: *const Loc,
input: *const DecodedInput,
) -> *mut i8 {
) -> *mut c_char {
let loc = unsafe { loc.as_ref().unwrap() };
let input = unsafe { input.as_ref().unwrap() };
let source = loc.source(input).unwrap();
Expand Down
3 changes: 2 additions & 1 deletion ruby-parser-cpp/src/string.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::blob_type;
use std::os::raw::{c_char};

blob_type!(StringBlob, String);
blob_type!(MaybeStringBlob, Option<String>);
Expand All @@ -10,7 +11,7 @@ pub extern "C" fn LIB_RUBY_PARSER_new_string_owned(ptr: *mut u8, len: usize) ->
}

#[no_mangle]
pub extern "C" fn LIB_RUBY_PARSER_new_string_from_cstr(ptr: *const i8) -> StringBlob {
pub extern "C" fn LIB_RUBY_PARSER_new_string_from_cstr(ptr: *const c_char) -> StringBlob {
let s = unsafe { std::ffi::CStr::from_ptr(ptr) };
StringBlob::from(s.to_str().unwrap_or_default().to_owned())
}
Expand Down
3 changes: 2 additions & 1 deletion ruby-parser-cpp/src/token.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::os::raw::{c_char};
use crate::blob_type;
#[allow(unused_imports)]
use lib_ruby_parser::{Bytes, LexState, Lexer, Loc, Token};
Expand Down Expand Up @@ -29,7 +30,7 @@ pub extern "C" fn lib_ruby_parser__test__make_token_eq(
}

#[no_mangle]
pub extern "C" fn LIB_RUBY_PARSER_token_name(token: *const Token) -> *mut i8 {
pub extern "C" fn LIB_RUBY_PARSER_token_name(token: *const Token) -> *mut c_char {
let token = unsafe { token.as_ref().unwrap() };
let token_name = token.token_name();
std::ffi::CString::new(token_name).unwrap().into_raw()
Expand Down

0 comments on commit 9f5fd5d

Please sign in to comment.