Skip to content

Commit

Permalink
v8: migrate setFlagsFromString to internal/errors
Browse files Browse the repository at this point in the history
PR-URL: #16535
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
jasnell committed Oct 30, 2017
1 parent 4fdd76d commit ef238fb
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
3 changes: 2 additions & 1 deletion doc/api/v8.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,11 @@ For example:
}
```

## v8.setFlagsFromString(string)
## v8.setFlagsFromString(flags)
<!-- YAML
added: v1.0.0
-->
* `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
Expand Down
9 changes: 8 additions & 1 deletion lib/v8.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
'use strict';

const { Buffer } = require('buffer');
const errors = require('internal/errors');
const {
Serializer: _Serializer,
Deserializer: _Deserializer
Expand All @@ -32,7 +33,7 @@ class Deserializer extends _Deserializer { }

const {
cachedDataVersionTag,
setFlagsFromString,
setFlagsFromString: _setFlagsFromString,
heapStatisticsArrayBuffer,
heapSpaceStatisticsArrayBuffer,
updateHeapStatisticsArrayBuffer,
Expand Down Expand Up @@ -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;

Expand Down
8 changes: 1 addition & 7 deletions src/node_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,7 @@ void UpdateHeapSpaceStatisticsBuffer(const FunctionCallbackInfo<Value>& args) {


void SetFlagsFromString(const FunctionCallbackInfo<Value>& 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());
}
Expand Down
17 changes: 11 additions & 6 deletions test/parallel/test-v8-flag-type-check.js
Original file line number Diff line number Diff line change
@@ -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'
}
);
});

0 comments on commit ef238fb

Please sign in to comment.