golang-wasm/example/dist/main.js

1 line
9.8 KiB
JavaScript

(()=>{var e={322:(e,t,s)=>{e=s.nmd(e),(()=>{if(void 0!==s.g);else if("undefined"!=typeof window)window.global=window;else{if("undefined"==typeof self)throw new Error("cannot export Go (neither global, window nor self is defined)");self.global=self}if(s.g.require||(s.g.require=s(717)),!s.g.fs&&s.g.require){const e=s(687);"object"==typeof e&&null!==e&&0!==Object.keys(e).length&&(s.g.fs=e)}const t=()=>{const e=new Error("not implemented");return e.code="ENOSYS",e};if(!s.g.fs){let e="";s.g.fs={constants:{O_WRONLY:-1,O_RDWR:-1,O_CREAT:-1,O_TRUNC:-1,O_APPEND:-1,O_EXCL:-1},writeSync(t,s){e+=n.decode(s);const i=e.lastIndexOf("\n");return-1!=i&&(console.log(e.substr(0,i)),e=e.substr(i+1)),s.length},write(e,s,i,n,o,r){0===i&&n===s.length&&null===o?r(null,this.writeSync(e,s)):r(t())},chmod(e,s,i){i(t())},chown(e,s,i,n){n(t())},close(e,s){s(t())},fchmod(e,s,i){i(t())},fchown(e,s,i,n){n(t())},fstat(e,s){s(t())},fsync(e,t){t(null)},ftruncate(e,s,i){i(t())},lchown(e,s,i,n){n(t())},link(e,s,i){i(t())},lstat(e,s){s(t())},mkdir(e,s,i){i(t())},open(e,s,i,n){n(t())},read(e,s,i,n,o,r){r(t())},readdir(e,s){s(t())},readlink(e,s){s(t())},rename(e,s,i){i(t())},rmdir(e,s){s(t())},stat(e,s){s(t())},symlink(e,s,i){i(t())},truncate(e,s,i){i(t())},unlink(e,s){s(t())},utimes(e,s,i,n){n(t())}}}if(s.g.process||(s.g.process={getuid:()=>-1,getgid:()=>-1,geteuid:()=>-1,getegid:()=>-1,getgroups(){throw t()},pid:-1,ppid:-1,umask(){throw t()},cwd(){throw t()},chdir(){throw t()}}),!s.g.crypto&&s.g.require){const e=s(796);s.g.crypto={getRandomValues(t){e.randomFillSync(t)}}}if(!s.g.crypto)throw new Error("global.crypto is not available, polyfill required (getRandomValues only)");if(s.g.performance||(s.g.performance={now(){const[e,t]=process.hrtime();return 1e3*e+t/1e6}}),!s.g.TextEncoder&&s.g.require&&(s.g.TextEncoder=s(502).TextEncoder),!s.g.TextEncoder)throw new Error("global.TextEncoder is not available, polyfill required");if(!s.g.TextDecoder&&s.g.require&&(s.g.TextDecoder=s(502).TextDecoder),!s.g.TextDecoder)throw new Error("global.TextDecoder is not available, polyfill required");const i=new TextEncoder("utf-8"),n=new TextDecoder("utf-8");if(s.g.Go=class{constructor(){this.argv=["js"],this.env={},this.exit=e=>{0!==e&&console.warn("exit code:",e)},this._exitPromise=new Promise((e=>{this._resolveExitPromise=e})),this._pendingEvent=null,this._scheduledTimeouts=new Map,this._nextCallbackTimeoutID=1;const e=(e,t)=>{this.mem.setUint32(e+0,t,!0),this.mem.setUint32(e+4,Math.floor(t/4294967296),!0)},t=e=>this.mem.getUint32(e+0,!0)+4294967296*this.mem.getInt32(e+4,!0),s=e=>{const t=this.mem.getFloat64(e,!0);if(0===t)return;if(!isNaN(t))return t;const s=this.mem.getUint32(e,!0);return this._values[s]},o=(e,t)=>{const s=2146959360;if("number"==typeof t&&0!==t)return isNaN(t)?(this.mem.setUint32(e+4,s,!0),void this.mem.setUint32(e,0,!0)):void this.mem.setFloat64(e,t,!0);if(void 0===t)return void this.mem.setFloat64(e,0,!0);let i=this._ids.get(t);void 0===i&&(i=this._idPool.pop(),void 0===i&&(i=this._values.length),this._values[i]=t,this._goRefCounts[i]=0,this._ids.set(t,i)),this._goRefCounts[i]++;let n=0;switch(typeof t){case"object":null!==t&&(n=1);break;case"string":n=2;break;case"symbol":n=3;break;case"function":n=4}this.mem.setUint32(e+4,s|n,!0),this.mem.setUint32(e,i,!0)},r=e=>{const s=t(e+0),i=t(e+8);return new Uint8Array(this._inst.exports.mem.buffer,s,i)},l=e=>{const i=t(e+0),n=t(e+8),o=new Array(n);for(let e=0;e<n;e++)o[e]=s(i+8*e);return o},a=e=>{const s=t(e+0),i=t(e+8);return n.decode(new DataView(this._inst.exports.mem.buffer,s,i))},c=Date.now()-performance.now();this.importObject={go:{"runtime.wasmExit":e=>{e>>>=0;const t=this.mem.getInt32(e+8,!0);this.exited=!0,delete this._inst,delete this._values,delete this._goRefCounts,delete this._ids,delete this._idPool,this.exit(t)},"runtime.wasmWrite":e=>{const s=t(8+(e>>>=0)),i=t(e+16),n=this.mem.getInt32(e+24,!0);fs.writeSync(s,new Uint8Array(this._inst.exports.mem.buffer,i,n))},"runtime.resetMemoryDataView":e=>{this.mem=new DataView(this._inst.exports.mem.buffer)},"runtime.nanotime1":t=>{e(8+(t>>>=0),1e6*(c+performance.now()))},"runtime.walltime1":t=>{t>>>=0;const s=(new Date).getTime();e(t+8,s/1e3),this.mem.setInt32(t+16,s%1e3*1e6,!0)},"runtime.scheduleTimeoutEvent":e=>{e>>>=0;const s=this._nextCallbackTimeoutID;this._nextCallbackTimeoutID++,this._scheduledTimeouts.set(s,setTimeout((()=>{for(this._resume();this._scheduledTimeouts.has(s);)console.warn("scheduleTimeoutEvent: missed timeout event"),this._resume()}),t(e+8)+1)),this.mem.setInt32(e+16,s,!0)},"runtime.clearTimeoutEvent":e=>{e>>>=0;const t=this.mem.getInt32(e+8,!0);clearTimeout(this._scheduledTimeouts.get(t)),this._scheduledTimeouts.delete(t)},"runtime.getRandomData":e=>{e>>>=0,crypto.getRandomValues(r(e+8))},"syscall/js.finalizeRef":e=>{e>>>=0;const t=this.mem.getUint32(e+8,!0);if(this._goRefCounts[t]--,0===this._goRefCounts[t]){const e=this._values[t];this._values[t]=null,this._ids.delete(e),this._idPool.push(t)}},"syscall/js.stringVal":e=>{o(24+(e>>>=0),a(e+8))},"syscall/js.valueGet":e=>{e>>>=0;const t=Reflect.get(s(e+8),a(e+16));e=this._inst.exports.getsp()>>>0,o(e+32,t)},"syscall/js.valueSet":e=>{e>>>=0,Reflect.set(s(e+8),a(e+16),s(e+32))},"syscall/js.valueDelete":e=>{e>>>=0,Reflect.deleteProperty(s(e+8),a(e+16))},"syscall/js.valueIndex":e=>{o(24+(e>>>=0),Reflect.get(s(e+8),t(e+16)))},"syscall/js.valueSetIndex":e=>{e>>>=0,Reflect.set(s(e+8),t(e+16),s(e+24))},"syscall/js.valueCall":e=>{e>>>=0;try{const t=s(e+8),i=Reflect.get(t,a(e+16)),n=l(e+32),r=Reflect.apply(i,t,n);e=this._inst.exports.getsp()>>>0,o(e+56,r),this.mem.setUint8(e+64,1)}catch(t){o(e+56,t),this.mem.setUint8(e+64,0)}},"syscall/js.valueInvoke":e=>{e>>>=0;try{const t=s(e+8),i=l(e+16),n=Reflect.apply(t,void 0,i);e=this._inst.exports.getsp()>>>0,o(e+40,n),this.mem.setUint8(e+48,1)}catch(t){o(e+40,t),this.mem.setUint8(e+48,0)}},"syscall/js.valueNew":e=>{e>>>=0;try{const t=s(e+8),i=l(e+16),n=Reflect.construct(t,i);e=this._inst.exports.getsp()>>>0,o(e+40,n),this.mem.setUint8(e+48,1)}catch(t){o(e+40,t),this.mem.setUint8(e+48,0)}},"syscall/js.valueLength":t=>{e(16+(t>>>=0),parseInt(s(t+8).length))},"syscall/js.valuePrepareString":t=>{t>>>=0;const n=i.encode(String(s(t+8)));o(t+16,n),e(t+24,n.length)},"syscall/js.valueLoadString":e=>{const t=s(8+(e>>>=0));r(e+16).set(t)},"syscall/js.valueInstanceOf":e=>{e>>>=0,this.mem.setUint8(e+24,s(e+8)instanceof s(e+16)?1:0)},"syscall/js.copyBytesToGo":t=>{const i=r(8+(t>>>=0)),n=s(t+32);if(!(n instanceof Uint8Array||n instanceof Uint8ClampedArray))return void this.mem.setUint8(t+48,0);const o=n.subarray(0,i.length);i.set(o),e(t+40,o.length),this.mem.setUint8(t+48,1)},"syscall/js.copyBytesToJS":t=>{const i=s(8+(t>>>=0)),n=r(t+16);if(!(i instanceof Uint8Array||i instanceof Uint8ClampedArray))return void this.mem.setUint8(t+48,0);const o=n.subarray(0,i.length);i.set(o),e(t+40,o.length),this.mem.setUint8(t+48,1)},debug:e=>{console.log(e)}}}}async run(e){if(!(e instanceof WebAssembly.Instance))throw new Error("Go.run: WebAssembly.Instance expected");this._inst=e,this.mem=new DataView(this._inst.exports.mem.buffer),this._values=[NaN,0,null,!0,!1,s.g,this],this._goRefCounts=new Array(this._values.length).fill(1/0),this._ids=new Map([[0,1],[null,2],[!0,3],[!1,4],[s.g,5],[this,6]]),this._idPool=[],this.exited=!1;let t=4096;const n=e=>{const s=t,n=i.encode(e+"\0");return new Uint8Array(this.mem.buffer,t,n.length).set(n),t+=n.length,t%8!=0&&(t+=8-t%8),s},o=this.argv.length,r=[];this.argv.forEach((e=>{r.push(n(e))})),r.push(0),Object.keys(this.env).sort().forEach((e=>{r.push(n(`${e}=${this.env[e]}`))})),r.push(0);const l=t;r.forEach((e=>{this.mem.setUint32(t,e,!0),this.mem.setUint32(t+4,0,!0),t+=8})),this._inst.exports.run(o,l),this.exited&&this._resolveExitPromise(),await this._exitPromise}_resume(){if(this.exited)throw new Error("Go program has already exited");this._inst.exports.resume(),this.exited&&this._resolveExitPromise()}_makeFuncWrapper(e){const t=this;return function(){const s={id:e,this:this,args:arguments};return t._pendingEvent=s,t._resume(),s.result}}},s.g.require&&s.g.require.main===e&&s.g.process&&s.g.process.versions&&!s.g.process.versions.electron){process.argv.length<3&&(console.error("usage: go_js_wasm_exec [wasm binary] [arguments]"),process.exit(1));const e=new Go;e.argv=process.argv.slice(2),e.env=Object.assign({TMPDIR:s(580).tmpdir()},process.env),e.exit=process.exit,WebAssembly.instantiate(fs.readFileSync(process.argv[2]),e.importObject).then((t=>(process.on("exit",(t=>{0!==t||e.exited||(e._pendingEvent={id:0},e._resume())})),e.run(t.instance)))).catch((e=>{console.error(e),process.exit(1)}))}})()},717:e=>{function t(e){var t=new Error("Cannot find module '"+e+"'");throw t.code="MODULE_NOT_FOUND",t}t.keys=()=>[],t.resolve=t,t.id=717,e.exports=t},796:()=>{},687:()=>{},580:()=>{},502:()=>{}},t={};function s(i){var n=t[i];if(void 0!==n)return n.exports;var o=t[i]={id:i,loaded:!1,exports:{}};return e[i](o,o.exports,s),o.loaded=!0,o.exports}s.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||new Function("return this")()}catch(e){if("object"==typeof window)return window}}(),s.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),s.nmd=e=>(e.paths=[],e.children||(e.children=[]),e),(()=>{"use strict";const e=s.g||window||self;e.__go_wasm__||(e.__go_wasm__={});const t=new Date;t.setSeconds(t.getSeconds()+3);const i=e.__go_wasm__;s(322);const n=fetch("main.wasm").then((e=>e.arrayBuffer())),o=(r=n,async function(){const t=new e.Go;let s=await r,n=await WebAssembly.instantiate(s,t.importObject);t.run(n.instance),setTimeout((()=>{!0!==i.__ready__&&console.warn("Golang Wasm Bridge (__go_wasm__.__ready__) still not true after max time")}),3e3)}(),new Proxy({},{get:(e,t)=>(...e)=>new Promise((async(s,n)=>{for(;!0!==i.__ready__;)console.log("waiting"),await new Promise(requestAnimationFrame);if(console.log("done!"),"function"!=typeof i[t])return void s(i[t]);const o=i[t].apply(void 0,e);o.error?n(o.error):s(o.result)}))}));var r;(async()=>{console.log(o),console.log(await o.test())})()})()})();