porffor 0.59.5 → 0.59.6

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.
@@ -525,6 +525,7 @@ export const __Array_prototype_reverse = (_this: any[]) => {
525
525
 
526
526
  // @porf-typed-array
527
527
  export const __Array_prototype_forEach = (_this: any[], callbackFn: any, thisArg: any) => {
528
+ if (Porffor.type(callbackFn) != Porffor.TYPES.function) throw new TypeError('Callback must be a function');
528
529
  const len: i32 = _this.length;
529
530
  let i: i32 = 0;
530
531
  while (i < len) {
@@ -534,6 +535,7 @@ export const __Array_prototype_forEach = (_this: any[], callbackFn: any, thisArg
534
535
 
535
536
  // @porf-typed-array
536
537
  export const __Array_prototype_filter = (_this: any[], callbackFn: any, thisArg: any) => {
538
+ if (Porffor.type(callbackFn) != Porffor.TYPES.function) throw new TypeError('Callback must be a function');
537
539
  const out: any[] = Porffor.allocate();
538
540
 
539
541
  const len: i32 = _this.length;
@@ -550,6 +552,7 @@ export const __Array_prototype_filter = (_this: any[], callbackFn: any, thisArg:
550
552
 
551
553
  // @porf-typed-array
552
554
  export const __Array_prototype_map = (_this: any[], callbackFn: any, thisArg: any) => {
555
+ if (Porffor.type(callbackFn) != Porffor.TYPES.function) throw new TypeError('Callback must be a function');
553
556
  const len: i32 = _this.length;
554
557
  const out: any[] = Porffor.allocate();
555
558
  out.length = len;
@@ -563,6 +566,7 @@ export const __Array_prototype_map = (_this: any[], callbackFn: any, thisArg: an
563
566
  };
564
567
 
565
568
  export const __Array_prototype_flatMap = (_this: any[], callbackFn: any, thisArg: any) => {
569
+ if (Porffor.type(callbackFn) != Porffor.TYPES.function) throw new TypeError('Callback must be a function');
566
570
  const len: i32 = _this.length;
567
571
  const out: any[] = Porffor.allocate();
568
572
 
@@ -580,6 +584,7 @@ export const __Array_prototype_flatMap = (_this: any[], callbackFn: any, thisArg
580
584
 
581
585
  // @porf-typed-array
582
586
  export const __Array_prototype_find = (_this: any[], callbackFn: any, thisArg: any) => {
587
+ if (Porffor.type(callbackFn) != Porffor.TYPES.function) throw new TypeError('Callback must be a function');
583
588
  const len: i32 = _this.length;
584
589
  let i: i32 = 0;
585
590
  while (i < len) {
@@ -590,6 +595,7 @@ export const __Array_prototype_find = (_this: any[], callbackFn: any, thisArg: a
590
595
 
591
596
  // @porf-typed-array
592
597
  export const __Array_prototype_findLast = (_this: any[], callbackFn: any, thisArg: any) => {
598
+ if (Porffor.type(callbackFn) != Porffor.TYPES.function) throw new TypeError('Callback must be a function');
593
599
  let i: i32 = _this.length;
594
600
  while (i > 0) {
595
601
  const el: any = _this[--i];
@@ -599,15 +605,18 @@ export const __Array_prototype_findLast = (_this: any[], callbackFn: any, thisAr
599
605
 
600
606
  // @porf-typed-array
601
607
  export const __Array_prototype_findIndex = (_this: any[], callbackFn: any, thisArg: any) => {
608
+ if (Porffor.type(callbackFn) != Porffor.TYPES.function) throw new TypeError('Callback must be a function');
602
609
  const len: i32 = _this.length;
603
610
  let i: i32 = 0;
604
611
  while (i < len) {
605
- if (!!callbackFn.call(thisArg, _this[i], i++, _this)) return i;
612
+ if (!!callbackFn.call(thisArg, _this[i], i, _this)) return i;
613
+ i++;
606
614
  }
607
615
  };
608
616
 
609
617
  // @porf-typed-array
610
618
  export const __Array_prototype_findLastIndex = (_this: any[], callbackFn: any, thisArg: any) => {
619
+ if (Porffor.type(callbackFn) != Porffor.TYPES.function) throw new TypeError('Callback must be a function');
611
620
  let i: i32 = _this.length;
612
621
  while (i > 0) {
613
622
  if (!!callbackFn.call(thisArg, _this[--i], i, _this)) return i;
@@ -616,6 +625,7 @@ export const __Array_prototype_findLastIndex = (_this: any[], callbackFn: any, t
616
625
 
617
626
  // @porf-typed-array
618
627
  export const __Array_prototype_every = (_this: any[], callbackFn: any, thisArg: any) => {
628
+ if (Porffor.type(callbackFn) != Porffor.TYPES.function) throw new TypeError('Callback must be a function');
619
629
  const len: i32 = _this.length;
620
630
  let i: i32 = 0;
621
631
  while (i < len) {
@@ -628,6 +638,7 @@ export const __Array_prototype_every = (_this: any[], callbackFn: any, thisArg:
628
638
 
629
639
  // @porf-typed-array
630
640
  export const __Array_prototype_some = (_this: any[], callbackFn: any, thisArg: any) => {
641
+ if (Porffor.type(callbackFn) != Porffor.TYPES.function) throw new TypeError('Callback must be a function');
631
642
  const len: i32 = _this.length;
632
643
  let i: i32 = 0;
633
644
  while (i < len) {
@@ -639,10 +650,12 @@ export const __Array_prototype_some = (_this: any[], callbackFn: any, thisArg: a
639
650
 
640
651
  // @porf-typed-array
641
652
  export const __Array_prototype_reduce = (_this: any[], callbackFn: any, initialValue: any) => {
653
+ if (Porffor.type(callbackFn) != Porffor.TYPES.function) throw new TypeError('Callback must be a function');
642
654
  const len: i32 = _this.length;
643
655
  let acc: any = initialValue;
644
656
  let i: i32 = 0;
645
657
  if (acc === undefined) {
658
+ if (len == 0) throw new TypeError('Reduce of empty array with no initial value');
646
659
  acc = _this[i++];
647
660
  }
648
661
 
@@ -655,10 +668,12 @@ export const __Array_prototype_reduce = (_this: any[], callbackFn: any, initialV
655
668
 
656
669
  // @porf-typed-array
657
670
  export const __Array_prototype_reduceRight = (_this: any[], callbackFn: any, initialValue: any) => {
671
+ if (Porffor.type(callbackFn) != Porffor.TYPES.function) throw new TypeError('Callback must be a function');
658
672
  const len: i32 = _this.length;
659
673
  let acc: any = initialValue;
660
674
  let i: i32 = len;
661
675
  if (acc === undefined) {
676
+ if (len == 0) throw new TypeError('Reduce of empty array with no initial value');
662
677
  acc = _this[--i];
663
678
  }
664
679
 
@@ -545,7 +545,7 @@ export const __ByteString_prototype_codePointAt = (_this: bytestring, index: num
545
545
  return Porffor.wasm.i32.load8_u(Porffor.wasm`local.get ${_this}` + index, 0, 4);
546
546
  };
547
547
 
548
- export const __String_prototype_startsWith = (_this: string, searchString: string, position: number) => {
548
+ export const __String_prototype_startsWith = (_this: string, searchString: string, position: number = 0) => {
549
549
  // todo: handle bytestring searchString
550
550
 
551
551
  // todo/perf: investigate whether for counter vs while ++s are faster
@@ -573,7 +573,7 @@ export const __String_prototype_startsWith = (_this: string, searchString: strin
573
573
  return true;
574
574
  };
575
575
 
576
- export const __ByteString_prototype_startsWith = (_this: bytestring, searchString: bytestring, position: number) => {
576
+ export const __ByteString_prototype_startsWith = (_this: bytestring, searchString: bytestring, position: number = 0) => {
577
577
  // if searching non-bytestring, bytestring will not start with it
578
578
  // todo: change this to just check if = string and ToString others
579
579
  if (Porffor.wasm`local.get ${searchString+1}` != Porffor.TYPES.bytestring) return false;
@@ -603,7 +603,7 @@ export const __ByteString_prototype_startsWith = (_this: bytestring, searchStrin
603
603
  };
604
604
 
605
605
 
606
- export const __String_prototype_endsWith = (_this: string, searchString: string, endPosition: number) => {
606
+ export const __String_prototype_endsWith = (_this: string, searchString: string, endPosition: any = undefined) => {
607
607
  // todo: handle bytestring searchString
608
608
 
609
609
  let i: i32 = Porffor.wasm`local.get ${_this}`,
@@ -641,7 +641,7 @@ export const __String_prototype_endsWith = (_this: string, searchString: string,
641
641
  return true;
642
642
  };
643
643
 
644
- export const __ByteString_prototype_endsWith = (_this: bytestring, searchString: bytestring, endPosition: number) => {
644
+ export const __ByteString_prototype_endsWith = (_this: bytestring, searchString: bytestring, endPosition: any = undefined) => {
645
645
  // if searching non-bytestring, bytestring will not start with it
646
646
  // todo: change this to just check if = string and ToString others
647
647
  if (Porffor.wasm`local.get ${searchString+1}` != Porffor.TYPES.bytestring) return false;
@@ -679,7 +679,7 @@ export const __ByteString_prototype_endsWith = (_this: bytestring, searchString:
679
679
  };
680
680
 
681
681
 
682
- export const __String_prototype_indexOf = (_this: string, searchString: string, position: number) => {
682
+ export const __String_prototype_indexOf = (_this: string, searchString: string, position: number = 0) => {
683
683
  // todo: handle bytestring searchString
684
684
 
685
685
  let thisPtr: i32 = Porffor.wasm`local.get ${_this}`;
@@ -717,7 +717,7 @@ export const __String_prototype_indexOf = (_this: string, searchString: string,
717
717
  return -1;
718
718
  };
719
719
 
720
- export const __ByteString_prototype_indexOf = (_this: bytestring, searchString: bytestring, position: number) => {
720
+ export const __ByteString_prototype_indexOf = (_this: bytestring, searchString: bytestring, position: number = 0) => {
721
721
  // if searching non-bytestring, bytestring will not start with it
722
722
  // todo: change this to just check if = string and ToString others
723
723
  if (Porffor.wasm`local.get ${searchString+1}` != Porffor.TYPES.bytestring) return -1;
@@ -758,7 +758,7 @@ export const __ByteString_prototype_indexOf = (_this: bytestring, searchString:
758
758
  };
759
759
 
760
760
 
761
- export const __String_prototype_lastIndexOf = (_this: string, searchString: string, position: number) => {
761
+ export const __String_prototype_lastIndexOf = (_this: string, searchString: string, position: any = undefined) => {
762
762
  // todo: handle bytestring searchString
763
763
 
764
764
  let thisPtr: i32 = Porffor.wasm`local.get ${_this}`;
@@ -802,7 +802,7 @@ export const __String_prototype_lastIndexOf = (_this: string, searchString: stri
802
802
  return -1;
803
803
  };
804
804
 
805
- export const __ByteString_prototype_lastIndexOf = (_this: bytestring, searchString: bytestring, position: number) => {
805
+ export const __ByteString_prototype_lastIndexOf = (_this: bytestring, searchString: bytestring, position: any = undefined) => {
806
806
  // if searching non-bytestring, bytestring will not start with it
807
807
  // todo: change this to just check if = string and ToString others
808
808
  if (Porffor.wasm`local.get ${searchString+1}` != Porffor.TYPES.bytestring) return -1;
@@ -848,7 +848,7 @@ export const __ByteString_prototype_lastIndexOf = (_this: bytestring, searchStri
848
848
  };
849
849
 
850
850
 
851
- export const __String_prototype_includes = (_this: string, searchString: string, position: number) => {
851
+ export const __String_prototype_includes = (_this: string, searchString: string, position: number = 0) => {
852
852
  // todo: handle bytestring searchString
853
853
 
854
854
  let thisPtr: i32 = Porffor.wasm`local.get ${_this}`;
@@ -886,7 +886,7 @@ export const __String_prototype_includes = (_this: string, searchString: string,
886
886
  return false;
887
887
  };
888
888
 
889
- export const __ByteString_prototype_includes = (_this: bytestring, searchString: bytestring, position: number) => {
889
+ export const __ByteString_prototype_includes = (_this: bytestring, searchString: bytestring, position: number = 0) => {
890
890
  // if searching non-bytestring, bytestring will not start with it
891
891
  // todo: change this to just check if = string and ToString others
892
892
  if (Porffor.wasm`local.get ${searchString+1}` != Porffor.TYPES.bytestring) return -1;
@@ -927,7 +927,7 @@ export const __ByteString_prototype_includes = (_this: bytestring, searchString:
927
927
  };
928
928
 
929
929
 
930
- export const __String_prototype_padStart = (_this: string, targetLength: number, padString: string) => {
930
+ export const __String_prototype_padStart = (_this: string, targetLength: number, padString: string = undefined) => {
931
931
  let out: string = Porffor.allocate();
932
932
 
933
933
  let outPtr: i32 = Porffor.wasm`local.get ${out}`;
@@ -969,7 +969,7 @@ export const __String_prototype_padStart = (_this: string, targetLength: number,
969
969
  return out;
970
970
  };
971
971
 
972
- export const __ByteString_prototype_padStart = (_this: bytestring, targetLength: number, padString: bytestring) => {
972
+ export const __ByteString_prototype_padStart = (_this: bytestring, targetLength: number, padString: bytestring = undefined) => {
973
973
  // todo: handle padString being non-bytestring
974
974
 
975
975
  let out: bytestring = Porffor.allocate();
@@ -1010,7 +1010,7 @@ export const __ByteString_prototype_padStart = (_this: bytestring, targetLength:
1010
1010
  };
1011
1011
 
1012
1012
 
1013
- export const __String_prototype_padEnd = (_this: string, targetLength: number, padString: string) => {
1013
+ export const __String_prototype_padEnd = (_this: string, targetLength: number, padString: string = undefined) => {
1014
1014
  let out: string = Porffor.allocate();
1015
1015
 
1016
1016
  let outPtr: i32 = Porffor.wasm`local.get ${out}`;
@@ -1052,7 +1052,7 @@ export const __String_prototype_padEnd = (_this: string, targetLength: number, p
1052
1052
  return out;
1053
1053
  };
1054
1054
 
1055
- export const __ByteString_prototype_padEnd = (_this: bytestring, targetLength: number, padString: bytestring) => {
1055
+ export const __ByteString_prototype_padEnd = (_this: bytestring, targetLength: number, padString: bytestring = undefined) => {
1056
1056
  // todo: handle padString being non-bytestring
1057
1057
 
1058
1058
  let out: bytestring = Porffor.allocate();
@@ -1612,6 +1612,10 @@ export const __String_prototype_split = (_this: string, separator: any, limit: a
1612
1612
 
1613
1613
  if (Porffor.wasm`local.get ${limit+1}` == Porffor.TYPES.undefined) limit = Number.MAX_SAFE_INTEGER;
1614
1614
  if (limit < 0) limit = Number.MAX_SAFE_INTEGER;
1615
+ if (limit == 0) {
1616
+ out.length = 0;
1617
+ return out;
1618
+ }
1615
1619
 
1616
1620
  if (separator == null) {
1617
1621
  out.length = 1;
@@ -1628,6 +1632,10 @@ i32.store8 0 12`;
1628
1632
  return out;
1629
1633
  }
1630
1634
 
1635
+ if (Porffor.type(separator) != Porffor.TYPES.string && Porffor.type(separator) != Porffor.TYPES.bytestring) {
1636
+ separator = ecma262.ToString(separator);
1637
+ }
1638
+
1631
1639
  let tmp: string = Porffor.allocate(), tmpLen: i32 = 0;
1632
1640
  const thisLen: i32 = _this.length * 2, sepLen: i32 = separator.length;
1633
1641
  if (sepLen == 1) {
@@ -1671,9 +1679,9 @@ i32.store8 0 12`;
1671
1679
  tmpLen++;
1672
1680
  }
1673
1681
  } else if (sepLen == 0) {
1674
- const clammedLimit: i32 = limit > thisLen ? thisLen : limit;
1675
1682
  tmpLen = 1;
1676
- for (let i = 0; i <= clammedLimit; i+=2) {
1683
+ let produced: i32 = 0;
1684
+ for (let i = 0; i < thisLen && produced < limit; i += 2) {
1677
1685
  tmp = Porffor.allocateBytes(8);
1678
1686
  const x: i32 = Porffor.wasm.i32.load16_u(Porffor.wasm`local.get ${_this}` + i, 0, 4);
1679
1687
 
@@ -1698,6 +1706,7 @@ i32.add
1698
1706
  i32.const 67
1699
1707
  i32.store8 0 12`;
1700
1708
  outLen++;
1709
+ produced++;
1701
1710
  }
1702
1711
  return out;
1703
1712
  } else {
@@ -1738,12 +1747,13 @@ i32.store8 0 12`;
1738
1747
  }
1739
1748
  } else sepInd = 0;
1740
1749
 
1741
- Porffor.wasm.i32.store16(Porffor.wasm`local.get ${tmp}` + tmpLen, x, 0, 4);
1750
+ Porffor.wasm.i32.store16(Porffor.wasm`local.get ${tmp}` + tmpLen * 2, x, 0, 4);
1742
1751
  tmpLen++;
1743
1752
  }
1744
1753
  }
1745
1754
 
1746
- if (tmpLen > 0 && outLen < limit) {
1755
+ if (outLen < limit) {
1756
+ // per spec, push final (possibly empty) segment unless limited
1747
1757
  tmp.length = tmpLen;
1748
1758
  Porffor.wasm`
1749
1759
  local.get ${out}
@@ -1772,6 +1782,13 @@ i32.store8 0 12`;
1772
1782
  export const __ByteString_prototype_split = (_this: bytestring, separator: any, limit: any) => {
1773
1783
  let out: any[] = Porffor.allocate(), outLen: i32 = 0;
1774
1784
 
1785
+ if (Porffor.wasm`local.get ${limit+1}` == Porffor.TYPES.undefined) limit = Number.MAX_SAFE_INTEGER;
1786
+ if (limit < 0) limit = Number.MAX_SAFE_INTEGER;
1787
+ if (limit == 0) {
1788
+ out.length = 0;
1789
+ return out;
1790
+ }
1791
+
1775
1792
  if (separator == null) {
1776
1793
  out.length = 1;
1777
1794
  // out[0] = _this; (but in wasm as it is a f64 array and we are in i32 space)
@@ -1787,8 +1804,9 @@ i32.store8 0 12`;
1787
1804
  return out;
1788
1805
  }
1789
1806
 
1790
- if (Porffor.wasm`local.get ${limit+1}` == Porffor.TYPES.undefined) limit = Number.MAX_SAFE_INTEGER;
1791
- if (limit < 0) limit = Number.MAX_SAFE_INTEGER;
1807
+ if (Porffor.type(separator) != Porffor.TYPES.string && Porffor.type(separator) != Porffor.TYPES.bytestring) {
1808
+ separator = ecma262.ToString(separator);
1809
+ }
1792
1810
 
1793
1811
  let tmp: bytestring = Porffor.allocate(), tmpLen: i32 = 0;
1794
1812
  const thisLen: i32 = _this.length, sepLen: i32 = separator.length;
@@ -1833,9 +1851,9 @@ i32.store8 0 12`;
1833
1851
  tmpLen++;
1834
1852
  }
1835
1853
  } else if (sepLen == 0) {
1836
- const clammedLimit: i32 = limit > thisLen ? thisLen : limit;
1837
1854
  tmpLen = 1;
1838
- for (let i = 0; i <= clammedLimit; i++) {
1855
+ let produced: i32 = 0;
1856
+ for (let i = 0; i < thisLen && produced < limit; i++) {
1839
1857
  tmp = Porffor.allocateBytes(8);
1840
1858
  const x: i32 = Porffor.wasm.i32.load8_u(Porffor.wasm`local.get ${_this}` + i, 0, 4);
1841
1859
 
@@ -1861,6 +1879,7 @@ i32.add
1861
1879
  i32.const 195
1862
1880
  i32.store8 0 12`;
1863
1881
  outLen++;
1882
+ produced++;
1864
1883
  }
1865
1884
  return out;
1866
1885
  } else {
@@ -1906,7 +1925,7 @@ i32.store8 0 12`;
1906
1925
  }
1907
1926
  }
1908
1927
 
1909
- if (tmpLen > 0 && outLen < limit) {
1928
+ if (outLen < limit) {
1910
1929
  tmp.length = tmpLen;
1911
1930
  Porffor.wasm`
1912
1931
  local.get ${out}