2017-12-31 14:44:44 -08:00
|
|
|
//! Runtime support for the `wasm-bindgen` tool
|
|
|
|
//!
|
|
|
|
//! This crate contains the runtime support necessary for `wasm-bindgen` the
|
2018-02-07 16:41:33 -08:00
|
|
|
//! attribute and tool. Crates pull in the `#[wasm_bindgen]` attribute through
|
|
|
|
//! this crate and this crate also provides JS bindings through the `JsValue`
|
|
|
|
//! interface.
|
2017-12-31 14:44:44 -08:00
|
|
|
|
2018-04-05 18:25:22 -07:00
|
|
|
#![feature(use_extern_macros, wasm_import_module, try_reserve, unsize)]
|
2018-04-19 13:08:54 -07:00
|
|
|
#![no_std]
|
2017-12-14 19:31:01 -08:00
|
|
|
|
|
|
|
extern crate wasm_bindgen_macro;
|
|
|
|
|
2018-04-19 13:08:54 -07:00
|
|
|
use core::cell::UnsafeCell;
|
|
|
|
use core::ops::Deref;
|
|
|
|
use core::ptr;
|
2017-12-19 09:25:41 -08:00
|
|
|
|
2018-04-13 22:58:35 -07:00
|
|
|
use convert::FromWasmAbi;
|
2018-03-21 08:09:59 -07:00
|
|
|
|
2018-04-19 13:08:54 -07:00
|
|
|
macro_rules! if_std {
|
|
|
|
($($i:item)*) => ($(
|
|
|
|
#[cfg(feature = "std")] $i
|
|
|
|
)*)
|
|
|
|
}
|
|
|
|
|
2017-12-31 14:44:44 -08:00
|
|
|
/// A module which is typically glob imported from:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use wasm_bindgen::prelude::*;
|
|
|
|
/// ```
|
2017-12-14 19:31:01 -08:00
|
|
|
pub mod prelude {
|
|
|
|
pub use wasm_bindgen_macro::wasm_bindgen;
|
2018-02-06 15:04:46 -08:00
|
|
|
pub use JsValue;
|
2018-04-19 13:08:54 -07:00
|
|
|
|
|
|
|
if_std! {
|
|
|
|
pub use closure::Closure;
|
|
|
|
}
|
2017-12-14 19:31:01 -08:00
|
|
|
}
|
|
|
|
|
2018-02-06 07:56:14 -08:00
|
|
|
pub mod convert;
|
Overhaul how type information gets to the CLI
This commit is a complete overhaul of how the `#[wasm_bindgen]` macro
communicates type information to the CLI tool, and it's done in a somewhat...
unconventional fashion.
Today we've got a problem where the generated JS needs to understand the types
of each function exported or imported. This understanding is what enables it to
generate the appropriate JS wrappers and such. We want to, however, be quite
flexible and extensible in types that are supported across the boundary, which
means that internally we rely on the trait system to resolve what's what.
Communicating the type information historically was done by creating a four byte
"descriptor" and using associated type projections to communicate that to the
CLI tool. Unfortunately four bytes isn't a lot of space to cram information like
arguments to a generic function, tuple types, etc. In general this just wasn't
flexible enough and the way custom references were treated was also already a
bit of a hack.
This commit takes a radical step of creating a **descriptor function** for each
function imported/exported. The really crazy part is that the `wasm-bindgen` CLI
tool now embeds a wasm interpreter and executes these functions when the CLI
tool is invoked. By allowing arbitrary functions to get executed it's now *much*
easier to inform `wasm-bindgen` about complicated structures of types. Rest
assured though that all these descriptor functions are automatically unexported
and gc'd away, so this should not have any impact on binary sizes
A new internal trait, `WasmDescribe`, is added to represent a description of all
types, sort of like a serialization of the structure of a type that
`wasm-bindgen` can understand. This works by calling a special exported function
with a `u32` value a bunch of times. This means that when we run a descriptor we
effectively get a `Vec<u32>` in the `wasm-bindgen` CLI tool. This list of
integers can then be parsed into a rich `enum` for the JS generation to work
with.
This commit currently only retains feature parity with the previous
implementation. I hope to soon solve issues like #123, #104, and #111 with this
support.
2018-04-13 07:33:46 -07:00
|
|
|
pub mod describe;
|
2018-02-06 07:56:14 -08:00
|
|
|
|
2018-04-19 13:08:54 -07:00
|
|
|
if_std! {
|
|
|
|
extern crate std;
|
|
|
|
use std::prelude::v1::*;
|
|
|
|
pub mod closure;
|
|
|
|
}
|
|
|
|
|
2017-12-31 14:44:44 -08:00
|
|
|
/// Representation of an object owned by JS.
|
|
|
|
///
|
2018-02-06 15:04:46 -08:00
|
|
|
/// A `JsValue` doesn't actually live in Rust right now but actually in a table
|
2017-12-31 14:44:44 -08:00
|
|
|
/// owned by the `wasm-bindgen` generated JS glue code. Eventually the ownership
|
|
|
|
/// will transfer into wasm directly and this will likely become more efficient,
|
|
|
|
/// but for now it may be slightly slow.
|
2018-02-06 15:04:46 -08:00
|
|
|
pub struct JsValue {
|
2017-12-14 19:31:01 -08:00
|
|
|
idx: u32,
|
|
|
|
}
|
2017-12-19 09:25:41 -08:00
|
|
|
|
2018-02-06 15:04:46 -08:00
|
|
|
impl JsValue {
|
2017-12-31 15:45:47 -08:00
|
|
|
/// Creates a new JS value which is a string.
|
2017-12-31 14:44:44 -08:00
|
|
|
///
|
|
|
|
/// The utf-8 string provided is copied to the JS heap and the string will
|
|
|
|
/// be owned by the JS garbage collector.
|
2018-02-06 15:04:46 -08:00
|
|
|
pub fn from_str(s: &str) -> JsValue {
|
2017-12-31 14:44:44 -08:00
|
|
|
unsafe {
|
2018-02-06 15:04:46 -08:00
|
|
|
JsValue { idx: __wbindgen_string_new(s.as_ptr(), s.len()) }
|
2017-12-31 14:44:44 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-31 15:45:47 -08:00
|
|
|
/// Creates a new JS value which is a number.
|
|
|
|
///
|
|
|
|
/// This function creates a JS value representing a number (a heap
|
|
|
|
/// allocated number) and returns a handle to the JS version of it.
|
2018-02-06 15:04:46 -08:00
|
|
|
pub fn from_f64(n: f64) -> JsValue {
|
2017-12-31 15:45:47 -08:00
|
|
|
unsafe {
|
2018-02-06 15:04:46 -08:00
|
|
|
JsValue { idx: __wbindgen_number_new(n) }
|
2017-12-31 15:45:47 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new JS value which is a boolean.
|
|
|
|
///
|
|
|
|
/// This function creates a JS object representing a boolean (a heap
|
|
|
|
/// allocated boolean) and returns a handle to the JS version of it.
|
2018-02-06 15:04:46 -08:00
|
|
|
pub fn from_bool(b: bool) -> JsValue {
|
2017-12-31 15:45:47 -08:00
|
|
|
unsafe {
|
2018-02-06 15:04:46 -08:00
|
|
|
JsValue { idx: __wbindgen_boolean_new(b as u32) }
|
2017-12-31 15:45:47 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new JS value representing `undefined`.
|
2018-02-06 15:04:46 -08:00
|
|
|
pub fn undefined() -> JsValue {
|
2017-12-31 15:45:47 -08:00
|
|
|
unsafe {
|
2018-02-06 15:04:46 -08:00
|
|
|
JsValue { idx: __wbindgen_undefined_new() }
|
2017-12-31 15:45:47 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new JS value representing `null`.
|
2018-02-06 15:04:46 -08:00
|
|
|
pub fn null() -> JsValue {
|
2017-12-31 15:45:47 -08:00
|
|
|
unsafe {
|
2018-02-06 15:04:46 -08:00
|
|
|
JsValue { idx: __wbindgen_null_new() }
|
2017-12-31 15:45:47 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new JS symbol with the optional description specified.
|
|
|
|
///
|
|
|
|
/// This function will invoke the `Symbol` constructor in JS and return the
|
|
|
|
/// JS object corresponding to the symbol created.
|
2018-02-06 15:04:46 -08:00
|
|
|
pub fn symbol(description: Option<&str>) -> JsValue {
|
2017-12-31 15:45:47 -08:00
|
|
|
unsafe {
|
|
|
|
let ptr = description.map(|s| s.as_ptr()).unwrap_or(ptr::null());
|
|
|
|
let len = description.map(|s| s.len()).unwrap_or(0);
|
2018-02-06 15:04:46 -08:00
|
|
|
JsValue { idx: __wbindgen_symbol_new(ptr, len) }
|
2017-12-31 15:45:47 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-02 09:58:27 -07:00
|
|
|
// #[doc(hidden)]
|
|
|
|
// pub unsafe fn __from_idx(idx: u32) -> JsValue {
|
|
|
|
// JsValue { idx }
|
|
|
|
// }
|
2018-02-06 07:56:14 -08:00
|
|
|
//
|
|
|
|
// #[doc(hidden)]
|
|
|
|
// pub fn __get_idx(&self) -> u32 {
|
|
|
|
// self.idx
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// #[doc(hidden)]
|
|
|
|
// pub fn __into_idx(self) -> u32 {
|
|
|
|
// let ret = self.idx;
|
|
|
|
// mem::forget(self);
|
|
|
|
// return ret
|
|
|
|
// }
|
2017-12-31 15:45:47 -08:00
|
|
|
|
|
|
|
/// Returns the `f64` value of this JS value if it's an instance of a
|
|
|
|
/// number.
|
|
|
|
///
|
|
|
|
/// If this JS value is not an instance of a number then this returns
|
|
|
|
/// `None`.
|
|
|
|
pub fn as_f64(&self) -> Option<f64> {
|
|
|
|
let mut invalid = 0;
|
|
|
|
unsafe {
|
|
|
|
let ret = __wbindgen_number_get(self.idx, &mut invalid);
|
|
|
|
if invalid == 1 {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(ret)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the `String` of this JS value if it's an instance of a
|
|
|
|
/// string and it's valid utf-8.
|
|
|
|
///
|
|
|
|
/// If this JS value is not an instance of a string or if it's not valid
|
|
|
|
/// utf-8 then this returns `None`.
|
2018-04-19 13:08:54 -07:00
|
|
|
#[cfg(feature = "std")]
|
2017-12-31 15:45:47 -08:00
|
|
|
pub fn as_string(&self) -> Option<String> {
|
|
|
|
unsafe {
|
|
|
|
let mut len = 0;
|
|
|
|
let ptr = __wbindgen_string_get(self.idx, &mut len);
|
|
|
|
if ptr.is_null() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
let data = Vec::from_raw_parts(ptr, len, len);
|
|
|
|
Some(String::from_utf8_unchecked(data))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the `bool` value of this JS value if it's an instance of a
|
|
|
|
/// boolean.
|
|
|
|
///
|
|
|
|
/// If this JS value is not an instance of a boolean then this returns
|
|
|
|
/// `None`.
|
|
|
|
pub fn as_bool(&self) -> Option<bool> {
|
|
|
|
unsafe {
|
|
|
|
match __wbindgen_boolean_get(self.idx) {
|
|
|
|
0 => Some(false),
|
|
|
|
1 => Some(true),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Tests whether this JS value is `null`
|
|
|
|
pub fn is_null(&self) -> bool {
|
|
|
|
unsafe {
|
|
|
|
__wbindgen_is_null(self.idx) == 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Tests whether this JS value is `undefined`
|
|
|
|
pub fn is_undefined(&self) -> bool {
|
|
|
|
unsafe {
|
|
|
|
__wbindgen_is_undefined(self.idx) == 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Tests whether the type of this JS value is `symbol`
|
|
|
|
pub fn is_symbol(&self) -> bool {
|
|
|
|
unsafe {
|
|
|
|
__wbindgen_is_symbol(self.idx) == 1
|
|
|
|
}
|
|
|
|
}
|
2017-12-19 09:25:41 -08:00
|
|
|
}
|
|
|
|
|
2018-02-06 15:04:46 -08:00
|
|
|
impl<'a> From<&'a str> for JsValue {
|
|
|
|
fn from(s: &'a str) -> JsValue {
|
|
|
|
JsValue::from_str(s)
|
2017-12-31 14:44:44 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:08:54 -07:00
|
|
|
if_std! {
|
|
|
|
impl<'a> From<&'a String> for JsValue {
|
|
|
|
fn from(s: &'a String) -> JsValue {
|
|
|
|
JsValue::from_str(s)
|
|
|
|
}
|
2017-12-31 15:45:47 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-06 15:04:46 -08:00
|
|
|
impl From<bool> for JsValue {
|
|
|
|
fn from(s: bool) -> JsValue {
|
|
|
|
JsValue::from_bool(s)
|
2017-12-31 15:45:47 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! numbers {
|
|
|
|
($($n:ident)*) => ($(
|
2018-02-06 15:04:46 -08:00
|
|
|
impl From<$n> for JsValue {
|
|
|
|
fn from(n: $n) -> JsValue {
|
|
|
|
JsValue::from_f64(n.into())
|
2017-12-31 15:45:47 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)*)
|
|
|
|
}
|
|
|
|
|
|
|
|
numbers! { i8 u8 i16 u16 i32 u32 f32 f64 }
|
|
|
|
|
2018-03-14 14:33:53 -07:00
|
|
|
#[wasm_import_module = "__wbindgen_placeholder__"]
|
2017-12-19 09:25:41 -08:00
|
|
|
extern {
|
2017-12-19 20:00:25 -08:00
|
|
|
fn __wbindgen_object_clone_ref(idx: u32) -> u32;
|
|
|
|
fn __wbindgen_object_drop_ref(idx: u32);
|
2017-12-31 14:44:44 -08:00
|
|
|
fn __wbindgen_string_new(ptr: *const u8, len: usize) -> u32;
|
2017-12-31 15:45:47 -08:00
|
|
|
fn __wbindgen_number_new(f: f64) -> u32;
|
|
|
|
fn __wbindgen_number_get(idx: u32, invalid: *mut u8) -> f64;
|
|
|
|
fn __wbindgen_null_new() -> u32;
|
|
|
|
fn __wbindgen_undefined_new() -> u32;
|
|
|
|
fn __wbindgen_is_null(idx: u32) -> u32;
|
|
|
|
fn __wbindgen_is_undefined(idx: u32) -> u32;
|
|
|
|
fn __wbindgen_boolean_new(val: u32) -> u32;
|
|
|
|
fn __wbindgen_boolean_get(idx: u32) -> u32;
|
|
|
|
fn __wbindgen_symbol_new(ptr: *const u8, len: usize) -> u32;
|
|
|
|
fn __wbindgen_is_symbol(idx: u32) -> u32;
|
|
|
|
fn __wbindgen_string_get(idx: u32, len: *mut usize) -> *mut u8;
|
2018-03-14 14:33:53 -07:00
|
|
|
fn __wbindgen_throw(a: *const u8, b: usize) -> !;
|
2018-04-05 18:25:22 -07:00
|
|
|
|
|
|
|
fn __wbindgen_cb_arity0(a: u32, b: u32, c: u32) -> u32;
|
|
|
|
fn __wbindgen_cb_arity1(a: u32, b: u32, c: u32) -> u32;
|
|
|
|
fn __wbindgen_cb_arity2(a: u32, b: u32, c: u32) -> u32;
|
|
|
|
fn __wbindgen_cb_arity3(a: u32, b: u32, c: u32) -> u32;
|
|
|
|
fn __wbindgen_cb_arity4(a: u32, b: u32, c: u32) -> u32;
|
|
|
|
fn __wbindgen_cb_arity5(a: u32, b: u32, c: u32) -> u32;
|
|
|
|
fn __wbindgen_cb_arity6(a: u32, b: u32, c: u32) -> u32;
|
|
|
|
fn __wbindgen_cb_arity7(a: u32, b: u32, c: u32) -> u32;
|
|
|
|
fn __wbindgen_cb_drop(idx: u32);
|
2018-04-06 08:49:21 -07:00
|
|
|
fn __wbindgen_cb_forget(idx: u32);
|
Overhaul how type information gets to the CLI
This commit is a complete overhaul of how the `#[wasm_bindgen]` macro
communicates type information to the CLI tool, and it's done in a somewhat...
unconventional fashion.
Today we've got a problem where the generated JS needs to understand the types
of each function exported or imported. This understanding is what enables it to
generate the appropriate JS wrappers and such. We want to, however, be quite
flexible and extensible in types that are supported across the boundary, which
means that internally we rely on the trait system to resolve what's what.
Communicating the type information historically was done by creating a four byte
"descriptor" and using associated type projections to communicate that to the
CLI tool. Unfortunately four bytes isn't a lot of space to cram information like
arguments to a generic function, tuple types, etc. In general this just wasn't
flexible enough and the way custom references were treated was also already a
bit of a hack.
This commit takes a radical step of creating a **descriptor function** for each
function imported/exported. The really crazy part is that the `wasm-bindgen` CLI
tool now embeds a wasm interpreter and executes these functions when the CLI
tool is invoked. By allowing arbitrary functions to get executed it's now *much*
easier to inform `wasm-bindgen` about complicated structures of types. Rest
assured though that all these descriptor functions are automatically unexported
and gc'd away, so this should not have any impact on binary sizes
A new internal trait, `WasmDescribe`, is added to represent a description of all
types, sort of like a serialization of the structure of a type that
`wasm-bindgen` can understand. This works by calling a special exported function
with a `u32` value a bunch of times. This means that when we run a descriptor we
effectively get a `Vec<u32>` in the `wasm-bindgen` CLI tool. This list of
integers can then be parsed into a rich `enum` for the JS generation to work
with.
This commit currently only retains feature parity with the previous
implementation. I hope to soon solve issues like #123, #104, and #111 with this
support.
2018-04-13 07:33:46 -07:00
|
|
|
|
|
|
|
fn __wbindgen_describe(v: u32);
|
2017-12-19 09:25:41 -08:00
|
|
|
}
|
|
|
|
|
2018-02-06 15:04:46 -08:00
|
|
|
impl Clone for JsValue {
|
|
|
|
fn clone(&self) -> JsValue {
|
2017-12-19 09:25:41 -08:00
|
|
|
unsafe {
|
2017-12-19 20:00:25 -08:00
|
|
|
let idx = __wbindgen_object_clone_ref(self.idx);
|
2018-02-06 15:04:46 -08:00
|
|
|
JsValue { idx }
|
2017-12-19 09:25:41 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-06 15:04:46 -08:00
|
|
|
impl Drop for JsValue {
|
2017-12-19 09:25:41 -08:00
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe {
|
2017-12-19 20:00:25 -08:00
|
|
|
__wbindgen_object_drop_ref(self.idx);
|
2017-12-19 09:25:41 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-19 19:50:06 -08:00
|
|
|
|
2018-03-21 08:09:59 -07:00
|
|
|
/// Wrapper type for imported statics.
|
|
|
|
///
|
|
|
|
/// This type is used whenever a `static` is imported from a JS module, for
|
|
|
|
/// example this import:
|
|
|
|
///
|
|
|
|
/// ```ignore
|
|
|
|
/// #[wasm_bindgen]
|
|
|
|
/// extern {
|
|
|
|
/// static console: JsValue;
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// will generate in Rust a value that looks like:
|
|
|
|
///
|
|
|
|
/// ```ignore
|
|
|
|
/// static console: JsStatic<JsValue> = ...;
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// This type implements `Deref` to the inner type so it's typically used as if
|
|
|
|
/// it were `&T`.
|
|
|
|
pub struct JsStatic<T> {
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub __inner: UnsafeCell<Option<T>>,
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub __init: fn() -> T,
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl<T: Sync> Sync for JsStatic<T> {}
|
|
|
|
unsafe impl<T: Send> Send for JsStatic<T> {}
|
|
|
|
|
2018-04-13 22:58:35 -07:00
|
|
|
impl<T: FromWasmAbi + 'static> Deref for JsStatic<T> {
|
2018-03-21 08:09:59 -07:00
|
|
|
type Target = T;
|
|
|
|
fn deref(&self) -> &T {
|
|
|
|
unsafe {
|
|
|
|
(*self.__inner.get()).get_or_insert_with(|| {
|
|
|
|
(self.__init)()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-31 14:44:44 -08:00
|
|
|
/// Throws a JS exception.
|
|
|
|
///
|
|
|
|
/// This function will throw a JS exception with the message provided. The
|
|
|
|
/// function will not return as the wasm stack will be popped when the exception
|
|
|
|
/// is thrown.
|
2017-12-19 19:50:06 -08:00
|
|
|
#[cold]
|
2017-12-21 12:25:13 -08:00
|
|
|
#[inline(never)]
|
2017-12-19 19:50:06 -08:00
|
|
|
pub fn throw(s: &str) -> ! {
|
|
|
|
unsafe {
|
|
|
|
__wbindgen_throw(s.as_ptr(), s.len());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub mod __rt {
|
2018-04-19 13:08:54 -07:00
|
|
|
use core::cell::{Cell, UnsafeCell};
|
|
|
|
use core::ops::{Deref, DerefMut};
|
|
|
|
pub extern crate core;
|
|
|
|
#[cfg(feature = "std")]
|
|
|
|
pub extern crate std;
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
#[cfg(feature = "std")]
|
|
|
|
macro_rules! __wbindgen_if_not_std {
|
|
|
|
($($i:item)*) => ()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
#[cfg(not(feature = "std"))]
|
|
|
|
macro_rules! __wbindgen_if_not_std {
|
|
|
|
($($i:item)*) => ($($i)*)
|
|
|
|
}
|
2017-12-19 19:50:06 -08:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn assert_not_null<T>(s: *mut T) {
|
|
|
|
if s.is_null() {
|
2017-12-21 12:25:13 -08:00
|
|
|
throw_null();
|
2017-12-19 19:50:06 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-21 12:25:13 -08:00
|
|
|
#[cold]
|
|
|
|
#[inline(never)]
|
|
|
|
fn throw_null() -> ! {
|
|
|
|
super::throw("null pointer passed to rust");
|
|
|
|
}
|
|
|
|
|
2017-12-19 19:50:06 -08:00
|
|
|
/// 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.
|
2018-04-05 18:25:22 -07:00
|
|
|
pub struct WasmRefCell<T: ?Sized> {
|
2017-12-19 19:50:06 -08:00
|
|
|
borrow: Cell<usize>,
|
|
|
|
value: UnsafeCell<T>,
|
|
|
|
}
|
|
|
|
|
2018-04-05 18:25:22 -07:00
|
|
|
impl<T: ?Sized> WasmRefCell<T> {
|
|
|
|
pub fn new(value: T) -> WasmRefCell<T> where T: Sized {
|
2017-12-19 19:50:06 -08:00
|
|
|
WasmRefCell {
|
|
|
|
value: UnsafeCell::new(value),
|
|
|
|
borrow: Cell::new(0),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-21 12:28:30 -08:00
|
|
|
pub fn get_mut(&mut self) -> &mut T {
|
|
|
|
unsafe {
|
|
|
|
&mut *self.value.get()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-19 19:50:06 -08:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 18:25:22 -07:00
|
|
|
pub fn into_inner(self) -> T where T: Sized {
|
2018-01-29 21:20:38 -08:00
|
|
|
self.value.into_inner()
|
2017-12-19 19:50:06 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 18:25:22 -07:00
|
|
|
pub struct Ref<'b, T: ?Sized + 'b> {
|
2017-12-19 19:50:06 -08:00
|
|
|
value: &'b T,
|
|
|
|
borrow: &'b Cell<usize>,
|
|
|
|
}
|
|
|
|
|
2018-04-05 18:25:22 -07:00
|
|
|
impl<'b, T: ?Sized> Deref for Ref<'b, T> {
|
2017-12-19 19:50:06 -08:00
|
|
|
type Target = T;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn deref(&self) -> &T {
|
|
|
|
self.value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 18:25:22 -07:00
|
|
|
impl<'b, T: ?Sized> Drop for Ref<'b, T> {
|
2017-12-19 19:50:06 -08:00
|
|
|
fn drop(&mut self) {
|
|
|
|
self.borrow.set(self.borrow.get() - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 18:25:22 -07:00
|
|
|
pub struct RefMut<'b, T: ?Sized + 'b> {
|
2017-12-19 19:50:06 -08:00
|
|
|
value: &'b mut T,
|
|
|
|
borrow: &'b Cell<usize>,
|
|
|
|
}
|
|
|
|
|
2018-04-05 18:25:22 -07:00
|
|
|
impl<'b, T: ?Sized> Deref for RefMut<'b, T> {
|
2017-12-19 19:50:06 -08:00
|
|
|
type Target = T;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn deref(&self) -> &T {
|
|
|
|
self.value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 18:25:22 -07:00
|
|
|
impl<'b, T: ?Sized> DerefMut for RefMut<'b, T> {
|
2017-12-19 19:50:06 -08:00
|
|
|
#[inline]
|
|
|
|
fn deref_mut(&mut self) -> &mut T {
|
|
|
|
self.value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 18:25:22 -07:00
|
|
|
impl<'b, T: ?Sized> Drop for RefMut<'b, T> {
|
2017-12-19 19:50:06 -08:00
|
|
|
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");
|
|
|
|
}
|
2018-02-06 08:58:15 -08:00
|
|
|
|
2018-04-19 13:08:54 -07:00
|
|
|
if_std! {
|
|
|
|
use std::prelude::v1::*;
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
pub extern fn __wbindgen_malloc(size: usize) -> *mut u8 {
|
|
|
|
use core::mem;
|
|
|
|
|
|
|
|
let mut ret = Vec::new();
|
|
|
|
if ret.try_reserve_exact(size).is_err() {
|
|
|
|
super::throw("invalid malloc request");
|
|
|
|
}
|
|
|
|
let ptr = ret.as_mut_ptr();
|
|
|
|
mem::forget(ret);
|
|
|
|
return ptr
|
2018-02-06 08:58:15 -08:00
|
|
|
}
|
|
|
|
|
2018-04-19 13:08:54 -07:00
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern fn __wbindgen_free(ptr: *mut u8, size: usize) {
|
|
|
|
drop(Vec::<u8>::from_raw_parts(ptr, 0, size));
|
|
|
|
}
|
2018-02-06 08:58:15 -08:00
|
|
|
}
|
|
|
|
|
2018-02-10 10:06:56 -08:00
|
|
|
pub fn link_this_library() {}
|
2017-12-19 19:50:06 -08:00
|
|
|
}
|