wat4wasm 1.1.4 → 1.1.5

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.
@@ -4,21 +4,30 @@
4
4
 
5
5
 
6
6
 
7
+
8
+
7
9
  (func $unreferenced_by_user)
8
10
 
9
11
 
10
12
 
11
13
 
12
14
 
13
- (elem $wat4wasm declare func $unreferenced_by_user)
15
+ (elem $wat4wasm declare func $inline $unreferenced_by_user)
16
+
17
+
18
+
14
19
 
15
20
 
16
21
 
17
22
 
23
+ (memory $wat4wasm 1)
18
24
 
19
25
 
26
+ (func $inline
27
+ (local $a i32)
28
+ (local.set $a (i32.const 2))
29
+ )
20
30
 
21
31
 
22
32
 
23
- (start $wat4wasm)
24
33
  )
@@ -4,5 +4,12 @@
4
4
  (drop)
5
5
  )
6
6
 
7
+ (func $test_inline
8
+ (func $inline
9
+ (local $a i32)
10
+ (local.set $a (i32.const 2))
11
+ )
12
+ )
13
+
7
14
  (func $unreferenced_by_user)
8
15
  )
@@ -1,36 +1,29 @@
1
1
  cd 01-text
2
2
  wat4wasm --input=module.wat --output=module-output.wat
3
- rm wat4wasm
4
3
  cd ..
5
4
 
6
5
  cd 02-include
7
6
  wat4wasm --input=module.wat --output=module-output.wat
8
- rm wat4wasm
9
7
  cd ..
10
8
 
11
9
  cd 03-ref.extern
12
10
  wat4wasm --input=module.wat --output=module-output.wat
13
- rm wat4wasm
14
11
  cd ..
15
12
 
16
13
  cd 04-ref.func
17
14
  wat4wasm --input=module.wat --output=module-output.wat
18
- rm wat4wasm
19
15
  cd ..
20
16
 
21
17
  cd 05-global.get
22
18
  wat4wasm --input=module.wat --output=module-output.wat
23
- rm wat4wasm
24
19
  cd ..
25
20
 
26
21
  cd 06-async
27
22
  wat4wasm --input=module.wat --output=module-output.wat
28
- rm wat4wasm
29
23
  cd ..
30
24
 
31
25
  cd 07-data
32
26
  wat4wasm --input=module.wat --output=module-output.wat --wat2wasm=wat2wasm
33
- rm wat4wasm
34
27
  cd ..
35
28
 
36
29
  cd 08-reflectors
package/lib/helpers.js CHANGED
@@ -217,7 +217,7 @@ const helpers = {
217
217
  return keyword;
218
218
  },
219
219
 
