Preserve argument names

This commit is contained in:
Caio
2019-03-14 08:46:42 -03:00
parent 90c3196b7b
commit 70f5373348
10 changed files with 97 additions and 14 deletions

View File

@ -46,6 +46,15 @@ impl<'src> Decode<'src> for &'src str {
}
}
impl<'src> Decode<'src> for String {
fn decode(data: &mut &'src [u8]) -> String {
let n = u32::decode(data);
let (a, b) = data.split_at(n as usize);
*data = b;
String::from_utf8(a.to_vec()).unwrap()
}
}
impl<'src, T: Decode<'src>> Decode<'src> for Vec<T> {
fn decode(data: &mut &'src [u8]) -> Self {
let n = u32::decode(data);

View File

@ -193,7 +193,7 @@ impl ClosureDescriptors {
builder.rust_argument("this.a").rust_argument("b");
}
builder.finally("if (this.cnt-- == 1) d(this.a, b);");
builder.process(&closure.function)?.finish(
builder.process(&closure.function, None)?.finish(
"function",
"f",
ExportedShim::TableElement(&mut shim),

View File

@ -75,9 +75,22 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
/// Generates all bindings necessary for the signature in `Function`,
/// creating necessary argument conversions and return value processing.
pub fn process(&mut self, function: &Function) -> Result<&mut Self, Error> {
for arg in function.arguments.iter() {
self.argument(arg)?;
pub fn process<'c, I>(
&mut self,
function: &Function,
opt_arg_names: I,
) -> Result<&mut Self, Error>
where
I: Into<Option<&'c Vec<String>>>,
{
if let Some(arg_names) = opt_arg_names.into() {
for (arg, arg_name) in function.arguments.iter().zip(arg_names) {
self.argument(arg, arg_name.clone())?;
}
} else {
for arg in function.arguments.iter() {
self.argument(arg, None)?;
}
}
self.ret(&function.ret)?;
Ok(self)
@ -138,15 +151,22 @@ impl<'a, 'b> Js2Rust<'a, 'b> {
self
}
fn abi_arg(&mut self) -> String {
let s = format!("arg{}", self.arg_idx);
fn abi_arg(&mut self, opt_arg_name: Option<String>) -> String {
let ret = if let Some(x) = opt_arg_name {
x
} else {
format!("arg{}", self.arg_idx)
};
self.arg_idx += 1;
s
ret
}
pub fn argument(&mut self, arg: &Descriptor) -> Result<&mut Self, Error> {
pub fn argument<I>(&mut self, arg: &Descriptor, opt_arg_name: I) -> Result<&mut Self, Error>
where
I: Into<Option<String>>,
{
let i = self.arg_idx;
let name = self.abi_arg();
let name = self.abi_arg(opt_arg_name.into());
let (arg, optional) = match arg {
Descriptor::Option(t) => (&**t, true),

View File

@ -2498,7 +2498,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
}
let (js, ts, js_doc) = Js2Rust::new(&export.function.name, self.cx)
.process(descriptor.unwrap_function())?
.process(descriptor.unwrap_function(), &export.function.arg_names)?
.finish(
"function",
&format!("wasm.{}", export.function.name),
@ -2554,7 +2554,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
} else {
None
})
.process(descriptor.unwrap_function())?
.process(descriptor.unwrap_function(), &export.function.arg_names)?
.finish(
"",
&format!("wasm.{}", wasm_name),
@ -2772,7 +2772,7 @@ impl<'a, 'b> SubContext<'a, 'b> {
let setter = ExportedShim::Named(&wasm_setter);
let mut cx = Js2Rust::new(&field.name, self.cx);
cx.method(true, false)
.argument(&descriptor)?
.argument(&descriptor, None)?
.ret(&Descriptor::Unit)?;
ts_dst.push_str(&format!(
"\n {}{}: {};",

View File

@ -285,7 +285,7 @@ impl<'a, 'b> Rust2Js<'a, 'b> {
} else {
builder.rust_argument("this.a");
}
builder.rust_argument("this.b").process(f)?.finish(
builder.rust_argument("this.b").process(f, None)?.finish(
"function",
"this.f",
ExportedShim::TableElement(&mut shim),