porffor 0.24.10 → 0.24.12

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.
@@ -94,12 +94,12 @@ memory.copy 0 0`;
94
94
  return _this.length = len + itemsLen;
95
95
  };
96
96
 
97
- export const __Array_prototype_slice = (_this: any[], start: number, end: number) => {
97
+ export const __Array_prototype_slice = (_this: any[], _start: any, _end: any) => {
98
98
  const len: i32 = _this.length;
99
- if (Porffor.rawType(end) == Porffor.TYPES.undefined) end = len;
99
+ if (Porffor.rawType(_end) == Porffor.TYPES.undefined) _end = len;
100
100
 
101
- start |= 0;
102
- end |= 0;
101
+ let start: i32 = ecma262.ToIntegerOrInfinity(_start);
102
+ let end: i32 = ecma262.ToIntegerOrInfinity(_end);
103
103
 
104
104
  if (start < 0) {
105
105
  start = len + start;
@@ -135,18 +135,18 @@ export const __Array_prototype_slice = (_this: any[], start: number, end: number
135
135
  return out;
136
136
  };
137
137
 
138
- export const __Array_prototype_splice = (_this: any[], start: number, deleteCount: any, ...items: any[]) => {
138
+ export const __Array_prototype_splice = (_this: any[], _start: any, _deleteCount: any, ...items: any[]) => {
139
139
  const len: i32 = _this.length;
140
140
 
141
- start |= 0;
141
+ let start: i32 = ecma262.ToIntegerOrInfinity(_start);
142
142
  if (start < 0) {
143
143
  start = len + start;
144
144
  if (start < 0) start = 0;
145
145
  }
146
146
  if (start > len) start = len;
147
147
 
148
- if (Porffor.rawType(deleteCount) == Porffor.TYPES.undefined) deleteCount = len - start;
149
- deleteCount |= 0;
148
+ if (Porffor.rawType(_deleteCount) == Porffor.TYPES.undefined) _deleteCount = len - start;
149
+ let deleteCount: i32 = ecma262.ToIntegerOrInfinity(_deleteCount);
150
150
 
151
151
  if (deleteCount < 0) deleteCount = 0;
152
152
  if (deleteCount > len - start) deleteCount = len - start;
@@ -233,14 +233,14 @@ memory.copy 0 0`;
233
233
  };
234
234
 
235
235
  // @porf-typed-array
