Skip to content

Commit

Permalink
Support embedding for local modules
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaslihotzki committed Jan 27, 2023
1 parent 79ad0fd commit 62a172a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 16 deletions.
20 changes: 17 additions & 3 deletions crates/cli-support/src/wit/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::decode::{LinkedModule, LocalModule};
use crate::descriptor::{Descriptor, Function};
use crate::descriptors::WasmBindgenDescriptorsSection;
use crate::intrinsic::Intrinsic;
Expand Down Expand Up @@ -345,6 +346,7 @@ impl<'a> Context<'a> {
id: ImportId,
module: &decode::ImportModule,
offset: usize,
local_modules: &[LocalModule],
inline_js: &[&str],
) -> Result<(), Error> {
let descriptor = Function {
Expand All @@ -355,7 +357,13 @@ impl<'a> Context<'a> {
};
let id = self.import_adapter(id, descriptor, AdapterJsImportKind::Normal)?;
let (path, content) = match module {
decode::ImportModule::Named(n) => (format!("snippets/{}", n), None),
decode::ImportModule::Named(n) => (
format!("snippets/{}", n),
local_modules
.iter()
.find(|m| m.identifier == *n)
.map(|m| m.contents),
),
decode::ImportModule::RawNamed(n) => (n.to_string(), None),
decode::ImportModule::Inline(idx) => (
format!(
Expand Down Expand Up @@ -389,7 +397,7 @@ impl<'a> Context<'a> {
linked_modules,
} = program;

for module in local_modules {
for module in &local_modules {
// All local modules we find should be unique, but the same module
// may have showed up in a few different blocks. If that's the case
// all the same identifiers should have the same contents.
Expand All @@ -416,7 +424,13 @@ impl<'a> Context<'a> {
.unwrap_or(0);
for module in linked_modules {
match self.function_imports.remove(module.link_function_name) {
Some((id, _)) => self.link_module(id, &module.module, offset, &inline_js[..])?,
Some((id, _)) => self.link_module(
id,
&module.module,
offset,
&local_modules[..],
&inline_js[..],
)?,
None => (),
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/futures/src/stream.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Converting JavaScript `AsyncIterator`s to Rust `Stream`s.
//!
//! Analogous to the promise to future conversion, this module allows
//! turning objects implementing the async iterator protocol into `Stream`s
//! Analogous to the promise to future convertion, this module allows the
//! turing objects implementing the async iterator protocol into `Stream`s
//! that produce values that can be awaited from.
//!

Expand All @@ -11,7 +11,7 @@ use core::pin::Pin;
use core::task::{Context, Poll};
use futures_core::stream::Stream;
use js_sys::{AsyncIterator, IteratorNext};
use wasm_bindgen::prelude::*;
use wasm_bindgen::{prelude::*, JsCast};

/// A `Stream` that yields values from an underlying `AsyncIterator`.
pub struct JsStream {
Expand Down
1 change: 1 addition & 0 deletions crates/futures/src/task/multithread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::sync::atomic::Ordering::SeqCst;
use std::sync::Arc;
use std::task::{Context, Poll, RawWaker, RawWakerVTable, Waker};
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;

const SLEEPING: i32 = 0;
const AWAKE: i32 = 1;
Expand Down
12 changes: 2 additions & 10 deletions crates/futures/src/task/wait_async_polyfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use js_sys::{Array, Promise};
use std::cell::RefCell;
use std::sync::atomic::AtomicI32;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use web_sys::{MessageEvent, Worker};

thread_local! {
Expand All @@ -52,16 +53,7 @@ fn alloc_helper() -> Worker {
return helper;
}

let worker_url = wasm_bindgen::link_to!(
inline_js = "
onmessage = function (ev) {
let [ia, index, value] = ev.data;
ia = new Int32Array(ia.buffer);
let result = Atomics.wait(ia, index, value);
postMessage(result);
};
"
);
let worker_url = wasm_bindgen::link_to!(module = "/src/task/worker.js");
Worker::new(&worker_url).unwrap_or_else(|js| wasm_bindgen::throw_val(js))
})
}
Expand Down
6 changes: 6 additions & 0 deletions crates/futures/src/task/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
onmessage = function (ev) {
let [ia, index, value] = ev.data;
ia = new Int32Array(ia.buffer);
let result = Atomics.wait(ia, index, value);
postMessage(result);
};

0 comments on commit 62a172a

Please sign in to comment.