Skip to content

Commit

Permalink
Add (non-working) cpp-to-wasm test
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Aug 18, 2021
1 parent f4aa662 commit 153bd3b
Show file tree
Hide file tree
Showing 10 changed files with 6,929 additions and 4 deletions.
7 changes: 5 additions & 2 deletions ffi/capi/Cargo.toml
Expand Up @@ -30,9 +30,9 @@ all-features = true
# Omit most optional dependency features from permutation testing
skip_optional_dependencies = true
# Bench feature gets tested separately and is only relevant for CI.
# wearos/freertos/x86tiny are not relevant in normal environments,
# wearos/freertos/x86tiny/wasm_embedding are not relevant in normal environments,
# and smaller_static gets tested on the FFI job anyway
denylist = ["bench", "wearos", "freertos", "x86tiny", "smaller_static"]
denylist = ["bench", "wearos", "freertos", "x86tiny", "smaller_static", "wasm_embedding"]

[lib]
crate-type = ["staticlib", "rlib", "cdylib"]
Expand All @@ -56,6 +56,9 @@ x86tiny = ["dlmalloc"]
# Enables no_std builds for freertos
freertos = ["freertos-rust", "cortex-m"]

# Enables things that enable Rust to be linked to C++ (etc) in WASM
wasm_embedding = []

[dependencies]
fixed_decimal = { path = "../../utils/fixed_decimal" }
icu_decimal = { path = "../../components/decimal/" }
Expand Down
42 changes: 42 additions & 0 deletions ffi/capi/src/wasm_glue.rs
Expand Up @@ -61,3 +61,45 @@ pub unsafe extern "C" fn icu4x_init() {
.map(|()| log::set_max_level(LevelFilter::Debug))
.unwrap();
}

#[cfg(feature = "wasm_embedding")]
mod allocator {
use alloc::alloc::GlobalAlloc;
use alloc::alloc::Layout;

// The default global allocator for Rust works fine when Rust
// is controlling the entire WASM stack, however when linked to
// C++ it does not, so we fall back to malloc/free
#[global_allocator]
static GLOBAL: WasmMalloc = WasmMalloc;

pub struct WasmMalloc;

extern "C" {
fn malloc(size: usize) -> *mut u8;
fn free(ptr: *mut u8);
fn calloc(num: usize, size: usize) -> *mut u8;
fn realloc(ptr: *mut u8, size: usize) -> *mut u8;
}
unsafe impl GlobalAlloc for WasmMalloc {
#[inline]
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
malloc(layout.size())
}

#[inline]
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
free(ptr)
}

#[inline]
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
calloc(layout.size(), 1)
}

#[inline]
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
realloc(ptr, new_size)
}
}
}
1 change: 1 addition & 0 deletions ffi/cpp/examples/fixeddecimal-wasm/.gitignore
@@ -0,0 +1 @@
a.out
25 changes: 25 additions & 0 deletions ffi/cpp/examples/fixeddecimal-wasm/Makefile
@@ -0,0 +1,25 @@
# This file is part of ICU4X. For terms of use, please see the file
# called LICENSE at the top level of the ICU4X source tree
# (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

.DEFAULT_GOAL := test
.PHONY: build

ALL_HEADERS := $(wildcard ../../include/*.hpp) $(wildcard ../../../capi/include/*.h)
ALL_RUST := $(wildcard ../../../capi//src/*.rs)

$(ALL_RUST):

$(ALL_HEADERS):




../../../../target/wasm32-unknown-unknown/debug/libicu_capi.a: $(ALL_RUST)
cargo +nightly build -p icu_capi --target wasm32-unknown-unknown --features wasm_embedding

# Currently this doesn't run successfully, however it's the status quo of compiling ICU4X in C++ to WASM
main.html: ../../../../target/wasm32-unknown-unknown/debug/libicu_capi.a $(ALL_HEADERS) test.cpp
emcc -std=c++17 test.cpp ../../../../target/wasm32-unknown-unknown/debug/libicu_capi.a -ldl -lpthread -lm -g -o main.html --emrun -sWASM=1

build: main.html
1,300 changes: 1,300 additions & 0 deletions ffi/cpp/examples/fixeddecimal-wasm/main.html

Large diffs are not rendered by default.

0 comments on commit 153bd3b

Please sign in to comment.