@cloudbase/container 2.5.29-beta.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.
@@ -0,0 +1,590 @@
1
+ // @ts-nocheck
2
+ /* eslint-disable */
3
+
4
+ // This file has been modified for use by the TinyGo compiler.
5
+
6
+ // Map multiple JavaScript environments to a single common API,
7
+ // preferring web standards over Node.js API.
8
+ //
9
+ // Environments considered:
10
+ // - Browsers
11
+ // - Node.js
12
+ // - Electron
13
+ // - Parcel
14
+
15
+ if (typeof global !== 'undefined') {
16
+ // global already exists
17
+ } else if (typeof window !== 'undefined') {
18
+ window.global = window
19
+ } else if (typeof self !== 'undefined') {
20
+ self.global = self
21
+ } else {
22
+ throw new Error('cannot export Go (neither global, window nor self is defined)')
23
+ }
24
+
25
+ if (!global.require && typeof require !== 'undefined') {
26
+ // global.require = require
27
+ }
28
+
29
+ // if (!global.fs && global.require) {
30
+ // global.fs = require('fs')
31
+ // }
32
+
33
+ const enosys = () => {
34
+ const err = new Error('not implemented')
35
+ err.code = 'ENOSYS'
36
+ return err
37
+ }
38
+
39
+ if (!global.fs) {
40
+ let outputBuf = ''
41
+ global.fs = {
42
+ constants: { O_WRONLY: -1, O_RDWR: -1, O_CREAT: -1, O_TRUNC: -1, O_APPEND: -1, O_EXCL: -1 }, // unused
43
+ writeSync(fd, buf) {
44
+ outputBuf += decoder.decode(buf)
45
+ const nl = outputBuf.lastIndexOf('\n')
46
+ if (nl != -1) {
47
+ console.log(outputBuf.substr(0, nl))
48
+ outputBuf = outputBuf.substr(nl + 1)
49
+ }
50
+ return buf.length
51
+ },
52
+ write(fd, buf, offset, length, position, callback) {
53
+ if (offset !== 0 || length !== buf.length || position !== null) {
54
+ callback(enosys())
55
+ return
56
+ }
57
+ const n = this.writeSync(fd, buf)
58
+ callback(null, n)
59
+ },
60
+ chmod(path, mode, callback) {
61
+ callback(enosys())
62
+ },
63
+ chown(path, uid, gid, callback) {
64
+ callback(enosys())
65
+ },
66
+ close(fd, callback) {
67
+ callback(enosys())
68
+ },
69
+ fchmod(fd, mode, callback) {
70
+ callback(enosys())
71
+ },
72
+ fchown(fd, uid, gid, callback) {
73
+ callback(enosys())
74
+ },
75
+ fstat(fd, callback) {
76
+ callback(enosys())
77
+ },
78
+ fsync(fd, callback) {
79
+ callback(null)
80
+ },
81
+ ftruncate(fd, length, callback) {
82
+ callback(enosys())
83
+ },
84
+ lchown(path, uid, gid, callback) {
85
+ callback(enosys())
86
+ },
87
+ link(path, link, callback) {
88
+ callback(enosys())
89
+ },
90
+ lstat(path, callback) {
91
+ callback(enosys())
92
+ },
93
+ mkdir(path, perm, callback) {
94
+ callback(enosys())
95
+ },
96
+ open(path, flags, mode, callback) {
97
+ callback(enosys())
98
+ },
99
+ read(fd, buffer, offset, length, position, callback) {
100
+ callback(enosys())
101
+ },
102
+ readdir(path, callback) {
103
+ callback(enosys())
104
+ },
105
+ readlink(path, callback) {
106
+ callback(enosys())
107
+ },
108
+ rename(from, to, callback) {
109
+ callback(enosys())
110
+ },
111
+ rmdir(path, callback) {
112
+ callback(enosys())
113
+ },
114
+ stat(path, callback) {
115
+ callback(enosys())
116
+ },
117
+ symlink(path, link, callback) {
118
+ callback(enosys())
119
+ },
120
+ truncate(path, length, callback) {
121
+ callback(enosys())
122
+ },
123
+ unlink(path, callback) {
124
+ callback(enosys())
125
+ },
126
+ utimes(path, atime, mtime, callback) {
127
+ callback(enosys())
128
+ },
129
+ }
130
+ }
131
+
132
+ if (!global.process) {
133
+ global.process = {
134
+ getuid() {
135
+ return -1
136
+ },
137
+ getgid() {
138
+ return -1
139
+ },
140
+ geteuid() {
141
+ return -1
142
+ },
143
+ getegid() {
144
+ return -1
145
+ },
146
+ getgroups() {
147
+ throw enosys()
148
+ },
149
+ pid: -1,
150
+ ppid: -1,
151
+ umask() {
152
+ throw enosys()
153
+ },
154
+ cwd() {
155
+ throw enosys()
156
+ },
157
+ chdir() {
158
+ throw enosys()
159
+ },
160
+ }
161
+ }
162
+
163
+ if (!global.crypto) {
164
+ throw new Error('globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)')
165
+ // const nodeCrypto = require('crypto')
166
+ // global.crypto = {
167
+ // getRandomValues(b) {
168
+ // nodeCrypto.randomFillSync(b)
169
+ // },
170
+ // }
171
+ }
172
+
173
+ if (!global.performance) {
174
+ throw new Error('globalThis.performance is not available, polyfill required (performance.now only)')
175
+ // global.performance = {
176
+ // now() {
177
+ // const [sec, nsec] = process.hrtime()
178
+ // return sec * 1000 + nsec / 1000000
179
+ // },
180
+ // }
181
+ }
182
+
183
+ if (!global.TextEncoder) {
184
+ throw new Error('globalThis.TextEncoder is not available, polyfill required')
185
+ // global.TextEncoder = require('util').TextEncoder
186
+ }
187
+
188
+ if (!global.TextDecoder) {
189
+ throw new Error('globalThis.TextDecoder is not available, polyfill required')
190
+ // global.TextDecoder = require('util').TextDecoder
191
+ }
192
+
193
+ // End of polyfills for common API.
194
+
195
+ const encoder = new TextEncoder('utf-8')
196
+ const decoder = new TextDecoder('utf-8')
197
+ var logLine = []
198
+
199
+ global.Go = class {
200
+ constructor() {
201
+ this._callbackTimeouts = new Map()
202
+ this._nextCallbackTimeoutID = 1
203
+
204
+ const mem = () => {
205
+ // The buffer may change when requesting more memory.
206
+ return new DataView(this._inst.exports.memory.buffer)
207
+ }
208
+
209
+ const setInt64 = (addr, v) => {
210
+ mem().setUint32(addr + 0, v, true)
211
+ mem().setUint32(addr + 4, Math.floor(v / 4294967296), true)
212
+ }
213
+
214
+ const getInt64 = (addr) => {
215
+ const low = mem().getUint32(addr + 0, true)
216
+ const high = mem().getInt32(addr + 4, true)
217
+ return low + high * 4294967296
218
+ }
219
+
220
+ const loadValue = (addr) => {
221
+ const f = mem().getFloat64(addr, true)
222
+ if (f === 0) {
223
+ return undefined
224
+ }
225
+ if (!isNaN(f)) {
226
+ return f
227
+ }
228
+
229
+ const id = mem().getUint32(addr, true)
230
+ return this._values[id]
231
+ }
232
+
233
+ const storeValue = (addr, v) => {
234
+ const nanHead = 0x7ff80000
235
+
236
+ if (typeof v === 'number') {
237
+ if (isNaN(v)) {
238
+ mem().setUint32(addr + 4, nanHead, true)
239
+ mem().setUint32(addr, 0, true)
240
+ return
241
+ }
242
+ if (v === 0) {
243
+ mem().setUint32(addr + 4, nanHead, true)
244
+ mem().setUint32(addr, 1, true)
245
+ return
246
+ }
247
+ mem().setFloat64(addr, v, true)
248
+ return
249
+ }
250
+
251
+ switch (v) {
252
+ case undefined:
253
+ mem().setFloat64(addr, 0, true)
254
+ return
255
+ case null:
256
+ mem().setUint32(addr + 4, nanHead, true)
257
+ mem().setUint32(addr, 2, true)
258
+ return
259
+ case true:
260
+ mem().setUint32(addr + 4, nanHead, true)
261
+ mem().setUint32(addr, 3, true)
262
+ return
263
+ case false:
264
+ mem().setUint32(addr + 4, nanHead, true)
265
+ mem().setUint32(addr, 4, true)
266
+ return
267
+ }
268
+
269
+ let id = this._ids.get(v)
270
+ if (id === undefined) {
271
+ id = this._idPool.pop()
272
+ if (id === undefined) {
273
+ id = this._values.length
274
+ }
275
+ this._values[id] = v
276
+ this._goRefCounts[id] = 0
277
+ this._ids.set(v, id)
278
+ }
279
+ this._goRefCounts[id]++
280
+ let typeFlag = 1
281
+ switch (typeof v) {
282
+ case 'string':
283
+ typeFlag = 2
284
+ break
285
+ case 'symbol':
286
+ typeFlag = 3
287
+ break
288
+ case 'function':
289
+ typeFlag = 4
290
+ break
291
+ }
292
+ mem().setUint32(addr + 4, nanHead | typeFlag, true)
293
+ mem().setUint32(addr, id, true)
294
+ }
295
+
296
+ const loadSlice = (array, len, cap) => {
297
+ return new Uint8Array(this._inst.exports.memory.buffer, array, len)
298
+ }
299
+
300
+ const loadSliceOfValues = (array, len, cap) => {
301
+ const a = new Array(len)
302
+ for (let i = 0; i < len; i++) {
303
+ a[i] = loadValue(array + i * 8)
304
+ }
305
+ return a
306
+ }
307
+
308
+ const loadString = (ptr, len) => {
309
+ return decoder.decode(new DataView(this._inst.exports.memory.buffer, ptr, len))
310
+ }
311
+
312
+ const timeOrigin = Date.now() - performance.now()
313
+ this.importObject = {
314
+ wasi_snapshot_preview1: {
315
+ // https://github.com/WebAssembly/WASI/blob/main/phases/snapshot/docs.md#fd_write
316
+ fd_write: function (fd, iovs_ptr, iovs_len, nwritten_ptr) {
317
+ let nwritten = 0
318
+ if (fd == 1) {
319
+ for (let iovs_i = 0; iovs_i < iovs_len; iovs_i++) {
320
+ let iov_ptr = iovs_ptr + iovs_i * 8 // assuming wasm32
321
+ let ptr = mem().getUint32(iov_ptr + 0, true)
322
+ let len = mem().getUint32(iov_ptr + 4, true)
323
+ nwritten += len
324
+ for (let i = 0; i < len; i++) {
325
+ let c = mem().getUint8(ptr + i)
326
+ if (c == 13) {
327
+ // CR
328
+ // ignore
329
+ } else if (c == 10) {
330
+ // LF
331
+ // write line
332
+ let line = decoder.decode(new Uint8Array(logLine))
333
+ logLine = []
334
+ console.log(line)
335
+ } else {
336
+ logLine.push(c)
337
+ }
338
+ }
339
+ }
340
+ } else {
341
+ console.error('invalid file descriptor:', fd)
342
+ }
343
+ mem().setUint32(nwritten_ptr, nwritten, true)
344
+ return 0
345
+ },
346
+ fd_close: () => 0, // dummy
347
+ fd_fdstat_get: () => 0, // dummy
348
+ fd_seek: () => 0, // dummy
349
+ proc_exit: (code) => {
350
+ if (global.process) {
351
+ // Node.js
352
+ process.exit(code)
353
+ } else {
354
+ // Can't exit in a browser.
355
+ throw 'trying to exit with code ' + code
356
+ }
357
+ },
358
+ random_get: (bufPtr, bufLen) => {
359
+ crypto.getRandomValues(loadSlice(bufPtr, bufLen))
360
+ return 0
361
+ },
362
+ },
363
+ env: {
364
+ // func ticks() float64
365
+ 'runtime.ticks': () => {
366
+ return timeOrigin + performance.now()
367
+ },
368
+
369
+ // func sleepTicks(timeout float64)
370
+ 'runtime.sleepTicks': (timeout) => {
371
+ // Do not sleep, only reactivate scheduler after the given timeout.
372
+ setTimeout(this._inst.exports.go_scheduler, timeout)
373
+ },
374
+
375
+ // func finalizeRef(v ref)
376
+ 'syscall/js.finalizeRef': (sp) => {
377
+ // Note: TinyGo does not support finalizers so this should never be
378
+ // called.
379
+ const id = mem().getUint32(sp, true)
380
+ this._goRefCounts[id]--
381
+ if (this._goRefCounts[id] === 0) {
382
+ const v = this._values[id]
383
+ this._values[id] = null
384
+ this._ids.delete(v)
385
+ this._idPool.push(id)
386
+ }
387
+ // console.error('syscall/js.finalizeRef not implemented');
388
+ },
389
+
390
+ // func stringVal(value string) ref
391
+ 'syscall/js.stringVal': (ret_ptr, value_ptr, value_len) => {
392
+ const s = loadString(value_ptr, value_len)
393
+ storeValue(ret_ptr, s)
394
+ },
395
+
396
+ // func valueGet(v ref, p string) ref
397
+ 'syscall/js.valueGet': (retval, v_addr, p_ptr, p_len) => {
398
+ let prop = loadString(p_ptr, p_len)
399
+ let value = loadValue(v_addr)
400
+ let result = Reflect.get(value, prop)
401
+ storeValue(retval, result)
402
+ },
403
+
404
+ // func valueSet(v ref, p string, x ref)
405
+ 'syscall/js.valueSet': (v_addr, p_ptr, p_len, x_addr) => {
406
+ const v = loadValue(v_addr)
407
+ const p = loadString(p_ptr, p_len)
408
+ const x = loadValue(x_addr)
409
+ Reflect.set(v, p, x)
410
+ },
411
+
412
+ // func valueDelete(v ref, p string)
413
+ 'syscall/js.valueDelete': (v_addr, p_ptr, p_len) => {
414
+ const v = loadValue(v_addr)
415
+ const p = loadString(p_ptr, p_len)
416
+ Reflect.deleteProperty(v, p)
417
+ },
418
+
419
+ // func valueIndex(v ref, i int) ref
420
+ 'syscall/js.valueIndex': (ret_addr, v_addr, i) => {
421
+ storeValue(ret_addr, Reflect.get(loadValue(v_addr), i))
422
+ },
423
+
424
+ // valueSetIndex(v ref, i int, x ref)
425
+ 'syscall/js.valueSetIndex': (v_addr, i, x_addr) => {
426
+ Reflect.set(loadValue(v_addr), i, loadValue(x_addr))
427
+ },
428
+
429
+ // func valueCall(v ref, m string, args []ref) (ref, bool)
430
+ 'syscall/js.valueCall': (ret_addr, v_addr, m_ptr, m_len, args_ptr, args_len, args_cap) => {
431
+ const v = loadValue(v_addr)
432
+ const name = loadString(m_ptr, m_len)
433
+ const args = loadSliceOfValues(args_ptr, args_len, args_cap)
434
+ try {
435
+ const m = Reflect.get(v, name)
436
+ storeValue(ret_addr, Reflect.apply(m, v, args))
437
+ mem().setUint8(ret_addr + 8, 1)
438
+ } catch (err) {
439
+ storeValue(ret_addr, err)
440
+ mem().setUint8(ret_addr + 8, 0)
441
+ }
442
+ },
443
+
444
+ // func valueInvoke(v ref, args []ref) (ref, bool)
445
+ 'syscall/js.valueInvoke': (ret_addr, v_addr, args_ptr, args_len, args_cap) => {
446
+ try {
447
+ const v = loadValue(v_addr)
448
+ const args = loadSliceOfValues(args_ptr, args_len, args_cap)
449
+ storeValue(ret_addr, Reflect.apply(v, undefined, args))
450
+ mem().setUint8(ret_addr + 8, 1)
451
+ } catch (err) {
452
+ storeValue(ret_addr, err)
453
+ mem().setUint8(ret_addr + 8, 0)
454
+ }
455
+ },
456
+
457
+ // func valueNew(v ref, args []ref) (ref, bool)
458
+ 'syscall/js.valueNew': (ret_addr, v_addr, args_ptr, args_len, args_cap) => {
459
+ const v = loadValue(v_addr)
460
+ const args = loadSliceOfValues(args_ptr, args_len, args_cap)
461
+ try {
462
+ storeValue(ret_addr, Reflect.construct(v, args))
463
+ mem().setUint8(ret_addr + 8, 1)
464
+ } catch (err) {
465
+ storeValue(ret_addr, err)
466
+ mem().setUint8(ret_addr + 8, 0)
467
+ }
468
+ },
469
+
470
+ // func valueLength(v ref) int
471
+ 'syscall/js.valueLength': (v_addr) => {
472
+ return loadValue(v_addr).length
473
+ },
474
+
475
+ // valuePrepareString(v ref) (ref, int)
476
+ 'syscall/js.valuePrepareString': (ret_addr, v_addr) => {
477
+ const s = String(loadValue(v_addr))
478
+ const str = encoder.encode(s)
479
+ storeValue(ret_addr, str)
480
+ setInt64(ret_addr + 8, str.length)
481
+ },
482
+
483
+ // valueLoadString(v ref, b []byte)
484
+ 'syscall/js.valueLoadString': (v_addr, slice_ptr, slice_len, slice_cap) => {
485
+ const str = loadValue(v_addr)
486
+ loadSlice(slice_ptr, slice_len, slice_cap).set(str)
487
+ },
488
+
489
+ // func valueInstanceOf(v ref, t ref) bool
490
+ 'syscall/js.valueInstanceOf': (v_addr, t_addr) => {
491
+ return loadValue(v_addr) instanceof loadValue(t_addr)
492
+ },
493
+
494
+ // func copyBytesToGo(dst []byte, src ref) (int, bool)
495
+ 'syscall/js.copyBytesToGo': (ret_addr, dest_addr, dest_len, dest_cap, source_addr) => {
496
+ let num_bytes_copied_addr = ret_addr
497
+ let returned_status_addr = ret_addr + 4 // Address of returned boolean status variable
498
+
499
+ const dst = loadSlice(dest_addr, dest_len)
500
+ const src = loadValue(source_addr)
501
+ if (!(src instanceof Uint8Array || src instanceof Uint8ClampedArray)) {
502
+ mem().setUint8(returned_status_addr, 0) // Return "not ok" status
503
+ return
504
+ }
505
+ const toCopy = src.subarray(0, dst.length)
506
+ dst.set(toCopy)
507
+ setInt64(num_bytes_copied_addr, toCopy.length)
508
+ mem().setUint8(returned_status_addr, 1) // Return "ok" status
509
+ },
510
+
511
+ // copyBytesToJS(dst ref, src []byte) (int, bool)
512
+ // Originally copied from upstream Go project, then modified:
513
+ // https://github.com/golang/go/blob/3f995c3f3b43033013013e6c7ccc93a9b1411ca9/misc/wasm/wasm_exec.js#L404-L416
514
+ 'syscall/js.copyBytesToJS': (ret_addr, dest_addr, source_addr, source_len, source_cap) => {
515
+ let num_bytes_copied_addr = ret_addr
516
+ let returned_status_addr = ret_addr + 4 // Address of returned boolean status variable
517
+
518
+ const dst = loadValue(dest_addr)
519
+ const src = loadSlice(source_addr, source_len)
520
+ if (!(dst instanceof Uint8Array || dst instanceof Uint8ClampedArray)) {
521
+ mem().setUint8(returned_status_addr, 0) // Return "not ok" status
522
+ return
523
+ }
524
+ const toCopy = src.subarray(0, dst.length)
525
+ dst.set(toCopy)
526
+ setInt64(num_bytes_copied_addr, toCopy.length)
527
+ mem().setUint8(returned_status_addr, 1) // Return "ok" status
528
+ },
529
+ },
530
+ }
531
+ }
532
+
533
+ async run(instance) {
534
+ this._inst = instance
535
+ this._values = [
536
+ // JS values that Go currently has references to, indexed by reference id
537
+ NaN,
538
+ 0,
539
+ null,
540
+ true,
541
+ false,
542
+ global,
543
+ this,
544
+ ]
545
+ this._goRefCounts = [] // number of references that Go has to a JS value, indexed by reference id
546
+ this._ids = new Map() // mapping from JS values to reference ids
547
+ this._idPool = [] // unused ids that have been garbage collected
548
+ this.exited = false // whether the Go program has exited
549
+
550
+ const mem = new DataView(this._inst.exports.memory.buffer)
551
+
552
+ while (true) {
553
+ const callbackPromise = new Promise((resolve) => {
554
+ this._resolveCallbackPromise = () => {
555
+ if (this.exited) {
556
+ throw new Error('bad callback: Go program has already exited')
557
+ }
558
+ setTimeout(resolve, 0) // make sure it is asynchronous
559
+ }
560
+ })
561
+ this._inst.exports._start()
562
+ if (this.exited) {
563
+ break
564
+ }
565
+ await callbackPromise
566
+ }
567
+ }
568
+
569
+ _resume() {
570
+ if (this.exited) {
571
+ throw new Error('Go program has already exited')
572
+ }
573
+ this._inst.exports.resume()
574
+ if (this.exited) {
575
+ this._resolveExitPromise()
576
+ }
577
+ }
578
+
579
+ _makeFuncWrapper(id) {
580
+ const go = this
581
+ return function () {
582
+ const event = { id: id, this: this, args: arguments }
583
+ go._pendingEvent = event
584
+ go._resume()
585
+ return event.result
586
+ }
587
+ }
588
+ }
589
+
590
+ export default global.Go