wat4wasm 1.1.0 → 1.1.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/lib/clean.js +27 -30
- package/lib/index.js +5 -5
- package/lib/processors/start.js +1 -1
- package/lib/processors/text.js +24 -6
- package/package.json +1 -1
- package/wat4wasm +52 -20
package/lib/clean.js
CHANGED
|
@@ -26,58 +26,54 @@ function FUNC_WAT4WASM_START_CALLS(wat) {
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
export default function (wat) {
|
|
29
|
+
console.log("")
|
|
29
30
|
let block, $name;
|
|
30
|
-
|
|
31
31
|
block = FUNC_WAT4WASM_BLOCK_ONEXTERNREADY(wat);
|
|
32
32
|
if (!block.hasAnyBlock) { wat = block.removedRaw(); }
|
|
33
|
-
|
|
34
33
|
block = FUNC_WAT4WASM_BLOCK_ONTEXTREADY(wat);
|
|
35
34
|
if (!block.hasAnyBlock) { wat = block.removedRaw(); }
|
|
36
|
-
|
|
37
35
|
block = FUNC_WAT4WASM_BLOCK_ONINIT(wat);
|
|
38
36
|
if (!block.hasAnyBlock) {
|
|
39
|
-
|
|
40
37
|
wat = block.removedRaw();
|
|
41
|
-
|
|
42
38
|
let $starts = FUNC_WAT4WASM_START_CALLS(wat);
|
|
43
39
|
if ($starts.length === 1) {
|
|
44
|
-
|
|
40
|
+
const $start = $starts.pop();
|
|
41
|
+
wat = wat.replace(`(start $wat4wasm)`, `(start ${$start})`);
|
|
42
|
+
console.log(`⚠️ replaced --> \x1b[34m(start \x1b[35m$wat4wasm\x1b[0m --> \x1b[35m${$start}\x1b[0m)\x1b[0m`);
|
|
45
43
|
}
|
|
46
|
-
|
|
47
44
|
let $func = FUNC_WAT4WASM(wat);
|
|
48
45
|
if ($func) {
|
|
46
|
+
console.log(`⚠️ removing --> \x1b[34m(func \x1b[35m$wat4wasm\x1b[0m ...)\x1b[0m`);
|
|
49
47
|
wat = $func.removedRaw();
|
|
50
48
|
}
|
|
51
|
-
|
|
52
49
|
block = ELEM_WAT4WASM(wat);
|
|
53
|
-
if (block.isInitial) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
50
|
+
if (block.isInitial) {
|
|
51
|
+
console.log(`⚠️ removing --> \x1b[34m(elem \x1b[35m$wat4wasm\x1b[0m ...)\x1b[0m`);
|
|
52
|
+
wat = block.removedRaw();
|
|
53
|
+
}
|
|
58
54
|
block = DATA_WAT4WASM(wat);
|
|
59
|
-
if (block.isInitial) {
|
|
60
|
-
|
|
55
|
+
if (block.isInitial) {
|
|
56
|
+
console.log(`⚠️ removing --> \x1b[34m(data \x1b[35m$wat4wasm\x1b[0m ...)\x1b[0m`);
|
|
57
|
+
wat = block.removedRaw();
|
|
58
|
+
}
|
|
59
|
+
block = TABLE_WAT4WASM(wat);
|
|
60
|
+
if (block.isInitial) {
|
|
61
|
+
console.log(`⚠️ removing --> \x1b[34m(table \x1b[35m$wat4wasm\x1b[0m ...)\x1b[0m`);
|
|
62
|
+
wat = block.removedRaw();
|
|
63
|
+
}
|
|
61
64
|
if (false === helpers.hasBlock(wat, "global.get", { $name: "$wat4wasm" }) &&
|
|
62
65
|
false === helpers.hasBlock(wat, "global.set", { $name: "$wat4wasm" })) {
|
|
66
|
+
console.log(`⚠️ removing --> \x1b[34m(global \x1b[35m$wat4wasm\x1b[0m ...)\x1b[0m`);
|
|
63
67
|
wat = GLOBAL_WAT4WASM(wat).removedRaw();
|
|
64
68
|
}
|
|
65
|
-
|
|
66
|
-
block = MEMORY_WAT4WASM(wat);
|
|
67
|
-
if (block && (helpers.containsMemoryOperation(wat) === false)) {
|
|
68
|
-
wat = block.removedRaw();
|
|
69
|
-
}
|
|
70
69
|
}
|
|
71
|
-
|
|
72
70
|
const maskSet = new helpers.MaskSet(wat);
|
|
73
71
|
const imports = new Array();
|
|
74
|
-
|
|
75
72
|
while (block = maskSet.lastBlockOf("import")) {
|
|
76
73
|
block.$name = helpers.parseFirstBlock(block).$name;
|
|
77
74
|
imports.push(block);
|
|
78
75
|
maskSet.remove(block);
|
|
79
76
|
}
|
|
80
|
-
|
|
81
77
|
wat = maskSet.restore();
|
|
82
78
|
wat = helpers.prepend(wat,
|
|
83
79
|
imports
|
|
@@ -86,32 +82,33 @@ export default function (wat) {
|
|
|
86
82
|
.map(b => b.toString())
|
|
87
83
|
.join("\n")
|
|
88
84
|
);
|
|
89
|
-
|
|
90
|
-
|
|
91
85
|
const funcMask = new helpers.MaskSet(wat);
|
|
92
86
|
const unusedCalls = new Set();
|
|
93
|
-
|
|
94
87
|
funcMask.maskAll("import");
|
|
95
88
|
funcMask.maskComments();
|
|
96
|
-
|
|
97
89
|
while (block = funcMask.lastBlockOf("func")) {
|
|
98
90
|
funcMask.mask(block);
|
|
99
91
|
if (($name = block.$name)) {
|
|
100
92
|
if (wat.match(new RegExp(`\\\((call|start|ref\\.func)\\s+\\${$name}(\\s|\\\))`))) {
|
|
101
93
|
continue;
|
|
102
94
|
}
|
|
103
|
-
|
|
104
95
|
if (unusedCalls.has($name) === false) {
|
|
105
96
|
unusedCalls.add($name);
|
|
106
97
|
}
|
|
107
98
|
}
|
|
108
99
|
}
|
|
109
|
-
|
|
110
100
|
unusedCalls.forEach($name => {
|
|
111
101
|
const begin = wat.lastIndexOf(`(func ${$name}`);
|
|
112
102
|
const block = helpers.parseBlockAt(wat, begin);
|
|
103
|
+
$name = $name.replace(".prototype.", ":")
|
|
104
|
+
console.log(`⚠️ removing unused --> \x1b[34m(func \x1b[32m${$name}\x1b[0m ...)\x1b[0m`);
|
|
113
105
|
if (block) { wat = block.removedRaw() }
|
|
114
106
|
});
|
|
115
|
-
|
|
107
|
+
const nonWat4WasmMemory = wat.replace(WAT4WASM_BLOCKS.memory, "");
|
|
108
|
+
if (nonWat4WasmMemory.match(/\(memory\s+(\$|\d)/)) {
|
|
109
|
+
console.log(`⚠️ removing --> \x1b[34m(memory \x1b[35m$wat4wasm\x1b[0m ...)\x1b[0m`);
|
|
110
|
+
wat = nonWat4WasmMemory;
|
|
111
|
+
}
|
|
112
|
+
console.log("")
|
|
116
113
|
return wat;
|
|
117
114
|
}
|
package/lib/index.js
CHANGED
|
@@ -68,12 +68,12 @@ processCLI(async wat4 => {
|
|
|
68
68
|
const netChange = outLines.length - inLines.length;
|
|
69
69
|
|
|
70
70
|
const stat = [
|
|
71
|
-
`\x1b[32m+${addedLines}\x1b[0m`.padStart(
|
|
72
|
-
`\x1b[34m-${removedLines}\x1b[0m`.padStart(
|
|
73
|
-
`\x1b[${netChange && 36 || 33}m\u0394\x1b[0m`.padStart(
|
|
74
|
-
`\x1b[${netChange && 36 || 33}m${netChange}\x1b[0m`.padStart(
|
|
71
|
+
`\x1b[32m+${addedLines}\x1b[0m`.padStart(16, " "),
|
|
72
|
+
`\x1b[34m-${removedLines}\x1b[0m`.padStart(16, " "),
|
|
73
|
+
`\x1b[${netChange && 36 || 33}m\u0394\x1b[0m`.padStart(14, " "),
|
|
74
|
+
`\x1b[${netChange && 36 || 33}m${netChange}\x1b[0m`.padStart(14, " "),
|
|
75
75
|
` byte(\u03B4) :`,
|
|
76
|
-
`\x1b[33m${wat2.length}\x1b[0m`.padStart(
|
|
76
|
+
`\x1b[33m${wat2.length}\x1b[0m`.padStart(16, " "),
|
|
77
77
|
`-->`,
|
|
78
78
|
`\x1b[33m${wat4.length}\x1b[0m`,
|
|
79
79
|
];
|
package/lib/processors/start.js
CHANGED
|
@@ -4,10 +4,10 @@ export const START_BLOCK_NAME = "start";
|
|
|
4
4
|
|
|
5
5
|
export default function (wat, WAT4WASM) {
|
|
6
6
|
let startCalls = [];
|
|
7
|
-
let removedWat = wat;
|
|
8
7
|
|
|
9
8
|
wat = wat.replaceAll(/\(main(\s+)(\$.[^\s]*)(\s)/g, `(start$1$2)\n\n(func$1$2$3`)
|
|
10
9
|
|
|
10
|
+
let removedWat = wat;
|
|
11
11
|
while (helpers.hasBlock(removedWat, START_BLOCK_NAME)) {
|
|
12
12
|
let block = helpers.lastBlockOf(removedWat, START_BLOCK_NAME);
|
|
13
13
|
removedWat = block.removedRaw();
|
package/lib/processors/text.js
CHANGED
|
@@ -59,12 +59,13 @@ const TEXT_ONINIT_BLOCK = (offset, length, setter) => String(
|
|
|
59
59
|
)
|
|
60
60
|
`).trim();
|
|
61
61
|
|
|
62
|
-
const extern = new Set();
|
|
63
62
|
|
|
64
|
-
|
|
63
|
+
function replaceAllText(wat, WAT4WASM) {
|
|
65
64
|
|
|
65
|
+
const extern = new Set();
|
|
66
66
|
const maskSet = new helpers.MaskSet(wat);
|
|
67
67
|
const textBlocks = new Array();
|
|
68
|
+
const textGetters = new Map();
|
|
68
69
|
|
|
69
70
|
while (maskSet.hasBlock(TEXT_BLOCK_NAME)) {
|
|
70
71
|
let block = maskSet.lastBlockOf(TEXT_BLOCK_NAME);
|
|
@@ -89,6 +90,8 @@ export default function (wat, WAT4WASM) {
|
|
|
89
90
|
block.isExtended = true;
|
|
90
91
|
|
|
91
92
|
wat = dataRequest.modifiedRaw;
|
|
93
|
+
|
|
94
|
+
textGetters.set(block.uuid, text)
|
|
92
95
|
}
|
|
93
96
|
else {
|
|
94
97
|
block.isExtended = false;
|
|
@@ -105,13 +108,10 @@ export default function (wat, WAT4WASM) {
|
|
|
105
108
|
(local.get $arguments) ;; ${block.strPreview}
|
|
106
109
|
)`).trim();
|
|
107
110
|
|
|
111
|
+
textGetters.set(textGetters.get(block.uuid), block.tableGetter)
|
|
108
112
|
wat = growRequest.modifiedRaw;
|
|
109
113
|
});
|
|
110
114
|
|
|
111
|
-
textBlocks.filter(block => block.isExtended).forEach(block => {
|
|
112
|
-
wat = wat.replaceAll(block.toString(), block.tableGetter);
|
|
113
|
-
});
|
|
114
|
-
|
|
115
115
|
const oninit = textBlocks.filter(block => block.isExtended).map(block =>
|
|
116
116
|
TEXT_ONINIT_BLOCK(
|
|
117
117
|
block.dataOffset,
|
|
@@ -123,5 +123,23 @@ export default function (wat, WAT4WASM) {
|
|
|
123
123
|
wat = APPEND_ON_INIT(wat, oninit);
|
|
124
124
|
}
|
|
125
125
|
|
|
126
|
+
while (helpers.hasBlock(wat, "text")) {
|
|
127
|
+
const block = helpers.lastBlockOf(wat, "text");
|
|
128
|
+
const text = helpers.findQuotedText(block);
|
|
129
|
+
const getter = textGetters.get(text);
|
|
130
|
+
|
|
131
|
+
if (getter) {
|
|
132
|
+
wat = block.replacedRaw(getter)
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return wat;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export default function (wat) {
|
|
140
|
+
while (helpers.hasBlock(wat, "text")) {
|
|
141
|
+
wat = replaceAllText(wat);
|
|
142
|
+
}
|
|
143
|
+
|
|
126
144
|
return wat;
|
|
127
145
|
}
|
package/package.json
CHANGED
package/wat4wasm
CHANGED
|
@@ -1277,8 +1277,8 @@ function REPLACE_ALL (wat) {
|
|
|
1277
1277
|
const START_BLOCK_NAME = "start";
|
|
1278
1278
|
function START (wat, WAT4WASM) {
|
|
1279
1279
|
let startCalls = [];
|
|
1280
|
-
let removedWat = wat;
|
|
1281
1280
|
wat = wat.replaceAll(/\(main(\s+)(\$.[^\s]*)(\s)/g, `(start$1$2)\n\n(func$1$2$3`)
|
|
1281
|
+
let removedWat = wat;
|
|
1282
1282
|
while (helpers.hasBlock(removedWat, START_BLOCK_NAME)) {
|
|
1283
1283
|
let block = helpers.lastBlockOf(removedWat, START_BLOCK_NAME);
|
|
1284
1284
|
removedWat = block.removedRaw();
|
|
@@ -1406,10 +1406,11 @@ const TEXT_ONINIT_BLOCK = (offset, length, setter) => String(
|
|
|
1406
1406
|
${setter}
|
|
1407
1407
|
)
|
|
1408
1408
|
`).trim();
|
|
1409
|
-
|
|
1410
|
-
|
|
1409
|
+
function replaceAllText(wat, WAT4WASM) {
|
|
1410
|
+
const extern = new Set();
|
|
1411
1411
|
const maskSet = new helpers.MaskSet(wat);
|
|
1412
1412
|
const textBlocks = new Array();
|
|
1413
|
+
const textGetters = new Map();
|
|
1413
1414
|
while (maskSet.hasBlock(TEXT_BLOCK_NAME)) {
|
|
1414
1415
|
let block = maskSet.lastBlockOf(TEXT_BLOCK_NAME);
|
|
1415
1416
|
maskSet.mask(block);
|
|
@@ -1428,6 +1429,7 @@ function TEXT (wat, WAT4WASM) {
|
|
|
1428
1429
|
block.strContent = text;
|
|
1429
1430
|
block.isExtended = true;
|
|
1430
1431
|
wat = dataRequest.modifiedRaw;
|
|
1432
|
+
textGetters.set(block.uuid, text)
|
|
1431
1433
|
}
|
|
1432
1434
|
else {
|
|
1433
1435
|
block.isExtended = false;
|
|
@@ -1442,11 +1444,9 @@ function TEXT (wat, WAT4WASM) {
|
|
|
1442
1444
|
(local.get $textDecoder)
|
|
1443
1445
|
(local.get $arguments) ;; ${block.strPreview}
|
|
1444
1446
|
)`).trim();
|
|
1447
|
+
textGetters.set(textGetters.get(block.uuid), block.tableGetter)
|
|
1445
1448
|
wat = growRequest.modifiedRaw;
|
|
1446
1449
|
});
|
|
1447
|
-
textBlocks.filter(block => block.isExtended).forEach(block => {
|
|
1448
|
-
wat = wat.replaceAll(block.toString(), block.tableGetter);
|
|
1449
|
-
});
|
|
1450
1450
|
const oninit = textBlocks.filter(block => block.isExtended).map(block =>
|
|
1451
1451
|
TEXT_ONINIT_BLOCK(
|
|
1452
1452
|
block.dataOffset,
|
|
@@ -1456,6 +1456,20 @@ function TEXT (wat, WAT4WASM) {
|
|
|
1456
1456
|
if (oninit.trim()) {
|
|
1457
1457
|
wat = APPEND_ON_INIT(wat, oninit);
|
|
1458
1458
|
}
|
|
1459
|
+
while (helpers.hasBlock(wat, "text")) {
|
|
1460
|
+
const block = helpers.lastBlockOf(wat, "text");
|
|
1461
|
+
const text = helpers.findQuotedText(block);
|
|
1462
|
+
const getter = textGetters.get(text);
|
|
1463
|
+
if (getter) {
|
|
1464
|
+
wat = block.replacedRaw(getter)
|
|
1465
|
+
}
|
|
1466
|
+
}
|
|
1467
|
+
return wat;
|
|
1468
|
+
}
|
|
1469
|
+
function TEXT (wat) {
|
|
1470
|
+
while (helpers.hasBlock(wat, "text")) {
|
|
1471
|
+
wat = replaceAllText(wat);
|
|
1472
|
+
}
|
|
1459
1473
|
return wat;
|
|
1460
1474
|
}
|
|
1461
1475
|
|
|
@@ -1693,6 +1707,7 @@ function W4W (wat) {
|
|
|
1693
1707
|
return calls;
|
|
1694
1708
|
}
|
|
1695
1709
|
function clean (wat) {
|
|
1710
|
+
console.log("")
|
|
1696
1711
|
let block, $name;
|
|
1697
1712
|
block = FUNC_WAT4WASM_BLOCK_ONEXTERNREADY(wat);
|
|
1698
1713
|
if (!block.hasAnyBlock) { wat = block.removedRaw(); }
|
|
@@ -1703,26 +1718,35 @@ function clean (wat) {
|
|
|
1703
1718
|
wat = block.removedRaw();
|
|
1704
1719
|
let $starts = FUNC_WAT4WASM_START_CALLS(wat);
|
|
1705
1720
|
if ($starts.length === 1) {
|
|
1706
|
-
|
|
1721
|
+
const $start = $starts.pop();
|
|
1722
|
+
wat = wat.replace(`(start $wat4wasm)`, `(start ${$start})`);
|
|
1723
|
+
console.log(`⚠️ replaced --> \x1b[34m(start \x1b[35m$wat4wasm\x1b[0m --> \x1b[35m${$start}\x1b[0m)\x1b[0m`);
|
|
1707
1724
|
}
|
|
1708
1725
|
let $func = FUNC_WAT4WASM(wat);
|
|
1709
1726
|
if ($func) {
|
|
1727
|
+
console.log(`⚠️ removing --> \x1b[34m(func \x1b[35m$wat4wasm\x1b[0m ...)\x1b[0m`);
|
|
1710
1728
|
wat = $func.removedRaw();
|
|
1711
1729
|
}
|
|
1712
1730
|
block = ELEM_WAT4WASM(wat);
|
|
1713
|
-
if (block.isInitial) {
|
|
1714
|
-
|
|
1715
|
-
|
|
1731
|
+
if (block.isInitial) {
|
|
1732
|
+
console.log(`⚠️ removing --> \x1b[34m(elem \x1b[35m$wat4wasm\x1b[0m ...)\x1b[0m`);
|
|
1733
|
+
wat = block.removedRaw();
|
|
1734
|
+
}
|
|
1716
1735
|
block = DATA_WAT4WASM(wat);
|
|
1717
|
-
if (block.isInitial) {
|
|
1736
|
+
if (block.isInitial) {
|
|
1737
|
+
console.log(`⚠️ removing --> \x1b[34m(data \x1b[35m$wat4wasm\x1b[0m ...)\x1b[0m`);
|
|
1738
|
+
wat = block.removedRaw();
|
|
1739
|
+
}
|
|
1740
|
+
block = TABLE_WAT4WASM(wat);
|
|
1741
|
+
if (block.isInitial) {
|
|
1742
|
+
console.log(`⚠️ removing --> \x1b[34m(table \x1b[35m$wat4wasm\x1b[0m ...)\x1b[0m`);
|
|
1743
|
+
wat = block.removedRaw();
|
|
1744
|
+
}
|
|
1718
1745
|
if (false === helpers.hasBlock(wat, "global.get", { $name: "$wat4wasm" }) &&
|
|
1719
1746
|
false === helpers.hasBlock(wat, "global.set", { $name: "$wat4wasm" })) {
|
|
1747
|
+
console.log(`⚠️ removing --> \x1b[34m(global \x1b[35m$wat4wasm\x1b[0m ...)\x1b[0m`);
|
|
1720
1748
|
wat = GLOBAL_WAT4WASM(wat).removedRaw();
|
|
1721
1749
|
}
|
|
1722
|
-
block = MEMORY_WAT4WASM(wat);
|
|
1723
|
-
if (block && (helpers.containsMemoryOperation(wat) === false)) {
|
|
1724
|
-
wat = block.removedRaw();
|
|
1725
|
-
}
|
|
1726
1750
|
}
|
|
1727
1751
|
const maskSet = new helpers.MaskSet(wat);
|
|
1728
1752
|
const imports = new Array();
|
|
@@ -1757,8 +1781,16 @@ function clean (wat) {
|
|
|
1757
1781
|
unusedCalls.forEach($name => {
|
|
1758
1782
|
const begin = wat.lastIndexOf(`(func ${$name}`);
|
|
1759
1783
|
const block = helpers.parseBlockAt(wat, begin);
|
|
1784
|
+
$name = $name.replace(".prototype.", ":")
|
|
1785
|
+
console.log(`⚠️ removing unused --> \x1b[34m(func \x1b[32m${$name}\x1b[0m ...)\x1b[0m`);
|
|
1760
1786
|
if (block) { wat = block.removedRaw() }
|
|
1761
1787
|
});
|
|
1788
|
+
const nonWat4WasmMemory = wat.replace(WAT4WASM_BLOCKS.memory, "");
|
|
1789
|
+
if (nonWat4WasmMemory.match(/\(memory\s+(\$|\d)/)) {
|
|
1790
|
+
console.log(`⚠️ removing --> \x1b[34m(memory \x1b[35m$wat4wasm\x1b[0m ...)\x1b[0m`);
|
|
1791
|
+
wat = nonWat4WasmMemory;
|
|
1792
|
+
}
|
|
1793
|
+
console.log("")
|
|
1762
1794
|
return wat;
|
|
1763
1795
|
}
|
|
1764
1796
|
|
|
@@ -2057,12 +2089,12 @@ processCLI(async wat4 => {
|
|
|
2057
2089
|
const addedLines = outLines.length - commonLines;
|
|
2058
2090
|
const netChange = outLines.length - inLines.length;
|
|
2059
2091
|
const stat = [
|
|
2060
|
-
`\x1b[32m+${addedLines}\x1b[0m`.padStart(
|
|
2061
|
-
`\x1b[34m-${removedLines}\x1b[0m`.padStart(
|
|
2062
|
-
`\x1b[${netChange && 36 || 33}m\u0394\x1b[0m`.padStart(
|
|
2063
|
-
`\x1b[${netChange && 36 || 33}m${netChange}\x1b[0m`.padStart(
|
|
2092
|
+
`\x1b[32m+${addedLines}\x1b[0m`.padStart(16, " "),
|
|
2093
|
+
`\x1b[34m-${removedLines}\x1b[0m`.padStart(16, " "),
|
|
2094
|
+
`\x1b[${netChange && 36 || 33}m\u0394\x1b[0m`.padStart(14, " "),
|
|
2095
|
+
`\x1b[${netChange && 36 || 33}m${netChange}\x1b[0m`.padStart(14, " "),
|
|
2064
2096
|
` byte(\u03B4) :`,
|
|
2065
|
-
`\x1b[33m${wat2.length}\x1b[0m`.padStart(
|
|
2097
|
+
`\x1b[33m${wat2.length}\x1b[0m`.padStart(16, " "),
|
|
2066
2098
|
`-->`,
|
|
2067
2099
|
`\x1b[33m${wat4.length}\x1b[0m`,
|
|
2068
2100
|
];
|