diff --git a/src/RJSUtil.hpp b/src/RJSUtil.hpp index 5e968d0c..598e0aea 100644 --- a/src/RJSUtil.hpp +++ b/src/RJSUtil.hpp @@ -128,6 +128,12 @@ static inline JSObjectRef RJSValidatedValueToFunction(JSContextRef ctx, JSValueR static inline double RJSValidatedValueToNumber(JSContextRef ctx, JSValueRef value) { JSValueRef exception = NULL; + if (JSValueIsUndefined(ctx, value)) { + throw std::invalid_argument("`undefined` is not a number."); + } + if (JSValueIsNull(ctx, value)) { + throw std::invalid_argument("`null` is not a number."); + } double number = JSValueToNumber(ctx, value, &exception); if (exception) { throw RJSException(ctx, exception); diff --git a/tests/ObjectTests.js b/tests/ObjectTests.js index 5d045e44..51f35e92 100644 --- a/tests/ObjectTests.js +++ b/tests/ObjectTests.js @@ -73,10 +73,52 @@ module.exports = BaseTest.extend({ TestCase.assertThrows(function() { obj.boolCol = 'cat'; }); - TestCase.assertThrows(function() { obj.intCol = 'dog'; }); + + TestCase.assertThrows(function() { + obj.boolCol = null; + }); + TestCase.assertThrows(function() { + obj.boolCol = undefined; + }); + TestCase.assertThrows(function() { + obj.intCol = null; + }); + TestCase.assertThrows(function() { + obj.intCol = undefined; + }); + TestCase.assertThrows(function() { + obj.floatCol = null; + }); + TestCase.assertThrows(function() { + obj.floatCol = undefined; + }); + TestCase.assertThrows(function() { + obj.doubleCol = null; + }); + TestCase.assertThrows(function() { + obj.doubleCol = undefined; + }); + TestCase.assertThrows(function() { + obj.stringCol = null; + }); + TestCase.assertThrows(function() { + obj.stringCol = undefined; + }); + TestCase.assertThrows(function() { + obj.dateCol = null; + }); + TestCase.assertThrows(function() { + obj.dateCol = undefined; + }); + TestCase.assertThrows(function() { + obj.dataCol = null; + }); + TestCase.assertThrows(function() { + obj.dataCol = undefined; + }); }); TestCase.assertThrows(function() {