Work around for building with webpack5 (#2512)

* Use  instead of

* rewrite js files in wasm-bindgen-cli reference tests

* fix formatting

* fix typo

* ensure function return

* run cargo fmt
This commit is contained in:
かめのこにょこにょこ 2021-04-19 23:20:59 +09:00 committed by GitHub
parent 0d911ead9c
commit fda6bb9f24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 78 additions and 79 deletions

View File

@ -419,11 +419,19 @@ impl<'a> Context<'a> {
for (id, js) in crate::sorted_iter(&self.wasm_import_definitions) {
let import = self.module.imports.get_mut(*id);
import.module = format!("./{}_bg.js", module_name);
footer.push_str("\nexport const ");
footer.push_str(&import.name);
footer.push_str(" = ");
footer.push_str(js.trim());
footer.push_str(";\n");
if js.starts_with("function") {
let body = &js[8..];
footer.push_str("\nexport function ");
footer.push_str(&import.name);
footer.push_str(body.trim());
footer.push_str(";\n");
} else {
footer.push_str("\nexport const ");
footer.push_str(&import.name);
footer.push_str(" = ");
footer.push_str(js.trim());
footer.push_str(";\n");
}
}
if needs_manual_start {
start = Some("\nwasm.__wbindgen_start();\n".to_string());
@ -1738,17 +1746,14 @@ impl<'a> Context<'a> {
(Some(table), Some(alloc)) => {
let add = self.expose_add_to_externref_table(table, alloc)?;
self.global(&format!(
"
function handleError(f) {{
return function () {{
try {{
return f.apply(this, arguments);
}} catch (e) {{
const idx = {}(e);
wasm.{}(idx);
}}
}};
"\
function handleError(f, args) {{
try {{
return f.apply(this, args);
}} catch (e) {{
const idx = {}(e);
wasm.{}(idx);
}}
}}
",
add, store,
@ -1757,16 +1762,13 @@ impl<'a> Context<'a> {
_ => {
self.expose_add_heap_object();
self.global(&format!(
"
function handleError(f) {{
return function () {{
try {{
return f.apply(this, arguments);
}} catch (e) {{
wasm.{}(addHeapObject(e));
}}
}};
"\
function handleError(f, args) {{
try {{
return f.apply(this, args);
}} catch (e) {{
wasm.{}(addHeapObject(e));
}}
}}
",
store,
@ -1782,27 +1784,24 @@ impl<'a> Context<'a> {
}
self.global(
"\
function logError(f) {
return function () {
try {
return f.apply(this, arguments);
} catch (e) {
let error = (function () {
try {
return e instanceof Error \
? `${e.message}\\n\\nStack:\\n${e.stack}` \
: e.toString();
} catch(_) {
return \"<failed to stringify thrown value>\";
}
}());
console.error(\"wasm-bindgen: imported JS function that \
was not marked as `catch` threw an error:\", \
error);
throw e;
}
};
function logError(f, args) {
try {
return f.apply(this, args);
} catch (e) {
let error = (function () {
try {
return e instanceof Error \
? `${e.message}\\n\\nStack:\\n${e.stack}` \
: e.toString();
} catch(_) {
return \"<failed to stringify thrown value>\";
}
}());
console.error(\"wasm-bindgen: imported JS function that \
was not marked as `catch` threw an error:\", \
error);
throw e;
}
}
",
);
@ -2404,9 +2403,15 @@ impl<'a> Context<'a> {
}
Kind::Import(core) => {
let code = if catch {
format!("handleError(function{})", code)
format!(
"function() {{ return handleError(function {}, arguments) }}",
code
)
} else if log_error {
format!("logError(function{})", code)
format!(
"function() {{ return logError(function {}, arguments) }}",
code
)
} else {
format!("function{}", code)
};

View File

@ -1,6 +1,6 @@
import * as wasm from './reference_test_bg.wasm';
export const __wbindgen_init_externref_table = function() {
export function __wbindgen_init_externref_table() {
const table = wasm.__wbindgen_export_0;
const offset = table.grow(4);
table.set(0, undefined);

View File

@ -24,16 +24,13 @@ function addToExternrefTable0(obj) {
return idx;
}
function handleError(f) {
return function () {
try {
return f.apply(this, arguments);
} catch (e) {
const idx = addToExternrefTable0(e);
wasm.__wbindgen_exn_store(idx);
}
};
function handleError(f, args) {
try {
return f.apply(this, args);
} catch (e) {
const idx = addToExternrefTable0(e);
wasm.__wbindgen_exn_store(idx);
}
}
/**
*/
@ -41,19 +38,19 @@ export function exported() {
wasm.exported();
}
export const __wbg_foo_8d66ddef0ff279d6 = handleError(function() {
export function __wbg_foo_8d66ddef0ff279d6() { return handleError(function () {
foo();
});
}, arguments) };
export const __wbindgen_throw = function(arg0, arg1) {
export function __wbindgen_throw(arg0, arg1) {
throw new Error(getStringFromWasm0(arg0, arg1));
};
export const __wbindgen_rethrow = function(arg0) {
export function __wbindgen_rethrow(arg0) {
throw arg0;
};
export const __wbindgen_init_externref_table = function() {
export function __wbindgen_init_externref_table() {
const table = wasm.__wbindgen_export_0;
const offset = table.grow(4);
table.set(0, undefined);

View File

@ -6,7 +6,7 @@ export function foo() {
wasm.foo();
}
export const __wbindgen_init_externref_table = function() {
export function __wbindgen_init_externref_table() {
const table = wasm.__wbindgen_export_0;
const offset = table.grow(4);
table.set(0, undefined);

View File

@ -29,15 +29,12 @@ function addHeapObject(obj) {
return idx;
}
function handleError(f) {
return function () {
try {
return f.apply(this, arguments);
} catch (e) {
wasm.__wbindgen_exn_store(addHeapObject(e));
}
};
function handleError(f, args) {
try {
return f.apply(this, args);
} catch (e) {
wasm.__wbindgen_exn_store(addHeapObject(e));
}
}
/**
*/
@ -45,11 +42,11 @@ export function exported() {
wasm.exported();
}
export const __wbg_foo_8d66ddef0ff279d6 = handleError(function() {
export function __wbg_foo_8d66ddef0ff279d6() { return handleError(function () {
foo();
});
}, arguments) };
export const __wbindgen_rethrow = function(arg0) {
export function __wbindgen_rethrow(arg0) {
throw takeObject(arg0);
};

View File

@ -83,7 +83,7 @@ export function foo(a) {
wasm.foo(ptr0, len0);
}
export const __wbindgen_throw = function(arg0, arg1) {
export function __wbindgen_throw(arg0, arg1) {
throw new Error(getStringFromWasm0(arg0, arg1));
};