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 da157a5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
20 changes: 17 additions & 3 deletions crates/cli-support/src/wit/mod.rs
@@ -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
11 changes: 1 addition & 10 deletions crates/futures/src/task/wait_async_polyfill.rs
Expand Up @@ -52,16 +52,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
@@ -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 da157a5

Please sign in to comment.