From 91295f4d16f81d444e8b72e16a92c539387ce246 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Tue, 20 Mar 2018 15:19:45 -0700 Subject: [PATCH] Fix wasm sizes on nightly Looks like the recent changes to `Vec::with_capacity` meant that our previous codegen to avoid panics no longer avoids panics. Let's pick up the `try_reserve` unstable feature for now and hopefully it'll be stabilized before the other pieces in the future. --- src/lib.rs | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 31088b33..9cef30bf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,7 +5,7 @@ //! this crate and this crate also provides JS bindings through the `JsValue` //! interface. -#![feature(use_extern_macros)] +#![feature(use_extern_macros, try_reserve)] extern crate wasm_bindgen_macro; @@ -399,18 +399,10 @@ pub mod __rt { #[no_mangle] pub extern fn __wbindgen_malloc(size: usize) -> *mut u8 { - // Any malloc request this big is bogus anyway. If this actually - // goes down to `Vec` we trigger a whole bunch of panicking - // machinery to get pulled in from libstd anyway as it'll verify - // the size passed in below. - // - // Head this all off by just aborting on too-big sizes. This - // avoids panicking (code bloat) and gives a better error - // message too hopefully. - if size >= usize::max_value() / 2 { + let mut ret = Vec::new(); + if ret.try_reserve_exact(size).is_err() { super::throw("invalid malloc request"); } - let mut ret = Vec::with_capacity(size); let ptr = ret.as_mut_ptr(); mem::forget(ret); return ptr