Skip to content

Commit

Permalink
fix: escape special chars when rendering Wasm module (#978)
Browse files Browse the repository at this point in the history
Closes #975
  • Loading branch information
bartlomieju authored Nov 21, 2024
1 parent a5bfca9 commit 2593d5c
Showing 1 changed file with 58 additions and 13 deletions.
71 changes: 58 additions & 13 deletions core/modules/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2035,21 +2035,28 @@ fn render_js_wasm_module(specifier: &str, wasm_deps: WasmDeps) -> String {
// an export can't be found on the module
.map(|(name_index, name)| format!(
"\"{}\" as import_{}_{}",
name, i, name_index
name.escape_default(),
i,
name_index
))
.collect::<Vec<_>>()
.join(", "),
key
key.escape_default(),
));
}

src.push("const importsObject = {".to_string());

for (i, (key, names)) in aggregated_imports.iter().enumerate() {
src.push(format!(" \"{}\": {{", key).to_string());
src.push(format!(" \"{}\": {{", key.escape_default()).to_string());

for (name_index, name) in names.iter().enumerate() {
src.push(format!(" \"{0}\": import_{1}_{2},", name, i, name_index));
src.push(format!(
" \"{0}\": import_{1}_{2},",
name.escape_default(),
i,
name_index
));
}

src.push(" },".to_string());
Expand All @@ -2067,16 +2074,16 @@ fn render_js_wasm_module(specifier: &str, wasm_deps: WasmDeps) -> String {
}

if !wasm_deps.exports.is_empty() {
for export_desc in wasm_deps.exports.iter() {
for (idx, export_desc) in wasm_deps.exports.iter().enumerate() {
if export_desc.name == "default" {
src.push(format!(
"export default modInstance.exports.{};",
export_desc.name
));
} else {
let escaped_name = export_desc.name.escape_default();
src.push(format!(
"export const {} = modInstance.exports.{};",
export_desc.name, export_desc.name
"const export{idx} = modInstance.exports[\"{escaped_name}\"];\nexport {{ export{idx} as \"{escaped_name}\" }};",
));
}
}
Expand Down Expand Up @@ -2195,12 +2202,50 @@ const importsObject = {
},
};
const modInstance = new import.meta.WasmInstance(wasmMod, importsObject);
export const export1 = modInstance.exports.export1;
export const export2 = modInstance.exports.export2;
export const export3 = modInstance.exports.export3;
export const export4 = modInstance.exports.export4;
export const export5 = modInstance.exports.export5;
export const export6 = modInstance.exports.export6;
const export0 = modInstance.exports["export1"];
export { export0 as "export1" };
const export1 = modInstance.exports["export2"];
export { export1 as "export2" };
const export2 = modInstance.exports["export3"];
export { export2 as "export3" };
const export3 = modInstance.exports["export4"];
export { export3 as "export4" };
const export4 = modInstance.exports["export5"];
export { export4 as "export5" };
const export5 = modInstance.exports["export6"];
export { export5 as "export6" };
export default modInstance.exports.default;"#,
);

let deps = WasmDeps {
imports: vec![wasm_dep_analyzer::Import {
name: "\n",
module: "\n",
import_type: wasm_dep_analyzer::ImportType::Function(1),
}],
exports: vec![wasm_dep_analyzer::Export {
name: "\n",
index: 0,
export_type: wasm_dep_analyzer::ExportType::Function(Ok(
wasm_dep_analyzer::FunctionSignature {
params: vec![],
returns: vec![],
},
)),
}],
};
let rendered = render_js_wasm_module("./bar.wasm", deps);
pretty_assertions::assert_eq!(
rendered,
r#"import wasmMod from "./bar.wasm" with { type: "$$deno-core-internal-wasm-module" };
import { "\n" as import_0_0 } from "\n";
const importsObject = {
"\n": {
"\n": import_0_0,
},
};
const modInstance = new import.meta.WasmInstance(wasmMod, importsObject);
const export0 = modInstance.exports["\n"];
export { export0 as "\n" };"#,
);
}

0 comments on commit 2593d5c

Please sign in to comment.