#![no_std]
#![doc(html_root_url = "https://docs.rs/wasm-bindgen/0.2")]
#![cfg_attr(feature = "nightly", feature(unsize))]
#[cfg(feature = "serde-serialize")]
extern crate serde;
#[cfg(feature = "serde-serialize")]
extern crate serde_json;
extern crate wasm_bindgen_macro;
use core::fmt;
use core::marker;
use core::mem;
use core::ops::{Deref, DerefMut};
use core::ptr;
use convert::FromWasmAbi;
macro_rules! if_std {
($($i:item)*) => ($(
#[cfg(feature = "std")] $i
)*)
}
pub mod prelude {
pub use wasm_bindgen_macro::wasm_bindgen;
pub use JsValue;
if_std! {
pub use closure::Closure;
}
}
pub mod convert;
pub mod describe;
mod cast;
pub use cast::JsCast;
if_std! {
extern crate std;
use std::prelude::v1::*;
pub mod closure;
}
pub struct JsValue {
idx: u32,
_marker: marker::PhantomData<*mut u8>, }
const JSIDX_OFFSET: u32 = 32; 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;
impl JsValue {
pub const NULL: JsValue = JsValue {
idx: JSIDX_NULL,
_marker: marker::PhantomData,
};
pub const UNDEFINED: JsValue = JsValue {
idx: JSIDX_UNDEFINED,
_marker: marker::PhantomData,
};
pub const TRUE: JsValue = JsValue {
idx: JSIDX_TRUE,
_marker: marker::PhantomData,
};
pub const FALSE: JsValue = JsValue {
idx: JSIDX_FALSE,
_marker: marker::PhantomData,
};
#[inline]
fn _new(idx: u32) -> JsValue {
JsValue {
idx,
_marker: marker::PhantomData,
}
}
#[inline]
pub fn from_str(s: &str) -> JsValue {
unsafe { JsValue::_new(__wbindgen_string_new(s.as_ptr(), s.len())) }
}
#[inline]
pub fn from_f64(n: f64) -> JsValue {
unsafe { JsValue::_new(__wbindgen_number_new(n)) }
}
#[inline]
pub fn from_bool(b: bool) -> JsValue {
if b {
JsValue::TRUE
} else {
JsValue::FALSE
}
}
#[inline]
pub fn undefined() -> JsValue {
JsValue::UNDEFINED
}
#[inline]
pub fn null() -> JsValue {
JsValue::NULL
}
pub fn symbol(description: Option<&str>) -> JsValue {
unsafe {
let ptr = description.map(|s| s.as_ptr()).unwrap_or(ptr::null());
let len = description.map(|s| s.len()).unwrap_or(0);
JsValue::_new(__wbindgen_symbol_new(ptr, len))
}
}
#[cfg(feature = "serde-serialize")]
pub fn from_serde<T>(t: &T) -> serde_json::Result<JsValue>
where
T: serde::ser::Serialize + ?Sized,
{
let s = serde_json::to_string(t)?;
unsafe { Ok(JsValue::_new(__wbindgen_json_parse(s.as_ptr(), s.len()))) }
}
#[cfg(feature = "serde-serialize")]
pub fn into_serde<T>(&self) -> serde_json::Result<T>
where
T: for<'a> serde::de::Deserialize<'a>,
{
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)
}
}
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)
}
}
}
pub fn is_string(&self) -> bool {
unsafe { __wbindgen_is_string(self.idx) == 1 }
}
#[cfg(feature = "std")]
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))
}
}
}
pub fn as_bool(&self) -> Option<bool> {
unsafe {
match __wbindgen_boolean_get(self.idx) {
0 => Some(false),
1 => Some(true),
_ => None,
}
}
}
#[inline]
pub fn is_null(&self) -> bool {
unsafe { __wbindgen_is_null(self.idx) == 1 }
}
#[inline]
pub fn is_undefined(&self) -> bool {
unsafe { __wbindgen_is_undefined(self.idx) == 1 }
}
#[inline]
pub fn is_symbol(&self) -> bool {
unsafe { __wbindgen_is_symbol(self.idx) == 1 }
}
#[inline]
pub fn is_object(&self) -> bool {
unsafe { __wbindgen_is_object(self.idx) == 1 }
}
#[inline]
pub fn is_function(&self) -> bool {
unsafe { __wbindgen_is_function(self.idx) == 1 }
}
}
impl PartialEq for JsValue {
#[inline]
fn eq(&self, other: &JsValue) -> bool {
unsafe { __wbindgen_jsval_eq(self.idx, other.idx) != 0 }
}
}
impl PartialEq<bool> for JsValue {
#[inline]
fn eq(&self, other: &bool) -> bool {
self.as_bool() == Some(*other)
}
}
impl PartialEq<str> for JsValue {
#[inline]
fn eq(&self, other: &str) -> bool {
*self == JsValue::from_str(other)
}
}
impl<'a> PartialEq<&'a str> for JsValue {
#[inline]
fn eq(&self, other: &&'a str) -> bool {
<JsValue as PartialEq<str>>::eq(self, other)
}
}
if_std! {
impl PartialEq<String> for JsValue {
#[inline]
fn eq(&self, other: &String) -> bool {
<JsValue as PartialEq<str>>::eq(self, other)
}
}
impl<'a> PartialEq<&'a String> for JsValue {
#[inline]
fn eq(&self, other: &&'a String) -> bool {
<JsValue as PartialEq<str>>::eq(self, other)
}
}
}
impl<'a> From<&'a str> for JsValue {
#[inline]
fn from(s: &'a str) -> JsValue {
JsValue::from_str(s)
}
}
if_std! {
impl<'a> From<&'a String> for JsValue {
#[inline]
fn from(s: &'a String) -> JsValue {
JsValue::from_str(s)
}
}
impl From<String> for JsValue {
#[inline]
fn from(s: String) -> JsValue {
JsValue::from_str(&s)
}
}
}
impl From<bool> for JsValue {
#[inline]
fn from(s: bool) -> JsValue {
JsValue::from_bool(s)
}
}
impl<'a, T> From<&'a T> for JsValue
where
T: JsCast,
{
#[inline]
fn from(s: &'a T) -> JsValue {
s.as_ref().clone()
}
}
impl<T> From<Option<T>> for JsValue
where
JsValue: From<T>,
{
#[inline]
fn from(s: Option<T>) -> JsValue {
match s {
Some(s) => s.into(),
None => JsValue::undefined(),
}
}
}
impl JsCast for JsValue {
#[inline]
fn instanceof(_val: &JsValue) -> bool {
true
}
#[inline]
fn unchecked_from_js(val: JsValue) -> Self {
val
}
#[inline]
fn unchecked_from_js_ref(val: &JsValue) -> &Self {
val
}
}
impl AsRef<JsValue> for JsValue {
#[inline]
fn as_ref(&self) -> &JsValue {
self
}
}
macro_rules! numbers {
($($n:ident)*) => ($(
impl PartialEq<$n> for JsValue {
#[inline]
fn eq(&self, other: &$n) -> bool {
self.as_f64() == Some(f64::from(*other))
}
}
impl From<$n> for JsValue {
#[inline]
fn from(n: $n) -> JsValue {
JsValue::from_f64(n.into())
}
}
)*)
}
numbers! { i8 u8 i16 u16 i32 u32 f32 f64 }
macro_rules! externs {
($(fn $name:ident($($args:tt)*) -> $ret:ty;)*) => (
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
#[link(wasm_import_module = "__wbindgen_placeholder__")]
extern "C" {
$(fn $name($($args)*) -> $ret;)*
}
$(
#[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))]
#[allow(unused_variables)]
unsafe extern "C" fn $name($($args)*) -> $ret {
panic!("function not implemented on non-wasm32 targets")
}
)*
)
}
externs! {
fn __wbindgen_object_clone_ref(idx: u32) -> u32;
fn __wbindgen_object_drop_ref(idx: u32) -> ();
fn __wbindgen_string_new(ptr: *const u8, len: usize) -> u32;
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;
fn __wbindgen_is_object(idx: u32) -> u32;
fn __wbindgen_is_function(idx: u32) -> u32;
fn __wbindgen_is_string(idx: u32) -> u32;
fn __wbindgen_string_get(idx: u32, len: *mut usize) -> *mut u8;
fn __wbindgen_throw(a: *const u8, b: usize) -> !;
fn __wbindgen_rethrow(a: u32) -> !;
fn __wbindgen_cb_drop(idx: u32) -> u32;
fn __wbindgen_cb_forget(idx: u32) -> ();
fn __wbindgen_describe(v: u32) -> ();
fn __wbindgen_describe_closure(a: u32, b: u32, c: u32) -> u32;
fn __wbindgen_json_parse(ptr: *const u8, len: usize) -> u32;
fn __wbindgen_json_serialize(idx: u32, ptr: *mut *mut u8) -> usize;
fn __wbindgen_jsval_eq(a: u32, b: u32) -> u32;
fn __wbindgen_memory() -> u32;
fn __wbindgen_module() -> u32;
}
impl Clone for JsValue {
fn clone(&self) -> JsValue {
unsafe {
let idx = __wbindgen_object_clone_ref(self.idx);
JsValue::_new(idx)
}
}
}
impl fmt::Debug for JsValue {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(n) = self.as_f64() {
return n.fmt(f);
}
#[cfg(feature = "std")]
{
if let Some(n) = self.as_string() {
return n.fmt(f);
}
}
if let Some(n) = self.as_bool() {
return n.fmt(f);
}
if self.is_null() {
return fmt::Display::fmt("null", f);
}
if self.is_undefined() {
return fmt::Display::fmt("undefined", f);
}
if self.is_symbol() {
return fmt::Display::fmt("Symbol(..)", f);
}
fmt::Display::fmt("[object]", f)
}
}
impl Drop for JsValue {
#[inline]
fn drop(&mut self) {
unsafe {
debug_assert!(self.idx >= JSIDX_OFFSET);
if self.idx >= JSIDX_RESERVED {
__wbindgen_object_drop_ref(self.idx);
}
}
}
}
#[cfg(feature = "std")]
pub struct JsStatic<T: 'static> {
#[doc(hidden)]
pub __inner: &'static std::thread::LocalKey<T>,
}
#[cfg(feature = "std")]
impl<T: FromWasmAbi + 'static> Deref for JsStatic<T> {
type Target = T;
fn deref(&self) -> &T {
unsafe { self.__inner.with(|ptr| &*(ptr as *const T)) }
}
}
#[cold]
#[inline(never)]
#[deprecated(note = "renamed to `throw_str`")]
#[doc(hidden)]
pub fn throw(s: &str) -> ! {
throw_str(s)
}
#[cold]
#[inline(never)]
pub fn throw_str(s: &str) -> ! {
unsafe {
__wbindgen_throw(s.as_ptr(), s.len());
}
}
#[cold]
#[inline(never)]
pub fn throw_val(s: JsValue) -> ! {
unsafe {
let idx = s.idx;
mem::forget(s);
__wbindgen_rethrow(idx);
}
}
#[doc(hidden)]
pub fn module() -> JsValue {
unsafe { JsValue::_new(__wbindgen_module()) }
}
pub fn memory() -> JsValue {
unsafe { JsValue::_new(__wbindgen_memory()) }
}
#[doc(hidden)]
pub mod __rt {
use core::cell::{Cell, UnsafeCell};
use core::ops::{Deref, DerefMut};
pub extern crate core;
#[cfg(feature = "std")]
pub extern crate std;
#[macro_export]
#[doc(hidden)]
#[cfg(feature = "std")]
macro_rules! __wbindgen_if_not_std {
($($i:item)*) => {};
}
#[macro_export]
#[doc(hidden)]
#[cfg(not(feature = "std"))]
macro_rules! __wbindgen_if_not_std {
($($i:item)*) => ($($i)*)
}
#[inline]
pub fn assert_not_null<T>(s: *mut T) {
if s.is_null() {
throw_null();
}
}
#[cold]
#[inline(never)]
fn throw_null() -> ! {
super::throw_str("null pointer passed to rust");
}
pub struct WasmRefCell<T: ?Sized> {
borrow: Cell<usize>,
value: UnsafeCell<T>,
}
impl<T: ?Sized> WasmRefCell<T> {
pub fn new(value: T) -> WasmRefCell<T>
where
T: Sized,
{
WasmRefCell {
value: UnsafeCell::new(value),
borrow: Cell::new(0),
}
}
pub fn get_mut(&mut self) -> &mut T {
unsafe { &mut *self.value.get() }
}
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
where
T: Sized,
{
self.value.into_inner()
}
}
pub struct Ref<'b, T: ?Sized + 'b> {
value: &'b T,
borrow: &'b Cell<usize>,
}
impl<'b, T: ?Sized> Deref for Ref<'b, T> {
type Target = T;
#[inline]
fn deref(&self) -> &T {
self.value
}
}
impl<'b, T: ?Sized> Drop for Ref<'b, T> {
fn drop(&mut self) {
self.borrow.set(self.borrow.get() - 1);
}
}
pub struct RefMut<'b, T: ?Sized + 'b> {
value: &'b mut T,
borrow: &'b Cell<usize>,
}
impl<'b, T: ?Sized> Deref for RefMut<'b, T> {
type Target = T;
#[inline]
fn deref(&self) -> &T {
self.value
}
}
impl<'b, T: ?Sized> DerefMut for RefMut<'b, T> {
#[inline]
fn deref_mut(&mut self) -> &mut T {
self.value
}
}
impl<'b, T: ?Sized> Drop for RefMut<'b, T> {
fn drop(&mut self) {
self.borrow.set(0);
}
}
fn borrow_fail() -> ! {
super::throw_str(
"recursive use of an object detected which would lead to \
unsafe aliasing in rust",
);
}
if_std! {
use std::alloc::{alloc, dealloc, Layout};
use std::mem;
#[no_mangle]
pub extern "C" fn __wbindgen_malloc(size: usize) -> *mut u8 {
let align = mem::align_of::<usize>();
if let Ok(layout) = Layout::from_size_align(size, align) {
unsafe {
if layout.size() > 0 {
let ptr = alloc(layout);
if !ptr.is_null() {
return ptr
}
} else {
return align as *mut u8
}
}
}
if cfg!(debug_assertions) {
super::throw_str("invalid malloc request")
} else {
std::process::abort();
}
}
#[no_mangle]
pub unsafe extern "C" fn __wbindgen_free(ptr: *mut u8, size: usize) {
if size == 0 {
return
}
let align = mem::align_of::<usize>();
let layout = Layout::from_size_align_unchecked(size, align);
dealloc(ptr, layout);
}
}
pub const GLOBAL_STACK_CAP: usize = 16;
#[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()
}
pub fn link_mem_intrinsics() {}
}
#[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
}
}