Skip to content

Commit

Permalink
Revert "wit-component: update the tool to expect mangled names. (byte…
Browse files Browse the repository at this point in the history
…codealliance#322)"

This reverts commit 6aadac3.

We chose not to use this approach to smuggling component type information
in core modules. See
WebAssembly/component-model#106 (comment)
for more details.
  • Loading branch information
Pat Hickey committed Sep 29, 2022
1 parent 4ddd546 commit d7919c1
Show file tree
Hide file tree
Showing 25 changed files with 206 additions and 227 deletions.
25 changes: 5 additions & 20 deletions crates/wit-component/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1120,18 +1120,14 @@ impl EncodingState {
ExportKind::Func,
&lowering.export_name,
);
exports.push((lowering.mangled_name.as_str(), ExportKind::Func, index));
exports.push((lowering.name, ExportKind::Func, index));
}

for lowering in &import.direct {
let func_index =
self.alias_func(&mut aliases, instance_index as u32, lowering.name);
let core_func_index = self.lower_func(&mut functions, func_index, []);
exports.push((
lowering.mangled_name.as_str(),
ExportKind::Func,
core_func_index,
));
exports.push((lowering.name, ExportKind::Func, core_func_index));
}

self.component.section(&aliases);
Expand Down Expand Up @@ -1178,11 +1174,8 @@ impl EncodingState {
let mut functions = CanonicalFunctionSection::new();
let mut interface_exports = Vec::new();
for func in &export.functions {
let mangled_name = export.mangle_funcname(func);
let name = expected_export_name(
(!is_default).then_some(export.name.as_str()),
&mangled_name,
);
let name =
expected_export_name((!is_default).then(|| export.name.as_str()), &func.name);

let core_func_index = self.alias_core_item(
&mut aliases,
Expand Down Expand Up @@ -1564,13 +1557,11 @@ impl EncodingState {
#[derive(Debug)]
struct DirectLowering<'a> {
name: &'a str,
mangled_name: String,
}

#[derive(Debug)]
struct IndirectLowering<'a> {
name: &'a str,
mangled_name: String,
sig: WasmSignature,
options: RequiredOptions,
export_name: String,
Expand Down Expand Up @@ -1622,8 +1613,6 @@ impl<'a> ImportEncoder<'a> {
RequiredOptions::None
});

let mangled_name = interface.mangle_funcname(f);

match options {
RequiredOptions::All
| RequiredOptions::Realloc
Expand All @@ -1632,18 +1621,14 @@ impl<'a> ImportEncoder<'a> {
self.indirect_count += 1;
indirect.push(IndirectLowering {
name: &f.name,
mangled_name,
sig,
options,
export_name: element_index.to_string(),
});
}
RequiredOptions::None => {
self.direct_count += 1;
direct.push(DirectLowering {
name: &f.name,
mangled_name,
});
direct.push(DirectLowering { name: &f.name });
}
}
}
Expand Down
14 changes: 1 addition & 13 deletions crates/wit-component/src/printing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,7 @@ impl InterfacePrinter {
self.output.push_str(" -> ");
self.print_type_name(interface, &rs[0].1)?;
}
_ => {
self.output.push_str(" -> (");
for (i, (name, ty)) in rs.iter().enumerate() {
if i > 0 {
self.output.push_str(", ");
}

self.output.push_str(name);
self.output.push_str(": ");
self.print_type_name(interface, ty)?;
}
self.output.push(')');
}
_ => todo!("multireturn: wit component printing"),
},
Results::Anon(ty) => {
self.output.push_str(" -> ");
Expand Down
22 changes: 14 additions & 8 deletions crates/wit-component/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,25 @@ fn validate_imported_interface(
imports: &IndexMap<&str, u32>,
types: &Types,
) -> Result<()> {
for f in &interface.functions {
let mangled_name = interface.mangle_funcname(f);
let ty = imports
.get(mangled_name.as_str())
.ok_or_else(|| anyhow!("module does not import required function `{mangled_name}`"))?;
for (func_name, ty) in imports {
let f = interface
.functions
.iter()
.find(|f| f.name == *func_name)
.ok_or_else(|| {
anyhow!(
"import interface `{}` is missing function `{}` that is required by the module",
name,
func_name,
)
})?;

let expected = wasm_sig_to_func_type(interface.wasm_signature(AbiVariant::GuestImport, f));
let ty = types.func_type_at(*ty).unwrap();
if ty != &expected {
bail!(
"type mismatch for function `{}` on imported interface `{}`: expected `{:?} -> {:?}` but found `{:?} -> {:?}`",
f.name,
func_name,
name,
expected.params(),
expected.results(),
Expand All @@ -198,8 +205,7 @@ fn validate_exported_interface(
types: &Types,
) -> Result<()> {
for f in &interface.functions {
let mangled_name = interface.mangle_funcname(f);
let expected_export = expected_export_name(name, &mangled_name);
let expected_export = expected_export_name(name, &f.name);
match exports.get(expected_export.as_ref()) {
Some(func_index) => {
let expected_ty =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
(module
(func (export "a: func(x: string) -> string") unreachable)
(func (export "a") unreachable)
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
(core module (;0;)
(type (;0;) (func (param i32)))
(type (;1;) (func (param i32 i32 i32 i32) (result i32)))
(import "foo" "a: func(b: record { x: u8 }) -> ()" (func (;0;) (type 0)))
(import "foo" "a" (func (;0;) (type 0)))
(func (;1;) (type 1) (param i32 i32 i32 i32) (result i32)
unreachable
)
Expand All @@ -24,20 +24,20 @@
(memory (;0;) 1)
(export "memory" (memory 0))
(export "cabi_realloc" (func 1))
(export "a: func(b: record { x: u8 }) -> ()" (func 2))
(export "a" (func 2))
)
(alias export 0 "a" (func (;0;)))
(core func (;0;) (canon lower (func 0)))
(core instance (;0;)
(export "a: func(b: record { x: u8 }) -> ()" (func 0))
(export "a" (func 0))
)
(core instance (;1;) (instantiate 0
(with "foo" (instance 0))
)
)
(alias core export 1 "memory" (core memory (;0;)))
(alias core export 1 "cabi_realloc" (core func (;1;)))
(alias core export 1 "a: func(b: record { x: u8 }) -> ()" (core func (;2;)))
(alias core export 1 "a" (core func (;2;)))
(func (;1;) (type 2) (canon lift (core func 2)))
(export "a" (func 1))
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(module
(import "foo" "a: func(b: record { x: u8 }) -> ()" (func (param i32)))
(import "foo" "a" (func (param i32)))
(memory (export "memory") 1)
(func (export "cabi_realloc") (param i32 i32 i32 i32) (result i32) unreachable)
(func (export "a: func(b: record { x: u8 }) -> ()") (param i32) unreachable)
(func (export "a") (param i32) unreachable)
)
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
(module
(func (export "foo#a: func(x: string) -> string") unreachable)
(func (export "foo#a") unreachable)
)
28 changes: 14 additions & 14 deletions crates/wit-component/tests/components/exports/component.wat
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,28 @@
(memory (;0;) 1)
(export "memory" (memory 0))
(export "cabi_realloc" (func 0))
(export "a: func() -> ()" (func 1))
(export "b: func(a: s8, b: s16, c: s32, d: s64) -> string" (func 2))
(export "c: func() -> tuple<s8, s16, s32, s64>" (func 3))
(export "foo#a: func() -> ()" (func 4))
(export "foo#b: func(x: string) -> variant { a, b(string), c(s64) }" (func 5))
(export "foo#c: func(x: variant { a, b(string), c(s64) }) -> string" (func 6))
(export "bar#a: func(x: flags { a, b, c }) -> ()" (func 7))
(export "a" (func 1))
(export "b" (func 2))
(export "c" (func 3))
(export "foo#a" (func 4))
(export "foo#b" (func 5))
(export "foo#c" (func 6))
(export "bar#a" (func 7))
)
(core instance (;0;) (instantiate 0))
(alias core export 0 "memory" (core memory (;0;)))
(alias core export 0 "cabi_realloc" (core func (;0;)))
(alias core export 0 "a: func() -> ()" (core func (;1;)))
(alias core export 0 "b: func(a: s8, b: s16, c: s32, d: s64) -> string" (core func (;2;)))
(alias core export 0 "c: func() -> tuple<s8, s16, s32, s64>" (core func (;3;)))
(alias core export 0 "a" (core func (;1;)))
(alias core export 0 "b" (core func (;2;)))
(alias core export 0 "c" (core func (;3;)))
(func (;0;) (type 0) (canon lift (core func 1)))
(func (;1;) (type 1) (canon lift (core func 2) (memory 0) (realloc 0) string-encoding=utf8))
(func (;2;) (type 3) (canon lift (core func 3) (memory 0)))
(alias core export 0 "bar#a: func(x: flags { a, b, c }) -> ()" (core func (;4;)))
(alias core export 0 "bar#a" (core func (;4;)))
(func (;3;) (type 5) (canon lift (core func 4)))
(alias core export 0 "foo#a: func() -> ()" (core func (;5;)))
(alias core export 0 "foo#b: func(x: string) -> variant { a, b(string), c(s64) }" (core func (;6;)))
(alias core export 0 "foo#c: func(x: variant { a, b(string), c(s64) }) -> string" (core func (;7;)))
(alias core export 0 "foo#a" (core func (;5;)))
(alias core export 0 "foo#b" (core func (;6;)))
(alias core export 0 "foo#c" (core func (;7;)))
(func (;4;) (type 0) (canon lift (core func 5)))
(func (;5;) (type 7) (canon lift (core func 6) (memory 0) (realloc 0) string-encoding=utf8))
(func (;6;) (type 8) (canon lift (core func 7) (memory 0) (realloc 0) string-encoding=utf8))
Expand Down
14 changes: 7 additions & 7 deletions crates/wit-component/tests/components/exports/module.wat
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
(module
(memory (export "memory") 1)
(func (export "cabi_realloc") (param i32 i32 i32 i32) (result i32) unreachable)
(func (export "a: func() -> ()") unreachable)
(func (export "b: func(a: s8, b: s16, c: s32, d: s64) -> string") (param i32 i32 i32 i64) (result i32) unreachable)
(func (export "c: func() -> tuple<s8, s16, s32, s64>") (result i32) unreachable)
(func (export "foo#a: func() -> ()") unreachable)
(func (export "foo#b: func(x: string) -> variant { a, b(string), c(s64) }") (param i32 i32) (result i32) unreachable)
(func (export "foo#c: func(x: variant { a, b(string), c(s64) }) -> string") (param i32 i64 i32) (result i32) unreachable)
(func (export "bar#a: func(x: flags { a, b, c }) -> ()") (param i32) unreachable)
(func (export "a") unreachable)
(func (export "b") (param i32 i32 i32 i64) (result i32) unreachable)
(func (export "c") (result i32) unreachable)
(func (export "foo#a") unreachable)
(func (export "foo#b") (param i32 i32) (result i32) unreachable)
(func (export "foo#c") (param i32 i64 i32) (result i32) unreachable)
(func (export "bar#a") (param i32) unreachable)
)
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
(type (;1;) (func (param i64 i32 i32)))
(type (;2;) (func (param i32 i32 i32)))
(type (;3;) (func (param i32 i32 i32 i32) (result i32)))
(import "foo" "a: func() -> ()" (func (;0;) (type 0)))
(import "bar" "a: func(x: u64, y: string) -> ()" (func (;1;) (type 1)))
(import "baz" "baz: func(x: list<u8>) -> list<u8>" (func (;2;) (type 2)))
(import "foo" "a" (func (;0;) (type 0)))
(import "bar" "a" (func (;1;) (type 1)))
(import "baz" "baz" (func (;2;) (type 2)))
(func (;3;) (type 3) (param i32 i32 i32 i32) (result i32)
unreachable
)
Expand Down Expand Up @@ -75,13 +75,13 @@
(alias export 2 "a" (func (;0;)))
(core func (;2;) (canon lower (func 0)))
(core instance (;1;)
(export "a: func(x: u64, y: string) -> ()" (func 0))
(export "a" (func 0))
)
(core instance (;2;)
(export "baz: func(x: list<u8>) -> list<u8>" (func 1))
(export "baz" (func 1))
)
(core instance (;3;)
(export "a: func() -> ()" (func 2))
(export "a" (func 2))
)
(core instance (;4;) (instantiate 0
(with "bar" (instance 1))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(module
(import "foo" "a: func() -> ()" (func))
(import "bar" "a: func(x: u64, y: string) -> ()" (func (param i64 i32 i32)))
(import "baz" "baz: func(x: list<u8>) -> list<u8>" (func (param i32 i32 i32)))
(import "foo" "a" (func))
(import "bar" "a" (func (param i64 i32 i32)))
(import "baz" "baz" (func (param i32 i32 i32)))
(memory (export "memory") 1)
(func (export "cabi_realloc") (param i32 i32 i32 i32) (result i32) unreachable)
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
(type (;2;) (func (param i32 i32) (result i32)))
(type (;3;) (func))
(type (;4;) (func (result i32)))
(import "foo" "a: func() -> string" (func (;0;) (type 0)))
(import "foo" "a" (func (;0;) (type 0)))
(func (;1;) (type 1) (param i32 i32 i32 i32) (result i32)
unreachable
)
Expand All @@ -32,9 +32,9 @@
(memory (;0;) 1)
(export "memory" (memory 0))
(export "cabi_realloc" (func 1))
(export "a: func(x: string) -> tuple<string, u32, string>" (func 2))
(export "bar#a: func() -> ()" (func 3))
(export "bar#b: func() -> string" (func 4))
(export "a" (func 2))
(export "bar#a" (func 3))
(export "bar#b" (func 4))
)
(core module (;1;)
(type (;0;) (func (param i32)))
Expand All @@ -56,7 +56,7 @@
(core instance (;0;) (instantiate 1))
(alias core export 0 "0" (core func (;0;)))
(core instance (;1;)
(export "a: func() -> string" (func 0))
(export "a" (func 0))
)
(core instance (;2;) (instantiate 0
(with "foo" (instance 1))
Expand All @@ -75,10 +75,10 @@
(with "" (instance 3))
)
)
(alias core export 2 "a: func(x: string) -> tuple<string, u32, string>" (core func (;3;)))
(alias core export 2 "a" (core func (;3;)))
(func (;1;) (type 1) (canon lift (core func 3) (memory 0) (realloc 1) string-encoding=utf8))
(alias core export 2 "bar#a: func() -> ()" (core func (;4;)))
(alias core export 2 "bar#b: func() -> string" (core func (;5;)))
(alias core export 2 "bar#a" (core func (;4;)))
(alias core export 2 "bar#b" (core func (;5;)))
(func (;2;) (type 2) (canon lift (core func 4)))
(func (;3;) (type 3) (canon lift (core func 5) (memory 0) (realloc 1) string-encoding=utf8))
(instance (;1;)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
(module
(import "foo" "a: func() -> string" (func (param i32)))
(import "foo" "a" (func (param i32)))
(memory (export "memory") 1)
(func (export "cabi_realloc") (param i32 i32 i32 i32) (result i32) unreachable)
(func (export "a: func(x: string) -> tuple<string, u32, string>") (param i32 i32) (result i32) unreachable)
(func (export "bar#a: func() -> ()") unreachable)
(func (export "bar#b: func() -> string") (result i32) unreachable)
(func (export "a") (param i32 i32) (result i32) unreachable)
(func (export "bar#a") unreachable)
(func (export "bar#b") (result i32) unreachable)
)
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
(module
(import "foo" "bar: func(s: string) -> ()" (func))
(import "foo" "bar" (func))
)
Loading

0 comments on commit d7919c1

Please sign in to comment.