From 9613c2781d80794a825523a5f1e9ab31ae570266 Mon Sep 17 00:00:00 2001 From: Doehyun Baek Date: Tue, 6 Feb 2024 14:55:44 +0900 Subject: [PATCH] Add external-call testcase again This was lost due to a merge process. Adding back on. Original PR: https://github.com/jakobgetz/wasm-r3/pull/4 --- tests/offline/external-call/test.js | 8 + .../offline/external-call/website/index.html | 12 ++ tests/offline/external-call/website/index.js | 11 + .../offline/external-call/website/pkg/demo.js | 198 ++++++++++++++++++ .../external-call/website/pkg/demo_bg.wasm | Bin 0 -> 17082 bytes 5 files changed, 229 insertions(+) create mode 100644 tests/offline/external-call/test.js create mode 100644 tests/offline/external-call/website/index.html create mode 100644 tests/offline/external-call/website/index.js create mode 100644 tests/offline/external-call/website/pkg/demo.js create mode 100644 tests/offline/external-call/website/pkg/demo_bg.wasm diff --git a/tests/offline/external-call/test.js b/tests/offline/external-call/test.js new file mode 100644 index 00000000..ac3e4c6b --- /dev/null +++ b/tests/offline/external-call/test.js @@ -0,0 +1,8 @@ +import { delay } from '../../../dist/tests/test-utils.cjs' + +export default async function test(analyser) { + const url = 'http://localhost:8000' + await analyser.start(url, { headless: true }) + await delay(100) + return await analyser.stop() +} \ No newline at end of file diff --git a/tests/offline/external-call/website/index.html b/tests/offline/external-call/website/index.html new file mode 100644 index 00000000..551c1499 --- /dev/null +++ b/tests/offline/external-call/website/index.html @@ -0,0 +1,12 @@ + + + + + Worker test + + + + + + + \ No newline at end of file diff --git a/tests/offline/external-call/website/index.js b/tests/offline/external-call/website/index.js new file mode 100644 index 00000000..ba3fcecf --- /dev/null +++ b/tests/offline/external-call/website/index.js @@ -0,0 +1,11 @@ +import init, { greet } from './pkg/demo.js'; + +async function run() { + await init(); + + console.log(greet("Hello")); + console.log(greet("World")); +} + + +run(); diff --git a/tests/offline/external-call/website/pkg/demo.js b/tests/offline/external-call/website/pkg/demo.js new file mode 100644 index 00000000..50005544 --- /dev/null +++ b/tests/offline/external-call/website/pkg/demo.js @@ -0,0 +1,198 @@ +let wasm; + +const cachedTextDecoder = (typeof TextDecoder !== 'undefined' ? new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }) : { decode: () => { throw Error('TextDecoder not available') } } ); + +if (typeof TextDecoder !== 'undefined') { cachedTextDecoder.decode(); }; + +let cachedUint8Memory0 = null; + +function getUint8Memory0() { + if (cachedUint8Memory0 === null || cachedUint8Memory0.byteLength === 0) { + cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer); + } + return cachedUint8Memory0; +} + +function getStringFromWasm0(ptr, len) { + ptr = ptr >>> 0; + return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len)); +} + +let WASM_VECTOR_LEN = 0; + +const cachedTextEncoder = (typeof TextEncoder !== 'undefined' ? new TextEncoder('utf-8') : { encode: () => { throw Error('TextEncoder not available') } } ); + +const encodeString = (typeof cachedTextEncoder.encodeInto === 'function' + ? function (arg, view) { + return cachedTextEncoder.encodeInto(arg, view); +} + : function (arg, view) { + const buf = cachedTextEncoder.encode(arg); + view.set(buf); + return { + read: arg.length, + written: buf.length + }; +}); + +function passStringToWasm0(arg, malloc, realloc) { + + if (realloc === undefined) { + const buf = cachedTextEncoder.encode(arg); + const ptr = malloc(buf.length, 1) >>> 0; + getUint8Memory0().subarray(ptr, ptr + buf.length).set(buf); + WASM_VECTOR_LEN = buf.length; + return ptr; + } + + let len = arg.length; + let ptr = malloc(len, 1) >>> 0; + + const mem = getUint8Memory0(); + + let offset = 0; + + for (; offset < len; offset++) { + const code = arg.charCodeAt(offset); + if (code > 0x7F) break; + mem[ptr + offset] = code; + } + + if (offset !== len) { + if (offset !== 0) { + arg = arg.slice(offset); + } + ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0; + const view = getUint8Memory0().subarray(ptr + offset, ptr + len); + const ret = encodeString(arg, view); + + offset += ret.written; + } + + WASM_VECTOR_LEN = offset; + return ptr; +} + +let cachedInt32Memory0 = null; + +function getInt32Memory0() { + if (cachedInt32Memory0 === null || cachedInt32Memory0.byteLength === 0) { + cachedInt32Memory0 = new Int32Array(wasm.memory.buffer); + } + return cachedInt32Memory0; +} +/** +* @param {string} a +* @returns {string} +*/ +export function greet(a) { + let deferred2_0; + let deferred2_1; + try { + const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); + const ptr0 = passStringToWasm0(a, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + wasm.greet(retptr, ptr0, len0); + var r0 = getInt32Memory0()[retptr / 4 + 0]; + var r1 = getInt32Memory0()[retptr / 4 + 1]; + deferred2_0 = r0; + deferred2_1 = r1; + return getStringFromWasm0(r0, r1); + } finally { + wasm.__wbindgen_add_to_stack_pointer(16); + wasm.__wbindgen_free(deferred2_0, deferred2_1, 1); + } +} + +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_log_24dc0bd9d833d95d = function(arg0, arg1) { + console.log(getStringFromWasm0(arg0, arg1)); + }; + + return imports; +} + +function __wbg_init_memory(imports, maybe_memory) { + +} + +function __wbg_finalize_init(instance, module) { + wasm = instance.exports; + __wbg_init.__wbindgen_wasm_module = module; + cachedInt32Memory0 = null; + cachedUint8Memory0 = null; + + + return wasm; +} + +function initSync(module) { + if (wasm !== undefined) return wasm; + + 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(input) { + if (wasm !== undefined) return wasm; + + if (typeof input === 'undefined') { + input = new URL('demo_bg.wasm', import.meta.url); + } + const imports = __wbg_get_imports(); + + if (typeof input === 'string' || (typeof Request === 'function' && input instanceof Request) || (typeof URL === 'function' && input instanceof URL)) { + input = fetch(input); + } + + __wbg_init_memory(imports); + + const { instance, module } = await __wbg_load(await input, imports); + + return __wbg_finalize_init(instance, module); +} + +export { initSync } +export default __wbg_init; diff --git a/tests/offline/external-call/website/pkg/demo_bg.wasm b/tests/offline/external-call/website/pkg/demo_bg.wasm new file mode 100644 index 0000000000000000000000000000000000000000..48b95dea929f01f7cdd01a11329842bcf1db8b42 GIT binary patch literal 17082 zcmeI4TZ~=TS;yDDoXgBUW1pmM>v|?+pHl-4v6D0RnUvC5mg00MZX|?wLb50J#Pu29 z=J;fis3o4#G%ctFOZ1_LKJ+1iK18BI5#%9)0U}zVf`Sx@JXBRAA`wBQ<{^|+q%yz% zxAxw1#&*+kk>G(?Gi$H=x4!%Ltu=9LS1(4+x#+{=r{inaqHFr?PAAvwJ1Rci>68Zb zM*3wHce<`Njdm{XM#Cy+_V2pIw@rJ-a-A)p_4!L@&#hlNwRZOGso~{QSBGn7Hcnl+eE!mKecoP=Pkqq&_C~jP*u^WO|8nR5HF`K*q5Rt$pL}?t!AI8b zI{(CGr{-Vz%^i0ncHOZDCsOb7+D0dduj#{Y$Ey?WkiVPH8#it=K1PMlIDKca+vOA+d?yd{9d+Wo-c2SPDX>B96wo?p9!*-Q;4f9++wlRRCBTX~u%3vsjDDv$kBdGz?^LhLf8HTxWaBWXE)4HjY+ z%Ol8;vk;)FrPatl)T;r|lM{)km>VWbD!B(IIQS>_qB>yENGF_?Xl-|$kXYbL-|><7 zV5ecL#;UZs?ga41{s1`PH_+&0>106VL{v~vW1HP5cRD6ZhGX&tF8g^p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