Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ Globals (`src/lib.ts`):
(`error`, `suppressed`)
- `Temporal`, the ISO 8601 calendar's part: `Duration`, `Instant`, `PlainDate`, `PlainTime`,
`PlainDateTime` and `Now` (`instant`, `plainDateTimeISO`, `plainDateISO`, `plainTimeISO`), with
`from` (ISO 8601 text), `compare`, `add`, `subtract`, `until`, `since`, `equals`, `toString` and
`toJSON`, and `_with` for `with`, a keyword here. Not here: `ZonedDateTime`, `PlainYearMonth`,
`from` (ISO 8601 text), `compare`, `add`, `subtract`, `until`, `since`, `equals`, `toString`,
`toJSON` and `with`. Not here: `ZonedDateTime`, `PlainYearMonth`,
`PlainMonthDay`, time zones other than UTC and the system's present offset, other calendars, and
the options bags: `until` and `since` give their default largest unit, and an overflowing day is
constrained to the month's last. Without a date to count from, a `Duration` with years, months or
Expand Down
126 changes: 32 additions & 94 deletions src/generics/lib.generics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -675,20 +675,10 @@ class Array<T> {
count = length - start;
}

// The removed elements are moved out, not read: they go to the end, the first of them
// last, and are popped off in order. Two reversals take them there - of everything from
// `start`, then of what stays - where each used to be swapped down one place at a time.
const end: int = length;
// the built-in splice gives back the removed elements, which the array no longer owns
const removed = this.data.splice(start, count);
const from: int = start;
const kept: int = end - count;
if (count > 0) {
__reverseRange(this.data, from, end);
__reverseRange(this.data, from, kept);
}

const removed: T[] = [];
for (let k = 0; k < count; k++)
removed.push(this.data.pop());
const kept: int = this.data.length;

