quantum-coin-js-sdk 1.0.39 → 2.1.0

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.
Files changed (226) hide show
  1. package/.gitignore +2 -1
  2. package/README.md +109 -768
  3. package/index.d.ts +76 -545
  4. package/index.js +319 -1194
  5. package/package.json +20 -5
  6. package/wasmBase64.js +7 -16
  7. package/wasm_exec.js +575 -587
  8. package/.idea/.name +0 -1
  9. package/.idea/modules.xml +0 -8
  10. package/.idea/quantum-coin-js-sdk.iml +0 -12
  11. package/.idea/vcs.xml +0 -6
  12. package/.vs/ProjectSettings.json +0 -3
  13. package/.vs/VSWorkspaceState.json +0 -8
  14. package/.vs/quantum-coin-js-sdk/FileContentIndex/a9d6de82-cfc3-4c22-ac38-06d706ff3673.vsidx +0 -0
  15. package/.vs/quantum-coin-js-sdk/FileContentIndex/fdc816df-1898-4153-99f2-c69ef4c2283b.vsidx +0 -0
  16. package/.vs/quantum-coin-js-sdk/config/applicationhost.config +0 -1011
  17. package/.vs/quantum-coin-js-sdk/v17/.wsuo +0 -0
  18. package/.vs/quantum-coin-js-sdk/v17/DocumentLayout.backup.json +0 -39
  19. package/.vs/quantum-coin-js-sdk/v17/DocumentLayout.json +0 -57
  20. package/.vs/slnx.sqlite +0 -0
  21. package/example/README.md +0 -14
  22. package/example/conversion-example.js +0 -19
  23. package/example/example-create-contract.js +0 -396
  24. package/example/example-encode-decode-rlp.js +0 -225
  25. package/example/example-event-pack-unpack.js +0 -391
  26. package/example/example-misc.js +0 -100
  27. package/example/example-rpc-send-signRawTransaction.js +0 -318
  28. package/example/example-rpc-send.js +0 -115
  29. package/example/example-send.js +0 -69
  30. package/example/example-token-pack-unpack.js +0 -960
  31. package/example/example-wallet-version4.js +0 -34
  32. package/example/example-wallet.js +0 -43
  33. package/example/example.js +0 -405
  34. package/example/node_modules/.bin/sdkgen +0 -16
  35. package/example/node_modules/.bin/sdkgen.cmd +0 -17
  36. package/example/node_modules/.bin/sdkgen.ps1 +0 -28
  37. package/example/node_modules/.package-lock.json +0 -47
  38. package/example/node_modules/quantumcoin/.github/workflows/publish-npmjs.yaml +0 -22
  39. package/example/node_modules/quantumcoin/LICENSE +0 -21
  40. package/example/node_modules/quantumcoin/README-SDK.md +0 -828
  41. package/example/node_modules/quantumcoin/README.md +0 -197
  42. package/example/node_modules/quantumcoin/config.d.ts +0 -50
  43. package/example/node_modules/quantumcoin/config.js +0 -123
  44. package/example/node_modules/quantumcoin/examples/AllSolidityTypes.sol +0 -184
  45. package/example/node_modules/quantumcoin/examples/SimpleIERC20.sol +0 -74
  46. package/example/node_modules/quantumcoin/examples/events.js +0 -41
  47. package/example/node_modules/quantumcoin/examples/events.ts +0 -35
  48. package/example/node_modules/quantumcoin/examples/example-generator-sdk-js.js +0 -100
  49. package/example/node_modules/quantumcoin/examples/example-generator-sdk-js.ts +0 -77
  50. package/example/node_modules/quantumcoin/examples/example-generator-sdk-ts.js +0 -100
  51. package/example/node_modules/quantumcoin/examples/example-generator-sdk-ts.ts +0 -77
  52. package/example/node_modules/quantumcoin/examples/example.js +0 -67
  53. package/example/node_modules/quantumcoin/examples/example.ts +0 -56
  54. package/example/node_modules/quantumcoin/examples/offline-signing.js +0 -77
  55. package/example/node_modules/quantumcoin/examples/offline-signing.ts +0 -65
  56. package/example/node_modules/quantumcoin/examples/package-lock.json +0 -596
  57. package/example/node_modules/quantumcoin/examples/package.json +0 -32
  58. package/example/node_modules/quantumcoin/examples/read-operations.js +0 -32
  59. package/example/node_modules/quantumcoin/examples/read-operations.ts +0 -31
  60. package/example/node_modules/quantumcoin/examples/sdk-generator-erc20.inline.json +0 -251
  61. package/example/node_modules/quantumcoin/examples/solidity-types.ts +0 -43
  62. package/example/node_modules/quantumcoin/examples/wallet-offline.js +0 -36
  63. package/example/node_modules/quantumcoin/examples/wallet-offline.ts +0 -26
  64. package/example/node_modules/quantumcoin/generate-sdk.js +0 -1846
  65. package/example/node_modules/quantumcoin/index.js +0 -12
  66. package/example/node_modules/quantumcoin/package.json +0 -95
  67. package/example/node_modules/quantumcoin/scripts/copy-declarations.js +0 -31
  68. package/example/node_modules/quantumcoin/scripts/run-all-one-by-one.js +0 -151
  69. package/example/node_modules/quantumcoin/src/abi/fragments.d.ts +0 -42
  70. package/example/node_modules/quantumcoin/src/abi/fragments.js +0 -63
  71. package/example/node_modules/quantumcoin/src/abi/index.d.ts +0 -13
  72. package/example/node_modules/quantumcoin/src/abi/index.js +0 -9
  73. package/example/node_modules/quantumcoin/src/abi/interface.d.ts +0 -128
  74. package/example/node_modules/quantumcoin/src/abi/interface.js +0 -605
  75. package/example/node_modules/quantumcoin/src/abi/js-abi-coder.d.ts +0 -8
  76. package/example/node_modules/quantumcoin/src/abi/js-abi-coder.js +0 -538
  77. package/example/node_modules/quantumcoin/src/constants.d.ts +0 -61
  78. package/example/node_modules/quantumcoin/src/constants.js +0 -94
  79. package/example/node_modules/quantumcoin/src/contract/contract-factory.d.ts +0 -28
  80. package/example/node_modules/quantumcoin/src/contract/contract-factory.js +0 -111
  81. package/example/node_modules/quantumcoin/src/contract/contract.d.ts +0 -113
  82. package/example/node_modules/quantumcoin/src/contract/contract.js +0 -408
  83. package/example/node_modules/quantumcoin/src/contract/index.d.ts +0 -9
  84. package/example/node_modules/quantumcoin/src/contract/index.js +0 -9
  85. package/example/node_modules/quantumcoin/src/errors/index.d.ts +0 -92
  86. package/example/node_modules/quantumcoin/src/errors/index.js +0 -200
  87. package/example/node_modules/quantumcoin/src/generator/index.d.ts +0 -81
  88. package/example/node_modules/quantumcoin/src/generator/index.js +0 -1571
  89. package/example/node_modules/quantumcoin/src/index.d.ts +0 -122
  90. package/example/node_modules/quantumcoin/src/index.js +0 -41
  91. package/example/node_modules/quantumcoin/src/internal/hex.d.ts +0 -68
  92. package/example/node_modules/quantumcoin/src/internal/hex.js +0 -180
  93. package/example/node_modules/quantumcoin/src/providers/extra-providers.d.ts +0 -139
  94. package/example/node_modules/quantumcoin/src/providers/extra-providers.js +0 -614
  95. package/example/node_modules/quantumcoin/src/providers/index.d.ts +0 -17
  96. package/example/node_modules/quantumcoin/src/providers/index.js +0 -10
  97. package/example/node_modules/quantumcoin/src/providers/json-rpc-provider.d.ts +0 -12
  98. package/example/node_modules/quantumcoin/src/providers/json-rpc-provider.js +0 -89
  99. package/example/node_modules/quantumcoin/src/providers/provider.d.ts +0 -206
  100. package/example/node_modules/quantumcoin/src/providers/provider.js +0 -524
  101. package/example/node_modules/quantumcoin/src/types/index.d.ts +0 -214
  102. package/example/node_modules/quantumcoin/src/types/index.js +0 -9
  103. package/example/node_modules/quantumcoin/src/utils/address.d.ts +0 -58
  104. package/example/node_modules/quantumcoin/src/utils/address.js +0 -144
  105. package/example/node_modules/quantumcoin/src/utils/encoding.d.ts +0 -120
  106. package/example/node_modules/quantumcoin/src/utils/encoding.js +0 -306
  107. package/example/node_modules/quantumcoin/src/utils/fixednumber.d.ts +0 -57
  108. package/example/node_modules/quantumcoin/src/utils/fixednumber.js +0 -366
  109. package/example/node_modules/quantumcoin/src/utils/hashing.d.ts +0 -76
  110. package/example/node_modules/quantumcoin/src/utils/hashing.js +0 -298
  111. package/example/node_modules/quantumcoin/src/utils/index.d.ts +0 -63
  112. package/example/node_modules/quantumcoin/src/utils/index.js +0 -14
  113. package/example/node_modules/quantumcoin/src/utils/result.d.ts +0 -57
  114. package/example/node_modules/quantumcoin/src/utils/result.js +0 -128
  115. package/example/node_modules/quantumcoin/src/utils/rlp.d.ts +0 -12
  116. package/example/node_modules/quantumcoin/src/utils/rlp.js +0 -215
  117. package/example/node_modules/quantumcoin/src/utils/units.d.ts +0 -29
  118. package/example/node_modules/quantumcoin/src/utils/units.js +0 -107
  119. package/example/node_modules/quantumcoin/src/wallet/index.d.ts +0 -10
  120. package/example/node_modules/quantumcoin/src/wallet/index.js +0 -8
  121. package/example/node_modules/quantumcoin/src/wallet/wallet.d.ts +0 -192
  122. package/example/node_modules/quantumcoin/src/wallet/wallet.js +0 -713
  123. package/example/node_modules/quantumcoin/test/e2e/all-solidity-types.dynamic.test.js +0 -207
  124. package/example/node_modules/quantumcoin/test/e2e/all-solidity-types.dynamic.test.ts +0 -191
  125. package/example/node_modules/quantumcoin/test/e2e/all-solidity-types.fixtures.js +0 -231
  126. package/example/node_modules/quantumcoin/test/e2e/all-solidity-types.generated-sdks.e2e.test.js +0 -387
  127. package/example/node_modules/quantumcoin/test/e2e/all-solidity-types.generated-sdks.e2e.test.ts +0 -350
  128. package/example/node_modules/quantumcoin/test/e2e/generator-interface.e2e.test.js +0 -165
  129. package/example/node_modules/quantumcoin/test/e2e/generator-interface.e2e.test.ts +0 -160
  130. package/example/node_modules/quantumcoin/test/e2e/helpers.js +0 -59
  131. package/example/node_modules/quantumcoin/test/e2e/signing-context-and-fee.e2e.test.js +0 -141
  132. package/example/node_modules/quantumcoin/test/e2e/signing-context-and-fee.e2e.test.ts +0 -128
  133. package/example/node_modules/quantumcoin/test/e2e/simple-erc20.generated-sdks.e2e.test.js +0 -168
  134. package/example/node_modules/quantumcoin/test/e2e/simple-erc20.generated-sdks.e2e.test.ts +0 -141
  135. package/example/node_modules/quantumcoin/test/e2e/transactional.test.js +0 -245
  136. package/example/node_modules/quantumcoin/test/e2e/transactional.test.ts +0 -208
  137. package/example/node_modules/quantumcoin/test/e2e/typed-generator.e2e.test.js +0 -407
  138. package/example/node_modules/quantumcoin/test/e2e/typed-generator.e2e.test.ts +0 -337
  139. package/example/node_modules/quantumcoin/test/fixtures/ConstructorParam.sol +0 -23
  140. package/example/node_modules/quantumcoin/test/fixtures/MultiContracts.sol +0 -37
  141. package/example/node_modules/quantumcoin/test/fixtures/SimpleStorage.sol +0 -18
  142. package/example/node_modules/quantumcoin/test/fixtures/StakingContract.abi.json +0 -1
  143. package/example/node_modules/quantumcoin/test/integration/ipc-provider.test.js +0 -49
  144. package/example/node_modules/quantumcoin/test/integration/ipc-provider.test.ts +0 -44
  145. package/example/node_modules/quantumcoin/test/integration/provider.test.js +0 -88
  146. package/example/node_modules/quantumcoin/test/integration/provider.test.ts +0 -85
  147. package/example/node_modules/quantumcoin/test/integration/ws-provider.test.js +0 -41
  148. package/example/node_modules/quantumcoin/test/integration/ws-provider.test.ts +0 -38
  149. package/example/node_modules/quantumcoin/test/security/abi-decoder-bounds.test.js +0 -122
  150. package/example/node_modules/quantumcoin/test/security/contract-overrides.test.js +0 -112
  151. package/example/node_modules/quantumcoin/test/security/generator-injection.test.js +0 -195
  152. package/example/node_modules/quantumcoin/test/security/malformed-input.test.js +0 -330
  153. package/example/node_modules/quantumcoin/test/security/malformed-input.test.ts +0 -35
  154. package/example/node_modules/quantumcoin/test/security/rpc-numeric-bounds.test.js +0 -81
  155. package/example/node_modules/quantumcoin/test/security/rpc-trust.test.js +0 -202
  156. package/example/node_modules/quantumcoin/test/unit/_encrypted-output.txt +0 -6
  157. package/example/node_modules/quantumcoin/test/unit/_log-encrypted-jsons.js +0 -45
  158. package/example/node_modules/quantumcoin/test/unit/_write-keystore-fixture.js +0 -16
  159. package/example/node_modules/quantumcoin/test/unit/abi-interface.test.js +0 -110
  160. package/example/node_modules/quantumcoin/test/unit/abi-interface.test.ts +0 -109
  161. package/example/node_modules/quantumcoin/test/unit/address-wallet.test.js +0 -923
  162. package/example/node_modules/quantumcoin/test/unit/address-wallet.test.ts +0 -877
  163. package/example/node_modules/quantumcoin/test/unit/browser-provider.test.js +0 -85
  164. package/example/node_modules/quantumcoin/test/unit/browser-provider.test.ts +0 -79
  165. package/example/node_modules/quantumcoin/test/unit/contract.test.js +0 -85
  166. package/example/node_modules/quantumcoin/test/unit/contract.test.ts +0 -83
  167. package/example/node_modules/quantumcoin/test/unit/encoding-units-rlp.test.js +0 -127
  168. package/example/node_modules/quantumcoin/test/unit/encoding-units-rlp.test.ts +0 -91
  169. package/example/node_modules/quantumcoin/test/unit/errors.test.js +0 -77
  170. package/example/node_modules/quantumcoin/test/unit/errors.test.ts +0 -76
  171. package/example/node_modules/quantumcoin/test/unit/filter-by-blockhash.test.js +0 -55
  172. package/example/node_modules/quantumcoin/test/unit/filter-by-blockhash.test.ts +0 -54
  173. package/example/node_modules/quantumcoin/test/unit/fixednumber.test.js +0 -656
  174. package/example/node_modules/quantumcoin/test/unit/fixednumber.test.ts +0 -660
  175. package/example/node_modules/quantumcoin/test/unit/fixtures/encrypted-keystores-48-32-36.js +0 -9
  176. package/example/node_modules/quantumcoin/test/unit/generate-contract-cli.test.js +0 -42
  177. package/example/node_modules/quantumcoin/test/unit/generate-contract-cli.test.ts +0 -41
  178. package/example/node_modules/quantumcoin/test/unit/generate-sdk-artifacts-json.test.js +0 -113
  179. package/example/node_modules/quantumcoin/test/unit/generate-sdk-artifacts-json.test.ts +0 -110
  180. package/example/node_modules/quantumcoin/test/unit/generator.test.js +0 -149
  181. package/example/node_modules/quantumcoin/test/unit/generator.test.ts +0 -148
  182. package/example/node_modules/quantumcoin/test/unit/hashing.test.js +0 -64
  183. package/example/node_modules/quantumcoin/test/unit/hashing.test.ts +0 -63
  184. package/example/node_modules/quantumcoin/test/unit/init.test.js +0 -39
  185. package/example/node_modules/quantumcoin/test/unit/init.test.ts +0 -38
  186. package/example/node_modules/quantumcoin/test/unit/interface.test.js +0 -56
  187. package/example/node_modules/quantumcoin/test/unit/interface.test.ts +0 -54
  188. package/example/node_modules/quantumcoin/test/unit/internal-hex.test.js +0 -81
  189. package/example/node_modules/quantumcoin/test/unit/internal-hex.test.ts +0 -80
  190. package/example/node_modules/quantumcoin/test/unit/populate-transaction.test.js +0 -98
  191. package/example/node_modules/quantumcoin/test/unit/populate-transaction.test.ts +0 -64
  192. package/example/node_modules/quantumcoin/test/unit/providers.test.js +0 -252
  193. package/example/node_modules/quantumcoin/test/unit/providers.test.ts +0 -251
  194. package/example/node_modules/quantumcoin/test/unit/result.test.js +0 -80
  195. package/example/node_modules/quantumcoin/test/unit/result.test.ts +0 -79
  196. package/example/node_modules/quantumcoin/test/unit/solidity-types.test.js +0 -49
  197. package/example/node_modules/quantumcoin/test/unit/solidity-types.test.ts +0 -39
  198. package/example/node_modules/quantumcoin/test/unit/utils.test.js +0 -57
  199. package/example/node_modules/quantumcoin/test/unit/utils.test.ts +0 -56
  200. package/example/node_modules/quantumcoin/test/verbose-logger.js +0 -74
  201. package/example/node_modules/quantumcoin/tsconfig.build.json +0 -14
  202. package/example/node_modules/seed-words/.github/workflows/publish-npmjs.yaml +0 -22
  203. package/example/node_modules/seed-words/BUILD.md +0 -7
  204. package/example/node_modules/seed-words/LICENSE +0 -121
  205. package/example/node_modules/seed-words/README.md +0 -67
  206. package/example/node_modules/seed-words/dist/seedwords.d.ts +0 -39
  207. package/example/node_modules/seed-words/package.json +0 -27
  208. package/example/node_modules/seed-words/seedwords.js +0 -315
  209. package/example/node_modules/seed-words/seedwords.txt +0 -65536
  210. package/example/node_modules/seed-words/tsconfig.json +0 -21
  211. package/example/package-lock.json +0 -56
  212. package/example/package.json +0 -15
  213. package/tests/encrypted-32.json +0 -1
  214. package/tests/encrypted-36.json +0 -1
  215. package/tests/encrypted-48.json +0 -1
  216. package/tests/generate-verify-vectors.js +0 -91
  217. package/tests/get-gas-price.test.js +0 -59
  218. package/tests/non-transactional.preinit.test.js +0 -41
  219. package/tests/non-transactional.test.js +0 -1389
  220. package/tests/scrypt.test.js +0 -128
  221. package/tests/sign-raw-keytype5-context-null.test.js +0 -107
  222. package/tests/sign-raw-transaction.test.js +0 -196
  223. package/tests/sign-verify.test.js +0 -311
  224. package/tests/transactional.relay.test.js +0 -131
  225. package/tests/transactional.rpc.test.js +0 -103
  226. package/tests/verify-vectors.json +0 -95035