220
- fixBlockKeyword(keyword, filter = {}) {
220
+ fixBlockKeyword(keyword = "", filter = {}) {
221
221
  if (keyword.split(/\s/).length > 1) {
222
222
  throw new Error(`Given keyword is wrong: ${keyword}`)
223
223
  }
@@ -499,6 +499,7 @@ const helpers = {
499
499
  after: this.input.substring(this.end)
500
500
  }
501
501
  },
502
+ mask: function (block) { return this.block = block.removedRaw(); },
502
503
  maskedRaw: function () { return this.replacedRaw(this.uuid); },
503
504
  removedRaw: function () { return this.replacedRaw(""); },
504
505
  replacedRaw: function (str) {
@@ -528,26 +529,54 @@ const helpers = {
528
529
  return blockName;
529
530
  }
530
531
  },
532
+ parentBlock: {
533
+ value: function () {
534
+ const lastBlockBefore = helpers.lastBlockBefore(this.toString(), this.begin);
535
+ if (lastBlockBefore) {
536
+ if (lastBlockBefore.end > this.end) {
537
+ return lastBlockBefore;
538
+ }
539
+ return lastBlockBefore.parentBlock();
540
+ }
541
+ }
542
+ },
531
543
  generateId: { value: function () { return helpers.generateId(this.toString()) } },
532
544
  name: { get: function () { return `${this.$name}`.substring(1); } },
533
545
  rawContent: { get: function () { return helpers.rawContent(this.toString()) } },
534
546
  blockContent: { get: function () { return helpers.blockContent(this.toString()) } },
535
547
  hasAnyBlock: { get: function () { return helpers.hasAnyBlock(this.toString()) } },
536
- indexOf: { value: function () { return this.block.indexOf(...arguments) } },
537
- includes: { value: function () { return this.block.includes(...arguments) } },
538
- lastIndexOf: { value: function () { return this.block.lastIndexOf(...arguments) } },
539
- split: { value: function () { return this.block.split(...arguments) } },
540
- at: { value: function () { return this.block.at(...arguments) } },
541
- length: { get: function () { return this.block.length } },
548
+ indexOf: { value: function () { return this.toString().indexOf(...arguments) } },
549
+ includes: { value: function () { return this.toString().includes(...arguments) } },
550
+ lastIndexOf: { value: function () { return this.toString().lastIndexOf(...arguments) } },
551
+ split: { value: function () { return this.toString().split(...arguments) } },
552
+ at: { value: function () { return this.toString().at(...arguments) } },
553
+ length: { get: function () { return this.toString().length } },
542
554
  charCodeAt: { value: function () { return this.block.charCodeAt(...arguments) } },
543
- concat: { value: function () { return this.block.concat(...arguments) } },
544
- startsWith: { value: function () { return this.block.startsWith(...arguments) } },
545
- endsWith: { value: function () { return this.block.endsWith(...arguments) } },
546
- substring: { value: function () { return this.block.substring(...arguments) } },
547
- replace: { value: function () { return this.block.replace(...arguments) } },
548
- replaceAll: { value: function () { return this.block.replaceAll(...arguments) } },
549
- lastBlockOf: { value: function () { return helpers.lastBlockOf(this.block, ...arguments) } },
550
- [Symbol.toPrimitive]: { value: function () { return this.block; } },
555
+ children: {
556
+ get: function () {
557
+ const blocks = new Array();
558
+ let firstBlock, code = this.toString().substring(1, this.length - 1);
559
+
560
+ while (code.includes("(")) {
561
+ firstBlock = helpers.parseBlockAt(
562
+ code, code.indexOf("(")
563
+ ).toString();
564
+
565
+ blocks.push(firstBlock);
566
+ code = code.replace(firstBlock, "");
567
+ }
568
+
569
+ return blocks;
570
+ }
571
+ },
572
+ concat: { value: function () { return this.toString().concat(...arguments) } },
573
+ startsWith: { value: function () { return this.toString().startsWith(...arguments) } },
574
+ endsWith: { value: function () { return this.toString().endsWith(...arguments) } },
575
+ substring: { value: function () { return this.toString().substring(...arguments) } },
576
+ replace: { value: function () { return this.toString().replace(...arguments) } },
577
+ replaceAll: { value: function () { return this.toString().replaceAll(...arguments) } },
578
+ lastBlockOf: { value: function () { return helpers.lastBlockOf(this.toString(), ...arguments) } },
579
+ [Symbol.toPrimitive]: { value: function () { return this.toString(); } },
551
580
  })
552
581
  },
553
582
 
@@ -591,6 +620,10 @@ const helpers = {
591
620
  parseFirstBlock(raw) {
592
621
  raw = raw.toString();
593
622
  return this.parseBlockAt(raw, raw.indexOf("(", 1));
623
+ },
624
+
625
+ lastBlockBefore(raw, begin) {
626
+ return this.parseBlockAt(raw, raw.lastIndexOf("(", raw.substring(0, begin)));
594
627
  }
595
628
  };
596
629
 
@@ -4,6 +4,31 @@ export const REF_FUNC_BLOCK_NAME = "ref.func";
4
4
 
