watr 2.2.1 → 2.2.3
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/package.json +1 -1
- package/readme.md +1 -2
- package/src/util.js +38 -23
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -7,7 +7,7 @@ Useful for hi-level languages or dynamic (in-browser) compilation.<br>
|
|
|
7
7
|
|
|
8
8
|
| Size (gzipped) | Performance (op/s)
|
|
9
9
|
---|---|---
|
|
10
|
-
watr | 3.8 kb |
|
|
10
|
+
watr | 3.8 kb | 6000
|
|
11
11
|
[wat-compiler](https://github.com/stagas/wat-compiler) | 6 kb | 348
|
|
12
12
|
[wabt](https://github.com/AssemblyScript/wabt.js) | 300 kb | 574
|
|
13
13
|
|
|
@@ -99,7 +99,6 @@ parse(`(func (export "double") (param f64) (result f64) (f64.mul (local.get 0) (
|
|
|
99
99
|
## Status
|
|
100
100
|
|
|
101
101
|
* [x] wasm core
|
|
102
|
-
* [ ] floating HEX support, eg. `(f32.const 0x1.fffffep+127)`.
|
|
103
102
|
* [x] multiple values
|
|
104
103
|
* [x] bulk memory ops (0 index)
|
|
105
104
|
* [ ] func/ref types
|
package/src/util.js
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
// encoding ref: https://github.com/j-s-n/WebBS/blob/master/compiler/byteCode.js
|
|
2
|
-
export const uleb = (
|
|
3
|
-
if (typeof
|
|
2
|
+
export const uleb = (n, buffer = []) => {
|
|
3
|
+
if (typeof n === 'string') n = parseInt(n.replaceAll('_', ''))
|
|
4
4
|
|
|
5
|
-
let byte =
|
|
6
|
-
|
|
5
|
+
let byte = n & 0b01111111;
|
|
6
|
+
n = n >>> 7;
|
|
7
7
|
|
|
8
|
-
if (
|
|
8
|
+
if (n === 0) {
|
|
9
9
|
buffer.push(byte);
|
|
10
10
|
return buffer;
|
|
11
11
|
} else {
|
|
12
12
|
buffer.push(byte | 0b10000000);
|
|
13
|
-
return uleb(
|
|
13
|
+
return uleb(n, buffer);
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
export function leb
|
|
18
|
-
if (typeof n === 'string') n = parseInt(n.replaceAll('_',''))
|
|
17
|
+
export function leb(n, buffer = []) {
|
|
18
|
+
if (typeof n === 'string') n = parseInt(n.replaceAll('_', ''))
|
|
19
19
|
|
|
20
20
|
while (true) {
|
|
21
21
|
const byte = Number(n & 0x7F)
|
|
@@ -29,10 +29,10 @@ export function leb (n, buffer=[]) {
|
|
|
29
29
|
return buffer
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
export function bigleb(n, buffer=[]) {
|
|
32
|
+
export function bigleb(n, buffer = []) {
|
|
33
33
|
if (typeof n === 'string') {
|
|
34
|
-
n = n.replaceAll('_','')
|
|
35
|
-
n = n[0]==='-'
|
|
34
|
+
n = n.replaceAll('_', '')
|
|
35
|
+
n = n[0] === '-' ? -BigInt(n.slice(1)) : BigInt(n)
|
|
36
36
|
byteView.setBigInt64(0, n)
|
|
37
37
|
n = byteView.getBigInt64(0)
|
|
38
38
|
}
|
|
@@ -50,21 +50,36 @@ export function bigleb(n, buffer=[]) {
|
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
// generalized float cases parser
|
|
53
|
-
const flt = input =>
|
|
54
|
-
|
|
53
|
+
const flt = input => {
|
|
54
|
+
if (input.includes('nan')) return input[0] === '-' ? -NaN : NaN;
|
|
55
|
+
if (input.includes('inf')) return input[0] === '-' ? -Infinity : Infinity;
|
|
56
|
+
|
|
57
|
+
input = input.replaceAll('_', '')
|
|
58
|
+
|
|
59
|
+
// 0x1.5p3
|
|
60
|
+
if (/^[+-]?0x/.test(input)) {
|
|
61
|
+
let sign = input[0] === '-' ? (input = input.slice(1), -1) : 1,
|
|
62
|
+
[sig, exp] = input.split(/p/i), [dec, fract] = sig.split('.')
|
|
63
|
+
sig = parseInt(dec) + (fract ? parseInt(fract, 16) / (16 ** fract.length) : 0)
|
|
64
|
+
return sign * (exp ? sig * 2 ** parseInt(exp, 10) : sig);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return parseFloat(input)
|
|
68
|
+
}
|
|
69
|
+
|
|
55
70
|
|
|
56
71
|
const byteView = new DataView(new BigInt64Array(1).buffer)
|
|
57
72
|
|
|
58
|
-
const F32_SIGN = 0x80000000, F32_NAN
|
|
59
|
-
export function f32
|
|
60
|
-
if (~(idx=input.indexOf('nan:'))) {
|
|
61
|
-
value = parseInt(input.slice(idx+4))
|
|
73
|
+
const F32_SIGN = 0x80000000, F32_NAN = 0x7f800000
|
|
74
|
+
export function f32(input, value, idx) {
|
|
75
|
+
if (~(idx = input.indexOf('nan:'))) {
|
|
76
|
+
value = parseInt(input.slice(idx + 4))
|
|
62
77
|
value |= F32_NAN
|
|
63
78
|
if (input[0] === '-') value |= F32_SIGN
|
|
64
79
|
byteView.setInt32(0, value)
|
|
65
80
|
}
|
|
66
81
|
else {
|
|
67
|
-
value=typeof input === 'string' ? flt(input) : input
|
|
82
|
+
value = typeof input === 'string' ? flt(input) : input
|
|
68
83
|
byteView.setFloat32(0, value);
|
|
69
84
|
}
|
|
70
85
|
|
|
@@ -76,16 +91,16 @@ export function f32 (input, value, idx) {
|
|
|
76
91
|
];
|
|
77
92
|
}
|
|
78
93
|
|
|
79
|
-
const F64_SIGN = 0x8000000000000000n, F64_NAN
|
|
80
|
-
export function f64
|
|
81
|
-
if (~(idx=input.indexOf('nan:'))) {
|
|
82
|
-
value = BigInt(input.slice(idx+4))
|
|
94
|
+
const F64_SIGN = 0x8000000000000000n, F64_NAN = 0x7ff0000000000000n
|
|
95
|
+
export function f64(input, value, idx) {
|
|
96
|
+
if (~(idx = input.indexOf('nan:'))) {
|
|
97
|
+
value = BigInt(input.slice(idx + 4))
|
|
83
98
|
value |= F64_NAN
|
|
84
99
|
if (input[0] === '-') value |= F64_SIGN
|
|
85
100
|
byteView.setBigInt64(0, value)
|
|
86
101
|
}
|
|
87
102
|
else {
|
|
88
|
-
value=typeof input === 'string' ? flt(input) : input
|
|
103
|
+
value = typeof input === 'string' ? flt(input) : input
|
|
89
104
|
byteView.setFloat64(0, value);
|
|
90
105
|
}
|
|
91
106
|
|