summaryrefslogtreecommitdiff
path: root/src/mongo/scripting
diff options
context:
space:
mode:
authorJonathan Reams <jbreams@mongodb.com>2015-07-24 17:30:50 -0400
committerJonathan Reams <jbreams@mongodb.com>2015-07-30 11:44:22 -0400
commit776125821c0eb2212d509ab1e52dc2139c495963 (patch)
tree842b8ae9beb1e999eae4cb2eb5507020cc1ff128 /src/mongo/scripting
parent9c76b002fed4cbb06685e556a865dba62dba7c17 (diff)
downloadmongo-776125821c0eb2212d509ab1e52dc2139c495963.tar.gz
SERVER-9973 Allow creating NumberLong from anything that supports toNumber
Diffstat (limited to 'src/mongo/scripting')
-rw-r--r--src/mongo/scripting/mozjs/numberlong.cpp22
-rw-r--r--src/mongo/scripting/mozjs/valuewriter.cpp3
2 files changed, 18 insertions, 7 deletions
diff --git a/src/mongo/scripting/mozjs/numberlong.cpp b/src/mongo/scripting/mozjs/numberlong.cpp
index f8955689c9e..c455acc0146 100644
--- a/src/mongo/scripting/mozjs/numberlong.cpp
+++ b/src/mongo/scripting/mozjs/numberlong.cpp
@@ -30,6 +30,8 @@
#include "mongo/scripting/mozjs/numberlong.h"
+#include <js/Conversions.h>
+
#include "mongo/scripting/mozjs/implscope.h"
#include "mongo/scripting/mozjs/objectwrapper.h"
#include "mongo/scripting/mozjs/valuereader.h"
@@ -121,16 +123,22 @@ void NumberLongInfo::construct(JSContext* cx, JS::CallArgs args) {
if (args.length() == 0) {
o.setNumber(kFloatApprox, 0);
} else if (args.length() == 1) {
- if (args.get(0).isNumber()) {
- o.setValue(kFloatApprox, args.get(0));
+ auto arg = args.get(0);
+ if (arg.isNumber()) {
+ o.setValue(kFloatApprox, arg);
} else {
- std::string str = ValueWriter(cx, args.get(0)).toString();
-
- unsigned long long val = parseLL(str.c_str());
+ std::string str = ValueWriter(cx, arg).toString();
+ long long val;
+ // For string values we call strtoll because we expect non-number string
+ // values to fail rather than return 0 (which is the behavior of ToInt64.
+ if (arg.isString())
+ val = parseLL(str.c_str());
+ // Otherwise we call the toNumber on the js value to get the long long value.
+ else
+ val = ValueWriter(cx, arg).toInt64();
// values above 2^53 are not accurately represented in JS
- if ((long long)val == (long long)(double)(long long)(val) &&
- val < 9007199254740992ULL) {
+ if ((long long)val == (long long)(double)(long long)(val) && val < 9007199254740992LL) {
o.setNumber(kFloatApprox, val);
} else {
o.setNumber(kFloatApprox, val);
diff --git a/src/mongo/scripting/mozjs/valuewriter.cpp b/src/mongo/scripting/mozjs/valuewriter.cpp
index e6aa91cbe44..0efeca6f8dd 100644
--- a/src/mongo/scripting/mozjs/valuewriter.cpp
+++ b/src/mongo/scripting/mozjs/valuewriter.cpp
@@ -130,6 +130,9 @@ int32_t ValueWriter::toInt32() {
int64_t ValueWriter::toInt64() {
int64_t out;
+ if (getScope(_context)->getNumberLongProto().instanceOf(_value))
+ return NumberLongInfo::ToNumberLong(_context, _value);
+
if (JS::ToInt64(_context, _value, &out))
return out;