Skip to content

Commit db595b2

Browse files
committed
lib: reintroduce v8 module
I introduced this module over a year ago in a pull request as the v8 module but it was quickly subsumed by the tracing module. The tracing module was recently removed again and that is why this commit introduces the v8 module again, including the new features it picked up commits d23ac0e and f8076c4. PR-URL: #131 Reviewed-By: Chris Dickinson <[email protected]> Reviewed-By: Christian Tellnes <[email protected]> Reviewed-By: Thorsten Lorenz <[email protected]> Reviewed-By: Trevor Norris <[email protected]>
1 parent 52fc406 commit db595b2

File tree

9 files changed

+190
-1
lines changed

9 files changed

+190
-1
lines changed

doc/api/_toc.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@
3434
* [UDP/Datagram](dgram.html)
3535
* [URL](url.html)
3636
* [Utilities](util.html)
37+
* [V8](v8.html)
3738
* [VM](vm.html)
3839
* [ZLIB](zlib.html)

doc/api/all.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@
3535
@include debugger
3636
@include cluster
3737
@include smalloc
38+
@include v8

doc/api/v8.markdown

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# V8
2+
3+
Stability: 1 - Experimental
4+
5+
This module exposes events and interfaces specific to the version of [V8][]
6+
built with node. These interfaces are subject to change by upstream and are
7+
therefore not covered under the stability index.
8+
9+
### getHeapStatistics()
10+
11+
Returns an object with the following properties
12+
13+
```
14+
{
15+
total_heap_size: 7326976,
16+
total_heap_size_executable: 4194304,
17+
total_physical_size: 7326976,
18+
used_heap_size: 3476208,
19+
heap_size_limit: 1535115264
20+
}
21+
```
22+
23+
### setFlagsFromString()
24+
25+
Set additional V8 command line flags. Use with care; changing settings
26+
after the VM has started may result in unpredictable behavior, including
27+
crashes and data loss. Or it may simply do nothing.
28+
29+
The V8 options available for a version of node may be determined by running
30+
`iojs --v8-options`. An unofficial, community-maintained list of options
31+
and their effects is available
32+
[here](https://github.com/thlorenz/v8-flags/blob/master/flags-0.11.md).
33+
34+
Usage:
35+
36+
```
37+
// Print GC events to stdout for one minute.
38+
var v8 = require('v8');
39+
v8.setFlagsFromString('--trace_gc');
40+
setTimeout(function() { v8.setFlagsFromString('--notrace_gc'); }, 60e3);
41+
```
42+
43+
[V8]: https://code.google.com/p/v8/

lib/repl.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ exports.writer = util.inspect;
7474
exports._builtinLibs = ['assert', 'buffer', 'child_process', 'cluster',
7575
'crypto', 'dgram', 'dns', 'domain', 'events', 'fs', 'http', 'https', 'net',
7676
'os', 'path', 'punycode', 'querystring', 'readline', 'stream',
77-
'string_decoder', 'tls', 'tty', 'url', 'util', 'vm', 'zlib', 'smalloc'];
77+
'string_decoder', 'tls', 'tty', 'url', 'util', 'v8', 'vm', 'zlib',
78+
'smalloc'];
7879

7980

8081
function REPLServer(prompt, stream, eval_, useGlobal, ignoreUndefined) {

lib/v8.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) 2014, StrongLoop Inc.
2+
//
3+
// Permission to use, copy, modify, and/or distribute this software for any
4+
// purpose with or without fee is hereby granted, provided that the above
5+
// copyright notice and this permission notice appear in all copies.
6+
//
7+
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8+
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9+
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10+
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11+
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12+
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13+
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14+
15+
'use strict';
16+
17+
var EventEmitter = require('events');
18+
var v8binding = process.binding('v8');
19+
20+
var v8 = module.exports = new EventEmitter();
21+
v8.getHeapStatistics = v8binding.getHeapStatistics;
22+
v8.setFlagsFromString = v8binding.setFlagsFromString;
23+
24+
25+
function emitGC(before, after) {
26+
v8.emit('gc', before, after);
27+
}
28+
29+
30+
v8.on('newListener', function(name) {
31+
if (name === 'gc' && EventEmitter.listenerCount(this, name) === 0) {
32+
v8binding.startGarbageCollectionTracking(emitGC);
33+
}
34+
});
35+
36+
37+
v8.on('removeListener', function(name) {
38+
if (name === 'gc' && EventEmitter.listenerCount(this, name) === 0) {
39+
v8binding.stopGarbageCollectionTracking();
40+
}
41+
});

