mirror of
https://github.com/oven-sh/bun
synced 2026-02-15 13:22:07 +00:00
test(js/internal): add unit tests for Dequeue (#16189)
This commit is contained in:
20
src/js/builtins.d.ts
vendored
20
src/js/builtins.d.ts
vendored
@@ -161,8 +161,26 @@ declare function $toPropertyKey(x: any): PropertyKey;
|
||||
* `$toObject(this, "Class.prototype.method requires that |this| not be null or undefined");`
|
||||
*/
|
||||
declare function $toObject(object: any, errorMessage?: string): object;
|
||||
/**
|
||||
* ## References
|
||||
* - [WebKit - `emit_intrinsic_newArrayWithSize`](https://github.com/oven-sh/WebKit/blob/e1a802a2287edfe7f4046a9dd8307c8b59f5d816/Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp#L2317)
|
||||
*/
|
||||
declare function $newArrayWithSize<T>(size: number): T[];
|
||||
declare function $newArrayWithSpecies(): TODO;
|
||||
/**
|
||||
* Optimized path for creating a new array storing objects with the same homogenous Structure
|
||||
* as {@link array}.
|
||||
*
|
||||
* @param size the initial size of the new array
|
||||
* @param array the array whose shape we want to copy
|
||||
*
|
||||
* @returns a new array
|
||||
*
|
||||
* ## References
|
||||
* - [WebKit - `emit_intrinsic_newArrayWithSpecies`](https://github.com/oven-sh/WebKit/blob/e1a802a2287edfe7f4046a9dd8307c8b59f5d816/Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp#L2328)
|
||||
* - [WebKit - #4909](https://github.com/WebKit/WebKit/pull/4909)
|
||||
* - [WebKit Bugzilla - Related Issue/Ticket](https://bugs.webkit.org/show_bug.cgi?id=245797)
|
||||
*/
|
||||
declare function $newArrayWithSpecies<T>(size: number, array: T[]): T[];
|
||||
declare function $newPromise(): TODO;
|
||||
declare function $createPromise(): TODO;
|
||||
declare const $iterationKindKey: TODO;
|
||||
|
||||
@@ -89,8 +89,8 @@ export function validateAndNormalizeQueuingStrategy(size, highWaterMark) {
|
||||
|
||||
$linkTimeConstant;
|
||||
export function createFIFO() {
|
||||
const Denqueue = require("internal/fifo");
|
||||
return new Denqueue();
|
||||
const Dequeue = require("internal/fifo");
|
||||
return new Dequeue();
|
||||
}
|
||||
|
||||
export function newQueue() {
|
||||
|
||||
@@ -151,3 +151,4 @@ export const bindgen = $zig("bindgen_test.zig", "getBindgenTestFunctions") as {
|
||||
};
|
||||
|
||||
export const noOpForTesting = $cpp("NoOpForTesting.cpp", "createNoOpForTesting");
|
||||
export const Dequeue = require("internal/fifo");
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
var slice = Array.prototype.slice;
|
||||
class Denqueue {
|
||||
class Dequeue<T> {
|
||||
_head: number;
|
||||
_tail: number;
|
||||
_capacityMask: number;
|
||||
_list: (T | undefined)[];
|
||||
|
||||
constructor() {
|
||||
this._head = 0;
|
||||
this._tail = 0;
|
||||
@@ -8,26 +13,21 @@ class Denqueue {
|
||||
this._list = $newArrayWithSize(4);
|
||||
}
|
||||
|
||||
_head;
|
||||
_tail;
|
||||
_capacityMask;
|
||||
_list;
|
||||
|
||||
size() {
|
||||
size(): number {
|
||||
if (this._head === this._tail) return 0;
|
||||
if (this._head < this._tail) return this._tail - this._head;
|
||||
else return this._capacityMask + 1 - (this._head - this._tail);
|
||||
}
|
||||
|
||||
isEmpty() {
|
||||
isEmpty(): boolean {
|
||||
return this.size() == 0;
|
||||
}
|
||||
|
||||
isNotEmpty() {
|
||||
isNotEmpty(): boolean {
|
||||
return this.size() > 0;
|
||||
}
|
||||
|
||||
shift() {
|
||||
shift(): T | undefined {
|
||||
var { _head: head, _tail, _list, _capacityMask } = this;
|
||||
if (head === _tail) return undefined;
|
||||
var item = _list[head];
|
||||
@@ -37,24 +37,21 @@ class Denqueue {
|
||||
return item;
|
||||
}
|
||||
|
||||
peek() {
|
||||
peek(): T | undefined {
|
||||
if (this._head === this._tail) return undefined;
|
||||
return this._list[this._head];
|
||||
}
|
||||
|
||||
push(item) {
|
||||
push(item: T): void {
|
||||
var tail = this._tail;
|
||||
$putByValDirect(this._list, tail, item);
|
||||
this._tail = (tail + 1) & this._capacityMask;
|
||||
if (this._tail === this._head) {
|
||||
this._growArray();
|
||||
}
|
||||
// if (this._capacity && this.size() > this._capacity) {
|
||||
// this.shift();
|
||||
// }
|
||||
}
|
||||
|
||||
toArray(fullCopy) {
|
||||
toArray(fullCopy: boolean): T[] {
|
||||
var list = this._list;
|
||||
var len = $toLength(list.length);
|
||||
|
||||
@@ -66,19 +63,19 @@ class Denqueue {
|
||||
var j = 0;
|
||||
for (var i = _head; i < len; i++) $putByValDirect(array, j++, list[i]);
|
||||
for (var i = 0; i < _tail; i++) $putByValDirect(array, j++, list[i]);
|
||||
return array;
|
||||
return array as T[];
|
||||
} else {
|
||||
return slice.$call(list, this._head, this._tail);
|
||||
}
|
||||
}
|
||||
|
||||
clear() {
|
||||
clear(): void {
|
||||
this._head = 0;
|
||||
this._tail = 0;
|
||||
this._list.fill(undefined);
|
||||
}
|
||||
|
||||
_growArray() {
|
||||
private _growArray(): void {
|
||||
if (this._head) {
|
||||
// copy existing data, head to end, then beginning to tail.
|
||||
this._list = this.toArray(true);
|
||||
@@ -92,10 +89,10 @@ class Denqueue {
|
||||
this._capacityMask = (this._capacityMask << 1) | 1;
|
||||
}
|
||||
|
||||
_shrinkArray() {
|
||||
private _shrinkArray(): void {
|
||||
this._list.length >>>= 1;
|
||||
this._capacityMask >>>= 1;
|
||||
}
|
||||
}
|
||||
|
||||
export default Denqueue;
|
||||
export default Dequeue;
|
||||
|
||||
Reference in New Issue
Block a user