mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-23 09:41:33 +00:00
Start optimizing code size:
* Use a bundled custom `WasmRefCell` instead of the one in the standard library. This one primarily doesn't panic via libstd which means that its code footprint is much smaller. * Add a `throw` function to `wasm_bindgen`-the-crate which can be used to throw an exception in JS from Rust. This is useful as a cheap way to throw an exception code-wise (little code bloat) and it's also a great way of reporting error messages to JS! * Cut down on the code size of `__wbindgen_malloc` by aborting on huge requests earlier. * Use a custom `assert_not_null` function which delegates to `throw` to test for incoming null pointers
This commit is contained in:
138
src/lib.rs
138
src/lib.rs
@ -53,3 +53,141 @@ impl Drop for JsObject {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cold]
|
||||
pub fn throw(s: &str) -> ! {
|
||||
extern {
|
||||
fn __wbindgen_throw(a: *const u8, b: usize) -> !;
|
||||
}
|
||||
unsafe {
|
||||
__wbindgen_throw(s.as_ptr(), s.len());
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub mod __rt {
|
||||
use std::cell::{Cell, UnsafeCell};
|
||||
use std::ops::{Deref, DerefMut};
|
||||
|
||||
#[inline]
|
||||
pub fn assert_not_null<T>(s: *mut T) {
|
||||
if s.is_null() {
|
||||
super::throw("null pointer passed to rust");
|
||||
}
|
||||
}
|
||||
|
||||
/// A vendored version of `RefCell` from the standard library.
|
||||
///
|
||||
/// Now why, you may ask, would we do that? Surely `RefCell` in libstd is
|
||||
/// quite good. And you're right, it is indeed quite good! Functionally
|
||||
/// nothing more is needed from `RefCell` in the standard library but for
|
||||
/// now this crate is also sort of optimizing for compiled code size.
|
||||
///
|
||||
/// One major factor to larger binaries in Rust is when a panic happens.
|
||||
/// Panicking in the standard library involves a fair bit of machinery
|
||||
/// (formatting, panic hooks, synchronization, etc). It's all worthwhile if
|
||||
/// you need it but for something like `WasmRefCell` here we don't actually
|
||||
/// need all that!
|
||||
///
|
||||
/// This is just a wrapper around all Rust objects passed to JS intended to
|
||||
/// guard accidental reentrancy, so this vendored version is intended solely
|
||||
/// to not panic in libstd. Instead when it "panics" it calls our `throw`
|
||||
/// function in this crate which raises an error in JS.
|
||||
pub struct WasmRefCell<T> {
|
||||
borrow: Cell<usize>,
|
||||
value: UnsafeCell<T>,
|
||||
}
|
||||
|
||||
impl<T> WasmRefCell<T> {
|
||||
pub fn new(value: T) -> WasmRefCell<T> {
|
||||
WasmRefCell {
|
||||
value: UnsafeCell::new(value),
|
||||
borrow: Cell::new(0),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn borrow(&self) -> Ref<T> {
|
||||
unsafe {
|
||||
if self.borrow.get() == usize::max_value() {
|
||||
borrow_fail();
|
||||
}
|
||||
self.borrow.set(self.borrow.get() + 1);
|
||||
Ref {
|
||||
value: &*self.value.get(),
|
||||
borrow: &self.borrow,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn borrow_mut(&self) -> RefMut<T> {
|
||||
unsafe {
|
||||
if self.borrow.get() != 0 {
|
||||
borrow_fail();
|
||||
}
|
||||
self.borrow.set(usize::max_value());
|
||||
RefMut {
|
||||
value: &mut *self.value.get(),
|
||||
borrow: &self.borrow,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn into_inner(self) -> T {
|
||||
unsafe {
|
||||
self.value.into_inner()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Ref<'b, T: 'b> {
|
||||
value: &'b T,
|
||||
borrow: &'b Cell<usize>,
|
||||
}
|
||||
|
||||
impl<'b, T> Deref for Ref<'b, T> {
|
||||
type Target = T;
|
||||
|
||||
#[inline]
|
||||
fn deref(&self) -> &T {
|
||||
self.value
|
||||
}
|
||||
}
|
||||
|
||||
impl<'b, T> Drop for Ref<'b, T> {
|
||||
fn drop(&mut self) {
|
||||
self.borrow.set(self.borrow.get() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
pub struct RefMut<'b, T: 'b> {
|
||||
value: &'b mut T,
|
||||
borrow: &'b Cell<usize>,
|
||||
}
|
||||
|
||||
impl<'b, T> Deref for RefMut<'b, T> {
|
||||
type Target = T;
|
||||
|
||||
#[inline]
|
||||
fn deref(&self) -> &T {
|
||||
self.value
|
||||
}
|
||||
}
|
||||
|
||||
impl<'b, T> DerefMut for RefMut<'b, T> {
|
||||
#[inline]
|
||||
fn deref_mut(&mut self) -> &mut T {
|
||||
self.value
|
||||
}
|
||||
}
|
||||
|
||||
impl<'b, T> Drop for RefMut<'b, T> {
|
||||
fn drop(&mut self) {
|
||||
self.borrow.set(0);
|
||||
}
|
||||
}
|
||||
|
||||
fn borrow_fail() -> ! {
|
||||
super::throw("recursive use of an object detected which would lead to \
|
||||
unsafe aliasing in rust");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user