Skip to content

Commit f8076c4

Browse files
committed
lib: add setFlagsFromString() to tracing module
Expose v8::V8::SetFlagsFromString() on tracing.v8 in lib/tracing.js. PR-URL: node-forward/node#62 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-by: Trevor Norris <trev.norris@gmail.com>
1 parent 83d2cb6 commit f8076c4

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

doc/api/tracing.markdown

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,21 @@ Returns an object with the following properties
5858
}
5959
```
6060

61+
### setFlagsFromString()
62+
63+
Set additional V8 command line flags. Use with care; changing settings
64+
after the VM has started may result in unpredictable behavior, including
65+
crashes and data loss. Or it may simply do nothing.
66+
67+
Usage:
68+
69+
```
70+
// Print GC events to stdout for one minute.
71+
var v8 = require('tracing').v8;
72+
v8.setFlagsFromString('--trace_gc');
73+
setTimeout(function() { v8.setFlagsFromString('--notrace_gc'); }, 60e3);
74+
```
75+
6176

6277
# Async Listeners
6378

lib/tracing.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ exports._nodeInitialization = function nodeInitialization(pobj) {
3131

3232
// Finish setting up the v8 Object.
3333
v8.getHeapStatistics = v8binding.getHeapStatistics;
34+
v8.setFlagsFromString = v8binding.setFlagsFromString;
3435

3536
// Part of the AsyncListener setup to share objects/callbacks with the
3637
// native layer.

src/node_v8.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ using v8::Local;
4141
using v8::Null;
4242
using v8::Number;
4343
using v8::Object;
44+
using v8::String;
4445
using v8::Uint32;
46+
using v8::V8;
4547
using v8::Value;
4648
using v8::kGCTypeAll;
4749
using v8::kGCTypeMarkSweepCompact;
@@ -204,6 +206,12 @@ void StopGarbageCollectionTracking(const FunctionCallbackInfo<Value>& args) {
204206
}
205207

206208

209+
void SetFlagsFromString(const FunctionCallbackInfo<Value>& args) {
210+
String::Utf8Value flags(args[0]);
211+
V8::SetFlagsFromString(*flags, flags.length());
212+
}
213+
214+
207215
void InitializeV8Bindings(Handle<Object> target,
208216
Handle<Value> unused,
209217
Handle<Context> context) {
@@ -215,6 +223,7 @@ void InitializeV8Bindings(Handle<Object> target,
215223
"stopGarbageCollectionTracking",
216224
StopGarbageCollectionTracking);
217225
env->SetMethod(target, "getHeapStatistics", GetHeapStatistics);
226+
env->SetMethod(target, "setFlagsFromString", SetFlagsFromString);
218227
}
219228

220229
} // namespace node

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('tracing').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);

0 commit comments

Comments
 (0)