wat4wasm 1.0.1 → 1.0.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/README.md +48 -4
- package/assets/ss-fullview.png +0 -0
- package/assets/ss-in2out.png +0 -0
- package/assets/ss-keyusages.png +0 -0
- package/lib/clean.js +14 -14
- package/lib/index.js +1 -1
- package/lib/processors/ref_func.js +2 -2
- package/lib/processors/replace_all.js +0 -1
- package/lib/processors/start.js +7 -3
- package/lib/processors/wat4wasm.js +12 -14
- package/package.json +1 -1
- package/test/test-output.html +1 -1
- package/test/test-output.wasm +0 -0
- package/test/test-output.wat +1115 -0
- package/test/test.wat +37 -46
- package/wat4wasm +35 -34
- /package/{ss-console.png → assets/ss-console.png} +0 -0
- /package/{ss-terminal.png → assets/ss-terminal.png} +0 -0
package/test/test.wat
CHANGED
|
@@ -3,71 +3,62 @@
|
|
|
3
3
|
(data $script "file://test_worker.js")
|
|
4
4
|
|
|
5
5
|
(include "test-sub.wat")
|
|
6
|
+
|
|
7
|
+
(main $init
|
|
8
|
+
(text "me") i32(1) f32(2.2) i64(4) f64(1)
|
|
9
|
+
(console $warn<ext.i32.f32.i64.f64>)
|
|
10
|
+
|
|
11
|
+
(async
|
|
12
|
+
(reflect $apply<ext.ext.ext>ext
|
|
13
|
+
(ref.extern $self.WebAssembly.instantiate<ext>)
|
|
14
|
+
(ref.extern $self.WebAssembly<ext>)
|
|
15
|
+
(array $of<ext.ext>ext
|
|
16
|
+
(data.view $module)
|
|
17
|
+
(self)
|
|
18
|
+
)
|
|
19
|
+
)
|
|
20
|
+
(then $oninstance
|
|
21
|
+
(param $instantiate <Object>)
|
|
22
|
+
|
|
23
|
+
(console $log<ext.ext>
|
|
24
|
+
(reflect $get<ext.ext>ext (this) (text "module"))
|
|
25
|
+
(reflect $get<ext.ext>ext (this) (text "instance"))
|
|
26
|
+
)
|
|
27
|
+
)
|
|
28
|
+
(catch ref.func $onwasmfailure)
|
|
29
|
+
(finally $finallyblock
|
|
30
|
+
(console $log<ext> (self))
|
|
31
|
+
)
|
|
32
|
+
)
|
|
33
|
+
)
|
|
6
34
|
|
|
7
|
-
(
|
|
8
|
-
(
|
|
35
|
+
(func $onwasmfailure
|
|
36
|
+
(param $error externref)
|
|
9
37
|
|
|
10
|
-
(
|
|
11
|
-
|
|
38
|
+
(console $error<ext.ext.ext>
|
|
39
|
+
(local.get $error)
|
|
40
|
+
(lget $error)
|
|
41
|
+
(this)
|
|
42
|
+
)
|
|
43
|
+
)
|
|
44
|
+
)
|
|
12
45
|
|
|
13
|
-
(self)
|
|
14
|
-
(console $log<ext>)
|
|
15
46
|
|
|
16
|
-
(null)
|
|
17
|
-
(console $log<ext>)
|
|
18
47
|
|
|
19
|
-
(func)
|
|
20
|
-
(console $log<fun>)
|
|
21
48
|
|
|
22
|
-
(true)
|
|
23
|
-
(console $log<i32>)
|
|
24
49
|
|
|
25
|
-
(false)
|
|
26
|
-
(console $log<i32>)
|
|
27
50
|
|
|
28
|
-
(NaN)
|
|
29
|
-
(console $log<ext>)
|
|
30
51
|
|
|
31
|
-
(nan)
|
|
32
|
-
(console $log<f32>)
|
|
33
52
|
|
|
34
|
-
(array)
|
|
35
|
-
(console $log<ext>)
|
|
36
53
|
|
|
37
|
-
(object)
|
|
38
|
-
(console $log<ext>)
|
|
39
54
|
|
|
40
|
-
(undefined)
|
|
41
|
-
(console $log<ext>)
|
|
42
55
|
|
|
43
|
-
(string)
|
|
44
|
-
(console $log<ext>)
|
|
45
56
|
|
|
46
|
-
(lget $this)
|
|
47
|
-
(console $log<f32>)
|
|
48
57
|
|
|
49
|
-
(gget $wat4wasm)
|
|
50
|
-
(console $log<ext>)
|
|
51
58
|
|
|
52
|
-
(tget $wat4wasm i32(0))
|
|
53
|
-
(console $log<ext>)
|
|
54
59
|
|
|
55
|
-
(grow $wat4wasm (null) i32(0))
|
|
56
|
-
(drop)
|
|
57
60
|
|
|
58
|
-
i32(2)
|
|
59
|
-
(console $log<i32>)
|
|
60
61
|
|
|
61
|
-
f32(- 2)
|
|
62
|
-
(console $log<f32>)
|
|
63
62
|
|
|
64
|
-
f32(-2.2)
|
|
65
|
-
(console $log<f32>)
|
|
66
63
|
|
|
67
|
-
f32(+ 2.2)
|
|
68
|
-
(console $log<f32>)
|
|
69
64
|
|
|
70
|
-
i64(+2)
|
|
71
|
-
(console $log<i64>)
|
|
72
|
-
)
|
|
73
|
-
)
|
package/wat4wasm
CHANGED
|
@@ -1168,7 +1168,7 @@ function REF_FUNC (wat, WAT4WASM) {
|
|
|
1168
1168
|
while (maskSetElem.hasBlock("elem")) {
|
|
1169
1169
|
const block = maskSetElem.lastBlockOf("elem");
|
|
1170
1170
|
const $name = block.$name;
|
|
1171
|
-
if (
|
|
1171
|
+
if ("$wat4wasm" !== $name) {
|
|
1172
1172
|
elemSegments.push(block.toString());
|
|
1173
1173
|
}
|
|
1174
1174
|
maskSetElem.mask(block);
|
|
@@ -1177,7 +1177,7 @@ function REF_FUNC (wat, WAT4WASM) {
|
|
|
1177
1177
|
while (maskSetRef.hasBlock(REF_FUNC_BLOCK_NAME)) {
|
|
1178
1178
|
const block = maskSetRef.lastBlockOf(REF_FUNC_BLOCK_NAME);
|
|
1179
1179
|
const $name = block.$name
|
|
1180
|
-
if (
|
|
1180
|
+
if ("$wat4wasm" !== $name) {
|
|
1181
1181
|
if (elemSegments.some(seg => seg.includes($name)) === false) {
|
|
1182
1182
|
if (needReference.has($name) === false) {
|
|
1183
1183
|
needReference.add($name);
|
|
@@ -1246,7 +1246,6 @@ function REPLACE_ALL (wat) {
|
|
|
1246
1246
|
.replaceAll(/(i32|f32|i64|f64|fun|ext)\((\+|\-|)\s*([0-9\.]+)\)/g, `($1.const $2$3)`)
|
|
1247
1247
|
.replaceAll(/\((.*)\.(set|tee)\s+([\+|\-])+\s+(\$.*)\s*\)/g, "($1.$2 $4 (i32.add ($1.get $4) (i32.const $31)))")
|
|
1248
1248
|
.replaceAll(/\(apply(?:\.*)(i32|f32|i64|f64|fun|ext|)(\s*)/g, `(call $self.Reflect.apply<ext.ext.ext>$1 $2`)
|
|
1249
|
-
.replaceAll(/\(main(\s+)(\$.[^\s]*)(\s)/g, `(start$1$2)\n\n(func$1$2$3`)
|
|
1250
1249
|
;
|
|
1251
1250
|
}
|
|
1252
1251
|
|
|
@@ -1254,16 +1253,17 @@ const START_BLOCK_NAME = "start";
|
|
|
1254
1253
|
function START (wat, WAT4WASM) {
|
|
1255
1254
|
let startCalls = [];
|
|
1256
1255
|
let removedWat = wat;
|
|
1256
|
+
wat = wat.replaceAll(/\(main(\s+)(\$.[^\s]*)(\s)/g, `(start$1$2)\n\n(func$1$2$3`)
|
|
1257
1257
|
while (helpers.hasBlock(removedWat, START_BLOCK_NAME)) {
|
|
1258
1258
|
let block = helpers.lastBlockOf(removedWat, START_BLOCK_NAME);
|
|
1259
1259
|
removedWat = block.removedRaw();
|
|
1260
|
-
if (block.includes(
|
|
1260
|
+
if (block.includes("$wat4wasm") === false) {
|
|
1261
1261
|
startCalls.push(block);
|
|
1262
1262
|
}
|
|
1263
1263
|
}
|
|
1264
1264
|
if (startCalls.length > 0) {
|
|
1265
1265
|
wat = removedWat;
|
|
1266
|
-
let $wat4func = helpers.lastBlockOf(wat, "func", { $name:
|
|
1266
|
+
let $wat4func = helpers.lastBlockOf(wat, "func", { $name: "$wat4wasm" });
|
|
1267
1267
|
let funcblock = $wat4func.toString();
|
|
1268
1268
|
const appends = startCalls.filter(start => {
|
|
1269
1269
|
let $name = `${start.$name}`;
|
|
@@ -1276,7 +1276,9 @@ function START (wat, WAT4WASM) {
|
|
|
1276
1276
|
if (appends.length) {
|
|
1277
1277
|
wat = $wat4func.replacedRaw(funcblock);
|
|
1278
1278
|
}
|
|
1279
|
-
|
|
1279
|
+
}
|
|
1280
|
+
if (!helpers.hasBlock(wat, "start", { $name: "$wat4wasm" })) {
|
|
1281
|
+
wat = helpers.append(wat, `(start $wat4wasm)`);
|
|
1280
1282
|
}
|
|
1281
1283
|
return wat;
|
|
1282
1284
|
}
|
|
@@ -1494,22 +1496,22 @@ const WAT4WASM_BLOCKS = {
|
|
|
1494
1496
|
data: WAT4WASM_DATA(),
|
|
1495
1497
|
};
|
|
1496
1498
|
function FUNC_WAT4WASM(wat) {
|
|
1497
|
-
return helpers.lastBlockOf(wat, "func", { $name:
|
|
1499
|
+
return helpers.lastBlockOf(wat, "func", { $name: "$wat4wasm" });
|
|
1498
1500
|
}
|
|
1499
1501
|
function ELEM_WAT4WASM(wat) {
|
|
1500
|
-
const block = helpers.lastBlockOf(wat, "elem", { $name:
|
|
1502
|
+
const block = helpers.lastBlockOf(wat, "elem", { $name: "$wat4wasm" });
|
|
1501
1503
|
return block && Object.assign(block, {
|
|
1502
1504
|
isInitial: helpers.generateId(block) === helpers.generateId(WAT4WASM_ELEM)
|
|
1503
1505
|
});
|
|
1504
1506
|
}
|
|
1505
1507
|
function MEMORY_WAT4WASM(wat) {
|
|
1506
|
-
return helpers.lastBlockOf(wat, "memory", { $name:
|
|
1508
|
+
return helpers.lastBlockOf(wat, "memory", { $name: "$wat4wasm" });
|
|
1507
1509
|
}
|
|
1508
1510
|
function GLOBAL_WAT4WASM(wat) {
|
|
1509
|
-
return helpers.lastBlockOf(wat, "global", { $name:
|
|
1511
|
+
return helpers.lastBlockOf(wat, "global", { $name: "$wat4wasm" });
|
|
1510
1512
|
}
|
|
1511
1513
|
function START_WAT4WASM(wat) {
|
|
1512
|
-
return helpers.lastBlockOf(wat, "start", { $name:
|
|
1514
|
+
return helpers.lastBlockOf(wat, "start", { $name: "$wat4wasm" });
|
|
1513
1515
|
}
|
|
1514
1516
|
function FUNC_WAT4WASM_NOBLOCKS(wat) {
|
|
1515
1517
|
const maskSet = new helpers.MaskSet(FUNC_WAT4WASM(wat));
|
|
@@ -1569,7 +1571,7 @@ function APPEND_ON_EXTERN_READY(wat, block) {
|
|
|
1569
1571
|
return wat;
|
|
1570
1572
|
}
|
|
1571
1573
|
function TABLE_WAT4WASM(wat) {
|
|
1572
|
-
const wat4table = helpers.lastBlockOf(wat, "table", { $name:
|
|
1574
|
+
const wat4table = helpers.lastBlockOf(wat, "table", { $name: "$wat4wasm" });
|
|
1573
1575
|
const lastIndex = parseInt(wat4table.toString().match(/\s(\d+)/).pop());
|
|
1574
1576
|
return wat4table && Object.assign(wat4table, {
|
|
1575
1577
|
lastIndex, isInitial: lastIndex === 1
|
|
@@ -1580,8 +1582,8 @@ function WAT4WASM_GROW_EXTERN_TABLE(wat) {
|
|
|
1580
1582
|
const lastIndex = wat4table.lastIndex;
|
|
1581
1583
|
return {
|
|
1582
1584
|
index: lastIndex,
|
|
1583
|
-
getter: `(table.get $
|
|
1584
|
-
generateSetter: value => `(table.set $
|
|
1585
|
+
getter: `(table.get $wat4wasm (i32.const ${lastIndex}))`,
|
|
1586
|
+
generateSetter: value => `(table.set $wat4wasm (i32.const ${lastIndex}) ${value.toString()})`,
|
|
1585
1587
|
modifiedRaw: wat4table.replacedRaw(WAT4WASM_TABLE(lastIndex + 1))
|
|
1586
1588
|
};
|
|
1587
1589
|
}
|
|
@@ -1598,7 +1600,7 @@ function WAT4WASM_REFERENCE_FUNC_ELEMENT(wat, $name) {
|
|
|
1598
1600
|
return wat;
|
|
1599
1601
|
}
|
|
1600
1602
|
function DATA_WAT4WASM(wat) {
|
|
1601
|
-
const block = helpers.lastBlockOf(wat, "data", { $name:
|
|
1603
|
+
const block = helpers.lastBlockOf(wat, "data", { $name: "$wat4wasm" });
|
|
1602
1604
|
const strhex = helpers.findQuotedText(block);
|
|
1603
1605
|
const buffer = Buffer.from(strhex.replaceAll(/[^a-f0-9A-F]/g, ""), "hex");
|
|
1604
1606
|
return Object.assign(block, {
|
|
@@ -1629,13 +1631,12 @@ function W4W (wat) {
|
|
|
1629
1631
|
WAT4WASM_BLOCKS.memory = WAT4WASM_MEMORY;
|
|
1630
1632
|
}
|
|
1631
1633
|
for (const BLOCK_NAME in WAT4WASM_BLOCKS) {
|
|
1632
|
-
if (wat.includes(`(${BLOCK_NAME} $
|
|
1634
|
+
if (wat.includes(`(${BLOCK_NAME} $wat4wasm`)) {
|
|
1633
1635
|
continue;
|
|
1634
1636
|
}
|
|
1635
1637
|
if (i === 31) console.log("")
|
|
1636
|
-
console.log(`🦋 appending element --> \x1b[${i++}m(${BLOCK_NAME} $
|
|
1637
|
-
|
|
1638
|
-
wat = helpers.append(wat, raw);
|
|
1638
|
+
console.log(`🦋 appending element --> \x1b[${i++}m(${BLOCK_NAME} $wat4wasm) ...)\x1b[0m`);
|
|
1639
|
+
wat = helpers.append(wat, WAT4WASM_BLOCKS[BLOCK_NAME]);
|
|
1639
1640
|
}
|
|
1640
1641
|
if (i !== 31) console.log("")
|
|
1641
1642
|
return wat;
|
|
@@ -1670,20 +1671,20 @@ function clean (wat) {
|
|
|
1670
1671
|
if ($func) {
|
|
1671
1672
|
wat = $func.removedRaw();
|
|
1672
1673
|
}
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1674
|
+
block = ELEM_WAT4WASM(wat);
|
|
1675
|
+
if (block.isInitial) { wat = block.removedRaw(); }
|
|
1676
|
+
block = TABLE_WAT4WASM(wat);
|
|
1677
|
+
if (block.isInitial) { wat = block.removedRaw(); }
|
|
1678
|
+
block = DATA_WAT4WASM(wat);
|
|
1679
|
+
if (block.isInitial) { wat = block.removedRaw(); }
|
|
1680
|
+
if (false === helpers.hasBlock(wat, "global.get", { $name: "$wat4wasm" }) &&
|
|
1681
|
+
false === helpers.hasBlock(wat, "global.set", { $name: "$wat4wasm" })) {
|
|
1682
|
+
wat = GLOBAL_WAT4WASM(wat).removedRaw();
|
|
1683
|
+
}
|
|
1684
|
+
block = MEMORY_WAT4WASM(wat);
|
|
1685
|
+
if (block && (helpers.containsMemoryOperation(wat) === false)) {
|
|
1686
|
+
wat = block.removedRaw();
|
|
1687
|
+
}
|
|
1687
1688
|
}
|
|
1688
1689
|
const maskSet = new helpers.MaskSet(wat);
|
|
1689
1690
|
const imports = new Array();
|
|
@@ -1934,6 +1935,7 @@ async function processCLI(compileCallback, PROCESS = process) {
|
|
|
1934
1935
|
|
|
1935
1936
|
|
|
1936
1937
|
const processors = [
|
|
1938
|
+
REPLACE_ALL,
|
|
1937
1939
|
W4W,
|
|
1938
1940
|
TEXT,
|
|
1939
1941
|
ASYNC,
|
|
@@ -1945,7 +1947,6 @@ const processors = [
|
|
|
1945
1947
|
REF_FUNC,
|
|
1946
1948
|
START,
|
|
1947
1949
|
STRING,
|
|
1948
|
-
REPLACE_ALL,
|
|
1949
1950
|
];
|
|
1950
1951
|
processCLI(async wat4 => {
|
|
1951
1952
|
let wat2 = wat4, f, i = -1, llen, m = -1, c = 0, ci = 0;
|
|
File without changes
|
|
File without changes
|