236
- export const __Array_prototype_fill = (_this: any[], value: any, start: any, end: any) => {
236
+ export const __Array_prototype_fill = (_this: any[], value: any, _start: any, _end: any) => {
237
237
  const len: i32 = _this.length;
238
238
 
239
- if (Porffor.rawType(start) == Porffor.TYPES.undefined) start = 0;
240
- if (Porffor.rawType(end) == Porffor.TYPES.undefined) end = len;
239
+ if (Porffor.rawType(_start) == Porffor.TYPES.undefined) _start = 0;
240
+ if (Porffor.rawType(_end) == Porffor.TYPES.undefined) _end = len;
241
241
 
242
- start |= 0;
243
- end |= 0;
242
+ let start: i32 = ecma262.ToIntegerOrInfinity(_start);
243
+ let end: i32 = ecma262.ToIntegerOrInfinity(_end);
244
244
 
245
245
  if (start < 0) {
246
246
  start = len + start;
@@ -261,12 +261,15 @@ export const __Array_prototype_fill = (_this: any[], value: any, start: any, end
261
261
  };
262
262
 
263
263
  // @porf-typed-array
264
- export const __Array_prototype_indexOf = (_this: any[], searchElement: any, position: number) => {
264
+ export const __Array_prototype_indexOf = (_this: any[], searchElement: any, _position: any) => {
265
265
  const len: i32 = _this.length;
266
- if (position > 0) {
266
+ let position: i32 = ecma262.ToIntegerOrInfinity(_position);
267
+ if (position >= 0) {
267
268
  if (position > len) position = len;
268
- else position |= 0;
269
- } else position = 0;
269
+ } else {
270
+ position = len + position;
271
+ if (position < 0) position = 0;
272
+ }
270
273
 
271
274
  for (let i: i32 = position; i < len; i++) {
272
275
  if (_this[i] === searchElement) return i;
@@ -276,12 +279,15 @@ export const __Array_prototype_indexOf = (_this: any[], searchElement: any, posi
276
279
  };
277
280
 
278
281
  // @porf-typed-array
279
- export const __Array_prototype_lastIndexOf = (_this: any[], searchElement: any, position: number) => {
282
+ export const __Array_prototype_lastIndexOf = (_this: any[], searchElement: any, _position: any) => {
280
283
  const len: i32 = _this.length;
281
- if (position > 0) {
284
+ let position: i32 = ecma262.ToIntegerOrInfinity(_position);
285
+ if (position >= 0) {
282
286
  if (position > len) position = len;
283
- else position |= 0;
284
- } else position = 0;
287
+ } else {
288
+ position = len + position;
289
+ if (position < 0) position = 0;
290
+ }
285
291
 
286
292
  for (let i: i32 = len - 1; i >= position; i--) {
287
293
  if (_this[i] === searchElement) return i;
@@ -291,12 +297,15 @@ export const __Array_prototype_lastIndexOf = (_this: any[], searchElement: any,
291
297
  };
292
298
 
293
299
  // @porf-typed-array
294
- export const __Array_prototype_includes = (_this: any[], searchElement: any, position: number) => {
300
+ export const __Array_prototype_includes = (_this: any[], searchElement: any, _position: any) => {
295
301
  const len: i32 = _this.length;
296
- if (position > 0) {
302
+ let position: i32 = ecma262.ToIntegerOrInfinity(_position);
303
+ if (position >= 0) {
297
304
  if (position > len) position = len;
298
- else position |= 0;
299
- } else position = 0;
305
+ } else {
306
+ position = len + position;
307
+ if (position < 0) position = 0;
308
+ }
300
309
 
301
310
  for (let i: i32 = position; i < len; i++) {
302
311
  if (_this[i] === searchElement) return true;
@@ -306,8 +315,10 @@ export const __Array_prototype_includes = (_this: any[], searchElement: any, pos
306
315
  };
307
316
 
308
317
  // @porf-typed-array
309
- export const __Array_prototype_with = (_this: any[], index: number, value: any) => {
318
+ export const __Array_prototype_with = (_this: any[], _index: any, value: any) => {
310
319
  const len: i32 = _this.length;
320
+
321
+ let index: i32 = ecma262.ToIntegerOrInfinity(_index);
311
322
  if (index < 0) {
312
323
  index = len + index;
313
324
  if (index < 0) {
@@ -329,23 +340,28 @@ export const __Array_prototype_with = (_this: any[], index: number, value: any)
329
340
  };
330
341
 
331
342
  // @porf-typed-array
332
- export const __Array_prototype_copyWithin = (_this: any[], target: number, start: number, end: any) => {
343
+ export const __Array_prototype_copyWithin = (_this: any[], _target: any, _start: any, _end: any) => {
333
344
  const len: i32 = _this.length;
345
+
346
+ let target: i32 = ecma262.ToIntegerOrInfinity(_target);
334
347
  if (target < 0) {
335
348
  target = len + target;
336
349
  if (target < 0) target = 0;
337
350
  }
338
351
  if (target > len) target = len;
339
352
 
353
+ let start: i32 = ecma262.ToIntegerOrInfinity(_start);
340
354
  if (start < 0) {
341
355
  start = len + start;
342
356
  if (start < 0) start = 0;
343
357
  }
344
358
  if (start > len) start = len;
345
359
 
346
- if (Porffor.rawType(end) == Porffor.TYPES.undefined) {
360
+ let end: i32;
361
+ if (Porffor.rawType(_end) == Porffor.TYPES.undefined) {
347
362
  end = len;
348
363
  } else {
364
+ end = ecma262.ToIntegerOrInfinity(_end);
349
365
  if (end < 0) {
350
366
  end = len + end;
351
367
  if (end < 0) end = 0;
@@ -680,21 +696,21 @@ export const __Array_prototype_toSorted = (_this: any[], callbackFn: any) => {
680
696
  return __Array_prototype_sort(out, callbackFn);
681
697
  };
682
698
 
683
- export const __Array_prototype_toSpliced = (_this: any[], start: number, deleteCount: any, ...items: any[]) => {
699
+ export const __Array_prototype_toSpliced = (_this: any[], _start: any, _deleteCount: any, ...items: any[]) => {
684
700
  let out: any[] = Porffor.allocate();
685
701
  Porffor.clone(_this, out);
686
702
 
687
703
  const len: i32 = _this.length;
688
704
 
689
- start |= 0;
705
+ let start: i32 = ecma262.ToIntegerOrInfinity(_start);
690
706
  if (start < 0) {
691
707
  start = len + start;
692
708
  if (start < 0) start = 0;
693
709
  }
694
710
  if (start > len) start = len;
695
711
 
696
- if (Porffor.rawType(deleteCount) == Porffor.TYPES.undefined) deleteCount = len - start;
697
- deleteCount |= 0;
712
+ if (Porffor.rawType(_deleteCount) == Porffor.TYPES.undefined) _deleteCount = len - start;
713
+ let deleteCount: i32 = ecma262.ToIntegerOrInfinity(_deleteCount);
698
714
 
699
715
  if (deleteCount < 0) deleteCount = 0;
700
716
  if (deleteCount > len - start) deleteCount = len - start;
@@ -765,8 +781,9 @@ memory.copy 0 0`;
765
781
  };
766
782
 
767
783
 
768
- export const __Array_prototype_flat = (_this: any[], depth: any) => {
769
- if (Porffor.rawType(depth) == Porffor.TYPES.undefined) depth = 1;
784
+ export const __Array_prototype_flat = (_this: any[], _depth: any) => {
785
+ if (Porffor.rawType(_depth) == Porffor.TYPES.undefined) _depth = 1;
786
+ let depth: i32 = ecma262.ToIntegerOrInfinity(_depth);
770
787
 
771
788
  let out: any[] = Porffor.allocate();
772
789
  if (depth <= 0) {
@@ -5,16 +5,18 @@ export const __ArrayBuffer_isView = function (value: any): boolean {
5
5
  return false;
6
6
  };
7
7
 
8
- export const ArrayBuffer = function (length: number): ArrayBuffer {
8
+ export const ArrayBuffer = function (length: any): ArrayBuffer {
9
+ // 1. If NewTarget is undefined, throw a TypeError exception.
9
10
  if (!new.target) throw new TypeError("Constructor ArrayBuffer requires 'new'");
10
11
 
11
- if (length < 0) throw new RangeError('Invalid ArrayBuffer length (negative)');
12
- if (length > 4294967295) throw new RangeError('Invalid ArrayBuffer length (over 32 bit address space)');
12
+ // 2. Let byteLength be ? ToIndex(length).
13
+ const byteLength: number = ecma262.ToIndex(length);
13
14
 
14
- length |= 0;
15
+ if (byteLength < 0) throw new RangeError('Invalid ArrayBuffer length (negative)');
16
+ if (byteLength > 4294967295) throw new RangeError('Invalid ArrayBuffer length (over 32 bit address space)');
15
17
 
16
- const out: ArrayBuffer = Porffor.allocateBytes(length + 4);
17
- Porffor.wasm.i32.store(out, length, 0, 0);
18
+ const out: ArrayBuffer = Porffor.allocateBytes(byteLength + 4);
19
+ Porffor.wasm.i32.store(out, byteLength, 0, 0);
18
20
 
19
21
  return out;
20
22
  };
@@ -635,13 +635,34 @@ export const __Object_groupBy = (items: any, callbackFn: any) => {
635
635
  };
636
636
 
637
637
 
638
- export const __Object_prototype_toString = (_this: object) => {
639
- let out: bytestring = '[object Object]';
640
- return out;
638
+ export const __Object_prototype_toString = (_this: any) => {
639
+ let out: bytestring = Porffor.allocate();
640
+
641
+ // 1. If the this value is undefined, return "[object Undefined]".
642
+ if (_this === undefined) return out = '[object Undefined]';
643
+
644
+ // 2. If the this value is null, return "[object Null]".
645
+ if (_this === null) return out = '[object Null]';
646
+
647
+ // todo: toStringTag support
648
+
649
+ const t: i32 = Porffor.rawType(_this);
650
+ if (t == Porffor.TYPES.array) return out = '[object Array]';
651
+ if (t == Porffor.TYPES.function) return out = '[object Function]';
652
+ if (t == Porffor.TYPES.boolean) return out = '[object Boolean]';
653
+ if (t == Porffor.TYPES.number) return out = '[object Number]';
654
+ if (Porffor.fastOr(
655
+ t == Porffor.TYPES.string,
656
+ t == Porffor.TYPES.bytestring)) return out = '[object String]';
657
+ if (t == Porffor.TYPES.date) return out = '[object Date]';
658
+ if (t == Porffor.TYPES.regexp) return out = '[object RegExp]';
659
+
660
+ return out = '[object Object]';
641
661
  };
642
662
 
643
- export const __Object_prototype_toLocaleString = (_this: object) => __Object_prototype_toLocaleString(_this);
663
+ export const __Object_prototype_toLocaleString = (_this: any) => __Object_prototype_toLocaleString(_this);
644
664
 
645
- export const __Object_prototype_valueOf = (_this: object) => {
665
+ export const __Object_prototype_valueOf = (_this: any) => {
666
+ // todo: ToObject
646
667
  return _this;
647
668
  };
@@ -80,6 +80,7 @@ declare global {
80
80
 
81
81
  const ecma262: {
82
82
  ToIntegerOrInfinity(argument: unknown): number;
83
+ ToIndex(value: unknown): number;
83
84
  ToString(argument: unknown): bytestring;
84
85
  ToNumber(argument: unknown): number;
85
86
  ToNumeric(argument: unknown): number;
@@ -79,6 +79,21 @@ export const __ecma262_ToIntegerOrInfinity = (argument: unknown): number => {
79
79
  return number;
80
80
  };
81
81
 
82
+ // 7.1.22 ToIndex (value)
83
+ export const __ecma262_ToIndex = (value: unknown): number => {
84
+ // 1. Let integer be ? ToIntegerOrInfinity(value).
85
+ const integer: number = __ecma262_ToIntegerOrInfinity(value);
86
+
87
+ // 2. If integer is not in the inclusive interval from 0 to 2**53 - 1, throw a RangeError exception.
88
+ if (Porffor.fastOr(
89
+ integer < 0,
90
+ integer > 9007199254740991
91
+ )) throw new RangeError('Invalid index');
92
+
93
+ // 3. Return integer.
94
+ return integer;
95
+ };
96
+
82
97
  // 7.1.17 ToString (argument)
83
98
  // https://tc39.es/ecma262/#sec-tostring
84
99
  export const __ecma262_ToString = (argument: unknown): any => {