porffor 0.2.0-ef043de → 0.2.0-f435128

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)
@@ -376,16 +380,65 @@ 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.2 Date.parse (string)
389
+ // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date.parse
390
+ export const __Date_parse = (string: bytestring): number => {
391
+ // formats we need to support:
392
+ // > new Date().toISOString()
393
+ // '2024-05-12T02:44:01.529Z'
394
+ // > new Date().toUTCString()
395
+ // 'Sun, 12 May 2024 02:44:10 GMT'
396
+ // > new Date().toString()
397
+ // 'Sun May 12 2024 02:44:13 GMT+0000 (UTC)'
398
+
399
+ let ptr: i32 = Porffor.wasm`local.get ${string}`;
387
400
  };
388
401
 
402
+ // 21.4.3.4 Date.UTC (year [, month [, date [, hours [, minutes [, seconds [, ms ]]]]]])
403
+ // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date.utc
404
+ export const __Date_UTC = (year: unknown, month: unknown, date: unknown, hours: unknown, minutes: unknown, seconds: unknown, ms: unknown): number => {
405
+ // todo: passing undefined to params should not act like no arg was passed
406
+
407
+ // 1. Let y be ? ToNumber(year).
408
+ const y: number = Number(year);
409
+
410
+ // 2. If month is present, let m be ? ToNumber(month); else let m be +0𝔽.
411
+ let m: number = 0;
412
+ if (Porffor.rawType(month) != Porffor.TYPES.undefined) m = Number(month);
413
+
414
+ // 3. If date is present, let dt be ? ToNumber(date); else let dt be 1𝔽.
415
+ let dt: number = 1;
416
+ if (Porffor.rawType(date) != Porffor.TYPES.undefined) dt = Number(date);
417
+
418
+ // 4. If hours is present, let h be ? ToNumber(hours); else let h be +0𝔽.
419
+ let h: number = 0;
420
+ if (Porffor.rawType(hours) != Porffor.TYPES.undefined) h = Number(hours);
421
+
422
+ // 5. If minutes is present, let min be ? ToNumber(minutes); else let min be +0𝔽.
423
+ let min: number = 0;
424
+ if (Porffor.rawType(minutes) != Porffor.TYPES.undefined) min = Number(minutes);
425
+
426
+ // 6. If seconds is present, let s be ? ToNumber(seconds); else let s be +0𝔽.
427
+ let s: number = 0;
428
+ if (Porffor.rawType(seconds) != Porffor.TYPES.undefined) s = Number(seconds);
429
+
430
+ // 7. If ms is present, let milli be ? ToNumber(ms); else let milli be +0𝔽.
431
+ let milli: number = 0;
432
+ if (Porffor.rawType(ms) != Porffor.TYPES.undefined) h = Number(ms);
433
+
434
+ // 8. Let yr be MakeFullYear(y).
435
+ const yr: number = __ecma262_MakeFullYear(y);
436
+
437
+ // 9. Return TimeClip(MakeDate(MakeDay(yr, m, dt), MakeTime(h, min, s, milli))).
438
+ return __ecma262_TimeClip(__ecma262_MakeDate(__ecma262_MakeDay(yr, m, dt), __ecma262_MakeTime(h, min, s, milli)));
439
+ };
440
+
441
+
389
442
  // dark wasm magic for a basic allocator, sorry.
