Skip to content

Commit

Permalink
Canonical ABI name mangling (#309)
Browse files Browse the repository at this point in the history
* Rename `canonical_abi_realloc` to `cabi_realloc`.

This follows the current Canonical ABI. This doesn't rename
`canonical_abi_free`, as that's expected to be removed when post-return
functions are implemented.

* Rename the "expected" type to "result".

This follows the current Canonical ABI.

* Implement function and value type name mangling.

This implements the name-mangling scheme in the current canonical ABI,
with the modification proposed in WebAssembly/component-model#104,
though that can be easily removed if the proposal is declined.

* Use name mangling in the bindings generators.

* Use the export base name rather than the mangled name for python identifiers.
  • Loading branch information
sunfishcode authored and guybedford committed Dec 17, 2022
1 parent e12fb49 commit 276d33d
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions crates/gen-host-js/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,12 @@ impl Js {
self.src.ts(" | null");
}
}
TypeDefKind::Expected(e) => {
TypeDefKind::Result(r) => {
self.needs_ty_result = true;
self.src.ts("Result<");
self.print_ty(iface, &e.ok);
self.print_ty(iface, &r.ok);
self.src.ts(", ");
self.print_ty(iface, &e.err);
self.print_ty(iface, &r.err);
self.src.ts(">");
}
TypeDefKind::Variant(_) => panic!("anonymous variant"),
Expand Down Expand Up @@ -512,21 +512,21 @@ impl Generator for Js {
self.src.ts(";\n");
}

fn type_expected(
fn type_result(
&mut self,
iface: &Interface,
_id: TypeId,
name: &str,
expected: &Expected,
result: &Result_,
docs: &Docs,
) {
self.docs(docs);
let name = name.to_camel_case();
self.needs_ty_result = true;
self.src.ts(&format!("export type {name} = Result<"));
self.print_ty(iface, &expected.ok);
self.print_ty(iface, &result.ok);
self.src.ts(", ");
self.print_ty(iface, &expected.err);
self.print_ty(iface, &result.err);
self.src.ts(">;\n");
}

Expand Down Expand Up @@ -663,7 +663,7 @@ impl Generator for Js {
.entry(*resource)
.or_insert(Vec::new()),
};
dst.push((func.name.to_string(), src));
dst.push((iface.mangle_funcname(func), src));
}

// As with `abi_variant` above, we're generating host-side bindings here
Expand Down Expand Up @@ -1856,7 +1856,7 @@ impl Bindgen for FunctionBindgen<'_> {
results.push(format!("variant{tmp}"));
}

Instruction::ExpectedLower {
Instruction::ResultLower {
results: result_types,
..
} => {
Expand Down Expand Up @@ -1891,14 +1891,14 @@ impl Bindgen for FunctionBindgen<'_> {
break;
}}
default: {{
throw new RangeError(\"invalid variant specified for expected\");
throw new RangeError(\"invalid variant specified for result\");
}}
}}
"
));
}

Instruction::ExpectedLift { .. } => {
Instruction::ResultLift { .. } => {
let (err, err_results) = self.blocks.pop().unwrap();
let (ok, ok_results) = self.blocks.pop().unwrap();
let err_result = &err_results[0];
Expand Down Expand Up @@ -2162,13 +2162,14 @@ impl Bindgen for FunctionBindgen<'_> {

Instruction::CallWasm {
iface: _,
name,
base_name: _,
mangled_name,
sig,
} => {
self.bind_results(sig.results.len(), results);
self.src.js(&self.src_object);
self.src.js("._exports['");
self.src.js(&name);
self.src.js(&mangled_name);
self.src.js("'](");
self.src.js(&operands.join(", "));
self.src.js(");\n");
Expand Down

0 comments on commit 276d33d

Please sign in to comment.