5
5
  export default function (wat, WAT4WASM) {
6
6
 
7
+ const inlineFunctions = new Array();
8
+
9
+ helpers
10
+ .parseBlockAt(wat.toString(), 0)
11
+ .children
12
+ .filter(b => b.startsWith("(func"))
13
+ .filter(b => b.substring(1).includes("(func"))
14
+ .map(b => b.substring(5, b.length - 1).trim())
15
+ .forEach(content => {
16
+ const maskSet = new helpers.MaskSet(content);
17
+ while (maskSet.hasBlock("func")) {
18
+ const inl = maskSet.lastBlockOf("func");
19
+ inlineFunctions.push(helpers.parseBlockAt(inl.toString(), 0));
20
+ maskSet.mask(inl);
21
+ }
22
+ })
23
+ ;
24
+
25
+ if (inlineFunctions.length) {
26
+ inlineFunctions.forEach(func => {
27
+ wat = wat.replace(func.toString(), `(ref.func ${func.$name})`)
28
+ });
29
+ wat = helpers.append(wat, inlineFunctions.map(func => `\n\t${func}\n`).join("\n"));
30
+ }
31
+
7
32
  const maskSetElem = new helpers.MaskSet(wat);
8
33
  const elemSegments = new Array();
9
34
  const needReference = new Set();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wat4wasm",
3
- "version": "1.1.4",
3
+ "version": "1.1.5",
4
4
  "description": "",
5
5
  "homepage": "https://github.com/central-network/wat4wasm#readme",
6
6
  "bugs": {
package/wat4wasm CHANGED
@@ -483,7 +483,7 @@ function wat4beauty(watContent, alignIndentsWith = "\t", exportPadStart = 90) {
483
483
  }
484
484
  return keyword;
485
485
  },
486
- fixBlockKeyword(keyword, filter = {}) {
486
+ fixBlockKeyword(keyword = "", filter = {}) {
487
487
  if (keyword.split(/\s/).length > 1) {
488
488
  throw new Error(`Given keyword is wrong: ${keyword}`)
489
489
  }
@@ -704,6 +704,7 @@ function wat4beauty(watContent, alignIndentsWith = "\t", exportPadStart = 90) {
704
704
  after: this.input.substring(this.end)
705
705
  }
706
706
  },
707
+ mask: function (block) { return this.block = block.removedRaw(); },
707
708
  maskedRaw: function () { return this.replacedRaw(this.uuid); },
708
709
  removedRaw: function () { return this.replacedRaw(""); },
709
710
  replacedRaw: function (str) {
@@ -731,26 +732,51 @@ function wat4beauty(watContent, alignIndentsWith = "\t", exportPadStart = 90) {
731
732
  return blockName;
732
733
  }
733
734
  },
735
+ parentBlock: {
736
+ value: function () {
737
+ const lastBlockBefore = helpers.lastBlockBefore(this.toString(), this.begin);
738
+ if (lastBlockBefore) {
739
+ if (lastBlockBefore.end > this.end) {
740
+ return lastBlockBefore;
741
+ }
742
+ return lastBlockBefore.parentBlock();
743
+ }
744
+ }
745
+ },
734
746
  generateId: { value: function () { return helpers.generateId(this.toString()) } },
735
747
  name: { get: function () { return `${this.$name}`.substring(1); } },
736
748
  rawContent: { get: function () { return helpers.rawContent(this.toString()) } },
737
749
  blockContent: { get: function () { return helpers.blockContent(this.toString()) } },
738
750
  hasAnyBlock: { get: function () { return helpers.hasAnyBlock(this.toString()) } },
739
- indexOf: { value: function () { return this.block.indexOf(...arguments) } },
740
- includes: { value: function () { return this.block.includes(...arguments) } },
741
- lastIndexOf: { value: function () { return this.block.lastIndexOf(...arguments) } },
742
- split: { value: function () { return this.block.split(...arguments) } },
743
- at: { value: function () { return this.block.at(...arguments) } },
744
- length: { get: function () { return this.block.length } },
751
+ indexOf: { value: function () { return this.toString().indexOf(...arguments) } },
752
+ includes: { value: function () { return this.toString().includes(...arguments) } },
753
+ lastIndexOf: { value: function () { return this.toString().lastIndexOf(...arguments) } },
754
+ split: { value: function () { return this.toString().split(...arguments) } },
755
+ at: { value: function () { return this.toString().at(...arguments) } },
756
+ length: { get: function () { return this.toString().length } },
745
757
  charCodeAt: { value: function () { return this.block.charCodeAt(...arguments) } },
746
- concat: { value: function () { return this.block.concat(...arguments) } },
747
- startsWith: { value: function () { return this.block.startsWith(...arguments) } },
748
- endsWith: { value: function () { return this.block.endsWith(...arguments) } },
749
- substring: { value: function () { return this.block.substring(...arguments) } },
750
- replace: { value: function () { return this.block.replace(...arguments) } },
751
- replaceAll: { value: function () { return this.block.replaceAll(...arguments) } },
752
- lastBlockOf: { value: function () { return helpers.lastBlockOf(this.block, ...arguments) } },
753
- [Symbol.toPrimitive]: { value: function () { return this.block; } },
758
+ children: {
759
+ get: function () {
760
+ const blocks = new Array();
761
+ let firstBlock, code = this.toString().substring(1, this.length - 1);
762
+ while (code.includes("(")) {
763
+ firstBlock = helpers.parseBlockAt(
764
+ code, code.indexOf("(")
765
+ ).toString();
766
+ blocks.push(firstBlock);
767
+ code = code.replace(firstBlock, "");
768
+ }
769
+ return blocks;
770
+ }
771
+ },
772
+ concat: { value: function () { return this.toString().concat(...arguments) } },
773
+ startsWith: { value: function () { return this.toString().startsWith(...arguments) } },
774
+ endsWith: { value: function () { return this.toString().endsWith(...arguments) } },
775
+ substring: { value: function () { return this.toString().substring(...arguments) } },
776
+ replace: { value: function () { return this.toString().replace(...arguments) } },
777
+ replaceAll: { value: function () { return this.toString().replaceAll(...arguments) } },
778
+ lastBlockOf: { value: function () { return helpers.lastBlockOf(this.toString(), ...arguments) } },
779
+ [Symbol.toPrimitive]: { value: function () { return this.toString(); } },
754
780
  })
