Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add cpp-to-wasm test * Run wasm test on CI * Fix CI * Add host test to test-ffi * pin emsdk * Pin emscripten test to older nightly too
- Loading branch information
1 parent
b66b237
commit 19c05d9
Showing
15 changed files
with
225 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// 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 ). | ||
|
||
// See comment in icu_capi's Cargo.toml | ||
// | ||
// This is essentially a hack that allows icu_capi to be compiled | ||
// to arbitrary `--crate-type`s without having to add a `--crate-type` | ||
// to the list in Cargo.toml | ||
extern crate icu_capi; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,4 +22,4 @@ a.out: ../../../../target/debug/libicu_capi.a $(ALL_HEADERS) test.cpp | |
build: a.out | ||
|
||
test: build | ||
./a.out | ||
./a.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
web-version.html | ||
web-version.wasm | ||
web-version.js | ||
node-version.js | ||
node-version.wasm | ||
package-lock.json | ||
node_modules | ||
a.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# 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 := build | ||
.PHONY: build test clean serve build-host test-host | ||
|
||
ALL_HEADERS := $(wildcard ../../include/*.hpp) $(wildcard ../../../capi/include/*.h) | ||
ALL_RUST := $(wildcard ../../../capi//src/*.rs) | ||
|
||
$(ALL_RUST): | ||
|
||
$(ALL_HEADERS): | ||
|
||
../../../../target/debug/libicu_capi.a: $(ALL_RUST) | ||
cargo build -p icu_capi | ||
|
||
a.out: ../../../../target/debug/libicu_capi.a $(ALL_HEADERS) test.cpp | ||
g++ -std=c++17 test.cpp ../../../../target/debug/libicu_capi.a -ldl -lpthread -lm -g | ||
|
||
../../../../target/wasm32-unknown-emscripten/release/libicu_capi.a: $(ALL_RUST) | ||
RUSTFLAGS="-Cpanic=abort" cargo +nightly-2021-02-28 build --release -p icu_capi --target wasm32-unknown-emscripten -Z build-std=std,panic_abort -Z build-std-features=panic_immediate_abort | ||
|
||
web-version.html: ../../../../target/wasm32-unknown-emscripten/release/libicu_capi.a $(ALL_HEADERS) test.cpp | ||
emcc -std=c++17 test.cpp ../../../../target/wasm32-unknown-emscripten/release/libicu_capi.a -ldl -lpthread -lm -g -o web-version.html --bind --emrun -sENVIRONMENT=web -sWASM=1 -sEXPORT_ES6=1 -sMODULARIZE=1 | ||
|
||
node-version.js: ../../../../target/wasm32-unknown-emscripten/release/libicu_capi.a $(ALL_HEADERS) test.cpp | ||
emcc -std=c++17 test.cpp ../../../../target/wasm32-unknown-emscripten/release/libicu_capi.a -ldl -lpthread -lm -g -o node-version.js --bind -sWASM=1 -sENVIRONMENT=node -sWASM_ASYNC_COMPILATION=0 -DNOMAIN | ||
|
||
build: web-version.html node-version.js | ||
|
||
test: node-version.js | ||
exec node ./node-test.js | ||
|
||
serve: web-version.html | ||
emrun web-version.html | ||
|
||
# These make it possible to ensure that the C++ code is up to date with the bindings | ||
# without needing to set up emsdk. This way `make test-ffi` works without emsdk. | ||
build-host: a.out | ||
|
||
test-host: build-host | ||
./a.out | ||
|
||
clean: | ||
rm -f web-version.html | ||
rm -f web-version.wasm | ||
rm -f web-version.js | ||
rm -f node-version.js | ||
rm -f node-version.wasm | ||
rm -f ../../../../target/wasm32-unknown-emscripten/release/libicu_capi.a | ||
rm -f ../../../../target/debug/libicu_capi.a | ||
rm -f a.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
This folder contains a test for calling ICU4X from C++ compiled to WASM (via emscripten). | ||
|
||
You need the [Emscripten SDK](https://emscripten.org/docs/getting_started/downloads.html) downloaded and sourced into your environment to run this. | ||
|
||
There are two ways to run the test. Firstly, you can call `make test`, which runs `node node-test.js` after building the appropriate WASM files. This runs a CLI test with the fixed decimal example in test.cpp. | ||
|
||
The other way is to run `make serve`, which will open a web page running test.cpp in your browser. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
wasm = require("./node-version.js"); | ||
|
||
const exitCode = wasm.runFixedDecimal(); | ||
if (exitCode !== 0) { | ||
throw new Error(`Test failed with exit code ${exitCode}`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"type": "commonjs", | ||
"scripts": { | ||
"test": "node node-test.js" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// 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 ). | ||
|
||
#ifdef __EMSCRIPTEN__ | ||
#include <emscripten/bind.h> | ||
#endif | ||
|
||
#include "../../include/ICU4XFixedDecimalFormat.hpp" | ||
|
||
#include <iostream> | ||
|
||
extern "C" void diplomat_init(); | ||
extern "C" void log_js(char* s) { | ||
std::cout<<"LOG: " <<s <<std::endl; | ||
} | ||
|
||
int runFixedDecimal() { | ||
#ifdef __EMSCRIPTEN__ | ||
diplomat_init(); | ||
#endif | ||
ICU4XLocale locale = ICU4XLocale::create("bn").value(); | ||
std::cout << "Running test for locale " << locale.tostring().ok().value() << std::endl; | ||
ICU4XDataProvider dp = ICU4XDataProvider::create_static().provider.value(); | ||
ICU4XFixedDecimalFormatOptions opts = {ICU4XFixedDecimalGroupingStrategy::Auto, ICU4XFixedDecimalSignDisplay::Auto}; | ||
ICU4XFixedDecimalFormat fdf = ICU4XFixedDecimalFormat::try_new(locale, dp, opts).fdf.value(); | ||
|
||
ICU4XFixedDecimal decimal = ICU4XFixedDecimal::create(1000007); | ||
std::string out = fdf.format(decimal).ok().value(); | ||
std::cout << "Formatted value is " << out << std::endl; | ||
if (out != "১০,০০,০০৭") { | ||
std::cout << "Output does not match expected output" << std::endl; | ||
return 1; | ||
} | ||
|
||
std::string out2; | ||
fdf.format_to_writeable(decimal, out2); | ||
std::cout << "Formatted writeable value is " << out2 << std::endl; | ||
if (out2 != "১০,০০,০০৭") { | ||
std::cout << "Output does not match expected output" << std::endl; | ||
return 1; | ||
} | ||
|
||
decimal.multiply_pow10(2); | ||
decimal.negate(); | ||
out = fdf.format(decimal).ok().value(); | ||
std::cout << "Value x100 and negated is " << out << std::endl; | ||
if (out != "-১০,০০,০০,৭০০") { | ||
std::cout << "Output does not match expected output" << std::endl; | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
|
||
#ifdef __EMSCRIPTEN__ | ||
EMSCRIPTEN_BINDINGS(testFixedDecimal) { | ||
emscripten::function("runFixedDecimal", &runFixedDecimal); | ||
} | ||
#endif | ||
|
||
#ifndef NOMAIN | ||
int main() { | ||
return runFixedDecimal(); | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters