2018-08-08 17:41:36 -07:00
|
|
|
//! Converting between JavaScript `Promise`s to Rust `Future`s.
|
2018-08-01 15:52:24 -05:00
|
|
|
//!
|
2018-08-08 17:41:36 -07:00
|
|
|
//! This crate provides a bridge for working with JavaScript `Promise` types as
|
|
|
|
//! a Rust `Future`, and similarly contains utilities to turn a rust `Future`
|
|
|
|
//! into a JavaScript `Promise`. This can be useful when working with
|
|
|
|
//! asynchronous or otherwise blocking work in Rust (wasm), and provides the
|
|
|
|
//! ability to interoperate with JavaScript events and JavaScript I/O
|
|
|
|
//! primitives.
|
2018-08-01 15:52:24 -05:00
|
|
|
//!
|
|
|
|
//! There are two main interfaces in this crate currently:
|
|
|
|
//!
|
2018-08-08 17:41:36 -07:00
|
|
|
//! 1. [**`JsFuture`**](./struct.JsFuture.html)
|
2018-08-01 15:52:24 -05:00
|
|
|
//!
|
2018-08-08 17:41:36 -07:00
|
|
|
//! A type that is constructed with a `Promise` and can then be used as a
|
|
|
|
//! `Future<Item = JsValue, Error = JsValue>`. This Rust future will resolve
|
|
|
|
//! or reject with the value coming out of the `Promise`.
|
|
|
|
//!
|
|
|
|
//! 2. [**`future_to_promise`**](./fn.future_to_promise.html)
|
|
|
|
//!
|
|
|
|
//! Converts a Rust `Future<Item = JsValue, Error = JsValue>` into a
|
|
|
|
//! JavaScript `Promise`. The future's result will translate to either a
|
|
|
|
//! rejected or resolved `Promise` in JavaScript.
|
|
|
|
//!
|
|
|
|
//! These two items should provide enough of a bridge to interoperate the two
|
|
|
|
//! systems and make sure that Rust/JavaScript can work together with
|
|
|
|
//! asynchronous and I/O work.
|
|
|
|
//!
|
|
|
|
//! # Example Usage
|
|
|
|
//!
|
|
|
|
//! This example wraps JavaScript's `Promise.resolve()` into a Rust `Future` for
|
|
|
|
//! running tasks on the next tick of the micro task queue. The futures built on
|
|
|
|
//! top of it can be scheduled for execution by conversion into a JavaScript
|
|
|
|
//! `Promise`.
|
|
|
|
//!
|
|
|
|
//! ```rust,no_run
|
|
|
|
//! extern crate futures;
|
|
|
|
//! extern crate js_sys;
|
|
|
|
//! extern crate wasm_bindgen;
|
|
|
|
//! extern crate wasm_bindgen_futures;
|
|
|
|
//!
|
|
|
|
//! use futures::{Async, Future, Poll};
|
|
|
|
//! use wasm_bindgen::prelude::*;
|
|
|
|
//! use wasm_bindgen_futures::{JsFuture, future_to_promise};
|
|
|
|
//!
|
|
|
|
//! /// A future that becomes ready after a tick of the micro task queue.
|
|
|
|
//! pub struct NextTick {
|
|
|
|
//! inner: JsFuture,
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! impl NextTick {
|
|
|
|
//! /// Construct a new `NextTick` future.
|
|
|
|
//! pub fn new() -> NextTick {
|
|
|
|
//! // Create a resolved promise that will run its callbacks on the next
|
|
|
|
//! // tick of the micro task queue.
|
2018-08-09 13:08:30 -07:00
|
|
|
//! let promise = js_sys::Promise::resolve(&JsValue::NULL);
|
2018-08-08 17:41:36 -07:00
|
|
|
//! // Convert the promise into a `JsFuture`.
|
|
|
|
//! let inner = JsFuture::from(promise);
|
|
|
|
//! NextTick { inner }
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! impl Future for NextTick {
|
|
|
|
//! type Item = ();
|
|
|
|
//! type Error = ();
|
|
|
|
//!
|
|
|
|
//! fn poll(&mut self) -> Poll<(), ()> {
|
|
|
|
//! // Polling a `NextTick` just forwards to polling if the inner promise is
|
|
|
|
//! // ready.
|
|
|
|
//! match self.inner.poll() {
|
|
|
|
//! Ok(Async::Ready(_)) => Ok(Async::Ready(())),
|
|
|
|
//! Ok(Async::NotReady) => Ok(Async::NotReady),
|
|
|
|
//! Err(_) => unreachable!(
|
|
|
|
//! "We only create NextTick with a resolved inner promise, never \
|
|
|
|
//! a rejected one, so we can't get an error here"
|
|
|
|
//! ),
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! /// Export a function to JavaScript that does some work in the next tick of the
|
|
|
|
//! /// micro task queue!
|
|
|
|
//! #[wasm_bindgen]
|
|
|
|
//! pub fn schedule_some_work_for_next_tick() -> js_sys::Promise {
|
|
|
|
//! let future = NextTick::new()
|
|
|
|
//! // Do some work...
|
|
|
|
//! .and_then(|_| {
|
|
|
|
//! Ok(42)
|
|
|
|
//! })
|
|
|
|
//! // And then convert the `Item` and `Error` into `JsValue`.
|
|
|
|
//! .map(|result| {
|
|
|
|
//! JsValue::from(result)
|
|
|
|
//! })
|
|
|
|
//! .map_err(|error| {
|
|
|
|
//! let js_error = js_sys::Error::new(&format!("uh oh! {:?}", error));
|
|
|
|
//! JsValue::from(js_error)
|
|
|
|
//! });
|
|
|
|
//!
|
|
|
|
//! // Convert the `Future<Item = JsValue, Error = JsValue>` into a JavaScript
|
|
|
|
//! // `Promise`!
|
|
|
|
//! future_to_promise(future)
|
|
|
|
//! }
|
|
|
|
//! ```
|
2018-08-01 15:52:24 -05:00
|
|
|
|
|
|
|
#![deny(missing_docs)]
|
|
|
|
|
2018-09-26 08:26:00 -07:00
|
|
|
use std::cell::{Cell, RefCell};
|
2018-10-10 12:41:26 -07:00
|
|
|
use std::rc::Rc;
|
2018-08-01 15:52:24 -05:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2018-09-26 08:26:00 -07:00
|
|
|
use futures::executor::{self, Notify, Spawn};
|
2019-01-12 17:42:05 +01:00
|
|
|
use futures::future;
|
Migrate `wasm-bindgen` to using `walrus`
This commit moves `wasm-bindgen` the CLI tool from internally using
`parity-wasm` for wasm parsing/serialization to instead use `walrus`.
The `walrus` crate is something we've been working on recently with an
aim to replace the usage of `parity-wasm` in `wasm-bindgen` to make the
current CLI tool more maintainable as well as more future-proof.
The `walrus` crate provides a much nicer AST to work with as well as a
structured `Module`, whereas `parity-wasm` provides a very raw interface
to the wasm module which isn't really appropriate for our use case. The
many transformations and tweaks that wasm-bindgen does have a huge
amount of ad-hoc index management to carefully craft a final wasm
binary, but this is all entirely taken care for us with the `walrus`
crate.
Additionally, `wasm-bindgen` will ingest and rewrite the wasm file,
often changing the binary offsets of functions. Eventually with DWARF
debug information we'll need to be sure to preserve the debug
information throughout the transformations that `wasm-bindgen` does
today. This is practically impossible to do with the `parity-wasm`
architecture, but `walrus` was designed from the get-go to solve this
problem transparently in the `walrus` crate itself. (it doesn't today,
but this is planned work)
It is the intention that this does not end up regressing any
`wasm-bindgen` use cases, neither in functionality or in speed. As a
large change and refactoring, however, it's likely that at least
something will arise! We'll want to continue to remain vigilant to any
issues that come up with this commit.
Note that the `gc` crate has been deleted as part of this change, as the
`gc` crate is no longer necessary since `walrus` does it automatically.
Additionally the `gc` crate was one of the main problems with preserving
debug information as it often deletes wasm items!
Finally, this also starts moving crates to the 2018 edition where
necessary since `walrus` requires the 2018 edition, and in general it's
more pleasant to work within the 2018 edition!
2019-01-31 09:54:23 -08:00
|
|
|
use futures::prelude::*;
|
2018-08-01 15:52:24 -05:00
|
|
|
use futures::sync::oneshot;
|
|
|
|
use js_sys::{Function, Promise};
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
|
2018-08-08 17:41:36 -07:00
|
|
|
/// A Rust `Future` backed by a JavaScript `Promise`.
|
2018-08-01 15:52:24 -05:00
|
|
|
///
|
2018-08-08 17:41:36 -07:00
|
|
|
/// This type is constructed with a JavaScript `Promise` object and translates
|
|
|
|
/// it to a Rust `Future`. This type implements the `Future` trait from the
|
|
|
|
/// `futures` crate and will either succeed or fail depending on what happens
|
|
|
|
/// with the JavaScript `Promise`.
|
2018-08-01 15:52:24 -05:00
|
|
|
///
|
|
|
|
/// Currently this type is constructed with `JsFuture::from`.
|
|
|
|
pub struct JsFuture {
|
|
|
|
resolved: oneshot::Receiver<JsValue>,
|
|
|
|
rejected: oneshot::Receiver<JsValue>,
|
|
|
|
callbacks: Option<(Closure<FnMut(JsValue)>, Closure<FnMut(JsValue)>)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Promise> for JsFuture {
|
|
|
|
fn from(js: Promise) -> JsFuture {
|
|
|
|
// Use the `then` method to schedule two callbacks, one for the
|
|
|
|
// resolved value and one for the rejected value. These two callbacks
|
|
|
|
// will be connected to oneshot channels which feed back into our
|
|
|
|
// future.
|
|
|
|
//
|
|
|
|
// This may not be the speediest option today but it should work!
|
|
|
|
let (tx1, rx1) = oneshot::channel();
|
|
|
|
let (tx2, rx2) = oneshot::channel();
|
|
|
|
let mut tx1 = Some(tx1);
|
|
|
|
let resolve = Closure::wrap(Box::new(move |val| {
|
|
|
|
drop(tx1.take().unwrap().send(val));
|
|
|
|
}) as Box<FnMut(_)>);
|
|
|
|
let mut tx2 = Some(tx2);
|
|
|
|
let reject = Closure::wrap(Box::new(move |val| {
|
|
|
|
drop(tx2.take().unwrap().send(val));
|
|
|
|
}) as Box<FnMut(_)>);
|
|
|
|
|
|
|
|
js.then2(&resolve, &reject);
|
|
|
|
|
|
|
|
JsFuture {
|
|
|
|
resolved: rx1,
|
|
|
|
rejected: rx2,
|
|
|
|
callbacks: Some((resolve, reject)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Future for JsFuture {
|
|
|
|
type Item = JsValue;
|
|
|
|
type Error = JsValue;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<JsValue, JsValue> {
|
|
|
|
// Test if either our resolved or rejected side is finished yet. Note
|
|
|
|
// that they will return errors if they're disconnected which can't
|
|
|
|
// happen until we drop the `callbacks` field, which doesn't happen
|
|
|
|
// till we're done, so we dont need to handle that.
|
|
|
|
if let Ok(Async::Ready(val)) = self.resolved.poll() {
|
|
|
|
drop(self.callbacks.take());
|
2018-09-26 08:26:00 -07:00
|
|
|
return Ok(val.into());
|
2018-08-01 15:52:24 -05:00
|
|
|
}
|
|
|
|
if let Ok(Async::Ready(val)) = self.rejected.poll() {
|
|
|
|
drop(self.callbacks.take());
|
2018-09-26 08:26:00 -07:00
|
|
|
return Err(val);
|
2018-08-01 15:52:24 -05:00
|
|
|
}
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-08 17:41:36 -07:00
|
|
|
/// Converts a Rust `Future` into a JavaScript `Promise`.
|
2018-08-01 15:52:24 -05:00
|
|
|
///
|
|
|
|
/// This function will take any future in Rust and schedule it to be executed,
|
2018-08-08 17:41:36 -07:00
|
|
|
/// returning a JavaScript `Promise` which can then be passed back to JavaScript
|
|
|
|
/// to get plumbed into the rest of a system.
|
2018-08-01 15:52:24 -05:00
|
|
|
///
|
|
|
|
/// The `future` provided must adhere to `'static` because it'll be scheduled
|
|
|
|
/// to run in the background and cannot contain any stack references. The
|
|
|
|
/// returned `Promise` will be resolved or rejected when the future completes,
|
|
|
|
/// depending on whether it finishes with `Ok` or `Err`.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// Note that in wasm panics are currently translated to aborts, but "abort" in
|
2018-08-08 17:41:36 -07:00
|
|
|
/// this case means that a JavaScript exception is thrown. The wasm module is
|
|
|
|
/// still usable (likely erroneously) after Rust panics.
|
2018-08-01 15:52:24 -05:00
|
|
|
///
|
|
|
|
/// If the `future` provided panics then the returned `Promise` **will not
|
|
|
|
/// resolve**. Instead it will be a leaked promise. This is an unfortunate
|
|
|
|
/// limitation of wasm currently that's hoped to be fixed one day!
|
|
|
|
pub fn future_to_promise<F>(future: F) -> Promise
|
2018-09-26 08:26:00 -07:00
|
|
|
where
|
|
|
|
F: Future<Item = JsValue, Error = JsValue> + 'static,
|
2018-08-01 15:52:24 -05:00
|
|
|
{
|
|
|
|
_future_to_promise(Box::new(future))
|
|
|
|
}
|
|
|
|
|
2018-08-08 17:41:36 -07:00
|
|
|
// Implementation of actually transforming a future into a JavaScript `Promise`.
|
2018-08-01 15:52:24 -05:00
|
|
|
//
|
|
|
|
// The only primitive we have to work with here is `Promise::new`, which gives
|
|
|
|
// us two callbacks that we can use to either reject or resolve the promise.
|
|
|
|
// It's our job to ensure that one of those callbacks is called at the
|
|
|
|
// appropriate time.
|
|
|
|
//
|
2018-08-08 17:41:36 -07:00
|
|
|
// Now we know that JavaScript (in general) can't block and is largely
|
2018-08-01 15:52:24 -05:00
|
|
|
// notification/callback driven. That means that our future must either have
|
|
|
|
// synchronous computational work to do, or it's "scheduled a notification" to
|
|
|
|
// happen. These notifications are likely callbacks to get executed when things
|
|
|
|
// finish (like a different promise or something like `setTimeout`). The general
|
|
|
|
// idea here is thus to do as much synchronous work as we can and then otherwise
|
|
|
|
// translate notifications of a future's task into "let's poll the future!"
|
|
|
|
//
|
|
|
|
// This isn't necessarily the greatest future executor in the world, but it
|
|
|
|
// should get the job done for now hopefully.
|
|
|
|
fn _future_to_promise(future: Box<Future<Item = JsValue, Error = JsValue>>) -> Promise {
|
|
|
|
let mut future = Some(executor::spawn(future));
|
|
|
|
return Promise::new(&mut |resolve, reject| {
|
|
|
|
Package::poll(&Arc::new(Package {
|
|
|
|
spawn: RefCell::new(future.take().unwrap()),
|
|
|
|
resolve,
|
|
|
|
reject,
|
|
|
|
notified: Cell::new(State::Notified),
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
struct Package {
|
|
|
|
// Our "spawned future". This'll have everything we need to poll the
|
|
|
|
// future and continue to move it forward.
|
|
|
|
spawn: RefCell<Spawn<Box<Future<Item = JsValue, Error = JsValue>>>>,
|
|
|
|
|
|
|
|
// The current state of this future, expressed in an enum below. This
|
|
|
|
// indicates whether we're currently polling the future, received a
|
|
|
|
// notification and need to keep polling, or if we're waiting for a
|
|
|
|
// notification to come in (and no one is polling).
|
|
|
|
notified: Cell<State>,
|
|
|
|
|
2018-08-08 17:41:36 -07:00
|
|
|
// Our two callbacks connected to the `Promise` that we returned to
|
|
|
|
// JavaScript. We'll be invoking one of these at the end.
|
2018-08-01 15:52:24 -05:00
|
|
|
resolve: Function,
|
|
|
|
reject: Function,
|
|
|
|
}
|
|
|
|
|
|
|
|
// The possible states our `Package` (future) can be in, tracked internally
|
|
|
|
// and used to guide what happens when polling a future.
|
|
|
|
enum State {
|
|
|
|
// This future is currently and actively being polled. Attempting to
|
|
|
|
// access the future will result in a runtime panic and is considered a
|
|
|
|
// bug.
|
|
|
|
Polling,
|
|
|
|
|
|
|
|
// This future has been notified, while it was being polled. This marker
|
|
|
|
// is used in the `Notify` implementation below, and indicates that a
|
|
|
|
// notification was received that the future is ready to make progress.
|
|
|
|
// If seen, however, it probably means that the future is also currently
|
|
|
|
// being polled.
|
|
|
|
Notified,
|
|
|
|
|
|
|
|
// The future is blocked, waiting for something to happen. Stored here
|
|
|
|
// is a self-reference to the future itself so we can pull it out in
|
|
|
|
// `Notify` and continue polling.
|
|
|
|
//
|
|
|
|
// Note that the self-reference here is an Arc-cycle that will leak
|
|
|
|
// memory unless the future completes, but currently that should be ok
|
|
|
|
// as we'll have to stick around anyway while the future is executing!
|
|
|
|
//
|
|
|
|
// This state is removed as soon as a notification comes in, so the leak
|
|
|
|
// should only be "temporary"
|
|
|
|
Waiting(Arc<Package>),
|
|
|
|
}
|
|
|
|
|
|
|
|
// No shared memory right now, wasm is single threaded, no need to worry
|
|
|
|
// about this!
|
|
|
|
unsafe impl Send for Package {}
|
|
|
|
unsafe impl Sync for Package {}
|
|
|
|
|
|
|
|
impl Package {
|
|
|
|
// Move the future contained in `me` as far forward as we can. This will
|
|
|
|
// do as much synchronous work as possible to complete the future,
|
|
|
|
// ensuring that when it blocks we're scheduled to get notified via some
|
|
|
|
// callback somewhere at some point (vague, right?)
|
|
|
|
//
|
|
|
|
// TODO: this probably shouldn't do as much synchronous work as possible
|
|
|
|
// as it can starve other computations. Rather it should instead
|
|
|
|
// yield every so often with something like `setTimeout` with the
|
|
|
|
// timeout set to zero.
|
|
|
|
fn poll(me: &Arc<Package>) {
|
|
|
|
loop {
|
|
|
|
match me.notified.replace(State::Polling) {
|
|
|
|
// We received a notification while previously polling, or
|
|
|
|
// this is the initial poll. We've got work to do below!
|
|
|
|
State::Notified => {}
|
|
|
|
|
|
|
|
// We've gone through this loop once and no notification was
|
|
|
|
// received while we were executing work. That means we got
|
|
|
|
// `NotReady` below and we're scheduled to receive a
|
|
|
|
// notification. Block ourselves and wait for later.
|
|
|
|
//
|
|
|
|
// When the notification comes in it'll notify our task, see
|
|
|
|
// our `Waiting` state, and resume the polling process
|
|
|
|
State::Polling => {
|
|
|
|
me.notified.set(State::Waiting(me.clone()));
|
2018-09-26 08:26:00 -07:00
|
|
|
break;
|
2018-08-01 15:52:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
State::Waiting(_) => panic!("shouldn't see waiting state!"),
|
|
|
|
}
|
|
|
|
|
|
|
|
let (val, f) = match me.spawn.borrow_mut().poll_future_notify(me, 0) {
|
|
|
|
// If the future is ready, immediately call the
|
|
|
|
// resolve/reject callback and then return as we're done.
|
|
|
|
Ok(Async::Ready(value)) => (value, &me.resolve),
|
|
|
|
Err(value) => (value, &me.reject),
|
|
|
|
|
|
|
|
// Otherwise keep going in our loop, if we weren't notified
|
|
|
|
// we'll break out and start waiting.
|
|
|
|
Ok(Async::NotReady) => continue,
|
|
|
|
};
|
|
|
|
|
|
|
|
drop(f.call1(&JsValue::undefined(), &val));
|
2018-09-26 08:26:00 -07:00
|
|
|
break;
|
2018-08-01 15:52:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Notify for Package {
|
|
|
|
fn notify(&self, _id: usize) {
|
2018-10-10 12:41:26 -07:00
|
|
|
let me = match self.notified.replace(State::Notified) {
|
|
|
|
// we need to schedule polling to resume, so keep going
|
|
|
|
State::Waiting(me) => me,
|
2018-08-01 15:52:24 -05:00
|
|
|
|
|
|
|
// we were already notified, and were just notified again;
|
|
|
|
// having now coalesced the notifications we return as it's
|
|
|
|
// still someone else's job to process this
|
2018-10-10 12:41:26 -07:00
|
|
|
State::Notified => return,
|
2018-08-01 15:52:24 -05:00
|
|
|
|
|
|
|
// the future was previously being polled, and we've just
|
|
|
|
// switched it to the "you're notified" state. We don't have
|
|
|
|
// access to the future as it's being polled, so the future
|
|
|
|
// polling process later sees this notification and will
|
|
|
|
// continue polling. For us, though, there's nothing else to do,
|
|
|
|
// so we bail out.
|
|
|
|
// later see
|
2018-10-10 12:41:26 -07:00
|
|
|
State::Polling => return,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Use `Promise.then` on a resolved promise to place our execution
|
|
|
|
// onto the next turn of the microtask queue, enqueueing our poll
|
|
|
|
// operation. We don't currently poll immediately as it turns out
|
|
|
|
// `futures` crate adapters aren't compatible with it and it also
|
|
|
|
// helps avoid blowing the stack by accident.
|
|
|
|
//
|
|
|
|
// Note that the `Rc`/`RefCell` trick here is basically to just
|
|
|
|
// ensure that our `Closure` gets cleaned up appropriately.
|
|
|
|
let promise = Promise::resolve(&JsValue::undefined());
|
|
|
|
let slot = Rc::new(RefCell::new(None));
|
|
|
|
let slot2 = slot.clone();
|
|
|
|
let closure = Closure::wrap(Box::new(move |_| {
|
|
|
|
let myself = slot2.borrow_mut().take();
|
|
|
|
debug_assert!(myself.is_some());
|
|
|
|
Package::poll(&me);
|
|
|
|
}) as Box<FnMut(JsValue)>);
|
|
|
|
promise.then(&closure);
|
|
|
|
*slot.borrow_mut() = Some(closure);
|
2018-08-01 15:52:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-05 14:34:50 +00:00
|
|
|
|
2019-01-05 14:37:39 +00:00
|
|
|
/// Converts a Rust `Future` on a local task queue.
|
|
|
|
///
|
|
|
|
/// The `future` provided must adhere to `'static` because it'll be scheduled
|
|
|
|
/// to run in the background and cannot contain any stack references.
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// This function has the same panic behavior as `future_to_promise`.
|
2019-01-05 14:34:50 +00:00
|
|
|
pub fn spawn_local<F>(future: F)
|
|
|
|
where
|
Migrate `wasm-bindgen` to using `walrus`
This commit moves `wasm-bindgen` the CLI tool from internally using
`parity-wasm` for wasm parsing/serialization to instead use `walrus`.
The `walrus` crate is something we've been working on recently with an
aim to replace the usage of `parity-wasm` in `wasm-bindgen` to make the
current CLI tool more maintainable as well as more future-proof.
The `walrus` crate provides a much nicer AST to work with as well as a
structured `Module`, whereas `parity-wasm` provides a very raw interface
to the wasm module which isn't really appropriate for our use case. The
many transformations and tweaks that wasm-bindgen does have a huge
amount of ad-hoc index management to carefully craft a final wasm
binary, but this is all entirely taken care for us with the `walrus`
crate.
Additionally, `wasm-bindgen` will ingest and rewrite the wasm file,
often changing the binary offsets of functions. Eventually with DWARF
debug information we'll need to be sure to preserve the debug
information throughout the transformations that `wasm-bindgen` does
today. This is practically impossible to do with the `parity-wasm`
architecture, but `walrus` was designed from the get-go to solve this
problem transparently in the `walrus` crate itself. (it doesn't today,
but this is planned work)
It is the intention that this does not end up regressing any
`wasm-bindgen` use cases, neither in functionality or in speed. As a
large change and refactoring, however, it's likely that at least
something will arise! We'll want to continue to remain vigilant to any
issues that come up with this commit.
Note that the `gc` crate has been deleted as part of this change, as the
`gc` crate is no longer necessary since `walrus` does it automatically.
Additionally the `gc` crate was one of the main problems with preserving
debug information as it often deletes wasm items!
Finally, this also starts moving crates to the 2018 edition where
necessary since `walrus` requires the 2018 edition, and in general it's
more pleasant to work within the 2018 edition!
2019-01-31 09:54:23 -08:00
|
|
|
F: Future<Item = (), Error = ()> + 'static,
|
2019-01-05 14:34:50 +00:00
|
|
|
{
|
|
|
|
future_to_promise(
|
|
|
|
future
|
2019-01-07 14:23:10 +00:00
|
|
|
.map(|()| JsValue::undefined())
|
2019-01-12 17:42:05 +01:00
|
|
|
.or_else(|()| future::ok::<JsValue, JsValue>(JsValue::undefined())),
|
2019-01-05 14:34:50 +00:00
|
|
|
);
|
|
|
|
}
|