#![allow(unsafe_code)]
use std::{
marker::PhantomData,
cell::RefCell,
collections::HashMap,
future::Future,
pin::Pin,
task::{Context, Poll, RawWaker, RawWakerVTable, Waker},
};
#[cfg(feature = "wasm-bindgen")]
use wasm_bindgen::prelude::*;
#[cfg(all(feature = "stdweb", not(feature = "wasm-bindgen")))]
use stdweb::unstable::TryInto;
thread_local! {
static READY: RefCell<HashMap<i32, JsVar>> = RefCell::new(HashMap::new())
}
fn wake_internal(promise: i32, result: i32) {
READY.with(|w| w.borrow_mut().insert(promise, JsVar(result)));
executor();
}
#[cfg(feature = "wasm-bindgen")]
#[doc = ""]
#[wasm_bindgen]
pub fn wake(promise: i32, result: i32) {
wake_internal(promise, result);
}
#[cfg(all(feature = "stdweb", not(feature = "wasm-bindgen")))]
#[js_export]
fn wake(promise: i32, result: i32) {
wake_internal(promise, result);
}
#[cfg(not(any(feature = "stdweb", feature = "wasm-bindgen")))]
#[no_mangle]
extern "C" fn wake(promise: i32, result: i32) {
wake_internal(promise, result);
}
#[derive(Debug)]
pub struct JsPromise<T: From<JsVar>>(JsVar, PhantomData::<T>);
impl<T: From<JsVar>> JsPromise<T> {
pub fn poll(&self) -> Poll<T> {
READY.with(|w| {
if let Some(value) = w.borrow_mut().remove(&(self.0).0) {
Poll::Ready(value.into())
} else {
Poll::Pending
}
})
}
}
#[derive(Debug)]
pub struct JsVar(i32);
impl JsVar {
pub unsafe fn into_promise<T: From<JsVar>>(self) -> JsPromise<T> {
self.set_waker_internal();
JsPromise(self, PhantomData)
}
pub unsafe fn into_fn(self) -> JsFn {
JsFn(self)
}
pub fn from_i32(value: i32) -> JsVar {
Self::from_i32_internal(value)
}
#[cfg(feature = "wasm-bindgen")]
fn from_i32_internal(value: i32) -> Self {
#[wasm_bindgen]
extern "C" {
fn _cala_js_store_int(idx: i32) -> i32;
}
Self(_cala_js_store_int(value))
}
#[cfg(all(feature = "stdweb", not(feature = "wasm-bindgen")))]
fn from_i32_internal(value: i32) -> Self {
Self(
js! {
return _cala_js_store_int(@{value});
}
.try_into()
.unwrap(),
)
}
#[cfg(not(any(feature = "stdweb", feature = "wasm-bindgen")))]
fn from_i32_internal(value: i32) -> Self {
extern "C" {
fn _cala_js_store_int(idx: i32) -> i32;
}
Self(unsafe { _cala_js_store_int(value) })
}
pub unsafe fn into_i32(&self) -> i32 {
Self::into_i32_internal(self)
}
#[cfg(feature = "wasm-bindgen")]
unsafe fn into_i32_internal(value: &JsVar) -> i32 {
#[wasm_bindgen]
extern "C" {
fn _cala_js_load_int(idx: i32) -> i32;
}
_cala_js_load_int(value.0)
}
#[cfg(all(feature = "stdweb", not(feature = "wasm-bindgen")))]
unsafe fn into_i32_internal(value: &JsVar) -> i32 {
let ret = js! {
return _cala_js_load_int(@{value.0});
};
ret.try_into().unwrap()
}
#[cfg(not(any(feature = "stdweb", feature = "wasm-bindgen")))]
unsafe fn into_i32_internal(value: &JsVar) -> i32 {
extern "C" {
fn _cala_js_load_int(idx: i32) -> i32;
}
_cala_js_load_int(value.0)
}
pub fn from_f32(value: f32) -> JsVar {
Self::from_f32_internal(value)
}
#[cfg(feature = "wasm-bindgen")]
fn from_f32_internal(value: f32) -> Self {
#[wasm_bindgen]
extern "C" {
fn _cala_js_store_float(idx: f32) -> i32;
}
Self(_cala_js_store_float(value))
}
#[cfg(all(feature = "stdweb", not(feature = "wasm-bindgen")))]
fn from_f32_internal(value: f32) -> Self {
Self(
js! {
return _cala_js_store_float(@{value});
}
.try_into()
.unwrap(),
)
}
#[cfg(not(any(feature = "stdweb", feature = "wasm-bindgen")))]
fn from_f32_internal(value: f32) -> Self {
extern "C" {
fn _cala_js_store_float(idx: f32) -> i32;
}
Self(unsafe { _cala_js_store_float(value) })
}
pub unsafe fn into_f32(&self) -> f32 {
Self::into_f32_internal(self)
}
#[cfg(feature = "wasm-bindgen")]
unsafe fn into_f32_internal(value: &JsVar) -> f32 {
#[wasm_bindgen]
extern "C" {
fn _cala_js_load_float(idx: i32) -> f32;
}
_cala_js_load_float(value.0)
}
#[cfg(all(feature = "stdweb", not(feature = "wasm-bindgen")))]
unsafe fn into_f32_internal(value: &JsVar) -> f32 {
let ret = js! {
return _cala_js_load_float(@{value.0});
};
let ret: f64 = ret.try_into().unwrap();
ret as _
}
#[cfg(not(any(feature = "stdweb", feature = "wasm-bindgen")))]
unsafe fn into_f32_internal(value: &JsVar) -> f32 {
extern "C" {
fn _cala_js_load_float(idx: i32) -> f32;
}
_cala_js_load_float(value.0)
}
pub fn from_f64(value: f64) -> JsVar {
Self::from_f64_internal(value)
}
#[cfg(feature = "wasm-bindgen")]
fn from_f64_internal(value: f64) -> Self {
#[wasm_bindgen]
extern "C" {
fn _cala_js_store_double(idx: f64) -> i32;
}
Self(_cala_js_store_double(value))
}
#[cfg(all(feature = "stdweb", not(feature = "wasm-bindgen")))]
fn from_f64_internal(value: f64) -> Self {
Self(
js! {
return _cala_js_store_double(@{value});
}
.try_into()
.unwrap(),
)
}
#[cfg(not(any(feature = "stdweb", feature = "wasm-bindgen")))]
fn from_f64_internal(value: f64) -> Self {
extern "C" {
fn _cala_js_store_double(idx: f64) -> i32;
}
Self(unsafe { _cala_js_store_double(value) })
}
pub unsafe fn into_f64(&self) -> f64 {
Self::into_f64_internal(self)
}
#[cfg(feature = "wasm-bindgen")]
unsafe fn into_f64_internal(value: &JsVar) -> f64 {
#[wasm_bindgen]
extern "C" {
fn _cala_js_load_double(idx: i32) -> f64;
}
_cala_js_load_double(value.0)
}
#[cfg(all(feature = "stdweb", not(feature = "wasm-bindgen")))]
unsafe fn into_f64_internal(value: &JsVar) -> f64 {
let ret = js! {
return _cala_js_load_double(@{value.0});
};
ret.try_into().unwrap()
}
#[cfg(not(any(feature = "stdweb", feature = "wasm-bindgen")))]
unsafe fn into_f64_internal(value: &JsVar) -> f64 {
extern "C" {
fn _cala_js_load_double(idx: i32) -> f64;
}
_cala_js_load_double(value.0)
}
pub unsafe fn read_utf16(&self, output: &mut Vec<u16>) {
let length = self.vecstr(output, 0); output.reserve_exact(length as usize - output.len());
let written = self.vecstr(output, length); output.set_len(written as usize);
debug_assert_eq!(length, written);
}
pub unsafe fn read_bytes(&self, output: &mut Vec<u8>) {
let length = self.vec8i(output, 0); output.reserve_exact(length as usize - output.len());
let written = self.vec8i(output, length); output.set_len(written as usize);
debug_assert_eq!(length, written);
}
pub unsafe fn read_ints(&self, output: &mut Vec<i32>) {
let length = self.vec32i(output, 0); output.reserve_exact(length as usize - output.len());
let written = self.vec32i(output, length); output.set_len(written as usize);
debug_assert_eq!(length, written);
}
pub unsafe fn read_floats(&self, output: &mut Vec<f32>) {
let length = self.vec32f(output, 0); output.reserve_exact(length as usize - output.len());
let written = self.vec32f(output, length); output.set_len(written as usize);
debug_assert_eq!(length, written);
}
pub unsafe fn read_doubles(&self, output: &mut Vec<f64>) {
let length = self.vec64f(output, 0); output.reserve_exact(length as usize - output.len());
let written = self.vec64f(output, length); output.set_len(written as usize);
debug_assert_eq!(length, written);
}
pub unsafe fn write_bytes(&self, input: &[u8]) {
self.slice8i(input);
}
pub unsafe fn write_ints(&self, input: &[i32]) {
self.slice32i(input);
}
pub unsafe fn write_floats(&self, input: &[f32]) {
self.slice32f(input);
}
pub unsafe fn write_doubles(&self, input: &[f64]) {
self.slice64f(input);
}
#[cfg(feature = "wasm-bindgen")]
unsafe fn vec8i(&self, output: &mut Vec<u8>, length: u32) -> u32 {
#[wasm_bindgen]
extern "C" {
fn _cala_js_read_bytes(j: i32, p: u32, l: u32) -> u32;
}
_cala_js_read_bytes(self.0, output.as_mut_ptr() as u32, length)
}
#[cfg(all(feature = "stdweb", not(feature = "wasm-bindgen")))]
unsafe fn vec8i(&self, output: &mut Vec<u8>, length: u32) -> u32 {
let ret = js! {
return _cala_js_read_bytes(
@{self.0},
@{output.as_mut_ptr() as u32},
@{length}
);
};
ret.try_into().unwrap()
}
#[cfg(not(any(feature = "stdweb", feature = "wasm-bindgen")))]
unsafe fn vec8i(&self, output: &mut Vec<u8>, length: u32) -> u32 {
extern "C" {
fn _cala_js_read_bytes(j: i32, p: u32, l: u32) -> u32;
}
_cala_js_read_bytes(self.0, output.as_mut_ptr() as u32, length)
}
#[cfg(feature = "wasm-bindgen")]
unsafe fn vec32i(&self, output: &mut Vec<i32>, length: u32) -> u32 {
#[wasm_bindgen]
extern "C" {
fn _cala_js_read_ints(j: i32, p: u32, l: u32) -> u32;
}
_cala_js_read_ints(self.0, output.as_mut_ptr() as u32, length)
}
#[cfg(all(feature = "stdweb", not(feature = "wasm-bindgen")))]
unsafe fn vec32i(&self, output: &mut Vec<i32>, length: u32) -> u32 {
let ret = js! {
return _cala_js_read_ints(
@{self.0},
@{output.as_mut_ptr() as u32},
@{length}
);
};
ret.try_into().unwrap()
}
#[cfg(not(any(feature = "stdweb", feature = "wasm-bindgen")))]
unsafe fn vec32i(&self, output: &mut Vec<i32>, length: u32) -> u32 {
extern "C" {
fn _cala_js_read_ints(j: i32, p: u32, l: u32) -> u32;
}
_cala_js_read_ints(self.0, output.as_mut_ptr() as u32, length)
}
#[cfg(feature = "wasm-bindgen")]
unsafe fn vec32f(&self, output: &mut Vec<f32>, length: u32) -> u32 {
#[wasm_bindgen]
extern "C" {
fn _cala_js_read_floats(j: i32, p: u32, l: u32) -> u32;
}
_cala_js_read_floats(self.0, output.as_mut_ptr() as u32, length)
}
#[cfg(all(feature = "stdweb", not(feature = "wasm-bindgen")))]
unsafe fn vec32f(&self, output: &mut Vec<f32>, length: u32) -> u32 {
let ret = js! {
return _cala_js_read_floats(
@{self.0},
@{output.as_mut_ptr() as u32},
@{length}
);
};
ret.try_into().unwrap()
}
#[cfg(not(any(feature = "stdweb", feature = "wasm-bindgen")))]
unsafe fn vec32f(&self, output: &mut Vec<f32>, length: u32) -> u32 {
extern "C" {
fn _cala_js_read_floats(j: i32, p: u32, l: u32) -> u32;
}
_cala_js_read_floats(self.0, output.as_mut_ptr() as u32, length)
}
#[cfg(feature = "wasm-bindgen")]
unsafe fn vec64f(&self, output: &mut Vec<f64>, length: u32) -> u32 {
#[wasm_bindgen]
extern "C" {
fn _cala_js_read_doubles(j: i32, p: u32, l: u32) -> u32;
}
_cala_js_read_doubles(self.0, output.as_mut_ptr() as u32, length)
}
#[cfg(all(feature = "stdweb", not(feature = "wasm-bindgen")))]
unsafe fn vec64f(&self, output: &mut Vec<f64>, length: u32) -> u32 {
let ret = js! {
return _cala_js_read_doubles(
@{self.0},
@{output.as_mut_ptr() as u32},
@{length}
);
};
ret.try_into().unwrap()
}
#[cfg(not(any(feature = "stdweb", feature = "wasm-bindgen")))]
unsafe fn vec64f(&self, output: &mut Vec<f64>, length: u32) -> u32 {
extern "C" {
fn _cala_js_read_doubles(j: i32, p: u32, l: u32) -> u32;
}
_cala_js_read_doubles(self.0, output.as_mut_ptr() as u32, length)
}
#[cfg(feature = "wasm-bindgen")]
unsafe fn slice8i(&self, input: &[u8]) {
#[wasm_bindgen]
extern "C" {
fn _cala_js_write_bytes(j: i32, p: u32, l: u32);
}
_cala_js_write_bytes(self.0, input.as_ptr() as u32, input.len() as u32)
}
#[cfg(all(feature = "stdweb", not(feature = "wasm-bindgen")))]
unsafe fn slice8i(&self, input: &[u8]) {
js! {
return _cala_js_write_bytes(
@{self.0},
@{input.as_ptr() as u32},
@{input.len() as u32}
);
};
}
#[cfg(not(any(feature = "stdweb", feature = "wasm-bindgen")))]
unsafe fn slice8i(&self, input: &[u8]) {
extern "C" {
fn _cala_js_write_bytes(j: i32, p: u32, l: u32) -> ();
}
_cala_js_write_bytes(self.0, input.as_ptr() as u32, input.len() as u32)
}
#[cfg(feature = "wasm-bindgen")]
unsafe fn slice32i(&self, input: &[i32]) {
#[wasm_bindgen]
extern "C" {
fn _cala_js_write_ints(j: i32, p: u32, l: u32);
}
_cala_js_write_ints(self.0, input.as_ptr() as u32, input.len() as u32)
}
#[cfg(all(feature = "stdweb", not(feature = "wasm-bindgen")))]
unsafe fn slice32i(&self, input: &[i32]) {
js! {
return _cala_js_write_ints(
@{self.0},
@{input.as_ptr() as u32},
@{input.len() as u32}
);
};
}
#[cfg(not(any(feature = "stdweb", feature = "wasm-bindgen")))]
unsafe fn slice32i(&self, input: &[i32]) {
extern "C" {
fn _cala_js_write_ints(j: i32, p: u32, l: u32) -> ();
}
_cala_js_write_ints(self.0, input.as_ptr() as u32, input.len() as u32)
}
#[cfg(feature = "wasm-bindgen")]
unsafe fn slice32f(&self, input: &[f32]) {
#[wasm_bindgen]
extern "C" {
fn _cala_js_write_floats(j: i32, p: u32, l: u32);
}
_cala_js_write_floats(self.0, input.as_ptr() as u32, input.len() as u32)
}
#[cfg(all(feature = "stdweb", not(feature = "wasm-bindgen")))]
unsafe fn slice32f(&self, input: &[f32]) {
js! {
return _cala_js_write_floats(
@{self.0},
@{input.as_ptr() as u32},
@{input.len() as u32}
);
};
}
#[cfg(not(any(feature = "stdweb", feature = "wasm-bindgen")))]
unsafe fn slice32f(&self, input: &[f32]) {
extern "C" {
fn _cala_js_write_floats(j: i32, p: u32, l: u32) -> ();
}
_cala_js_write_floats(self.0, input.as_ptr() as u32, input.len() as u32)
}
#[cfg(feature = "wasm-bindgen")]
unsafe fn slice64f(&self, input: &[f64]) {
#[wasm_bindgen]
extern "C" {
fn _cala_js_write_doubles(j: i32, p: u32, l: u32);
}
_cala_js_write_doubles(
self.0,
input.as_ptr() as u32,
input.len() as u32,
)
}
#[cfg(all(feature = "stdweb", not(feature = "wasm-bindgen")))]
unsafe fn slice64f(&self, input: &[f64]) {
js! {
_cala_js_write_doubles(
@{self.0},
@{input.as_ptr() as u32},
@{input.len() as u32}
);
};
}
#[cfg(not(any(feature = "stdweb", feature = "wasm-bindgen")))]
unsafe fn slice64f(&self, input: &[f64]) {
extern "C" {
fn _cala_js_write_doubles(j: i32, p: u32, l: u32) -> ();
}
_cala_js_write_doubles(
self.0,
input.as_ptr() as u32,
input.len() as u32,
)
}
#[cfg(feature = "wasm-bindgen")]
unsafe fn vecstr<T>(&self, output: &mut Vec<T>, length: u32) -> u32 {
#[wasm_bindgen]
extern "C" {
fn _cala_js_read_text(idx: i32, p: u32, l: u32) -> u32;
}
_cala_js_read_text(self.0, output.as_ptr() as u32, length)
}
#[cfg(all(feature = "stdweb", not(feature = "wasm-bindgen")))]
unsafe fn vecstr<T>(&self, output: &mut Vec<T>, length: u32) -> u32 {
let ret = js! {
return _cala_js_read_text(
@{self.0},
@{output.as_ptr() as u32},
@{length}
);
};
ret.try_into().unwrap()
}
#[cfg(not(any(feature = "stdweb", feature = "wasm-bindgen")))]
unsafe fn vecstr<T>(&self, output: &mut Vec<T>, length: u32) -> u32 {
extern "C" {
fn _cala_js_read_text(idx: i32, p: u32, l: u32) -> u32;
}
_cala_js_read_text(self.0, output.as_ptr() as u32, length)
}
#[cfg(feature = "wasm-bindgen")]
unsafe fn set_waker_internal(&self) {
#[wasm_bindgen]
extern "C" {
fn _cala_js_waker(idx: i32);
}
_cala_js_waker(self.0)
}
#[cfg(all(feature = "stdweb", not(feature = "wasm-bindgen")))]
unsafe fn set_waker_internal(&self) {
let ret = js! { _cala_js_waker(@{self.0}); };
ret.try_into().unwrap()
}
#[cfg(not(any(feature = "stdweb", feature = "wasm-bindgen")))]
unsafe fn set_waker_internal(&self) {
extern "C" {
fn _cala_js_waker(idx: i32);
}
_cala_js_waker(self.0)
}
fn drop_internal(&self) {
READY.with(|w| w.borrow_mut().remove(&self.0));
}
}
impl Drop for JsVar {
#[cfg(feature = "wasm-bindgen")]
fn drop(&mut self) {
#[wasm_bindgen]
extern "C" {
fn _cala_js_free(idx: i32);
}
self.drop_internal();
_cala_js_free(self.0);
}
#[cfg(all(feature = "stdweb", not(feature = "wasm-bindgen")))]
fn drop(&mut self) {
self.drop_internal();
js! {
_cala_js_free(@{self.0});
}
}
#[cfg(not(any(feature = "stdweb", feature = "wasm-bindgen")))]
fn drop(&mut self) {
extern "C" {
fn _cala_js_free(idx: i32) -> ();
}
self.drop_internal();
unsafe {
_cala_js_free(self.0);
}
}
}
#[derive(Debug)]
pub struct JsString(JsVar);
impl JsString {
#[cfg(feature = "wasm-bindgen")]
pub fn new(string: &str) -> JsString {
#[wasm_bindgen]
extern "C" {
fn _cala_js_text(p: u32, l: u32) -> i32;
}
let mut text = Vec::with_capacity(string.len());
for c in string.encode_utf16() {
text.push(c);
}
JsString(JsVar(_cala_js_text(
text.as_ptr() as u32,
text.len() as u32,
)))
}
#[cfg(all(feature = "stdweb", not(feature = "wasm-bindgen")))]
pub fn new(string: &str) -> JsString {
let mut text = Vec::with_capacity(string.len());
for c in string.encode_utf16() {
text.push(c);
}
let string = js! {
return _cala_js_text(@{text.as_ptr() as u32}, @{text.len() as u32});
};
JsString(JsVar(string.try_into().unwrap()))
}
#[cfg(not(any(feature = "stdweb", feature = "wasm-bindgen")))]
pub fn new(string: &str) -> JsString {
extern "C" {
fn _cala_js_text(p: u32, l: u32) -> i32;
}
let mut text = Vec::with_capacity(string.len());
for c in string.encode_utf16() {
text.push(c);
}
JsString(JsVar(unsafe {
_cala_js_text(text.as_ptr() as u32, text.len() as u32)
}))
}
pub fn as_var(&self) -> &JsVar {
&self.0
}
pub unsafe fn from_var(var: JsVar) -> JsString {
JsString(var)
}
}
#[derive(Debug)]
pub struct JsFn(JsVar);
impl JsFn {
#[cfg(feature = "wasm-bindgen")]
pub unsafe fn new(string: &str) -> JsFn {
#[wasm_bindgen]
extern "C" {
fn _cala_js_function(idx: i32) -> i32;
}
let javascript = format!(
"\
\"use strict\";\
return function(param_a, param_b) {{ {} }};\
",
string
);
let string = JsString::new(&javascript);
let func = _cala_js_function(string.as_var().0);
JsFn(JsVar(func))
}
#[cfg(all(feature = "stdweb", not(feature = "wasm-bindgen")))]
pub unsafe fn new(string: &str) -> JsFn {
let javascript = format!(
"\
\"use strict\";\
return function(param_a, param_b) {{ {} }};\
",
string
);
let string = JsString::new(&javascript);
let func = js! {
return _cala_js_function(@{string.as_var().0});
};
JsFn(JsVar(func.try_into().unwrap()))
}
#[cfg(not(any(feature = "stdweb", feature = "wasm-bindgen")))]
pub unsafe fn new(string: &str) -> JsFn {
extern "C" {
fn _cala_js_function(idx: i32) -> i32;
}
let javascript = format!(
"\
\"use strict\";\
return function(param_a, param_b) {{ {} }};\
",
string
);
let string = JsString::new(&javascript);
let func = _cala_js_function(string.as_var().0);
JsFn(JsVar(func))
}
#[cfg(feature = "wasm-bindgen")]
pub unsafe fn call(
&self,
a: Option<&JsVar>,
b: Option<&JsVar>,
) -> Option<JsVar> {
#[wasm_bindgen]
extern "C" {
fn _cala_js_call(function: i32, param_a: i32, param_b: i32) -> i32;
}
let ret = _cala_js_call(
(self.0).0,
a.map(|x| x.0).unwrap_or(-1),
b.map(|x| x.0).unwrap_or(-1),
);
if ret == -1 {
None
} else {
Some(JsVar(ret))
}
}
#[cfg(all(feature = "stdweb", not(feature = "wasm-bindgen")))]
pub unsafe fn call(
&self,
a: Option<&JsVar>,
b: Option<&JsVar>,
) -> Option<JsVar> {
let ret = js! {
return _cala_js_call(@{(self.0).0},
@{a.map(|x| x.0).unwrap_or(-1)},
@{b.map(|x| x.0).unwrap_or(-1)});
};
if ret == -1 {
None
} else {
Some(JsVar(ret.try_into().unwrap()))
}
}
#[cfg(not(any(feature = "stdweb", feature = "wasm-bindgen")))]
pub unsafe fn call(
&self,
a: Option<&JsVar>,
b: Option<&JsVar>,
) -> Option<JsVar> {
extern "C" {
fn _cala_js_call(function: i32, param_a: i32, param_b: i32) -> i32;
}
let ret = _cala_js_call(
(self.0).0,
a.map(|x| x.0).unwrap_or(-1),
b.map(|x| x.0).unwrap_or(-1),
);
if ret == -1 {
None
} else {
Some(JsVar(ret))
}
}
}
thread_local!(
static FUTURE: RefCell<Option<Pin<Box<dyn Future<Output = ()>>>>> =
RefCell::new(None);
);
pub fn block_on<F: Future<Output = ()> + 'static>(main: F) {
FUTURE.with(move |future| {
*future.borrow_mut() = Some(Box::pin(async {
panic_hook();
main.await;
}));
});
executor();
}
fn executor() {
FUTURE.with(|future| {
if let Some(future) = future.borrow_mut().as_mut() {
let waker = waker();
let mut cx = Context::from_waker(&waker);
let _ = future.as_mut().poll(&mut cx);
}
});
}
#[inline]
fn waker() -> Waker {
#[inline]
unsafe fn clone(data: *const ()) -> RawWaker {
RawWaker::new(data, &RawWakerVTable::new(clone, wake, wake, drop))
}
#[inline]
unsafe fn wake(_data: *const ()) {
executor();
}
#[inline]
unsafe fn drop(_data: *const ()) {}
unsafe {
Waker::from_raw(RawWaker::new(
std::ptr::null(),
&RawWakerVTable::new(clone, wake, wake, drop),
))
}
}
fn panic_hook_internal(panic_info: &std::panic::PanicInfo<'_>) {
let msg = panic_info.to_string();
let message = JsString::new(&format!("Cala App panicked!: {:?}", msg));
let eprint = unsafe { JsFn::new("throw new Error(param_a);") };
unsafe {
assert!(eprint.call(Some(message.as_var()), None).is_none());
}
}
pub fn panic_hook() {
let hook = std::panic::take_hook();
std::panic::set_hook(Box::new(move |p| {
hook(p);
panic_hook_internal(p);
}));
}