diff options
author | Jason Carey <jcarey@argv.me> | 2015-10-06 20:45:57 -0400 |
---|---|---|
committer | Jason Carey <jcarey@argv.me> | 2015-10-06 20:45:57 -0400 |
commit | 054b0faf2cc16e3d40d2d40ffdda1e219e4c24e3 (patch) | |
tree | 38cf21d16af837af7a0273cd76ef1bd3c1315e4b /src/mongo | |
parent | cad0c421371e55fdf65c0be4badd23120bec72c1 (diff) | |
download | mongo-054b0faf2cc16e3d40d2d40ffdda1e219e4c24e3.tar.gz |
SERVER-19977 Use JS_NewArrayObject directly
Rather than creating objects with stringified integer keys, just create
them directly with JS_NewArrayObject, which can take JS::CallArgs or
JS::AutoValueVector's directly.
Diffstat (limited to 'src/mongo')
-rw-r--r-- | src/mongo/scripting/mozjs/jsthread.cpp | 13 | ||||
-rw-r--r-- | src/mongo/scripting/mozjs/nativefunction.cpp | 16 | ||||
-rw-r--r-- | src/mongo/scripting/mozjs/valuereader.cpp | 17 |
3 files changed, 16 insertions, 30 deletions
diff --git a/src/mongo/scripting/mozjs/jsthread.cpp b/src/mongo/scripting/mozjs/jsthread.cpp index 2e5ea289d84..b257876afc6 100644 --- a/src/mongo/scripting/mozjs/jsthread.cpp +++ b/src/mongo/scripting/mozjs/jsthread.cpp @@ -88,17 +88,12 @@ public: "first argument must be a function", args.get(0).isObject() && JS_ObjectIsFunction(cx, args.get(0).toObjectOrNull())); - JS::RootedObject robj(cx, JS_NewPlainObject(cx)); - ObjectWrapper wobj(cx, robj); - for (unsigned i = 0; i < args.length(); ++i) { - // 10 decimal digits for a 32 bit unsigned, then 1 for the null - char buf[11]; - std::sprintf(buf, "%i", i); - - wobj.setValue(buf, args.get(i)); + JS::RootedObject robj(cx, JS_NewArrayObject(cx, args)); + if (!robj) { + uasserted(ErrorCodes::JSInterpreterFailure, "Failed to JS_NewArrayObject"); } - _sharedData->_args = wobj.toBSON(); + _sharedData->_args = ObjectWrapper(cx, robj).toBSON(); _sharedData->_stack = currentJSStackToString(cx); diff --git a/src/mongo/scripting/mozjs/nativefunction.cpp b/src/mongo/scripting/mozjs/nativefunction.cpp index 6399bb60709..45e804a6feb 100644 --- a/src/mongo/scripting/mozjs/nativefunction.cpp +++ b/src/mongo/scripting/mozjs/nativefunction.cpp @@ -77,20 +77,12 @@ void NativeFunctionInfo::call(JSContext* cx, JS::CallArgs args) { return; } - BSONObjBuilder bob; - JS::RootedObject robj(cx, JS_NewPlainObject(cx)); - ObjectWrapper wobj(cx, robj); - - for (unsigned i = 0; i < args.length(); i++) { - // 11 is enough here. unsigned's are only 32 bits, and 1 << 32 is only - // 10 decimal digits. +1 for the null and we're only at 11. - char buf[11]; - std::sprintf(buf, "%i", i); - - wobj.setValue(buf, args.get(i)); + JS::RootedObject robj(cx, JS_NewArrayObject(cx, args)); + if (!robj) { + uasserted(ErrorCodes::JSInterpreterFailure, "Failed to JS_NewArrayObject"); } - BSONObj out = holder->_func(wobj.toBSON(), holder->_ctx); + BSONObj out = holder->_func(ObjectWrapper(cx, robj).toBSON(), holder->_ctx); ValueReader(cx, args.rval()).fromBSONElement(out.firstElement(), out, false); } diff --git a/src/mongo/scripting/mozjs/valuereader.cpp b/src/mongo/scripting/mozjs/valuereader.cpp index b78ed461297..292bb32f936 100644 --- a/src/mongo/scripting/mozjs/valuereader.cpp +++ b/src/mongo/scripting/mozjs/valuereader.cpp @@ -76,20 +76,19 @@ void ValueReader::fromBSONElement(const BSONElement& elem, const BSONObj& parent _value.setInt32(elem.Int()); return; case mongo::Array: { - auto arrayPtr = JS_NewArrayObject(_context, 0); - uassert(ErrorCodes::JSInterpreterFailure, "Failed to JS_NewArrayObject", arrayPtr); - JS::RootedObject array(_context, arrayPtr); + JS::AutoValueVector avv(_context); - unsigned i = 0; BSONForEach(subElem, elem.embeddedObject()) { - // We use an unsigned 32 bit integer, so 10 base 10 digits and - // 1 null byte - char str[11]; - sprintf(str, "%i", i++); JS::RootedValue member(_context); ValueReader(_context, &member).fromBSONElement(subElem, parent, readOnly); - ObjectWrapper(_context, array).setValue(str, member); + if (!avv.append(member)) { + uasserted(ErrorCodes::JSInterpreterFailure, "Failed to append to JS array"); + } + } + JS::RootedObject array(_context, JS_NewArrayObject(_context, avv)); + if (!array) { + uasserted(ErrorCodes::JSInterpreterFailure, "Failed to JS_NewArrayObject"); } _value.setObjectOrNull(array); return; |