Skip to content

Commit a3f740b

Browse files
committed
Re-support WASM via simple stub headers
libsecp256k1 really only barely uses libc at all, and in practice, things like memcpy/memcmp get optimized into something other than a libc call. Thus, if we provide simple stub headers, things seem to work with wasm-pack just fine.
1 parent 9aa768d commit a3f740b

File tree

7 files changed

+52
-8
lines changed

7 files changed

+52
-8
lines changed

.travis.yml

+15-8
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
language: rust
2-
# cache:
3-
# directories:
4-
# - cargo_web
2+
cache:
3+
directories:
4+
- wasm
55

66
rust:
77
- stable
88
- beta
99
- nightly
1010
- 1.22.0
11+
distro: bionic
1112
os:
1213
- linux
1314
- windows
1415

1516
addons:
1617
chrome: stable
18+
apt:
19+
packages:
20+
- clang-9
21+
- nodejs
1722

1823
matrix:
1924
exclude:
@@ -46,8 +51,10 @@ script:
4651
cd no_std_test &&
4752
cargo run --release | grep -q "Verified Successfully";
4853
fi
49-
- #if [ ${TRAVIS_RUST_VERSION} == "stable" -a "$TRAVIS_OS_NAME" = "linux" ]; then
50-
#CARGO_TARGET_DIR=cargo_web cargo install --verbose --force cargo-web &&
51-
#cargo web build --verbose --target=asmjs-unknown-emscripten &&
52-
#cargo web test --verbose --target=asmjs-unknown-emscripten;
53-
#fi
54+
- if [ ${TRAVIS_RUST_VERSION} == "stable" -a "$TRAVIS_OS_NAME" = "linux" ]; then
55+
clang --version &&
56+
CARGO_TARGET_DIR=wasm cargo install --verbose --force wasm-pack &&
57+
sed -i 's/\[lib\]/[lib]\ncrate-type = ["cdylib", "rlib"]/' Cargo.toml &&
58+
CC=clang-9 wasm-pack build &&
59+
CC=clang-9 wasm-pack test --node;
60+
fi

Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ rand_core = "0.4"
4646
serde_test = "1.0"
4747
bitcoin_hashes = "0.7"
4848

49+
[target.wasm32-unknown-unknown.dev-dependencies]
50+
wasm-bindgen-test = "0.3"
51+
rand = { version = "0.6", features = ["wasm-bindgen"] }
52+
4953
[dependencies.rand]
5054
version = "0.6"
5155
optional = true

secp256k1-sys/build.rs

+4
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ fn main() {
8484
.define("USE_SCALAR_8X32", Some("1"));
8585
}
8686

87+
if env::var("TARGET").unwrap() == "wasm32-unknown-unknown" {
88+
base_config.include("wasm-sysroot");
89+
}
90+
8791
// secp256k1
8892
base_config.file("depend/secp256k1/contrib/lax_der_parsing.c")
8993
.file("depend/secp256k1/src/secp256k1.c")

secp256k1-sys/wasm-sysroot/stdio.h

Whitespace-only changes.

secp256k1-sys/wasm-sysroot/stdlib.h

Whitespace-only changes.

secp256k1-sys/wasm-sysroot/string.h

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
void *memset(void *s, int c, unsigned long n);
2+
void *memcpy(void *dest, const void *src, unsigned long n);

src/lib.rs

+27
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,33 @@ mod tests {
10601060
assert_tokens(&sig.compact(), &[Token::BorrowedBytes(&SIG_BYTES[..])]);
10611061
assert_tokens(&sig.readable(), &[Token::BorrowedStr(SIG_STR)]);
10621062
}
1063+
1064+
// For WASM, just run through our general tests in this file all at once.
1065+
#[cfg(target_arch = "wasm32")]
1066+
extern crate wasm_bindgen_test;
1067+
#[cfg(target_arch = "wasm32")]
1068+
use self::wasm_bindgen_test::*;
1069+
#[cfg(target_arch = "wasm32")]
1070+
#[wasm_bindgen_test]
1071+
fn stuff() {
1072+
test_manual_create_destroy();
1073+
test_raw_ctx();
1074+
// Note that, sadly, WASM doesn't currently properly unwind panics, so use of the library
1075+
// via unsafe primitives may cause abort() instead of catch-able panics.
1076+
/*assert!(std::panic::catch_unwind(|| {
1077+
test_panic_raw_ctx();
1078+
}).is_err());*/
1079+
test_preallocation();
1080+
capabilities();
1081+
signature_serialize_roundtrip();
1082+
signature_display();
1083+
signature_lax_der();
1084+
sign_and_verify();
1085+
sign_and_verify_extreme();
1086+
sign_and_verify_fail();
1087+
test_bad_slice();
1088+
test_low_s();
1089+
}
10631090
}
10641091

10651092
#[cfg(all(test, feature = "unstable"))]

0 commit comments

Comments
 (0)