Skip to content

Commit

Permalink
Toolchain upgrade (#477)
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Jul 29, 2024
1 parent 304312c commit 9d0de85
Show file tree
Hide file tree
Showing 44 changed files with 588 additions and 405 deletions.
208 changes: 164 additions & 44 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 @@ -39,19 +39,19 @@ base64 = "0.22.1"
heck = "0.5.0"
js-component-bindgen = { path = "./crates/js-component-bindgen" }
structopt = "0.3.26"
wasm-encoder = "0.209.1"
wasm-metadata = "0.209.1"
wasmparser = "0.209.1"
wasmprinter = "0.209.1"
wasmtime-environ = { version = "22.0.0", features = [
wasm-encoder = "0.212.0"
wasm-metadata = "0.212.0"
wasmparser = "0.212.0"
wasmprinter = "0.212.0"
wasmtime-environ = { version = "23.0.1", features = [
"component-model",
"compile",
] }
wat = "1.209.1"
wit-bindgen = "0.26.0"
wit-bindgen-core = "0.26.0"
wit-component = { version = "0.209.1", features = ["dummy-module"] }
wit-parser = "0.209.1"
wat = "1.212.0"
wit-bindgen = "0.27.0"
wit-bindgen-core = "0.27.0"
wit-component = { version = "0.212.0", features = ["dummy-module"] }
wit-parser = "0.212.0"
xshell = "0.2.6"

[dev-dependencies]
Expand Down
30 changes: 8 additions & 22 deletions crates/js-component-bindgen-component/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
use std::path::PathBuf;

use anyhow::{Context, Result};
use js_component_bindgen::{
generate_types,
source::wit_parser::{Resolve, UnresolvedPackage},
transpile,
};
use anyhow::Result;
use js_component_bindgen::{generate_types, source::wit_parser::Resolve, transpile};

/// Calls [`write!`] with the passed arguments and unwraps the result.
///
Expand Down Expand Up @@ -117,34 +113,24 @@ impl Guest for JsComponentBindgenComponent {
opts: TypeGenerationOptions,
) -> Result<Vec<(String, Vec<u8>)>, String> {
let mut resolve = Resolve::default();
let id = match opts.wit {
Wit::Source(source) => {
let pkg = UnresolvedPackage::parse(&PathBuf::from(format!("{name}.wit")), &source)
.map_err(|e| e.to_string())?;
resolve.push(pkg).map_err(|e| e.to_string())?
}
let ids = match opts.wit {
Wit::Source(source) => resolve
.push_str(format!("{name}.wit"), &source)
.map_err(|e| e.to_string())?,
Wit::Path(path) => {
let path = PathBuf::from(path);
if path.is_dir() {
resolve.push_dir(&path).map_err(|e| e.to_string())?.0
} else {
let contents = std::fs::read(&path)
.with_context(|| format!("failed to read file {path:?}"))
.map_err(|e| e.to_string())?;
let text = match std::str::from_utf8(&contents) {
Ok(s) => s,
Err(_) => return Err("input file is not valid utf-8".into()),
};
let pkg = UnresolvedPackage::parse(&path, text).map_err(|e| e.to_string())?;
resolve.push(pkg).map_err(|e| e.to_string())?
resolve.push_file(&path).map_err(|e| e.to_string())?
}
}
Wit::Binary(_) => todo!(),
};

let world_string = opts.world.map(|world| world.to_string());
let world = resolve
.select_world(id, world_string.as_deref())
.select_world(&ids, world_string.as_deref())
.map_err(|e| e.to_string())?;

let opts = js_component_bindgen::TranspileOpts {
Expand Down
7 changes: 3 additions & 4 deletions crates/js-component-bindgen/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ pub enum AugmentedOp {
I64Store,
F32Store,
F64Store,

MemorySize,
}

Expand Down Expand Up @@ -533,7 +532,7 @@ macro_rules! define_visit {
(augment $self:ident I32Store16 $memarg:ident) => {
$self.0.augment_op($memarg.memory, AugmentedOp::I32Store16);
};
(augment $self:ident MemorySize $mem:ident $byte:ident) => {
(augment $self:ident MemorySize $mem:ident) => {
$self.0.augment_op($mem, AugmentedOp::MemorySize);
};

Expand Down Expand Up @@ -672,7 +671,7 @@ macro_rules! define_translate {
(translate $self:ident F64Store $memarg:ident) => {{
$self.augment(AugmentedOp::F64Store, F64Store, $memarg)
}};
(translate $self:ident MemorySize $mem:ident $byte:ident) => {{
(translate $self:ident MemorySize $mem:ident) => {{
if $mem < 1 {
$self.func.instruction(&MemorySize($mem));
} else {
Expand Down Expand Up @@ -707,7 +706,7 @@ macro_rules! define_translate {
CallIndirect { ty: $ty, table: $table }
});
(mk ReturnCallIndirect $ty:ident $table:ident) => (
ReturnCallIndirect { ty: $ty, table: $table }
ReturnCallIndirect { type_index: $ty, table_index: $table }
);
(mk I32Const $v:ident) => (I32Const($v));
(mk I64Const $v:ident) => (I64Const($v));
Expand Down
10 changes: 5 additions & 5 deletions crates/js-component-bindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ use anyhow::Result;
use transpile_bindgen::transpile_bindgen;

use anyhow::{bail, Context};
use wasmtime_environ::component::Export;
use wasmtime_environ::component::{ComponentTypesBuilder, StaticModuleIndex};
use wasmtime_environ::component::{ComponentTypesBuilder, Export, StaticModuleIndex};
use wasmtime_environ::wasmparser::Validator;
use wasmtime_environ::{PrimaryMap, ScopeVec, Tunables};
use wit_component::DecodedWasm;
Expand Down Expand Up @@ -81,7 +80,7 @@ pub fn generate_types(
/// Outputs the file map and import and export metadata for the Transpilation
#[cfg(feature = "transpile-bindgen")]
pub fn transpile(component: &[u8], opts: TranspileOpts) -> Result<Transpiled, anyhow::Error> {
use wasmtime_environ::component::Translator;
use wasmtime_environ::component::{Component, Translator};

let name = opts.name.clone();
let mut files = files::Files::default();
Expand All @@ -96,7 +95,7 @@ pub fn transpile(component: &[u8], opts: TranspileOpts) -> Result<Transpiled, an
.context("failed to extract interface information from component")?;

let (resolve, world_id) = match decoded {
DecodedWasm::WitPackage(..) => bail!("unexpected wit package as input"),
DecodedWasm::WitPackages(_, _) => bail!("unexpected wit package as input"),
DecodedWasm::Component(resolve, world_id) => (resolve, world_id),
};

Expand Down Expand Up @@ -128,7 +127,8 @@ pub fn transpile(component: &[u8], opts: TranspileOpts) -> Result<Transpiled, an
.map(|(_i, module)| core::Translation::new(module, opts.multi_memory))
.collect::<Result<_>>()?;

let types = types.finish(&PrimaryMap::new(), Vec::new(), Vec::new());
let mut wasmtime_component = Component::default();
let types = types.finish(&mut wasmtime_component);

// Insert all core wasm modules into the generated `Files` which will
// end up getting used in the `generate_instantiate` method.
Expand Down
42 changes: 28 additions & 14 deletions crates/js-component-bindgen/src/transpile_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ use std::cell::RefCell;
use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::fmt::Write;
use std::mem;
use wasmparser::collections::IndexMap;
use wasmtime_environ::component::Transcode;
use wasmtime_environ::component::{ExportIndex, NameMap, NameMapNoIntern, Transcode};
use wasmtime_environ::{
component,
component::{
Expand Down Expand Up @@ -192,11 +191,22 @@ pub fn transpile_bindgen(
.iter()
.map(|(export_name, canon_export_name)| {
let export = if canon_export_name.contains(':') {
&instantiator.component.exports[*canon_export_name]
instantiator
.component
.exports
.get(canon_export_name, &NameMapNoIntern)
.unwrap()
} else {
&instantiator.component.exports[&canon_export_name.to_kebab_case()]
instantiator
.component
.exports
.get(&canon_export_name.to_kebab_case(), &NameMapNoIntern)
.unwrap()
};
(export_name.to_string(), export.clone())
(
export_name.to_string(),
instantiator.component.export_items[*export].clone(),
)
})
.collect();

Expand Down Expand Up @@ -502,23 +512,26 @@ impl<'a> Instantiator<'a, '_> {
self.exports_resource_types = self.imports_resource_types.clone();
for (key, item) in &self.resolve.worlds[self.world].exports {
let name = &self.resolve.name_world_key(key);
let (_, export) = self
let (_, export_idx) = self
.component
.exports
.iter()
.raw_iter()
.find(|(expt_name, _)| *expt_name == name)
.unwrap();
let export = &self.component.export_items[*export_idx];
match item {
WorldItem::Interface { id, stability: _ } => {
let iface = &self.resolve.interfaces[*id];
let Export::Instance { exports, .. } = &export else {
unreachable!()
};
for (ty_name, ty) in &iface.types {
match exports.get(ty_name).unwrap() {
match self.component.export_items
[*exports.get(ty_name, &NameMapNoIntern).unwrap()]
{
Export::Type(TypeDef::Resource(resource)) => {
let ty = crate::dealias(self.resolve, *ty);
let resource_idx = self.types[*resource].ty;
let resource_idx = self.types[resource].ty;
self.exports_resource_types.insert(ty, resource_idx);
}
Export::Type(_) => {}
Expand Down Expand Up @@ -1740,8 +1753,9 @@ impl<'a> Instantiator<'a, '_> {
format!("exports{i}{}", maybe_quote_member(name))
}

fn exports(&mut self, exports: &IndexMap<String, Export>) {
for (export_name, export) in exports.iter() {
fn exports(&mut self, exports: &NameMap<String, ExportIndex>) {
for (export_name, export_idx) in exports.raw_iter() {
let export = &self.component.export_items[*export_idx];
let world_key = &self.exports[export_name];
let item = &self.resolve.worlds[self.world].exports[world_key];
let mut resource_map = ResourceMap::new();
Expand Down Expand Up @@ -1802,7 +1816,8 @@ impl<'a> Instantiator<'a, '_> {
WorldItem::Interface { id, stability: _ } => *id,
WorldItem::Function(_) | WorldItem::Type(_) => unreachable!(),
};
for (func_name, export) in exports {
for (func_name, export_idx) in exports.raw_iter() {
let export = &self.component.export_items[*export_idx];
let (def, options, func_ty) = match export {
Export::LiftedFunction { func, options, ty } => (func, options, ty),
Export::Type(_) => continue, // ignored
Expand Down Expand Up @@ -1862,8 +1877,7 @@ impl<'a> Instantiator<'a, '_> {
Export::Type(_) => {}

// This can't be tested at this time so leave it unimplemented
Export::ModuleStatic(_) => unimplemented!(),
Export::ModuleImport { .. } => unimplemented!(),
Export::ModuleStatic { .. } | Export::ModuleImport { .. } => unimplemented!(),
}
}
self.gen.esm_bindgen.populate_export_aliases();
Expand Down
33 changes: 23 additions & 10 deletions crates/js-component-bindgen/src/ts_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,16 +426,27 @@ impl TsBindgen {
let local_name = local_name.to_upper_camel_case();

if !local_exists {
uwriteln!(
self.src,
"import {{ {} }} from './{}.js';",
if camel == local_name {
camel.to_string()
} else {
format!("{camel} as {local_name}")
},
&file_name[0..file_name.len() - 5]
);
// TypeScript doesn't work with empty namespaces, so we don't import in this case,
// just define them as empty.
let is_empty_interface = resolve.interfaces[id].functions.len() == 0
&& resolve.interfaces[id]
.types
.iter()
.all(|(_, ty)| !matches!(resolve.types[*ty].kind, TypeDefKind::Resource));
if is_empty_interface {
uwriteln!(self.src, "declare const {local_name}: {{}};");
} else {
uwriteln!(
self.src,
"import {{ {} }} from './{}.js';",
if camel == local_name {
camel.to_string()
} else {
format!("{camel} as {local_name}")
},
&file_name[0..file_name.len() - 5]
);
}
}

if iface_exists {
Expand All @@ -445,6 +456,7 @@ impl TsBindgen {
let mut gen = self.ts_interface(resolve, false);

uwriteln!(gen.src, "export namespace {camel} {{");

for (_, func) in resolve.interfaces[id].functions.iter() {
gen.ts_func(func, false, true);
}
Expand All @@ -459,6 +471,7 @@ impl TsBindgen {
}
}
}

uwriteln!(gen.src, "}}");

gen.types(id);
Expand Down
18 changes: 9 additions & 9 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, UnresolvedPackage};
use wit_parser::Resolve;

use exports::local::wasm_tools::tools::{
EmbedOpts, Guest, ModuleMetaType, ModuleMetadata, ProducersFields, StringEncoding,
Expand Down Expand Up @@ -58,12 +58,12 @@ impl Guest for WasmToolsJs {
// let world = decode_world("component", &binary);

let doc = match &decoded {
DecodedWasm::WitPackage(_resolve, _pkg) => panic!("Unexpected wit package"),
DecodedWasm::WitPackages(_resolve, _pkg) => panic!("Unexpected wit package"),
DecodedWasm::Component(resolve, world) => resolve.worlds[*world].package.unwrap(),
};

let output = WitPrinter::default()
.print(decoded.resolve(), doc)
.print(decoded.resolve(), &[doc])
.map_err(|e| format!("Unable to print wit\n${:?}", e))?;

Ok(output)
Expand All @@ -74,23 +74,23 @@ impl Guest for WasmToolsJs {

let mut resolve = Resolve::default();

let id = if let Some(wit_source) = &embed_opts.wit_source {
let ids = if let Some(wit_source) = &embed_opts.wit_source {
let path = PathBuf::from("component.wit");
let pkg = UnresolvedPackage::parse(&path, wit_source).map_err(|e| e.to_string())?;
resolve.push(pkg).map_err(|e| e.to_string())?
resolve
.push_str(&path, wit_source)
.map_err(|e| e.to_string())?
} else {
let wit_path = &PathBuf::from(embed_opts.wit_path.as_ref().unwrap());
if metadata(wit_path).unwrap().is_file() {
let pkg = UnresolvedPackage::parse_file(wit_path).map_err(|e| e.to_string())?;
resolve.push(pkg).map_err(|e| e.to_string())?
resolve.push_file(wit_path).map_err(|e| e.to_string())?
} else {
resolve.push_dir(wit_path).map_err(|e| e.to_string())?.0
}
};

let world_string = embed_opts.world.as_ref().map(|world| world.to_string());
let world = resolve
.select_world(id, world_string.as_deref())
.select_world(&ids, world_string.as_deref())
.map_err(|e| e.to_string())?;

let string_encoding = match &embed_opts.string_encoding {
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 9d0de85

Please sign in to comment.