node.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
'lib/tty.js',
6565
'lib/url.js',
6666
'lib/util.js',
67+
'lib/v8.js',
6768
'lib/vm.js',
6869
'lib/zlib.js',
6970
],

test/simple/test-v8-flags.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) 2014, StrongLoop Inc.
2+
//
3+
// Permission to use, copy, modify, and/or distribute this software for any
4+
// purpose with or without fee is hereby granted, provided that the above
5+
// copyright notice and this permission notice appear in all copies.
6+
//
7+
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8+
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9+
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10+
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11+
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12+
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13+
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14+
15+
var common = require('../common');
16+
var assert = require('assert');
17+
var v8 = require('v8');
18+
var vm = require('vm');
19+
20+
v8.setFlagsFromString('--allow_natives_syntax');
21+
assert(eval('%_IsSmi(42)'));
22+
assert(vm.runInThisContext('%_IsSmi(42)'));
23+
24+
v8.setFlagsFromString('--noallow_natives_syntax');
25+
assert.throws(function() { eval('%_IsSmi(42)') }, SyntaxError);
26+
assert.throws(function() { vm.runInThisContext('%_IsSmi(42)') }, SyntaxError);

test/simple/test-v8-gc.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) 2014, StrongLoop Inc.
2+
//
3+
// Permission to use, copy, modify, and/or distribute this software for any
4+
// purpose with or without fee is hereby granted, provided that the above
5+
// copyright notice and this permission notice appear in all copies.
6+
//
7+
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8+
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9+
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10+
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11+
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12+
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13+
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14+
15+
// Flags: --expose_gc
16+
17+
var common = require('../common');
18+
var assert = require('assert');
19+
var v8 = require('v8');
20+
21+
assert(typeof gc === 'function', 'Run this test with --expose_gc.');
22+
23+
var ncalls = 0;
24+
var before;
25+
var after;
26+
27+
function ongc(before_, after_) {
28+
// Try very hard to not create garbage because that could kick off another
29+
// garbage collection cycle.
30+
before = before_;
31+
after = after_;
32+
ncalls += 1;
33+
}
34+
35+
gc();
36+
v8.on('gc', ongc);
37+
gc();
38+
v8.removeListener('gc', ongc);
39+
gc();
40+
41+
assert.equal(ncalls, 1);
42+
assert.equal(typeof before, 'object');
43+
assert.equal(typeof after, 'object');
44+
assert.equal(typeof before.timestamp, 'number');
45+
assert.equal(typeof after.timestamp, 'number');
46+
assert.equal(before.timestamp <= after.timestamp, true);

test/simple/test-v8-stats.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) 2014, StrongLoop Inc.
2+
//
3+
// Permission to use, copy, modify, and/or distribute this software for any
4+
// purpose with or without fee is hereby granted, provided that the above
5+
// copyright notice and this permission notice appear in all copies.
6+
//
7+
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8+
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9+
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10+
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11+
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12+
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13+
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14+
15+
var common = require('../common');
16+
var assert = require('assert');
17+
var v8 = require('v8');
18+
19+
var s = v8.getHeapStatistics();
20+
var keys = [
21+
'heap_size_limit',
22+
'total_heap_size',
23+
'total_heap_size_executable',
24+
'total_physical_size',
25+
'used_heap_size'];
26+
assert.deepEqual(Object.keys(s).sort(), keys);
27+
keys.forEach(function(key) {
28+
assert.equal(typeof s[key], 'number');
29+
});

0 commit comments

Comments
 (0)