watr 1.3.2 → 1.4.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.
package/src/util.js ADDED
@@ -0,0 +1,102 @@
1
+ // encoding ref: https://github.com/j-s-n/WebBS/blob/master/compiler/byteCode.js
2
+ export const uleb = (number, buffer=[]) => {
3
+ if (typeof number === 'string') number = parseInt(number.replaceAll('_',''))
4
+
5
+ let byte = number & 0b01111111;
6
+ number = number >>> 7;
7
+
8
+ if (number === 0) {
9
+ buffer.push(byte);
10
+ return buffer;
11
+ } else {
12
+ buffer.push(byte | 0b10000000);
13
+ return uleb(number, buffer);
14
+ }
15
+ }
16
+
17
+ export function leb (n, buffer=[]) {
18
+ if (typeof n === 'string') n = parseInt(n.replaceAll('_',''))
19
+
20
+ while (true) {
21
+ const byte = Number(n & 0x7F)
22
+ n >>= 7
23
+ if ((n === 0 && (byte & 0x40) === 0) || (n === -1 && (byte & 0x40) !== 0)) {
24
+ buffer.push(byte)
25
+ break
26
+ }
27
+ buffer.push((byte | 0x80))
28
+ }
29
+ return buffer
30
+ }
31
+
32
+ export function bigleb(n, buffer=[]) {
33
+ if (typeof n === 'string') {
34
+ n = n.replaceAll('_','')
35
+ n = n[0]==='-'?-BigInt(n.slice(1)):BigInt(n)
36
+ byteView.setBigInt64(0, n)
37
+ n = byteView.getBigInt64(0)
38
+ }
39
+
40
+ while (true) {
41
+ const byte = Number(n & 0x7Fn)
42
+ n >>= 7n
43
+ if ((n === 0n && (byte & 0x40) === 0) || (n === -1n && (byte & 0x40) !== 0)) {
44
+ buffer.push(byte)
45
+ break
46
+ }
47
+ buffer.push((byte | 0x80))
48
+ }
49
+ return buffer
50
+ }
51
+
52
+ // generalized float cases parser
53
+ const flt = input => input==='nan'||input==='+nan'?NaN:input==='-nan'?-NaN:
54
+ input==='inf'||input==='+inf'?Infinity:input==='-inf'?-Infinity:parseFloat(input.replaceAll('_',''))
55
+
56
+ const byteView = new DataView(new BigInt64Array(1).buffer)
57
+
58
+ const F32_SIGN = 0x80000000, F32_NAN = 0x7f800000
59
+ export function f32 (input, value, idx) {
60
+ if (~(idx=input.indexOf('nan:'))) {
61
+ value = parseInt(input.slice(idx+4))
62
+ value |= F32_NAN
63
+ if (input[0] === '-') value |= F32_SIGN
64
+ byteView.setInt32(0, value)
65
+ }
66
+ else {
67
+ value=typeof input === 'string' ? flt(input) : input
68
+ byteView.setFloat32(0, value);
69
+ }
70
+
71
+ return [
72
+ byteView.getUint8(3),
73
+ byteView.getUint8(2),
74
+ byteView.getUint8(1),
75
+ byteView.getUint8(0)
76
+ ];
77
+ }
78
+
79
+ const F64_SIGN = 0x8000000000000000n, F64_NAN = 0x7ff0000000000000n
80
+ export function f64 (input, value, idx) {
81
+ if (~(idx=input.indexOf('nan:'))) {
82
+ value = BigInt(input.slice(idx+4))
83
+ value |= F64_NAN
84
+ if (input[0] === '-') value |= F64_SIGN
85
+ byteView.setBigInt64(0, value)
86
+ }
87
+ else {
88
+ value=typeof input === 'string' ? flt(input) : input
89
+ byteView.setFloat64(0, value);
90
+ }
91
+
92
+ return [
93
+ byteView.getUint8(7),
94
+ byteView.getUint8(6),
95
+ byteView.getUint8(5),
96
+ byteView.getUint8(4),
97
+ byteView.getUint8(3),
98
+ byteView.getUint8(2),
99
+ byteView.getUint8(1),
100
+ byteView.getUint8(0)
101
+ ];
102
+ }