porffor 0.16.0-f9dde1759 → 0.16.0-fa3914030

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.
@@ -0,0 +1,62 @@
1
+ // general widely used ecma262/spec functions
2
+
3
+ // 7.1.5 ToIntegerOrInfinity (argument)
4
+ // https://tc39.es/ecma262/#sec-tointegerorinfinity
5
+ export const __ecma262_ToIntegerOrInfinity = (argument: unknown): number => {
6
+ // 1. Let number be ? ToNumber(argument).
7
+ let number: number = Number(argument);
8
+
9
+ // 2. If number is one of NaN, +0𝔽, or -0𝔽, return 0.
10
+ if (Number.isNaN(number)) return 0;
11
+
12
+ // 3. If number is +∞𝔽, return +∞.
13
+ // 4. If number is -∞𝔽, return -∞.
14
+ if (!Number.isFinite(number)) return number;
15
+
16
+ // 5. Return truncate(ℝ(number)).
17
+ number = Math.trunc(number);
18
+
19
+ // return 0 for -0
20
+ if (number == 0) return 0;
21
+ return number;
22
+ };
23
+
24
+ // todo: support non-bytestring properly
25
+ // 7.1.17 ToString (argument)
26
+ // https://tc39.es/ecma262/#sec-tostring
27
+ export const __ecma262_ToString = (argument: unknown): bytestring => {
28
+ let out: bytestring = '';
29
+ const type: i32 = Porffor.rawType(argument);
30
+
31
+ // 1. If argument is a String, return argument.
32
+ if (Porffor.fastOr(
33
+ type == Porffor.TYPES.string,
34
+ type == Porffor.TYPES.bytestring)) return argument;
35
+
36
+ // 2. If argument is a Symbol, throw a TypeError exception.
37
+ if (type == Porffor.TYPES.symbol) throw new TypeError('Cannot convert a Symbol value to a string');
38
+
39
+ // 3. If argument is undefined, return "undefined".
40
+ if (type == Porffor.TYPES.undefined) return out = 'undefined';
41
+
42
+ // 4. If argument is null, return "null".
43
+ if (Porffor.fastAnd(
44
+ type == Porffor.TYPES.object,
45
+ argument == 0)) return out = 'null';
46
+
47
+ if (type == Porffor.TYPES.boolean) {
48
+ // 5. If argument is true, return "true".
49
+ if (argument == true) return out = 'true';
50
+
51
+ // 6. If argument is false, return "false".
52
+ return out = 'false';
53
+ }
54
+
55
+ // 7. If argument is a Number, return Number::toString(argument, 10).
56
+ // 8. If argument is a BigInt, return BigInt::toString(argument, 10).
57
+ // 9. Assert: argument is an Object.
58
+ // 10. Let primValue be ? ToPrimitive(argument, string).
59
+ // 11. Assert: primValue is not an Object.
60
+ // 12. Return ? ToString(primValue).
61
+ return argument.toString();
62
+ };