package/wasm_exec.js CHANGED
@@ -1,587 +1,575 @@
1
- /*
2
- Copyright 2009 The Go Authors.
3
-
4
- Redistribution and use in source and binary forms, with or without
5
- modification, are permitted provided that the following conditions are
6
- met:
7
-
8
- * Redistributions of source code must retain the above copyright
9
- notice, this list of conditions and the following disclaimer.
10
- * Redistributions in binary form must reproduce the above
11
- copyright notice, this list of conditions and the following disclaimer
12
- in the documentation and/or other materials provided with the
13
- distribution.
14
- * Neither the name of Google LLC nor the names of its
15
- contributors may be used to endorse or promote products derived from
16
- this software without specific prior written permission.
17
-
18
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
- */
30
-
31
- "use strict";
32
-
33
- (() => {
34
- const enosys = () => {
35
- const err = new Error("not implemented");
36
- err.code = "ENOSYS";
37
- return err;
38
- };
39
-
40
- if (!globalThis.fs) {
41
- let outputBuf = "";
42
- globalThis.fs = {
43
- constants: { O_WRONLY: -1, O_RDWR: -1, O_CREAT: -1, O_TRUNC: -1, O_APPEND: -1, O_EXCL: -1 }, // unused
44
- writeSync(fd, buf) {
45
- outputBuf += decoder.decode(buf);
46
- const nl = outputBuf.lastIndexOf("\n");
47
- if (nl != -1) {
48
- console.log(outputBuf.substring(0, nl));
49
- outputBuf = outputBuf.substring(nl + 1);
50
- }
51
- return buf.length;
52
- },
53
- write(fd, buf, offset, length, position, callback) {
54
- if (offset !== 0 || length !== buf.length || position !== null) {
55
- callback(enosys());
56
- return;
57
- }
58
- const n = this.writeSync(fd, buf);
59
- callback(null, n);
60
- },
61
- chmod(path, mode, callback) { callback(enosys()); },
62
- chown(path, uid, gid, callback) { callback(enosys()); },
63
- close(fd, callback) { callback(enosys()); },
64
- fchmod(fd, mode, callback) { callback(enosys()); },
65
- fchown(fd, uid, gid, callback) { callback(enosys()); },
66
- fstat(fd, callback) { callback(enosys()); },
67
- fsync(fd, callback) { callback(null); },
68
- ftruncate(fd, length, callback) { callback(enosys()); },
69
- lchown(path, uid, gid, callback) { callback(enosys()); },
70
- link(path, link, callback) { callback(enosys()); },
71
- lstat(path, callback) { callback(enosys()); },
72
- mkdir(path, perm, callback) { callback(enosys()); },
73
- open(path, flags, mode, callback) { callback(enosys()); },
74
- read(fd, buffer, offset, length, position, callback) { callback(enosys()); },
75
- readdir(path, callback) { callback(enosys()); },
76
- readlink(path, callback) { callback(enosys()); },
77
- rename(from, to, callback) { callback(enosys()); },
78
- rmdir(path, callback) { callback(enosys()); },
79
- stat(path, callback) { callback(enosys()); },
80
- symlink(path, link, callback) { callback(enosys()); },
81
- truncate(path, length, callback) { callback(enosys()); },
82
- unlink(path, callback) { callback(enosys()); },
83
- utimes(path, atime, mtime, callback) { callback(enosys()); },
84
- };
85
- }
86
-
87
- if (!globalThis.process) {
88
- globalThis.process = {
89
- getuid() { return -1; },
90
- getgid() { return -1; },
91
- geteuid() { return -1; },
92
- getegid() { return -1; },
93
- getgroups() { throw enosys(); },
94
- pid: -1,
95
- ppid: -1,
96
- umask() { throw enosys(); },
97
- cwd() { throw enosys(); },
98
- chdir() { throw enosys(); },
99
- }
100
- }
101
-
102
- if (!globalThis.crypto) {
103
- throw new Error("globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)");
104
- }
105
-
106
- if (!globalThis.performance) {
107
- throw new Error("globalThis.performance is not available, polyfill required (performance.now only)");
108
- }
109
-
110
- if (!globalThis.TextEncoder) {
111
- throw new Error("globalThis.TextEncoder is not available, polyfill required");
112
- }
113
-
114
- if (!globalThis.TextDecoder) {
115
- throw new Error("globalThis.TextDecoder is not available, polyfill required");
116
- }
117
-
118
- const encoder = new TextEncoder("utf-8");
119
- const decoder = new TextDecoder("utf-8");
120
-
121
- globalThis.Go = class {
122
- constructor() {
123
- this.argv = ["js"];
124
- this.env = {};
125
- this.exit = (code) => {
126
- if (code !== 0) {
127
- console.warn("exit code:", code);
128
- }
129
- };
130
- this._exitPromise = new Promise((resolve) => {
131
- this._resolveExitPromise = resolve;
132
- });
133
- this._pendingEvent = null;
134
- this._scheduledTimeouts = new Map();
135
- this._nextCallbackTimeoutID = 1;
136
-
137
- const setInt64 = (addr, v) => {
138
- this.mem.setUint32(addr + 0, v, true);
139
- this.mem.setUint32(addr + 4, Math.floor(v / 4294967296), true);
140
- }
141
-
142
- const setInt32 = (addr, v) => {
143
- this.mem.setUint32(addr + 0, v, true);
144
- }
145
-
146
- const getInt64 = (addr) => {
147
- const low = this.mem.getUint32(addr + 0, true);
148
- const high = this.mem.getInt32(addr + 4, true);
149
- return low + high * 4294967296;
150
- }
151
-
152
- const loadValue = (addr) => {
153
- const f = this.mem.getFloat64(addr, true);
154
- if (f === 0) {
155
- return undefined;
156
- }
157
- if (!isNaN(f)) {
158
- return f;
159
- }
160
-
161
- const id = this.mem.getUint32(addr, true);
162
- return this._values[id];
163
- }
164
-
165
- const storeValue = (addr, v) => {
166
- const nanHead = 0x7FF80000;
167
-
168
- if (typeof v === "number" && v !== 0) {
169
- if (isNaN(v)) {
170
- this.mem.setUint32(addr + 4, nanHead, true);
171
- this.mem.setUint32(addr, 0, true);
172
- return;
173
- }
174
- this.mem.setFloat64(addr, v, true);
175
- return;
176
- }
177
-
178
- if (v === undefined) {
179
- this.mem.setFloat64(addr, 0, true);
180
- return;
181
- }
182
-
183
- let id = this._ids.get(v);
184
- if (id === undefined) {
185
- id = this._idPool.pop();
186
- if (id === undefined) {
187
- id = this._values.length;
188
- }
189
- this._values[id] = v;
190
- this._goRefCounts[id] = 0;
191
- this._ids.set(v, id);
192
- }
193
- this._goRefCounts[id]++;
194
- let typeFlag = 0;
195
- switch (typeof v) {
196
- case "object":
197
- if (v !== null) {
198
- typeFlag = 1;
199
- }
200
- break;
201
- case "string":
202
- typeFlag = 2;
203
- break;
204
- case "symbol":
205
- typeFlag = 3;
206
- break;
207
- case "function":
208
- typeFlag = 4;
209
- break;
210
- }
211
- this.mem.setUint32(addr + 4, nanHead | typeFlag, true);
212
- this.mem.setUint32(addr, id, true);
213
- }
214
-
215
- const loadSlice = (addr) => {
216
- const array = getInt64(addr + 0);
217
- const len = getInt64(addr + 8);
218
- return new Uint8Array(this._inst.exports.mem.buffer, array, len);
219
- }
220
-
221
- const loadSliceOfValues = (addr) => {
222
- const array = getInt64(addr + 0);
223
- const len = getInt64(addr + 8);
224
- const a = new Array(len);
225
- for (let i = 0; i < len; i++) {
226
- a[i] = loadValue(array + i * 8);
227
- }
228
- return a;
229
- }
230
-
231
- const loadString = (addr) => {
232
- const saddr = getInt64(addr + 0);
233
- const len = getInt64(addr + 8);
234
- return decoder.decode(new DataView(this._inst.exports.mem.buffer, saddr, len));
235
- }
236
-
237
- const timeOrigin = Date.now() - performance.now();
238
- this.importObject = {
239
- _gotest: {
240
- add: (a, b) => a + b,
241
- },
242
- gojs: {
243
- // Go's SP does not change as long as no Go code is running. Some operations (e.g. calls, getters and setters)
244
- // may synchronously trigger a Go event handler. This makes Go code get executed in the middle of the imported
245
- // function. A goroutine can switch to a new stack if the current stack is too small (see morestack function).
246
- // This changes the SP, thus we have to update the SP used by the imported function.
247
-
248
- // func wasmExit(code int32)
249
- "runtime.wasmExit": (sp) => {
250
- sp >>>= 0;
251
- const code = this.mem.getInt32(sp + 8, true);
252
- this.exited = true;
253
- delete this._inst;
254
- delete this._values;
255
- delete this._goRefCounts;
256
- delete this._ids;
257
- delete this._idPool;
258
- this.exit(code);
259
- },
260
-
261
- // func wasmWrite(fd uintptr, p unsafe.Pointer, n int32)
262
- "runtime.wasmWrite": (sp) => {
263
- sp >>>= 0;
264
- const fd = getInt64(sp + 8);
265
- const p = getInt64(sp + 16);
266
- const n = this.mem.getInt32(sp + 24, true);
267
- fs.writeSync(fd, new Uint8Array(this._inst.exports.mem.buffer, p, n));
268
- },
269
-
270
- // func resetMemoryDataView()
271
- "runtime.resetMemoryDataView": (sp) => {
272
- sp >>>= 0;
273
- this.mem = new DataView(this._inst.exports.mem.buffer);
274
- },
275
-
276
- // func nanotime1() int64
277
- "runtime.nanotime1": (sp) => {
278
- sp >>>= 0;
279
- setInt64(sp + 8, (timeOrigin + performance.now()) * 1000000);
280
- },
281
-
282
- // func walltime() (sec int64, nsec int32)
283
- "runtime.walltime": (sp) => {
284
- sp >>>= 0;
285
- const msec = (new Date).getTime();
286
- setInt64(sp + 8, msec / 1000);
287
- this.mem.setInt32(sp + 16, (msec % 1000) * 1000000, true);
288
- },
289
-
290
- // func scheduleTimeoutEvent(delay int64) int32
291
- "runtime.scheduleTimeoutEvent": (sp) => {
292
- sp >>>= 0;
293
- const id = this._nextCallbackTimeoutID;
294
- this._nextCallbackTimeoutID++;
295
- this._scheduledTimeouts.set(id, setTimeout(
296
- () => {
297
- this._resume();
298
- while (this._scheduledTimeouts.has(id)) {
299
- // for some reason Go failed to register the timeout event, log and try again
300
- // (temporary workaround for https://github.com/golang/go/issues/28975)
301
- console.warn("scheduleTimeoutEvent: missed timeout event");
302
- this._resume();
303
- }
304
- },
305
- getInt64(sp + 8),
306
- ));
307
- this.mem.setInt32(sp + 16, id, true);
308
- },
309
-
310
- // func clearTimeoutEvent(id int32)
311
- "runtime.clearTimeoutEvent": (sp) => {
312
- sp >>>= 0;
313
- const id = this.mem.getInt32(sp + 8, true);
314
- clearTimeout(this._scheduledTimeouts.get(id));
315
- this._scheduledTimeouts.delete(id);
316
- },
317
-
318
- // func getRandomData(r []byte)
319
- "runtime.getRandomData": (sp) => {
320
- sp >>>= 0;
321
- crypto.getRandomValues(loadSlice(sp + 8));
322
- },
323
-
324
- // func finalizeRef(v ref)
325
- "syscall/js.finalizeRef": (sp) => {
326
- sp >>>= 0;
327
- const id = this.mem.getUint32(sp + 8, true);
328
- this._goRefCounts[id]--;
329
- if (this._goRefCounts[id] === 0) {
330
- const v = this._values[id];
331
- this._values[id] = null;
332
- this._ids.delete(v);
333
- this._idPool.push(id);
334
- }
335
- },
336
-
337
- // func stringVal(value string) ref
338
- "syscall/js.stringVal": (sp) => {
339
- sp >>>= 0;
340
- storeValue(sp + 24, loadString(sp + 8));
341
- },
342
-
343
- // func valueGet(v ref, p string) ref
344
- "syscall/js.valueGet": (sp) => {
345
- sp >>>= 0;
346
- const result = Reflect.get(loadValue(sp + 8), loadString(sp + 16));
347
- sp = this._inst.exports.getsp() >>> 0; // see comment above
348
- storeValue(sp + 32, result);
349
- },
350
-
351
- // func valueSet(v ref, p string, x ref)
352
- "syscall/js.valueSet": (sp) => {
353
- sp >>>= 0;
354
- Reflect.set(loadValue(sp + 8), loadString(sp + 16), loadValue(sp + 32));
355
- },
356
-
357
- // func valueDelete(v ref, p string)
358
- "syscall/js.valueDelete": (sp) => {
359
- sp >>>= 0;
360
- Reflect.deleteProperty(loadValue(sp + 8), loadString(sp + 16));
361
- },
362
-
363
- // func valueIndex(v ref, i int) ref
364
- "syscall/js.valueIndex": (sp) => {
365
- sp >>>= 0;
366
- storeValue(sp + 24, Reflect.get(loadValue(sp + 8), getInt64(sp + 16)));
367
- },
368
-
369
- // valueSetIndex(v ref, i int, x ref)
370
- "syscall/js.valueSetIndex": (sp) => {
371
- sp >>>= 0;
372
- Reflect.set(loadValue(sp + 8), getInt64(sp + 16), loadValue(sp + 24));
373
- },
374
-
375
- // func valueCall(v ref, m string, args []ref) (ref, bool)
376
- "syscall/js.valueCall": (sp) => {
377
- sp >>>= 0;
378
- try {
379
- const v = loadValue(sp + 8);
380
- const m = Reflect.get(v, loadString(sp + 16));
381
- const args = loadSliceOfValues(sp + 32);
382
- const result = Reflect.apply(m, v, args);
383
- sp = this._inst.exports.getsp() >>> 0; // see comment above
384
- storeValue(sp + 56, result);
385
- this.mem.setUint8(sp + 64, 1);
386
- } catch (err) {
387
- sp = this._inst.exports.getsp() >>> 0; // see comment above
388
- storeValue(sp + 56, err);
389
- this.mem.setUint8(sp + 64, 0);
390
- }
391
- },
392
-
393
- // func valueInvoke(v ref, args []ref) (ref, bool)
394
- "syscall/js.valueInvoke": (sp) => {
395
- sp >>>= 0;
396
- try {
397
- const v = loadValue(sp + 8);
398
- const args = loadSliceOfValues(sp + 16);
399
- const result = Reflect.apply(v, undefined, args);
400
- sp = this._inst.exports.getsp() >>> 0; // see comment above
401
- storeValue(sp + 40, result);
402
- this.mem.setUint8(sp + 48, 1);
403
- } catch (err) {
404
- sp = this._inst.exports.getsp() >>> 0; // see comment above
405
- storeValue(sp + 40, err);
406
- this.mem.setUint8(sp + 48, 0);
407
- }
408
- },
409
-
410
- // func valueNew(v ref, args []ref) (ref, bool)
411
- "syscall/js.valueNew": (sp) => {
412
- sp >>>= 0;
413
- try {
414
- const v = loadValue(sp + 8);
415
- const args = loadSliceOfValues(sp + 16);
416
- const result = Reflect.construct(v, args);
417
- sp = this._inst.exports.getsp() >>> 0; // see comment above
418
- storeValue(sp + 40, result);
419
- this.mem.setUint8(sp + 48, 1);
420
- } catch (err) {
421
- sp = this._inst.exports.getsp() >>> 0; // see comment above
422
- storeValue(sp + 40, err);
423
- this.mem.setUint8(sp + 48, 0);
424
- }
425
- },
426
-
427
- // func valueLength(v ref) int
428
- "syscall/js.valueLength": (sp) => {
429
- sp >>>= 0;
430
- setInt64(sp + 16, parseInt(loadValue(sp + 8).length));
431
- },
432
-
433
- // valuePrepareString(v ref) (ref, int)
434
- "syscall/js.valuePrepareString": (sp) => {
435
- sp >>>= 0;
436
- const str = encoder.encode(String(loadValue(sp + 8)));
437
- storeValue(sp + 16, str);
438
- setInt64(sp + 24, str.length);
439
- },
440
-
441
- // valueLoadString(v ref, b []byte)
442
- "syscall/js.valueLoadString": (sp) => {
443
- sp >>>= 0;
444
- const str = loadValue(sp + 8);
445
- loadSlice(sp + 16).set(str);
446
- },
447
-
448
- // func valueInstanceOf(v ref, t ref) bool
449
- "syscall/js.valueInstanceOf": (sp) => {
450
- sp >>>= 0;
451
- this.mem.setUint8(sp + 24, (loadValue(sp + 8) instanceof loadValue(sp + 16)) ? 1 : 0);
452
- },
453
-
454
- // func copyBytesToGo(dst []byte, src ref) (int, bool)
455
- "syscall/js.copyBytesToGo": (sp) => {
456
- sp >>>= 0;
457
- const dst = loadSlice(sp + 8);
458
- const src = loadValue(sp + 32);
459
- if (!(src instanceof Uint8Array || src instanceof Uint8ClampedArray)) {
460
- this.mem.setUint8(sp + 48, 0);
461
- return;
462
- }
463
- const toCopy = src.subarray(0, dst.length);
464
- dst.set(toCopy);
465
- setInt64(sp + 40, toCopy.length);
466
- this.mem.setUint8(sp + 48, 1);
467
- },
468
-
469
- // func copyBytesToJS(dst ref, src []byte) (int, bool)
470
- "syscall/js.copyBytesToJS": (sp) => {
471
- sp >>>= 0;
472
- const dst = loadValue(sp + 8);
473
- const src = loadSlice(sp + 16);
474
- if (!(dst instanceof Uint8Array || dst instanceof Uint8ClampedArray)) {
475
- this.mem.setUint8(sp + 48, 0);
476
- return;
477
- }
478
- const toCopy = src.subarray(0, dst.length);
479
- dst.set(toCopy);
480
- setInt64(sp + 40, toCopy.length);
481
- this.mem.setUint8(sp + 48, 1);
482
- },
483
-
484
- "debug": (value) => {
485
- console.log(value);
486
- },
487
- }
488
- };
489
- }
490
-
491
- async run(instance) {
492
- if (!(instance instanceof WebAssembly.Instance)) {
493
- throw new Error("Go.run: WebAssembly.Instance expected");
494
- }
495
- this._inst = instance;
496
- this.mem = new DataView(this._inst.exports.mem.buffer);
497
- this._values = [ // JS values that Go currently has references to, indexed by reference id
498
- NaN,
499
- 0,
500
- null,
501
- true,
502
- false,
503
- globalThis,
504
- this,
505
- ];
506
- this._goRefCounts = new Array(this._values.length).fill(Infinity); // number of references that Go has to a JS value, indexed by reference id
507
- this._ids = new Map([ // mapping from JS values to reference ids
508
- [0, 1],
509
- [null, 2],
510
- [true, 3],
511
- [false, 4],
512
- [globalThis, 5],
513
- [this, 6],
514
- ]);
515
- this._idPool = []; // unused ids that have been garbage collected
516
- this.exited = false; // whether the Go program has exited
517
-
518
- // Pass command line arguments and environment variables to WebAssembly by writing them to the linear memory.
519
- let offset = 4096;
520
-
521
- const strPtr = (str) => {
522
- const ptr = offset;
523
- const bytes = encoder.encode(str + "\0");
524
- new Uint8Array(this.mem.buffer, offset, bytes.length).set(bytes);
525
- offset += bytes.length;
526
- if (offset % 8 !== 0) {
527
- offset += 8 - (offset % 8);
528
- }
529
- return ptr;
530
- };
531
-
532
- const argc = this.argv.length;
533
-
534
- const argvPtrs = [];
535
- this.argv.forEach((arg) => {
536
- argvPtrs.push(strPtr(arg));
537
- });
538
- argvPtrs.push(0);
539
-
540
- const keys = Object.keys(this.env).sort();
541
- keys.forEach((key) => {
542
- argvPtrs.push(strPtr(`${key}=${this.env[key]}`));
543
- });
544
- argvPtrs.push(0);
545
-
546
- const argv = offset;
547
- argvPtrs.forEach((ptr) => {
548
- this.mem.setUint32(offset, ptr, true);
549
- this.mem.setUint32(offset + 4, 0, true);
550
- offset += 8;
551
- });
552
-
553
- // The linker guarantees global data starts from at least wasmMinDataAddr.
554
- // Keep in sync with cmd/link/internal/ld/data.go:wasmMinDataAddr.
555
- const wasmMinDataAddr = 4096 + 8192;
556
- if (offset >= wasmMinDataAddr) {
557
- throw new Error("total length of command line and environment variables exceeds limit");
558
- }
559
-
560
- this._inst.exports.run(argc, argv);
561
- if (this.exited) {
562
- this._resolveExitPromise();
563
- }
564
- await this._exitPromise;
565
- }
566
-
567
- _resume() {
568
- if (this.exited) {
569
- throw new Error("Go program has already exited");
570
- }
571
- this._inst.exports.resume();
572
- if (this.exited) {
573
- this._resolveExitPromise();
574
- }
575
- }
576
-
577
- _makeFuncWrapper(id) {
578
- const go = this;
579
- return function () {
580
- const event = { id: id, this: this, args: arguments };
581
- go._pendingEvent = event;
582
- go._resume();
583
- return event.result;
584
- };
585
- }
586
- }
587
- })();
1
+ // Copyright 2018 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ "use strict";
6
+
7
+ (() => {
8
+ const enosys = () => {
9
+ const err = new Error("not implemented");
10
+ err.code = "ENOSYS";
11
+ return err;
12
+ };
13
+
14
+ if (!globalThis.fs) {
15
+ let outputBuf = "";
16
+ globalThis.fs = {
17
+ constants: { O_WRONLY: -1, O_RDWR: -1, O_CREAT: -1, O_TRUNC: -1, O_APPEND: -1, O_EXCL: -1, O_DIRECTORY: -1 }, // unused
18
+ writeSync(fd, buf) {
19
+ outputBuf += decoder.decode(buf);
20
+ const nl = outputBuf.lastIndexOf("\n");
21
+ if (nl != -1) {
22
+ console.log(outputBuf.substring(0, nl));
23
+ outputBuf = outputBuf.substring(nl + 1);
24
+ }
25
+ return buf.length;
26
+ },
27
+ write(fd, buf, offset, length, position, callback) {
28
+ if (offset !== 0 || length !== buf.length || position !== null) {
29
+ callback(enosys());
30
+ return;
31
+ }
32
+ const n = this.writeSync(fd, buf);
33
+ callback(null, n);
34
+ },
35
+ chmod(path, mode, callback) { callback(enosys()); },
36
+ chown(path, uid, gid, callback) { callback(enosys()); },
37
+ close(fd, callback) { callback(enosys()); },
38
+ fchmod(fd, mode, callback) { callback(enosys()); },
39
+ fchown(fd, uid, gid, callback) { callback(enosys()); },
40
+ fstat(fd, callback) { callback(enosys()); },
41
+ fsync(fd, callback) { callback(null); },
42
+ ftruncate(fd, length, callback) { callback(enosys()); },
43
+ lchown(path, uid, gid, callback) { callback(enosys()); },
44
+ link(path, link, callback) { callback(enosys()); },
45
+ lstat(path, callback) { callback(enosys()); },
46
+ mkdir(path, perm, callback) { callback(enosys()); },
47
+ open(path, flags, mode, callback) { callback(enosys()); },
48
+ read(fd, buffer, offset, length, position, callback) { callback(enosys()); },
49
+ readdir(path, callback) { callback(enosys()); },
50
+ readlink(path, callback) { callback(enosys()); },
51
+ rename(from, to, callback) { callback(enosys()); },
52
+ rmdir(path, callback) { callback(enosys()); },
53
+ stat(path, callback) { callback(enosys()); },
54
+ symlink(path, link, callback) { callback(enosys()); },
55
+ truncate(path, length, callback) { callback(enosys()); },
56
+ unlink(path, callback) { callback(enosys()); },
57
+ utimes(path, atime, mtime, callback) { callback(enosys()); },
58
+ };
59
+ }
60
+
61
+ if (!globalThis.process) {
62
+ globalThis.process = {
63
+ getuid() { return -1; },
64
+ getgid() { return -1; },
65
+ geteuid() { return -1; },
66
+ getegid() { return -1; },
67
+ getgroups() { throw enosys(); },
68
+ pid: -1,
69
+ ppid: -1,
70
+ umask() { throw enosys(); },
71
+ cwd() { throw enosys(); },
72
+ chdir() { throw enosys(); },
73
+ }
74
+ }
75
+
76
+ if (!globalThis.path) {
77
+ globalThis.path = {
78
+ resolve(...pathSegments) {
79
+ return pathSegments.join("/");
80
+ }
81
+ }
82
+ }
83
+
84
+ if (!globalThis.crypto) {
85
+ throw new Error("globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)");
86
+ }
87
+
88
+ if (!globalThis.performance) {
89
+ throw new Error("globalThis.performance is not available, polyfill required (performance.now only)");
90
+ }
91
+
92
+ if (!globalThis.TextEncoder) {
93
+ throw new Error("globalThis.TextEncoder is not available, polyfill required");
94
+ }
95
+
96
+ if (!globalThis.TextDecoder) {
97
+ throw new Error("globalThis.TextDecoder is not available, polyfill required");
98
+ }
99
+
100
+ const encoder = new TextEncoder("utf-8");
101
+ const decoder = new TextDecoder("utf-8");
102
+
103
+ globalThis.Go = class {
104
+ constructor() {
105
+ this.argv = ["js"];
106
+ this.env = {};
107
+ this.exit = (code) => {
108
+ if (code !== 0) {
109
+ console.warn("exit code:", code);
110
+ }
111
+ };
112
+ this._exitPromise = new Promise((resolve) => {
113
+ this._resolveExitPromise = resolve;
114
+ });
115
+ this._pendingEvent = null;
116
+ this._scheduledTimeouts = new Map();
117
+ this._nextCallbackTimeoutID = 1;
118
+
119
+ const setInt64 = (addr, v) => {
120
+ this.mem.setUint32(addr + 0, v, true);
121
+ this.mem.setUint32(addr + 4, Math.floor(v / 4294967296), true);
122
+ }
123
+
124
+ const setInt32 = (addr, v) => {
125
+ this.mem.setUint32(addr + 0, v, true);
126
+ }
127
+
128
+ const getInt64 = (addr) => {
129
+ const low = this.mem.getUint32(addr + 0, true);
130
+ const high = this.mem.getInt32(addr + 4, true);
131
+ return low + high * 4294967296;
132
+ }
133
+
134
+ const loadValue = (addr) => {
135
+ const f = this.mem.getFloat64(addr, true);
136
+ if (f === 0) {
137
+ return undefined;
138
+ }
139
+ if (!isNaN(f)) {
140
+ return f;
141
+ }
142
+
143
+ const id = this.mem.getUint32(addr, true);
144
+ return this._values[id];
145
+ }
146
+
147
+ const storeValue = (addr, v) => {
148
+ const nanHead = 0x7FF80000;
149
+
150
+ if (typeof v === "number" && v !== 0) {
151
+ if (isNaN(v)) {
152
+ this.mem.setUint32(addr + 4, nanHead, true);
153
+ this.mem.setUint32(addr, 0, true);
154
+ return;
155
+ }
156
+ this.mem.setFloat64(addr, v, true);
157
+ return;
158
+ }
159
+
160
+ if (v === undefined) {
161
+ this.mem.setFloat64(addr, 0, true);
162
+ return;
163
+ }
164
+
165
+ let id = this._ids.get(v);
166
+ if (id === undefined) {
167
+ id = this._idPool.pop();
168
+ if (id === undefined) {
169
+ id = this._values.length;
170
+ }
171
+ this._values[id] = v;
172
+ this._goRefCounts[id] = 0;
173
+ this._ids.set(v, id);
174
+ }
175
+ this._goRefCounts[id]++;
176
+ let typeFlag = 0;
177
+ switch (typeof v) {
178
+ case "object":
179
+ if (v !== null) {
180
+ typeFlag = 1;
181
+ }
182
+ break;
183
+ case "string":
184
+ typeFlag = 2;
185
+ break;
186
+ case "symbol":
187
+ typeFlag = 3;
188
+ break;
189
+ case "function":
190
+ typeFlag = 4;
191
+ break;
192
+ }
193
+ this.mem.setUint32(addr + 4, nanHead | typeFlag, true);
194
+ this.mem.setUint32(addr, id, true);
195
+ }
196
+
197
+ const loadSlice = (addr) => {
198
+ const array = getInt64(addr + 0);
199
+ const len = getInt64(addr + 8);
200
+ return new Uint8Array(this._inst.exports.mem.buffer, array, len);
201
+ }
202
+
203
+ const loadSliceOfValues = (addr) => {
204
+ const array = getInt64(addr + 0);
205
+ const len = getInt64(addr + 8);
206
+ const a = new Array(len);
207
+ for (let i = 0; i < len; i++) {
208
+ a[i] = loadValue(array + i * 8);
209
+ }
210
+ return a;
211
+ }
212
+
213
+ const loadString = (addr) => {
214
+ const saddr = getInt64(addr + 0);
215
+ const len = getInt64(addr + 8);
216
+ return decoder.decode(new DataView(this._inst.exports.mem.buffer, saddr, len));
217
+ }
218
+
219
+ const testCallExport = (a, b) => {
220
+ this._inst.exports.testExport0();
221
+ return this._inst.exports.testExport(a, b);
222
+ }
223
+
224
+ const timeOrigin = Date.now() - performance.now();
225
+ this.importObject = {
226
+ _gotest: {
227
+ add: (a, b) => a + b,
228
+ callExport: testCallExport,
229
+ },
230
+ gojs: {
231
+ // Go's SP does not change as long as no Go code is running. Some operations (e.g. calls, getters and setters)
232
+ // may synchronously trigger a Go event handler. This makes Go code get executed in the middle of the imported
233
+ // function. A goroutine can switch to a new stack if the current stack is too small (see morestack function).
234
+ // This changes the SP, thus we have to update the SP used by the imported function.
235
+
236
+ // func wasmExit(code int32)
237
+ "runtime.wasmExit": (sp) => {
238
+ sp >>>= 0;
239
+ const code = this.mem.getInt32(sp + 8, true);
240
+ this.exited = true;
241
+ delete this._inst;
242
+ delete this._values;
243
+ delete this._goRefCounts;
244
+ delete this._ids;
245
+ delete this._idPool;
246
+ this.exit(code);
247
+ },
248
+
249
+ // func wasmWrite(fd uintptr, p unsafe.Pointer, n int32)
250
+ "runtime.wasmWrite": (sp) => {
251
+ sp >>>= 0;
252
+ const fd = getInt64(sp + 8);
253
+ const p = getInt64(sp + 16);
254
+ const n = this.mem.getInt32(sp + 24, true);
255
+ fs.writeSync(fd, new Uint8Array(this._inst.exports.mem.buffer, p, n));
256
+ },
257
+
258
+ // func resetMemoryDataView()
259
+ "runtime.resetMemoryDataView": (sp) => {
260
+ sp >>>= 0;
261
+ this.mem = new DataView(this._inst.exports.mem.buffer);
262
+ },
263
+
264
+ // func nanotime1() int64
265
+ "runtime.nanotime1": (sp) => {
266
+ sp >>>= 0;
267
+ setInt64(sp + 8, (timeOrigin + performance.now()) * 1000000);
268
+ },
269
+
270
+ // func walltime() (sec int64, nsec int32)
271
+ "runtime.walltime": (sp) => {
272
+ sp >>>= 0;
273
+ const msec = (new Date).getTime();
274
+ setInt64(sp + 8, msec / 1000);
275
+ this.mem.setInt32(sp + 16, (msec % 1000) * 1000000, true);
276
+ },
277
+
278
+ // func scheduleTimeoutEvent(delay int64) int32
279
+ "runtime.scheduleTimeoutEvent": (sp) => {
280
+ sp >>>= 0;
281
+ const id = this._nextCallbackTimeoutID;
282
+ this._nextCallbackTimeoutID++;
283
+ this._scheduledTimeouts.set(id, setTimeout(
284
+ () => {
285
+ this._resume();
286
+ while (this._scheduledTimeouts.has(id)) {
287
+ // for some reason Go failed to register the timeout event, log and try again
288
+ // (temporary workaround for https://github.com/golang/go/issues/28975)
289
+ console.warn("scheduleTimeoutEvent: missed timeout event");
290
+ this._resume();
291
+ }
292
+ },
293
+ getInt64(sp + 8),
294
+ ));
295
+ this.mem.setInt32(sp + 16, id, true);
296
+ },
297
+
298
+ // func clearTimeoutEvent(id int32)
299
+ "runtime.clearTimeoutEvent": (sp) => {
300
+ sp >>>= 0;
301
+ const id = this.mem.getInt32(sp + 8, true);
302
+ clearTimeout(this._scheduledTimeouts.get(id));
303
+ this._scheduledTimeouts.delete(id);
304
+ },
305
+
306
+ // func getRandomData(r []byte)
307
+ "runtime.getRandomData": (sp) => {
308
+ sp >>>= 0;
309
+ crypto.getRandomValues(loadSlice(sp + 8));
310
+ },
311
+
312
+ // func finalizeRef(v ref)
313
+ "syscall/js.finalizeRef": (sp) => {
314
+ sp >>>= 0;
315
+ const id = this.mem.getUint32(sp + 8, true);
316
+ this._goRefCounts[id]--;
317
+ if (this._goRefCounts[id] === 0) {
318
+ const v = this._values[id];
319
+ this._values[id] = null;
320
+ this._ids.delete(v);
321
+ this._idPool.push(id);
322
+ }
323
+ },
324
+
325
+ // func stringVal(value string) ref
326
+ "syscall/js.stringVal": (sp) => {
327
+ sp >>>= 0;
328
+ storeValue(sp + 24, loadString(sp + 8));
329
+ },
330
+
331
+ // func valueGet(v ref, p string) ref
332
+ "syscall/js.valueGet": (sp) => {
333
+ sp >>>= 0;
334
+ const result = Reflect.get(loadValue(sp + 8), loadString(sp + 16));
335
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
336
+ storeValue(sp + 32, result);
337
+ },
338
+
339
+ // func valueSet(v ref, p string, x ref)
340
+ "syscall/js.valueSet": (sp) => {
341
+ sp >>>= 0;
342
+ Reflect.set(loadValue(sp + 8), loadString(sp + 16), loadValue(sp + 32));
343
+ },
344
+
345
+ // func valueDelete(v ref, p string)
346
+ "syscall/js.valueDelete": (sp) => {
347
+ sp >>>= 0;
348
+ Reflect.deleteProperty(loadValue(sp + 8), loadString(sp + 16));
349
+ },
350
+
351
+ // func valueIndex(v ref, i int) ref
352
+ "syscall/js.valueIndex": (sp) => {
353
+ sp >>>= 0;
354
+ storeValue(sp + 24, Reflect.get(loadValue(sp + 8), getInt64(sp + 16)));
355
+ },
356
+
357
+ // valueSetIndex(v ref, i int, x ref)
358
+ "syscall/js.valueSetIndex": (sp) => {
359
+ sp >>>= 0;
360
+ Reflect.set(loadValue(sp + 8), getInt64(sp + 16), loadValue(sp + 24));
361
+ },
362
+
363
+ // func valueCall(v ref, m string, args []ref) (ref, bool)
364
+ "syscall/js.valueCall": (sp) => {
365
+ sp >>>= 0;
366
+ try {
367
+ const v = loadValue(sp + 8);
368
+ const m = Reflect.get(v, loadString(sp + 16));
369
+ const args = loadSliceOfValues(sp + 32);
370
+ const result = Reflect.apply(m, v, args);
371
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
372
+ storeValue(sp + 56, result);
373
+ this.mem.setUint8(sp + 64, 1);
374
+ } catch (err) {
375
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
376
+ storeValue(sp + 56, err);
377
+ this.mem.setUint8(sp + 64, 0);
378
+ }
379
+ },
380
+
381
+ // func valueInvoke(v ref, args []ref) (ref, bool)
382
+ "syscall/js.valueInvoke": (sp) => {
383
+ sp >>>= 0;
384
+ try {
385
+ const v = loadValue(sp + 8);
386
+ const args = loadSliceOfValues(sp + 16);
387
+ const result = Reflect.apply(v, undefined, args);
388
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
389
+ storeValue(sp + 40, result);
390
+ this.mem.setUint8(sp + 48, 1);
391
+ } catch (err) {
392
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
393
+ storeValue(sp + 40, err);
394
+ this.mem.setUint8(sp + 48, 0);
395
+ }
396
+ },
397
+
398
+ // func valueNew(v ref, args []ref) (ref, bool)
399
+ "syscall/js.valueNew": (sp) => {
400
+ sp >>>= 0;
401
+ try {
402
+ const v = loadValue(sp + 8);
403
+ const args = loadSliceOfValues(sp + 16);
404
+ const result = Reflect.construct(v, args);
405
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
406
+ storeValue(sp + 40, result);
407
+ this.mem.setUint8(sp + 48, 1);
408
+ } catch (err) {
409
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
410
+ storeValue(sp + 40, err);
411
+ this.mem.setUint8(sp + 48, 0);
412
+ }
413
+ },
414
+
415
+ // func valueLength(v ref) int
416
+ "syscall/js.valueLength": (sp) => {
417
+ sp >>>= 0;
418
+ setInt64(sp + 16, parseInt(loadValue(sp + 8).length));
419
+ },
420
+
421
+ // valuePrepareString(v ref) (ref, int)
422
+ "syscall/js.valuePrepareString": (sp) => {
423
+ sp >>>= 0;
424
+ const str = encoder.encode(String(loadValue(sp + 8)));
425
+ storeValue(sp + 16, str);
426
+ setInt64(sp + 24, str.length);
427
+ },
428
+
429
+ // valueLoadString(v ref, b []byte)
430
+ "syscall/js.valueLoadString": (sp) => {
431
+ sp >>>= 0;
432
+ const str = loadValue(sp + 8);
433
+ loadSlice(sp + 16).set(str);
434
+ },
435
+
436
+ // func valueInstanceOf(v ref, t ref) bool
437
+ "syscall/js.valueInstanceOf": (sp) => {
438
+ sp >>>= 0;
439
+ this.mem.setUint8(sp + 24, (loadValue(sp + 8) instanceof loadValue(sp + 16)) ? 1 : 0);
440
+ },
441
+
442
+ // func copyBytesToGo(dst []byte, src ref) (int, bool)
443
+ "syscall/js.copyBytesToGo": (sp) => {
444
+ sp >>>= 0;
445
+ const dst = loadSlice(sp + 8);
446
+ const src = loadValue(sp + 32);
447
+ if (!(src instanceof Uint8Array || src instanceof Uint8ClampedArray)) {
448
+ this.mem.setUint8(sp + 48, 0);
449
+ return;
450
+ }
451
+ const toCopy = src.subarray(0, dst.length);
452
+ dst.set(toCopy);
453
+ setInt64(sp + 40, toCopy.length);
454
+ this.mem.setUint8(sp + 48, 1);
455
+ },
456
+
457
+ // func copyBytesToJS(dst ref, src []byte) (int, bool)
458
+ "syscall/js.copyBytesToJS": (sp) => {
459
+ sp >>>= 0;
460
+ const dst = loadValue(sp + 8);
461
+ const src = loadSlice(sp + 16);
462
+ if (!(dst instanceof Uint8Array || dst instanceof Uint8ClampedArray)) {
463
+ this.mem.setUint8(sp + 48, 0);
464
+ return;
465
+ }
466
+ const toCopy = src.subarray(0, dst.length);
467
+ dst.set(toCopy);
468
+ setInt64(sp + 40, toCopy.length);
469
+ this.mem.setUint8(sp + 48, 1);
470
+ },
471
+
472
+ "debug": (value) => {
473
+ console.log(value);
474
+ },
475
+ }
476
+ };
477
+ }
478
+
479
+ async run(instance) {
480
+ if (!(instance instanceof WebAssembly.Instance)) {
481
+ throw new Error("Go.run: WebAssembly.Instance expected");
482
+ }
483
+ this._inst = instance;
484
+ this.mem = new DataView(this._inst.exports.mem.buffer);
485
+ this._values = [ // JS values that Go currently has references to, indexed by reference id
486
+ NaN,
487
+ 0,
488
+ null,
489
+ true,
490
+ false,
491
+ globalThis,
492
+ this,
493
+ ];
494
+ this._goRefCounts = new Array(this._values.length).fill(Infinity); // number of references that Go has to a JS value, indexed by reference id
495
+ this._ids = new Map([ // mapping from JS values to reference ids
496
+ [0, 1],
497
+ [null, 2],
498
+ [true, 3],
499
+ [false, 4],
500
+ [globalThis, 5],
501
+ [this, 6],
502
+ ]);
503
+ this._idPool = []; // unused ids that have been garbage collected
504
+ this.exited = false; // whether the Go program has exited
505
+
506
+ // Pass command line arguments and environment variables to WebAssembly by writing them to the linear memory.
507
+ let offset = 4096;
508
+
509
+ const strPtr = (str) => {
510
+ const ptr = offset;
511
+ const bytes = encoder.encode(str + "\0");
512
+ new Uint8Array(this.mem.buffer, offset, bytes.length).set(bytes);
513
+ offset += bytes.length;
514
+ if (offset % 8 !== 0) {
515
+ offset += 8 - (offset % 8);
516
+ }
517
+ return ptr;
518
+ };
519
+
520
+ const argc = this.argv.length;
521
+
522
+ const argvPtrs = [];
523
+ this.argv.forEach((arg) => {
524
+ argvPtrs.push(strPtr(arg));
525
+ });
526
+ argvPtrs.push(0);
527
+
528
+ const keys = Object.keys(this.env).sort();
529
+ keys.forEach((key) => {
530
+ argvPtrs.push(strPtr(`${key}=${this.env[key]}`));
531
+ });
532
+ argvPtrs.push(0);
533
+
534
+ const argv = offset;
535
+ argvPtrs.forEach((ptr) => {
536
+ this.mem.setUint32(offset, ptr, true);
537
+ this.mem.setUint32(offset + 4, 0, true);
538
+ offset += 8;
539
+ });
540
+
541
+ // The linker guarantees global data starts from at least wasmMinDataAddr.
542
+ // Keep in sync with cmd/link/internal/ld/data.go:wasmMinDataAddr.
543
+ const wasmMinDataAddr = 4096 + 8192;
544
+ if (offset >= wasmMinDataAddr) {
545
+ throw new Error("total length of command line and environment variables exceeds limit");
546
+ }
547
+
548
+ this._inst.exports.run(argc, argv);
549
+ if (this.exited) {
550
+ this._resolveExitPromise();
551
+ }
552
+ await this._exitPromise;
553
+ }
554
+
555
+ _resume() {
556
+ if (this.exited) {
557
+ throw new Error("Go program has already exited");
558
+ }
559
+ this._inst.exports.resume();
560
+ if (this.exited) {
561
+ this._resolveExitPromise();
562
+ }
563
+ }
564
+
565
+ _makeFuncWrapper(id) {
566
+ const go = this;
567
+ return function () {
568
+ const event = { id: id, this: this, args: arguments };
569
+ go._pendingEvent = event;
570
+ go._resume();
571
+ return event.result;
572
+ };
573
+ }
574
+ }
575
+ })();