diff --git a/crates/cli/tests/reference/getter-setter.wat b/crates/cli/tests/reference/getter-setter.wat index 7834c0fccf8..47036eab44f 100644 --- a/crates/cli/tests/reference/getter-setter.wat +++ b/crates/cli/tests/reference/getter-setter.wat @@ -11,16 +11,16 @@ (type (;9;) (func (param i32 f64))) (import "./reference_test_bg.js" "__wbindgen_init_externref_table" (func (;0;) (type 0))) (func $__wbindgen_realloc (;1;) (type 8) (param i32 i32 i32 i32) (result i32)) - (func $__wbindgen_malloc (;2;) (type 6) (param i32 i32) (result i32)) - (func $foo_set_weird (;3;) (type 7) (param i32 i32 i32)) - (func $__wbg_set_foo_y (;4;) (type 9) (param i32 f64)) - (func $__wbg_get_foo_y (;5;) (type 4) (param i32) (result f64)) - (func $__wbg_get_foo_x (;6;) (type 3) (param i32) (result i32)) - (func $foo_set_z (;7;) (type 9) (param i32 f64)) - (func $foo_set_lone_setter (;8;) (type 9) (param i32 f64)) - (func $foo_z (;9;) (type 4) (param i32) (result f64)) - (func $foo_lone_getter (;10;) (type 4) (param i32) (result f64)) - (func $__wbg_set_foo_x (;11;) (type 5) (param i32 i32)) + (func $__wbg_set_foo_y (;2;) (type 9) (param i32 f64)) + (func $__wbindgen_malloc (;3;) (type 6) (param i32 i32) (result i32)) + (func $__wbg_get_foo_y (;4;) (type 4) (param i32) (result f64)) + (func $__wbg_get_foo_x (;5;) (type 3) (param i32) (result i32)) + (func $__wbg_set_foo_x (;6;) (type 5) (param i32 i32)) + (func $foo_set_weird (;7;) (type 7) (param i32 i32 i32)) + (func $foo_set_z (;8;) (type 9) (param i32 f64)) + (func $foo_set_lone_setter (;9;) (type 9) (param i32 f64)) + (func $foo_z (;10;) (type 4) (param i32) (result f64)) + (func $foo_lone_getter (;11;) (type 4) (param i32) (result f64)) (func $foo_weird (;12;) (type 3) (param i32) (result i32)) (func $foo_x_static (;13;) (type 1) (result i32)) (func $__wbg_foo_free (;14;) (type 5) (param i32 i32)) diff --git a/crates/cli/tests/reference/import.d.ts b/crates/cli/tests/reference/import.d.ts index 091dbcc51a6..7ab506c7866 100644 --- a/crates/cli/tests/reference/import.d.ts +++ b/crates/cli/tests/reference/import.d.ts @@ -1,3 +1,36 @@ /* tslint:disable */ /* eslint-disable */ export function exported(): void; + +export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module; + +export interface InitOutput { + readonly memory: WebAssembly.Memory; + readonly exported: () => [number, number]; + readonly __wbindgen_exn_store: (a: number) => void; + readonly __externref_table_alloc: () => number; + readonly __wbindgen_export_2: WebAssembly.Table; + readonly __externref_table_dealloc: (a: number) => void; + readonly __wbindgen_start: () => void; +} + +export type SyncInitInput = BufferSource | WebAssembly.Module; +/** +* Instantiates the given `module`, which can either be bytes or +* a precompiled `WebAssembly.Module`. +* +* @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated. +* +* @returns {InitOutput} +*/ +export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput; + +/** +* If `module_or_path` is {RequestInfo} or {URL}, makes a request and +* for everything else, calls `WebAssembly.instantiate` directly. +* +* @param {{ module_or_path: InitInput | Promise }} module_or_path - Passing `InitInput` directly is deprecated. +* +* @returns {Promise} +*/ +export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise } | InitInput | Promise): Promise; diff --git a/crates/cli/tests/reference/import.js b/crates/cli/tests/reference/import.js index 5fd514c7295..b1bc886f595 100644 --- a/crates/cli/tests/reference/import.js +++ b/crates/cli/tests/reference/import.js @@ -1,10 +1,10 @@ import { default as default1 } from 'tests/wasm/import_class.js'; +import * as __wbg_star0 from './snippets/reference-test-ddc0ab9a51c9d25f/inline0.js'; +import * as __wbg_star1 from 'foo-raw'; +import * as __wbg_star2 from 'pure-extern'; +import * as __wbg_star3 from 'tests/wasm/imports.js'; let wasm; -export function __wbg_set_wasm(val) { - wasm = val; -} - function addToExternrefTable0(obj) { const idx = wasm.__externref_table_alloc(); @@ -21,11 +21,9 @@ function handleError(f, args) { } } -const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder; - -let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true }); +const cachedTextDecoder = (typeof TextDecoder !== 'undefined' ? new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }) : { decode: () => { throw Error('TextDecoder not available') } } ); -cachedTextDecoder.decode(); +if (typeof TextDecoder !== 'undefined') { cachedTextDecoder.decode(); }; let cachedUint8ArrayMemory0 = null; @@ -54,62 +52,154 @@ export function exported() { } } -export function __wbg_add_7fbfb2c172506d12(arg0, arg1) { - const ret = add(arg0, arg1); - return ret; -}; - -export function __wbg_barfromfoo_29614885590bfb6f() { - bar_from_foo(); -}; - -export function __wbg_catchme_f7d87ea824a61e87() { return handleError(function () { - catch_me(); -}, arguments) }; - -export function __wbg_get_56ba567010fb9959(arg0) { - const ret = arg0.get(); - return ret; -}; - -export function __wbg_myfunction_8c7b624429f78550() { - b.my_function(); -}; - -export function __wbg_new_d21827b66c7fd25d(arg0) { - const ret = new default1(arg0); - return ret; -}; - -export function __wbg_nocatch_be850a8dddd9599d() { - no_catch(); -}; - -export function __wbg_reload_84c12f152ad689f0() { - window.location.reload(); -}; - -export function __wbg_static_accessor_CONST_9e9d5ae758197645() { - const ret = a.CONST; - return ret; -}; - -export function __wbg_write_c2ce0ce33a6087d5(arg0, arg1) { - window.document.write(getStringFromWasm0(arg0, arg1)); -}; - -export function __wbindgen_init_externref_table() { - const table = wasm.__wbindgen_export_2; - const offset = table.grow(4); - table.set(0, undefined); - table.set(offset + 0, undefined); - table.set(offset + 1, null); - table.set(offset + 2, true); - table.set(offset + 3, false); - ; -}; - -export function __wbindgen_throw(arg0, arg1) { - throw new Error(getStringFromWasm0(arg0, arg1)); -}; +async function __wbg_load(module, imports) { + if (typeof Response === 'function' && module instanceof Response) { + if (typeof WebAssembly.instantiateStreaming === 'function') { + try { + return await WebAssembly.instantiateStreaming(module, imports); + + } catch (e) { + if (module.headers.get('Content-Type') != 'application/wasm') { + console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e); + + } else { + throw e; + } + } + } + + const bytes = await module.arrayBuffer(); + return await WebAssembly.instantiate(bytes, imports); + + } else { + const instance = await WebAssembly.instantiate(module, imports); + + if (instance instanceof WebAssembly.Instance) { + return { instance, module }; + + } else { + return instance; + } + } +} + +function __wbg_get_imports() { + const imports = {}; + imports.wbg = {}; + imports.wbg.__wbg_catchme_f7d87ea824a61e87 = function() { return handleError(function () { + catch_me(); + }, arguments) }; + imports.wbg.__wbg_get_56ba567010fb9959 = function(arg0) { + const ret = arg0.get(); + return ret; + }; + imports.wbg.__wbg_myfunction_8c7b624429f78550 = function() { + b.my_function(); + }; + imports.wbg.__wbg_new_d21827b66c7fd25d = function(arg0) { + const ret = new default1(arg0); + return ret; + }; + imports.wbg.__wbg_nocatch_be850a8dddd9599d = function() { + no_catch(); + }; + imports.wbg.__wbg_reload_84c12f152ad689f0 = function() { + window.location.reload(); + }; + imports.wbg.__wbg_static_accessor_CONST_9e9d5ae758197645 = function() { + const ret = a.CONST; + return ret; + }; + imports.wbg.__wbg_write_c2ce0ce33a6087d5 = function(arg0, arg1) { + window.document.write(getStringFromWasm0(arg0, arg1)); + }; + imports.wbg.__wbindgen_init_externref_table = function() { + const table = wasm.__wbindgen_export_2; + const offset = table.grow(4); + table.set(0, undefined); + table.set(offset + 0, undefined); + table.set(offset + 1, null); + table.set(offset + 2, true); + table.set(offset + 3, false); + ; + }; + imports.wbg.__wbindgen_throw = function(arg0, arg1) { + throw new Error(getStringFromWasm0(arg0, arg1)); + }; + imports['./snippets/reference-test-ddc0ab9a51c9d25f/inline0.js'] = __wbg_star0; + imports['foo-raw'] = __wbg_star1; + imports['pure-extern'] = __wbg_star2; + imports['tests/wasm/imports.js'] = __wbg_star3; + + return imports; +} + +function __wbg_init_memory(imports, memory) { + +} + +function __wbg_finalize_init(instance, module) { + wasm = instance.exports; + __wbg_init.__wbindgen_wasm_module = module; + cachedUint8ArrayMemory0 = null; + + + wasm.__wbindgen_start(); + return wasm; +} + +function initSync(module) { + if (wasm !== undefined) return wasm; + + + if (typeof module !== 'undefined') { + if (Object.getPrototypeOf(module) === Object.prototype) { + ({module} = module) + } else { + console.warn('using deprecated parameters for `initSync()`; pass a single object instead') + } + } + + const imports = __wbg_get_imports(); + + __wbg_init_memory(imports); + + if (!(module instanceof WebAssembly.Module)) { + module = new WebAssembly.Module(module); + } + + const instance = new WebAssembly.Instance(module, imports); + + return __wbg_finalize_init(instance, module); +} + +async function __wbg_init(module_or_path) { + if (wasm !== undefined) return wasm; + + + if (typeof module_or_path !== 'undefined') { + if (Object.getPrototypeOf(module_or_path) === Object.prototype) { + ({module_or_path} = module_or_path) + } else { + console.warn('using deprecated parameters for the initialization function; pass a single object instead') + } + } + + if (typeof module_or_path === 'undefined') { + module_or_path = new URL('reference_test_bg.wasm', import.meta.url); + } + const imports = __wbg_get_imports(); + + if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) { + module_or_path = fetch(module_or_path); + } + + __wbg_init_memory(imports); + + const { instance, module } = await __wbg_load(await module_or_path, imports); + + return __wbg_finalize_init(instance, module); +} +export { initSync }; +export default __wbg_init; diff --git a/crates/cli/tests/reference/import.rs b/crates/cli/tests/reference/import.rs index 1299f5eaf83..f16aa59c89d 100644 --- a/crates/cli/tests/reference/import.rs +++ b/crates/cli/tests/reference/import.rs @@ -1,3 +1,5 @@ +// FLAGS: --target=web + use wasm_bindgen::prelude::*; #[wasm_bindgen] @@ -15,14 +17,29 @@ extern "C" { fn reload(); #[wasm_bindgen(js_namespace = ["window", "document"])] fn write(s: &str); +} - // module import - #[wasm_bindgen(module = "./foo.js")] +// module import +#[wasm_bindgen(module = "tests/wasm/imports.js")] +extern "C" { fn bar_from_foo(); - #[wasm_bindgen(inline_js = "export function add(a,b) { return a + b; }")] +} + +#[wasm_bindgen(raw_module = "foo-raw")] +extern "C" { + fn bar_from_foo_raw(); +} + +#[wasm_bindgen(inline_js = "export function add(a,b) { return a + b; }")] +extern "C" { fn add(a: f64, b: f64) -> f64; } +#[link(wasm_import_module = "pure-extern")] +extern "C" { + fn extern_fn(); +} + #[wasm_bindgen(js_namespace = ["a"])] extern "C" { // test that namespaces are overwritten and not inherited/concatenated @@ -45,6 +62,10 @@ extern "C" { #[wasm_bindgen] pub fn exported() -> Result<(), JsValue> { bar_from_foo(); + bar_from_foo_raw(); + unsafe { + extern_fn(); + } let _ = add(CONST.with(Clone::clone), 2.0); reload(); write(""); diff --git a/crates/cli/tests/reference/import.wat b/crates/cli/tests/reference/import.wat index 506e67cca2b..f36f1e8a613 100644 --- a/crates/cli/tests/reference/import.wat +++ b/crates/cli/tests/reference/import.wat @@ -3,7 +3,7 @@ (type (;1;) (func (result i32))) (type (;2;) (func (result i32 i32))) (type (;3;) (func (param i32))) - (import "./reference_test_bg.js" "__wbindgen_init_externref_table" (func (;0;) (type 0))) + (import "wbg" "__wbindgen_init_externref_table" (func (;0;) (type 0))) (func $__wbindgen_exn_store (;1;) (type 3) (param i32)) (func $__externref_table_dealloc (;2;) (type 3) (param i32)) (func $"exported multivalue shim" (;3;) (type 2) (result i32 i32)) diff --git a/crates/macro-support/src/parser.rs b/crates/macro-support/src/parser.rs index 41e54d7531e..269fad8a875 100644 --- a/crates/macro-support/src/parser.rs +++ b/crates/macro-support/src/parser.rs @@ -160,9 +160,9 @@ macro_rules! attrgen { (method, false, Method(Span)), (static_method_of, false, StaticMethodOf(Span, Ident)), (js_namespace, false, JsNamespace(Span, JsNamespace, Vec)), - (module, false, Module(Span, String, Span)), - (raw_module, false, RawModule(Span, String, Span)), - (inline_js, false, InlineJs(Span, String, Span)), + (module, true, Module(Span, String, Span)), + (raw_module, true, RawModule(Span, String, Span)), + (inline_js, true, InlineJs(Span, String, Span)), (getter, false, Getter(Span, Option)), (setter, false, Setter(Span, Option)), (indexing_getter, false, IndexingGetter(Span)), @@ -2202,7 +2202,7 @@ pub fn check_unused_attrs(tokens: &mut TokenStream) { let unused_attrs = unused_attrs.iter().map(|UnusedState { error, ident }| { if *error { let text = format!("invalid attribute {} in this position", ident); - quote::quote! { ::core::compile_error!(#text); } + quote::quote_spanned! { ident.span() => ::core::compile_error!(#text); } } else { quote::quote! { let #ident: (); } }