390
443
  export const __Porffor_date_allocate = (): Date => {
391
444
  const hack: bytestring = '';
@@ -413,7 +466,8 @@ export const __Porffor_date_write = (ptr: Date, val: number) => {
413
466
  Porffor.wasm.f64.store(ptr, val, 0, 0);
414
467
  };
415
468
 
416
-
469
+ // 21.4.2.1 Date (...values)
470
+ // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date
417
471
  export const Date$constructor = (v0: unknown, v1: unknown, v2: unknown, v3: unknown, v4: unknown, v5: unknown, v6: unknown): Date => {
418
472
  // todo: passing undefined to params should not act like no arg was passed
419
473
 
@@ -454,7 +508,7 @@ export const Date$constructor = (v0: unknown, v1: unknown, v2: unknown, v3: unkn
454
508
  // 1. Assert: The next step never returns an abrupt completion because v is a String.
455
509
 
456
510
  // 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
511
+ tv = __Date_parse(value);
458
512
  } else {
459
513
  // iii. Else,
460
514
  // 1. Let tv be ? ToNumber(v).
@@ -515,54 +569,6 @@ export const Date$constructor = (v0: unknown, v1: unknown, v2: unknown, v3: unkn
515
569
  };
516
570
 
517
571
 
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
572
  // 21.4.4 Properties of the Date Prototype Object
567
573
  // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-properties-of-the-date-prototype-object
568
574
 
@@ -1134,7 +1140,7 @@ export const ___date_prototype_setUTCDate = (_this: Date, date: any) => {
1134
1140
  if (Number.isNaN(t)) return NaN;
1135
1141
 
1136
1142
  // 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));
1143
+ const newDate: number = __ecma262_MakeDate(__ecma262_MakeDay(__ecma262_YearFromTime(t), __ecma262_MonthFromTime(t), dt), __ecma262_TimeWithinDay(t));
1138
1144
 
1139
1145
  // 7. Let v be TimeClip(newDate).
1140
1146
  const v: number = __ecma262_TimeClip(newDate);
@@ -1367,4 +1373,484 @@ export const ___date_prototype_setUTCSeconds = (_this: Date, sec: any, ms: any)
1367
1373
 
1368
1374
  // 11. Return v.
1369
1375
  return v;
1376
+ };
1377
+
1378
+
1379
+ // 21.4.1.32 Date Time String Format
1380
+ // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date-time-string-format
1381
+ // The format is as follows: YYYY-MM-DDTHH:mm:ss.sssZ
1382
+ // 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.
1383
+ // - "-" (hyphen) appears literally twice in the string.
1384
+ // MM is the month of the year as two decimal digits from 01 (January) to 12 (December).
1385
+ // DD is the day of the month as two decimal digits from 01 to 31.
1386
+ // T "T" appears literally in the string, to indicate the beginning of the time element.
1387
+ // HH is the number of complete hours that have passed since midnight as two decimal digits from 00 to 24.
1388
+ // : ":" (colon) appears literally twice in the string.
1389
+ // mm is the number of complete minutes since the start of the hour as two decimal digits from 00 to 59.
1390
+ // ss is the number of complete seconds since the start of the minute as two decimal digits from 00 to 59.
1391
+ // . "." (dot) appears literally in the string.
1392
+ // sss is the number of complete milliseconds since the start of the second as three decimal digits.
1393
+ // 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)
1394
+
1395
+ // fast appending string
1396
+ export const __Porffor_bytestring_appendStr = (str: bytestring, appendage: bytestring): i32 => {
1397
+ const strLen: i32 = str.length;
1398
+ const appendageLen: i32 = appendage.length;
1399
+ let strPtr: i32 = Porffor.wasm`local.get ${str}` + strLen;
1400
+ let appendagePtr: i32 = Porffor.wasm`local.get ${appendage}`;
1401
+ let endPtr: i32 = appendagePtr + appendageLen;
1402
+
1403
+ while (appendagePtr < endPtr) {
1404
+ Porffor.wasm.i32.store8(strPtr++, Porffor.wasm.i32.load8_u(appendagePtr++, 0, 4), 0, 4);
1405
+ }
1406
+
1407
+ str.length = strLen + appendageLen;
1408
+ return 1;
1409
+ };
1410
+
1411
+ // fast appending single character
1412
+ export const __Porffor_bytestring_appendChar = (str: bytestring, char: i32): i32 => {
1413
+ const len: i32 = str.length;
1414
+ Porffor.wasm.i32.store8(Porffor.wasm`local.get ${str}` + len, char, 0, 4);
1415
+ str.length = len + 1;
1416
+ return 1;
1417
+ };
1418
+
1419
+ // fast appending padded number
1420
+ export const __Porffor_bytestring_appendPadNum = (str: bytestring, num: number, len: number): i32 => {
1421
+ let numStr: bytestring = Number.prototype.toFixed(num, 0);
1422
+
1423
+ let strPtr: i32 = Porffor.wasm`local.get ${str}` + str.length;
1424
+
1425
+ let numStrLen: i32 = numStr.length;
1426
+ const strPtrEnd: i32 = strPtr + (len - numStrLen);
1427
+ while (strPtr < strPtrEnd) {
1428
+ Porffor.wasm.i32.store8(strPtr++, 48, 0, 4);
1429
+ }
1430
+
1431
+ let numPtr: i32 = Porffor.wasm`local.get ${numStr}`;
1432
+ const numPtrEnd: i32 = numPtr + numStrLen;
1433
+ while (numPtr < numPtrEnd) {
1434
+ Porffor.wasm.i32.store8(strPtr++, Porffor.wasm.i32.load8_u(numPtr++, 0, 4), 0, 4);
1435
+ }
1436
+
1437
+ str.length = strPtr - Porffor.wasm`local.get ${str}`;
1438
+
1439
+ return 1;
1440
+ };
1441
+
1442
+ // Timestamp to UTC DTSF
1443
+ export const __ecma262_ToUTCDTSF = (t: number): bytestring => {
1444
+ const year: number = __ecma262_YearFromTime(t);
1445
+
1446
+ let out: bytestring = '';
1447
+ out.length = 0;
1448
+
1449
+ if (year < 0 || year >= 10000) {
1450
+ // extended year format
1451
+ // sign
1452
+ __Porffor_bytestring_appendChar(out, year > 0 ? 43 : 45);
1453
+
1454
+ // 6 digit year
1455
+ __Porffor_bytestring_appendPadNum(out, year, 6);
1456
+ } else {
1457
+ // 4 digit year
1458
+ __Porffor_bytestring_appendPadNum(out, year, 4);
1459
+ }
1460
+ __Porffor_bytestring_appendChar(out, 45); // -
1461
+
1462
+ // 2 digit month (01-12)
1463
+ __Porffor_bytestring_appendPadNum(out, __ecma262_MonthFromTime(t) + 1, 2);
1464
+ __Porffor_bytestring_appendChar(out, 45); // -
1465
+
1466
+ // 2 digit day of the month
1467
+ __Porffor_bytestring_appendPadNum(out, __ecma262_DateFromTime(t), 2);
1468
+ __Porffor_bytestring_appendChar(out, 84); // T
1469
+
1470
+ // 2 digit hour
1471
+ __Porffor_bytestring_appendPadNum(out, __ecma262_HourFromTime(t), 2);
1472
+ __Porffor_bytestring_appendChar(out, 58); // :
1473
+
1474
+ // 2 digit minute
1475
+ __Porffor_bytestring_appendPadNum(out, __ecma262_MinFromTime(t), 2);
1476
+ __Porffor_bytestring_appendChar(out, 58); // :
1477
+
1478
+ // 2 digit second
1479
+ __Porffor_bytestring_appendPadNum(out, __ecma262_SecFromTime(t), 2);
1480
+ __Porffor_bytestring_appendChar(out, 46); // .
1481
+
1482
+ // 3 digit millisecond
1483
+ __Porffor_bytestring_appendPadNum(out, __ecma262_msFromTime(t), 3);
1484
+ __Porffor_bytestring_appendChar(out, 90); // Z
1485
+
1486
+ return out;
1487
+ };
1488
+
1489
+ // 21.4.4.36 Date.prototype.toISOString ()
1490
+ // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date.prototype.toisostring
1491
+ export const ___date_prototype_toISOString = (_this: Date) => {
1492
+ // 1. Let dateObject be the this value.
1493
+ // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]).
1494
+ // 3. Let tv be dateObject.[[DateValue]].
1495
+ const tv: number = __Porffor_date_read(_this);
1496
+
1497
+ // 4. If tv is NaN, throw a RangeError exception.
1498
+ if (Number.isNaN(tv)) {
1499
+ // todo throw
1500
+ return;
1501
+ }
1502
+
1503
+ // 5. Assert: tv is an integral Number.
1504
+
1505
+ // 6. If tv corresponds with a year that cannot be represented in the Date Time String Format, throw a RangeError exception.
1506
+ // todo
1507
+
1508
+ // 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".
1509
+ return __ecma262_ToUTCDTSF(tv);
1510
+ };
1511
+
1512
+ // 21.4.4.37 Date.prototype.toJSON (key)
1513
+ // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date.prototype.tojson
1514
+ export const ___date_prototype_toJSON = (_this: Date, key: any) => {
1515
+ // 1. Let O be ? ToObject(this value).
1516
+ // 2. Let tv be ? ToPrimitive(O, number).
1517
+ // todo: use generic Number() once it supports Date
1518
+ const tv: number = __Porffor_date_read(_this);
1519
+
1520
+ // 3. If tv is a Number and tv is not finite, return null.
1521
+ if (!Number.isFinite(tv)) return null;
1522
+
1523
+ // 4. Return ? Invoke(O, "toISOString").
1524
+ return ___date_prototype_toISOString(_this);
1525
+ };
1526
+
1527
+
1528
+ // 21.4.4.41.1 TimeString (tv)
1529
+ // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-timestring
1530
+ export const __ecma262_TimeString = (tv: number): bytestring => {
1531
+ // we do not follow spec exactly by using number vars and appending at the end
1532
+
1533
+ // 1. Let hour be ToZeroPaddedDecimalString(ℝ(HourFromTime(tv)), 2).
1534
+ const hour: number = __ecma262_HourFromTime(tv);
1535
+
1536
+ // 2. Let minute be ToZeroPaddedDecimalString(ℝ(MinFromTime(tv)), 2).
1537
+ const minute: number = __ecma262_MinFromTime(tv);
1538
+
1539
+ // 3. Let second be ToZeroPaddedDecimalString(ℝ(SecFromTime(tv)), 2).
1540
+ const second: number = __ecma262_SecFromTime(tv);
1541
+
1542
+ // 4. Return the string-concatenation of hour, ":", minute, ":", second, the code unit 0x0020 (SPACE), and "GMT".
1543
+ let out: bytestring = '';
1544
+ out.length = 0;
1545
+
1546
+ __Porffor_bytestring_appendPadNum(out, hour, 2);
1547
+ __Porffor_bytestring_appendChar(out, 58); // ':'
1548
+
1549
+ __Porffor_bytestring_appendPadNum(out, minute, 2);
1550
+ __Porffor_bytestring_appendChar(out, 58); // ':'
1551
+
1552
+ __Porffor_bytestring_appendPadNum(out, second, 2);
1553
+
1554
+ __Porffor_bytestring_appendChar(out, 32); // ' '
1555
+ __Porffor_bytestring_appendChar(out, 71); // 'G'
1556
+ __Porffor_bytestring_appendChar(out, 77); // 'M'
1557
+ __Porffor_bytestring_appendChar(out, 84); // 'T'
1558
+
1559
+ return out;
1560
+ };
1561
+
1562
+ export const __ecma262_WeekDayName = (tv: number): bytestring => {
1563
+ // Name of the entry in Table 62 with the Number WeekDay(tv).
1564
+ // Table 62: Names of days of the week
1565
+ // Number Name
1566
+ // +0𝔽 "Sun"
1567
+ // 1𝔽 "Mon"
1568
+ // 2𝔽 "Tue"
1569
+ // 3𝔽 "Wed"
1570
+ // 4𝔽 "Thu"
1571
+ // 5𝔽 "Fri"
1572
+ // 6𝔽 "Sat"
1573
+
1574
+ const weekday: number = __ecma262_WeekDay(tv);
1575
+
1576
+ const lut: bytestring = 'SunMonTueWedThuFriSat';
1577
+
1578
+ let out: bytestring = '';
1579
+ out.length = 3;
1580
+
1581
+ let outPtr: number = Porffor.wasm`local.get ${out}`;
1582
+ let lutPtr: number = Porffor.wasm`local.get ${lut}` + (weekday * 3);
1583
+
1584
+ Porffor.wasm.i32.store8(outPtr++, Porffor.wasm.i32.load8_u(lutPtr++, 0, 4), 0, 4);
1585
+ Porffor.wasm.i32.store8(outPtr++, Porffor.wasm.i32.load8_u(lutPtr++, 0, 4), 0, 4);
1586
+ Porffor.wasm.i32.store8(outPtr, Porffor.wasm.i32.load8_u(lutPtr, 0, 4), 0, 4);
1587
+
1588
+ return out;
1589
+ };
1590
+
1591
+ export const __ecma262_MonthName = (tv: number): bytestring => {
1592
+ // Name of the entry in Table 63 with the Number MonthFromTime(tv).
1593
+ // Table 63: Names of months of the year
1594
+ // Number Name
1595
+ // +0𝔽 "Jan"
1596
+ // 1𝔽 "Feb"
1597
+ // 2𝔽 "Mar"
1598
+ // 3𝔽 "Apr"
1599
+ // 4𝔽 "May"
1600
+ // 5𝔽 "Jun"
1601
+ // 6𝔽 "Jul"
1602
+ // 7𝔽 "Aug"
1603
+ // 8𝔽 "Sep"
1604
+ // 9𝔽 "Oct"
1605
+ // 10𝔽 "Nov"
1606
+ // 11𝔽 "Dec"
1607
+
1608
+ const month: number = __ecma262_MonthFromTime(tv);
1609
+
1610
+ const lut: bytestring = 'JanFebMarAprMayJunJulAugSepOctNovDec';
1611
+
1612
+ let out: bytestring = '';
1613
+ out.length = 3;
1614
+
1615
+ let outPtr: number = Porffor.wasm`local.get ${out}`;
1616
+ let lutPtr: number = Porffor.wasm`local.get ${lut}` + (month * 3);
1617
+
1618
+ Porffor.wasm.i32.store8(outPtr++, Porffor.wasm.i32.load8_u(lutPtr++, 0, 4), 0, 4);
1619
+ Porffor.wasm.i32.store8(outPtr++, Porffor.wasm.i32.load8_u(lutPtr++, 0, 4), 0, 4);
1620
+ Porffor.wasm.i32.store8(outPtr, Porffor.wasm.i32.load8_u(lutPtr, 0, 4), 0, 4);
1621
+
1622
+ return out;
1623
+ };
1624
+
1625
+ // 21.4.4.41.2 DateString (tv)
1626
+ // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-datestring
1627
+ export const __ecma262_DateString = (tv: number): bytestring => {
1628
+ // we do not follow spec exactly by using number vars and appending at the end
1629
+
1630
+ // 1. Let weekday be the Name of the entry in Table 62 with the Number WeekDay(tv).
1631
+ const weekday: bytestring = __ecma262_WeekDayName(tv);
1632
+
1633
+ // 2. Let month be the Name of the entry in Table 63 with the Number MonthFromTime(tv).
1634
+ const month: bytestring = __ecma262_MonthName(tv);
1635
+
1636
+ // 3. Let day be ToZeroPaddedDecimalString(ℝ(DateFromTime(tv)), 2).
1637
+ const day: number = __ecma262_DateFromTime(tv);
1638
+
1639
+ // 4. Let yv be YearFromTime(tv).
1640
+ const yv: number = __ecma262_YearFromTime(tv);
1641
+
1642
+ // 5. If yv is +0𝔽 or yv > +0𝔽, let yearSign be the empty String; otherwise, let yearSign be "-".
1643
+ // 6. Let paddedYear be ToZeroPaddedDecimalString(abs(ℝ(yv)), 4).
1644
+ // 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.
1645
+ let out: bytestring = '';
1646
+ out.length = 0;
1647
+
1648
+ // weekday
1649
+ __Porffor_bytestring_appendStr(out, weekday);
1650
+ __Porffor_bytestring_appendChar(out, 32); // ' '
1651
+
1652
+ // month
1653
+ __Porffor_bytestring_appendStr(out, month);
1654
+ __Porffor_bytestring_appendChar(out, 32); // ' '
1655
+
1656
+ // day
1657
+ __Porffor_bytestring_appendPadNum(out, day, 2);
1658
+ __Porffor_bytestring_appendChar(out, 32); // ' '
1659
+
1660
+ // year
1661
+ if (yv < 0) __Porffor_bytestring_appendChar(out, 45); // sign
1662
+ __Porffor_bytestring_appendPadNum(out, yv, 4);
1663
+
1664
+ return out;
1665
+ };
1666
+
1667
+ // 21.4.4.41.3 TimeZoneString (tv)
1668
+ // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-timezonestring
1669
+ export const __ecma262_TimeZoneString = (tv: number) => {
1670
+ // todo: time zone support
1671
+ let out: bytestring = '+0000 (UTC)';
1672
+ return out;
1673
+ };
1674
+
1675
+ // 21.4.4.41.4 ToDateString (tv)
1676
+ // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-todatestring
1677
+ export const __ecma262_ToDateString = (tv: number) => {
1678
+ let out: bytestring = '';
1679
+ out.length = 0;
1680
+
1681
+ // 1. If tv is NaN, return "Invalid Date".
1682
+ if (Number.isNaN(tv)) {
1683
+ out = 'Invalid Date';
1684
+ return out;
1685
+ }
1686
+
1687
+ // 2. Let t be LocalTime(tv).
1688
+ const t: number = __ecma262_LocalTime(tv);
1689
+
1690
+ // 3. Return the string-concatenation of DateString(t), the code unit 0x0020 (SPACE), TimeString(t), and TimeZoneString(tv).
1691
+ __Porffor_bytestring_appendStr(out, __ecma262_DateString(t));
1692
+ __Porffor_bytestring_appendChar(out, 32);
1693
+
1694
+ __Porffor_bytestring_appendStr(out, __ecma262_TimeString(t));
1695
+
1696
+ __Porffor_bytestring_appendStr(out, __ecma262_TimeZoneString(tv));
1697
+
1698
+ return out;
1699
+ };
1700
+
1701
+ // 21.4.4.41 Date.prototype.toString ()
1702
+ // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date.prototype.tostring
1703
+ export const ___date_prototype_toString = (_this: Date) => {
1704
+ // 1. Let dateObject be the this value.
1705
+ // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]).
1706
+ // 3. Let tv be dateObject.[[DateValue]].
1707
+ const tv: number = __Porffor_date_read(_this);
1708
+
1709
+ // 4. Return ToDateString(tv).
1710
+ return __ecma262_ToDateString(tv);
1711
+ };
1712
+
1713
+ // 21.4.4.42 Date.prototype.toTimeString ()
1714
+ // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date.prototype.totimestring
1715
+ export const ___date_prototype_toTimeString = (_this: Date) => {
1716
+ // 1. Let dateObject be the this value.
1717
+ // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]).
1718
+ // 3. Let tv be dateObject.[[DateValue]].
1719
+ const tv: number = __Porffor_date_read(_this);
1720
+
1721
+ // 4. If tv is NaN, return "Invalid Date".
1722
+ let out: bytestring = '';
1723
+ out.length = 0;
1724
+
1725
+ if (Number.isNaN(tv)) {
1726
+ out = 'Invalid Date';
1727
+ return out;
1728
+ }
1729
+
1730
+ // 5. Let t be LocalTime(tv).
1731
+ const t: number = __ecma262_LocalTime(tv);
1732
+
1733
+ // 6. Return the string-concatenation of TimeString(t) and TimeZoneString(tv).
1734
+ __Porffor_bytestring_appendStr(out, __ecma262_TimeString(t));
1735
+ __Porffor_bytestring_appendStr(out, __ecma262_TimeZoneString(tv));
1736
+
1737
+ return out;
1738
+ };
1739
+
1740
+
1741
+ // 21.4.4.35 Date.prototype.toDateString ()
1742
+ // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date.prototype.todatestring
1743
+ export const ___date_prototype_toDateString = (_this: Date) => {
1744
+ // 1. Let dateObject be the this value.
1745
+ // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]).
1746
+ // 3. Let tv be dateObject.[[DateValue]].
1747
+ const tv: number = __Porffor_date_read(_this);
1748
+
1749
+ // 4. If tv is NaN, return "Invalid Date".
1750
+ let out: bytestring = '';
1751
+ out.length = 0;
1752
+
1753
+ if (Number.isNaN(tv)) {
1754
+ out = 'Invalid Date';
1755
+ return out;
1756
+ }
1757
+
1758
+ // 5. Let t be LocalTime(tv).
1759
+ const t: number = __ecma262_LocalTime(tv);
1760
+
1761
+ // 6. Return DateString(t).
1762
+ out = __ecma262_DateString(t);
1763
+ return out;
1764
+ };
1765
+
1766
+ // 21.4.4.43 Date.prototype.toUTCString ()
1767
+ // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date.prototype.toutcstring
1768
+ export const ___date_prototype_toUTCString = (_this: Date) => {
1769
+ // 1. Let dateObject be the this value.
1770
+ // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]).
1771
+ // 3. Let tv be dateObject.[[DateValue]].
1772
+ const tv: number = __Porffor_date_read(_this);
1773
+
1774
+ // 4. If tv is NaN, return "Invalid Date".
1775
+ let out: bytestring = '';
1776
+ out.length = 0;
1777
+
1778
+ if (Number.isNaN(tv)) {
1779
+ out = 'Invalid Date';
1780
+ return out;
1781
+ }
1782
+
1783
+ // 5. Let weekday be the Name of the entry in Table 62 with the Number WeekDay(tv).
1784
+ const weekday: bytestring = __ecma262_WeekDayName(tv);
1785
+
1786
+ // 6. Let month be the Name of the entry in Table 63 with the Number MonthFromTime(tv).
1787
+ const month: bytestring = __ecma262_MonthName(tv);
1788
+
1789
+ // 7. Let day be ToZeroPaddedDecimalString(ℝ(DateFromTime(tv)), 2).
1790
+ const day: number = __ecma262_DateFromTime(tv);
1791
+
1792
+ // 8. Let yv be YearFromTime(tv).
1793
+ const yv: number = __ecma262_YearFromTime(tv);
1794
+
1795
+ // 9. If yv is +0𝔽 or yv > +0𝔽, let yearSign be the empty String; otherwise, let yearSign be "-".
1796
+ // 10. Let paddedYear be ToZeroPaddedDecimalString(abs(ℝ(yv)), 4).
1797
+ // 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).
1798
+ // weekday
1799
+ __Porffor_bytestring_appendStr(out, weekday);
1800
+ __Porffor_bytestring_appendChar(out, 44); // ','
1801
+ __Porffor_bytestring_appendChar(out, 32); // ' '
1802
+
1803
+ // day
1804
+ __Porffor_bytestring_appendPadNum(out, day, 2);
1805
+ __Porffor_bytestring_appendChar(out, 32); // ' '
1806
+
1807
+ // month
1808
+ __Porffor_bytestring_appendStr(out, month);
1809
+ __Porffor_bytestring_appendChar(out, 32); // ' '
1810
+
1811
+ // year
1812
+ if (yv < 0) __Porffor_bytestring_appendChar(out, 45); // sign
1813
+ __Porffor_bytestring_appendPadNum(out, yv, 4);
1814
+
1815
+ __Porffor_bytestring_appendChar(out, 32); // ' '
1816
+ __Porffor_bytestring_appendStr(out, __ecma262_TimeString(tv));
1817
+
1818
+ return out;
1819
+ };
1820
+
1821
+ // 21.4.4.38 Date.prototype.toLocaleDateString ([ reserved1 [, reserved2 ]])
1822
+ // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date.prototype.tolocaledatestring
1823
+ export const ___date_prototype_toLocaleDateString = (_this: Date, reserved1: any, reserved2: any) => {
1824
+ return ___date_prototype_toDateString(_this);
1825
+ };
1826
+
1827
+ // 21.4.4.39 Date.prototype.toLocaleString ([ reserved1 [, reserved2 ]])
1828
+ // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date.prototype.tolocalestring
1829
+ export const ___date_prototype_toLocaleString = (_this: Date, reserved1: any, reserved2: any) => {
1830
+ return ___date_prototype_toString(_this);
1831
+ };
1832
+
1833
+ // 21.4.4.40 Date.prototype.toLocaleTimeString ([ reserved1 [, reserved2 ]])
1834
+ // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date.prototype.tolocaletimestring
1835
+ export const ___date_prototype_toLocaleTimeString = (_this: Date, reserved1: any, reserved2: any) => {
1836
+ return ___date_prototype_toTimeString(_this);
1837
+ };
1838
+
1839
+
1840
+ // 21.4.4.44 Date.prototype.valueOf ()
1841
+ export const ___date_prototype_valueOf = (_this: Date) => {
1842
+ // 1. Let dateObject be the this value.
1843
+ // 2. Perform ? RequireInternalSlot(dateObject, [[DateValue]]).
1844
+ // 3. Return dateObject.[[DateValue]].
1845
+ return __Porffor_date_read(_this);
1846
+ };
1847
+
1848
+
1849
+ // 21.4.2.1 Date (...values)
1850
+ // https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date
1851
+ export const Date = (): bytestring => {
1852
+ // 1. If NewTarget is undefined, then
1853
+ // a. Let now be the time value (UTC) identifying the current time.
1854
+ // b. Return ToDateString(now).
1855
+ return __ecma262_ToDateString(__Date_now());
1370
1856
  };
@@ -1,4 +1,4 @@
1
- // @porf -funsafe-no-unlikely-proto-checks -valtype=i32
1
+ // @porf --funsafe-no-unlikely-proto-checks --valtype=i32
2
2
 
3
3
  import type {} from './porffor';
4
4
 
@@ -1,4 +1,4 @@
1
- // @porf -funsafe-no-unlikely-proto-checks
1
+ // @porf --funsafe-no-unlikely-proto-checks
2
2
 
3
3
  // radix: number|any for rawType check
4
4
  // export const parseInt = (input: string|bytestring, radix: number|any): f64 => {
@@ -1,4 +1,4 @@
1
- // // @porf -funsafe-no-unlikely-proto-checks
1
+ // @porf --funsafe-no-unlikely-proto-checks
2
2
 
3
3
  // radix: number|any for rawType check
4
4
  export const __Number_prototype_toString = (_this: number, radix: number|any) => {