mirror of https://github.com/lukechilds/node.git
Browse Source
Internal modules can be used to share private code between public modules without risk to expose private APIs to the user. PR-URL: https://github.com/iojs/io.js/pull/848 Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>v1.8.0-commit
Vladimir Kurchatkin
10 years ago
12 changed files with 91 additions and 35 deletions
@ -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'); |
|||
|
@ -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; |
|||
}; |
@ -0,0 +1 @@ |
|||
module.exports = require('internal/freelist'); |
@ -0,0 +1 @@ |
|||
module.exports = 42; |
@ -0,0 +1,6 @@ |
|||
// Flags: --expose_internals
|
|||
|
|||
var common = require('../common'); |
|||
var assert = require('assert'); |
|||
|
|||
assert.equal(typeof require('internal/freelist').FreeList, 'function'); |
@ -0,0 +1,8 @@ |
|||
var common = require('../common'); |
|||
var assert = require('assert'); |
|||
|
|||
assert.throws(function() { |
|||
require('internal/freelist'); |
|||
}); |
|||
|
|||
assert(require('../fixtures/internal-modules') === 42); |
Loading…
Reference in new issue