Skip to content

Commit 3e5688c

Browse files
authored
Merge pull request #189 from bschoenmaeckers/emscripten
Add support for emscripten
2 parents ceeab78 + 4c0e969 commit 3e5688c

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed

.github/workflows/rust.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,29 @@ jobs:
108108
tool: wasm-pack
109109
- run: wasm-pack test --node
110110

111+
test-emscripten:
112+
strategy:
113+
fail-fast: false
114+
matrix:
115+
toolchain:
116+
- stable
117+
- nightly
118+
runs-on: ubuntu-latest
119+
steps:
120+
- name: Checkout
121+
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6
122+
with:
123+
persist-credentials: false
124+
- name: Install Rust
125+
run: |
126+
rustup toolchain install ${{ matrix.toolchain }} --profile minimal --no-self-update --target wasm32-unknown-emscripten
127+
rustup default ${{ matrix.toolchain }}
128+
- uses: mymindstorm/setup-emsdk@6ab9eb1bda2574c4ddb79809fc9247783eaf9021 # v14
129+
- env:
130+
CARGO_TARGET_WASM32_UNKNOWN_EMSCRIPTEN_RUNNER: node
131+
CARGO_BUILD_TARGET: wasm32-unknown-emscripten
132+
run: cargo test
133+
111134
build-cross:
112135
strategy:
113136
fail-fast: false

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ mod ffi_utils;
4545
all(target_arch = "wasm32", target_os = "unknown"),
4646
path = "tz_wasm32_unknown.rs"
4747
)]
48+
#[cfg_attr(
49+
all(target_arch = "wasm32", target_os = "emscripten"),
50+
path = "tz_wasm32_emscripten.rs"
51+
)]
4852
#[cfg_attr(
4953
any(target_os = "freebsd", target_os = "dragonfly"),
5054
path = "tz_freebsd.rs"

src/platform.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ pub fn get_timezone_inner() -> Result<String, crate::GetTimezoneError> {
55
#[cfg(not(feature = "fallback"))]
66
compile_error!(
77
"iana-time-zone is currently implemented for Linux, Window, MacOS, FreeBSD, NetBSD, \
8-
OpenBSD, Dragonfly, WebAssembly (browser), iOS, Illumos, Android, AIX, Solaris and Haiku.",
8+
OpenBSD, Dragonfly, WebAssembly (browser), emscripten, iOS, Illumos, Android, AIX, Solaris and Haiku.",
99
);

src/tz_wasm32_emscripten.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use crate::GetTimezoneError;
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+
}
9+
10+
pub(crate) fn get_timezone_inner() -> Result<String, GetTimezoneError> {
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+
}
30+
}

0 commit comments

Comments
 (0)