src: add type check to v8.setFlagsFromString()
authorRoman Klauke <romaaan.git@gmail.com>
Thu, 7 May 2015 18:27:12 +0000 (20:27 +0200)
committerBen Noordhuis <info@bnoordhuis.nl>
Fri, 8 May 2015 17:09:45 +0000 (19:09 +0200)
Calling v8.setFlagsFromString with e.g a function as a flag argument
gave no exception or warning that the function call will fail.

There is now an exception if the function gets called with the wrong
flag type (string is required) or that a flag is expected.

Other APIs already provide exceptions if the argument has not the
expected type.

PR-URL: https://github.com/iojs/io.js/pull/1652
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
doc/api/v8.markdown
src/node_v8.cc
test/parallel/test-v8-flag-type-check.js [new file with mode: 0644]

index adced82..cedd5c8 100644 (file)
@@ -20,7 +20,7 @@ Returns an object with the following properties
 }
 ```
 
-## setFlagsFromString()
+## setFlagsFromString(string)
 
 Set additional V8 command line flags.  Use with care; changing settings
 after the VM has started may result in unpredictable behavior, including
index f3bdda4..2834a21 100644 (file)
@@ -60,6 +60,13 @@ void GetHeapStatistics(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");
+
   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
new file mode 100644 (file)
index 0000000..1bb501d
--- /dev/null
@@ -0,0 +1,6 @@
+var common = require('../common');
+var assert = require('assert');
+var v8 = require('v8');
+
+assert.throws(function() {v8.setFlagsFromString(1)}, TypeError);
+assert.throws(function() {v8.setFlagsFromString()}, TypeError);