Skip to content

Commit

Permalink
JsValue module conversions (#2771)
Browse files Browse the repository at this point in the history
* Update `linera-wasmer` to `4.4.0-linera.6`

This version includes `JsValue` conversions for `Module` that include
type hints.

* `linera-execution`: add `JsValue` conversions for `wasm::Module`

* `linera-execution`: preserve JsValue conversions in module traits

* `linera-execution`: document `DynInto`

* `linera-base`: adopt `dyn_convert`

* `linera_base::task`: adopt `linera_execution::Post`

* `linera_execution::wasm`: fix up comments

* `linera-execution`: remove redundant TODO

We actually can't remove this `Send` and `Sync` until we can send the
`ExecutionStateRequest`s via `postMessage`, i.e. issue #2552.  That
issue is far enough divorced from this code that I don't want to link
it here, so let's just remove the TODO.
  • Loading branch information
Twey authored Nov 5, 2024
1 parent 570c77b commit 88743d7
Show file tree
Hide file tree
Showing 10 changed files with 216 additions and 38 deletions.
26 changes: 16 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,14 @@ wasm-bindgen-test = "0.3.42"
wasm-encoder = "0.24.1"
wasm-instrument = "0.4.0"
wasm_thread = "0.3.0"
wasmer = { package = "linera-wasmer", version = "4.4.0-linera.5", default-features = false }
wasmer-compiler-singlepass = { package = "linera-wasmer-compiler-singlepass", version = "4.4.0-linera.5", default-features = false, features = ["std", "unwind", "avx"] }
wasmer = { package = "linera-wasmer", version = "4.4.0-linera.6", default-features = false }
wasmer-compiler-singlepass = { package = "linera-wasmer-compiler-singlepass", version = "4.4.0-linera.6", default-features = false, features = ["std", "unwind", "avx"] }
wasmparser = "0.101.1"
wasmtime = { version = "25.0.0", default-features = false, features = ["cranelift", "runtime", "std"] }
wasmtimer = "0.2.0"
webassembly-test = "0.1.0"
web-sys = "0.3.69"
js-sys = "0.3.70"
web-time = "1.1.0"
wit-bindgen = "0.24.0"
zstd = "0.13.2"
Expand Down
26 changes: 16 additions & 10 deletions examples/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions linera-base/src/dyn_convert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) Zefchain Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

/*!
Object-safe conversion traits.
*/

/// An object-safe version of `std::convert::Into`.
pub trait DynInto<To> {
/// Converts a boxed object into the target type.
fn into_box(self: Box<Self>) -> To;
}

impl<To, From: Into<To>> DynInto<To> for From {
fn into_box(self: Box<From>) -> To {
(*self).into()
}
}
1 change: 1 addition & 0 deletions linera-base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod abi;
pub mod command;
pub mod crypto;
pub mod data_types;
pub mod dyn_convert;
mod graphql;
pub mod identifiers;
mod limited_writer;
Expand Down
17 changes: 17 additions & 0 deletions linera-base/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ use std::future::Future;
mod implementation {
use super::*;

/// Types that can be _explicitly_ sent to a new thread.
/// This differs from `Send` in that we can provide an explicit post step
/// (e.g. `postMessage` on the Web).
pub trait Post: Send + Sync {}

impl<T: Send + Sync> Post for T {}

/// The type of errors that can result from awaiting a task to completion.
pub type Error = tokio::task::JoinError;
/// The type of a future awaiting another task.
Expand All @@ -36,8 +43,18 @@ mod implementation {
#[cfg(web)]
mod implementation {
use futures::channel::oneshot;
use wasm_bindgen_futures::wasm_bindgen::JsValue;

use super::*;
use crate::dyn_convert;

/// Types that can be _explicitly_ sent to a new thread.
/// This differs from `Send` in that we can provide an explicit post step
/// (e.g. `postMessage` on the Web).
// TODO(#2809): this trait is overly liberal.
pub trait Post: dyn_convert::DynInto<JsValue> {}

impl<T: dyn_convert::DynInto<JsValue>> Post for T {}

/// The type of errors that can result from awaiting a task to completion.
pub type Error = oneshot::Canceled;
Expand Down
4 changes: 3 additions & 1 deletion linera-execution/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ metrics = ["prometheus", "linera-views/metrics"]
unstable-oracles = []
wasmer = [
"dep:wasmer",
"wasmer/enable-serde",
"linera-witty/wasmer",
"wasm-encoder",
"wasm-instrument",
Expand All @@ -29,7 +30,7 @@ wasmtime = [
"wasm-encoder",
"wasmparser",
]
web = ["linera-base/web", "linera-views/web"]
web = ["linera-base/web", "linera-views/web", "js-sys"]

[dependencies]
anyhow.workspace = true
Expand All @@ -43,6 +44,7 @@ dashmap.workspace = true
derive_more = { workspace = true, features = ["display"] }
dyn-clone.workspace = true
futures.workspace = true
js-sys = { workspace = true, optional = true }
linera-base.workspace = true
linera-views.workspace = true
linera-views-derive.workspace = true
Expand Down
Loading

0 comments on commit 88743d7

Please sign in to comment.