Skip to content

Commit 6ed4a11

Browse files
committed
Avoid sys-locale in host on macOS
1 parent 34d0483 commit 6ed4a11

3 files changed

Lines changed: 55 additions & 2 deletions

File tree

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@ crate-type = ["staticlib"]
1313
# Note: we use "=version" for reproducability, cargo may silently use a more recent version if you do not use `=`.
1414
[dependencies]
1515
libc = "=0.2.180" # keep version in sync with libc workspace dep below
16-
sys-locale.workspace = true
1716
crossterm = "0.27"
1817
roc_std_new.workspace = true
1918
roc_io_error.workspace = true
2019
roc_random.workspace = true
2120
roc_command.workspace = true
2221
memoffset = "=0.9.1"
2322

23+
[target.'cfg(not(target_os = "macos"))'.dependencies]
24+
sys-locale.workspace = true
25+
2426
[workspace]
2527
members = [
2628
"crates/roc_io_error",

crates/roc_host/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ path = "src/lib.rs"
1616
crossterm.workspace = true
1717
memmap2.workspace = true
1818
memchr.workspace = true
19-
sys-locale.workspace = true
2019
libc.workspace = true
2120
backtrace.workspace = true
2221
roc_std.workspace = true

src/lib.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,8 +669,47 @@ unsafe fn write_try_bool_result(
669669
std::ptr::write(ret_ptr as *mut TryBoolPathErr, try_result);
670670
}
671671

672+
#[cfg(target_os = "macos")]
673+
fn locale_from_env() -> Option<String> {
674+
for key in ["LC_ALL", "LC_CTYPE", "LANG"] {
675+
if let Ok(value) = std::env::var(key) {
676+
let trimmed = value.trim();
677+
if trimmed.is_empty() {
678+
continue;
679+
}
680+
let locale = trimmed
681+
.split('.')
682+
.next()
683+
.unwrap_or(trimmed)
684+
.split('@')
685+
.next()
686+
.unwrap_or(trimmed)
687+
.trim();
688+
if !locale.is_empty() {
689+
return Some(locale.to_string());
690+
}
691+
}
692+
}
693+
694+
None
695+
}
696+
697+
/// Hosted function: Locale.all!
698+
/// Takes {}, returns List(Str)
699+
#[cfg(target_os = "macos")]
700+
extern "C" fn hosted_locale_all(ops: *const RocOps, ret_ptr: *mut c_void, _args_ptr: *mut c_void) {
701+
let roc_ops = unsafe { &*ops };
702+
let locales = locale_from_env().unwrap_or_else(|| "en-US".to_string());
703+
let mut list = RocList::with_capacity(1, roc_ops);
704+
list.push(RocStr::from_str(&locales, roc_ops), roc_ops);
705+
unsafe {
706+
*(ret_ptr as *mut RocList<RocStr>) = list;
707+
}
708+
}
709+
672710
/// Hosted function: Locale.all!
673711
/// Takes {}, returns List(Str)
712+
#[cfg(not(target_os = "macos"))]
674713
extern "C" fn hosted_locale_all(ops: *const RocOps, ret_ptr: *mut c_void, _args_ptr: *mut c_void) {
675714
let roc_ops = unsafe { &*ops };
676715
let locales = sys_locale::get_locales().collect::<Vec<_>>();
@@ -686,6 +725,19 @@ extern "C" fn hosted_locale_all(ops: *const RocOps, ret_ptr: *mut c_void, _args_
686725

687726
/// Hosted function: Locale.get!
688727
/// Takes {}, returns Str
728+
#[cfg(target_os = "macos")]
729+
extern "C" fn hosted_locale_get(ops: *const RocOps, ret_ptr: *mut c_void, _args_ptr: *mut c_void) {
730+
let roc_ops = unsafe { &*ops };
731+
let locale = locale_from_env().unwrap_or_else(|| "en-US".to_string());
732+
let roc_str = RocStr::from_str(&locale, roc_ops);
733+
unsafe {
734+
*(ret_ptr as *mut RocStr) = roc_str;
735+
}
736+
}
737+
738+
/// Hosted function: Locale.get!
739+
/// Takes {}, returns Str
740+
#[cfg(not(target_os = "macos"))]
689741
extern "C" fn hosted_locale_get(ops: *const RocOps, ret_ptr: *mut c_void, _args_ptr: *mut c_void) {
690742
let roc_ops = unsafe { &*ops };
691743
let locale = sys_locale::get_locale().unwrap_or_else(|| "en-US".to_string());

0 commit comments

Comments
 (0)