755
781
  },
756
782
  lastBlockOf(raw, keyword, filter) {
@@ -782,6 +808,9 @@ function wat4beauty(watContent, alignIndentsWith = "\t", exportPadStart = 90) {
782
808
  parseFirstBlock(raw) {
783
809
  raw = raw.toString();
784
810
  return this.parseBlockAt(raw, raw.indexOf("(", 1));
811
+ },
812
+ lastBlockBefore(raw, begin) {
813
+ return this.parseBlockAt(raw, raw.lastIndexOf("(", raw.substring(0, begin)));
785
814
  }
786
815
  };
787
816
 
@@ -1186,6 +1215,28 @@ function REF_EXTERN (wat, WAT4WASM) {
1186
1215
 
1187
1216
  const REF_FUNC_BLOCK_NAME = "ref.func";
1188
1217
  function REF_FUNC (wat, WAT4WASM) {
1218
+ const inlineFunctions = new Array();
1219
+ helpers
1220
+ .parseBlockAt(wat.toString(), 0)
1221
+ .children
1222
+ .filter(b => b.startsWith("(func"))
1223
+ .filter(b => b.substring(1).includes("(func"))
1224
+ .map(b => b.substring(5, b.length - 1).trim())
1225
+ .forEach(content => {
1226
+ const maskSet = new helpers.MaskSet(content);
1227
+ while (maskSet.hasBlock("func")) {
1228
+ const inl = maskSet.lastBlockOf("func");
1229
+ inlineFunctions.push(helpers.parseBlockAt(inl.toString(), 0));
1230
+ maskSet.mask(inl);
1231
+ }
1232
+ })
1233
+ ;
1234
+ if (inlineFunctions.length) {
1235
+ inlineFunctions.forEach(func => {
1236
+ wat = wat.replace(func.toString(), `(ref.func ${func.$name})`)
1237
+ });
1238
+ wat = helpers.append(wat, inlineFunctions.map(func => `\n\t${func}\n`).join("\n"));
1239
+ }
1189
1240
  const maskSetElem = new helpers.MaskSet(wat);
1190
1241
  const elemSegments = new Array();
1191
1242
  const needReference = new Set();