// a few items go in one at a time, as in unshift
if (items.length < 8) {
Expand Down Expand Up @@ -861,7 +851,7 @@ class Array<T> {
}

@sharesElements
public _with(index: int, value: T) {
public with(index: int, value: T) {
return this.data._with(index, value);
}

Expand Down Expand Up @@ -1279,8 +1269,7 @@ class TypedArray<T> {
return this.slice().sort(callbackfn);
}

// `with`, which is a keyword here, as Array's is
public _with(index: int, value: T): TypedArray<T> {
public with(index: int, value: T): TypedArray<T> {
if (index < 0) {
index = index + this.count;
}
Expand Down Expand Up @@ -1316,76 +1305,36 @@ type Float32Array = TypedArray<f32>;
type Float64Array = TypedArray<f64>;

// IEEE 754 binary16, the element of Float16Array and of DataView's getFloat16 and setFloat16, and
// what Math.f16round rounds to. Converted here, bit by bit, not through an f16: LLVM lowers that to
// the __truncsfhf2 and __extendhfsf2 helpers, which the MSVC runtime does not have. Generic only so
// that a program that never uses them does not get them (see sleep).
// what Math.f16round rounds to: an f16, kept as its bits. Generic only so that a program that never
// uses them does not get them (see sleep).
namespace __Float16 {
function bitsOf<X = int>(x: number): u64 {
let value: f64 = x;
let bits: u64 = 0;
memcpy(<Opaque>Ref(bits), <Opaque>Ref(value), 8);
return bits;
}

// the nearest binary16, ties to even, as JavaScript rounds: too large is Infinity, too small 0
function encode<X = int>(x: number): u16 {
const b = bitsOf(x);
const sign = <u16>((b >> 48) & 0x8000);
const exponent = <int>((b >> 52) & 0x7FF);
const fraction: u64 = b & 0xFFFFFFFFFFFFF;
if (exponent == 0x7FF) {
return fraction != 0 ? <u16>0x7E00 : sign | 0x7C00;
}

const e = exponent - 1023 + 15;
if (e >= 31) {
return sign | 0x7C00;
}

if (e <= 0) {
// a subnormal, or 0: the 53 bits, the leading 1 among them, shifted down to 10
const shift = 43 - e;
if (shift > 53) {
return sign;
}

const full: u64 = fraction | 0x10000000000000;
let h = full >> shift;
const rest = full & ((<u64>1 << shift) - 1);
const half = <u64>1 << (shift - 1);
if (rest > half || (rest == half && (h & 1) != 0)) h++;
return sign | <u16>h;
}

// a carry out of the fraction goes into the exponent, which is right: up to Infinity
let h: u64 = (<u64>e << 10) | (fraction >> 42);
const rest = fraction & 0x3FFFFFFFFFF;
if (rest > 0x20000000000 || (rest == 0x20000000000 && (h & 1) != 0)) h++;
return sign | <u16>h;
let half: f16 = <f16>x;
let bits: u16 = 0;
memcpy(<Opaque>Ref(bits), <Opaque>Ref(half), 2);
return bits;
}

function decode<X = int>(bits: u16): number {
const e = (bits >> 10) & 0x1F;
const m = bits & 0x3FF;
let value: number;
if (e == 0) {
value = m * Math.pow(2, -24);
} else if (e == 31) {
value = m != 0 ? Number.NaN : Number.POSITIVE_INFINITY;
} else {
value = (1 + m / 1024) * Math.pow(2, e - 15);
}

// times -1, not -value: the compiler's unary minus is 0 - value, which makes -0 of 0
return (bits & 0x8000) != 0 ? value * -1 : value;
let given: u16 = bits;
let half: f16 = <f16>0;
memcpy(<Opaque>Ref(half), <Opaque>Ref(given), 2);
return <number>half;
}
}

// A ConvertingTypedArray's element on the way in and out: S, the kept element, says which kind it
// is (see ConvertingTypedArray). Functions here, not static methods: a generic class's static
// generic method cannot be called.
namespace __ConvertedElement {
function encode<E>(value: number): E {
// The two typed arrays whose element is not a C type: Uint8ClampedArray keeps a byte, and a number
// written to it is clamped into 0..255 and rounded half to even, where Uint8Array wraps it;
// Float16Array keeps the bits of a binary16 (see __Float16). Each element is a `number` to the
// program, as in JavaScript, and is converted on the way in and out. S, the kept element, tells the
// two apart: u8 is Uint8ClampedArray, u16 is Float16Array. Otherwise as TypedArray: a window that
// subarray() shares, a fixed length, and the same methods.
class ConvertingTypedArray<S> {
private elements: TypedArray<S>;

// an element on the way in and out: E, the kept element, says which kind it is (see above)
private static encode<E>(value: number): E {
if (sizeof<E>() == 1) {
// JavaScript's ToUint8Clamp
if (!(value > 0)) return <E>0;
Expand All @@ -1400,28 +1349,18 @@ namespace __ConvertedElement {
return <E>__Float16.encode(value);
}

function decode<E>(element: E): number {
private static decode<E>(element: E): number {
if (sizeof<E>() == 1) {
return element;
}

return __Float16.decode(<u16>element);
}
}

// The two typed arrays whose element is not a C type: Uint8ClampedArray keeps a byte, and a number
// written to it is clamped into 0..255 and rounded half to even, where Uint8Array wraps it;
// Float16Array keeps the bits of a binary16 (see __Float16). Each element is a `number` to the
// program, as in JavaScript, and is converted on the way in and out. S, the kept element, tells the
// two apart: u8 is Uint8ClampedArray, u16 is Float16Array. Otherwise as TypedArray: a window that
// subarray() shares, a fixed length, and the same methods, with `_with` for `with`.
class ConvertingTypedArray<S> {
private elements: TypedArray<S>;

constructor(data: number[] = []) {
this.elements = new TypedArray<S>(data.length);
for (let i = 0; i < data.length; i++) {
this.elements[i] = __ConvertedElement.encode<S>(data[i]);
this.elements[i] = ConvertingTypedArray.encode<S>(data[i]);
}
}

Expand All @@ -1439,11 +1378,11 @@ class ConvertingTypedArray<S> {

// outside the window a read gives 0 and a write does nothing, as TypedArray's
public get(index: int): number {
return __ConvertedElement.decode<S>(this.elements[index]);
return ConvertingTypedArray.decode<S>(this.elements[index]);
}

public set(index: int, value: number) {
this.elements[index] = __ConvertedElement.encode<S>(value);
this.elements[index] = ConvertingTypedArray.encode<S>(value);
}

public get length() {
Expand Down Expand Up @@ -1543,7 +1482,7 @@ class ConvertingTypedArray<S> {

// the value is converted once, as JavaScript does
public fill(value: number, start = 0, end = this.length): ConvertingTypedArray<S> {
this.elements.fill(__ConvertedElement.encode<S>(value), start, end);
this.elements.fill(ConvertingTypedArray.encode<S>(value), start, end);
return this;
}

Expand Down Expand Up @@ -1716,8 +1655,7 @@ class ConvertingTypedArray<S> {
return this.slice().sort(callbackfn);
}

// `with`, which is a keyword here
public _with(index: int, value: number): ConvertingTypedArray<S> {
public with(index: int, value: number): ConvertingTypedArray<S> {
if (index < 0) {
index = index + this.length;
}
Expand Down
6 changes: 3 additions & 3 deletions src/lib.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -967,7 +967,7 @@ declare namespace Temporal {
subtract(duration: Duration): PlainDate;
until(other: PlainDate): Duration;
since(other: PlainDate): Duration;
_with(fields: { year?: int, month?: int, day?: int }): PlainDate;
with(fields: { year?: int, month?: int, day?: int }): PlainDate;
equals(other: PlainDate): boolean;
toPlainDateTime(time?: PlainTime): PlainDateTime;
toString(): string;
Expand All @@ -994,7 +994,7 @@ declare namespace Temporal {
subtract(duration: Duration): PlainTime;
until(other: PlainTime): Duration;
since(other: PlainTime): Duration;
_with(fields: { hour?: int, minute?: int, second?: int, millisecond?: int, microsecond?: int, nanosecond?: int }): PlainTime;
with(fields: { hour?: int, minute?: int, second?: int, millisecond?: int, microsecond?: int, nanosecond?: int }): PlainTime;
equals(other: PlainTime): boolean;
toString(): string;
toJSON(): string;
Expand Down Expand Up @@ -1031,7 +1031,7 @@ declare namespace Temporal {
subtract(duration: Duration): PlainDateTime;
until(other: PlainDateTime): Duration;
since(other: PlainDateTime): Duration;
_with(fields: { year?: int, month?: int, day?: int, hour?: int, minute?: int, second?: int,
with(fields: { year?: int, month?: int, day?: int, hour?: int, minute?: int, second?: int,
millisecond?: int, microsecond?: int, nanosecond?: int }): PlainDateTime;
equals(other: PlainDateTime): boolean;
toPlainDate(): PlainDate;
Expand Down
12 changes: 6 additions & 6 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4272,8 +4272,8 @@ export namespace Temporal {
return new Duration(0, 0, 0, this.__epochDays() - other.__epochDays());
}

// `with`, which is a keyword here, as Array's is: a copy with the fields given replaced
public _with(fields: { year?: int, month?: int, day?: int }): PlainDate {
// a copy with the fields given replaced
public with(fields: { year?: int, month?: int, day?: int }): PlainDate {
const year = fields.year != undefined ? fields.year : this.isoYear;
const month = fields.month != undefined ? fields.month : this.isoMonth;
let day = fields.day != undefined ? fields.day : this.isoDay;
Expand Down Expand Up @@ -4366,8 +4366,8 @@ export namespace Temporal {
return spanOfTime(this).minus(spanOfTime(other)).toDuration(HOURS);
}

// `with`, a copy with the fields given replaced
public _with(fields: { hour?: int, minute?: int, second?: int, millisecond?: int, microsecond?: int, nanosecond?: int }): PlainTime {
// a copy with the fields given replaced
public with(fields: { hour?: int, minute?: int, second?: int, millisecond?: int, microsecond?: int, nanosecond?: int }): PlainTime {
return new PlainTime(
fields.hour != undefined ? fields.hour : this.parts[0],
fields.minute != undefined ? fields.minute : this.parts[1],
Expand Down Expand Up @@ -4470,8 +4470,8 @@ export namespace Temporal {
return this.span().minus(other.span()).toDuration(DAYS);
}

// `with`, a copy with the fields given replaced
public _with(fields: { year?: int, month?: int, day?: int, hour?: int, minute?: int, second?: int,
// a copy with the fields given replaced
public with(fields: { year?: int, month?: int, day?: int, hour?: int, minute?: int, second?: int,
millisecond?: int, microsecond?: int, nanosecond?: int }): PlainDateTime {
const year = fields.year != undefined ? fields.year : this.date.year;
const month = fields.month != undefined ? fields.month : this.date.month;
Expand Down
4 changes: 2 additions & 2 deletions tests/temporal_plain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function main() {
assert(date.since(from).toString() == "P19782D", "since");
assert(Temporal.PlainDate.compare(from, date) == -1 && date.equals(Temporal.PlainDate.from("2024-02-29")),
"compare and equals");
assert(date._with({ month: 4, day: 31 }).toString() == "2024-04-30", "with, constrained");
assert(date.with({ month: 4, day: 31 }).toString() == "2024-04-30", "with, constrained");
assert(new Temporal.PlainDate(-44, 3, 15).toString() == "-000044-03-15", "an expanded year");
assert(Temporal.PlainDate.from("1969-12-31").dayOfWeek == 3, "a Wednesday before 1970");

Expand All @@ -41,7 +41,7 @@ function main() {
assert(time.add(Temporal.Duration.from("PT11H")).toString() == "00:45:30.25", "around midnight");
assert(time.subtract(Temporal.Duration.from("PT14H")).toString() == "23:45:30.25", "back around midnight");
assert(new Temporal.PlainTime(8).until(time).toString() == "PT5H45M30.25S", "until, in hours and smaller");
assert(time._with({ second: 0, millisecond: 0 }).toString() == "13:45:00", "with");
assert(time.with({ second: 0, millisecond: 0 }).toString() == "13:45:00", "with");

const dt = Temporal.PlainDateTime.from("2024-12-31T23:30");
assert(dt.year == 2024 && dt.hour == 23 && dt.minute == 30, "date and time fields");
Expand Down
2 changes: 1 addition & 1 deletion tests/typed_array_clamped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function main() {
assert(filled.join() == "255,255,255", "fill clamps");
const doubled = given.map((v: number, i: number) => v * 2);
assert(doubled.join() == "0,255,255", "map clamps");
const changed = given._with(0, 77.5);
const changed = given.with(0, 77.5);
assert(changed[0] == 78 && given[0] == 0, "with clamps, and copies");

// subarray shares the elements, slice copies them
Expand Down
2 changes: 1 addition & 1 deletion tests/typed_array_methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function main() {
assert(ints.filter((v) => v % 2 == 0).join() == "2,4", "filter");
assert(ints.toReversed().join() == "4,3,2,1" && ints.join() == "1,2,3,4", "toReversed leaves it");
assert(new Int32Array([3, 1, 2]).toSorted().join() == "1,2,3", "toSorted");
assert(ints._with(-1, 9).join() == "1,2,3,9" && ints[3] == 4, "with");
assert(ints.with(-1, 9).join() == "1,2,3,9" && ints[3] == 4, "with");

// in place, each giving the array itself
const g = new Int16Array([1, 2, 3, 4, 5]);
Expand Down
Loading