From 64168eb9b43e30e4c0b986c9b29c41be63e85df6 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Thu, 26 Oct 2017 16:31:23 -0700 Subject: [PATCH] v8: migrate setFlagsFromString to internal/errors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/16535 Reviewed-By: Refael Ackermann Reviewed-By: Michaƫl Zasso Reviewed-By: Joyee Cheung Reviewed-By: Matteo Collina Reviewed-By: Luigi Pinca --- doc/api/v8.md | 3 ++- lib/v8.js | 9 ++++++++- src/node_v8.cc | 8 +------- test/parallel/test-v8-flag-type-check.js | 17 +++++++++++------ 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/doc/api/v8.md b/doc/api/v8.md index 634d3199a1a..64c9be1a88b 100644 --- a/doc/api/v8.md +++ b/doc/api/v8.md @@ -133,10 +133,11 @@ For example: } ``` -## v8.setFlagsFromString(string) +## v8.setFlagsFromString(flags) +* `flags` {string} The `v8.setFlagsFromString()` method can be used to programmatically set V8 command line flags. This method should be used with care. Changing settings diff --git a/lib/v8.js b/lib/v8.js index fccc39edc68..fffd18bf1be 100644 --- a/lib/v8.js +++ b/lib/v8.js @@ -15,6 +15,7 @@ 'use strict'; const { Buffer } = require('buffer'); +const errors = require('internal/errors'); const { Serializer: _Serializer, Deserializer: _Deserializer @@ -32,7 +33,7 @@ class Deserializer extends _Deserializer { } const { cachedDataVersionTag, - setFlagsFromString, + setFlagsFromString: _setFlagsFromString, heapStatisticsArrayBuffer, heapSpaceStatisticsArrayBuffer, updateHeapStatisticsArrayBuffer, @@ -64,6 +65,12 @@ const heapStatisticsBuffer = const heapSpaceStatisticsBuffer = new Float64Array(heapSpaceStatisticsArrayBuffer); +function setFlagsFromString(flags) { + if (typeof flags !== 'string') + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'flags', 'string'); + _setFlagsFromString(flags); +} + function getHeapStatistics() { const buffer = heapStatisticsBuffer; diff --git a/src/node_v8.cc b/src/node_v8.cc index f46bed248ea..352b4b334ad 100644 --- a/src/node_v8.cc +++ b/src/node_v8.cc @@ -114,13 +114,7 @@ void UpdateHeapSpaceStatisticsBuffer(const FunctionCallbackInfo& args) { void SetFlagsFromString(const FunctionCallbackInfo& args) { - Environment* env = Environment::GetCurrent(args); - - if (args.Length() < 1) - return env->ThrowTypeError("v8 flag is required"); - if (!args[0]->IsString()) - return env->ThrowTypeError("v8 flag must be a string"); - + CHECK(args[0]->IsString()); String::Utf8Value flags(args[0]); V8::SetFlagsFromString(*flags, flags.length()); } diff --git a/test/parallel/test-v8-flag-type-check.js b/test/parallel/test-v8-flag-type-check.js index 144e8f7a2e3..67a34b51f2a 100644 --- a/test/parallel/test-v8-flag-type-check.js +++ b/test/parallel/test-v8-flag-type-check.js @@ -1,9 +1,14 @@ 'use strict'; -require('../common'); -const assert = require('assert'); +const common = require('../common'); const v8 = require('v8'); -assert.throws(function() { v8.setFlagsFromString(1); }, - /^TypeError: v8 flag must be a string$/); -assert.throws(function() { v8.setFlagsFromString(); }, - /^TypeError: v8 flag is required$/); +[ 1, undefined ].forEach((i) => { + common.expectsError( + () => v8.setFlagsFromString(i), + { + code: 'ERR_INVALID_ARG_TYPE', + type: TypeError, + message: 'The "flags" argument must be of type string' + } + ); +});