#![feature(use_extern_macros, wasm_import_module, try_reserve, unsize)]
extern crate wasm_bindgen_macro;
use std::cell::UnsafeCell;
use std::ops::Deref;
use std::ptr;
use convert::FromWasmAbi;
pub mod prelude {
pub use wasm_bindgen_macro::wasm_bindgen;
pub use JsValue;
pub use closure::Closure;
}
pub mod convert;
pub mod closure;
pub mod describe;
pub struct JsValue {
idx: u32,
}
impl JsValue {
pub fn from_str(s: &str) -> JsValue {
unsafe {
JsValue { idx: __wbindgen_string_new(s.as_ptr(), s.len()) }
}
}
pub fn from_f64(n: f64) -> JsValue {
unsafe {
JsValue { idx: __wbindgen_number_new(n) }
}
}
pub fn from_bool(b: bool) -> JsValue {
unsafe {
JsValue { idx: __wbindgen_boolean_new(b as u32) }
}
}
pub fn undefined() -> JsValue {
unsafe {
JsValue { idx: __wbindgen_undefined_new() }
}
}
pub fn null() -> JsValue {
unsafe {
JsValue { idx: __wbindgen_null_new() }
}
}
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 { idx: __wbindgen_symbol_new(ptr, len) }
}
}
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 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,
}
}
}
pub fn is_null(&self) -> bool {
unsafe {
__wbindgen_is_null(self.idx) == 1
}
}
pub fn is_undefined(&self) -> bool {
unsafe {
__wbindgen_is_undefined(self.idx) == 1
}
}
pub fn is_symbol(&self) -> bool {
unsafe {
__wbindgen_is_symbol(self.idx) == 1
}
}
}
impl<'a> From<&'a str> for JsValue {
fn from(s: &'a str) -> JsValue {
JsValue::from_str(s)
}
}
impl<'a> From<&'a String> for JsValue {
fn from(s: &'a String) -> JsValue {
JsValue::from_str(s)
}
}
impl From<bool> for JsValue {
fn from(s: bool) -> JsValue {
JsValue::from_bool(s)
}
}
macro_rules! numbers {
($($n:ident)*) => ($(
impl From<$n> for JsValue {
fn from(n: $n) -> JsValue {
JsValue::from_f64(n.into())
}
}
)*)
}
numbers! { i8 u8 i16 u16 i32 u32 f32 f64 }
#[wasm_import_module = "__wbindgen_placeholder__"]
extern {
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_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;
fn __wbindgen_throw(a: *const u8, b: usize) -> !;
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);
fn __wbindgen_cb_forget(idx: u32);
fn __wbindgen_describe(v: u32);
}
impl Clone for JsValue {
fn clone(&self) -> JsValue {
unsafe {
let idx = __wbindgen_object_clone_ref(self.idx);
JsValue { idx }
}
}
}
impl Drop for JsValue {
fn drop(&mut self) {
unsafe {
__wbindgen_object_drop_ref(self.idx);
}
}
}
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> {}
impl<T: FromWasmAbi + 'static> Deref for JsStatic<T> {
type Target = T;
fn deref(&self) -> &T {
unsafe {
(*self.__inner.get()).get_or_insert_with(|| {
(self.__init)()
})
}
}
}
#[cold]
#[inline(never)]
pub fn throw(s: &str) -> ! {
unsafe {
__wbindgen_throw(s.as_ptr(), s.len());
}
}
#[doc(hidden)]
pub mod __rt {
use std::cell::{Cell, UnsafeCell};
use std::mem;
use std::ops::{Deref, DerefMut};
#[inline]
pub fn assert_not_null<T>(s: *mut T) {
if s.is_null() {
throw_null();
}
}
#[cold]
#[inline(never)]
fn throw_null() -> ! {
super::throw("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("recursive use of an object detected which would lead to \
unsafe aliasing in rust");
}
#[no_mangle]
pub extern fn __wbindgen_malloc(size: usize) -> *mut u8 {
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
}
#[no_mangle]
pub unsafe extern fn __wbindgen_free(ptr: *mut u8, size: usize) {
drop(Vec::<u8>::from_raw_parts(ptr, 0, size));
}
pub fn link_this_library() {}
}