#![no_std]
#![cfg_attr(wasm_bindgen_unstable_test_coverage, feature(coverage_attribute))]
#![allow(coherence_leak_check)]
#![doc(html_root_url = "https://docs.rs/wasm-bindgen/0.2")]
extern crate alloc;
use alloc::boxed::Box;
use alloc::string::String;
use alloc::vec::Vec;
use core::convert::TryFrom;
use core::marker;
use core::mem;
use core::ops::{
Add, BitAnd, BitOr, BitXor, Deref, DerefMut, Div, Mul, Neg, Not, Rem, Shl, Shr, Sub,
};
use core::ptr::NonNull;
use crate::convert::{FromWasmAbi, TryFromJsValue, WasmRet, WasmSlice};
macro_rules! if_std {
($($i:item)*) => ($(
#[cfg(feature = "std")] $i
)*)
}
macro_rules! externs {
($(#[$attr:meta])* extern "C" { $(fn $name:ident($($args:tt)*) -> $ret:ty;)* }) => (
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
$(#[$attr])*
extern "C" {
$(fn $name($($args)*) -> $ret;)*
}
$(
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
#[allow(unused_variables)]
unsafe extern fn $name($($args)*) -> $ret {
panic!("function not implemented on non-wasm32 targets")
}
)*
)
}
pub mod prelude {
pub use crate::closure::Closure;
pub use crate::JsCast;
pub use crate::JsValue;
pub use crate::UnwrapThrowExt;
#[doc(hidden)]
pub use wasm_bindgen_macro::__wasm_bindgen_class_marker;
pub use wasm_bindgen_macro::wasm_bindgen;
pub use crate::JsError;
}
pub use wasm_bindgen_macro::link_to;
pub mod closure;
pub mod convert;
pub mod describe;
mod link;
mod cast;
pub use crate::cast::{JsCast, JsObject};
if_std! {
extern crate std;
use std::prelude::v1::*;
mod externref;
mod cache;
pub use cache::intern::{intern, unintern};
}
pub struct JsValue {
idx: u32,
_marker: marker::PhantomData<*mut u8>, }
const JSIDX_OFFSET: u32 = 128; const JSIDX_UNDEFINED: u32 = JSIDX_OFFSET;
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::_new(JSIDX_NULL);
pub const UNDEFINED: JsValue = JsValue::_new(JSIDX_UNDEFINED);
pub const TRUE: JsValue = JsValue::_new(JSIDX_TRUE);
pub const FALSE: JsValue = JsValue::_new(JSIDX_FALSE);
#[inline]
const fn _new(idx: u32) -> JsValue {
JsValue {
idx,
_marker: marker::PhantomData,
}
}
#[allow(clippy::should_implement_trait)] #[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 bigint_from_str(s: &str) -> JsValue {
unsafe { JsValue::_new(__wbindgen_bigint_from_str(s.as_ptr(), s.len())) }
}
#[inline]
pub const fn from_bool(b: bool) -> JsValue {
if b {
JsValue::TRUE
} else {
JsValue::FALSE
}
}
#[inline]
pub const fn undefined() -> JsValue {
JsValue::UNDEFINED
}
#[inline]
pub const fn null() -> JsValue {
JsValue::NULL
}
pub fn symbol(description: Option<&str>) -> JsValue {
unsafe {
match description {
Some(description) => JsValue::_new(__wbindgen_symbol_named_new(
description.as_ptr(),
description.len(),
)),
None => JsValue::_new(__wbindgen_symbol_anonymous_new()),
}
}
}
#[cfg(feature = "serde-serialize")]
#[deprecated = "causes dependency cycles, use `serde-wasm-bindgen` or `gloo_utils::format::JsValueSerdeExt` instead"]
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")]
#[deprecated = "causes dependency cycles, use `serde-wasm-bindgen` or `gloo_utils::format::JsValueSerdeExt` instead"]
pub fn into_serde<T>(&self) -> serde_json::Result<T>
where
T: for<'a> serde::de::Deserialize<'a>,
{
unsafe {
let ret = __wbindgen_json_serialize(self.idx);
let s = String::from_abi(ret);
serde_json::from_str(&s)
}
}
#[inline]
pub fn as_f64(&self) -> Option<f64> {
unsafe { __wbindgen_number_get(self.idx).join() }
}
#[inline]
pub fn is_string(&self) -> bool {
unsafe { __wbindgen_is_string(self.idx) == 1 }
}
#[inline]
pub fn as_string(&self) -> Option<String> {
unsafe { FromWasmAbi::from_abi(__wbindgen_string_get(self.idx)) }
}
#[inline]
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_array(&self) -> bool {
unsafe { __wbindgen_is_array(self.idx) == 1 }
}
#[inline]
pub fn is_function(&self) -> bool {
unsafe { __wbindgen_is_function(self.idx) == 1 }
}
#[inline]
pub fn is_bigint(&self) -> bool {
unsafe { __wbindgen_is_bigint(self.idx) == 1 }
}
#[inline]
pub fn js_typeof(&self) -> JsValue {
unsafe { JsValue::_new(__wbindgen_typeof(self.idx)) }
}
#[inline]
pub fn js_in(&self, obj: &JsValue) -> bool {
unsafe { __wbindgen_in(self.idx, obj.idx) == 1 }
}
#[inline]
pub fn is_truthy(&self) -> bool {
!self.is_falsy()
}
#[inline]
pub fn is_falsy(&self) -> bool {
unsafe { __wbindgen_is_falsy(self.idx) == 1 }
}
#[cfg(feature = "std")]
fn as_debug_string(&self) -> String {
unsafe {
let mut ret = [0; 2];
__wbindgen_debug_string(&mut ret, self.idx);
let data = Vec::from_raw_parts(ret[0] as *mut u8, ret[1], ret[1]);
String::from_utf8_unchecked(data)
}
}
#[inline]
pub fn loose_eq(&self, other: &Self) -> bool {
unsafe { __wbindgen_jsval_loose_eq(self.idx, other.idx) != 0 }
}
#[inline]
pub fn bit_not(&self) -> JsValue {
unsafe { JsValue::_new(__wbindgen_bit_not(self.idx)) }
}
#[inline]
pub fn unsigned_shr(&self, rhs: &Self) -> u32 {
unsafe { __wbindgen_unsigned_shr(self.idx, rhs.idx) }
}
#[inline]
pub fn checked_div(&self, rhs: &Self) -> Self {
unsafe { JsValue::_new(__wbindgen_checked_div(self.idx, rhs.idx)) }
}
#[inline]
pub fn pow(&self, rhs: &Self) -> Self {
unsafe { JsValue::_new(__wbindgen_pow(self.idx, rhs.idx)) }
}
#[inline]
pub fn lt(&self, other: &Self) -> bool {
unsafe { __wbindgen_lt(self.idx, other.idx) == 1 }
}
#[inline]
pub fn le(&self, other: &Self) -> bool {
unsafe { __wbindgen_le(self.idx, other.idx) == 1 }
}
#[inline]
pub fn ge(&self, other: &Self) -> bool {
unsafe { __wbindgen_ge(self.idx, other.idx) == 1 }
}
#[inline]
pub fn gt(&self, other: &Self) -> bool {
unsafe { __wbindgen_gt(self.idx, other.idx) == 1 }
}
#[inline]
pub fn unchecked_into_f64(&self) -> f64 {
unsafe { __wbindgen_as_number(self.idx) }
}
}
impl PartialEq for JsValue {
#[inline]
fn eq(&self, other: &Self) -> 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)
}
}
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)
}
}
macro_rules! forward_deref_unop {
(impl $imp:ident, $method:ident for $t:ty) => {
impl $imp for $t {
type Output = <&'static $t as $imp>::Output;
#[inline]
fn $method(self) -> <&'static $t as $imp>::Output {
$imp::$method(&self)
}
}
};
}
macro_rules! forward_deref_binop {
(impl $imp:ident, $method:ident for $t:ty) => {
impl<'a> $imp<$t> for &'a $t {
type Output = <&'static $t as $imp<&'static $t>>::Output;
#[inline]
fn $method(self, other: $t) -> <&'static $t as $imp<&'static $t>>::Output {
$imp::$method(self, &other)
}
}
impl $imp<&$t> for $t {
type Output = <&'static $t as $imp<&'static $t>>::Output;
#[inline]
fn $method(self, other: &$t) -> <&'static $t as $imp<&'static $t>>::Output {
$imp::$method(&self, other)
}
}
impl $imp<$t> for $t {
type Output = <&'static $t as $imp<&'static $t>>::Output;
#[inline]
fn $method(self, other: $t) -> <&'static $t as $imp<&'static $t>>::Output {
$imp::$method(&self, &other)
}
}
};
}
impl Not for &JsValue {
type Output = bool;
#[inline]
fn not(self) -> Self::Output {
JsValue::is_falsy(self)
}
}
forward_deref_unop!(impl Not, not for JsValue);
impl TryFrom<JsValue> for f64 {
type Error = JsValue;
#[inline]
fn try_from(val: JsValue) -> Result<Self, Self::Error> {
f64::try_from(&val)
}
}
impl TryFrom<&JsValue> for f64 {
type Error = JsValue;
#[inline]
fn try_from(val: &JsValue) -> Result<Self, Self::Error> {
let jsval = unsafe { JsValue::_new(__wbindgen_try_into_number(val.idx)) };
match jsval.as_f64() {
Some(num) => Ok(num),
None => Err(jsval),
}
}
}
impl Neg for &JsValue {
type Output = JsValue;
#[inline]
fn neg(self) -> Self::Output {
unsafe { JsValue::_new(__wbindgen_neg(self.idx)) }
}
}
forward_deref_unop!(impl Neg, neg for JsValue);
impl BitAnd for &JsValue {
type Output = JsValue;
#[inline]
fn bitand(self, rhs: Self) -> Self::Output {
unsafe { JsValue::_new(__wbindgen_bit_and(self.idx, rhs.idx)) }
}
}
forward_deref_binop!(impl BitAnd, bitand for JsValue);
impl BitOr for &JsValue {
type Output = JsValue;
#[inline]
fn bitor(self, rhs: Self) -> Self::Output {
unsafe { JsValue::_new(__wbindgen_bit_or(self.idx, rhs.idx)) }
}
}
forward_deref_binop!(impl BitOr, bitor for JsValue);
impl BitXor for &JsValue {
type Output = JsValue;
#[inline]
fn bitxor(self, rhs: Self) -> Self::Output {
unsafe { JsValue::_new(__wbindgen_bit_xor(self.idx, rhs.idx)) }
}
}
forward_deref_binop!(impl BitXor, bitxor for JsValue);
impl Shl for &JsValue {
type Output = JsValue;
#[inline]
fn shl(self, rhs: Self) -> Self::Output {
unsafe { JsValue::_new(__wbindgen_shl(self.idx, rhs.idx)) }
}
}
forward_deref_binop!(impl Shl, shl for JsValue);
impl Shr for &JsValue {
type Output = JsValue;
#[inline]
fn shr(self, rhs: Self) -> Self::Output {
unsafe { JsValue::_new(__wbindgen_shr(self.idx, rhs.idx)) }
}
}
forward_deref_binop!(impl Shr, shr for JsValue);
impl Add for &JsValue {
type Output = JsValue;
#[inline]
fn add(self, rhs: Self) -> Self::Output {
unsafe { JsValue::_new(__wbindgen_add(self.idx, rhs.idx)) }
}
}
forward_deref_binop!(impl Add, add for JsValue);
impl Sub for &JsValue {
type Output = JsValue;
#[inline]
fn sub(self, rhs: Self) -> Self::Output {
unsafe { JsValue::_new(__wbindgen_sub(self.idx, rhs.idx)) }
}
}
forward_deref_binop!(impl Sub, sub for JsValue);
impl Div for &JsValue {
type Output = JsValue;
#[inline]
fn div(self, rhs: Self) -> Self::Output {
unsafe { JsValue::_new(__wbindgen_div(self.idx, rhs.idx)) }
}
}
forward_deref_binop!(impl Div, div for JsValue);
impl Mul for &JsValue {
type Output = JsValue;
#[inline]
fn mul(self, rhs: Self) -> Self::Output {
unsafe { JsValue::_new(__wbindgen_mul(self.idx, rhs.idx)) }
}
}
forward_deref_binop!(impl Mul, mul for JsValue);
impl Rem for &JsValue {
type Output = JsValue;
#[inline]
fn rem(self, rhs: Self) -> Self::Output {
unsafe { JsValue::_new(__wbindgen_rem(self.idx, rhs.idx)) }
}
}
forward_deref_binop!(impl Rem, rem for JsValue);
impl<'a> From<&'a str> for JsValue {
#[inline]
fn from(s: &'a str) -> JsValue {
JsValue::from_str(s)
}
}
impl<T> From<*mut T> for JsValue {
#[inline]
fn from(s: *mut T) -> JsValue {
JsValue::from(s as usize)
}
}
impl<T> From<*const T> for JsValue {
#[inline]
fn from(s: *const T) -> JsValue {
JsValue::from(s as usize)
}
}
impl<T> From<NonNull<T>> for JsValue {
#[inline]
fn from(s: NonNull<T>) -> JsValue {
JsValue::from(s.as_ptr() as usize)
}
}
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 TryFrom<JsValue> for String {
type Error = JsValue;
fn try_from(value: JsValue) -> Result<Self, Self::Error> {
match value.as_string() {
Some(s) => Ok(s),
None => Err(value),
}
}
}
impl TryFromJsValue for String {
type Error = JsValue;
fn try_from_js_value(value: JsValue) -> Result<Self, Self::Error> {
match value.as_string() {
Some(s) => Ok(s),
None => Err(value),
}
}
}
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! big_numbers {
(|$arg:ident|, $($n:ident = $handle:expr,)*) => ($(
impl PartialEq<$n> for JsValue {
#[inline]
fn eq(&self, other: &$n) -> bool {
self == &JsValue::from(*other)
}
}
impl From<$n> for JsValue {
#[inline]
fn from($arg: $n) -> JsValue {
unsafe { JsValue::_new($handle) }
}
}
)*)
}
fn bigint_get_as_i64(v: &JsValue) -> Option<i64> {
unsafe { __wbindgen_bigint_get_as_i64(v.idx).join() }
}
macro_rules! try_from_for_num64 {
($ty:ty) => {
impl TryFrom<JsValue> for $ty {
type Error = JsValue;
#[inline]
fn try_from(v: JsValue) -> Result<Self, JsValue> {
bigint_get_as_i64(&v)
.map(|as_i64| as_i64 as Self)
.filter(|as_self| v == *as_self)
.ok_or(v)
}
}
};
}
try_from_for_num64!(i64);
try_from_for_num64!(u64);
macro_rules! try_from_for_num128 {
($ty:ty, $hi_ty:ty) => {
impl TryFrom<JsValue> for $ty {
type Error = JsValue;
#[inline]
fn try_from(v: JsValue) -> Result<Self, JsValue> {
let lo = match bigint_get_as_i64(&v) {
Some(lo) => lo as u64,
None => return Err(v),
};
let hi = v >> JsValue::from(64_u64);
let hi = <$hi_ty>::try_from(hi)?;
Ok(Self::from(hi) << 64 | Self::from(lo))
}
}
};
}
try_from_for_num128!(i128, i64);
try_from_for_num128!(u128, u64);
big_numbers! {
|n|,
i64 = __wbindgen_bigint_from_i64(n),
u64 = __wbindgen_bigint_from_u64(n),
i128 = __wbindgen_bigint_from_i128((n >> 64) as i64, n as u64),
u128 = __wbindgen_bigint_from_u128((n >> 64) as u64, n as u64),
}
impl PartialEq<usize> for JsValue {
#[inline]
fn eq(&self, other: &usize) -> bool {
*self == (*other as u32)
}
}
impl From<usize> for JsValue {
#[inline]
fn from(n: usize) -> Self {
Self::from(n as u32)
}
}
impl PartialEq<isize> for JsValue {
#[inline]
fn eq(&self, other: &isize) -> bool {
*self == (*other as i32)
}
}
impl From<isize> for JsValue {
#[inline]
fn from(n: isize) -> Self {
Self::from(n as i32)
}
}
externs! {
#[link(wasm_import_module = "__wbindgen_placeholder__")]
extern "C" {
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_bigint_from_str(ptr: *const u8, len: usize) -> u32;
fn __wbindgen_bigint_from_i64(n: i64) -> u32;
fn __wbindgen_bigint_from_u64(n: u64) -> u32;
fn __wbindgen_bigint_from_i128(hi: i64, lo: u64) -> u32;
fn __wbindgen_bigint_from_u128(hi: u64, lo: u64) -> u32;
fn __wbindgen_symbol_named_new(ptr: *const u8, len: usize) -> u32;
fn __wbindgen_symbol_anonymous_new() -> u32;
fn __wbindgen_externref_heap_live_count() -> u32;
fn __wbindgen_is_null(idx: u32) -> u32;
fn __wbindgen_is_undefined(idx: u32) -> u32;
fn __wbindgen_is_symbol(idx: u32) -> u32;
fn __wbindgen_is_object(idx: u32) -> u32;
fn __wbindgen_is_array(idx: u32) -> u32;
fn __wbindgen_is_function(idx: u32) -> u32;
fn __wbindgen_is_string(idx: u32) -> u32;
fn __wbindgen_is_bigint(idx: u32) -> u32;
fn __wbindgen_typeof(idx: u32) -> u32;
fn __wbindgen_in(prop: u32, obj: u32) -> u32;
fn __wbindgen_is_falsy(idx: u32) -> u32;
fn __wbindgen_as_number(idx: u32) -> f64;
fn __wbindgen_try_into_number(idx: u32) -> u32;
fn __wbindgen_neg(idx: u32) -> u32;
fn __wbindgen_bit_and(a: u32, b: u32) -> u32;
fn __wbindgen_bit_or(a: u32, b: u32) -> u32;
fn __wbindgen_bit_xor(a: u32, b: u32) -> u32;
fn __wbindgen_bit_not(idx: u32) -> u32;
fn __wbindgen_shl(a: u32, b: u32) -> u32;
fn __wbindgen_shr(a: u32, b: u32) -> u32;
fn __wbindgen_unsigned_shr(a: u32, b: u32) -> u32;
fn __wbindgen_add(a: u32, b: u32) -> u32;
fn __wbindgen_sub(a: u32, b: u32) -> u32;
fn __wbindgen_div(a: u32, b: u32) -> u32;
fn __wbindgen_checked_div(a: u32, b: u32) -> u32;
fn __wbindgen_mul(a: u32, b: u32) -> u32;
fn __wbindgen_rem(a: u32, b: u32) -> u32;
fn __wbindgen_pow(a: u32, b: u32) -> u32;
fn __wbindgen_lt(a: u32, b: u32) -> u32;
fn __wbindgen_le(a: u32, b: u32) -> u32;
fn __wbindgen_ge(a: u32, b: u32) -> u32;
fn __wbindgen_gt(a: u32, b: u32) -> u32;
fn __wbindgen_number_get(idx: u32) -> WasmRet<Option<f64>>;
fn __wbindgen_boolean_get(idx: u32) -> u32;
fn __wbindgen_string_get(idx: u32) -> WasmSlice;
fn __wbindgen_bigint_get_as_i64(idx: u32) -> WasmRet<Option<i64>>;
fn __wbindgen_debug_string(ret: *mut [usize; 2], idx: u32) -> ();
fn __wbindgen_throw(a: *const u8, b: usize) -> !;
fn __wbindgen_rethrow(a: u32) -> !;
fn __wbindgen_error_new(a: *const u8, b: usize) -> u32;
fn __wbindgen_cb_drop(idx: u32) -> 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) -> WasmSlice;
fn __wbindgen_jsval_eq(a: u32, b: u32) -> u32;
fn __wbindgen_jsval_loose_eq(a: u32, b: u32) -> u32;
fn __wbindgen_copy_to_typed_array(ptr: *const u8, len: usize, idx: u32) -> ();
fn __wbindgen_uint8_array_new(ptr: *mut u8, len: usize) -> u32;
fn __wbindgen_uint8_clamped_array_new(ptr: *mut u8, len: usize) -> u32;
fn __wbindgen_uint16_array_new(ptr: *mut u16, len: usize) -> u32;
fn __wbindgen_uint32_array_new(ptr: *mut u32, len: usize) -> u32;
fn __wbindgen_biguint64_array_new(ptr: *mut u64, len: usize) -> u32;
fn __wbindgen_int8_array_new(ptr: *mut i8, len: usize) -> u32;
fn __wbindgen_int16_array_new(ptr: *mut i16, len: usize) -> u32;
fn __wbindgen_int32_array_new(ptr: *mut i32, len: usize) -> u32;
fn __wbindgen_bigint64_array_new(ptr: *mut i64, len: usize) -> u32;
fn __wbindgen_float32_array_new(ptr: *mut f32, len: usize) -> u32;
fn __wbindgen_float64_array_new(ptr: *mut f64, len: usize) -> u32;
fn __wbindgen_array_new() -> u32;
fn __wbindgen_array_push(array: u32, value: u32) -> ();
fn __wbindgen_not(idx: u32) -> u32;
fn __wbindgen_exports() -> u32;
fn __wbindgen_memory() -> u32;
fn __wbindgen_module() -> u32;
fn __wbindgen_function_table() -> u32;
}
}
impl Clone for JsValue {
#[inline]
fn clone(&self) -> JsValue {
unsafe {
let idx = __wbindgen_object_clone_ref(self.idx);
JsValue::_new(idx)
}
}
}
#[cfg(feature = "std")]
impl core::fmt::Debug for JsValue {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, "JsValue({})", self.as_debug_string())
}
}
#[cfg(not(feature = "std"))]
impl core::fmt::Debug for JsValue {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
f.write_str("JsValue")
}
}
impl Drop for JsValue {
#[inline]
fn drop(&mut self) {
unsafe {
debug_assert!(self.idx >= JSIDX_OFFSET, "free of stack slot {}", self.idx);
if self.idx >= JSIDX_RESERVED {
__wbindgen_object_drop_ref(self.idx);
}
}
}
}
impl Default for JsValue {
fn default() -> Self {
Self::UNDEFINED
}
}
#[cfg(feature = "std")]
#[deprecated = "use with `#[wasm_bindgen(thread_local)]` instead"]
pub struct JsStatic<T: 'static> {
#[doc(hidden)]
pub __inner: &'static std::thread::LocalKey<T>,
}
#[cfg(feature = "std")]
#[allow(deprecated)]
#[cfg(not(target_feature = "atomics"))]
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);
}
}
pub fn externref_heap_live_count() -> u32 {
unsafe { __wbindgen_externref_heap_live_count() }
}
#[doc(hidden)]
pub fn anyref_heap_live_count() -> u32 {
externref_heap_live_count()
}
pub trait UnwrapThrowExt<T>: Sized {
#[cfg_attr(
any(
debug_assertions,
not(all(target_arch = "wasm32", target_os = "unknown"))
),
track_caller
)]
fn unwrap_throw(self) -> T {
if cfg!(all(
debug_assertions,
all(target_arch = "wasm32", target_os = "unknown")
)) {
let loc = core::panic::Location::caller();
let msg = alloc::format!(
"called `{}::unwrap_throw()` ({}:{}:{})",
core::any::type_name::<Self>(),
loc.file(),
loc.line(),
loc.column()
);
self.expect_throw(&msg)
} else {
self.expect_throw("called `unwrap_throw()`")
}
}
#[cfg_attr(
any(
debug_assertions,
not(all(target_arch = "wasm32", target_os = "unknown"))
),
track_caller
)]
fn expect_throw(self, message: &str) -> T;
}
impl<T> UnwrapThrowExt<T> for Option<T> {
fn unwrap_throw(self) -> T {
const MSG: &str = "called `Option::unwrap_throw()` on a `None` value";
if cfg!(all(target_arch = "wasm32", target_os = "unknown")) {
if let Some(val) = self {
val
} else if cfg!(debug_assertions) {
let loc = core::panic::Location::caller();
let msg =
alloc::format!("{} ({}:{}:{})", MSG, loc.file(), loc.line(), loc.column(),);
throw_str(&msg)
} else {
throw_str(MSG)
}
} else {
self.expect(MSG)
}
}
fn expect_throw(self, message: &str) -> T {
if cfg!(all(target_arch = "wasm32", target_os = "unknown")) {
if let Some(val) = self {
val
} else if cfg!(debug_assertions) {
let loc = core::panic::Location::caller();
let msg = alloc::format!(
"{} ({}:{}:{})",
message,
loc.file(),
loc.line(),
loc.column(),
);
throw_str(&msg)
} else {
throw_str(message)
}
} else {
self.expect(message)
}
}
}
impl<T, E> UnwrapThrowExt<T> for Result<T, E>
where
E: core::fmt::Debug,
{
fn unwrap_throw(self) -> T {
const MSG: &str = "called `Result::unwrap_throw()` on an `Err` value";
if cfg!(all(target_arch = "wasm32", target_os = "unknown")) {
match self {
Ok(val) => val,
Err(err) => {
if cfg!(debug_assertions) {
let loc = core::panic::Location::caller();
let msg = alloc::format!(
"{} ({}:{}:{}): {:?}",
MSG,
loc.file(),
loc.line(),
loc.column(),
err
);
throw_str(&msg)
} else {
throw_str(MSG)
}
}
}
} else {
self.expect(MSG)
}
}
fn expect_throw(self, message: &str) -> T {
if cfg!(all(target_arch = "wasm32", target_os = "unknown")) {
match self {
Ok(val) => val,
Err(err) => {
if cfg!(debug_assertions) {
let loc = core::panic::Location::caller();
let msg = alloc::format!(
"{} ({}:{}:{}): {:?}",
message,
loc.file(),
loc.line(),
loc.column(),
err
);
throw_str(&msg)
} else {
throw_str(message)
}
}
}
} else {
self.expect(message)
}
}
}
pub fn module() -> JsValue {
unsafe { JsValue::_new(__wbindgen_module()) }
}
pub fn exports() -> JsValue {
unsafe { JsValue::_new(__wbindgen_exports()) }
}
pub fn memory() -> JsValue {
unsafe { JsValue::_new(__wbindgen_memory()) }
}
pub fn function_table() -> JsValue {
unsafe { JsValue::_new(__wbindgen_function_table()) }
}
#[doc(hidden)]
pub mod __rt {
use crate::JsValue;
use core::borrow::{Borrow, BorrowMut};
use core::cell::{Cell, UnsafeCell};
use core::convert::Infallible;
use core::mem;
use core::ops::{Deref, DerefMut};
pub extern crate alloc;
pub extern crate core;
#[cfg(feature = "std")]
pub extern crate std;
use alloc::alloc::{alloc, dealloc, realloc, Layout};
use alloc::boxed::Box;
use alloc::rc::Rc;
pub use once_cell::sync::Lazy;
#[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 {
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);
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> Borrow<T> for Ref<'b, T> {
#[inline]
fn borrow(&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> Borrow<T> for RefMut<'b, T> {
#[inline]
fn borrow(&self) -> &T {
self.value
}
}
impl<'b, T: ?Sized> BorrowMut<T> for RefMut<'b, T> {
#[inline]
fn borrow_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",
);
}
pub struct RcRef<T: ?Sized + 'static> {
ref_: Ref<'static, T>,
_rc: Rc<WasmRefCell<T>>,
}
impl<T: ?Sized> RcRef<T> {
pub fn new(rc: Rc<WasmRefCell<T>>) -> Self {
let ref_ = unsafe { (*Rc::as_ptr(&rc)).borrow() };
Self { _rc: rc, ref_ }
}
}
impl<T: ?Sized> Deref for RcRef<T> {
type Target = T;
#[inline]
fn deref(&self) -> &T {
&self.ref_
}
}
impl<T: ?Sized> Borrow<T> for RcRef<T> {
#[inline]
fn borrow(&self) -> &T {
&self.ref_
}
}
pub struct RcRefMut<T: ?Sized + 'static> {
ref_: RefMut<'static, T>,
_rc: Rc<WasmRefCell<T>>,
}
impl<T: ?Sized> RcRefMut<T> {
pub fn new(rc: Rc<WasmRefCell<T>>) -> Self {
let ref_ = unsafe { (*Rc::as_ptr(&rc)).borrow_mut() };
Self { _rc: rc, ref_ }
}
}
impl<T: ?Sized> Deref for RcRefMut<T> {
type Target = T;
#[inline]
fn deref(&self) -> &T {
&self.ref_
}
}
impl<T: ?Sized> DerefMut for RcRefMut<T> {
#[inline]
fn deref_mut(&mut self) -> &mut T {
&mut self.ref_
}
}
impl<T: ?Sized> Borrow<T> for RcRefMut<T> {
#[inline]
fn borrow(&self) -> &T {
&self.ref_
}
}
impl<T: ?Sized> BorrowMut<T> for RcRefMut<T> {
#[inline]
fn borrow_mut(&mut self) -> &mut T {
&mut self.ref_
}
}
#[no_mangle]
pub extern "C" fn __wbindgen_malloc(size: usize, align: usize) -> *mut u8 {
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;
}
}
}
malloc_failure();
}
#[no_mangle]
pub unsafe extern "C" fn __wbindgen_realloc(
ptr: *mut u8,
old_size: usize,
new_size: usize,
align: usize,
) -> *mut u8 {
debug_assert!(old_size > 0);
debug_assert!(new_size > 0);
if let Ok(layout) = Layout::from_size_align(old_size, align) {
let ptr = realloc(ptr, layout, new_size);
if !ptr.is_null() {
return ptr;
}
}
malloc_failure();
}
#[cold]
fn malloc_failure() -> ! {
cfg_if::cfg_if! {
if #[cfg(debug_assertions)] {
super::throw_str("invalid malloc request")
} else if #[cfg(feature = "std")] {
std::process::abort();
} else if #[cfg(all(
target_arch = "wasm32",
target_os = "unknown"
))] {
core::arch::wasm32::unreachable();
} else {
unreachable!()
}
}
}
#[no_mangle]
pub unsafe extern "C" fn __wbindgen_free(ptr: *mut u8, size: usize, align: usize) {
if size == 0 {
return;
}
let layout = Layout::from_size_align_unchecked(size, align);
dealloc(ptr, layout);
}
#[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
pub fn link_mem_intrinsics() {
crate::link::link_intrinsics();
}
if_std! {
std::thread_local! {
static GLOBAL_EXNDATA: Cell<[u32; 2]> = Cell::new([0; 2]);
}
#[no_mangle]
pub unsafe extern "C" fn __wbindgen_exn_store(idx: u32) {
GLOBAL_EXNDATA.with(|data| {
debug_assert_eq!(data.get()[0], 0);
data.set([1, idx]);
});
}
pub fn take_last_exception() -> Result<(), super::JsValue> {
GLOBAL_EXNDATA.with(|data| {
let ret = if data.get()[0] == 1 {
Err(super::JsValue::_new(data.get()[1]))
} else {
Ok(())
};
data.set([0, 0]);
ret
})
}
}
pub trait IntoJsResult {
fn into_js_result(self) -> Result<JsValue, JsValue>;
}
impl IntoJsResult for () {
fn into_js_result(self) -> Result<JsValue, JsValue> {
Ok(JsValue::undefined())
}
}
impl<T: Into<JsValue>> IntoJsResult for T {
fn into_js_result(self) -> Result<JsValue, JsValue> {
Ok(self.into())
}
}
impl<T: Into<JsValue>, E: Into<JsValue>> IntoJsResult for Result<T, E> {
fn into_js_result(self) -> Result<JsValue, JsValue> {
match self {
Ok(e) => Ok(e.into()),
Err(e) => Err(e.into()),
}
}
}
impl<E: Into<JsValue>> IntoJsResult for Result<(), E> {
fn into_js_result(self) -> Result<JsValue, JsValue> {
match self {
Ok(()) => Ok(JsValue::undefined()),
Err(e) => Err(e.into()),
}
}
}
pub trait Start {
fn start(self);
}
impl Start for () {
#[inline]
fn start(self) {}
}
impl<E: Into<JsValue>> Start for Result<(), E> {
#[inline]
fn start(self) {
if let Err(e) = self {
crate::throw_val(e.into());
}
}
}
pub struct MainWrapper<T>(pub Option<T>);
pub trait Main {
fn __wasm_bindgen_main(&mut self);
}
impl Main for &mut &mut MainWrapper<()> {
#[inline]
fn __wasm_bindgen_main(&mut self) {}
}
impl Main for &mut &mut MainWrapper<Infallible> {
#[inline]
fn __wasm_bindgen_main(&mut self) {}
}
impl<E: Into<JsValue>> Main for &mut &mut MainWrapper<Result<(), E>> {
#[inline]
fn __wasm_bindgen_main(&mut self) {
if let Err(e) = self.0.take().unwrap() {
crate::throw_val(e.into());
}
}
}
impl<E: core::fmt::Debug> Main for &mut MainWrapper<Result<(), E>> {
#[inline]
fn __wasm_bindgen_main(&mut self) {
if let Err(e) = self.0.take().unwrap() {
crate::throw_str(&alloc::format!("{:?}", e));
}
}
}
pub const fn flat_len<T, const SIZE: usize>(slices: [&[T]; SIZE]) -> usize {
let mut len = 0;
let mut i = 0;
while i < slices.len() {
len += slices[i].len();
i += 1;
}
len
}
pub const fn flat_byte_slices<const RESULT_LEN: usize, const SIZE: usize>(
slices: [&[u8]; SIZE],
) -> [u8; RESULT_LEN] {
let mut result = [0; RESULT_LEN];
let mut slice_index = 0;
let mut result_offset = 0;
while slice_index < slices.len() {
let mut i = 0;
let slice = slices[slice_index];
while i < slice.len() {
result[result_offset] = slice[i];
i += 1;
result_offset += 1;
}
slice_index += 1;
}
result
}
pub const fn encode_u32_to_fixed_len_bytes(value: u32) -> [u8; 5] {
let mut result: [u8; 5] = [0; 5];
let mut i = 0;
while i < 4 {
result[i] = ((value >> (7 * i)) | 0x80) as u8;
i += 1;
}
result[4] = (value >> (7 * 4)) as u8;
result
}
pub trait VectorIntoJsValue: Sized {
fn vector_into_jsvalue(vector: Box<[Self]>) -> JsValue;
}
impl<T: VectorIntoJsValue> From<Box<[T]>> for JsValue {
fn from(vector: Box<[T]>) -> Self {
T::vector_into_jsvalue(vector)
}
}
pub fn js_value_vector_into_jsvalue<T: Into<JsValue>>(vector: Box<[T]>) -> JsValue {
let result = unsafe { JsValue::_new(super::__wbindgen_array_new()) };
for value in vector.into_vec() {
let js: JsValue = value.into();
unsafe { super::__wbindgen_array_push(result.idx, js.idx) }
mem::forget(js);
}
result
}
}
#[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
}
}
#[derive(Clone, Debug)]
pub struct JsError {
value: JsValue,
}
impl JsError {
#[inline]
pub fn new(s: &str) -> JsError {
Self {
value: unsafe { JsValue::_new(crate::__wbindgen_error_new(s.as_ptr(), s.len())) },
}
}
}
if_std! {
impl<E> From<E> for JsError
where
E: std::error::Error,
{
fn from(error: E) -> Self {
JsError::new(&error.to_string())
}
}
}
impl From<JsError> for JsValue {
fn from(error: JsError) -> Self {
error.value
}
}
macro_rules! typed_arrays {
($($ty:ident $ctor:ident $clamped_ctor:ident,)*) => {
$(
impl From<Box<[$ty]>> for JsValue {
fn from(mut vector: Box<[$ty]>) -> Self {
let result = unsafe { JsValue::_new($ctor(vector.as_mut_ptr(), vector.len())) };
mem::forget(vector);
result
}
}
impl From<Clamped<Box<[$ty]>>> for JsValue {
fn from(mut vector: Clamped<Box<[$ty]>>) -> Self {
let result = unsafe { JsValue::_new($clamped_ctor(vector.as_mut_ptr(), vector.len())) };
mem::forget(vector);
result
}
}
)*
};
}
typed_arrays! {
u8 __wbindgen_uint8_array_new __wbindgen_uint8_clamped_array_new,
u16 __wbindgen_uint16_array_new __wbindgen_uint16_array_new,
u32 __wbindgen_uint32_array_new __wbindgen_uint32_array_new,
u64 __wbindgen_biguint64_array_new __wbindgen_biguint64_array_new,
i8 __wbindgen_int8_array_new __wbindgen_int8_array_new,
i16 __wbindgen_int16_array_new __wbindgen_int16_array_new,
i32 __wbindgen_int32_array_new __wbindgen_int32_array_new,
i64 __wbindgen_bigint64_array_new __wbindgen_bigint64_array_new,
f32 __wbindgen_float32_array_new __wbindgen_float32_array_new,
f64 __wbindgen_float64_array_new __wbindgen_float64_array_new,
}
impl __rt::VectorIntoJsValue for JsValue {
fn vector_into_jsvalue(vector: Box<[JsValue]>) -> JsValue {
__rt::js_value_vector_into_jsvalue::<JsValue>(vector)
}
}
impl<T: JsObject> __rt::VectorIntoJsValue for T {
fn vector_into_jsvalue(vector: Box<[T]>) -> JsValue {
__rt::js_value_vector_into_jsvalue::<T>(vector)
}
}
impl __rt::VectorIntoJsValue for String {
fn vector_into_jsvalue(vector: Box<[String]>) -> JsValue {
__rt::js_value_vector_into_jsvalue::<String>(vector)
}
}
impl<T> From<Vec<T>> for JsValue
where
JsValue: From<Box<[T]>>,
{
fn from(vector: Vec<T>) -> Self {
JsValue::from(vector.into_boxed_slice())
}
}
impl<T> From<Clamped<Vec<T>>> for JsValue
where
JsValue: From<Clamped<Box<[T]>>>,
{
fn from(vector: Clamped<Vec<T>>) -> Self {
JsValue::from(Clamped(vector.0.into_boxed_slice()))
}
}