Skip to content

Commit

Permalink
Merge branch 'main' into feat=allow-custom-weval-bin
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford authored Nov 25, 2024
2 parents 98cb7b9 + 7b8adf1 commit 08b5efe
Show file tree
Hide file tree
Showing 58 changed files with 415 additions and 300 deletions.
190 changes: 98 additions & 92 deletions Cargo.lock

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ resolver = "2"

[workspace.package]
edition = "2021"
version = "1.7.2"
version = "1.8.1"

[profile.release]
codegen-units = 1
Expand All @@ -41,19 +41,19 @@ log = "0.4.22"
semver = "1.0.23"
js-component-bindgen = { path = "./crates/js-component-bindgen" }
structopt = "0.3.26"
wasm-encoder = "0.218.0"
wasm-metadata = "0.218.0"
wasmparser = "0.218.0"
wasmprinter = "0.218.0"
wasmtime-environ = { version = "26.0.0", features = [
wasm-encoder = "0.219.1"
wasm-metadata = "0.219.1"
wasmparser = "0.219.1"
wasmprinter = "0.219.1"
wasmtime-environ = { version = "27.0.0", features = [
"component-model",
"compile",
] }
wat = "1.218.0"
wit-bindgen = "0.33.0"
wit-bindgen-core = "0.33.0"
wit-component = { version = "0.218.0", features = ["dummy-module"] }
wit-parser = "0.218.0"
wit-bindgen = "0.34.0"
wit-bindgen-core = "0.34.0"
wit-component = { version = "0.219.1", features = ["dummy-module"] }
wit-parser = "0.219.1"
xshell = "0.2.6"

[dev-dependencies]
Expand Down
12 changes: 2 additions & 10 deletions crates/js-component-bindgen/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,6 @@ impl Augmenter<'_> {
// records various bits of information about the module within `self`.
for payload in Parser::new(0).parse_all(self.translation.wasm) {
match payload? {
Payload::Version { .. } => {}
Payload::End(_) => {}

Payload::TypeSection(s) => {
for grp in s.into_iter_err_on_gc_types() {
self.types.push(grp?);
Expand All @@ -246,29 +243,22 @@ impl Augmenter<'_> {
self.imports.push(i);
}
}

Payload::ExportSection(s) => {
for e in s {
let e = e?;
self.exports.push(e);
}
}

Payload::FunctionSection(s) => {
for ty in s {
let ty = ty?;
self.local_func_tys.push(ty);
}
}

Payload::CodeSectionStart { .. } => {}
Payload::CodeSectionEntry(body) => {
self.local_funcs.push(body);
}

// Ignore all custom sections for now
Payload::CustomSection(_) => {}

// NB: these sections are theoretically possible to handle but
// are not required at this time.
Payload::DataCountSection { .. }
Expand Down Expand Up @@ -297,6 +287,8 @@ impl Augmenter<'_> {
| Payload::ComponentTypeSection(_) => {
bail!("component section found in module using multiple memories")
}

_ => {}
}
}

Expand Down
12 changes: 8 additions & 4 deletions crates/js-component-bindgen/src/esm_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,14 @@ impl EsmBindgen {
}
iface = match iface.get_mut(&path[i]).unwrap() {
ImportBinding::Interface(iface) => iface,
ImportBinding::Local(_) => panic!(
"Imported interface {} cannot be both a function and an interface",
&path[0..i].join(".")
),
ImportBinding::Local(local) => {
panic!(
"Internal bindgen error: Import '{}' cannot be both an interface '{}' and a function '{}'",
&path[0..i + 1].join("."),
&path[i + 1..].join("."),
&local[0],
);
}
};
}
if let Some(ref mut existing) = iface.get_mut(&path[path.len() - 1]) {
Expand Down
8 changes: 4 additions & 4 deletions crates/js-component-bindgen/src/function_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1131,7 +1131,7 @@ impl Bindgen for FunctionBindgen<'_> {
uwriteln!(
self.src,
"for (const rsc of {cur_resource_borrows}) {{
rsc[{symbol_resource_handle}] = null;
rsc[{symbol_resource_handle}] = undefined;
}}
{cur_resource_borrows} = [];"
);
Expand All @@ -1141,7 +1141,7 @@ impl Bindgen for FunctionBindgen<'_> {
"for (const {{ rsc, drop }} of {cur_resource_borrows}) {{
if (rsc[{symbol_resource_handle}]) {{
drop(rsc[{symbol_resource_handle}]);
delete rsc[{symbol_resource_handle}];
rsc[{symbol_resource_handle}] = undefined;
}}
}}
{cur_resource_borrows} = [];"
Expand Down Expand Up @@ -1263,7 +1263,7 @@ impl Bindgen for FunctionBindgen<'_> {
finalizationRegistry{tid}.unregister({rsc});
{rsc_table_remove}(handleTable{tid}, {handle});
{rsc}[{symbol_dispose}] = {empty_func};
{rsc}[{symbol_resource_handle}] = null;
{rsc}[{symbol_resource_handle}] = undefined;
{dtor}(handleTable{tid}[({handle} << 1) + 1] & ~{rsc_flag});
}}}});"
);
Expand Down Expand Up @@ -1396,7 +1396,7 @@ impl Bindgen for FunctionBindgen<'_> {
}}
finalizationRegistry{tid}.unregister({op});
{op}[{symbol_dispose}] = {empty_func};
{op}[{symbol_resource_handle}] = null;",
{op}[{symbol_resource_handle}] = undefined;",
);
} else {
// When expecting a borrow, the JS resource provided will always be an own
Expand Down
53 changes: 43 additions & 10 deletions crates/js-component-bindgen/src/transpile_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1076,9 +1076,6 @@ impl<'a> Instantiator<'a, '_> {
let (import_name, _) = &self.component.import_types[*import_index];
let world_key = &self.imports[import_name];

// nested interfaces only currently possible through mapping
let (import_specifier, maybe_iface_member) = map_import(&self.gen.opts.map, import_name);

let (func, func_name, iface_name) =
match &self.resolve.worlds[self.world].imports[world_key] {
WorldItem::Function(func) => {
Expand All @@ -1098,6 +1095,29 @@ impl<'a> Instantiator<'a, '_> {
WorldItem::Type(_) => unreachable!(),
};

// nested interfaces only currently possible through mapping
let (import_specifier, maybe_iface_member) = map_import(
&self.gen.opts.map,
if iface_name.is_some() {
import_name
} else {
match func.kind {
FunctionKind::Method(_) => {
let stripped = import_name.strip_prefix("[method]").unwrap();
&stripped[0..stripped.find(".").unwrap()]
}
FunctionKind::Static(_) => {
let stripped = import_name.strip_prefix("[static]").unwrap();
&stripped[0..stripped.find(".").unwrap()]
}
FunctionKind::Constructor(_) => {
import_name.strip_prefix("[constructor]").unwrap()
}
FunctionKind::Freestanding => import_name,
}
},
);

let mut resource_map = ResourceMap::new();
self.create_resource_fn_map(func, func_ty, &mut resource_map);

Expand Down Expand Up @@ -1323,6 +1343,10 @@ impl<'a> Instantiator<'a, '_> {
local_name,
);
}
} else if let Some(iface_member) = iface_member {
self.gen
.esm_bindgen
.add_import_binding(&[import_specifier, iface_member.into()], local_name);
} else if let Some(import_binding) = import_binding {
self.gen
.esm_bindgen
Expand Down Expand Up @@ -1385,19 +1409,24 @@ impl<'a> Instantiator<'a, '_> {
wit_parser::TypeOwner::Interface(iface) => {
match &self.resolve.interfaces[iface].name {
Some(name) => (WorldKey::Interface(iface), Some(name.as_str())),
None => (
self.resolve.worlds[self.world]
None => {
let key = self.resolve.worlds[self.world]
.imports
.iter()
.find(|&(_, item)| match item {
WorldItem::Interface { id, .. } => *id == iface,
_ => false,
})
.unwrap()
.0
.clone(),
None,
),
.0;
(
key.clone(),
match key {
WorldKey::Name(ref name) => Some(name.as_str()),
WorldKey::Interface(_) => None,
},
)
}
}
}
wit_parser::TypeOwner::None => unimplemented!(),
Expand All @@ -1416,7 +1445,11 @@ impl<'a> Instantiator<'a, '_> {
import_specifier,
iface_name,
maybe_iface_member.as_deref(),
Some(resource_name),
if iface_name.is_some() {
Some(resource_name)
} else {
None
},
local_name_str.to_string(),
);

