Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: disbanden circular dependency #14885

Closed
wants to merge 3 commits into from
Closed
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
20 changes: 9 additions & 11 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
'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 errors = require('internal/errors');

Expand Down Expand Up @@ -115,10 +114,9 @@ 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 { from } = require('buffer').Buffer;
const len = a.byteLength;
if (len !== b.byteLength) {
return false;
Expand All @@ -131,8 +129,8 @@ function areSimilarTypedArrays(a, b) {
}
return true;
}
return compare(from(a.buffer, a.byteOffset, len),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just make this Buffer.from() to make it clearer what is happening

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am no sure I can follow. The Buffer.from part was removed intentionally. Should I add a comment to describe the Uint8Array part?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO a comment explaining just that would be great.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I edited the comment above the function. I am not really sure what to write besides what I now changed.

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 @@ -186,11 +184,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 @@ -219,10 +217,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);
}
const actualTag = objectToString(actual);
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 @@ -1131,6 +1132,7 @@ module.exports = exports = {
inspect,
isArray: Array.isArray,
isBoolean,
isBuffer,
isNull,
isNullOrUndefined,
isNumber,
Expand Down Expand Up @@ -1162,18 +1164,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;
}
});