Debian Codegraph @

debian/rust-vendor/jiff/src/tz/system/wasm_emscripten.rs

raw
1 use core::ffi::{c_char, CStr};
2 use core::ptr::NonNull;
3
4 use crate::{
5 tz::{TimeZone, TimeZoneDatabase},
6 util::constant::unwrapr,
7 };
8
9 extern "C" {
10 /// <https://emscripten.org/docs/api_reference/emscripten.h.html#c.emscripten_run_script_string>
11 fn emscripten_run_script_string(script: *const c_char) -> *mut c_char;
12 }
13
14 pub(super) fn get(db: &TimeZoneDatabase) -> Option<TimeZone> {
15 const SCRIPT: &CStr = unwrapr!(
16 CStr::from_bytes_with_nul(
17 "Intl.DateTimeFormat().resolvedOptions().timeZone\0".as_bytes(),
18 ),
19 "not a valid C string literal"
20 );
21
22 // SAFETY: SCRIPT is a valid pointer to a null-terminated string. The
23 // result will be a pointer to a null-terminated C string, or null if the
24 // script failed to run. The returned value is valid until the next call
25 // to `emscripten_run_script_string` in the same thread, so it is valid
26 // for the duration of this function. This likely relies on emscripten
27 // managing its string lifetimes carefully, possibly through TLS or some
28 // other shenanigans. Either way, this usage seems clearly okay even in a
29 // multi-threaded environment according to emscripten docs:
30 //
31 // > This function can be called from a pthread, and it is executed
32 // > in the scope of the Web Worker that is hosting the pthread. To
33 // > evaluate a function in the scope of the main runtime thread,
34 // > see the function emscripten_sync_run_in_main_runtime_thread().
35 //
36 // From: <https://emscripten.org/docs/api_reference/emscripten.h.html#c.emscripten_run_script_string>
37 let script_result =
38 unsafe { emscripten_run_script_string(SCRIPT.as_ptr()) };
39 let script_ptr = match NonNull::new(script_result) {
40 Some(script_cstr) => script_cstr,
41 None => {
42 trace!(
43 "executing JavaScript to discover IANA time zone \
44 identifier returned a null pointer",
45 );
46 return None;
47 }
48 };
49 // SAFETY: OK because of the contract of `emscripten_run_script_string`.
50 // We assume that when it returns a non-null pointer (which is checked
51 // above) that it is a valid C string.
52 let script_cstr = unsafe { CStr::from_ptr(script_ptr.as_ptr()) };
53 let name = match script_cstr.to_str().ok() {
54 Some(name) => name,
55 None => {
56 trace!(
57 "executing JavaScript to discover IANA time zone \
58 identifier returned invalid UTF-8: {script_cstr}",
59 script_cstr =
60 crate::util::escape::Bytes(script_cstr.to_bytes()),
61 );
62 return None;
63 }
64 };
65 let tz = match db.get(name) {
66 Ok(tz) => tz,
67 Err(_err) => {
68 trace!(
69 "got {name:?} as time zone name, \
70 but failed to find time zone with that name in \
71 zoneinfo database {db:?}: {_err}",
72 );
73 return None;
74 }
75 };
76 Some(tz)
77 }
78
79 pub(super) fn read(_db: &TimeZoneDatabase, path: &str) -> Option<TimeZone> {
80 match super::read_unnamed_tzif_file(path) {
81 Ok(tz) => Some(tz),
82 Err(_err) => {
83 trace!("failed to read {path} as unnamed time zone: {_err}");
84 None
85 }
86 }
87 }