wat4wasm 1.0.8 → 1.1.1
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/examples/01-text/module-output.wat +3 -35
- package/examples/02-include/module-output.wat +2 -2
- package/examples/03-ref.extern/module-output.wat +31 -549
- package/examples/04-ref.func/module-output.wat +3 -4
- package/examples/05-global.get/module-output.wat +19 -269
- package/examples/06-async/module-output.wat +5 -215
- package/examples/07-data/module-output.wasm +0 -0
- package/examples/08-reflectors/module-output.wat +50 -264
- package/examples/09-replaceAll/module-output.wat +3 -1
- package/examples/99-complex/apply_1.js +5 -0
- package/examples/99-complex/output.html +1 -1
- package/examples/shell-usages.sh +10 -23
- package/lib/clean.js +27 -30
- package/lib/cli.js +29 -2
- package/lib/index.js +5 -5
- package/package.json +1 -1
- package/wat4wasm +57 -15
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/cli.js
CHANGED
|
@@ -25,6 +25,7 @@ export async function processCLI(compileCallback, PROCESS = process) {
|
|
|
25
25
|
faviconLinkHref: "data:null",
|
|
26
26
|
printOnly: false,
|
|
27
27
|
passthroughArgs: [],
|
|
28
|
+
applyScripts: [],
|
|
28
29
|
};
|
|
29
30
|
|
|
30
31
|
for (let i = 0; i < args.length; i++) {
|
|
@@ -33,6 +34,8 @@ export async function processCLI(compileCallback, PROCESS = process) {
|
|
|
33
34
|
config.outputFile = arg.split("=")[1];
|
|
34
35
|
} else if (arg.startsWith("--input=")) {
|
|
35
36
|
config.inputFile = arg.split("=")[1];
|
|
37
|
+
} else if (arg.startsWith("--apply=")) {
|
|
38
|
+
config.applyScripts.push(arg.split("=")[1]);
|
|
36
39
|
} else if (arg.startsWith("--wat2wasm=")) {
|
|
37
40
|
config.wat2wasmPath = arg.split("=")[1];
|
|
38
41
|
} else if (arg.startsWith("--tag=")) {
|
|
@@ -80,6 +83,19 @@ export async function processCLI(compileCallback, PROCESS = process) {
|
|
|
80
83
|
PROCESS.exit(1);
|
|
81
84
|
}
|
|
82
85
|
|
|
86
|
+
config.inputDirectory = process
|
|
87
|
+
.cwd()
|
|
88
|
+
.replaceAll("file://", "")
|
|
89
|
+
.concat(config
|
|
90
|
+
.inputFile
|
|
91
|
+
.split("/")
|
|
92
|
+
.reverse().slice(1)
|
|
93
|
+
.reverse()
|
|
94
|
+
.join("/")
|
|
95
|
+
)
|
|
96
|
+
.concat("/")
|
|
97
|
+
.replaceAll("//", "/");
|
|
98
|
+
|
|
83
99
|
const isJSTarget = config.outputFile?.endsWith(".js");
|
|
84
100
|
const isHTMLTarget = config.outputFile?.endsWith(".html");
|
|
85
101
|
|
|
@@ -94,7 +110,6 @@ export async function processCLI(compileCallback, PROCESS = process) {
|
|
|
94
110
|
config.outputFile = config.inputFile.replace(/\.wat$/, "-output.wat");
|
|
95
111
|
}
|
|
96
112
|
|
|
97
|
-
|
|
98
113
|
try {
|
|
99
114
|
console.log(`\x1b[0m\x1b[33m🚀 Wat4Wasm: Processing ${config.inputFile}...\x1b[0m`);
|
|
100
115
|
if (!fs.existsSync(config.inputFile)) {
|
|
@@ -104,7 +119,19 @@ export async function processCLI(compileCallback, PROCESS = process) {
|
|
|
104
119
|
const rawCode = fs.readFileSync(config.inputFile, "utf8");
|
|
105
120
|
|
|
106
121
|
// 2. Call the provided compile function
|
|
107
|
-
|
|
122
|
+
let compiled = await compileCallback(rawCode);
|
|
123
|
+
|
|
124
|
+
let applyScript, applyCount = config.applyScripts.length;
|
|
125
|
+
|
|
126
|
+
while (applyScript = config.applyScripts[--applyCount]) {
|
|
127
|
+
const baseName = applyScript.split("/").pop().split(".").reverse().slice(1).reverse().join(".");
|
|
128
|
+
console.log(`\x1b[0m\x1b[34m🎸 apply call[\x1b[35m${applyCount}\x1b[0m] \x1b[32m-->\x1b[0m \x1b[35m${baseName}\x1b[0m(wat)\x1b[0m`);
|
|
129
|
+
const { default: applyFunction } = await import(
|
|
130
|
+
config.inputDirectory.concat(applyScript)
|
|
131
|
+
);
|
|
132
|
+
compiled = await applyFunction(compiled);
|
|
133
|
+
}
|
|
134
|
+
|
|
108
135
|
console.log("\x1b[0m")
|
|
109
136
|
|
|
110
137
|
// 3. Handle Output
|
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/package.json
CHANGED
package/wat4wasm
CHANGED
|
@@ -1693,6 +1693,7 @@ function W4W (wat) {
|
|
|
1693
1693
|
return calls;
|
|
1694
1694
|
}
|
|
1695
1695
|
function clean (wat) {
|
|
1696
|
+
console.log("")
|
|
1696
1697
|
let block, $name;
|
|
1697
1698
|
block = FUNC_WAT4WASM_BLOCK_ONEXTERNREADY(wat);
|
|
1698
1699
|
if (!block.hasAnyBlock) { wat = block.removedRaw(); }
|
|
@@ -1703,26 +1704,35 @@ function clean (wat) {
|
|
|
1703
1704
|
wat = block.removedRaw();
|
|
1704
1705
|
let $starts = FUNC_WAT4WASM_START_CALLS(wat);
|
|
1705
1706
|
if ($starts.length === 1) {
|
|
1706
|
-
|
|
1707
|
+
const $start = $starts.pop();
|
|
1708
|
+
wat = wat.replace(`(start $wat4wasm)`, `(start ${$start})`);
|
|
1709
|
+
console.log(`⚠️ replaced --> \x1b[34m(start \x1b[35m$wat4wasm\x1b[0m --> \x1b[35m${$start}\x1b[0m)\x1b[0m`);
|
|
1707
1710
|
}
|
|
1708
1711
|
let $func = FUNC_WAT4WASM(wat);
|
|
1709
1712
|
if ($func) {
|
|
1713
|
+
console.log(`⚠️ removing --> \x1b[34m(func \x1b[35m$wat4wasm\x1b[0m ...)\x1b[0m`);
|
|
1710
1714
|
wat = $func.removedRaw();
|
|
1711
1715
|
}
|
|
1712
1716
|
block = ELEM_WAT4WASM(wat);
|
|
1713
|
-
if (block.isInitial) {
|
|
1714
|
-
|
|
1715
|
-
|
|
1717
|
+
if (block.isInitial) {
|
|
1718
|
+
console.log(`⚠️ removing --> \x1b[34m(elem \x1b[35m$wat4wasm\x1b[0m ...)\x1b[0m`);
|
|
1719
|
+
wat = block.removedRaw();
|
|
1720
|
+
}
|
|
1716
1721
|
block = DATA_WAT4WASM(wat);
|
|
1717
|
-
if (block.isInitial) {
|
|
1722
|
+
if (block.isInitial) {
|
|
1723
|
+
console.log(`⚠️ removing --> \x1b[34m(data \x1b[35m$wat4wasm\x1b[0m ...)\x1b[0m`);
|
|
1724
|
+
wat = block.removedRaw();
|
|
1725
|
+
}
|
|
1726
|
+
block = TABLE_WAT4WASM(wat);
|
|
1727
|
+
if (block.isInitial) {
|
|
1728
|
+
console.log(`⚠️ removing --> \x1b[34m(table \x1b[35m$wat4wasm\x1b[0m ...)\x1b[0m`);
|
|
1729
|
+
wat = block.removedRaw();
|
|
1730
|
+
}
|
|
1718
1731
|
if (false === helpers.hasBlock(wat, "global.get", { $name: "$wat4wasm" }) &&
|
|
1719
1732
|
false === helpers.hasBlock(wat, "global.set", { $name: "$wat4wasm" })) {
|
|
1733
|
+
console.log(`⚠️ removing --> \x1b[34m(global \x1b[35m$wat4wasm\x1b[0m ...)\x1b[0m`);
|
|
1720
1734
|
wat = GLOBAL_WAT4WASM(wat).removedRaw();
|
|
1721
1735
|
}
|
|
1722
|
-
block = MEMORY_WAT4WASM(wat);
|
|
1723
|
-
if (block && (helpers.containsMemoryOperation(wat) === false)) {
|
|
1724
|
-
wat = block.removedRaw();
|
|
1725
|
-
}
|
|
1726
1736
|
}
|
|
1727
1737
|
const maskSet = new helpers.MaskSet(wat);
|
|
1728
1738
|
const imports = new Array();
|
|
@@ -1757,8 +1767,16 @@ function clean (wat) {
|
|
|
1757
1767
|
unusedCalls.forEach($name => {
|
|
1758
1768
|
const begin = wat.lastIndexOf(`(func ${$name}`);
|
|
1759
1769
|
const block = helpers.parseBlockAt(wat, begin);
|
|
1770
|
+
$name = $name.replace(".prototype.", ":")
|
|
1771
|
+
console.log(`⚠️ removing unused --> \x1b[34m(func \x1b[32m${$name}\x1b[0m ...)\x1b[0m`);
|
|
1760
1772
|
if (block) { wat = block.removedRaw() }
|
|
1761
1773
|
});
|
|
1774
|
+
const nonWat4WasmMemory = wat.replace(WAT4WASM_BLOCKS.memory, "");
|
|
1775
|
+
if (nonWat4WasmMemory.match(/\(memory\s+(\$|\d)/)) {
|
|
1776
|
+
console.log(`⚠️ removing --> \x1b[34m(memory \x1b[35m$wat4wasm\x1b[0m ...)\x1b[0m`);
|
|
1777
|
+
wat = nonWat4WasmMemory;
|
|
1778
|
+
}
|
|
1779
|
+
console.log("")
|
|
1762
1780
|
return wat;
|
|
1763
1781
|
}
|
|
1764
1782
|
|
|
@@ -1785,6 +1803,7 @@ async function processCLI(compileCallback, PROCESS = process) {
|
|
|
1785
1803
|
faviconLinkHref: "data:null",
|
|
1786
1804
|
printOnly: false,
|
|
1787
1805
|
passthroughArgs: [],
|
|
1806
|
+
applyScripts: [],
|
|
1788
1807
|
};
|
|
1789
1808
|
for (let i = 0; i < args.length; i++) {
|
|
1790
1809
|
const arg = args[i];
|
|
@@ -1792,6 +1811,8 @@ async function processCLI(compileCallback, PROCESS = process) {
|
|
|
1792
1811
|
config.outputFile = arg.split("=")[1];
|
|
1793
1812
|
} else if (arg.startsWith("--input=")) {
|
|
1794
1813
|
config.inputFile = arg.split("=")[1];
|
|
1814
|
+
} else if (arg.startsWith("--apply=")) {
|
|
1815
|
+
config.applyScripts.push(arg.split("=")[1]);
|
|
1795
1816
|
} else if (arg.startsWith("--wat2wasm=")) {
|
|
1796
1817
|
config.wat2wasmPath = arg.split("=")[1];
|
|
1797
1818
|
} else if (arg.startsWith("--tag=")) {
|
|
@@ -1837,6 +1858,18 @@ async function processCLI(compileCallback, PROCESS = process) {
|
|
|
1837
1858
|
console.error("Error: Input file required! Usage: wat4wasm <input.wat> [options]");
|
|
1838
1859
|
PROCESS.exit(1);
|
|
1839
1860
|
}
|
|
1861
|
+
config.inputDirectory = process
|
|
1862
|
+
.cwd()
|
|
1863
|
+
.replaceAll("file://", "")
|
|
1864
|
+
.concat(config
|
|
1865
|
+
.inputFile
|
|
1866
|
+
.split("/")
|
|
1867
|
+
.reverse().slice(1)
|
|
1868
|
+
.reverse()
|
|
1869
|
+
.join("/")
|
|
1870
|
+
)
|
|
1871
|
+
.concat("/")
|
|
1872
|
+
.replaceAll("//", "/");
|
|
1840
1873
|
const isJSTarget = config.outputFile?.endsWith(".js");
|
|
1841
1874
|
const isHTMLTarget = config.outputFile?.endsWith(".html");
|
|
1842
1875
|
if (isHTMLTarget || isJSTarget) {
|
|
@@ -1855,7 +1888,16 @@ async function processCLI(compileCallback, PROCESS = process) {
|
|
|
1855
1888
|
}
|
|
1856
1889
|
const rawCode = fs.readFileSync(config.inputFile, "utf8");
|
|
1857
1890
|
// 2. Call the provided compile function
|
|
1858
|
-
|
|
1891
|
+
let compiled = await compileCallback(rawCode);
|
|
1892
|
+
let applyScript, applyCount = config.applyScripts.length;
|
|
1893
|
+
while (applyScript = config.applyScripts[--applyCount]) {
|
|
1894
|
+
const baseName = applyScript.split("/").pop().split(".").reverse().slice(1).reverse().join(".");
|
|
1895
|
+
console.log(`\x1b[0m\x1b[34m🎸 apply call[\x1b[35m${applyCount}\x1b[0m] \x1b[32m-->\x1b[0m \x1b[35m${baseName}\x1b[0m(wat)\x1b[0m`);
|
|
1896
|
+
const { default: applyFunction } = await import(
|
|
1897
|
+
config.inputDirectory.concat(applyScript)
|
|
1898
|
+
);
|
|
1899
|
+
compiled = await applyFunction(compiled);
|
|
1900
|
+
}
|
|
1859
1901
|
console.log("\x1b[0m")
|
|
1860
1902
|
// 3. Handle Output
|
|
1861
1903
|
if (config.printOnly) {
|
|
@@ -2033,12 +2075,12 @@ processCLI(async wat4 => {
|
|
|
2033
2075
|
const addedLines = outLines.length - commonLines;
|
|
2034
2076
|
const netChange = outLines.length - inLines.length;
|
|
2035
2077
|
const stat = [
|
|
2036
|
-
`\x1b[32m+${addedLines}\x1b[0m`.padStart(
|
|
2037
|
-
`\x1b[34m-${removedLines}\x1b[0m`.padStart(
|
|
2038
|
-
`\x1b[${netChange && 36 || 33}m\u0394\x1b[0m`.padStart(
|
|
2039
|
-
`\x1b[${netChange && 36 || 33}m${netChange}\x1b[0m`.padStart(
|
|
2078
|
+
`\x1b[32m+${addedLines}\x1b[0m`.padStart(16, " "),
|
|
2079
|
+
`\x1b[34m-${removedLines}\x1b[0m`.padStart(16, " "),
|
|
2080
|
+
`\x1b[${netChange && 36 || 33}m\u0394\x1b[0m`.padStart(14, " "),
|
|
2081
|
+
`\x1b[${netChange && 36 || 33}m${netChange}\x1b[0m`.padStart(14, " "),
|
|
2040
2082
|
` byte(\u03B4) :`,
|
|
2041
|
-
`\x1b[33m${wat2.length}\x1b[0m`.padStart(
|
|
2083
|
+
`\x1b[33m${wat2.length}\x1b[0m`.padStart(16, " "),
|
|
2042
2084
|
`-->`,
|
|
2043
2085
|
`\x1b[33m${wat4.length}\x1b[0m`,
|
|
2044
2086
|
];
|