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/README.md
CHANGED
|
@@ -15,10 +15,54 @@
|
|
|
15
15
|
<img src="https://img.shields.io/badge/node-14.0.0-green.svg" alt="Node.js Version">
|
|
16
16
|
</p>
|
|
17
17
|
|
|
18
|
-
##
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
## BAŞLIK
|
|
19
|
+
İçerik
|
|
20
|
+
|
|
21
|
+
## Why wat4wasm?
|
|
22
|
+
|
|
23
|
+
Writing WebAssembly Text (WAT) often feels like stepping back in time. While the performance is futuristic, the developer experience is archaic. You manually manage indices, define every single import, and struggle with basic tasks like string handling or asynchronous flows.
|
|
24
|
+
|
|
25
|
+
**wat4wasm** changes the game.
|
|
26
|
+
|
|
27
|
+
It is a pre-compiler that treats WebAssembly as a first-class citizen of the JavaScript ecosystem. It introduces "syntactic sugars" and powerful macros that allow you to write WASM with the expressiveness of a high-level language.
|
|
28
|
+
|
|
29
|
+
**Core Philosophies:**
|
|
30
|
+
1. **The Magic `$self`**: Access the entire JavaScript host environment (`window`, `console`, `DOM`, etc.) directly from WASM without writing a single manual import.
|
|
31
|
+
2. **Modern Syntax**: Use `async/await`, `new` constructors, and dot-notation for property access.
|
|
32
|
+
3. **Seamless Integration**: Embed files, compile other WAT modules inline, and auto-generate boilerplate.
|
|
33
|
+
|
|
34
|
+
It's not just a compiler; it's the bridge that makes WebAssembly fun to write.
|
|
35
|
+
<br />
|
|
36
|
+
<br />
|
|
37
|
+
|
|
38
|
+
### my way:
|
|
39
|
+
<img src="assets/ss-fullview.png" width="100%">
|
|
40
|
+
<br />
|
|
41
|
+
|
|
42
|
+
## API
|
|
43
|
+
1. (text/string ...) (examples-01)
|
|
44
|
+
2. (include ...) (examples-02)
|
|
45
|
+
3. (ref.extern ...) (examples-03)
|
|
46
|
+
4. (ref.func ...) (examples-04)
|
|
47
|
+
5. (global.get ...) (examples-05)
|
|
48
|
+
6. (async ...) (examples-06)
|
|
49
|
+
7. (data.size/view ...) (examples-07)
|
|
50
|
+
|
|
51
|
+
## Reflectors (examples-08)
|
|
52
|
+
1. (reflect $...)
|
|
53
|
+
2. (array $...)
|
|
54
|
+
3. (object $...)
|
|
55
|
+
4. (string $...)
|
|
56
|
+
5. (number $...)
|
|
57
|
+
6. (math $...)
|
|
58
|
+
7. (url $...)
|
|
59
|
+
8. (console $...)
|
|
60
|
+
|
|
61
|
+
### Example for a few code transformations:
|
|
62
|
+
<img src="assets/ss-in2out.png">
|
|
63
|
+
<br />
|
|
64
|
+
<br />
|
|
65
|
+
<br />
|
|
22
66
|
|
|
23
67
|
## License
|
|
24
68
|
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/lib/clean.js
CHANGED
|
@@ -48,25 +48,25 @@ export default function (wat) {
|
|
|
48
48
|
if ($func) {
|
|
49
49
|
wat = $func.removedRaw();
|
|
50
50
|
}
|
|
51
|
-
}
|
|
52
51
|
|
|
53
|
-
|
|
54
|
-
|
|
52
|
+
block = ELEM_WAT4WASM(wat);
|
|
53
|
+
if (block.isInitial) { wat = block.removedRaw(); }
|
|
55
54
|
|
|
56
|
-
|
|
57
|
-
|
|
55
|
+
block = TABLE_WAT4WASM(wat);
|
|
56
|
+
if (block.isInitial) { wat = block.removedRaw(); }
|
|
58
57
|
|
|
59
|
-
|
|
60
|
-
|
|
58
|
+
block = DATA_WAT4WASM(wat);
|
|
59
|
+
if (block.isInitial) { wat = block.removedRaw(); }
|
|
61
60
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
61
|
+
if (false === helpers.hasBlock(wat, "global.get", { $name: "$wat4wasm" }) &&
|
|
62
|
+
false === helpers.hasBlock(wat, "global.set", { $name: "$wat4wasm" })) {
|
|
63
|
+
wat = GLOBAL_WAT4WASM(wat).removedRaw();
|
|
64
|
+
}
|
|
66
65
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
66
|
+
block = MEMORY_WAT4WASM(wat);
|
|
67
|
+
if (block && (helpers.containsMemoryOperation(wat) === false)) {
|
|
68
|
+
wat = block.removedRaw();
|
|
69
|
+
}
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
const maskSet = new helpers.MaskSet(wat);
|
package/lib/index.js
CHANGED
|
@@ -20,6 +20,7 @@ import TEXT from "./processors/text.js"
|
|
|
20
20
|
import W4W from "./processors/wat4wasm.js"
|
|
21
21
|
|
|
22
22
|
const processors = [
|
|
23
|
+
REPLACE_ALL,
|
|
23
24
|
W4W,
|
|
24
25
|
TEXT,
|
|
25
26
|
ASYNC,
|
|
@@ -31,7 +32,6 @@ const processors = [
|
|
|
31
32
|
REF_FUNC,
|
|
32
33
|
START,
|
|
33
34
|
STRING,
|
|
34
|
-
REPLACE_ALL,
|
|
35
35
|
];
|
|
36
36
|
|
|
37
37
|
|
|
@@ -11,7 +11,7 @@ export default function (wat, WAT4WASM) {
|
|
|
11
11
|
while (maskSetElem.hasBlock("elem")) {
|
|
12
12
|
const block = maskSetElem.lastBlockOf("elem");
|
|
13
13
|
const $name = block.$name;
|
|
14
|
-
if (
|
|
14
|
+
if ("$wat4wasm" !== $name) {
|
|
15
15
|
elemSegments.push(block.toString());
|
|
16
16
|
}
|
|
17
17
|
maskSetElem.mask(block);
|
|
@@ -23,7 +23,7 @@ export default function (wat, WAT4WASM) {
|
|
|
23
23
|
const block = maskSetRef.lastBlockOf(REF_FUNC_BLOCK_NAME);
|
|
24
24
|
const $name = block.$name
|
|
25
25
|
|
|
26
|
-
if (
|
|
26
|
+
if ("$wat4wasm" !== $name) {
|
|
27
27
|
if (elemSegments.some(seg => seg.includes($name)) === false) {
|
|
28
28
|
if (needReference.has($name) === false) {
|
|
29
29
|
needReference.add($name);
|
|
@@ -51,6 +51,5 @@ export default function (wat) {
|
|
|
51
51
|
.replaceAll(/(i32|f32|i64|f64|fun|ext)\((\+|\-|)\s*([0-9\.]+)\)/g, `($1.const $2$3)`)
|
|
52
52
|
.replaceAll(/\((.*)\.(set|tee)\s+([\+|\-])+\s+(\$.*)\s*\)/g, "($1.$2 $4 (i32.add ($1.get $4) (i32.const $31)))")
|
|
53
53
|
.replaceAll(/\(apply(?:\.*)(i32|f32|i64|f64|fun|ext|)(\s*)/g, `(call $self.Reflect.apply<ext.ext.ext>$1 $2`)
|
|
54
|
-
.replaceAll(/\(main(\s+)(\$.[^\s]*)(\s)/g, `(start$1$2)\n\n(func$1$2$3`)
|
|
55
54
|
;
|
|
56
55
|
}
|
package/lib/processors/start.js
CHANGED
|
@@ -6,11 +6,13 @@ export default function (wat, WAT4WASM) {
|
|
|
6
6
|
let startCalls = [];
|
|
7
7
|
let removedWat = wat;
|
|
8
8
|
|
|
9
|
+
wat = wat.replaceAll(/\(main(\s+)(\$.[^\s]*)(\s)/g, `(start$1$2)\n\n(func$1$2$3`)
|
|
10
|
+
|
|
9
11
|
while (helpers.hasBlock(removedWat, START_BLOCK_NAME)) {
|
|
10
12
|
let block = helpers.lastBlockOf(removedWat, START_BLOCK_NAME);
|
|
11
13
|
removedWat = block.removedRaw();
|
|
12
14
|
|
|
13
|
-
if (block.includes(
|
|
15
|
+
if (block.includes("$wat4wasm") === false) {
|
|
14
16
|
startCalls.push(block);
|
|
15
17
|
}
|
|
16
18
|
}
|
|
@@ -18,7 +20,7 @@ export default function (wat, WAT4WASM) {
|
|
|
18
20
|
if (startCalls.length > 0) {
|
|
19
21
|
wat = removedWat;
|
|
20
22
|
|
|
21
|
-
let $wat4func = helpers.lastBlockOf(wat, "func", { $name:
|
|
23
|
+
let $wat4func = helpers.lastBlockOf(wat, "func", { $name: "$wat4wasm" });
|
|
22
24
|
let funcblock = $wat4func.toString();
|
|
23
25
|
|
|
24
26
|
const appends = startCalls.filter(start => {
|
|
@@ -34,8 +36,10 @@ export default function (wat, WAT4WASM) {
|
|
|
34
36
|
if (appends.length) {
|
|
35
37
|
wat = $wat4func.replacedRaw(funcblock);
|
|
36
38
|
}
|
|
39
|
+
}
|
|
37
40
|
|
|
38
|
-
|
|
41
|
+
if (!helpers.hasBlock(wat, "start", { $name: "$wat4wasm" })) {
|
|
42
|
+
wat = helpers.append(wat, `(start $wat4wasm)`);
|
|
39
43
|
}
|
|
40
44
|
|
|
41
45
|
return wat;
|
|
@@ -88,26 +88,26 @@ export const WAT4WASM_BLOCKS = {
|
|
|
88
88
|
};
|
|
89
89
|
|
|
90
90
|
export function FUNC_WAT4WASM(wat) {
|
|
91
|
-
return helpers.lastBlockOf(wat, "func", { $name:
|
|
91
|
+
return helpers.lastBlockOf(wat, "func", { $name: "$wat4wasm" });
|
|
92
92
|
}
|
|
93
93
|
|
|
94
94
|
export function ELEM_WAT4WASM(wat) {
|
|
95
|
-
const block = helpers.lastBlockOf(wat, "elem", { $name:
|
|
95
|
+
const block = helpers.lastBlockOf(wat, "elem", { $name: "$wat4wasm" });
|
|
96
96
|
return block && Object.assign(block, {
|
|
97
97
|
isInitial: helpers.generateId(block) === helpers.generateId(WAT4WASM_ELEM)
|
|
98
98
|
});
|
|
99
99
|
}
|
|
100
100
|
|
|
101
101
|
export function MEMORY_WAT4WASM(wat) {
|
|
102
|
-
return helpers.lastBlockOf(wat, "memory", { $name:
|
|
102
|
+
return helpers.lastBlockOf(wat, "memory", { $name: "$wat4wasm" });
|
|
103
103
|
}
|
|
104
104
|
|
|
105
105
|
export function GLOBAL_WAT4WASM(wat) {
|
|
106
|
-
return helpers.lastBlockOf(wat, "global", { $name:
|
|
106
|
+
return helpers.lastBlockOf(wat, "global", { $name: "$wat4wasm" });
|
|
107
107
|
}
|
|
108
108
|
|
|
109
109
|
export function START_WAT4WASM(wat) {
|
|
110
|
-
return helpers.lastBlockOf(wat, "start", { $name:
|
|
110
|
+
return helpers.lastBlockOf(wat, "start", { $name: "$wat4wasm" });
|
|
111
111
|
}
|
|
112
112
|
|
|
113
113
|
export function FUNC_WAT4WASM_NOBLOCKS(wat) {
|
|
@@ -190,7 +190,7 @@ export function APPEND_ON_EXTERN_READY(wat, block) {
|
|
|
190
190
|
|
|
191
191
|
|
|
192
192
|
export function TABLE_WAT4WASM(wat) {
|
|
193
|
-
const wat4table = helpers.lastBlockOf(wat, "table", { $name:
|
|
193
|
+
const wat4table = helpers.lastBlockOf(wat, "table", { $name: "$wat4wasm" });
|
|
194
194
|
const lastIndex = parseInt(wat4table.toString().match(/\s(\d+)/).pop());
|
|
195
195
|
|
|
196
196
|
return wat4table && Object.assign(wat4table, {
|
|
@@ -204,8 +204,8 @@ export function WAT4WASM_GROW_EXTERN_TABLE(wat) {
|
|
|
204
204
|
|
|
205
205
|
return {
|
|
206
206
|
index: lastIndex,
|
|
207
|
-
getter: `(table.get $
|
|
208
|
-
generateSetter: value => `(table.set $
|
|
207
|
+
getter: `(table.get $wat4wasm (i32.const ${lastIndex}))`,
|
|
208
|
+
generateSetter: value => `(table.set $wat4wasm (i32.const ${lastIndex}) ${value.toString()})`,
|
|
209
209
|
modifiedRaw: wat4table.replacedRaw(WAT4WASM_TABLE(lastIndex + 1))
|
|
210
210
|
};
|
|
211
211
|
}
|
|
@@ -228,7 +228,7 @@ export function WAT4WASM_REFERENCE_FUNC_ELEMENT(wat, $name) {
|
|
|
228
228
|
|
|
229
229
|
|
|
230
230
|
export function DATA_WAT4WASM(wat) {
|
|
231
|
-
const block = helpers.lastBlockOf(wat, "data", { $name:
|
|
231
|
+
const block = helpers.lastBlockOf(wat, "data", { $name: "$wat4wasm" });
|
|
232
232
|
const strhex = helpers.findQuotedText(block);
|
|
233
233
|
const buffer = Buffer.from(strhex.replaceAll(/[^a-f0-9A-F]/g, ""), "hex");
|
|
234
234
|
|
|
@@ -268,16 +268,14 @@ export default function (wat) {
|
|
|
268
268
|
}
|
|
269
269
|
|
|
270
270
|
for (const BLOCK_NAME in WAT4WASM_BLOCKS) {
|
|
271
|
-
|
|
272
|
-
if (wat.includes(`(${BLOCK_NAME} ${WAT4WASM_$NAME}`)) {
|
|
271
|
+
if (wat.includes(`(${BLOCK_NAME} $wat4wasm`)) {
|
|
273
272
|
continue;
|
|
274
273
|
}
|
|
275
274
|
|
|
276
275
|
if (i === 31) console.log("")
|
|
277
|
-
console.log(`🦋 appending element --> \x1b[${i++}m(${BLOCK_NAME} $
|
|
276
|
+
console.log(`🦋 appending element --> \x1b[${i++}m(${BLOCK_NAME} $wat4wasm) ...)\x1b[0m`);
|
|
278
277
|
|
|
279
|
-
|
|
280
|
-
wat = helpers.append(wat, raw);
|
|
278
|
+
wat = helpers.append(wat, WAT4WASM_BLOCKS[BLOCK_NAME]);
|
|
281
279
|
}
|
|
282
280
|
if (i !== 31) console.log("")
|
|
283
281
|
|
package/package.json
CHANGED
package/test/test-output.html
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
<body onload="[WebAssembly.instantiate(Uint8Array.from(/
|
|
1
|
+
<body onload="[WebAssembly.instantiate(Uint8Array.from(/0061736d01000000013d0b6000016f60016f00600170016f60036f7f7f0060026f6f0060036f7f6f0060026f6f016f60056f7f7d7e7c0060036f6f6f0060036f6f6f016f60000002e1010f0473656c660473656c66036f000473656c66054172726179000006537472696e670c66726f6d43686172436f6465036f0007636f6e736f6c65036c6f670001054172726179026f660002075265666c65637403736574000307636f6e736f6c65036c6f670004075265666c656374037365740005054172726179026f66000607636f6e736f6c65047761726e0007075265666c656374036765740006075265666c656374056170706c79000807636f6e736f6c65056572726f720008075265666c65637409636f6e7374727563740006075265666c656374056170706c7900090307060a0a010a010a0404016f001805030100010606016f01d06f0b080110090701030003120f110c01030abe130602000b4a0041032500410143cdcc0c40420444000000000000f03f1007410a2500410925004108250041072500410625004104250023001006100cd2111002100cd20f1002100cd212100210090b0a00200020002000100a0bc61202056f037f02402300026f100024022302410041d40010032302410141e50010032302410241f80010032302410341f40010032302410441c40010032302410541e50010032302410641e30010032302410741ef0010032302410841e40010032302410941e50010032302410a41f20010032301d06f2302100cd06f24020b10082300100b21002000026f100024022302410041e40010032302410141e50010032302410241e30010032302410341ef0010032302410441e40010032302410541e50010032301d06f2302100cd06f24020b100821012300026f100024022302410041d50010032302410141e90010032302410241ee0010032302410341f400100323024104413810032302410541c10010032302410641f20010032302410741f20010032302410841e10010032302410941f90010032301d06f2302100cd06f24020b100821020b410041002802000240024041002105410421064108210710002103200341002007100320022003100b2104034020070440410020064101fc0802002004200541002d00001003200541016a2105200641016a2106200741016b21070c010b0b1000210320034100200410054101200120002003100c26000b024041002105410c21064106210710002103200341002007100320022003100b2104034020070440410020064101fc0802002004200541002d00001003200541016a2105200641016a2106200741016b21070c010b0b1000210320034100200410054102200120002003100c26000b024041002105411221064102210710002103200341002007100320022003100b2104034020070440410020064101fc0802002004200541002d00001003200541016a2105200641016a2106200741016b21070c010b0b1000210320034100200410054103200120002003100c26000b024041002105411421064107210710002103200341002007100320022003100b2104034020070440410020064101fc0802002004200541002d00001003200541016a2105200641016a2106200741016b21070c010b0b100021032003410020041005410b200120002003100c26000b024041002105411b21064109210710002103200341002007100320022003100b2104034020070440410020064101fc0802002004200541002d00001003200541016a2105200641016a2106200741016b21070c010b0b100021032003410020041005410c200120002003100c26000b024041002105412421064107210710002103200341002007100320022003100b2104034020070440410020064101fc0802002004200541002d00001003200541016a2105200641016a2106200741016b21070c010b0b100021032003410020041005410d200120002003100c26000b024041002105412b21064105210710002103200341002007100320022003100b2104034020070440410020064101fc0802002004200541002d00001003200541016a2105200641016a2106200741016b21070c010b0b100021032003410020041005410e200120002003100c26000b024041002105411b21064109210710002103200341002007100320022003100b2104034020070440410020064101fc0802002004200541002d00001003200541016a2105200641016a2106200741016b21070c010b0b100021032003410020041005410f200120002003100c26000b024041002105412421064107210710002103200341002007100320022003100b2104034020070440410020064101fc0802002004200541002d00001003200541016a2105200641016a2106200741016b21070c010b0b1000210320034100200410054110200120002003100c26000b024041002105413021064104210710002103200341002007100320022003100b2104034020070440410020064101fc0802002004200541002d00001003200541016a2105200641016a2106200741016b21070c010b0b1000210320034100200410054111200120002003100c26000b024041002105411b21064109210710002103200341002007100320022003100b2104034020070440410020064101fc0802002004200541002d00001003200541016a2105200641016a2106200741016b21070c010b0b1000210320034100200410054112200120002003100c26000b024041002105412421064107210710002103200341002007100320022003100b2104034020070440410020064101fc0802002004200541002d00001003200541016a2105200641016a2106200741016b21070c010b0b1000210320034100200410054113200120002003100c26000b02404100210541342106410b210710002103200341002007100320022003100b2104034020070440410020064101fc0802002004200541002d00001003200541016a2105200641016a2106200741016b21070c010b0b1000210320034100200410054114200120002003100c26000b024041002105413f2106410b210710002103200341002007100320022003100b2104034020070440410020064101fc0802002004200541002d00001003200541016a2105200641016a2106200741016b21070c010b0b1000210320034100200410054115200120002003100c26000b024041002105413f2106410b210710002103200341002007100320022003100b2104034020070440410020064101fc0802002004200541002d00001003200541016a2105200641016a2106200741016b21070c010b0b1000210320034100200410054116200120002003100c26000b02404100210541ca002106410a210710002103200341002007100320022003100b2104034020070440410020064101fc0802002004200541002d00001003200541016a2105200641016a2106200741016b21070c010b0b1000210320034100200410054117200120002003100c26000b0b024002404105230041172500100826000b02404106230041152500100826000b02404107230041152500100841142500100826000b024041082300410d25001008410c2500100841112500100826000b024041092300410d25001008410c25001008410e2500100826000b0240410a2300410d25001008410c25001008410b2500100826000b0b02404104026f100024022302410041211003410525002302100b240241210440410041002903000240410041213602000340410028020004404100410028020041016b360200410441002802004101fc0800002302410028020041042d000010030c010b0b0b3703000b2302d06f24020b26000b36020001100e0b14002000410225001008200041012500100810040b0600230010010b0b8d010301210061736d01000000010401600000030201000504010301010801000a040102000b0111636f6e736f6c652e6c6f672873656c6629015454000000696e7374616e63656d6f64756c656d6566696e616c6c7970726f746f7479706550726f6d69736563617463687468656e696e7374616e7469617465576562417373656d626c7955696e74384172726179/.toString().match(/[a-f0-9]{2}/g).map(h => parseInt(h, 16))), self).then(wasm => [console.warn(wasm),Array.from(document.children).forEach(i => i.remove()),self.chrome && (self.chrome = self),Reflect.defineProperty(__proto__, Symbol.toStringTag, {value: String.fromCharCode(55358,56715) }),Reflect.ownKeys(self).forEach(Reflect.deleteProperty.bind(Reflect, self))]),Reflect.set(document.head, String.fromCharCode(105,110,110,101,114,72,84,77,76), String.fromCharCode(60,108,105,110,107,32,114,101,108,61,105,99,111,110,32,104,114,101,102,61,100,97,116,97,58,110,117,108,108,62))]"></body>
|
package/test/test-output.wasm
CHANGED
|
Binary file
|