Skip to content

Commit

Permalink
lib: add setFlagsFromString() to tracing module
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
bnoordhuis committed Nov 20, 2014
1 parent 83d2cb6 commit f8076c4
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
15 changes: 15 additions & 0 deletions doc/api/tracing.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ Returns an object with the following properties
}
```

### setFlagsFromString()

Set additional V8 command line flags. Use with care; changing settings
after the VM has started may result in unpredictable behavior, including
crashes and data loss. Or it may simply do nothing.

Usage:

```
// Print GC events to stdout for one minute.
var v8 = require('tracing').v8;
v8.setFlagsFromString('--trace_gc');
setTimeout(function() { v8.setFlagsFromString('--notrace_gc'); }, 60e3);
```


# Async Listeners

Expand Down
1 change: 1 addition & 0 deletions lib/tracing.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ exports._nodeInitialization = function nodeInitialization(pobj) {

// Finish setting up the v8 Object.
v8.getHeapStatistics = v8binding.getHeapStatistics;
v8.setFlagsFromString = v8binding.setFlagsFromString;

// Part of the AsyncListener setup to share objects/callbacks with the
// native layer.
Expand Down
9 changes: 9 additions & 0 deletions src/node_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ using v8::Local;
using v8::Null;
using v8::Number;
using v8::Object;
using v8::String;
using v8::Uint32;
using v8::V8;
using v8::Value;
using v8::kGCTypeAll;
using v8::kGCTypeMarkSweepCompact;
Expand Down Expand Up @@ -204,6 +206,12 @@ void StopGarbageCollectionTracking(const FunctionCallbackInfo<Value>& args) {
}


void SetFlagsFromString(const FunctionCallbackInfo<Value>& args) {
String::Utf8Value flags(args[0]);
V8::SetFlagsFromString(*flags, flags.length());
}


void InitializeV8Bindings(Handle<Object> target,
Handle<Value> unused,
Handle<Context> context) {
Expand All @@ -215,6 +223,7 @@ void InitializeV8Bindings(Handle<Object> target,
"stopGarbageCollectionTracking",
StopGarbageCollectionTracking);
env->SetMethod(target, "getHeapStatistics", GetHeapStatistics);
env->SetMethod(target, "setFlagsFromString", SetFlagsFromString);
}

} // namespace node
Expand Down
26 changes: 26 additions & 0 deletions test/simple/test-v8-flags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2014, StrongLoop Inc.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

var common = require('../common');
var assert = require('assert');
var v8 = require('tracing').v8;
var vm = require('vm');

v8.setFlagsFromString('--allow_natives_syntax');
assert(eval('%_IsSmi(42)'));
assert(vm.runInThisContext('%_IsSmi(42)'));

v8.setFlagsFromString('--noallow_natives_syntax');
assert.throws(function() { eval('%_IsSmi(42)') }, SyntaxError);
assert.throws(function() { vm.runInThisContext('%_IsSmi(42)') }, SyntaxError);

0 comments on commit f8076c4

Please sign in to comment.