porffor 0.2.0-3272f21 → 0.2.0-3756d63

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.
@@ -1,4 +1,4 @@
1
- // @porf -funsafe-no-unlikely-proto-checks
1
+ // @porf --funsafe-no-unlikely-proto-checks
2
2
 
3
3
  // 21.4.1.3 Day (t)
4
4
  // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-day
@@ -259,17 +259,21 @@ export const __ecma262_UTC = (t: number): number => {
259
259
  // https://tc39.es/ecma262/multipage/abstract-operations.html#sec-tointegerorinfinity
260
260
  export const __ecma262_ToIntegerOrInfinity = (argument: unknown): number => {
261
261
  // 1. Let number be ? ToNumber(argument).
262
- const number: number = Number(argument);
262
+ let number: number = Number(argument);
263
263
 
264
264
  // 2. If number is one of NaN, +0𝔽, or -0𝔽, return 0.
265
265
  if (Number.isNaN(number)) return 0;
266
266
 
267
267
  // 3. If number is +∞𝔽, return +∞.
268
268
  // 4. If number is -∞𝔽, return -∞.
269
- // if (!Number.isFinite(number)) return number;
269
+ if (!Number.isFinite(number)) return number;
270
270
 
271
271
  // 5. Return truncate(ℝ(number)).
272
- return Math.trunc(number);
272
+ number = Math.trunc(number);
273
+
274
+ // return 0 for -0
275
+ if (number == 0) return 0;
276
+ return number;
273
277
  };
274
278
 
275
279
  // 21.4.1.27 MakeTime (hour, min, sec, ms)
@@ -355,7 +359,7 @@ export const __ecma262_MakeFullYear = (year: number): number => {
355
359
  const truncated: number = __ecma262_ToIntegerOrInfinity(year);
356
360
 
357
361
  // 3. If truncated is in the inclusive interval from 0 to 99, return 1900𝔽 + 𝔽(truncated).
358
- if (truncated >= 0 && truncated <= 99) return 1900 + truncated;
362
+ if (Porffor.fastAnd(truncated >= 0, truncated <= 99)) return 1900 + truncated;
359
363
 
360
364
  // 4. Return 𝔽(truncated).
361
365
  return truncated;
@@ -376,16 +380,343 @@ export const __ecma262_TimeClip = (time: number): number => {
376
380
  };
377
381
 
378
382
 
379
- // 21.4.2.1 Date (...values)
380
- // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date
381
- export const Date = (): bytestring => {
382
- // 1. If NewTarget is undefined, then
383
- // a. Let now be the time value (UTC) identifying the current time.
384
- // b. Return ToDateString(now).
385
- // return Date$constructor().toString();
386
- return '';
383
+ // 21.4.3.1 Date.now ()
384
+ // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date.now
385
+ // This function returns the time value designating the UTC date and time of the occurrence of the call to it.
386
+ export const __Date_now = (): number => Math.trunc(performance.timeOrigin + performance.now());
387
+
388
+ // 21.4.3.4 Date.UTC (year [, month [, date [, hours [, minutes [, seconds [, ms ]]]]]])
389
+ // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date.utc
390
+ export const __Date_UTC = (year: unknown, month: unknown, date: unknown, hours: unknown, minutes: unknown, seconds: unknown, ms: unknown): number => {
391
+ // todo: passing undefined to params should not act like no arg was passed
392
+
393
+ // 1. Let y be ? ToNumber(year).
394
+ const y: number = Number(year);
395
+
396
+ // 2. If month is present, let m be ? ToNumber(month); else let m be +0𝔽.
397
+ let m: number = 0;
398
+ if (Porffor.rawType(month) != Porffor.TYPES.undefined) m = Number(month);
399
+
400
+ // 3. If date is present, let dt be ? ToNumber(date); else let dt be 1𝔽.
401
+ let dt: number = 1;
402
+ if (Porffor.rawType(date) != Porffor.TYPES.undefined) dt = Number(date);
403
+
404
+ // 4. If hours is present, let h be ? ToNumber(hours); else let h be +0𝔽.
405
+ let h: number = 0;
406
+ if (Porffor.rawType(hours) != Porffor.TYPES.undefined) h = Number(hours);
407
+
408
+ // 5. If minutes is present, let min be ? ToNumber(minutes); else let min be +0𝔽.
409
+ let min: number = 0;
410
+ if (Porffor.rawType(minutes) != Porffor.TYPES.undefined) min = Number(minutes);
411
+
412
+ // 6. If seconds is present, let s be ? ToNumber(seconds); else let s be +0𝔽.
413
+ let s: number = 0;
414
+ if (Porffor.rawType(seconds) != Porffor.TYPES.undefined) s = Number(seconds);
415
+
416
+ // 7. If ms is present, let milli be ? ToNumber(ms); else let milli be +0𝔽.
417
+ let milli: number = 0;
418
+ if (Porffor.rawType(ms) != Porffor.TYPES.undefined) h = Number(ms);
419
+
420
+ // 8. Let yr be MakeFullYear(y).
421
+ const yr: number = __ecma262_MakeFullYear(y);
422
+
423
+ // 9. Return TimeClip(MakeDate(MakeDay(yr, m, dt), MakeTime(h, min, s, milli))).
424
+ return __ecma262_TimeClip(__ecma262_MakeDate(__ecma262_MakeDay(yr, m, dt), __ecma262_MakeTime(h, min, s, milli)));
425
+ };
426
+
427
+
428
+ export const __ecma262_WeekDayName = (tv: number): bytestring => {
429
+ // Name of the entry in Table 62 with the Number WeekDay(tv).
430
+ // Table 62: Names of days of the week
431
+ // Number Name
432
+ // +0𝔽 "Sun"
433
+ // 1𝔽 "Mon"
434
+ // 2𝔽 "Tue"
435
+ // 3𝔽 "Wed"
436
+ // 4𝔽 "Thu"
437
+ // 5𝔽 "Fri"
438
+ // 6𝔽 "Sat"
439
+
440
+ const weekday: number = __ecma262_WeekDay(tv);
441
+
442
+ const lut: bytestring = 'SunMonTueWedThuFriSat';
443
+
444
+ let out: bytestring = '';
445
+ out.length = 3;
446
+
447
+ let outPtr: number = Porffor.wasm`local.get ${out}`;
448
+ let lutPtr: number = Porffor.wasm`local.get ${lut}` + (weekday * 3);
449
+
450
+ Porffor.wasm.i32.store8(outPtr++, Porffor.wasm.i32.load8_u(lutPtr++, 0, 4), 0, 4);
451
+ Porffor.wasm.i32.store8(outPtr++, Porffor.wasm.i32.load8_u(lutPtr++, 0, 4), 0, 4);
452
+ Porffor.wasm.i32.store8(outPtr, Porffor.wasm.i32.load8_u(lutPtr, 0, 4), 0, 4);
453
+
454
+ return out;
455
+ };
456
+
457
+ export const __ecma262_MonthName = (tv: number): bytestring => {
458
+ // Name of the entry in Table 63 with the Number MonthFromTime(tv).
459
+ // Table 63: Names of months of the year
460
+ // Number Name
461
+ // +0𝔽 "Jan"
462
+ // 1𝔽 "Feb"
463
+ // 2𝔽 "Mar"
464
+ // 3𝔽 "Apr"
465
+ // 4𝔽 "May"
466
+ // 5𝔽 "Jun"
467
+ // 6𝔽 "Jul"
468
+ // 7𝔽 "Aug"
469
+ // 8𝔽 "Sep"
470
+ // 9𝔽 "Oct"
471
+ // 10𝔽 "Nov"
472
+ // 11𝔽 "Dec"
473
+
474
+ const month: number = __ecma262_MonthFromTime(tv);
475
+
476
+ const lut: bytestring = 'JanFebMarAprMayJunJulAugSepOctNovDec';
477
+
478
+ let out: bytestring = '';
479
+ out.length = 3;
480
+
481
+ let outPtr: number = Porffor.wasm`local.get ${out}`;
482
+ let lutPtr: number = Porffor.wasm`local.get ${lut}` + (month * 3);
483
+
484
+ Porffor.wasm.i32.store8(outPtr++, Porffor.wasm.i32.load8_u(lutPtr++, 0, 4), 0, 4);
485
+ Porffor.wasm.i32.store8(outPtr++, Porffor.wasm.i32.load8_u(lutPtr++, 0, 4), 0, 4);
486
+ Porffor.wasm.i32.store8(outPtr, Porffor.wasm.i32.load8_u(lutPtr, 0, 4), 0, 4);
487
+
488
+ return out;
489
+ };
490
+
491
+ export const __ecma262_ParseMonthName = (ptr: number): number => {
492
+ const a: i32 = Porffor.wasm.i32.load8_u(ptr, 0, 4);
493
+
494
+ if (a == 74) { // J
495
+ const b: i32 = Porffor.wasm.i32.load8_u(ptr, 0, 5);
496
+
497
+ if (b == 97) return 0; // a - Jan
498
+ if (b == 117) { // u
499
+ const c: i32 = Porffor.wasm.i32.load8_u(ptr, 0, 6);
500
+ if (c == 110) return 5; // n - Jun
501
+ if (c == 108) return 6; // l - Jul
502
+ }
503
+ }
504
+
505
+ if (a == 77) { // M
506
+ const b: i32 = Porffor.wasm.i32.load8_u(ptr, 0, 5);
507
+ if (b == 97) { // a
508
+ const c: i32 = Porffor.wasm.i32.load8_u(ptr, 0, 6);
509
+ if (c == 114) return 2; // r - Mar
510
+ if (c == 121) return 4; // y - May
511
+ }
512
+ }
513
+
514
+ if (a == 65) { // A
515
+ const b: i32 = Porffor.wasm.i32.load8_u(ptr, 0, 5);
516
+ if (b == 112) return 3; // p - Apr
517
+ if (b == 117) return 7; // u - Aug
518
+ }
519
+
520
+ if (a == 70) return 1; // F - Feb
521
+ if (a == 83) return 8; // S - Sep
522
+ if (a == 79) return 9; // O - Oct
523
+ if (a == 78) return 10; // N - Nov
524
+ if (a == 68) return 11; // D - Dec
525
+
526
+ return -1;
387
527
  };
388
528
 
529
+
530
+ // DTSF parser
531
+ export const __ecma262_ParseDTSF = (string: bytestring): number => {
532
+ // formats we need to support:
533
+ // > new Date().toISOString()
534
+ // '2024-05-12T02:44:01.529Z'
535
+
536
+ let y: number = 0;
537
+ let m: number = 0;
538
+ let dt: number = 1;
539
+ let h: number = 0;
540
+ let min: number = 0;
541
+ let s: number = 0;
542
+ let milli: number = 0;
543
+ let tzHour: number = 0;
544
+ let tzMin: number = 0;
545
+
546
+ let n: number = 0;
547
+ let nInd: number = 0;
548
+ let z: boolean = false;
549
+
550
+ const len: i32 = string.length;
551
+ const endPtr: i32 = Porffor.wasm`local.get ${string}` + len;
552
+ let ptr: i32 = Porffor.wasm`local.get ${string}`;
553
+
554
+ while (ptr <= endPtr) { // <= to include extra null byte to set last n
555
+ const chr: i32 = Porffor.wasm.i32.load8_u(ptr++, 0, 4);
556
+ if (Porffor.fastAnd(chr >= 48, chr <= 57)) { // 0-9
557
+ n *= 10;
558
+ n += chr - 48;
559
+ continue;
560
+ }
561
+
562
+ if (chr == 45) { // -
563
+ if (Porffor.fastOr(ptr == Porffor.wasm`local.get ${string}`, nInd == 7)) n = -n;
564
+ }
565
+
566
+ if (n > 0) {
567
+ if (nInd == 0) y = n;
568
+ else if (nInd == 1) m = n - 1;
569
+ else if (nInd == 2) dt = n;
570
+ else if (nInd == 3) h = n;
571
+ else if (nInd == 4) min = n;
572
+ else if (nInd == 5) s = n;
573
+ else if (nInd == 6) milli = n;
574
+ else if (nInd == 7) tzHour = n;
575
+ else if (nInd == 8) tzMin = n;
576
+
577
+ n = 0;
578
+ nInd++;
579
+ }
580
+
581
+ if (chr == 90) { // Z
582
+ if (ptr == len) z = true;
583
+ }
584
+ }
585
+
586
+ h += tzHour;
587
+ min += tzMin;
588
+
589
+ return __ecma262_TimeClip(__ecma262_MakeDate(__ecma262_MakeDay(y, m, dt), __ecma262_MakeTime(h, min, s, milli)));
590
+
591
+ // we do not support local time yet so useless check
592
+ // let t: number = __ecma262_TimeClip(__ecma262_MakeDate(__ecma262_MakeDay(y, m, dt), __ecma262_MakeTime(h, min, s, milli)));
593
+
594
+ // "When the time zone offset is absent, date-only forms are interpreted as a UTC time
595
+ // and date-time forms are interpreted as local time.
596
+ // This is due to a historical spec error that was not consistent with ISO 8601
597
+ // but could not be changed due to web compatibility." :))
598
+ // if (Porffor.fastAnd(
599
+ // nInd > 3, // not date-only
600
+ // z == false, // not utc (ending with Z)
601
+ // nInd < 8, // no time zone offset
602
+ // )) {
603
+ // t = __ecma262_UTC(t);
604
+ // }
605
+
606
+ // return t;
607
+ };
608
+
609
+ // RFC 7231 or Date.prototype.toString() parser
610
+ export const __ecma262_ParseRFC7231OrToString = (string: bytestring): number => {
611
+ // formats we need to support:
612
+ // > new Date().toUTCString()
613
+ // 'Sun, 12 May 2024 02:44:10 GMT'
614
+ // > new Date().toString()
615
+ // 'Sun May 12 2024 02:44:13 GMT+0000 (UTC)'
616
+
617
+ // skip week day
618
+ let ptr: i32 = Porffor.wasm`local.get ${string}` + 4;
619
+
620
+ // skip potential ' '
621
+ if (Porffor.wasm.i32.load8_u(ptr, 0, 4) == 32) ptr++;
622
+
623
+ let dt: number = 0;
624
+ let m: number = -1;
625
+
626
+ // check if date now via numerical
627
+ let chr: i32 = Porffor.wasm.i32.load8_u(ptr, 0, 4);
628
+ if (Porffor.fastAnd(chr >= 48, chr <= 57)) { // 0-9
629
+ // date, month name
630
+ while (true) { // use >0 check instead of !=' ' to handle malformed
631
+ chr = Porffor.wasm.i32.load8_u(ptr++, 0, 4);
632
+ if (chr < 48) break;
633
+
634
+ dt *= 10;
635
+ dt += chr - 48;
636
+ }
637
+
638
+ m = __ecma262_ParseMonthName(ptr);
639
+ ptr += 3;
640
+ } else {
641
+ // month name, date
642
+ m = __ecma262_ParseMonthName(ptr);
643
+ ptr += 4;
644
+
645
+ while (true) { // use >0 check instead of !=' ' to handle malformed
646
+ chr = Porffor.wasm.i32.load8_u(ptr++, 0, 4);
647
+ if (chr < 48) break;
648
+
649
+ dt *= 10;
650
+ dt += chr - 48;
651
+ }
652
+ }
653
+
654
+ // check we parsed month and date correctly
655
+ if (Porffor.fastOr(m == -1, dt == 0, dt > 31)) {
656
+ return NaN;
657
+ }
658
+
659
+ let y: number = 0;
660
+ let h: number = 0;
661
+ let min: number = 0;
662
+ let s: number = 0;
663
+ let tz: number = 0;
664
+
665
+ let n: number = 0;
666
+ let nInd: number = 0;
667
+
668
+ const len: i32 = string.length;
669
+ const endPtr: i32 = Porffor.wasm`local.get ${string}` + len;
670
+
671
+ while (ptr <= endPtr) { // <= to include extra null byte to set last n
672
+ const chr: i32 = Porffor.wasm.i32.load8_u(ptr++, 0, 4);
673
+ if (Porffor.fastAnd(chr >= 48, chr <= 57)) { // 0-9
674
+ n *= 10;
675
+ n += chr - 48;
676
+ continue;
677
+ }
678
+
679
+ if (chr == 45) { // -
680
+ if (nInd == 4) n = -n;
681
+ }
682
+
683
+ if (n > 0) {
684
+ if (nInd == 0) y = n;
685
+ else if (nInd == 1) h = n;
686
+ else if (nInd == 2) min = n;
687
+ else if (nInd == 3) s = n;
688
+ else if (nInd == 4) tz = n;
689
+
690
+ n = 0;
691
+ nInd++;
692
+ }
693
+ }
694
+
695
+ return __ecma262_TimeClip(__ecma262_MakeDate(__ecma262_MakeDay(y, m, dt), __ecma262_MakeTime(h, min, s, 0)));
696
+ };
697
+
698
+ // 21.4.3.2 Date.parse (string)
699
+ // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date.parse
700
+ export const __Date_parse = (string: bytestring): number => {
701
+ // formats we need to support:
702
+ // > new Date().toISOString()
703
+ // '2024-05-12T02:44:01.529Z'
704
+ // > new Date().toUTCString()
705
+ // 'Sun, 12 May 2024 02:44:10 GMT'
706
+ // > new Date().toString()
707
+ // 'Sun May 12 2024 02:44:13 GMT+0000 (UTC)'
708
+
709
+ // if first char is numerical, use DTSF parser
710
+ const chr: i32 = Porffor.wasm.i32.load8_u(string, 0, 4);;
711
+ if (Porffor.fastAnd(chr >= 48, chr <= 57)) { // 0-9
712
+ return __ecma262_ParseDTSF(string);
713
+ }
714
+
715
+ // else, use RFC 7231 or Date.prototype.toString() parser
716
+ return __ecma262_ParseRFC7231OrToString(string);
717
+ };
718
+
719
+
389
720
  // dark wasm magic for a basic allocator, sorry.
390
721
  export const __Porffor_date_allocate = (): Date => {
391
722
  const hack: bytestring = '';
@@ -413,7 +744,8 @@ export const __Porffor_date_write = (ptr: Date, val: number) => {
413
744
  Porffor.wasm.f64.store(ptr, val, 0, 0);
414
745
  };
415
746
 
416
-
747
+ // 21.4.2.1 Date (...values)
748
+ // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date
417
749
  export const Date$constructor = (v0: unknown, v1: unknown, v2: unknown, v3: unknown, v4: unknown, v5: unknown, v6: unknown): Date => {
418
750
  // todo: passing undefined to params should not act like no arg was passed
419
751
 
@@ -450,11 +782,11 @@ export const Date$constructor = (v0: unknown, v1: unknown, v2: unknown, v3: unkn
450
782
  } else {
451
783
  // c. Else,
452
784
  // ii. If v is a String, then
453
- if (valueType == Porffor.TYPES.string || valueType == Porffor.TYPES._bytestring) {
785
+ if (Porffor.fastOr(valueType == Porffor.TYPES.string, valueType == Porffor.TYPES._bytestring)) {
454
786
  // 1. Assert: The next step never returns an abrupt completion because v is a String.
455
787
 
456
788
  // 2. Let tv be the result of parsing v as a date, in exactly the same manner as for the parse method (21.4.3.2).
457
- // todo
789
+ tv = __Date_parse(value);
458
790
  } else {
459
791
  // iii. Else,
460
792
  // 1. Let tv be ? ToNumber(v).
@@ -515,54 +847,6 @@ export const Date$constructor = (v0: unknown, v1: unknown, v2: unknown, v3: unkn
515
847
  };
516
848
 
517
849
 
518
- // 21.4.3.1 Date.now ()
519
- // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date.now
520
- // This function returns the time value designating the UTC date and time of the occurrence of the call to it.
521
- export const __Date_now = (): number => Math.trunc(performance.timeOrigin + performance.now());
522
-
523
- // 21.4.3.2 Date.parse (string)
524
- // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date.parse
525
- // todo
526
-
527
- // 21.4.3.4 Date.UTC (year [, month [, date [, hours [, minutes [, seconds [, ms ]]]]]])
528
- // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date.utc
529
- export const __Date_UTC = (year: unknown, month: unknown, date: unknown, hours: unknown, minutes: unknown, seconds: unknown, ms: unknown): number => {
530
- // todo: passing undefined to params should not act like no arg was passed
531
-
532
- // 1. Let y be ? ToNumber(year).
533
- const y: number = Number(year);
534
-
535
- // 2. If month is present, let m be ? ToNumber(month); else let m be +0𝔽.
536
- let m: number = 0;
537
- if (Porffor.rawType(month) != Porffor.TYPES.undefined) m = Number(month);
538
-
539
- // 3. If date is present, let dt be ? ToNumber(date); else let dt be 1𝔽.
540
- let dt: number = 1;
541
- if (Porffor.rawType(date) != Porffor.TYPES.undefined) dt = Number(date);
542
-
543
- // 4. If hours is present, let h be ? ToNumber(hours); else let h be +0𝔽.
544
- let h: number = 0;
545
- if (Porffor.rawType(hours) != Porffor.TYPES.undefined) h = Number(hours);
546
-
547
- // 5. If minutes is present, let min be ? ToNumber(minutes); else let min be +0𝔽.
548
- let min: number = 0;
549
- if (Porffor.rawType(minutes) != Porffor.TYPES.undefined) min = Number(minutes);
550
-
551
- // 6. If seconds is present, let s be ? ToNumber(seconds); else let s be +0𝔽.
552
- let s: number = 0;
553
- if (Porffor.rawType(seconds) != Porffor.TYPES.undefined) s = Number(seconds);
554
-
555
- // 7. If ms is present, let milli be ? ToNumber(ms); else let milli be +0𝔽.
556
- let milli: number = 0;
557
- if (Porffor.rawType(ms) != Porffor.TYPES.undefined) h = Number(ms);
558
-
559
- // 8. Let yr be MakeFullYear(y).
560
- const yr: number = __ecma262_MakeFullYear(y);
561
-
562
- // 9. Return TimeClip(MakeDate(MakeDay(yr, m, dt), MakeTime(h, min, s, milli))).
563
- return __ecma262_TimeClip(__ecma262_MakeDate(__ecma262_MakeDay(yr, m, dt), __ecma262_MakeTime(h, min, s, milli)));
564
- };
565
-
566
850
  // 21.4.4 Properties of the Date Prototype Object
567
851
  // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-properties-of-the-date-prototype-object
568
852
 
@@ -1134,7 +1418,7 @@ export const ___date_prototype_setUTCDate = (_this: Date, date: any) => {
1134
1418
  if (Number.isNaN(t)) return NaN;
1135
1419
 
1136
1420
  // 6. Let newDate be MakeDate(MakeDay(YearFromTime(t), MonthFromTime(t), dt), TimeWithinDay(t)).
1137
- const newDate = __ecma262_MakeDate(__ecma262_MakeDay(__ecma262_YearFromTime(t), __ecma262_MonthFromTime(t), dt), __ecma262_TimeWithinDay(t));
1421
+ const newDate: number = __ecma262_MakeDate(__ecma262_MakeDay(__ecma262_YearFromTime(t), __ecma262_MonthFromTime(t), dt), __ecma262_TimeWithinDay(t));
1138
1422
 
1139
1423
  // 7. Let v be TimeClip(newDate).
1140
1424
  const v: number = __ecma262_TimeClip(newDate);
@@ -1367,4 +1651,421 @@ export const ___date_prototype_setUTCSeconds = (_this: Date, sec: any, ms: any)
1367
1651
 
1368
1652
  // 11. Return v.
1369
1653
  return v;
1654
+ };
1655
+
1656
+
1657
+ // 21.4.1.32 Date Time String Format
1658
+ // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date-time-string-format
1659
+ // The format is as follows: YYYY-MM-DDTHH:mm:ss.sssZ
1660
+ // YYYY is the year in the proleptic Gregorian calendar as four decimal digits from 0000 to 9999, or as an expanded year of "+" or "-" followed by six decimal digits.
1661
+ // - "-" (hyphen) appears literally twice in the string.
1662
+ // MM is the month of the year as two decimal digits from 01 (January) to 12 (December).
1663
+ // DD is the day of the month as two decimal digits from 01 to 31.
1664
+ // T "T" appears literally in the string, to indicate the beginning of the time element.
1665
+ // HH is the number of complete hours that have passed since midnight as two decimal digits from 00 to 24.
1666
+ // : ":" (colon) appears literally twice in the string.
1667
+ // mm is the number of complete minutes since the start of the hour as two decimal digits from 00 to 59.
1668
+ // ss is the number of complete seconds since the start of the minute as two decimal digits from 00 to 59.
1669
+ // . "." (dot) appears literally in the string.
1670
+ // sss is the number of complete milliseconds since the start of the second as three decimal digits.
1671
+ // Z is the UTC offset representation specified as "Z" (for UTC with no offset) or as either "+" or "-" followed by a time expression HH:mm (a subset of the time zone offset string format for indicating local time ahead of or behind UTC, respectively)
1672
+
1673
+ // fast appending string
1674
+ export const __Porffor_bytestring_appendStr = (str: bytestring, appendage: bytestring): i32 => {
1675
+ const strLen: i32 = str.length;
1676
+ const appendageLen: i32 = appendage.length;
1677
+ let strPtr: i32 = Porffor.wasm`local.get ${str}` + strLen;
1678
+ let appendagePtr: i32 = Porffor.wasm`local.get ${appendage}`;
1679
+ let endPtr: i32 = appendagePtr + appendageLen;
1680
+
1681
+ while (appendagePtr < endPtr) {
1682
+ Porffor.wasm.i32.store8(strPtr++, Porffor.wasm.i32.load8_u(appendagePtr++, 0, 4), 0, 4);
1683
+ }
1684
+
1685
+ str.length = strLen + appendageLen;
1686
+ return 1;
1687
+ };
1688
+
1689
+ // fast appending single character
1690
+ export const __Porffor_bytestring_appendChar = (str: bytestring, char: i32): i32 => {
1691
+ const len: i32 = str.length;
1692
+ Porffor.wasm.i32.store8(Porffor.wasm`local.get ${str}` + len, char, 0, 4);
1693
+ str.length = len + 1;
1694
+ return 1;
1695
+ };
1696
+
1697
+ // fast appending padded number
1698
+ export const __Porffor_bytestring_appendPadNum = (str: bytestring, num: number, len: number): i32 => {
1699
+ let numStr: bytestring = Number.prototype.toFixed(num, 0);
1700
+
1701
+ let strPtr: i32 = Porffor.wasm`local.get ${str}` + str.length;
1702
+
1703
+ let numStrLen: i32 = numStr.length;
1704
+ const strPtrEnd: i32 = strPtr + (len - numStrLen);
1705
+ while (strPtr < strPtrEnd) {
1706
+ Porffor.wasm.i32.store8(strPtr++, 48, 0, 4);
1707
+ }
1708
+
1709
+ let numPtr: i32 = Porffor.wasm`local.get ${numStr}`;
1710
+ const numPtrEnd: i32 = numPtr + numStrLen;
1711
+ while (numPtr < numPtrEnd) {
1712
+ Porffor.wasm.i32.store8(strPtr++, Porffor.wasm.i32.load8_u(numPtr++, 0, 4), 0, 4);
1713
+ }
1714
+
1715
+ str.length = strPtr - Porffor.wasm`local.get ${str}`;
1716
+
1717
+ return 1;
1718
+ };
1719
+
1720
+ // Timestamp to UTC DTSF
1721
+ export const __ecma262_ToUTCDTSF = (t: number): bytestring => {
1722
+ const year: number = __ecma262_YearFromTime(t);
1723
+
1724
+ let out: bytestring = '';
1725
+ out.length = 0;
1726
+
1727
+ if (Porffor.fastOr(year < 0, year >= 10000)) {
1728
+ // extended year format
1729
+ // sign
1730
+ __Porffor_bytestring_appendChar(out, year > 0 ? 43 : 45);
1731
+
1732
+ // 6 digit year
1733
+ __Porffor_bytestring_appendPadNum(out, year, 6);
1734
+ } else {
1735
+ // 4 digit year
1736
+ __Porffor_bytestring_appendPadNum(out, year, 4);
1737
+ }
1738
+ __Porffor_bytestring_appendChar(out, 45); // -
1739
+
1740
+ // 2 digit month (01-12)
1741
+ __Porffor_bytestring_appendPadNum(out, __ecma262_MonthFromTime(t) + 1, 2);
1742
+ __Porffor_bytestring_appendChar(out, 45); // -
1743
+
1744
+ // 2 digit day of the month
1745
+ __Porffor_bytestring_appendPadNum(out, __ecma262_DateFromTime(t), 2);
1746
+ __Porffor_bytestring_appendChar(out, 84); // T
1747
+
1748
+ // 2 digit hour
1749
+ __Porffor_bytestring_appendPadNum(out, __ecma262_HourFromTime(t), 2);
1750
+ __Porffor_bytestring_appendChar(out, 58); // :
1751
+
1752
+ // 2 digit minute
1753
+ __Porffor_bytestring_appendPadNum(out, __ecma262_MinFromTime(t), 2);
1754
+ __Porffor_bytestring_appendChar(out, 58); // :
1755
+
1756
+ // 2 digit second
1757
+ __Porffor_bytestring_appendPadNum(out, __ecma262_SecFromTime(t), 2);
1758
+ __Porffor_bytestring_appendChar(out, 46); // .
1759
+
1760
+ // 3 digit millisecond
1761
+ __Porffor_bytestring_appendPadNum(out, __ecma262_msFromTime(t), 3);
1762
+ __Porffor_bytestring_appendChar(out, 90); // Z
1763
+
1764
+ return out;
1765
+ };
1766
+
1767
+ // 21.4.4.36 Date.prototype.toISOString ()
1768
+ // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date.prototype.toisostring
1769
+ export const ___date_prototype_toISOString = (_this: Date) => {
1770
+ // 1. Let dateObject be the this value.
1771
+ // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]).
1772
+ // 3. Let tv be dateObject.[[DateValue]].
1773
+ const tv: number = __Porffor_date_read(_this);
1774
+
1775
+ // 4. If tv is NaN, throw a RangeError exception.
1776
+ if (Number.isNaN(tv)) {
1777
+ // todo throw
1778
+ return;
1779
+ }
1780
+
1781
+ // 5. Assert: tv is an integral Number.
1782
+
1783
+ // 6. If tv corresponds with a year that cannot be represented in the Date Time String Format, throw a RangeError exception.
1784
+ // todo
1785
+
1786
+ // 7. Return a String representation of tv in the Date Time String Format on the UTC time scale, including all format elements and the UTC offset representation "Z".
1787
+ return __ecma262_ToUTCDTSF(tv);
1788
+ };
1789
+
1790
+ // 21.4.4.37 Date.prototype.toJSON (key)
1791
+ // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date.prototype.tojson
1792
+ export const ___date_prototype_toJSON = (_this: Date, key: any) => {
1793
+ // 1. Let O be ? ToObject(this value).
1794
+ // 2. Let tv be ? ToPrimitive(O, number).
1795
+ // todo: use generic Number() once it supports Date
1796
+ const tv: number = __Porffor_date_read(_this);
1797
+
1798
+ // 3. If tv is a Number and tv is not finite, return null.
1799
+ if (!Number.isFinite(tv)) return null;
1800
+
1801
+ // 4. Return ? Invoke(O, "toISOString").
1802
+ return ___date_prototype_toISOString(_this);
1803
+ };
1804
+
1805
+
1806
+ // 21.4.4.41.1 TimeString (tv)
1807
+ // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-timestring
1808
+ export const __ecma262_TimeString = (tv: number): bytestring => {
1809
+ // we do not follow spec exactly by using number vars and appending at the end
1810
+
1811
+ // 1. Let hour be ToZeroPaddedDecimalString(ℝ(HourFromTime(tv)), 2).
1812
+ const hour: number = __ecma262_HourFromTime(tv);
1813
+
1814
+ // 2. Let minute be ToZeroPaddedDecimalString(ℝ(MinFromTime(tv)), 2).
1815
+ const minute: number = __ecma262_MinFromTime(tv);
1816
+
1817
+ // 3. Let second be ToZeroPaddedDecimalString(ℝ(SecFromTime(tv)), 2).
1818
+ const second: number = __ecma262_SecFromTime(tv);
1819
+
1820
+ // 4. Return the string-concatenation of hour, ":", minute, ":", second, the code unit 0x0020 (SPACE), and "GMT".
1821
+ let out: bytestring = '';
1822
+ out.length = 0;
1823
+
1824
+ __Porffor_bytestring_appendPadNum(out, hour, 2);
1825
+ __Porffor_bytestring_appendChar(out, 58); // ':'
1826
+
1827
+ __Porffor_bytestring_appendPadNum(out, minute, 2);
1828
+ __Porffor_bytestring_appendChar(out, 58); // ':'
1829
+
1830
+ __Porffor_bytestring_appendPadNum(out, second, 2);
1831
+
1832
+ __Porffor_bytestring_appendChar(out, 32); // ' '
1833
+ __Porffor_bytestring_appendChar(out, 71); // 'G'
1834
+ __Porffor_bytestring_appendChar(out, 77); // 'M'
1835
+ __Porffor_bytestring_appendChar(out, 84); // 'T'
1836
+
1837
+ return out;
1838
+ };
1839
+
1840
+ // 21.4.4.41.2 DateString (tv)
1841
+ // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-datestring
1842
+ export const __ecma262_DateString = (tv: number): bytestring => {
1843
+ // we do not follow spec exactly by using number vars and appending at the end
1844
+
1845
+ // 1. Let weekday be the Name of the entry in Table 62 with the Number WeekDay(tv).
1846
+ const weekday: bytestring = __ecma262_WeekDayName(tv);
1847
+
1848
+ // 2. Let month be the Name of the entry in Table 63 with the Number MonthFromTime(tv).
1849
+ const month: bytestring = __ecma262_MonthName(tv);
1850
+
1851
+ // 3. Let day be ToZeroPaddedDecimalString(ℝ(DateFromTime(tv)), 2).
1852
+ const day: number = __ecma262_DateFromTime(tv);
1853
+
1854
+ // 4. Let yv be YearFromTime(tv).
1855
+ const yv: number = __ecma262_YearFromTime(tv);
1856
+
1857
+ // 5. If yv is +0𝔽 or yv > +0𝔽, let yearSign be the empty String; otherwise, let yearSign be "-".
1858
+ // 6. Let paddedYear be ToZeroPaddedDecimalString(abs(ℝ(yv)), 4).
1859
+ // 7. Return the string-concatenation of weekday, the code unit 0x0020 (SPACE), month, the code unit 0x0020 (SPACE), day, the code unit 0x0020 (SPACE), yearSign, and paddedYear.
1860
+ let out: bytestring = '';
1861
+ out.length = 0;
1862
+
1863
+ // weekday
1864
+ __Porffor_bytestring_appendStr(out, weekday);
1865
+ __Porffor_bytestring_appendChar(out, 32); // ' '
1866
+
1867
+ // month
1868
+ __Porffor_bytestring_appendStr(out, month);
1869
+ __Porffor_bytestring_appendChar(out, 32); // ' '
1870
+
1871
+ // day
1872
+ __Porffor_bytestring_appendPadNum(out, day, 2);
1873
+ __Porffor_bytestring_appendChar(out, 32); // ' '
1874
+
1875
+ // year
1876
+ if (yv < 0) __Porffor_bytestring_appendChar(out, 45); // sign
1877
+ __Porffor_bytestring_appendPadNum(out, yv, 4);
1878
+
1879
+ return out;
1880
+ };
1881
+
1882
+ // 21.4.4.41.3 TimeZoneString (tv)
1883
+ // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-timezonestring
1884
+ export const __ecma262_TimeZoneString = (tv: number) => {
1885
+ // todo: time zone support
1886
+ let out: bytestring = '+0000 (UTC)';
1887
+ return out;
1888
+ };
1889
+
1890
+ // 21.4.4.41.4 ToDateString (tv)
1891
+ // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-todatestring
1892
+ export const __ecma262_ToDateString = (tv: number) => {
1893
+ let out: bytestring = '';
1894
+ out.length = 0;
1895
+
1896
+ // 1. If tv is NaN, return "Invalid Date".
1897
+ if (Number.isNaN(tv)) {
1898
+ out = 'Invalid Date';
1899
+ return out;
1900
+ }
1901
+
1902
+ // 2. Let t be LocalTime(tv).
1903
+ const t: number = __ecma262_LocalTime(tv);
1904
+
1905
+ // 3. Return the string-concatenation of DateString(t), the code unit 0x0020 (SPACE), TimeString(t), and TimeZoneString(tv).
1906
+ __Porffor_bytestring_appendStr(out, __ecma262_DateString(t));
1907
+ __Porffor_bytestring_appendChar(out, 32);
1908
+
1909
+ __Porffor_bytestring_appendStr(out, __ecma262_TimeString(t));
1910
+
1911
+ __Porffor_bytestring_appendStr(out, __ecma262_TimeZoneString(tv));
1912
+
1913
+ return out;
1914
+ };
1915
+
1916
+ // 21.4.4.41 Date.prototype.toString ()
1917
+ // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date.prototype.tostring
1918
+ export const ___date_prototype_toString = (_this: Date) => {
1919
+ // 1. Let dateObject be the this value.
1920
+ // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]).
1921
+ // 3. Let tv be dateObject.[[DateValue]].
1922
+ const tv: number = __Porffor_date_read(_this);
1923
+
1924
+ // 4. Return ToDateString(tv).
1925
+ return __ecma262_ToDateString(tv);
1926
+ };
1927
+
1928
+ // 21.4.4.42 Date.prototype.toTimeString ()
1929
+ // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date.prototype.totimestring
1930
+ export const ___date_prototype_toTimeString = (_this: Date) => {
1931
+ // 1. Let dateObject be the this value.
1932
+ // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]).
1933
+ // 3. Let tv be dateObject.[[DateValue]].
1934
+ const tv: number = __Porffor_date_read(_this);
1935
+
1936
+ // 4. If tv is NaN, return "Invalid Date".
1937
+ let out: bytestring = '';
1938
+ out.length = 0;
1939
+
1940
+ if (Number.isNaN(tv)) {
1941
+ out = 'Invalid Date';
1942
+ return out;
1943
+ }
1944
+
1945
+ // 5. Let t be LocalTime(tv).
1946
+ const t: number = __ecma262_LocalTime(tv);
1947
+
1948
+ // 6. Return the string-concatenation of TimeString(t) and TimeZoneString(tv).
1949
+ __Porffor_bytestring_appendStr(out, __ecma262_TimeString(t));
1950
+ __Porffor_bytestring_appendStr(out, __ecma262_TimeZoneString(tv));
1951
+
1952
+ return out;
1953
+ };
1954
+
1955
+
1956
+ // 21.4.4.35 Date.prototype.toDateString ()
1957
+ // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date.prototype.todatestring
1958
+ export const ___date_prototype_toDateString = (_this: Date) => {
1959
+ // 1. Let dateObject be the this value.
1960
+ // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]).
1961
+ // 3. Let tv be dateObject.[[DateValue]].
1962
+ const tv: number = __Porffor_date_read(_this);
1963
+
1964
+ // 4. If tv is NaN, return "Invalid Date".
1965
+ let out: bytestring = '';
1966
+ out.length = 0;
1967
+
1968
+ if (Number.isNaN(tv)) {
1969
+ out = 'Invalid Date';
1970
+ return out;
1971
+ }
1972
+
1973
+ // 5. Let t be LocalTime(tv).
1974
+ const t: number = __ecma262_LocalTime(tv);
1975
+
1976
+ // 6. Return DateString(t).
1977
+ out = __ecma262_DateString(t);
1978
+ return out;
1979
+ };
1980
+
1981
+ // 21.4.4.43 Date.prototype.toUTCString ()
1982
+ // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date.prototype.toutcstring
1983
+ export const ___date_prototype_toUTCString = (_this: Date) => {
1984
+ // 1. Let dateObject be the this value.
1985
+ // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]).
1986
+ // 3. Let tv be dateObject.[[DateValue]].
1987
+ const tv: number = __Porffor_date_read(_this);
1988
+
1989
+ // 4. If tv is NaN, return "Invalid Date".
1990
+ let out: bytestring = '';
1991
+ out.length = 0;
1992
+
1993
+ if (Number.isNaN(tv)) {
1994
+ out = 'Invalid Date';
1995
+ return out;
1996
+ }
1997
+
1998
+ // 5. Let weekday be the Name of the entry in Table 62 with the Number WeekDay(tv).
1999
+ const weekday: bytestring = __ecma262_WeekDayName(tv);
2000
+
2001
+ // 6. Let month be the Name of the entry in Table 63 with the Number MonthFromTime(tv).
2002
+ const month: bytestring = __ecma262_MonthName(tv);
2003
+
2004
+ // 7. Let day be ToZeroPaddedDecimalString(ℝ(DateFromTime(tv)), 2).
2005
+ const day: number = __ecma262_DateFromTime(tv);
2006
+
2007
+ // 8. Let yv be YearFromTime(tv).
2008
+ const yv: number = __ecma262_YearFromTime(tv);
2009
+
2010
+ // 9. If yv is +0𝔽 or yv > +0𝔽, let yearSign be the empty String; otherwise, let yearSign be "-".
2011
+ // 10. Let paddedYear be ToZeroPaddedDecimalString(abs(ℝ(yv)), 4).
2012
+ // 11. Return the string-concatenation of weekday, ",", the code unit 0x0020 (SPACE), day, the code unit 0x0020 (SPACE), month, the code unit 0x0020 (SPACE), yearSign, paddedYear, the code unit 0x0020 (SPACE), and TimeString(tv).
2013
+ // weekday
2014
+ __Porffor_bytestring_appendStr(out, weekday);
2015
+ __Porffor_bytestring_appendChar(out, 44); // ','
2016
+ __Porffor_bytestring_appendChar(out, 32); // ' '
2017
+
2018
+ // day
2019
+ __Porffor_bytestring_appendPadNum(out, day, 2);
2020
+ __Porffor_bytestring_appendChar(out, 32); // ' '
2021
+
2022
+ // month
2023
+ __Porffor_bytestring_appendStr(out, month);
2024
+ __Porffor_bytestring_appendChar(out, 32); // ' '
2025
+
2026
+ // year
2027
+ if (yv < 0) __Porffor_bytestring_appendChar(out, 45); // sign
2028
+ __Porffor_bytestring_appendPadNum(out, yv, 4);
2029
+
2030
+ __Porffor_bytestring_appendChar(out, 32); // ' '
2031
+ __Porffor_bytestring_appendStr(out, __ecma262_TimeString(tv));
2032
+
2033
+ return out;
2034
+ };
2035
+
2036
+ // 21.4.4.38 Date.prototype.toLocaleDateString ([ reserved1 [, reserved2 ]])
2037
+ // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date.prototype.tolocaledatestring
2038
+ export const ___date_prototype_toLocaleDateString = (_this: Date, reserved1: any, reserved2: any) => {
2039
+ return ___date_prototype_toDateString(_this);
2040
+ };
2041
+
2042
+ // 21.4.4.39 Date.prototype.toLocaleString ([ reserved1 [, reserved2 ]])
2043
+ // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date.prototype.tolocalestring
2044
+ export const ___date_prototype_toLocaleString = (_this: Date, reserved1: any, reserved2: any) => {
2045
+ return ___date_prototype_toString(_this);
2046
+ };
2047
+
2048
+ // 21.4.4.40 Date.prototype.toLocaleTimeString ([ reserved1 [, reserved2 ]])
2049
+ // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date.prototype.tolocaletimestring
2050
+ export const ___date_prototype_toLocaleTimeString = (_this: Date, reserved1: any, reserved2: any) => {
2051
+ return ___date_prototype_toTimeString(_this);
2052
+ };
2053
+
2054
+
2055
+ // 21.4.4.44 Date.prototype.valueOf ()
2056
+ export const ___date_prototype_valueOf = (_this: Date) => {
2057
+ // 1. Let dateObject be the this value.
2058
+ // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]).
2059
+ // 3. Return dateObject.[[DateValue]].
2060
+ return __Porffor_date_read(_this);
2061
+ };
2062
+
2063
+
2064
+ // 21.4.2.1 Date (...values)
2065
+ // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date
2066
+ export const Date = (): bytestring => {
2067
+ // 1. If NewTarget is undefined, then
2068
+ // a. Let now be the time value (UTC) identifying the current time.
2069
+ // b. Return ToDateString(now).
2070
+ return __ecma262_ToDateString(__Date_now());
1370
2071
  };