tsgo-wasm 0.0.0 → 7.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,55 @@
1
+ Apache License
2
+
3
+ Version 2.0, January 2004
4
+
5
+ http://www.apache.org/licenses/
6
+
7
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
8
+
9
+ 1. Definitions.
10
+
11
+ "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
16
+
17
+ "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
18
+
19
+ "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
20
+
21
+ "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
22
+
23
+ "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
24
+
25
+ "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
26
+
27
+ "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
28
+
29
+ "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
30
+
31
+ 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
32
+
33
+ 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
34
+
35
+ 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
36
+
37
+ You must give any other recipients of the Work or Derivative Works a copy of this License; and
38
+
39
+ You must cause any modified files to carry prominent notices stating that You changed the files; and
40
+
41
+ You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
42
+
43
+ If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
44
+
45
+ 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
46
+
47
+ 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
48
+
49
+ 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
50
+
51
+ 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
52
+
53
+ 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
54
+
55
+ END OF TERMS AND CONDITIONS
package/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # TypeScript 7 WASM
2
+
3
+ [![npm version][npm-version-src]][npm-version-href]
4
+ [![npm downloads][npm-downloads-src]][npm-downloads-href]
5
+ [![Release][release-src]][release-href]
6
+
7
+ **Unofficial** WebAssembly (WASM) distribution of [TypeScript 7](https://github.com/microsoft/typescript-go), automatically built daily via GitHub Actions.
8
+
9
+ Use this version if your operating system or architecture is not supported by the native build, or if you need to run it in browsers.
10
+
11
+ Download the latest binaries from the [Releases page](https://github.com/sxzz/tsgo-wasm/releases).
12
+
13
+ Read more about the TypeScript 7 [here](https://devblogs.microsoft.com/typescript/typescript-native-port/)!
14
+
15
+ > [!WARNING]
16
+ > This is **NOT** an official distribution from the TypeScript official team, but all binaries are built directly from the official source code without any modifications via GitHub Actions.
17
+
18
+ ## Usage
19
+
20
+ ```bash
21
+ npm install tsgo-wasm
22
+
23
+ npx tsgo-wasm -v
24
+ # Version 7.0.0-dev.20250519
25
+ ```
26
+
27
+ ## 🧯 Safety
28
+
29
+ The binaries are built through GitHub Actions using source code directly from the official TypeScript repository.
30
+ Our automated build process preserves the code integrity without any modifications.
31
+ The complete build workflow is transparent and open-source, available for inspection [here](./.github/workflows/release.yml).
32
+
33
+ We understand security concerns regarding unofficial binaries.
34
+ For additional peace of mind, you're welcome to fork this repository and execute the build workflow yourself.
35
+
36
+ ## Credits
37
+
38
+ Thanks to Claude 3.7 Sonnet for helping me build the GitHub Actions workflow.
39
+
40
+ ## License
41
+
42
+ The TypeScript project and this project both are licensed under the [Apache License 2.0](./LICENSE).
43
+
44
+ <!-- Badges -->
45
+
46
+ [npm-version-src]: https://img.shields.io/npm/v/tsgo-wasm.svg
47
+ [npm-version-href]: https://npmjs.com/package/tsgo-wasm
48
+ [npm-downloads-src]: https://img.shields.io/npm/dm/tsgo-wasm
49
+ [npm-downloads-href]: https://www.npmcharts.com/compare/tsgo-wasm?interval=30
50
+ [release-src]: https://github.com/sxzz/tsgo-wasm/actions/workflows/release.yml/badge.svg
51
+ [release-href]: https://github.com/sxzz/tsgo-wasm/actions/workflows/release.yml
package/package.json CHANGED
@@ -1,16 +1,36 @@
1
1
  {
2
2
  "name": "tsgo-wasm",
3
3
  "type": "module",
4
- "version": "0.0.0",
5
- "description": "TypeScript Go",
6
- "homepage": "Native port of TypeScript",
4
+ "version": "7.0.2",
5
+ "description": "TypeScript 7 WebAssembly (WASM) distribution",
7
6
  "license": "Apache-2.0",
7
+ "homepage": "https://github.com/sxzz/tsgo-wasm#readme",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/sxzz/tsgo-wasm.git",
11
+ "directory": "packages/wasm"
12
+ },
13
+ "keywords": [
14
+ "typescript-go",
15
+ "typescript",
16
+ "tsgo",
17
+ "go",
18
+ "wasm"
19
+ ],
20
+ "main": "tsgo.wasm",
21
+ "bin": {
22
+ "tsgo-wasm": "tsgo-wasm"
23
+ },
8
24
  "files": [
25
+ "tsgo-wasm",
9
26
  "tsgo.wasm"
10
27
  ],
11
- "main": "tsgo.wasm",
12
28
  "publishConfig": {
13
29
  "access": "public",
14
30
  "tag": "latest"
31
+ },
32
+ "buildInfo": {
33
+ "date": "2026-07-08",
34
+ "commit": "2bd066d87f5bafd315be9f40889d0a60b9e58e0b"
15
35
  }
16
- }
36
+ }
package/tsgo-wasm ADDED
@@ -0,0 +1,522 @@
1
+ #!/usr/bin/env node
2
+ import fs from 'node:fs'
3
+ import { createRequire } from 'node:module'
4
+ import path from 'node:path'
5
+ import { tmpdir } from 'node:os'
6
+ import { fileURLToPath } from 'node:url'
7
+
8
+ const require = createRequire(import.meta.url)
9
+ globalThis.fs = fs
10
+ globalThis.path = path
11
+ globalThis.require = require
12
+
13
+ const filename = fileURLToPath(import.meta.url)
14
+
15
+ const encoder = new TextEncoder('utf-8')
16
+ const decoder = new TextDecoder('utf-8')
17
+
18
+ class Go {
19
+ constructor() {
20
+ this._exitPromise = new Promise((resolve) => {
21
+ this._resolveExitPromise = resolve
22
+ })
23
+ this._pendingEvent = null
24
+ this._scheduledTimeouts = new Map()
25
+ this._nextCallbackTimeoutID = 1
26
+
27
+ const setInt64 = (addr, v) => {
28
+ this.mem.setUint32(addr + 0, v, true)
29
+ this.mem.setUint32(addr + 4, Math.floor(v / 4294967296), true)
30
+ }
31
+
32
+ const getInt64 = (addr) => {
33
+ const low = this.mem.getUint32(addr + 0, true)
34
+ const high = this.mem.getInt32(addr + 4, true)
35
+ return low + high * 4294967296
36
+ }
37
+
38
+ const loadValue = (addr) => {
39
+ const f = this.mem.getFloat64(addr, true)
40
+ if (f === 0) {
41
+ return undefined
42
+ }
43
+ if (!isNaN(f)) {
44
+ return f
45
+ }
46
+
47
+ const id = this.mem.getUint32(addr, true)
48
+ return this._values[id]
49
+ }
50
+
51
+ const storeValue = (addr, v) => {
52
+ const nanHead = 0x7ff80000
53
+
54
+ if (typeof v === 'number' && v !== 0) {
55
+ if (isNaN(v)) {
56
+ this.mem.setUint32(addr + 4, nanHead, true)
57
+ this.mem.setUint32(addr, 0, true)
58
+ return
59
+ }
60
+ this.mem.setFloat64(addr, v, true)
61
+ return
62
+ }
63
+
64
+ if (v === undefined) {
65
+ this.mem.setFloat64(addr, 0, true)
66
+ return
67
+ }
68
+
69
+ let id = this._ids.get(v)
70
+ if (id === undefined) {
71
+ id = this._idPool.pop()
72
+ if (id === undefined) {
73
+ id = this._values.length
74
+ }
75
+ this._values[id] = v
76
+ this._goRefCounts[id] = 0
77
+ this._ids.set(v, id)
78
+ }
79
+ this._goRefCounts[id]++
80
+ let typeFlag = 0
81
+ switch (typeof v) {
82
+ case 'object':
83
+ if (v !== null) {
84
+ typeFlag = 1
85
+ }
86
+ break
87
+ case 'string':
88
+ typeFlag = 2
89
+ break
90
+ case 'symbol':
91
+ typeFlag = 3
92
+ break
93
+ case 'function':
94
+ typeFlag = 4
95
+ break
96
+ }
97
+ this.mem.setUint32(addr + 4, nanHead | typeFlag, true)
98
+ this.mem.setUint32(addr, id, true)
99
+ }
100
+
101
+ const loadSlice = (addr) => {
102
+ const array = getInt64(addr + 0)
103
+ const len = getInt64(addr + 8)
104
+ return new Uint8Array(this._inst.exports.mem.buffer, array, len)
105
+ }
106
+
107
+ const loadSliceOfValues = (addr) => {
108
+ const array = getInt64(addr + 0)
109
+ const len = getInt64(addr + 8)
110
+ const a = new Array(len)
111
+ for (let i = 0; i < len; i++) {
112
+ a[i] = loadValue(array + i * 8)
113
+ }
114
+ return a
115
+ }
116
+
117
+ const loadString = (addr) => {
118
+ const saddr = getInt64(addr + 0)
119
+ const len = getInt64(addr + 8)
120
+ return decoder.decode(
121
+ new DataView(this._inst.exports.mem.buffer, saddr, len),
122
+ )
123
+ }
124
+
125
+ const testCallExport = (a, b) => {
126
+ this._inst.exports.testExport0()
127
+ return this._inst.exports.testExport(a, b)
128
+ }
129
+
130
+ const timeOrigin = Date.now() - performance.now()
131
+ this.importObject = {
132
+ _gotest: {
133
+ add: (a, b) => a + b,
134
+ callExport: testCallExport,
135
+ },
136
+ gojs: {
137
+ // Go's SP does not change as long as no Go code is running. Some operations (e.g. calls, getters and setters)
138
+ // may synchronously trigger a Go event handler. This makes Go code get executed in the middle of the imported
139
+ // function. A goroutine can switch to a new stack if the current stack is too small (see morestack function).
140
+ // This changes the SP, thus we have to update the SP used by the imported function.
141
+
142
+ // func wasmExit(code int32)
143
+ 'runtime.wasmExit': (sp) => {
144
+ sp >>>= 0
145
+ const code = this.mem.getInt32(sp + 8, true)
146
+ this.exited = true
147
+ delete this._inst
148
+ delete this._values
149
+ delete this._goRefCounts
150
+ delete this._ids
151
+ delete this._idPool
152
+ this.exit(code)
153
+ },
154
+
155
+ // func wasmWrite(fd uintptr, p unsafe.Pointer, n int32)
156
+ 'runtime.wasmWrite': (sp) => {
157
+ sp >>>= 0
158
+ const fd = getInt64(sp + 8)
159
+ const p = getInt64(sp + 16)
160
+ const n = this.mem.getInt32(sp + 24, true)
161
+ fs.writeSync(fd, new Uint8Array(this._inst.exports.mem.buffer, p, n))
162
+ },
163
+
164
+ // func resetMemoryDataView()
165
+ 'runtime.resetMemoryDataView': (sp) => {
166
+ sp >>>= 0
167
+ this.mem = new DataView(this._inst.exports.mem.buffer)
168
+ },
169
+
170
+ // func nanotime1() int64
171
+ 'runtime.nanotime1': (sp) => {
172
+ sp >>>= 0
173
+ setInt64(sp + 8, (timeOrigin + performance.now()) * 1000000)
174
+ },
175
+
176
+ // func walltime() (sec int64, nsec int32)
177
+ 'runtime.walltime': (sp) => {
178
+ sp >>>= 0
179
+ const msec = new Date().getTime()
180
+ setInt64(sp + 8, msec / 1000)
181
+ this.mem.setInt32(sp + 16, (msec % 1000) * 1000000, true)
182
+ },
183
+
184
+ // func scheduleTimeoutEvent(delay int64) int32
185
+ 'runtime.scheduleTimeoutEvent': (sp) => {
186
+ sp >>>= 0
187
+ const id = this._nextCallbackTimeoutID
188
+ this._nextCallbackTimeoutID++
189
+ this._scheduledTimeouts.set(
190
+ id,
191
+ setTimeout(
192
+ () => {
193
+ this._resume()
194
+ while (this._scheduledTimeouts.has(id)) {
195
+ // for some reason Go failed to register the timeout event, log and try again
196
+ // (temporary workaround for https://github.com/golang/go/issues/28975)
197
+ console.warn('scheduleTimeoutEvent: missed timeout event')
198
+ this._resume()
199
+ }
200
+ },
201
+ getInt64(sp + 8),
202
+ ),
203
+ )
204
+ this.mem.setInt32(sp + 16, id, true)
205
+ },
206
+
207
+ // func clearTimeoutEvent(id int32)
208
+ 'runtime.clearTimeoutEvent': (sp) => {
209
+ sp >>>= 0
210
+ const id = this.mem.getInt32(sp + 8, true)
211
+ clearTimeout(this._scheduledTimeouts.get(id))
212
+ this._scheduledTimeouts.delete(id)
213
+ },
214
+
215
+ // func getRandomData(r []byte)
216
+ 'runtime.getRandomData': (sp) => {
217
+ sp >>>= 0
218
+ crypto.getRandomValues(loadSlice(sp + 8))
219
+ },
220
+
221
+ // func finalizeRef(v ref)
222
+ 'syscall/js.finalizeRef': (sp) => {
223
+ sp >>>= 0
224
+ const id = this.mem.getUint32(sp + 8, true)
225
+ this._goRefCounts[id]--
226
+ if (this._goRefCounts[id] === 0) {
227
+ const v = this._values[id]
228
+ this._values[id] = null
229
+ this._ids.delete(v)
230
+ this._idPool.push(id)
231
+ }
232
+ },
233
+
234
+ // func stringVal(value string) ref
235
+ 'syscall/js.stringVal': (sp) => {
236
+ sp >>>= 0
237
+ storeValue(sp + 24, loadString(sp + 8))
238
+ },
239
+
240
+ // func valueGet(v ref, p string) ref
241
+ 'syscall/js.valueGet': (sp) => {
242
+ sp >>>= 0
243
+ const result = Reflect.get(loadValue(sp + 8), loadString(sp + 16))
244
+ sp = this._inst.exports.getsp() >>> 0 // see comment above
245
+ storeValue(sp + 32, result)
246
+ },
247
+
248
+ // func valueSet(v ref, p string, x ref)
249
+ 'syscall/js.valueSet': (sp) => {
250
+ sp >>>= 0
251
+ Reflect.set(
252
+ loadValue(sp + 8),
253
+ loadString(sp + 16),
254
+ loadValue(sp + 32),
255
+ )
256
+ },
257
+
258
+ // func valueDelete(v ref, p string)
259
+ 'syscall/js.valueDelete': (sp) => {
260
+ sp >>>= 0
261
+ Reflect.deleteProperty(loadValue(sp + 8), loadString(sp + 16))
262
+ },
263
+
264
+ // func valueIndex(v ref, i int) ref
265
+ 'syscall/js.valueIndex': (sp) => {
266
+ sp >>>= 0
267
+ storeValue(sp + 24, Reflect.get(loadValue(sp + 8), getInt64(sp + 16)))
268
+ },
269
+
270
+ // valueSetIndex(v ref, i int, x ref)
271
+ 'syscall/js.valueSetIndex': (sp) => {
272
+ sp >>>= 0
273
+ Reflect.set(loadValue(sp + 8), getInt64(sp + 16), loadValue(sp + 24))
274
+ },
275
+
276
+ // func valueCall(v ref, m string, args []ref) (ref, bool)
277
+ 'syscall/js.valueCall': (sp) => {
278
+ sp >>>= 0
279
+ try {
280
+ const v = loadValue(sp + 8)
281
+ const m = Reflect.get(v, loadString(sp + 16))
282
+ const args = loadSliceOfValues(sp + 32)
283
+ const result = Reflect.apply(m, v, args)
284
+ sp = this._inst.exports.getsp() >>> 0 // see comment above
285
+ storeValue(sp + 56, result)
286
+ this.mem.setUint8(sp + 64, 1)
287
+ } catch (err) {
288
+ sp = this._inst.exports.getsp() >>> 0 // see comment above
289
+ storeValue(sp + 56, err)
290
+ this.mem.setUint8(sp + 64, 0)
291
+ }
292
+ },
293
+
294
+ // func valueInvoke(v ref, args []ref) (ref, bool)
295
+ 'syscall/js.valueInvoke': (sp) => {
296
+ sp >>>= 0
297
+ try {
298
+ const v = loadValue(sp + 8)
299
+ const args = loadSliceOfValues(sp + 16)
300
+ const result = Reflect.apply(v, undefined, args)
301
+ sp = this._inst.exports.getsp() >>> 0 // see comment above
302
+ storeValue(sp + 40, result)
303
+ this.mem.setUint8(sp + 48, 1)
304
+ } catch (err) {
305
+ sp = this._inst.exports.getsp() >>> 0 // see comment above
306
+ storeValue(sp + 40, err)
307
+ this.mem.setUint8(sp + 48, 0)
308
+ }
309
+ },
310
+
311
+ // func valueNew(v ref, args []ref) (ref, bool)
312
+ 'syscall/js.valueNew': (sp) => {
313
+ sp >>>= 0
314
+ try {
315
+ const v = loadValue(sp + 8)
316
+ const args = loadSliceOfValues(sp + 16)
317
+ const result = Reflect.construct(v, args)
318
+ sp = this._inst.exports.getsp() >>> 0 // see comment above
319
+ storeValue(sp + 40, result)
320
+ this.mem.setUint8(sp + 48, 1)
321
+ } catch (err) {
322
+ sp = this._inst.exports.getsp() >>> 0 // see comment above
323
+ storeValue(sp + 40, err)
324
+ this.mem.setUint8(sp + 48, 0)
325
+ }
326
+ },
327
+
328
+ // func valueLength(v ref) int
329
+ 'syscall/js.valueLength': (sp) => {
330
+ sp >>>= 0
331
+ setInt64(sp + 16, parseInt(loadValue(sp + 8).length))
332
+ },
333
+
334
+ // valuePrepareString(v ref) (ref, int)
335
+ 'syscall/js.valuePrepareString': (sp) => {
336
+ sp >>>= 0
337
+ const str = encoder.encode(String(loadValue(sp + 8)))
338
+ storeValue(sp + 16, str)
339
+ setInt64(sp + 24, str.length)
340
+ },
341
+
342
+ // valueLoadString(v ref, b []byte)
343
+ 'syscall/js.valueLoadString': (sp) => {
344
+ sp >>>= 0
345
+ const str = loadValue(sp + 8)
346
+ loadSlice(sp + 16).set(str)
347
+ },
348
+
349
+ // func valueInstanceOf(v ref, t ref) bool
350
+ 'syscall/js.valueInstanceOf': (sp) => {
351
+ sp >>>= 0
352
+ this.mem.setUint8(
353
+ sp + 24,
354
+ loadValue(sp + 8) instanceof loadValue(sp + 16) ? 1 : 0,
355
+ )
356
+ },
357
+
358
+ // func copyBytesToGo(dst []byte, src ref) (int, bool)
359
+ 'syscall/js.copyBytesToGo': (sp) => {
360
+ sp >>>= 0
361
+ const dst = loadSlice(sp + 8)
362
+ const src = loadValue(sp + 32)
363
+ if (
364
+ !(src instanceof Uint8Array || src instanceof Uint8ClampedArray)
365
+ ) {
366
+ this.mem.setUint8(sp + 48, 0)
367
+ return
368
+ }
369
+ const toCopy = src.subarray(0, dst.length)
370
+ dst.set(toCopy)
371
+ setInt64(sp + 40, toCopy.length)
372
+ this.mem.setUint8(sp + 48, 1)
373
+ },
374
+
375
+ // func copyBytesToJS(dst ref, src []byte) (int, bool)
376
+ 'syscall/js.copyBytesToJS': (sp) => {
377
+ sp >>>= 0
378
+ const dst = loadValue(sp + 8)
379
+ const src = loadSlice(sp + 16)
380
+ if (
381
+ !(dst instanceof Uint8Array || dst instanceof Uint8ClampedArray)
382
+ ) {
383
+ this.mem.setUint8(sp + 48, 0)
384
+ return
385
+ }
386
+ const toCopy = src.subarray(0, dst.length)
387
+ dst.set(toCopy)
388
+ setInt64(sp + 40, toCopy.length)
389
+ this.mem.setUint8(sp + 48, 1)
390
+ },
391
+
392
+ debug: (value) => {
393
+ console.log(value)
394
+ },
395
+ },
396
+ }
397
+ }
398
+
399
+ async run(instance) {
400
+ if (!(instance instanceof WebAssembly.Instance)) {
401
+ throw new Error('Go.run: WebAssembly.Instance expected')
402
+ }
403
+ this._inst = instance
404
+ this.mem = new DataView(this._inst.exports.mem.buffer)
405
+ this._values = [
406
+ // JS values that Go currently has references to, indexed by reference id
407
+ NaN,
408
+ 0,
409
+ null,
410
+ true,
411
+ false,
412
+ globalThis,
413
+ this,
414
+ ]
415
+ this._goRefCounts = new Array(this._values.length).fill(Infinity) // number of references that Go has to a JS value, indexed by reference id
416
+ this._ids = new Map([
417
+ // mapping from JS values to reference ids
418
+ [0, 1],
419
+ [null, 2],
420
+ [true, 3],
421
+ [false, 4],
422
+ [globalThis, 5],
423
+ [this, 6],
424
+ ])
425
+ this._idPool = [] // unused ids that have been garbage collected
426
+ this.exited = false // whether the Go program has exited
427
+
428
+ // Pass command line arguments and environment variables to WebAssembly by writing them to the linear memory.
429
+ let offset = 4096
430
+
431
+ const strPtr = (str) => {
432
+ const ptr = offset
433
+ const bytes = encoder.encode(str + '\0')
434
+ new Uint8Array(this.mem.buffer, offset, bytes.length).set(bytes)
435
+ offset += bytes.length
436
+ if (offset % 8 !== 0) {
437
+ offset += 8 - (offset % 8)
438
+ }
439
+ return ptr
440
+ }
441
+
442
+ const argc = this.argv.length
443
+
444
+ const argvPtrs = []
445
+ this.argv.forEach((arg) => {
446
+ argvPtrs.push(strPtr(arg))
447
+ })
448
+ argvPtrs.push(0)
449
+
450
+ const keys = Object.keys(this.env).sort()
451
+ keys.forEach((key) => {
452
+ argvPtrs.push(strPtr(`${key}=${this.env[key]}`))
453
+ })
454
+ argvPtrs.push(0)
455
+
456
+ const argv = offset
457
+ argvPtrs.forEach((ptr) => {
458
+ this.mem.setUint32(offset, ptr, true)
459
+ this.mem.setUint32(offset + 4, 0, true)
460
+ offset += 8
461
+ })
462
+
463
+ // The linker guarantees global data starts from at least wasmMinDataAddr.
464
+ // Keep in sync with cmd/link/internal/ld/data.go:wasmMinDataAddr.
465
+ const wasmMinDataAddr = 4096 + 8192
466
+ if (offset >= wasmMinDataAddr) {
467
+ throw new Error(
468
+ 'total length of command line and environment variables exceeds limit',
469
+ )
470
+ }
471
+
472
+ this._inst.exports.run(argc, argv)
473
+ if (this.exited) {
474
+ this._resolveExitPromise()
475
+ }
476
+ await this._exitPromise
477
+ }
478
+
479
+ _resume() {
480
+ if (this.exited) {
481
+ throw new Error('Go program has already exited')
482
+ }
483
+ this._inst.exports.resume()
484
+ if (this.exited) {
485
+ this._resolveExitPromise()
486
+ }
487
+ }
488
+
489
+ _makeFuncWrapper(id) {
490
+ const go = this
491
+ return function () {
492
+ const event = { id: id, this: this, args: arguments }
493
+ go._pendingEvent = event
494
+ go._resume()
495
+ return event.result
496
+ }
497
+ }
498
+ }
499
+
500
+ const go = new Go()
501
+ go.argv = process.argv.slice(1)
502
+ go.env = Object.assign({ TMPDIR: tmpdir() }, process.env)
503
+ go.exit = process.exit
504
+ await WebAssembly.instantiate(
505
+ fs.readFileSync(path.resolve(filename, '../tsgo.wasm')),
506
+ go.importObject,
507
+ )
508
+ .then((result) => {
509
+ process.on('exit', (code) => {
510
+ // Node.js exits if no event handler is pending
511
+ if (code === 0 && !go.exited) {
512
+ // deadlock, make Go print error and stack traces
513
+ go._pendingEvent = { id: 0 }
514
+ go._resume()
515
+ }
516
+ })
517
+ return go.run(result.instance)
518
+ })
519
+ .catch((error) => {
520
+ console.error(error)
521
+ process.exit(1)
522
+ })
package/tsgo.wasm CHANGED
Binary file