From f8076c40672d6317d8c2e1259b5d23235156cd99 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Fri, 14 Nov 2014 23:20:07 +0100 Subject: [PATCH] lib: add setFlagsFromString() to tracing module Expose v8::V8::SetFlagsFromString() on tracing.v8 in lib/tracing.js. PR-URL: https://github.com/node-forward/node/pull/62 Reviewed-By: Colin Ihrig Reviewed-by: Trevor Norris --- doc/api/tracing.markdown | 15 +++++++++++++++ lib/tracing.js | 1 + src/node_v8.cc | 9 +++++++++ test/simple/test-v8-flags.js | 26 ++++++++++++++++++++++++++ 4 files changed, 51 insertions(+) create mode 100644 test/simple/test-v8-flags.js diff --git a/doc/api/tracing.markdown b/doc/api/tracing.markdown index 92e6809a04..fc106ca589 100644 --- a/doc/api/tracing.markdown +++ b/doc/api/tracing.markdown @@ -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 diff --git a/lib/tracing.js b/lib/tracing.js index 49d0dec35c..5e35f43591 100644 --- a/lib/tracing.js +++ b/lib/tracing.js @@ -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. diff --git a/src/node_v8.cc b/src/node_v8.cc index d59e661fbd..fa60bbee4b 100644 --- a/src/node_v8.cc +++ b/src/node_v8.cc @@ -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; @@ -204,6 +206,12 @@ void StopGarbageCollectionTracking(const FunctionCallbackInfo& args) { } +void SetFlagsFromString(const FunctionCallbackInfo& args) { + String::Utf8Value flags(args[0]); + V8::SetFlagsFromString(*flags, flags.length()); +} + + void InitializeV8Bindings(Handle target, Handle unused, Handle context) { @@ -215,6 +223,7 @@ void InitializeV8Bindings(Handle target, "stopGarbageCollectionTracking", StopGarbageCollectionTracking); env->SetMethod(target, "getHeapStatistics", GetHeapStatistics); + env->SetMethod(target, "setFlagsFromString", SetFlagsFromString); } } // namespace node diff --git a/test/simple/test-v8-flags.js b/test/simple/test-v8-flags.js new file mode 100644 index 0000000000..1de36db676 --- /dev/null +++ b/test/simple/test-v8-flags.js @@ -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);