-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Internal modules can be used to share private code between public modules without risk to expose private APIs to the user. PR-URL: #848 Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
- Loading branch information
1 parent
4581421
commit 2db758c
Showing
12 changed files
with
91 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,3 @@ | ||
'use strict'; | ||
|
||
// This is a free list to avoid creating so many of the same object. | ||
exports.FreeList = function(name, max, constructor) { | ||
this.name = name; | ||
this.constructor = constructor; | ||
this.max = max; | ||
this.list = []; | ||
}; | ||
|
||
|
||
exports.FreeList.prototype.alloc = function() { | ||
//debug("alloc " + this.name + " " + this.list.length); | ||
return this.list.length ? this.list.shift() : | ||
this.constructor.apply(this, arguments); | ||
}; | ||
|
||
|
||
exports.FreeList.prototype.free = function(obj) { | ||
//debug("free " + this.name + " " + this.list.length); | ||
if (this.list.length < this.max) { | ||
this.list.push(obj); | ||
return true; | ||
} | ||
return false; | ||
}; | ||
module.exports = require('internal/freelist'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strict'; | ||
|
||
// This is a free list to avoid creating so many of the same object. | ||
exports.FreeList = function(name, max, constructor) { | ||
this.name = name; | ||
this.constructor = constructor; | ||
this.max = max; | ||
this.list = []; | ||
}; | ||
|
||
|
||
exports.FreeList.prototype.alloc = function() { | ||
return this.list.length ? this.list.shift() : | ||
this.constructor.apply(this, arguments); | ||
}; | ||
|
||
|
||
exports.FreeList.prototype.free = function(obj) { | ||
if (this.list.length < this.max) { | ||
this.list.push(obj); | ||
return true; | ||
} | ||
return false; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,6 +69,8 @@ | |
'lib/v8.js', | ||
'lib/vm.js', | ||
'lib/zlib.js', | ||
|
||
'lib/internal/freelist.js', | ||
], | ||
}, | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('internal/freelist'); |
1 change: 1 addition & 0 deletions
1
test/fixtures/internal-modules/node_modules/internal/freelist.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Flags: --expose_internals | ||
|
||
var common = require('../common'); | ||
var assert = require('assert'); | ||
|
||
assert.equal(typeof require('internal/freelist').FreeList, 'function'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
var common = require('../common'); | ||
var assert = require('assert'); | ||
|
||
assert.throws(function() { | ||
require('internal/freelist'); | ||
}); | ||
|
||
assert(require('../fixtures/internal-modules') === 42); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters