summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Carey <jcarey@argv.me>2018-09-13 15:04:20 -0400
committerJason Carey <jcarey@argv.me>2018-09-17 17:57:16 -0400
commit1faa184e835a7a628631064af08389471d64ed0f (patch)
treec833b04d8ed6f9d4668e8b185c3f8b92fc46e556
parent11892a26868b452a161b143bb93a04d2ac3b6748 (diff)
downloadmongo-1faa184e835a7a628631064af08389471d64ed0f.tar.gz
SERVER-37126 Always _runSafely in implscope
Extends _runSafely use to all public methods on implscope
-rw-r--r--src/mongo/scripting/mozjs/implscope.cpp179
1 files changed, 85 insertions, 94 deletions
diff --git a/src/mongo/scripting/mozjs/implscope.cpp b/src/mongo/scripting/mozjs/implscope.cpp
index 660bc7f59b9..bf35606f2ba 100644
--- a/src/mongo/scripting/mozjs/implscope.cpp
+++ b/src/mongo/scripting/mozjs/implscope.cpp
@@ -536,37 +536,31 @@ auto MozJSImplScope::_runSafely(ImplScopeFunction&& functionToRun) -> decltype(f
}
void MozJSImplScope::setNumber(const char* field, double val) {
- _runSafely([this, &field, &val] { ObjectWrapper(_context, _global).setNumber(field, val); });
+ _runSafely([&] { ObjectWrapper(_context, _global).setNumber(field, val); });
}
void MozJSImplScope::setString(const char* field, StringData val) {
- _runSafely([this, &field, &val] { ObjectWrapper(_context, _global).setString(field, val); });
+ _runSafely([&] { ObjectWrapper(_context, _global).setString(field, val); });
}
void MozJSImplScope::setBoolean(const char* field, bool val) {
- _runSafely([this, &field, &val] { ObjectWrapper(_context, _global).setBoolean(field, val); });
+ _runSafely([&] { ObjectWrapper(_context, _global).setBoolean(field, val); });
}
void MozJSImplScope::setElement(const char* field, const BSONElement& e, const BSONObj& parent) {
- _runSafely([this, &field, &e, &parent] {
-
- ObjectWrapper(_context, _global).setBSONElement(field, e, parent, false);
- });
+ _runSafely([&] { ObjectWrapper(_context, _global).setBSONElement(field, e, parent, false); });
}
void MozJSImplScope::setObject(const char* field, const BSONObj& obj, bool readOnly) {
- _runSafely([this, &field, &obj, &readOnly] {
-
- ObjectWrapper(_context, _global).setBSON(field, obj, readOnly);
- });
+ _runSafely([&] { ObjectWrapper(_context, _global).setBSON(field, obj, readOnly); });
}
int MozJSImplScope::type(const char* field) {
- return _runSafely([this, &field] { return ObjectWrapper(_context, _global).type(field); });
+ return _runSafely([&] { return ObjectWrapper(_context, _global).type(field); });
}
double MozJSImplScope::getNumber(const char* field) {
- return _runSafely([this, &field] { return ObjectWrapper(_context, _global).getNumber(field); });
+ return _runSafely([&] { return ObjectWrapper(_context, _global).getNumber(field); });
}
int MozJSImplScope::getNumberInt(const char* field) {
@@ -585,7 +579,7 @@ Decimal128 MozJSImplScope::getNumberDecimal(const char* field) {
}
std::string MozJSImplScope::getString(const char* field) {
- return _runSafely([this, &field] { return ObjectWrapper(_context, _global).getString(field); });
+ return _runSafely([&] { return ObjectWrapper(_context, _global).getString(field); });
}
bool MozJSImplScope::getBoolean(const char* field) {
@@ -594,13 +588,11 @@ bool MozJSImplScope::getBoolean(const char* field) {
}
BSONObj MozJSImplScope::getObject(const char* field) {
- return _runSafely([this, &field] { return ObjectWrapper(_context, _global).getObject(field); });
+ return _runSafely([&] { return ObjectWrapper(_context, _global).getObject(field); });
}
void MozJSImplScope::newFunction(StringData raw, JS::MutableHandleValue out) {
- MozJSEntry entry(this);
-
- _MozJSCreateFunction(raw, std::move(out));
+ _runSafely([&] { _MozJSCreateFunction(raw, std::move(out)); });
}
void MozJSImplScope::_MozJSCreateFunction(StringData raw, JS::MutableHandleValue fun) {
@@ -661,16 +653,16 @@ bool hasFunctionIdentifier(StringData code) {
}
ScriptingFunction MozJSImplScope::_createFunction(const char* raw) {
- MozJSEntry entry(this);
-
- JS::RootedValue fun(_context);
- _MozJSCreateFunction(raw, &fun);
- _funcs.emplace_back(_context, fun.get());
- return _funcs.size();
+ return _runSafely([&] {
+ JS::RootedValue fun(_context);
+ _MozJSCreateFunction(raw, &fun);
+ _funcs.emplace_back(_context, fun.get());
+ return _funcs.size();
+ });
}
void MozJSImplScope::setFunction(const char* field, const char* code) {
- _runSafely([this, &field, &code] {
+ _runSafely([&] {
JS::RootedValue fun(_context);
_MozJSCreateFunction(code, &fun);
ObjectWrapper(_context, _global).setValue(field, fun);
@@ -678,7 +670,7 @@ void MozJSImplScope::setFunction(const char* field, const char* code) {
}
void MozJSImplScope::rename(const char* from, const char* to) {
- _runSafely([this, &from, &to] { ObjectWrapper(_context, _global).rename(from, to); });
+ _runSafely([&] { ObjectWrapper(_context, _global).rename(from, to); });
}
int MozJSImplScope::invoke(ScriptingFunction func,
@@ -688,62 +680,62 @@ int MozJSImplScope::invoke(ScriptingFunction func,
bool ignoreReturn,
bool readOnlyArgs,
bool readOnlyRecv) {
- MozJSEntry entry(this);
-
- auto funcValue = _funcs[func - 1];
- JS::RootedValue result(_context);
+ return _runSafely([&] {
+ auto funcValue = _funcs[func - 1];
+ JS::RootedValue result(_context);
- const int nargs = argsObject ? argsObject->nFields() : 0;
+ const int nargs = argsObject ? argsObject->nFields() : 0;
- JS::AutoValueVector args(_context);
+ JS::AutoValueVector args(_context);
- if (nargs) {
- BSONObjIterator it(*argsObject);
- for (int i = 0; i < nargs; i++) {
- BSONElement next = it.next();
+ if (nargs) {
+ BSONObjIterator it(*argsObject);
+ for (int i = 0; i < nargs; i++) {
+ BSONElement next = it.next();
- JS::RootedValue value(_context);
- ValueReader(_context, &value).fromBSONElement(next, *argsObject, readOnlyArgs);
+ JS::RootedValue value(_context);
+ ValueReader(_context, &value).fromBSONElement(next, *argsObject, readOnlyArgs);
- args.append(value);
+ args.append(value);
+ }
}
- }
- JS::RootedValue smrecv(_context);
- if (recv)
- ValueReader(_context, &smrecv).fromBSON(*recv, nullptr, readOnlyRecv);
- else
- smrecv.setObjectOrNull(_global);
+ JS::RootedValue smrecv(_context);
+ if (recv)
+ ValueReader(_context, &smrecv).fromBSON(*recv, nullptr, readOnlyRecv);
+ else
+ smrecv.setObjectOrNull(_global);
- if (timeoutMs)
- _engine->getDeadlineMonitor().startDeadline(this, timeoutMs);
- else {
- _engine->getDeadlineMonitor().startDeadline(this, -1);
- }
+ if (timeoutMs)
+ _engine->getDeadlineMonitor().startDeadline(this, timeoutMs);
+ else {
+ _engine->getDeadlineMonitor().startDeadline(this, -1);
+ }
- JS::RootedValue out(_context);
- JS::RootedObject obj(_context, smrecv.toObjectOrNull());
+ JS::RootedValue out(_context);
+ JS::RootedObject obj(_context, smrecv.toObjectOrNull());
- bool success = JS::Call(_context, obj, funcValue, args, &out);
+ bool success = JS::Call(_context, obj, funcValue, args, &out);
- _engine->getDeadlineMonitor().stopDeadline(this);
+ _engine->getDeadlineMonitor().stopDeadline(this);
- _checkErrorState(success);
+ _checkErrorState(success);
- if (!ignoreReturn) {
- // must validate the handle because TerminateExecution may have
- // been thrown after the above checks
- if (out.isObject() && _nativeFunctionProto.instanceOf(out)) {
- warning() << "storing native function as return value";
- _lastRetIsNativeCode = true;
- } else {
- _lastRetIsNativeCode = false;
- }
+ if (!ignoreReturn) {
+ // must validate the handle because TerminateExecution may have
+ // been thrown after the above checks
+ if (out.isObject() && _nativeFunctionProto.instanceOf(out)) {
+ warning() << "storing native function as return value";
+ _lastRetIsNativeCode = true;
+ } else {
+ _lastRetIsNativeCode = false;
+ }
- ObjectWrapper(_context, _global).setValue(kInvokeResult, out);
- }
+ ObjectWrapper(_context, _global).setValue(kInvokeResult, out);
+ }
- return 0;
+ return 0;
+ });
}
bool MozJSImplScope::exec(StringData code,
@@ -752,45 +744,45 @@ bool MozJSImplScope::exec(StringData code,
bool reportError,
bool assertOnError,
int timeoutMs) {
- MozJSEntry entry(this);
-
- JS::CompileOptions co(_context);
- setCompileOptions(&co);
- co.setFileAndLine(name.c_str(), 1);
- JS::RootedScript script(_context);
+ return _runSafely([&] {
+ JS::CompileOptions co(_context);
+ setCompileOptions(&co);
+ co.setFileAndLine(name.c_str(), 1);
+ JS::RootedScript script(_context);
- bool success = JS::Compile(_context, co, code.rawData(), code.size(), &script);
+ bool success = JS::Compile(_context, co, code.rawData(), code.size(), &script);
- if (_checkErrorState(success, reportError, assertOnError))
- return false;
+ if (_checkErrorState(success, reportError, assertOnError))
+ return false;
- if (timeoutMs) {
- _engine->getDeadlineMonitor().startDeadline(this, timeoutMs);
- } else {
- _engine->getDeadlineMonitor().startDeadline(this, -1);
- }
+ if (timeoutMs) {
+ _engine->getDeadlineMonitor().startDeadline(this, timeoutMs);
+ } else {
+ _engine->getDeadlineMonitor().startDeadline(this, -1);
+ }
- JS::RootedValue out(_context);
+ JS::RootedValue out(_context);
- success = JS_ExecuteScript(_context, script, &out);
+ success = JS_ExecuteScript(_context, script, &out);
- _engine->getDeadlineMonitor().stopDeadline(this);
+ _engine->getDeadlineMonitor().stopDeadline(this);
- if (_checkErrorState(success, reportError, assertOnError))
- return false;
+ if (_checkErrorState(success, reportError, assertOnError))
+ return false;
- ObjectWrapper(_context, _global).setValue(kExecResult, out);
+ ObjectWrapper(_context, _global).setValue(kExecResult, out);
- if (printResult && !out.isUndefined()) {
- // appears to only be used by shell
- std::cout << ValueWriter(_context, out).toString() << std::endl;
- }
+ if (printResult && !out.isUndefined()) {
+ // appears to only be used by shell
+ std::cout << ValueWriter(_context, out).toString() << std::endl;
+ }
- return true;
+ return true;
+ });
}
void MozJSImplScope::injectNative(const char* field, NativeFunction func, void* data) {
- _runSafely([this, &field, &func, &data] {
+ _runSafely([&] {
JS::RootedObject obj(_context);
NativeFunctionInfo::make(_context, &obj, func, data);
@@ -816,7 +808,6 @@ void MozJSImplScope::sleep(Milliseconds ms) {
}
void MozJSImplScope::externalSetup() {
-
_runSafely([&] {
if (_connectState == ConnectState::External)
return;