Skip to content

Add tests for the interface types output of wasm-bindgen #1898

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 6 additions & 21 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ impl<'a> Context<'a> {
} => {
js.push_str("let wasm;\n");

for (id, js) in sorted_iter(&self.wasm_import_definitions) {
for (id, js) in crate::sorted_iter(&self.wasm_import_definitions) {
let import = self.module.imports.get_mut(*id);
import.module = format!("./{}.js", module_name);
footer.push_str("\nmodule.exports.");
Expand All @@ -254,7 +254,7 @@ impl<'a> Context<'a> {
"import * as wasm from './{}_bg.wasm';\n",
module_name
));
for (id, js) in sorted_iter(&self.wasm_import_definitions) {
for (id, js) in crate::sorted_iter(&self.wasm_import_definitions) {
let import = self.module.imports.get_mut(*id);
import.module = format!("./{}.js", module_name);
footer.push_str("\nexport const ");
Expand Down Expand Up @@ -328,7 +328,7 @@ impl<'a> Context<'a> {
OutputMode::Node {
experimental_modules: false,
} => {
for (module, items) in sorted_iter(&self.js_imports) {
for (module, items) in crate::sorted_iter(&self.js_imports) {
imports.push_str("const { ");
for (i, (item, rename)) in items.iter().enumerate() {
if i > 0 {
Expand All @@ -351,7 +351,7 @@ impl<'a> Context<'a> {
experimental_modules: true,
}
| OutputMode::Web => {
for (module, items) in sorted_iter(&self.js_imports) {
for (module, items) in crate::sorted_iter(&self.js_imports) {
imports.push_str("import { ");
for (i, (item, rename)) in items.iter().enumerate() {
if i > 0 {
Expand Down Expand Up @@ -450,7 +450,7 @@ impl<'a> Context<'a> {
imports_init.push_str(module_name);
imports_init.push_str(" = {};\n");
}
for (id, js) in sorted_iter(&self.wasm_import_definitions) {
for (id, js) in crate::sorted_iter(&self.wasm_import_definitions) {
let import = self.module.imports.get_mut(*id);
import.module = module_name.to_string();
imports_init.push_str("imports.");
Expand Down Expand Up @@ -1852,7 +1852,7 @@ impl<'a> Context<'a> {
}

pub fn generate(&mut self) -> Result<(), Error> {
for (id, adapter) in sorted_iter(&self.wit.adapters) {
for (id, adapter) in crate::sorted_iter(&self.wit.adapters) {
let instrs = match &adapter.kind {
AdapterKind::Import { .. } => continue,
AdapterKind::Local { instructions } => instructions,
Expand Down Expand Up @@ -3055,21 +3055,6 @@ impl ExportedClass {
}
}

/// Returns a sorted iterator over a hash map, sorted based on key.
///
/// The intention of this API is to be used whenever the iteration order of a
/// `HashMap` might affect the generated JS bindings. We want to ensure that the
/// generated output is deterministic and we do so by ensuring that iteration of
/// hash maps is consistently sorted.
fn sorted_iter<K, V>(map: &HashMap<K, V>) -> impl Iterator<Item = (&K, &V)>
where
K: Ord,
{
let mut pairs = map.iter().collect::<Vec<_>>();
pairs.sort_by_key(|(k, _)| *k);
pairs.into_iter()
}

#[test]
fn test_generate_identifier() {
let mut used_names: HashMap<String, usize> = HashMap::new();
Expand Down
Loading