Merge pull request #558 from alexcrichton/wasm2es6js

Simplify wasm2es6js output
This commit is contained in:
Nick Fitzgerald 2018-07-26 13:51:56 -07:00 committed by GitHub
commit 58482b07a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -82,7 +82,15 @@ impl Output {
)); ));
continue; continue;
} }
Internal::Table(_) => continue, Internal::Table(_) => {
exports.push_str(&format!(
"
export const {}: WebAssembly.Table;
",
entry.field()
));
continue;
}
Internal::Global(_) => continue, Internal::Global(_) => continue,
}; };
@ -136,8 +144,8 @@ impl Output {
} }
let mut js_imports = String::new(); let mut js_imports = String::new();
let mut exports = String::new(); let mut exports = String::new();
let mut set_exports = String::new();
let mut imports = String::new(); let mut imports = String::new();
let mut export_mem = false;
if let Some(i) = self.module.import_section() { if let Some(i) = self.module.import_section() {
let mut set = HashSet::new(); let mut set = HashSet::new();
@ -170,73 +178,26 @@ impl Output {
} }
if let Some(i) = self.module.export_section() { if let Some(i) = self.module.export_section() {
let imported_functions = self
.module
.import_section()
.map(|m| m.functions() as u32)
.unwrap_or(0);
for entry in i.entries() { for entry in i.entries() {
let idx = match *entry.internal() { exports.push_str("export let ");
Internal::Function(i) => i - imported_functions, exports.push_str(entry.field());
Internal::Memory(_) => { exports.push_str(";\n");
export_mem = true; set_exports.push_str(entry.field());
continue; set_exports.push_str(" = wasm.exports.");
} set_exports.push_str(entry.field());
Internal::Table(_) => continue, set_exports.push_str(";\n");
Internal::Global(_) => continue,
};
let functions = self
.module
.function_section()
.expect("failed to find function section");
let idx = functions.entries()[idx as usize].type_ref();
let types = self
.module
.type_section()
.expect("failed to find type section");
let ty = match types.types()[idx as usize] {
Type::Function(ref f) => f,
};
let mut args = String::new();
for (i, _) in ty.params().iter().enumerate() {
if i > 0 {
args.push_str(", ");
}
args.push((b'a' + (i as u8)) as char);
}
exports.push_str(&format!(
"
export function {name}({args}) {{
{ret} wasm.exports.{name}({args});
}}
",
name = entry.field(),
args = args,
ret = if ty.return_type().is_some() {
"return"
} else {
""
},
));
} }
} }
let inst = format!( let inst = format!(
" "
WebAssembly.instantiate(bytes,{{ {imports} }}) WebAssembly.instantiate(bytes,{{ {imports} }})
.then(obj => {{ .then(obj => {{
wasm = obj.instance; const wasm = obj.instance;
{memory} {set_exports}
}}) }})
", ",
imports = imports, imports = imports,
memory = if export_mem { set_exports = set_exports,
"memory = wasm.exports.memory;"
} else {
""
},
); );
let (bytes, booted) = if self.base64 { let (bytes, booted) = if self.base64 {
let wasm = serialize(self.module).expect("failed to serialize"); let wasm = serialize(self.module).expect("failed to serialize");
@ -274,9 +235,7 @@ impl Output {
Ok(format!( Ok(format!(
" "
{js_imports} {js_imports}
let wasm;
{bytes} {bytes}
{mem_export}
export const booted = {booted}; export const booted = {booted};
{exports} {exports}
", ",
@ -284,7 +243,6 @@ impl Output {
booted = booted, booted = booted,
js_imports = js_imports, js_imports = js_imports,
exports = exports, exports = exports,
mem_export = if export_mem { "export let memory;" } else { "" },
)) ))
} }