frontend-hamroun 1.2.80 → 1.2.83

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 (128) hide show
  1. package/dist/index.cjs +1 -1
  2. package/dist/index.cjs.map +1 -1
  3. package/dist/index.client.cjs +1 -1
  4. package/dist/index.client.js +2 -2
  5. package/dist/index.js +11 -7
  6. package/dist/index.js.map +1 -1
  7. package/dist/{renderer-Din1y3YM.cjs → renderer-BL3gq8cW.cjs} +2 -2
  8. package/dist/{renderer-Din1y3YM.cjs.map → renderer-BL3gq8cW.cjs.map} +1 -1
  9. package/dist/{renderer-Bo9zkUZ_.js → renderer-Dyy-o05F.js} +2 -2
  10. package/dist/{renderer-Bo9zkUZ_.js.map → renderer-Dyy-o05F.js.map} +1 -1
  11. package/dist/{server-renderer-QHt45Ip2.js → server-renderer-C1WXH-zV.js} +99 -73
  12. package/dist/server-renderer-C1WXH-zV.js.map +1 -0
  13. package/dist/server-renderer-Chs-nmJm.cjs +2 -0
  14. package/dist/server-renderer-Chs-nmJm.cjs.map +1 -0
  15. package/dist/server-renderer.cjs +1 -1
  16. package/dist/server-renderer.js +1 -1
  17. package/package.json +1 -1
  18. package/templates/basic-app/src/App.jsx +16 -0
  19. package/templates/basic-app/src/client.jsx +5 -0
  20. package/templates/basic-app/src/components/Counter.jsx +13 -0
  21. package/templates/basic-app/src/jsx-shim.js +3 -0
  22. package/templates/basic-app/src/jsx-shim.ts +7 -0
  23. package/templates/basic-app/src/main.jsx +98 -0
  24. package/templates/basic-app/src/server.js +47 -0
  25. package/templates/complete-app/api/hello.js +0 -0
  26. package/templates/complete-app/lib/frontend-hamroun.js +182 -0
  27. package/templates/complete-app/package.json +18 -0
  28. package/templates/complete-app/pages/about.js +119 -0
  29. package/templates/complete-app/pages/about.jsx +0 -0
  30. package/templates/complete-app/pages/index.js +157 -0
  31. package/templates/complete-app/pages/index.jsx +0 -0
  32. package/templates/complete-app/pages/wasm-demo.js +290 -0
  33. package/templates/complete-app/pages/wasm-demo.jsx +0 -0
  34. package/templates/complete-app/public/client.js +89 -0
  35. package/templates/complete-app/public/index.html +118 -0
  36. package/templates/complete-app/public/styles.css +76 -0
  37. package/templates/complete-app/server.js +226 -0
  38. package/templates/complete-app/src/App.tsx +59 -0
  39. package/templates/complete-app/src/client.tsx +18 -0
  40. package/templates/complete-app/src/server.ts +218 -0
  41. package/templates/complete-app/tsconfig.json +22 -0
  42. package/templates/complete-app/tsconfig.server.json +19 -0
  43. package/templates/{ssr-template → complete-app}/vite.config.js +16 -5
  44. package/templates/complete-app/vite.config.ts +30 -0
  45. package/templates/complete-app/wasm/build.bat +0 -0
  46. package/templates/complete-app/wasm/build.sh +0 -0
  47. package/templates/complete-app/wasm/example.go +0 -0
  48. package/templates/fullstack-app/build/main.css +874 -874
  49. package/templates/fullstack-app/build/main.css.map +7 -7
  50. package/templates/fullstack-app/build/main.js +996 -967
  51. package/templates/fullstack-app/build/main.js.map +7 -7
  52. package/templates/fullstack-app/package-lock.json +6301 -0
  53. package/templates/fullstack-app/public/styles.css +768 -768
  54. package/templates/go/example.go +154 -99
  55. package/templates/ssr-template/dist/client/assets/main-D-VH3xOb.js +1 -0
  56. package/templates/ssr-template/dist/client/index.html +23 -0
  57. package/templates/ssr-template/dist/client.js +951 -0
  58. package/templates/ssr-template/dist/server.js +739 -0
  59. package/templates/ssr-template/esbuild.config.js +33 -0
  60. package/templates/ssr-template/jsx-shim.js +1 -0
  61. package/templates/ssr-template/package.json +14 -8
  62. package/templates/ssr-template/src/App.tsx +847 -49
  63. package/templates/ssr-template/src/client.tsx +3 -17
  64. package/templates/ssr-template/src/server.ts +21 -204
  65. package/templates/ssr-template/tsconfig.json +9 -8
  66. package/templates/ssr-template/tsconfig.server.json +6 -14
  67. package/templates/ssr-template/vite.config.ts +19 -17
  68. package/templates/wasm/build-wasm.js +228 -0
  69. package/templates/wasm/dist/assets/index-BNqTDBdE.js +295 -0
  70. package/templates/wasm/dist/example.wasm +0 -0
  71. package/templates/wasm/dist/index.html +53 -0
  72. package/templates/{go-wasm-app/public/wasm → wasm/dist}/wasm_exec.js +572 -561
  73. package/templates/wasm/esbuild.config.js +63 -0
  74. package/templates/wasm/go/main.go +256 -0
  75. package/templates/wasm/go/wasm_exec.js +0 -0
  76. package/templates/wasm/index.html +97 -0
  77. package/templates/wasm/jsx-shim.js +9 -0
  78. package/templates/wasm/package-lock.json +4577 -0
  79. package/templates/wasm/package.json +25 -0
  80. package/templates/wasm/public/example.wasm +0 -0
  81. package/templates/wasm/public/wasm_exec.js +572 -0
  82. package/templates/wasm/src/App.tsx +550 -0
  83. package/templates/wasm/src/client.tsx +220 -0
  84. package/templates/wasm/src/index.tsx +21 -0
  85. package/templates/wasm/src/server.ts +145 -0
  86. package/templates/wasm/tsconfig.json +21 -0
  87. package/templates/wasm/tsconfig.node.json +13 -0
  88. package/templates/wasm/tsconfig.server.json +23 -0
  89. package/templates/wasm/vite.config.ts +38 -0
  90. package/templates/wasm/wasm-loader.js +103 -0
  91. package/dist/server-renderer-CqIpQ-od.cjs +0 -2
  92. package/dist/server-renderer-CqIpQ-od.cjs.map +0 -1
  93. package/dist/server-renderer-QHt45Ip2.js.map +0 -1
  94. package/templates/basic-app/bun.lock +0 -196
  95. package/templates/basic-app/docs/rapport_pfe.aux +0 -27
  96. package/templates/basic-app/docs/rapport_pfe.out +0 -10
  97. package/templates/basic-app/docs/rapport_pfe.pdf +0 -0
  98. package/templates/basic-app/docs/rapport_pfe.tex +0 -68
  99. package/templates/basic-app/docs/rapport_pfe.toc +0 -14
  100. package/templates/basic-app/package-lock.json +0 -4185
  101. package/templates/go-wasm-app/README.md +0 -38
  102. package/templates/go-wasm-app/babel.config.js +0 -15
  103. package/templates/go-wasm-app/build-client.js +0 -49
  104. package/templates/go-wasm-app/build-wasm.js +0 -237
  105. package/templates/go-wasm-app/package.json +0 -23
  106. package/templates/go-wasm-app/public/index.html +0 -128
  107. package/templates/go-wasm-app/public/styles.css +0 -197
  108. package/templates/go-wasm-app/public/wasm/example.wasm +0 -0
  109. package/templates/go-wasm-app/public/wasm/wasm_exec_node.js +0 -39
  110. package/templates/go-wasm-app/server.js +0 -521
  111. package/templates/go-wasm-app/src/App.jsx +0 -38
  112. package/templates/go-wasm-app/src/app.js +0 -153
  113. package/templates/go-wasm-app/src/client.js +0 -57
  114. package/templates/go-wasm-app/src/components/Footer.jsx +0 -13
  115. package/templates/go-wasm-app/src/components/Header.jsx +0 -19
  116. package/templates/go-wasm-app/src/components/WasmDemo.jsx +0 -120
  117. package/templates/go-wasm-app/src/main.jsx +0 -12
  118. package/templates/go-wasm-app/src/wasm/example.go +0 -75
  119. package/templates/go-wasm-app/tsconfig.server.json +0 -18
  120. package/templates/go-wasm-app/vite.config.js +0 -34
  121. package/templates/ssr-template/package-lock.json +0 -2478
  122. package/templates/ssr-template/public/index.html +0 -47
  123. package/templates/ssr-template/server.js +0 -369
  124. /package/templates/{ssr-template → complete-app}/client.js +0 -0
  125. /package/templates/{ssr-template → complete-app}/readme.md +0 -0
  126. /package/templates/{ssr-template → complete-app}/server.ts +0 -0
  127. /package/templates/{ssr-template → complete-app}/src/client.ts +0 -0
  128. /package/templates/{ssr-template → complete-app}/src/pages/index.tsx +0 -0
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "wasm-app",
3
+ "version": "1.0.0",
4
+ "description": "Frontend Hamroun WASM Application",
5
+ "main": "src/App.tsx",
6
+ "type": "module",
7
+ "scripts": {
8
+ "dev": "vite",
9
+ "build": "vite build",
10
+ "build:wasm": "cd wasm && set GOOS=js && set GOARCH=wasm && go build -o ../public/example.wasm main.go",
11
+ "copy:wasm-exec": "copy \"%GOROOT%\\misc\\wasm\\wasm_exec.js\" public\\",
12
+ "setup:wasm": "npm run copy:wasm-exec && npm run build:wasm",
13
+ "preview": "vite preview",
14
+ "type-check": "tsc --noEmit"
15
+ },
16
+ "dependencies": {
17
+ "baraqex": "^1.0.36",
18
+ "frontend-hamroun": "^1.2.35"
19
+ },
20
+ "devDependencies": {
21
+ "@types/node": "^20.0.0",
22
+ "typescript": "^5.0.0",
23
+ "vite": "^5.0.0"
24
+ }
25
+ }
@@ -0,0 +1,572 @@
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 }, // 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.crypto) {
77
+ throw new Error("globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)");
78
+ }
79
+
80
+ if (!globalThis.performance) {
81
+ throw new Error("globalThis.performance is not available, polyfill required (performance.now only)");
82
+ }
83
+
84
+ if (!globalThis.TextEncoder) {
85
+ throw new Error("globalThis.TextEncoder is not available, polyfill required");
86
+ }
87
+
88
+ if (!globalThis.TextDecoder) {
89
+ throw new Error("globalThis.TextDecoder is not available, polyfill required");
90
+ }
91
+
92
+ const encoder = new TextEncoder("utf-8");
93
+ const decoder = new TextDecoder("utf-8");
94
+
95
+ globalThis.Go = class {
96
+ constructor() {
97
+ this.argv = ["js"];
98
+ this.env = {};
99
+ this.exit = (code) => {
100
+ if (code !== 0) {
101
+ console.warn("exit code:", code);
102
+ }
103
+ };
104
+ this._exitPromise = new Promise((resolve) => {
105
+ this._resolveExitPromise = resolve;
106
+ });
107
+ this._pendingEvent = null;
108
+ this._scheduledTimeouts = new Map();
109
+ this._nextCallbackTimeoutID = 1;
110
+
111
+ const setInt64 = (addr, v) => {
112
+ this.mem.setUint32(addr + 0, v, true);
113
+ this.mem.setUint32(addr + 4, Math.floor(v / 4294967296), true);
114
+ }
115
+
116
+ const setInt32 = (addr, v) => {
117
+ this.mem.setUint32(addr + 0, v, true);
118
+ }
119
+
120
+ const getInt64 = (addr) => {
121
+ const low = this.mem.getUint32(addr + 0, true);
122
+ const high = this.mem.getInt32(addr + 4, true);
123
+ return low + high * 4294967296;
124
+ }
125
+
126
+ const loadValue = (addr) => {
127
+ const f = this.mem.getFloat64(addr, true);
128
+ if (f === 0) {
129
+ return undefined;
130
+ }
131
+ if (!isNaN(f)) {
132
+ return f;
133
+ }
134
+
135
+ const id = this.mem.getUint32(addr, true);
136
+ return this._values[id];
137
+ }
138
+
139
+ const storeValue = (addr, v) => {
140
+ const nanHead = 0x7FF80000;
141
+
142
+ if (typeof v === "number" && v !== 0) {
143
+ if (isNaN(v)) {
144
+ this.mem.setUint32(addr + 4, nanHead, true);
145
+ this.mem.setUint32(addr, 0, true);
146
+ return;
147
+ }
148
+ this.mem.setFloat64(addr, v, true);
149
+ return;
150
+ }
151
+
152
+ if (v === undefined) {
153
+ this.mem.setFloat64(addr, 0, true);
154
+ return;
155
+ }
156
+
157
+ let id = this._ids.get(v);
158
+ if (id === undefined) {
159
+ id = this._idPool.pop();
160
+ if (id === undefined) {
161
+ id = this._values.length;
162
+ }
163
+ this._values[id] = v;
164
+ this._goRefCounts[id] = 0;
165
+ this._ids.set(v, id);
166
+ }
167
+ this._goRefCounts[id]++;
168
+ let typeFlag = 0;
169
+ switch (typeof v) {
170
+ case "object":
171
+ if (v !== null) {
172
+ typeFlag = 1;
173
+ }
174
+ break;
175
+ case "string":
176
+ typeFlag = 2;
177
+ break;
178
+ case "symbol":
179
+ typeFlag = 3;
180
+ break;
181
+ case "function":
182
+ typeFlag = 4;
183
+ break;
184
+ }
185
+ this.mem.setUint32(addr + 4, nanHead | typeFlag, true);
186
+ this.mem.setUint32(addr, id, true);
187
+ }
188
+
189
+ const loadSlice = (addr) => {
190
+ const array = getInt64(addr + 0);
191
+ const len = getInt64(addr + 8);
192
+ return new Uint8Array(this._inst.exports.mem.buffer, array, len);
193
+ }
194
+
195
+ const loadSliceOfValues = (addr) => {
196
+ const array = getInt64(addr + 0);
197
+ const len = getInt64(addr + 8);
198
+ const a = new Array(len);
199
+ for (let i = 0; i < len; i++) {
200
+ a[i] = loadValue(array + i * 8);
201
+ }
202
+ return a;
203
+ }
204
+
205
+ const loadString = (addr) => {
206
+ const saddr = getInt64(addr + 0);
207
+ const len = getInt64(addr + 8);
208
+ return decoder.decode(new DataView(this._inst.exports.mem.buffer, saddr, len));
209
+ }
210
+
211
+ const timeOrigin = Date.now() - performance.now();
212
+ this.importObject = {
213
+ _gotest: {
214
+ add: (a, b) => a + b,
215
+ },
216
+ gojs: {
217
+ // Go's SP does not change as long as no Go code is running. Some operations (e.g. calls, getters and setters)
218
+ // may synchronously trigger a Go event handler. This makes Go code get executed in the middle of the imported
219
+ // function. A goroutine can switch to a new stack if the current stack is too small (see morestack function).
220
+ // This changes the SP, thus we have to update the SP used by the imported function.
221
+
222
+ // func wasmExit(code int32)
223
+ "runtime.wasmExit": (sp) => {
224
+ sp >>>= 0;
225
+ const code = this.mem.getInt32(sp + 8, true);
226
+ this.exited = true;
227
+ delete this._inst;
228
+ delete this._values;
229
+ delete this._goRefCounts;
230
+ delete this._ids;
231
+ delete this._idPool;
232
+
233
+ // Modified: Don't call process.exit in Node.js testing environment
234
+ // Instead, just call the exit handler and resolve the promise
235
+ if (typeof process !== 'undefined' && process.env && process.env.NODE_ENV !== 'production') {
236
+ // In development/testing, just log and continue
237
+ console.log('[WASM] Go program finished with exit code:', code);
238
+ this.exit(code);
239
+ this._resolveExitPromise();
240
+ } else {
241
+ // In production, call the original exit behavior
242
+ this.exit(code);
243
+ }
244
+ },
245
+
246
+ // func wasmWrite(fd uintptr, p unsafe.Pointer, n int32)
247
+ "runtime.wasmWrite": (sp) => {
248
+ sp >>>= 0;
249
+ const fd = getInt64(sp + 8);
250
+ const p = getInt64(sp + 16);
251
+ const n = this.mem.getInt32(sp + 24, true);
252
+ fs.writeSync(fd, new Uint8Array(this._inst.exports.mem.buffer, p, n));
253
+ },
254
+
255
+ // func resetMemoryDataView()
256
+ "runtime.resetMemoryDataView": (sp) => {
257
+ sp >>>= 0;
258
+ this.mem = new DataView(this._inst.exports.mem.buffer);
259
+ },
260
+
261
+ // func nanotime1() int64
262
+ "runtime.nanotime1": (sp) => {
263
+ sp >>>= 0;
264
+ setInt64(sp + 8, (timeOrigin + performance.now()) * 1000000);
265
+ },
266
+
267
+ // func walltime() (sec int64, nsec int32)
268
+ "runtime.walltime": (sp) => {
269
+ sp >>>= 0;
270
+ const msec = (new Date).getTime();
271
+ setInt64(sp + 8, msec / 1000);
272
+ this.mem.setInt32(sp + 16, (msec % 1000) * 1000000, true);
273
+ },
274
+
275
+ // func scheduleTimeoutEvent(delay int64) int32
276
+ "runtime.scheduleTimeoutEvent": (sp) => {
277
+ sp >>>= 0;
278
+ const id = this._nextCallbackTimeoutID;
279
+ this._nextCallbackTimeoutID++;
280
+ this._scheduledTimeouts.set(id, setTimeout(
281
+ () => {
282
+ this._resume();
283
+ while (this._scheduledTimeouts.has(id)) {
284
+ // for some reason Go failed to register the timeout event, log and try again
285
+ // (temporary workaround for https://github.com/golang/go/issues/28975)
286
+ console.warn("scheduleTimeoutEvent: missed timeout event");
287
+ this._resume();
288
+ }
289
+ },
290
+ getInt64(sp + 8),
291
+ ));
292
+ this.mem.setInt32(sp + 16, id, true);
293
+ },
294
+
295
+ // func clearTimeoutEvent(id int32)
296
+ "runtime.clearTimeoutEvent": (sp) => {
297
+ sp >>>= 0;
298
+ const id = this.mem.getInt32(sp + 8, true);
299
+ clearTimeout(this._scheduledTimeouts.get(id));
300
+ this._scheduledTimeouts.delete(id);
301
+ },
302
+
303
+ // func getRandomData(r []byte)
304
+ "runtime.getRandomData": (sp) => {
305
+ sp >>>= 0;
306
+ crypto.getRandomValues(loadSlice(sp + 8));
307
+ },
308
+
309
+ // func finalizeRef(v ref)
310
+ "syscall/js.finalizeRef": (sp) => {
311
+ sp >>>= 0;
312
+ const id = this.mem.getUint32(sp + 8, true);
313
+ this._goRefCounts[id]--;
314
+ if (this._goRefCounts[id] === 0) {
315
+ const v = this._values[id];
316
+ this._values[id] = null;
317
+ this._ids.delete(v);
318
+ this._idPool.push(id);
319
+ }
320
+ },
321
+
322
+ // func stringVal(value string) ref
323
+ "syscall/js.stringVal": (sp) => {
324
+ sp >>>= 0;
325
+ storeValue(sp + 24, loadString(sp + 8));
326
+ },
327
+
328
+ // func valueGet(v ref, p string) ref
329
+ "syscall/js.valueGet": (sp) => {
330
+ sp >>>= 0;
331
+ const result = Reflect.get(loadValue(sp + 8), loadString(sp + 16));
332
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
333
+ storeValue(sp + 32, result);
334
+ },
335
+
336
+ // func valueSet(v ref, p string, x ref)
337
+ "syscall/js.valueSet": (sp) => {
338
+ sp >>>= 0;
339
+ Reflect.set(loadValue(sp + 8), loadString(sp + 16), loadValue(sp + 32));
340
+ },
341
+
342
+ // func valueDelete(v ref, p string)
343
+ "syscall/js.valueDelete": (sp) => {
344
+ sp >>>= 0;
345
+ Reflect.deleteProperty(loadValue(sp + 8), loadString(sp + 16));
346
+ },
347
+
348
+ // func valueIndex(v ref, i int) ref
349
+ "syscall/js.valueIndex": (sp) => {
350
+ sp >>>= 0;
351
+ storeValue(sp + 24, Reflect.get(loadValue(sp + 8), getInt64(sp + 16)));
352
+ },
353
+
354
+ // valueSetIndex(v ref, i int, x ref)
355
+ "syscall/js.valueSetIndex": (sp) => {
356
+ sp >>>= 0;
357
+ Reflect.set(loadValue(sp + 8), getInt64(sp + 16), loadValue(sp + 24));
358
+ },
359
+
360
+ // func valueCall(v ref, m string, args []ref) (ref, bool)
361
+ "syscall/js.valueCall": (sp) => {
362
+ sp >>>= 0;
363
+ try {
364
+ const v = loadValue(sp + 8);
365
+ const m = Reflect.get(v, loadString(sp + 16));
366
+ const args = loadSliceOfValues(sp + 32);
367
+ const result = Reflect.apply(m, v, args);
368
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
369
+ storeValue(sp + 56, result);
370
+ this.mem.setUint8(sp + 64, 1);
371
+ } catch (err) {
372
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
373
+ storeValue(sp + 56, err);
374
+ this.mem.setUint8(sp + 64, 0);
375
+ }
376
+ },
377
+
378
+ // func valueInvoke(v ref, args []ref) (ref, bool)
379
+ "syscall/js.valueInvoke": (sp) => {
380
+ sp >>>= 0;
381
+ try {
382
+ const v = loadValue(sp + 8);
383
+ const args = loadSliceOfValues(sp + 16);
384
+ const result = Reflect.apply(v, undefined, args);
385
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
386
+ storeValue(sp + 40, result);
387
+ this.mem.setUint8(sp + 48, 1);
388
+ } catch (err) {
389
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
390
+ storeValue(sp + 40, err);
391
+ this.mem.setUint8(sp + 48, 0);
392
+ }
393
+ },
394
+
395
+ // func valueNew(v ref, args []ref) (ref, bool)
396
+ "syscall/js.valueNew": (sp) => {
397
+ sp >>>= 0;
398
+ try {
399
+ const v = loadValue(sp + 8);
400
+ const args = loadSliceOfValues(sp + 16);
401
+ const result = Reflect.construct(v, args);
402
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
403
+ storeValue(sp + 40, result);
404
+ this.mem.setUint8(sp + 48, 1);
405
+ } catch (err) {
406
+ sp = this._inst.exports.getsp() >>> 0; // see comment above
407
+ storeValue(sp + 40, err);
408
+ this.mem.setUint8(sp + 48, 0);
409
+ }
410
+ },
411
+
412
+ // func valueLength(v ref) int
413
+ "syscall/js.valueLength": (sp) => {
414
+ sp >>>= 0;
415
+ setInt64(sp + 16, parseInt(loadValue(sp + 8).length));
416
+ },
417
+
418
+ // valuePrepareString(v ref) (ref, int)
419
+ "syscall/js.valuePrepareString": (sp) => {
420
+ sp >>>= 0;
421
+ const str = encoder.encode(String(loadValue(sp + 8)));
422
+ storeValue(sp + 16, str);
423
+ setInt64(sp + 24, str.length);
424
+ },
425
+
426
+ // valueLoadString(v ref, b []byte)
427
+ "syscall/js.valueLoadString": (sp) => {
428
+ sp >>>= 0;
429
+ const str = loadValue(sp + 8);
430
+ loadSlice(sp + 16).set(str);
431
+ },
432
+
433
+ // func valueInstanceOf(v ref, t ref) bool
434
+ "syscall/js.valueInstanceOf": (sp) => {
435
+ sp >>>= 0;
436
+ this.mem.setUint8(sp + 24, (loadValue(sp + 8) instanceof loadValue(sp + 16)) ? 1 : 0);
437
+ },
438
+
439
+ // func copyBytesToGo(dst []byte, src ref) (int, bool)
440
+ "syscall/js.copyBytesToGo": (sp) => {
441
+ sp >>>= 0;
442
+ const dst = loadSlice(sp + 8);
443
+ const src = loadValue(sp + 32);
444
+ if (!(src instanceof Uint8Array || src instanceof Uint8ClampedArray)) {
445
+ this.mem.setUint8(sp + 48, 0);
446
+ return;
447
+ }
448
+ const toCopy = src.subarray(0, dst.length);
449
+ dst.set(toCopy);
450
+ setInt64(sp + 40, toCopy.length);
451
+ this.mem.setUint8(sp + 48, 1);
452
+ },
453
+
454
+ // func copyBytesToJS(dst ref, src []byte) (int, bool)
455
+ "syscall/js.copyBytesToJS": (sp) => {
456
+ sp >>>= 0;
457
+ const dst = loadValue(sp + 8);
458
+ const src = loadSlice(sp + 16);
459
+ if (!(dst instanceof Uint8Array || dst 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
+ "debug": (value) => {
470
+ console.log(value);
471
+ },
472
+ }
473
+ };
474
+ }
475
+
476
+ async run(instance) {
477
+ if (!(instance instanceof WebAssembly.Instance)) {
478
+ throw new Error("Go.run: WebAssembly.Instance expected");
479
+ }
480
+ this._inst = instance;
481
+ this.mem = new DataView(this._inst.exports.mem.buffer);
482
+ this._values = [ // JS values that Go currently has references to, indexed by reference id
483
+ NaN,
484
+ 0,
485
+ null,
486
+ true,
487
+ false,
488
+ globalThis,
489
+ this,
490
+ ];
491
+ this._goRefCounts = new Array(this._values.length).fill(Infinity); // number of references that Go has to a JS value, indexed by reference id
492
+ this._ids = new Map([ // mapping from JS values to reference ids
493
+ [0, 1],
494
+ [null, 2],
495
+ [true, 3],
496
+ [false, 4],
497
+ [globalThis, 5],
498
+ [this, 6],
499
+ ]);
500
+ this._idPool = []; // unused ids that have been garbage collected
501
+ this.exited = false; // whether the Go program has exited
502
+
503
+ // Pass command line arguments and environment variables to WebAssembly by writing them to the linear memory.
504
+ let offset = 4096;
505
+
506
+ const strPtr = (str) => {
507
+ const ptr = offset;
508
+ const bytes = encoder.encode(str + "\0");
509
+ new Uint8Array(this.mem.buffer, offset, bytes.length).set(bytes);
510
+ offset += bytes.length;
511
+ if (offset % 8 !== 0) {
512
+ offset += 8 - (offset % 8);
513
+ }
514
+ return ptr;
515
+ };
516
+
517
+ const argc = this.argv.length;
518
+
519
+ const argvPtrs = [];
520
+ this.argv.forEach((arg) => {
521
+ argvPtrs.push(strPtr(arg));
522
+ });
523
+ argvPtrs.push(0);
524
+
525
+ const keys = Object.keys(this.env).sort();
526
+ keys.forEach((key) => {
527
+ argvPtrs.push(strPtr(`${key}=${this.env[key]}`));
528
+ });
529
+ argvPtrs.push(0);
530
+
531
+ const argv = offset;
532
+ argvPtrs.forEach((ptr) => {
533
+ this.mem.setUint32(offset, ptr, true);
534
+ this.mem.setUint32(offset + 4, 0, true);
535
+ offset += 8;
536
+ });
537
+
538
+ // The linker guarantees global data starts from at least wasmMinDataAddr.
539
+ // Keep in sync with cmd/link/internal/ld/data.go:wasmMinDataAddr.
540
+ const wasmMinDataAddr = 4096 + 8192;
541
+ if (offset >= wasmMinDataAddr) {
542
+ throw new Error("total length of command line and environment variables exceeds limit");
543
+ }
544
+
545
+ this._inst.exports.run(argc, argv);
546
+ if (this.exited) {
547
+ this._resolveExitPromise();
548
+ }
549
+ await this._exitPromise;
550
+ }
551
+
552
+ _resume() {
553
+ if (this.exited) {
554
+ throw new Error("Go program has already exited");
555
+ }
556
+ this._inst.exports.resume();
557
+ if (this.exited) {
558
+ this._resolveExitPromise();
559
+ }
560
+ }
561
+
562
+ _makeFuncWrapper(id) {
563
+ const go = this;
564
+ return function () {
565
+ const event = { id: id, this: this, args: arguments };
566
+ go._pendingEvent = event;
567
+ go._resume();
568
+ return event.result;
569
+ };
570
+ }
571
+ }
572
+ })();