Skip to content

Commit 187c2a9

Browse files
Drop emscripten-functions dependency
1 parent a9ddeda commit 187c2a9

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ getrandom = { version = "0.2.1", features = ["js"] }
4343
[target.'cfg(target_os = "haiku")'.dependencies]
4444
iana-time-zone-haiku = { version = "0.1.1", path = "haiku" }
4545

46-
[target.'cfg(target_os = "emscripten")'.dependencies]
47-
emscripten-functions = "0.3"
48-
4946
[dev-dependencies]
5047
chrono-tz = "0.10.1"
5148
# Set a minimum, but unused, dependency on `getrandom` to ensure that cfg-if

src/tz_wasm32_emscripten.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,30 @@
11
use crate::GetTimezoneError;
2-
use emscripten_functions::emscripten::run_script_string;
2+
use std::ffi::CStr;
3+
use std::os::raw::c_char;
4+
use std::ptr::NonNull;
5+
6+
extern "C" {
7+
fn emscripten_run_script_string(script: *const c_char) -> *mut c_char;
8+
}
39

410
pub(crate) fn get_timezone_inner() -> Result<String, GetTimezoneError> {
5-
run_script_string("Intl.DateTimeFormat().resolvedOptions().timeZone")
6-
.ok_or_else(|| GetTimezoneError::OsError)
11+
const SCRIPT: &CStr = {
12+
match CStr::from_bytes_with_nul(
13+
"Intl.DateTimeFormat().resolvedOptions().timeZone\0".as_bytes(),
14+
) {
15+
Ok(s) => s,
16+
Err(_) => panic!("Invalid UTF-8 data"),
17+
}
18+
};
19+
20+
unsafe {
21+
NonNull::new(emscripten_run_script_string(SCRIPT.as_ptr()))
22+
.ok_or_else(|| GetTimezoneError::OsError)
23+
.and_then(|ptr| {
24+
CStr::from_ptr(ptr.as_ptr())
25+
.to_owned()
26+
.into_string()
27+
.map_err(|_| GetTimezoneError::FailedParsingString)
28+
})
29+
}
730
}

0 commit comments

Comments
 (0)