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-19 13:08:54 -07:00
|
|
|
#![no_std]
|
2018-07-19 14:57:04 -05:00
|
|
|
#![doc(html_root_url = "https://docs.rs/wasm-bindgen/0.2")]
|
2018-08-19 14:45:59 -07:00
|
|
|
#![cfg_attr(feature = "nightly", feature(unsize))]
|
2017-12-14 19:31:01 -08:00
|
|
|
|
2018-04-26 19:03:46 -07:00
|
|
|
#[cfg(feature = "serde-serialize")]
|
|
|
|
extern crate serde;
|
|
|
|
#[cfg(feature = "serde-serialize")]
|
|
|
|
extern crate serde_json;
|
|
|
|
|
2017-12-14 19:31:01 -08:00
|
|
|
extern crate wasm_bindgen_macro;
|
|
|
|
|
2018-07-05 20:14:38 -07:00
|
|
|
use core::fmt;
|
2018-10-10 12:50:54 -07:00
|
|
|
use core::marker;
|
2018-09-17 18:26:45 -07:00
|
|
|
use core::mem;
|
2018-09-24 13:49:12 -07:00
|
|
|
use core::ops::{Deref, DerefMut};
|
2018-04-19 13:08:54 -07:00
|
|
|
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-08-04 09:02:31 -07:00
|
|
|
mod cast;
|
|
|
|
pub use cast::JsCast;
|
|
|
|
|
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,
|
2018-10-10 12:50:54 -07:00
|
|
|
_marker: marker::PhantomData<*mut u8>, // not at all threadsafe
|
2017-12-14 19:31:01 -08:00
|
|
|
}
|
2017-12-19 09:25:41 -08:00
|
|
|
|
2018-11-29 18:15:36 -08:00
|
|
|
const JSIDX_OFFSET: u32 = 32; // keep in sync with js/mod.rs
|
|
|
|
const JSIDX_UNDEFINED: u32 = JSIDX_OFFSET + 0;
|
|
|
|
const JSIDX_NULL: u32 = JSIDX_OFFSET + 1;
|
|
|
|
const JSIDX_TRUE: u32 = JSIDX_OFFSET + 2;
|
|
|
|
const JSIDX_FALSE: u32 = JSIDX_OFFSET + 3;
|
|
|
|
const JSIDX_RESERVED: u32 = JSIDX_OFFSET + 4;
|
2018-06-01 16:46:42 -05:00
|
|
|
|
2018-02-06 15:04:46 -08:00
|
|
|
impl JsValue {
|
2018-06-08 14:22:18 -07:00
|
|
|
/// The `null` JS value constant.
|
2018-10-10 12:50:54 -07:00
|
|
|
pub const NULL: JsValue = JsValue {
|
|
|
|
idx: JSIDX_NULL,
|
|
|
|
_marker: marker::PhantomData,
|
|
|
|
};
|
2018-06-08 14:22:18 -07:00
|
|
|
|
|
|
|
/// The `undefined` JS value constant.
|
2018-06-27 22:42:34 -07:00
|
|
|
pub const UNDEFINED: JsValue = JsValue {
|
|
|
|
idx: JSIDX_UNDEFINED,
|
2018-10-10 12:50:54 -07:00
|
|
|
_marker: marker::PhantomData,
|
2018-06-27 22:42:34 -07:00
|
|
|
};
|
2018-06-08 14:22:18 -07:00
|
|
|
|
|
|
|
/// The `true` JS value constant.
|
2018-10-10 12:50:54 -07:00
|
|
|
pub const TRUE: JsValue = JsValue {
|
|
|
|
idx: JSIDX_TRUE,
|
|
|
|
_marker: marker::PhantomData,
|
|
|
|
};
|
2018-06-08 14:22:18 -07:00
|
|
|
|
|
|
|
/// The `false` JS value constant.
|
2018-10-10 12:50:54 -07:00
|
|
|
pub const FALSE: JsValue = JsValue {
|
|
|
|
idx: JSIDX_FALSE,
|
|
|
|
_marker: marker::PhantomData,
|
|
|
|
};
|
|
|
|
|
2018-10-28 13:16:10 -07:00
|
|
|
#[inline]
|
2018-10-10 12:50:54 -07:00
|
|
|
fn _new(idx: u32) -> JsValue {
|
|
|
|
JsValue {
|
|
|
|
idx,
|
|
|
|
_marker: marker::PhantomData,
|
|
|
|
}
|
|
|
|
}
|
2018-06-08 14:22:18 -07:00
|
|
|
|
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-10-01 15:27:34 -07:00
|
|
|
#[inline]
|
2018-02-06 15:04:46 -08:00
|
|
|
pub fn from_str(s: &str) -> JsValue {
|
2018-11-27 12:07:59 -08:00
|
|
|
unsafe { JsValue::_new(__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-10-01 15:27:34 -07:00
|
|
|
#[inline]
|
2018-02-06 15:04:46 -08:00
|
|
|
pub fn from_f64(n: f64) -> JsValue {
|
2018-11-27 12:07:59 -08:00
|
|
|
unsafe { JsValue::_new(__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-10-01 15:27:34 -07:00
|
|
|
#[inline]
|
2018-02-06 15:04:46 -08:00
|
|
|
pub fn from_bool(b: bool) -> JsValue {
|
2018-11-27 12:07:59 -08:00
|
|
|
if b {
|
|
|
|
JsValue::TRUE
|
|
|
|
} else {
|
|
|
|
JsValue::FALSE
|
|
|
|
}
|
2017-12-31 15:45:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new JS value representing `undefined`.
|
2018-10-01 15:27:34 -07:00
|
|
|
#[inline]
|
2018-02-06 15:04:46 -08:00
|
|
|
pub fn undefined() -> JsValue {
|
2018-10-01 15:27:34 -07:00
|
|
|
JsValue::UNDEFINED
|
2017-12-31 15:45:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new JS value representing `null`.
|
2018-10-01 15:27:34 -07:00
|
|
|
#[inline]
|
2018-02-06 15:04:46 -08:00
|
|
|
pub fn null() -> JsValue {
|
2018-10-01 15:27:34 -07:00
|
|
|
JsValue::NULL
|
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-10-10 12:50:54 -07:00
|
|
|
JsValue::_new(__wbindgen_symbol_new(ptr, len))
|
2017-12-31 15:45:47 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-26 19:03:46 -07:00
|
|
|
/// Creates a new `JsValue` from the JSON serialization of the object `t`
|
|
|
|
/// provided.
|
|
|
|
///
|
|
|
|
/// This function will serialize the provided value `t` to a JSON string,
|
|
|
|
/// send the JSON string to JS, parse it into a JS object, and then return
|
|
|
|
/// a handle to the JS object. This is unlikely to be super speedy so it's
|
|
|
|
/// not recommended for large payloads, but it's a nice to have in some
|
|
|
|
/// situations!
|
|
|
|
///
|
|
|
|
/// Usage of this API requires activating the `serde-serialize` feature of
|
|
|
|
/// the `wasm-bindgen` crate.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// Returns any error encountered when serializing `T` into JSON.
|
|
|
|
#[cfg(feature = "serde-serialize")]
|
|
|
|
pub fn from_serde<T>(t: &T) -> serde_json::Result<JsValue>
|
2018-06-27 22:42:34 -07:00
|
|
|
where
|
|
|
|
T: serde::ser::Serialize + ?Sized,
|
2018-04-26 19:03:46 -07:00
|
|
|
{
|
|
|
|
let s = serde_json::to_string(t)?;
|
2018-11-27 12:07:59 -08:00
|
|
|
unsafe { Ok(JsValue::_new(__wbindgen_json_parse(s.as_ptr(), s.len()))) }
|
2018-04-26 19:03:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Invokes `JSON.stringify` on this value and then parses the resulting
|
|
|
|
/// JSON into an arbitrary Rust value.
|
|
|
|
///
|
|
|
|
/// This function will first call `JSON.stringify` on the `JsValue` itself.
|
|
|
|
/// The resulting string is then passed into Rust which then parses it as
|
|
|
|
/// JSON into the resulting value.
|
|
|
|
///
|
|
|
|
/// Usage of this API requires activating the `serde-serialize` feature of
|
|
|
|
/// the `wasm-bindgen` crate.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// Returns any error encountered when parsing the JSON into a `T`.
|
|
|
|
#[cfg(feature = "serde-serialize")]
|
|
|
|
pub fn into_serde<T>(&self) -> serde_json::Result<T>
|
2018-06-27 22:42:34 -07:00
|
|
|
where
|
|
|
|
T: for<'a> serde::de::Deserialize<'a>,
|
2018-04-26 19:03:46 -07:00
|
|
|
{
|
2019-01-10 23:37:12 +00:00
|
|
|
unsafe {
|
|
|
|
let mut ptr = ptr::null_mut();
|
|
|
|
let len = __wbindgen_json_serialize(self.idx, &mut ptr);
|
|
|
|
let s = Vec::from_raw_parts(ptr, len, len);
|
|
|
|
let s = String::from_utf8_unchecked(s);
|
|
|
|
serde_json::from_str(&s)
|
|
|
|
}
|
2018-04-26 19:03:46 -07:00
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-09 16:35:25 -07:00
|
|
|
/// Tests whether this JS value is a JS string.
|
|
|
|
pub fn is_string(&self) -> bool {
|
|
|
|
unsafe { __wbindgen_is_string(self.idx) == 1 }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If this JS value is a string value, this function copies the JS string
|
|
|
|
/// value into wasm linear memory, encoded as UTF-8, and returns it as a
|
|
|
|
/// Rust `String`.
|
|
|
|
///
|
2018-11-09 13:12:16 +01:00
|
|
|
/// To avoid the copying and re-encoding, consider the
|
|
|
|
/// `JsString::try_from()` function from [js-sys](https://docs.rs/js-sys)
|
|
|
|
/// instead.
|
2017-12-31 15:45:47 -08:00
|
|
|
///
|
|
|
|
/// 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`
|
2018-10-01 15:27:34 -07:00
|
|
|
#[inline]
|
2017-12-31 15:45:47 -08:00
|
|
|
pub fn is_null(&self) -> bool {
|
2018-06-27 22:42:34 -07:00
|
|
|
unsafe { __wbindgen_is_null(self.idx) == 1 }
|
2017-12-31 15:45:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Tests whether this JS value is `undefined`
|
2018-10-01 15:27:34 -07:00
|
|
|
#[inline]
|
2017-12-31 15:45:47 -08:00
|
|
|
pub fn is_undefined(&self) -> bool {
|
2018-06-27 22:42:34 -07:00
|
|
|
unsafe { __wbindgen_is_undefined(self.idx) == 1 }
|
2017-12-31 15:45:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Tests whether the type of this JS value is `symbol`
|
2018-10-01 15:27:34 -07:00
|
|
|
#[inline]
|
2017-12-31 15:45:47 -08:00
|
|
|
pub fn is_symbol(&self) -> bool {
|
2018-06-27 22:42:34 -07:00
|
|
|
unsafe { __wbindgen_is_symbol(self.idx) == 1 }
|
2017-12-31 15:45:47 -08:00
|
|
|
}
|
2018-07-09 16:35:25 -07:00
|
|
|
|
|
|
|
/// Tests whether `typeof self == "object" && self !== null`.
|
2018-10-01 15:27:34 -07:00
|
|
|
#[inline]
|
2018-07-09 16:35:25 -07:00
|
|
|
pub fn is_object(&self) -> bool {
|
|
|
|
unsafe { __wbindgen_is_object(self.idx) == 1 }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Tests whether the type of this JS value is `function`.
|
2018-10-01 15:27:34 -07:00
|
|
|
#[inline]
|
2018-07-09 16:35:25 -07:00
|
|
|
pub fn is_function(&self) -> bool {
|
|
|
|
unsafe { __wbindgen_is_function(self.idx) == 1 }
|
|
|
|
}
|
2019-01-09 17:10:24 +00:00
|
|
|
|
2019-01-10 23:37:12 +00:00
|
|
|
/// Get a string representation of the JavaScript object for debugging
|
2019-01-09 17:42:56 +00:00
|
|
|
#[cfg(feature = "std")]
|
2019-01-10 23:37:12 +00:00
|
|
|
fn as_debug_string(&self) -> String {
|
2019-01-09 17:42:56 +00:00
|
|
|
unsafe {
|
|
|
|
let mut len = 0;
|
2019-01-10 23:37:12 +00:00
|
|
|
let ptr = __wbindgen_debug_string(self.idx, &mut len);
|
2019-01-09 17:42:56 +00:00
|
|
|
if ptr.is_null() {
|
2019-01-10 23:37:12 +00:00
|
|
|
unreachable!("`__wbindgen_debug_string` must return a valid string")
|
2019-01-09 17:42:56 +00:00
|
|
|
} else {
|
|
|
|
let data = Vec::from_raw_parts(ptr, len, len);
|
|
|
|
String::from_utf8_unchecked(data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-19 09:25:41 -08:00
|
|
|
}
|
|
|
|
|
2018-06-01 16:47:45 -05:00
|
|
|
impl PartialEq for JsValue {
|
2018-10-01 15:27:34 -07:00
|
|
|
#[inline]
|
2018-06-01 16:47:45 -05:00
|
|
|
fn eq(&self, other: &JsValue) -> bool {
|
2018-06-27 22:42:34 -07:00
|
|
|
unsafe { __wbindgen_jsval_eq(self.idx, other.idx) != 0 }
|
2018-06-01 16:47:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-20 11:34:54 -07:00
|
|
|
impl PartialEq<bool> for JsValue {
|
2018-10-01 15:27:34 -07:00
|
|
|
#[inline]
|
2018-07-20 11:34:54 -07:00
|
|
|
fn eq(&self, other: &bool) -> bool {
|
|
|
|
self.as_bool() == Some(*other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq<str> for JsValue {
|
2018-10-01 15:27:34 -07:00
|
|
|
#[inline]
|
2018-07-20 11:34:54 -07:00
|
|
|
fn eq(&self, other: &str) -> bool {
|
|
|
|
*self == JsValue::from_str(other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> PartialEq<&'a str> for JsValue {
|
2018-10-01 15:27:34 -07:00
|
|
|
#[inline]
|
2018-07-20 11:34:54 -07:00
|
|
|
fn eq(&self, other: &&'a str) -> bool {
|
|
|
|
<JsValue as PartialEq<str>>::eq(self, other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if_std! {
|
|
|
|
impl PartialEq<String> for JsValue {
|
2018-10-01 15:27:34 -07:00
|
|
|
#[inline]
|
2018-07-20 11:34:54 -07:00
|
|
|
fn eq(&self, other: &String) -> bool {
|
|
|
|
<JsValue as PartialEq<str>>::eq(self, other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl<'a> PartialEq<&'a String> for JsValue {
|
2018-10-01 15:27:34 -07:00
|
|
|
#[inline]
|
2018-07-20 11:34:54 -07:00
|
|
|
fn eq(&self, other: &&'a String) -> bool {
|
|
|
|
<JsValue as PartialEq<str>>::eq(self, other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-06 15:04:46 -08:00
|
|
|
impl<'a> From<&'a str> for JsValue {
|
2018-11-27 12:07:59 -08:00
|
|
|
#[inline]
|
2018-02-06 15:04:46 -08:00
|
|
|
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 {
|
2018-10-01 15:27:34 -07:00
|
|
|
#[inline]
|
2018-04-19 13:08:54 -07:00
|
|
|
fn from(s: &'a String) -> JsValue {
|
|
|
|
JsValue::from_str(s)
|
|
|
|
}
|
2017-12-31 15:45:47 -08:00
|
|
|
}
|
2018-07-20 11:34:54 -07:00
|
|
|
|
|
|
|
impl From<String> for JsValue {
|
2018-10-01 15:27:34 -07:00
|
|
|
#[inline]
|
2018-07-20 11:34:54 -07:00
|
|
|
fn from(s: 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 {
|
2018-10-01 15:27:34 -07:00
|
|
|
#[inline]
|
2018-02-06 15:04:46 -08:00
|
|
|
fn from(s: bool) -> JsValue {
|
|
|
|
JsValue::from_bool(s)
|
2017-12-31 15:45:47 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 08:26:00 -07:00
|
|
|
impl<'a, T> From<&'a T> for JsValue
|
|
|
|
where
|
|
|
|
T: JsCast,
|
|
|
|
{
|
2018-10-01 15:27:34 -07:00
|
|
|
#[inline]
|
2018-08-14 10:16:18 -07:00
|
|
|
fn from(s: &'a T) -> JsValue {
|
|
|
|
s.as_ref().clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 08:26:00 -07:00
|
|
|
impl<T> From<Option<T>> for JsValue
|
|
|
|
where
|
|
|
|
JsValue: From<T>,
|
|
|
|
{
|
2018-10-01 15:27:34 -07:00
|
|
|
#[inline]
|
2018-08-14 10:16:18 -07:00
|
|
|
fn from(s: Option<T>) -> JsValue {
|
|
|
|
match s {
|
|
|
|
Some(s) => s.into(),
|
|
|
|
None => JsValue::undefined(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-04 09:02:31 -07:00
|
|
|
impl JsCast for JsValue {
|
|
|
|
// everything is a `JsValue`!
|
2018-10-01 15:27:34 -07:00
|
|
|
#[inline]
|
2018-09-26 08:26:00 -07:00
|
|
|
fn instanceof(_val: &JsValue) -> bool {
|
|
|
|
true
|
|
|
|
}
|
2018-10-01 15:27:34 -07:00
|
|
|
#[inline]
|
2018-09-26 08:26:00 -07:00
|
|
|
fn unchecked_from_js(val: JsValue) -> Self {
|
|
|
|
val
|
|
|
|
}
|
2018-10-01 15:27:34 -07:00
|
|
|
#[inline]
|
2018-09-26 08:26:00 -07:00
|
|
|
fn unchecked_from_js_ref(val: &JsValue) -> &Self {
|
|
|
|
val
|
|
|
|
}
|
2018-08-04 09:02:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<JsValue> for JsValue {
|
2018-10-01 15:27:34 -07:00
|
|
|
#[inline]
|
2018-09-26 08:26:00 -07:00
|
|
|
fn as_ref(&self) -> &JsValue {
|
|
|
|
self
|
|
|
|
}
|
2018-08-04 09:02:31 -07:00
|
|
|
}
|
|
|
|
|
2017-12-31 15:45:47 -08:00
|
|
|
macro_rules! numbers {
|
|
|
|
($($n:ident)*) => ($(
|
2018-07-20 11:34:54 -07:00
|
|
|
impl PartialEq<$n> for JsValue {
|
2018-10-01 15:27:34 -07:00
|
|
|
#[inline]
|
2018-07-20 11:34:54 -07:00
|
|
|
fn eq(&self, other: &$n) -> bool {
|
|
|
|
self.as_f64() == Some(f64::from(*other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-06 15:04:46 -08:00
|
|
|
impl From<$n> for JsValue {
|
2018-10-01 15:27:34 -07:00
|
|
|
#[inline]
|
2018-02-06 15:04:46 -08:00
|
|
|
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-04-27 13:43:47 -07:00
|
|
|
macro_rules! externs {
|
|
|
|
($(fn $name:ident($($args:tt)*) -> $ret:ty;)*) => (
|
|
|
|
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
|
2018-07-21 19:00:20 -07:00
|
|
|
#[link(wasm_import_module = "__wbindgen_placeholder__")]
|
2018-11-27 12:27:00 -08:00
|
|
|
extern "C" {
|
2018-04-27 13:43:47 -07:00
|
|
|
$(fn $name($($args)*) -> $ret;)*
|
|
|
|
}
|
|
|
|
|
|
|
|
$(
|
|
|
|
#[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))]
|
|
|
|
#[allow(unused_variables)]
|
2018-11-27 12:27:00 -08:00
|
|
|
unsafe extern "C" fn $name($($args)*) -> $ret {
|
2018-04-27 13:43:47 -07:00
|
|
|
panic!("function not implemented on non-wasm32 targets")
|
|
|
|
}
|
|
|
|
)*
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
externs! {
|
2017-12-19 20:00:25 -08:00
|
|
|
fn __wbindgen_object_clone_ref(idx: u32) -> u32;
|
2018-04-27 13:43:47 -07:00
|
|
|
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_is_null(idx: u32) -> u32;
|
|
|
|
fn __wbindgen_is_undefined(idx: 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;
|
2018-07-09 16:35:25 -07:00
|
|
|
fn __wbindgen_is_object(idx: u32) -> u32;
|
|
|
|
fn __wbindgen_is_function(idx: u32) -> u32;
|
|
|
|
fn __wbindgen_is_string(idx: u32) -> u32;
|
2017-12-31 15:45:47 -08:00
|
|
|
fn __wbindgen_string_get(idx: u32, len: *mut usize) -> *mut u8;
|
2019-01-10 23:37:12 +00:00
|
|
|
fn __wbindgen_debug_string(idx: u32, len: *mut usize) -> *mut u8;
|
2018-03-14 14:33:53 -07:00
|
|
|
fn __wbindgen_throw(a: *const u8, b: usize) -> !;
|
2018-09-17 18:26:45 -07:00
|
|
|
fn __wbindgen_rethrow(a: u32) -> !;
|
2018-04-05 18:25:22 -07:00
|
|
|
|
2018-09-24 15:10:58 -07:00
|
|
|
fn __wbindgen_cb_drop(idx: u32) -> u32;
|
2018-04-27 13:43:47 -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
|
|
|
|
2018-04-27 13:43:47 -07:00
|
|
|
fn __wbindgen_describe(v: u32) -> ();
|
2018-11-29 12:01:16 -08:00
|
|
|
fn __wbindgen_describe_closure(a: u32, b: u32, c: u32) -> u32;
|
2018-04-26 19:03:46 -07:00
|
|
|
|
|
|
|
fn __wbindgen_json_parse(ptr: *const u8, len: usize) -> u32;
|
|
|
|
fn __wbindgen_json_serialize(idx: u32, ptr: *mut *mut u8) -> usize;
|
2018-06-01 16:47:45 -05:00
|
|
|
fn __wbindgen_jsval_eq(a: u32, b: u32) -> u32;
|
2018-08-21 10:42:52 -07:00
|
|
|
|
|
|
|
fn __wbindgen_memory() -> u32;
|
2018-10-04 20:00:23 -07:00
|
|
|
fn __wbindgen_module() -> 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-10-10 12:50:54 -07:00
|
|
|
JsValue::_new(idx)
|
2017-12-19 09:25:41 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-10 23:37:12 +00:00
|
|
|
#[cfg(feature = "std")]
|
2018-07-05 20:14:38 -07:00
|
|
|
impl fmt::Debug for JsValue {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2019-01-10 23:39:36 +00:00
|
|
|
write!(f, "JsValue({})", self.as_debug_string())
|
2019-01-10 23:37:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "std"))]
|
|
|
|
impl fmt::Debug for JsValue {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2019-01-11 19:24:24 +00:00
|
|
|
f.write_str("JsValue")
|
2018-07-05 20:14:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-06 15:04:46 -08:00
|
|
|
impl Drop for JsValue {
|
2018-10-01 15:27:34 -07:00
|
|
|
#[inline]
|
2017-12-19 09:25:41 -08:00
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe {
|
2018-11-29 18:15:36 -08:00
|
|
|
// We definitely should never drop anything in the stack area
|
|
|
|
debug_assert!(self.idx >= JSIDX_OFFSET);
|
2018-10-24 23:33:01 -07:00
|
|
|
|
2018-11-29 18:15:36 -08:00
|
|
|
// Otherwise if we're not dropping one of our reserved values,
|
|
|
|
// actually call the intrinsic. See #1054 for eventually removing
|
|
|
|
// this branch.
|
2018-10-24 23:33:01 -07:00
|
|
|
if self.idx >= JSIDX_RESERVED {
|
2018-06-01 16:46:42 -05: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]
|
2018-11-27 12:27:00 -08:00
|
|
|
/// extern "C" {
|
2018-03-21 08:09:59 -07:00
|
|
|
/// 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`.
|
2018-10-10 12:50:54 -07:00
|
|
|
#[cfg(feature = "std")]
|
2018-04-21 13:14:33 -07:00
|
|
|
pub struct JsStatic<T: 'static> {
|
2018-03-21 08:09:59 -07:00
|
|
|
#[doc(hidden)]
|
2018-10-10 12:50:54 -07:00
|
|
|
pub __inner: &'static std::thread::LocalKey<T>,
|
2018-03-21 08:09:59 -07:00
|
|
|
}
|
|
|
|
|
2018-10-10 12:50:54 -07:00
|
|
|
#[cfg(feature = "std")]
|
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 {
|
2018-10-10 12:50:54 -07:00
|
|
|
// We know that our tls key is never overwritten after initialization,
|
|
|
|
// so it should be safe (on that axis at least) to hand out a reference
|
|
|
|
// that lives longer than the closure below.
|
|
|
|
//
|
|
|
|
// FIXME: this is not sound if we ever implement thread exit hooks on
|
|
|
|
// wasm, as the pointer will eventually be invalidated but you can get
|
|
|
|
// `&'static T` from this interface. We... probably need to deprecate
|
|
|
|
// and/or remove this interface nowadays.
|
2018-11-27 12:07:59 -08:00
|
|
|
unsafe { self.__inner.with(|ptr| &*(ptr as *const T)) }
|
2018-03-21 08:09:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-17 18:26:45 -07:00
|
|
|
#[cold]
|
|
|
|
#[inline(never)]
|
|
|
|
#[deprecated(note = "renamed to `throw_str`")]
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn throw(s: &str) -> ! {
|
|
|
|
throw_str(s)
|
|
|
|
}
|
|
|
|
|
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.
|
2018-09-17 18:26:45 -07:00
|
|
|
///
|
|
|
|
/// Note that it is very easy to leak memory with this function because this
|
|
|
|
/// function, unlike `panic!` on other platforms, **will not run destructors**.
|
|
|
|
/// It's recommended to return a `Result` where possible to avoid the worry of
|
|
|
|
/// leaks.
|
2017-12-19 19:50:06 -08:00
|
|
|
#[cold]
|
2017-12-21 12:25:13 -08:00
|
|
|
#[inline(never)]
|
2018-09-17 18:26:45 -07:00
|
|
|
pub fn throw_str(s: &str) -> ! {
|
2017-12-19 19:50:06 -08:00
|
|
|
unsafe {
|
|
|
|
__wbindgen_throw(s.as_ptr(), s.len());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-17 18:26:45 -07:00
|
|
|
/// Rethrow a JS exception
|
|
|
|
///
|
|
|
|
/// This function will throw a JS exception with the JS value provided. This
|
|
|
|
/// function will not return and the wasm stack will be popped until the point
|
|
|
|
/// of entry of wasm itself.
|
|
|
|
///
|
|
|
|
/// Note that it is very easy to leak memory with this function because this
|
|
|
|
/// function, unlike `panic!` on other platforms, **will not run destructors**.
|
|
|
|
/// It's recommended to return a `Result` where possible to avoid the worry of
|
|
|
|
/// leaks.
|
|
|
|
#[cold]
|
|
|
|
#[inline(never)]
|
|
|
|
pub fn throw_val(s: JsValue) -> ! {
|
|
|
|
unsafe {
|
|
|
|
let idx = s.idx;
|
|
|
|
mem::forget(s);
|
|
|
|
__wbindgen_rethrow(idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-04 20:00:23 -07:00
|
|
|
/// Returns a handle to this wasm instance's `WebAssembly.Module`
|
|
|
|
///
|
|
|
|
/// Note that this is only available when the final wasm app is built with
|
|
|
|
/// `--no-modules`, it's not recommended to rely on this API yet! This is
|
|
|
|
/// largely just an experimental addition to enable threading demos. Using this
|
|
|
|
/// may prevent your wasm module from building down the road.
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub fn module() -> JsValue {
|
2018-11-27 12:07:59 -08:00
|
|
|
unsafe { JsValue::_new(__wbindgen_module()) }
|
2018-10-04 20:00:23 -07:00
|
|
|
}
|
|
|
|
|
2018-08-21 10:42:52 -07:00
|
|
|
/// Returns a handle to this wasm instance's `WebAssembly.Memory`
|
|
|
|
pub fn memory() -> JsValue {
|
2018-11-27 12:07:59 -08:00
|
|
|
unsafe { JsValue::_new(__wbindgen_memory()) }
|
2018-08-21 10:42:52 -07:00
|
|
|
}
|
|
|
|
|
2017-12-19 19:50:06 -08:00
|
|
|
#[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]
|
2018-07-06 20:04:49 -07:00
|
|
|
#[doc(hidden)]
|
2018-04-19 13:08:54 -07:00
|
|
|
#[cfg(feature = "std")]
|
|
|
|
macro_rules! __wbindgen_if_not_std {
|
2018-06-27 22:42:34 -07:00
|
|
|
($($i:item)*) => {};
|
2018-04-19 13:08:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
2018-07-06 20:04:49 -07:00
|
|
|
#[doc(hidden)]
|
2018-04-19 13:08:54 -07:00
|
|
|
#[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() -> ! {
|
2018-09-17 18:26:45 -07:00
|
|
|
super::throw_str("null pointer passed to rust");
|
2017-12-21 12:25:13 -08:00
|
|
|
}
|
|
|
|
|
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> {
|
2018-06-27 22:42:34 -07:00
|
|
|
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 {
|
2018-06-27 22:42:34 -07:00
|
|
|
unsafe { &mut *self.value.get() }
|
2017-12-21 12:28:30 -08:00
|
|
|
}
|
|
|
|
|
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-06-27 22:42:34 -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() -> ! {
|
2018-09-17 18:26:45 -07:00
|
|
|
super::throw_str(
|
2018-06-27 22:42:34 -07:00
|
|
|
"recursive use of an object detected which would lead to \
|
|
|
|
unsafe aliasing in rust",
|
|
|
|
);
|
2017-12-19 19:50:06 -08:00
|
|
|
}
|
2018-02-06 08:58:15 -08:00
|
|
|
|
2018-04-19 13:08:54 -07:00
|
|
|
if_std! {
|
2018-10-09 18:08:46 -07:00
|
|
|
use std::alloc::{alloc, dealloc, Layout};
|
2018-07-13 10:10:00 -07:00
|
|
|
use std::mem;
|
2018-04-19 13:08:54 -07:00
|
|
|
|
|
|
|
#[no_mangle]
|
2018-11-27 12:27:00 -08:00
|
|
|
pub extern "C" fn __wbindgen_malloc(size: usize) -> *mut u8 {
|
2018-07-13 10:10:00 -07:00
|
|
|
let align = mem::align_of::<usize>();
|
|
|
|
if let Ok(layout) = Layout::from_size_align(size, align) {
|
|
|
|
unsafe {
|
2018-10-28 08:51:47 -07:00
|
|
|
if layout.size() > 0 {
|
|
|
|
let ptr = alloc(layout);
|
|
|
|
if !ptr.is_null() {
|
|
|
|
return ptr
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return align as *mut u8
|
2018-07-13 10:10:00 -07:00
|
|
|
}
|
|
|
|
}
|
2018-04-19 13:08:54 -07:00
|
|
|
}
|
2018-07-13 10:10:00 -07:00
|
|
|
|
2018-11-09 12:18:58 -08:00
|
|
|
if cfg!(debug_assertions) {
|
|
|
|
super::throw_str("invalid malloc request")
|
|
|
|
} else {
|
|
|
|
std::process::abort();
|
|
|
|
}
|
2018-02-06 08:58:15 -08:00
|
|
|
}
|
|
|
|
|
2018-04-19 13:08:54 -07:00
|
|
|
#[no_mangle]
|
2018-11-27 12:27:00 -08:00
|
|
|
pub unsafe extern "C" fn __wbindgen_free(ptr: *mut u8, size: usize) {
|
2018-07-19 14:44:23 -05:00
|
|
|
// This happens for zero-length slices, and in that case `ptr` is
|
|
|
|
// likely bogus so don't actually send this to the system allocator
|
|
|
|
if size == 0 {
|
|
|
|
return
|
|
|
|
}
|
2018-07-13 10:10:00 -07:00
|
|
|
let align = mem::align_of::<usize>();
|
|
|
|
let layout = Layout::from_size_align_unchecked(size, align);
|
2018-10-09 18:08:46 -07:00
|
|
|
dealloc(ptr, layout);
|
2018-04-19 13:08:54 -07:00
|
|
|
}
|
2018-02-06 08:58:15 -08:00
|
|
|
}
|
|
|
|
|
2018-07-17 16:56:22 -05:00
|
|
|
pub const GLOBAL_STACK_CAP: usize = 16;
|
|
|
|
|
|
|
|
// Increase the alignment to 8 here because this can be used as a
|
|
|
|
// BigUint64Array pointer base which requires alignment 8
|
|
|
|
#[repr(align(8))]
|
|
|
|
struct GlobalData([u32; GLOBAL_STACK_CAP]);
|
|
|
|
|
|
|
|
static mut GLOBAL_STACK: GlobalData = GlobalData([0; GLOBAL_STACK_CAP]);
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn __wbindgen_global_argument_ptr() -> *mut u32 {
|
|
|
|
GLOBAL_STACK.0.as_mut_ptr()
|
|
|
|
}
|
|
|
|
|
2018-07-14 11:04:47 -05:00
|
|
|
/// This is a curious function necessary to get wasm-bindgen working today,
|
|
|
|
/// and it's a bit of an unfortunate hack.
|
|
|
|
///
|
|
|
|
/// The general problem is that somehow we need the above two symbols to
|
|
|
|
/// exist in the final output binary (__wbindgen_malloc and
|
|
|
|
/// __wbindgen_free). These symbols may be called by JS for various
|
|
|
|
/// bindings, so we for sure need to make sure they're exported.
|
|
|
|
///
|
|
|
|
/// The problem arises, though, when what if no Rust code uses the symbols?
|
|
|
|
/// For all intents and purposes it looks to LLVM and the linker like the
|
|
|
|
/// above two symbols are dead code, so they're completely discarded!
|
|
|
|
///
|
|
|
|
/// Specifically what happens is this:
|
|
|
|
///
|
|
|
|
/// * The above two symbols are generated into some object file inside of
|
|
|
|
/// libwasm_bindgen.rlib
|
|
|
|
/// * The linker, LLD, will not load this object file unless *some* symbol
|
|
|
|
/// is loaded from the object. In this case, if the Rust code never calls
|
|
|
|
/// __wbindgen_malloc or __wbindgen_free then the symbols never get linked
|
|
|
|
/// in.
|
|
|
|
/// * Later when `wasm-bindgen` attempts to use the symbols they don't
|
|
|
|
/// exist, causing an error.
|
|
|
|
///
|
|
|
|
/// This function is a weird hack for this problem. We inject a call to this
|
|
|
|
/// function in all generated code. Usage of this function should then
|
|
|
|
/// ensure that the above two intrinsics are translated.
|
|
|
|
///
|
|
|
|
/// Due to how rustc creates object files this function (and anything inside
|
|
|
|
/// it) will be placed into the same object file as the two intrinsics
|
|
|
|
/// above. That means if this function is called and referenced we'll pull
|
|
|
|
/// in the object file and link the intrinsics.
|
|
|
|
///
|
|
|
|
/// Ideas for how to improve this are most welcome!
|
2018-09-26 08:26:00 -07:00
|
|
|
pub fn link_mem_intrinsics() {}
|
2017-12-19 19:50:06 -08:00
|
|
|
}
|
2018-09-24 13:49:12 -07:00
|
|
|
|
|
|
|
/// A wrapper type around slices and vectors for binding the `Uint8ClampedArray`
|
|
|
|
/// array in JS.
|
|
|
|
///
|
|
|
|
/// If you need to invoke a JS API which must take `Uint8ClampedArray` array,
|
|
|
|
/// then you can define it as taking one of these types:
|
|
|
|
///
|
|
|
|
/// * `Clamped<&[u8]>`
|
|
|
|
/// * `Clamped<&mut [u8]>`
|
|
|
|
/// * `Clamped<Vec<u8>>`
|
|
|
|
///
|
|
|
|
/// All of these types will show up as `Uint8ClampedArray` in JS and will have
|
|
|
|
/// different forms of ownership in Rust.
|
|
|
|
#[derive(Copy, Clone, PartialEq, Debug, Eq)]
|
|
|
|
pub struct Clamped<T>(pub T);
|
|
|
|
|
|
|
|
impl<T> Deref for Clamped<T> {
|
|
|
|
type Target = T;
|
|
|
|
|
|
|
|
fn deref(&self) -> &T {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> DerefMut for Clamped<T> {
|
|
|
|
fn deref_mut(&mut self) -> &mut T {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|