|  | 
| 2 | 2 | // Use of this source code is governed by a BSD-style | 
| 3 | 3 | // license that can be found in the LICENSE file. | 
| 4 | 4 | 
 | 
| 5 |  | -(() => { | 
| 6 |  | -    // Map multiple JavaScript environments to a single common API, | 
| 7 |  | -    // preferring web standards over Node.js API. | 
| 8 |  | -    // | 
| 9 |  | -    // Environments considered: | 
| 10 |  | -    // - Browsers | 
| 11 |  | -    // - Node.js | 
| 12 |  | -    // - Electron | 
| 13 |  | -    // - Parcel | 
| 14 |  | -    // - Webpack | 
| 15 |  | - | 
| 16 |  | -    if (typeof global !== 'undefined') { | 
| 17 |  | -        // global already exists | 
| 18 |  | -    } else if (typeof window !== 'undefined') { | 
| 19 |  | -        window.global = window; | 
| 20 |  | -    } else if (typeof self !== 'undefined') { | 
| 21 |  | -        self.global = self; | 
| 22 |  | -    } else { | 
| 23 |  | -        throw new Error('cannot export Go (neither global, window nor self is defined)'); | 
| 24 |  | -    } | 
| 25 |  | - | 
| 26 |  | -    if (!global.require && typeof require !== 'undefined') { | 
| 27 |  | -        global.require = require; | 
| 28 |  | -    } | 
| 29 |  | - | 
| 30 |  | -    if (!global.fs && global.require) { | 
| 31 |  | -        const fs = require('fs'); | 
| 32 |  | -        if (typeof fs === 'object' && fs !== null && Object.keys(fs).length !== 0) { | 
| 33 |  | -            global.fs = fs; | 
| 34 |  | -        } | 
| 35 |  | -    } | 
|  | 5 | +'use strict'; | 
| 36 | 6 | 
 | 
|  | 7 | +(() => { | 
| 37 | 8 |     const enosys = () => { | 
| 38 | 9 |         const err = new Error('not implemented'); | 
| 39 | 10 |         err.code = 'ENOSYS'; | 
| 40 | 11 |         return err; | 
| 41 | 12 |     }; | 
| 42 | 13 | 
 | 
| 43 |  | -    if (!global.fs) { | 
|  | 14 | +    if (!globalThis.fs) { | 
| 44 | 15 |         let outputBuf = ''; | 
| 45 |  | -        global.fs = { | 
|  | 16 | +        globalThis.fs = { | 
| 46 | 17 |             constants: { | 
| 47 | 18 |                 O_WRONLY: -1, | 
| 48 | 19 |                 O_RDWR: -1, | 
|  | 
| 140 | 111 |         }; | 
| 141 | 112 |     } | 
| 142 | 113 | 
 | 
| 143 |  | -    if (!global.process) { | 
| 144 |  | -        global.process = { | 
|  | 114 | +    if (!globalThis.process) { | 
|  | 115 | +        globalThis.process = { | 
| 145 | 116 |             getuid() { | 
| 146 | 117 |                 return -1; | 
| 147 | 118 |             }, | 
|  | 
| 171 | 142 |         } | 
| 172 | 143 |     } | 
| 173 | 144 | 
 | 
| 174 |  | -    if (!global.crypto && global.require) { | 
| 175 |  | -        const nodeCrypto = require('crypto'); | 
| 176 |  | -        global.crypto = { | 
| 177 |  | -            getRandomValues(b) { | 
| 178 |  | -                nodeCrypto.randomFillSync(b); | 
| 179 |  | -            }, | 
| 180 |  | -        }; | 
| 181 |  | -    } | 
| 182 |  | -    if (!global.crypto) { | 
| 183 |  | -        throw new Error('global.crypto is not available, polyfill required (getRandomValues only)'); | 
|  | 145 | +    if (!globalThis.crypto) { | 
|  | 146 | +        throw new Error('globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)'); | 
| 184 | 147 |     } | 
| 185 | 148 | 
 | 
| 186 |  | -    if (!global.performance) { | 
| 187 |  | -        global.performance = { | 
| 188 |  | -            now() { | 
| 189 |  | -                const [sec, nsec] = process.hrtime(); | 
| 190 |  | -                return sec * 1000 + nsec / 1000000; | 
| 191 |  | -            }, | 
| 192 |  | -        }; | 
|  | 149 | +    if (!globalThis.performance) { | 
|  | 150 | +        throw new Error('globalThis.performance is not available, polyfill required (performance.now only)'); | 
| 193 | 151 |     } | 
| 194 | 152 | 
 | 
| 195 |  | -    if (!global.TextEncoder && global.require) { | 
| 196 |  | -        global.TextEncoder = require('util').TextEncoder; | 
| 197 |  | -    } | 
| 198 |  | -    if (!global.TextEncoder) { | 
| 199 |  | -        throw new Error('global.TextEncoder is not available, polyfill required'); | 
|  | 153 | +    if (!globalThis.TextEncoder) { | 
|  | 154 | +        throw new Error('globalThis.TextEncoder is not available, polyfill required'); | 
| 200 | 155 |     } | 
| 201 | 156 | 
 | 
| 202 |  | -    if (!global.TextDecoder && global.require) { | 
| 203 |  | -        global.TextDecoder = require('util').TextDecoder; | 
|  | 157 | +    if (!globalThis.TextDecoder) { | 
|  | 158 | +        throw new Error('globalThis.TextDecoder is not available, polyfill required'); | 
| 204 | 159 |     } | 
| 205 |  | -    if (!global.TextDecoder) { | 
| 206 |  | -        throw new Error('global.TextDecoder is not available, polyfill required'); | 
| 207 |  | -    } | 
| 208 |  | - | 
| 209 |  | -    // End of polyfills for common API. | 
| 210 | 160 | 
 | 
| 211 | 161 |     const encoder = new TextEncoder('utf-8'); | 
| 212 | 162 |     const decoder = new TextDecoder('utf-8'); | 
| 213 | 163 | 
 | 
| 214 |  | -    global.Go = class { | 
|  | 164 | +    globalThis.Go = class { | 
| 215 | 165 |         constructor() { | 
| 216 | 166 |             this.argv = ['js']; | 
| 217 | 167 |             this.env = {}; | 
|  | 
| 586 | 536 |                 null, | 
| 587 | 537 |                 true, | 
| 588 | 538 |                 false, | 
| 589 |  | -                global, | 
|  | 539 | +                globalThis, | 
| 590 | 540 |                 this, | 
| 591 | 541 |             ]; | 
| 592 | 542 |             this._goRefCounts = new Array(this._values.length).fill(Infinity); // number of references that Go has to a JS value, indexed by reference id | 
|  | 
| 595 | 545 |                 [null, 2], | 
| 596 | 546 |                 [true, 3], | 
| 597 | 547 |                 [false, 4], | 
| 598 |  | -                [global, 5], | 
|  | 548 | +                [globalThis, 5], | 
| 599 | 549 |                 [this, 6], | 
| 600 | 550 |             ]); | 
| 601 | 551 |             this._idPool = [];   // unused ids that have been garbage collected | 
|  | 
| 670 | 620 |             }; | 
| 671 | 621 |         } | 
| 672 | 622 |     } | 
| 673 |  | - | 
| 674 |  | -    if ( | 
| 675 |  | -        typeof module !== 'undefined' && | 
| 676 |  | -        global.require && | 
| 677 |  | -        global.require.main === module && | 
| 678 |  | -        global.process && | 
| 679 |  | -        global.process.versions && | 
| 680 |  | -        !global.process.versions.electron | 
| 681 |  | -    ) { | 
| 682 |  | -        if (process.argv.length < 3) { | 
| 683 |  | -            console.error('usage: go_js_wasm_exec [wasm binary] [arguments]'); | 
| 684 |  | -            process.exit(1); | 
| 685 |  | -        } | 
| 686 |  | - | 
| 687 |  | -        const go = new Go(); | 
| 688 |  | -        go.argv = process.argv.slice(2); | 
| 689 |  | -        go.env = Object.assign({TMPDIR: require('os').tmpdir()}, process.env); | 
| 690 |  | -        go.exit = process.exit; | 
| 691 |  | -        WebAssembly.instantiate(fs.readFileSync(process.argv[2]), go.importObject).then((result) => { | 
| 692 |  | -            process.on('exit', (code) => { // Node.js exits if no event handler is pending | 
| 693 |  | -                if (code === 0 && !go.exited) { | 
| 694 |  | -                    // deadlock, make Go print error and stack traces | 
| 695 |  | -                    go._pendingEvent = {id: 0}; | 
| 696 |  | -                    go._resume(); | 
| 697 |  | -                } | 
| 698 |  | -            }); | 
| 699 |  | -            return go.run(result.instance); | 
| 700 |  | -        }).catch((err) => { | 
| 701 |  | -            console.error(err); | 
| 702 |  | -            process.exit(1); | 
| 703 |  | -        }); | 
| 704 |  | -    } | 
| 705 | 623 | })(); | 
0 commit comments