Skip to content

Commit

Permalink
lib: remove circular reference
Browse files Browse the repository at this point in the history
PR-URL: #14885
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
BridgeAR authored and MylesBorins committed Sep 12, 2017
1 parent 391855c commit d5a376a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 30 deletions.
24 changes: 9 additions & 15 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@
'use strict';

const { compare } = process.binding('buffer');
const util = require('util');
const { isSet, isMap } = process.binding('util');
const { isSet, isMap, isDate, isRegExp } = process.binding('util');
const { objectToString } = require('internal/util');
const { Buffer } = require('buffer');
const errors = require('internal/errors');

// The assert module provides functions that throw
Expand Down Expand Up @@ -108,8 +106,8 @@ function areSimilarRegExps(a, b) {
}

// For small buffers it's faster to compare the buffer in a loop. The c++
// barrier including the Buffer.from operation takes the advantage of the faster
// compare otherwise. 300 was the number after which compare became faster.
// barrier including the Uint8Array operation takes the advantage of the faster
// binary compare otherwise. The break even point was at about 300 characters.
function areSimilarTypedArrays(a, b) {
const len = a.byteLength;
if (len !== b.byteLength) {
Expand All @@ -123,12 +121,8 @@ function areSimilarTypedArrays(a, b) {
}
return true;
}
return compare(Buffer.from(a.buffer,
a.byteOffset,
len),
Buffer.from(b.buffer,
b.byteOffset,
b.byteLength)) === 0;
return compare(new Uint8Array(a.buffer, a.byteOffset, len),
new Uint8Array(b.buffer, b.byteOffset, b.byteLength)) === 0;
}

function isFloatTypedArrayTag(tag) {
Expand Down Expand Up @@ -189,11 +183,11 @@ function strictDeepEqual(actual, expected) {
// Skip testing the part below and continue in the callee function.
return;
}
if (util.isDate(actual)) {
if (isDate(actual)) {
if (actual.getTime() !== expected.getTime()) {
return false;
}
} else if (util.isRegExp(actual)) {
} else if (isRegExp(actual)) {
if (!areSimilarRegExps(actual, expected)) {
return false;
}
Expand Down Expand Up @@ -229,10 +223,10 @@ function looseDeepEqual(actual, expected) {
if (expected === null || typeof expected !== 'object') {
return false;
}
if (util.isDate(actual) && util.isDate(expected)) {
if (isDate(actual) && isDate(expected)) {
return actual.getTime() === expected.getTime();
}
if (util.isRegExp(actual) && util.isRegExp(expected)) {
if (isRegExp(actual) && isRegExp(expected)) {
return areSimilarRegExps(actual, expected);
}
if (actual instanceof Error && expected instanceof Error) {
Expand Down
17 changes: 2 additions & 15 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

const errors = require('internal/errors');
const { TextDecoder, TextEncoder } = require('internal/encoding');
const { isBuffer } = require('buffer').Buffer;

const { errname } = process.binding('uv');

Expand Down Expand Up @@ -1118,6 +1119,7 @@ module.exports = exports = {
inspect,
isArray: Array.isArray,
isBoolean,
isBuffer,
isNull,
isNullOrUndefined,
isNumber,
Expand Down Expand Up @@ -1149,18 +1151,3 @@ module.exports = exports = {
'util.puts is deprecated. Use console.log instead.',
'DEP0027')
};

// Avoid a circular dependency
var isBuffer;
Object.defineProperty(exports, 'isBuffer', {
configurable: true,
enumerable: true,
get() {
if (!isBuffer)
isBuffer = require('buffer').Buffer.isBuffer;
return isBuffer;
},
set(val) {
isBuffer = val;
}
});

0 comments on commit d5a376a

Please sign in to comment.