watr 2.2.0 → 2.2.2

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "watr",
3
- "version": "2.2.0",
3
+ "version": "2.2.2",
4
4
  "description": "Ligth & fast WAT compiler",
5
5
  "main": "watr.js",
6
6
  "exports": {
package/readme.md CHANGED
@@ -7,7 +7,7 @@ Useful for hi-level languages or dynamic (in-browser) compilation.<br>
7
7
 
8
8
  &nbsp; | Size (gzipped) | Performance (op/s)
9
9
  ---|---|---
10
- watr | 3.8 kb | 6250
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/compile.js CHANGED
@@ -352,7 +352,7 @@ const iinit = ([op, literal], ctx) => op[0] === 'f' ?
352
352
  [op[1] === '3' ? OP_F32_CONST : OP_F64_CONST, ...(op[1] === '3' ? f32 : f64)(literal), OP_END] :
353
353
  [op[1] === '3' ? OP_I32_CONST : OP_I64_CONST, ...(op[1] === '3' ? leb : bigleb)(literal[0] === '$' ? ctx.global[literal] : literal), OP_END]
354
354
 
355
- const escape = { n: 10, r: 13, t: 9, v: 1 }
355
+ const escape = { n: 10, r: 13, t: 9, v: 1, '\\': 92 }
356
356
 
357
357
  // build string binary
358
358
  const str = str => {
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 = (number, buffer=[]) => {
3
- if (typeof number === 'string') number = parseInt(number.replaceAll('_',''))
2
+ export const uleb = (n, buffer = []) => {
3
+ if (typeof n === 'string') n = parseInt(n.replaceAll('_', ''))
4
4
 
5
- let byte = number & 0b01111111;
6
- number = number >>> 7;
5
+ let byte = n & 0b01111111;
6
+ n = n >>> 7;
7
7
 
8
- if (number === 0) {
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(number, buffer);
13
+ return uleb(n, buffer);
14
14
  }
15
15
  }
16
16
 
17
- export function leb (n, buffer=[]) {
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]==='-'?-BigInt(n.slice(1)):BigInt(n)
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,35 @@ export function bigleb(n, buffer=[]) {
50
50
  }
51
51
 
52
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('_',''))
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 (input.startsWith('0x')) {
61
+ let [sig, exp] = input.split(/p/i), [dec, fract] = sig.split('.');
62
+ sig = parseInt(dec) + (fract ? parseInt(fract, 16) / (16 ** fract.length) : 0)
63
+ return exp ? sig * 2 ** parseInt(exp, 10) : sig;
64
+ }
65
+
66
+ return parseFloat(input)
67
+ }
68
+
55
69
 
56
70
  const byteView = new DataView(new BigInt64Array(1).buffer)
57
71
 
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))
72
+ const F32_SIGN = 0x80000000, F32_NAN = 0x7f800000
73
+ export function f32(input, value, idx) {
74
+ if (~(idx = input.indexOf('nan:'))) {
75
+ value = parseInt(input.slice(idx + 4))
62
76
  value |= F32_NAN
63
77
  if (input[0] === '-') value |= F32_SIGN
64
78
  byteView.setInt32(0, value)
65
79
  }
66
80
  else {
67
- value=typeof input === 'string' ? flt(input) : input
81
+ value = typeof input === 'string' ? flt(input) : input
68
82
  byteView.setFloat32(0, value);
69
83
  }
70
84
 
@@ -76,16 +90,16 @@ export function f32 (input, value, idx) {
76
90
  ];
77
91
  }
78
92
 
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))
93
+ const F64_SIGN = 0x8000000000000000n, F64_NAN = 0x7ff0000000000000n
94
+ export function f64(input, value, idx) {
95
+ if (~(idx = input.indexOf('nan:'))) {
96
+ value = BigInt(input.slice(idx + 4))
83
97
  value |= F64_NAN
84
98
  if (input[0] === '-') value |= F64_SIGN
85
99
  byteView.setBigInt64(0, value)
86
100
  }
87
101
  else {
88
- value=typeof input === 'string' ? flt(input) : input
102
+ value = typeof input === 'string' ? flt(input) : input
89
103
  byteView.setFloat64(0, value);
90
104
  }
91
105