Skip to content

Commit 2db758c

Browse files
committed
iojs: introduce internal modules
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>
1 parent 4581421 commit 2db758c

File tree

12 files changed

+91
-35
lines changed

12 files changed

+91
-35
lines changed

lib/_http_common.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const FreeList = require('freelist').FreeList;
3+
const FreeList = require('internal/freelist').FreeList;
44
const HTTPParser = process.binding('http_parser').HTTPParser;
55

66
const incoming = require('_http_incoming');

lib/freelist.js

+1-24
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,3 @@
11
'use strict';
22

3-
// This is a free list to avoid creating so many of the same object.
4-
exports.FreeList = function(name, max, constructor) {
5-
this.name = name;
6-
this.constructor = constructor;
7-
this.max = max;
8-
this.list = [];
9-
};
10-
11-
12-
exports.FreeList.prototype.alloc = function() {
13-
//debug("alloc " + this.name + " " + this.list.length);
14-
return this.list.length ? this.list.shift() :
15-
this.constructor.apply(this, arguments);
16-
};
17-
18-
19-
exports.FreeList.prototype.free = function(obj) {
20-
//debug("free " + this.name + " " + this.list.length);
21-
if (this.list.length < this.max) {
22-
this.list.push(obj);
23-
return true;
24-
}
25-
return false;
26-
};
3+
module.exports = require('internal/freelist');

lib/internal/freelist.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
// This is a free list to avoid creating so many of the same object.
4+
exports.FreeList = function(name, max, constructor) {
5+
this.name = name;
6+
this.constructor = constructor;
7+
this.max = max;
8+
this.list = [];
9+
};
10+
11+
12+
exports.FreeList.prototype.alloc = function() {
13+
return this.list.length ? this.list.shift() :
14+
this.constructor.apply(this, arguments);
15+
};
16+
17+
18+
exports.FreeList.prototype.free = function(obj) {
19+
if (this.list.length < this.max) {
20+
this.list.push(obj);
21+
return true;
22+
}
23+
return false;
24+
};

lib/module.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ Module._nodeModulePaths = function(from) {
200200

201201

202202
Module._resolveLookupPaths = function(request, parent) {
203-
if (NativeModule.exists(request)) {
203+
if (NativeModule.nonInternalExists(request)) {
204204
return [request, []];
205205
}
206206

@@ -262,7 +262,7 @@ Module._load = function(request, parent, isMain) {
262262
return cachedModule.exports;
263263
}
264264

265-
if (NativeModule.exists(filename)) {
265+
if (NativeModule.nonInternalExists(filename)) {
266266
// REPL is a special case, because it needs the real require.
267267
if (filename == 'repl') {
268268
var replModule = new Module('repl');
@@ -299,7 +299,7 @@ Module._load = function(request, parent, isMain) {
299299
};
300300

301301
Module._resolveFilename = function(request, parent) {
302-
if (NativeModule.exists(request)) {
302+
if (NativeModule.nonInternalExists(request)) {
303303
return request;
304304
}
305305

node.gyp

+2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
'lib/v8.js',
7070
'lib/vm.js',
7171
'lib/zlib.js',
72+
73+
'lib/internal/freelist.js',
7274
],
7375
},
7476

src/node.cc

+3
Original file line numberDiff line numberDiff line change
@@ -3133,6 +3133,9 @@ static void ParseArgs(int* argc,
31333133
} else if (strncmp(arg, "--icu-data-dir=", 15) == 0) {
31343134
icu_data_dir = arg + 15;
31353135
#endif
3136+
} else if (strcmp(arg, "--expose-internals") == 0 ||
3137+
strcmp(arg, "--expose_internals") == 0) {
3138+
// consumed in js
31363139
} else {
31373140
// V8 option. Pass through as-is.
31383141
new_v8_argv[new_v8_argc] = arg;

src/node.js

+21
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,27 @@
838838
return NativeModule._source.hasOwnProperty(id);
839839
};
840840

841+
const EXPOSE_INTERNALS = process.execArgv.some(function(arg) {
842+
return arg.match(/^--expose[-_]internals$/);
843+
});
844+
845+
if (EXPOSE_INTERNALS) {
846+
NativeModule.nonInternalExists = NativeModule.exists;
847+
848+
NativeModule.isInternal = function(id) {
849+
return false;
850+
};
851+
} else {
852+
NativeModule.nonInternalExists = function(id) {
853+
return NativeModule.exists(id) && !NativeModule.isInternal(id);
854+
};
855+
856+
NativeModule.isInternal = function(id) {
857+
return id.startsWith('internal/');
858+
};
859+
}
860+
861+
841862
NativeModule.getSource = function(id) {
842863
return NativeModule._source[id];
843864
};
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('internal/freelist');

test/fixtures/internal-modules/node_modules/internal/freelist.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Flags: --expose_internals
2+
3+
var common = require('../common');
4+
var assert = require('assert');
5+
6+
assert.equal(typeof require('internal/freelist').FreeList, 'function');
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var common = require('../common');
2+
var assert = require('assert');
3+
4+
assert.throws(function() {
5+
require('internal/freelist');
6+
});
7+
8+
assert(require('../fixtures/internal-modules') === 42);

tools/js2c.py

+20-7
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,11 @@ def ReadMacros(lines):
238238

239239

240240
NATIVE_DECLARATION = """\
241-
{ "%(id)s", %(id)s_native, sizeof(%(id)s_native)-1 },
241+
{ "%(id)s", %(escaped_id)s_native, sizeof(%(escaped_id)s_native)-1 },
242242
"""
243243

244244
SOURCE_DECLARATION = """\
245-
const char %(id)s_native[] = { %(data)s };
245+
const char %(escaped_id)s_native[] = { %(data)s };
246246
"""
247247

248248

@@ -293,16 +293,29 @@ def JS2C(source, target):
293293
lines = ExpandMacros(lines, macros)
294294
lines = CompressScript(lines, do_jsmin)
295295
data = ToCArray(s, lines)
296-
id = os.path.basename(str(s)).split('.')[0]
296+
id = '/'.join(re.split('/|\\\\', s)[1:]).split('.')[0]
297297
if delay: id = id[:-6]
298298
if delay:
299299
delay_ids.append((id, len(lines)))
300300
else:
301301
ids.append((id, len(lines)))
302-
source_lines.append(SOURCE_DECLARATION % { 'id': id, 'data': data })
303-
source_lines_empty.append(SOURCE_DECLARATION % { 'id': id, 'data': 0 })
304-
native_lines.append(NATIVE_DECLARATION % { 'id': id })
305-
302+
303+
escaped_id = id.replace('/', '$')
304+
source_lines.append(SOURCE_DECLARATION % {
305+
'id': id,
306+
'escaped_id': escaped_id,
307+
'data': data
308+
})
309+
source_lines_empty.append(SOURCE_DECLARATION % {
310+
'id': id,
311+
'escaped_id': escaped_id,
312+
'data': 0
313+
})
314+
native_lines.append(NATIVE_DECLARATION % {
315+
'id': id,
316+
'escaped_id': escaped_id
317+
})
318+
306319
# Build delay support functions
307320
get_index_cases = [ ]
308321
get_script_source_cases = [ ]

0 commit comments

Comments
 (0)