| 95 | |
| 96 | globalThis.Go = class { |
| 97 | constructor() { |
| 98 | this.argv = ["js"]; |
| 99 | this.env = {}; |
| 100 | this.exit = (code) => { |
| 101 | if (code !== 0) { |
| 102 | console.warn("exit code:", code); |
| 103 | } |
| 104 | }; |
| 105 | this._exitPromise = new Promise((resolve) => { |
| 106 | this._resolveExitPromise = resolve; |
| 107 | }); |
| 108 | this._pendingEvent = null; |
| 109 | this._scheduledTimeouts = new Map(); |
| 110 | this._nextCallbackTimeoutID = 1; |
| 111 | |
| 112 | const setInt64 = (addr, v) => { |
| 113 | this.mem.setUint32(addr + 0, v, true); |
| 114 | this.mem.setUint32(addr + 4, Math.floor(v / 4294967296), true); |
| 115 | } |
| 116 | |
| 117 | const setInt32 = (addr, v) => { |
| 118 | this.mem.setUint32(addr + 0, v, true); |
| 119 | } |
| 120 | |
| 121 | const getInt64 = (addr) => { |
| 122 | const low = this.mem.getUint32(addr + 0, true); |
| 123 | const high = this.mem.getInt32(addr + 4, true); |
| 124 | return low + high * 4294967296; |
| 125 | } |
| 126 | |
| 127 | const loadValue = (addr) => { |
| 128 | const f = this.mem.getFloat64(addr, true); |
| 129 | if (f === 0) { |
| 130 | return undefined; |
| 131 | } |
| 132 | if (!isNaN(f)) { |
| 133 | return f; |
| 134 | } |
| 135 | |
| 136 | const id = this.mem.getUint32(addr, true); |
| 137 | return this._values[id]; |
| 138 | } |
| 139 | |
| 140 | const storeValue = (addr, v) => { |
| 141 | const nanHead = 0x7FF80000; |
| 142 | |
| 143 | if (typeof v === "number" && v !== 0) { |
| 144 | if (isNaN(v)) { |
| 145 | this.mem.setUint32(addr + 4, nanHead, true); |
| 146 | this.mem.setUint32(addr, 0, true); |
| 147 | return; |
| 148 | } |
| 149 | this.mem.setFloat64(addr, v, true); |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | if (v === undefined) { |
| 154 | this.mem.setFloat64(addr, 0, true); |