-
Notifications
You must be signed in to change notification settings - Fork 30.2k
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: refactor some internal/* code #12644
Changes from all commits
7008983
dc289b3
3d75f9f
ae8de10
0c9bd21
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,4 +22,4 @@ class FreeList { | |
} | ||
} | ||
|
||
module.exports = {FreeList}; | ||
module.exports = FreeList; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,5 @@ | ||
'use strict'; | ||
|
||
exports = module.exports = { | ||
makeRequireFunction, | ||
stripBOM, | ||
stripShebang, | ||
addBuiltinLibsToObject | ||
}; | ||
|
||
exports.requireDepth = 0; | ||
|
||
// Invoke with makeRequireFunction(module) where |module| is the Module object | ||
// to use as the context for the require() function. | ||
function makeRequireFunction(mod) { | ||
|
@@ -85,7 +76,7 @@ function stripShebang(content) { | |
return content; | ||
} | ||
|
||
exports.builtinLibs = [ | ||
const builtinLibs = [ | ||
'assert', 'buffer', 'child_process', 'cluster', 'crypto', 'dgram', 'dns', | ||
'domain', 'events', 'fs', 'http', 'https', 'net', 'os', 'path', 'punycode', | ||
'querystring', 'readline', 'repl', 'stream', 'string_decoder', 'tls', 'tty', | ||
|
@@ -94,7 +85,7 @@ exports.builtinLibs = [ | |
|
||
function addBuiltinLibsToObject(object) { | ||
// Make built-in modules available directly (loaded lazily). | ||
exports.builtinLibs.forEach((name) => { | ||
builtinLibs.forEach((name) => { | ||
// Goals of this mechanism are: | ||
// - Lazy loading of built-in modules | ||
// - Having all built-in modules available as non-enumerable properties | ||
|
@@ -130,3 +121,12 @@ function addBuiltinLibsToObject(object) { | |
}); | ||
}); | ||
} | ||
|
||
module.exports = exports = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's my preference but in a separate issue @sam-github expressed a preference for these to retain the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why? There is no point. Since you are already exposing the variable name to grab it this way If necessary, we can remove this in another PR entirely but idk why we'd add more here... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also agree that re-assigning There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm perfectly happy with removing it. |
||
addBuiltinLibsToObject, | ||
builtinLibs, | ||
makeRequireFunction, | ||
requireDepth: 0, | ||
stripBOM, | ||
stripShebang | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,71 +2,71 @@ | |
|
||
const Buffer = require('buffer').Buffer; | ||
|
||
module.exports = BufferList; | ||
module.exports = class BufferList { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you benchmark this change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, negligible difference in master with 5.7. Notable improvement under TF-I ... tho I'd have to pull the numbers back up. It was a week or so ago when I ran it. |
||
constructor() { | ||
this.head = null; | ||
this.tail = null; | ||
this.length = 0; | ||
} | ||
|
||
function BufferList() { | ||
this.head = null; | ||
this.tail = null; | ||
this.length = 0; | ||
} | ||
push(v) { | ||
const entry = { data: v, next: null }; | ||
if (this.length > 0) | ||
this.tail.next = entry; | ||
else | ||
this.head = entry; | ||
this.tail = entry; | ||
++this.length; | ||
} | ||
|
||
BufferList.prototype.push = function(v) { | ||
const entry = { data: v, next: null }; | ||
if (this.length > 0) | ||
this.tail.next = entry; | ||
else | ||
unshift(v) { | ||
const entry = { data: v, next: this.head }; | ||
if (this.length === 0) | ||
this.tail = entry; | ||
this.head = entry; | ||
this.tail = entry; | ||
++this.length; | ||
}; | ||
++this.length; | ||
} | ||
|
||
BufferList.prototype.unshift = function(v) { | ||
const entry = { data: v, next: this.head }; | ||
if (this.length === 0) | ||
this.tail = entry; | ||
this.head = entry; | ||
++this.length; | ||
}; | ||
shift() { | ||
if (this.length === 0) | ||
return; | ||
const ret = this.head.data; | ||
if (this.length === 1) | ||
this.head = this.tail = null; | ||
else | ||
this.head = this.head.next; | ||
--this.length; | ||
return ret; | ||
} | ||
|
||
BufferList.prototype.shift = function() { | ||
if (this.length === 0) | ||
return; | ||
const ret = this.head.data; | ||
if (this.length === 1) | ||
clear() { | ||
this.head = this.tail = null; | ||
else | ||
this.head = this.head.next; | ||
--this.length; | ||
return ret; | ||
}; | ||
|
||
BufferList.prototype.clear = function() { | ||
this.head = this.tail = null; | ||
this.length = 0; | ||
}; | ||
this.length = 0; | ||
} | ||
|
||
BufferList.prototype.join = function(s) { | ||
if (this.length === 0) | ||
return ''; | ||
var p = this.head; | ||
var ret = '' + p.data; | ||
while (p = p.next) | ||
ret += s + p.data; | ||
return ret; | ||
}; | ||
join(s) { | ||
if (this.length === 0) | ||
return ''; | ||
var p = this.head; | ||
var ret = '' + p.data; | ||
while (p = p.next) | ||
ret += s + p.data; | ||
return ret; | ||
} | ||
|
||
BufferList.prototype.concat = function(n) { | ||
if (this.length === 0) | ||
return Buffer.alloc(0); | ||
if (this.length === 1) | ||
return this.head.data; | ||
const ret = Buffer.allocUnsafe(n >>> 0); | ||
var p = this.head; | ||
var i = 0; | ||
while (p) { | ||
p.data.copy(ret, i); | ||
i += p.data.length; | ||
p = p.next; | ||
concat(n) { | ||
if (this.length === 0) | ||
return Buffer.alloc(0); | ||
if (this.length === 1) | ||
return this.head.data; | ||
const ret = Buffer.allocUnsafe(n >>> 0); | ||
var p = this.head; | ||
var i = 0; | ||
while (p) { | ||
p.data.copy(ret, i); | ||
i += p.data.length; | ||
p = p.next; | ||
} | ||
return ret; | ||
} | ||
return ret; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might want to check if the implicit coercion to a 32-bit value was intentional (e.g. performance related or the values were not originally numbers for some reason)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I explored that a bit a couldn't find anything specific. I could be wrong, but I think the switch was more defensive just in case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This also converts to an integer if it was a float, or some other type of number-ish.