refactor: switch api and gateway to V8 (#4879)

Co-authored-by: Jan <66554238+Vaporox@users.noreply.github.com>
This commit is contained in:
Sugden
2021-02-11 17:10:35 +00:00
committed by GitHub
parent ae3c3d80ee
commit ee5bc1a5c4
33 changed files with 372 additions and 364 deletions

View File

@@ -7,12 +7,12 @@ const { RangeError } = require('../errors');
*/
class BitField {
/**
* @param {BitFieldResolvable} [bits=0] Bit(s) to read from
* @param {BitFieldResolvable} [bits=this.constructor.defaultBit] Bit(s) to read from
*/
constructor(bits) {
constructor(bits = this.constructor.defaultBit) {
/**
* Bitfield of the packed bits
* @type {number}
* @type {number|bigint}
*/
this.bitfield = this.constructor.resolve(bits);
}
@@ -23,7 +23,7 @@ class BitField {
* @returns {boolean}
*/
any(bit) {
return (this.bitfield & this.constructor.resolve(bit)) !== 0;
return (this.bitfield & this.constructor.resolve(bit)) !== this.constructor.defaultBit;
}
/**
@@ -71,7 +71,7 @@ class BitField {
* @returns {BitField} These bits or new BitField if the instance is frozen.
*/
add(...bits) {
let total = 0;
let total = this.constructor.defaultBit;
for (const bit of bits) {
total |= this.constructor.resolve(bit);
}
@@ -86,7 +86,7 @@ class BitField {
* @returns {BitField} These bits or new BitField if the instance is frozen.
*/
remove(...bits) {
let total = 0;
let total = this.constructor.defaultBit;
for (const bit of bits) {
total |= this.constructor.resolve(bit);
}
@@ -117,7 +117,7 @@ class BitField {
}
toJSON() {
return this.bitfield;
return typeof this.bitfield === 'number' ? this.bitfield : this.bitfield.toString();
}
valueOf() {
@@ -133,18 +133,20 @@ class BitField {
* * A bit number (this can be a number literal or a value taken from {@link BitField.FLAGS})
* * An instance of BitField
* * An Array of BitFieldResolvable
* @typedef {number|BitField|BitFieldResolvable[]} BitFieldResolvable
* @typedef {number|bigint|BitField|BitFieldResolvable[]} BitFieldResolvable
*/
/**
* Resolves bitfields to their numeric form.
* @param {BitFieldResolvable} [bit=0] - bit(s) to resolve
* @returns {number}
* @param {BitFieldResolvable} [bit] - bit(s) to resolve
* @returns {number|bigint}
*/
static resolve(bit = 0) {
if (typeof bit === 'number' && bit >= 0) return bit;
static resolve(bit) {
const { defaultBit } = this;
if (typeof bit === 'undefined') return defaultBit;
if (typeof defaultBit === typeof bit && bit >= defaultBit) return bit;
if (bit instanceof BitField) return bit.bitfield;
if (Array.isArray(bit)) return bit.map(p => this.resolve(p)).reduce((prev, p) => prev | p, 0);
if (Array.isArray(bit)) return bit.map(p => this.resolve(p)).reduce((prev, p) => prev | p, defaultBit);
if (typeof bit === 'string' && typeof this.FLAGS[bit] !== 'undefined') return this.FLAGS[bit];
throw new RangeError('BITFIELD_INVALID', bit);
}
@@ -158,4 +160,10 @@ class BitField {
*/
BitField.FLAGS = {};
/**
* @type {number|bigint}
* @private
*/
BitField.defaultBit = 0;
module.exports = BitField;