Expand Down
2 changes: 1 addition & 1 deletion crates/js-component-bindgen/src/ts_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ pub fn ts_bindgen(
TypeDefKind::Future(_) => todo!("generate for future"),
TypeDefKind::Stream(_) => todo!("generate for stream"),
TypeDefKind::Unknown => unreachable!(),
TypeDefKind::Resource => todo!(),
TypeDefKind::Resource => {}
TypeDefKind::Handle(_) => todo!(),
}
let output = gen.finish();
Expand Down
4 changes: 2 additions & 2 deletions crates/wasm-tools-component/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::path::PathBuf;
use wasm_encoder::{Encode, Section};
use wasm_metadata::Producers;
use wit_component::{ComponentEncoder, DecodedWasm, WitPrinter};
use wit_parser::Resolve;
use wit_parser::{Mangling, Resolve};

use exports::local::wasm_tools::tools::{
EmbedOpts, EnabledFeatureSet, Guest, ModuleMetaType, ModuleMetadata, ProducersFields,
Expand Down Expand Up @@ -122,7 +122,7 @@ impl Guest for WasmToolsJs {
..
}
) {
wit_component::dummy_module(&resolve, world)
wit_component::dummy_module(&resolve, world, Mangling::Standard32)
} else {
if binary.is_none() {
return Err(
Expand Down
10 changes: 9 additions & 1 deletion examples/components/string-reverse-upper/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ world revup {

## Dependencies

First, install required dependencies:
First, install required project-level dependencies:

```console
npm install
Expand All @@ -51,6 +51,11 @@ npm install
> [!NOTE]
> As this is a regular NodeJS project, you can use your package manager of choice (e.g. `yarn`, `pnpm`)
Along with projet related dependencies, since this is a *compound* component (this component is composed with another),
we'll need to install the [WebAssembly Composition tool (`wac`)][wac], which we will use later to compose components.

[wac]: https://github.com/bytecodealliance/wac

## Building the WebAssembly binary (pre-composition)

Then, build the component with `jco`:
Expand Down Expand Up @@ -97,6 +102,9 @@ The component we will be composing with our as-of-yet incomplete binary *must* s
npm run compose
```

> [!WARN]
> This step is the one that uses [`wac`][wac] -- ensure it's installed!
Running the command above will produce a `string-reverse-upper.wasm` file (this time, that is *complete*).

After running component composition, there will be a component with all its imports satisfied, called `string-reverse-upper.composed.wasm`.
Expand Down
Binary file modified lib/wasi_snapshot_preview1.command.wasm
Binary file not shown.
Binary file modified lib/wasi_snapshot_preview1.reactor.wasm
Binary file not shown.
Loading

0 comments on commit 08b5efe

Please sign in to comment.