@@ -27,77 +27,69 @@ <h2>WebAssembly Greeting Demo</h2>
2727</ section >
2828
2929< script type ="module ">
30- const wasmUrl = '/ wasm/hello.wasm' ;
30+ const wasmPath = 'wasm/hello.wasm' ; // relative (works on GitHub Pages subpaths)
3131const loadButton = document . getElementById ( 'load-wasm' ) ;
3232const output = document . getElementById ( 'wasm-result' ) ;
33- const utf8Decoder = new TextDecoder ( 'utf-8' ) ;
33+ const utf8 = new TextDecoder ( ) ;
3434
35- function createWasiImports ( ) {
36- const noop = ( ) => 0 ;
35+ if ( ! loadButton || ! output ) {
36+ console . error ( 'Missing #load-wasm button or #wasm-result element in the DOM.' ) ;
37+ }
3738
38- return {
39- wasi_snapshot_preview1 : {
40- proc_exit ( code ) {
41- if ( code !== 0 ) {
42- throw new Error ( `WebAssembly module exited with code ${ code } ` ) ;
43- }
44- } ,
45- fd_close : noop ,
46- fd_fdstat_get : noop ,
47- fd_seek : noop ,
48- fd_write : noop ,
49- environ_get : noop ,
50- environ_sizes_get : noop ,
51- args_get : noop ,
52- args_sizes_get : noop ,
53- clock_time_get : noop ,
54- random_get : noop ,
55- } ,
56- } ;
39+ async function instantiateWasm ( url , imports = { } ) {
40+ // Try streaming first (faster if server sets application/wasm)
41+ try {
42+ const res = await fetch ( url ) ;
43+ if ( ! res . ok ) throw new Error ( `HTTP ${ res . status } ${ res . statusText } ` ) ;
44+ try {
45+ return await WebAssembly . instantiateStreaming ( res , imports ) ;
46+ } catch {
47+ // Fallback if wrong MIME
48+ const bytes = await res . arrayBuffer ( ) ;
49+ return await WebAssembly . instantiate ( bytes , imports ) ;
50+ }
51+ } catch ( e ) {
52+ throw new Error ( `Failed to fetch/instantiate WASM: ${ e . message } ` ) ;
53+ }
5754}
5855
5956async function loadGreetingFromWasm ( ) {
60- output . textContent = 'Loading greeting...' ;
57+ if ( output ) output . textContent = 'Loading greeting...' ;
6158
6259 try {
63- const response = await fetch ( wasmUrl ) ;
64- if ( ! response . ok ) {
65- throw new Error ( `Unexpected response: ${ response . status } ${ response . statusText } ` ) ;
66- }
67-
68- const buffer = await response . arrayBuffer ( ) ;
69- const imports = createWasiImports ( ) ;
70- const { instance } = await WebAssembly . instantiate ( buffer , imports ) ;
60+ // No imports needed for your module, but keep an empty object for clarity
61+ const { instance } = await instantiateWasm ( new URL ( wasmPath , document . baseURI ) ) ;
7162 const { exports } = instance ;
7263
7364 const getGreeting = exports . get_greeting || exports . _get_greeting ;
7465 const getGreetingLength = exports . get_greeting_length || exports . _get_greeting_length ;
7566
7667 if ( typeof getGreeting !== 'function' || typeof getGreetingLength !== 'function' ) {
77- throw new Error ( 'Expected WebAssembly exports were not found.' ) ;
68+ throw new Error ( 'Expected WebAssembly exports "get_greeting" and "get_greeting_length" not found.' ) ;
7869 }
7970
71+ // Some toolchains expose an optional initializer (not needed here, but harmless)
8072 if ( typeof exports . _initialize === 'function' ) {
8173 exports . _initialize ( ) ;
8274 }
8375
8476 const memory = exports . memory ;
85-
8677 if ( ! ( memory instanceof WebAssembly . Memory ) ) {
8778 throw new Error ( 'WebAssembly memory export is missing.' ) ;
8879 }
8980
90- const pointer = getGreeting ( ) ;
91- const length = getGreetingLength ( ) ;
92- const bytes = new Uint8Array ( memory . buffer , pointer , length ) ;
93- const greeting = utf8Decoder . decode ( bytes ) ;
81+ const ptr = getGreeting ( ) ;
82+ const len = getGreetingLength ( ) ;
83+
84+ const bytes = new Uint8Array ( memory . buffer , ptr , len ) ;
85+ const greeting = utf8 . decode ( bytes ) ;
9486
95- output . textContent = greeting ;
87+ if ( output ) output . textContent = greeting ;
9688 } catch ( error ) {
97- console . error ( 'Failed to load WebAssembly module:' , error ) ;
98- output . textContent = `Failed to load greeting: ${ error . message } ` ;
89+ console . error ( error ) ;
90+ if ( output ) output . textContent = `Failed to load greeting: ${ error . message } ` ;
9991 }
10092}
10193
102- loadButton . addEventListener ( 'click' , loadGreetingFromWasm ) ;
94+ loadButton ? .addEventListener ( 'click' , loadGreetingFromWasm ) ;
10395</ script >
0 commit comments