summaryrefslogtreecommitdiff
path: root/src/mongo/scripting/mozjs/implscope.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo/scripting/mozjs/implscope.cpp')
-rw-r--r--src/mongo/scripting/mozjs/implscope.cpp195
1 files changed, 105 insertions, 90 deletions
diff --git a/src/mongo/scripting/mozjs/implscope.cpp b/src/mongo/scripting/mozjs/implscope.cpp
index c37ebc0cb2a..c31d5b940a4 100644
--- a/src/mongo/scripting/mozjs/implscope.cpp
+++ b/src/mongo/scripting/mozjs/implscope.cpp
@@ -258,7 +258,6 @@ MozJSImplScope::ASANHandles::ASANHandles() {
}
MozJSImplScope::ASANHandles::~ASANHandles() {
- invariant(_handles.empty());
invariant(kCurrentASANHandles == this);
kCurrentASANHandles = nullptr;
}
@@ -342,6 +341,14 @@ MozJSImplScope::MozRuntime::MozRuntime(const MozJSScriptEngine* engine) {
.setIon(true)
.setAsyncStack(false)
.setNativeRegExp(true);
+ } else {
+ JS::RuntimeOptionsRef(_runtime.get())
+ .setAsmJS(false)
+ .setThrowOnAsmJSValidationFailure(false)
+ .setBaseline(false)
+ .setIon(false)
+ .setAsyncStack(false)
+ .setNativeRegExp(false);
}
const StackLocator locator;
@@ -403,6 +410,7 @@ MozJSImplScope::MozJSImplScope(MozJSScriptEngine* engine)
_connectState(ConnectState::Not),
_status(Status::OK()),
_generation(0),
+ _requireOwnedObjects(false),
_hasOutOfMemoryException(false),
_binDataProto(_context),
_bsonProto(_context),
@@ -491,82 +499,81 @@ void MozJSImplScope::init(const BSONObj* data) {
}
}
-void MozJSImplScope::setNumber(const char* field, double val) {
- MozJSEntry entry(this);
+template <typename ImplScopeFunction>
+auto MozJSImplScope::_runSafely(ImplScopeFunction&& functionToRun) -> decltype(functionToRun()) {
+ try {
+ MozJSEntry entry(this);
+ return functionToRun();
+ } catch (...) {
+ _error = _status.reason();
- ObjectWrapper(_context, _global).setNumber(field, val);
+ // Clear the status state
+ auto status = std::move(_status);
+ uassertStatusOK(status);
+ throw;
+ }
}
-void MozJSImplScope::setString(const char* field, StringData val) {
- MozJSEntry entry(this);
+void MozJSImplScope::setNumber(const char* field, double val) {
+ _runSafely([this, &field, &val] { ObjectWrapper(_context, _global).setNumber(field, val); });
+}
- ObjectWrapper(_context, _global).setString(field, val);
+void MozJSImplScope::setString(const char* field, StringData val) {
+ _runSafely([this, &field, &val] { ObjectWrapper(_context, _global).setString(field, val); });
}
void MozJSImplScope::setBoolean(const char* field, bool val) {
- MozJSEntry entry(this);
-
- ObjectWrapper(_context, _global).setBoolean(field, val);
+ _runSafely([this, &field, &val] { ObjectWrapper(_context, _global).setBoolean(field, val); });
}
void MozJSImplScope::setElement(const char* field, const BSONElement& e, const BSONObj& parent) {
- MozJSEntry entry(this);
+ _runSafely([this, &field, &e, &parent] {
- ObjectWrapper(_context, _global).setBSONElement(field, e, parent, false);
+ ObjectWrapper(_context, _global).setBSONElement(field, e, parent, false);
+ });
}
void MozJSImplScope::setObject(const char* field, const BSONObj& obj, bool readOnly) {
- MozJSEntry entry(this);
+ _runSafely([this, &field, &obj, &readOnly] {
- ObjectWrapper(_context, _global).setBSON(field, obj, readOnly);
+ ObjectWrapper(_context, _global).setBSON(field, obj, readOnly);
+ });
}
int MozJSImplScope::type(const char* field) {
- MozJSEntry entry(this);
-
- return ObjectWrapper(_context, _global).type(field);
+ return _runSafely([this, &field] { return ObjectWrapper(_context, _global).type(field); });
}
double MozJSImplScope::getNumber(const char* field) {
- MozJSEntry entry(this);
-
- return ObjectWrapper(_context, _global).getNumber(field);
+ return _runSafely([this, &field] { return ObjectWrapper(_context, _global).getNumber(field); });
}
int MozJSImplScope::getNumberInt(const char* field) {
- MozJSEntry entry(this);
-
- return ObjectWrapper(_context, _global).getNumberInt(field);
+ return _runSafely(
+ [this, &field] { return ObjectWrapper(_context, _global).getNumberInt(field); });
}
long long MozJSImplScope::getNumberLongLong(const char* field) {
- MozJSEntry entry(this);
-
- return ObjectWrapper(_context, _global).getNumberLongLong(field);
+ return _runSafely(
+ [this, &field] { return ObjectWrapper(_context, _global).getNumberLongLong(field); });
}
Decimal128 MozJSImplScope::getNumberDecimal(const char* field) {
- MozJSEntry entry(this);
-
- return ObjectWrapper(_context, _global).getNumberDecimal(field);
+ return _runSafely(
+ [this, &field] { return ObjectWrapper(_context, _global).getNumberDecimal(field); });
}
std::string MozJSImplScope::getString(const char* field) {
- MozJSEntry entry(this);
-
- return ObjectWrapper(_context, _global).getString(field);
+ return _runSafely([this, &field] { return ObjectWrapper(_context, _global).getString(field); });
}
bool MozJSImplScope::getBoolean(const char* field) {
- MozJSEntry entry(this);
-
- return ObjectWrapper(_context, _global).getBoolean(field);
+ return _runSafely(
+ [this, &field] { return ObjectWrapper(_context, _global).getBoolean(field); });
}
BSONObj MozJSImplScope::getObject(const char* field) {
- MozJSEntry entry(this);
-
- return ObjectWrapper(_context, _global).getObject(field);
+ return _runSafely([this, &field] { return ObjectWrapper(_context, _global).getObject(field); });
}
void MozJSImplScope::newFunction(StringData raw, JS::MutableHandleValue out) {
@@ -642,17 +649,15 @@ ScriptingFunction MozJSImplScope::_createFunction(const char* raw) {
}
void MozJSImplScope::setFunction(const char* field, const char* code) {
- MozJSEntry entry(this);
-
- JS::RootedValue fun(_context);
- _MozJSCreateFunction(code, &fun);
- ObjectWrapper(_context, _global).setValue(field, fun);
+ _runSafely([this, &field, &code] {
+ JS::RootedValue fun(_context);
+ _MozJSCreateFunction(code, &fun);
+ ObjectWrapper(_context, _global).setValue(field, fun);
+ });
}
void MozJSImplScope::rename(const char* from, const char* to) {
- MozJSEntry entry(this);
-
- ObjectWrapper(_context, _global).rename(from, to);
+ _runSafely([this, &from, &to] { ObjectWrapper(_context, _global).rename(from, to); });
}
int MozJSImplScope::invoke(ScriptingFunction func,
@@ -764,15 +769,15 @@ bool MozJSImplScope::exec(StringData code,
}
void MozJSImplScope::injectNative(const char* field, NativeFunction func, void* data) {
- MozJSEntry entry(this);
-
- JS::RootedObject obj(_context);
+ _runSafely([this, &field, &func, &data] {
+ JS::RootedObject obj(_context);
- NativeFunctionInfo::make(_context, &obj, func, data);
+ NativeFunctionInfo::make(_context, &obj, func, data);
- JS::RootedValue value(_context);
- value.setObjectOrNull(obj);
- ObjectWrapper(_context, _global).setValue(field, value);
+ JS::RootedValue value(_context);
+ value.setObjectOrNull(obj);
+ ObjectWrapper(_context, _global).setValue(field, value);
+ });
}
void MozJSImplScope::gc() {
@@ -781,63 +786,65 @@ void MozJSImplScope::gc() {
}
void MozJSImplScope::localConnectForDbEval(OperationContext* txn, const char* dbName) {
- MozJSEntry entry(this);
-
- if (_connectState == ConnectState::External)
- uasserted(12510, "externalSetup already called, can't call localConnect");
- if (_connectState == ConnectState::Local) {
- if (_localDBName == dbName)
- return;
- uasserted(12511,
- str::stream() << "localConnect previously called with name " << _localDBName);
- }
+ _runSafely([this, &txn, &dbName] {
+ if (_connectState == ConnectState::External)
+ uasserted(12510, "externalSetup already called, can't call localConnect");
+ if (_connectState == ConnectState::Local) {
+ if (_localDBName == dbName)
+ return;
+ uasserted(12511,
+ str::stream() << "localConnect previously called with name " << _localDBName);
+ }
- // NOTE: order is important here. the following methods must be called after
- // the above conditional statements.
+ // NOTE: order is important here. the following methods must be called after
+ // the above conditional statements.
- _connectState = ConnectState::Local;
- _localDBName = dbName;
+ _connectState = ConnectState::Local;
+ _localDBName = dbName;
- loadStored(txn);
+ loadStored(txn);
- // install db access functions in the global object
- installDBAccess();
+ // install db access functions in the global object
+ installDBAccess();
- // install the Mongo function object and instantiate the 'db' global
- _mongoLocalProto.install(_global);
- execCoreFiles();
+ // install the Mongo function object and instantiate the 'db' global
+ _mongoLocalProto.install(_global);
+ execCoreFiles();
- const char* const makeMongo = "const _mongo = new Mongo()";
- exec(makeMongo, "local connect 2", false, true, true, 0);
+ const char* const makeMongo = "const _mongo = new Mongo()";
+ exec(makeMongo, "local connect 2", false, true, true, 0);
- std::string makeDB = str::stream() << "const db = _mongo.getDB(\"" << dbName << "\");";
- exec(makeDB, "local connect 3", false, true, true, 0);
+ std::string makeDB = str::stream() << "const db = _mongo.getDB(\"" << dbName << "\");";
+ exec(makeDB, "local connect 3", false, true, true, 0);
+ });
}
void MozJSImplScope::externalSetup() {
- MozJSEntry entry(this);
- if (_connectState == ConnectState::External)
- return;
- if (_connectState == ConnectState::Local)
- uasserted(12512, "localConnect already called, can't call externalSetup");
+ _runSafely([&] {
+ if (_connectState == ConnectState::External)
+ return;
+ if (_connectState == ConnectState::Local)
+ uasserted(12512, "localConnect already called, can't call externalSetup");
- // install db access functions in the global object
- installDBAccess();
+ // install db access functions in the global object
+ installDBAccess();
- // install thread-related functions (e.g. _threadInject)
- installFork();
+ // install thread-related functions (e.g. _threadInject)
+ installFork();
- // install the Mongo function object
- _mongoExternalProto.install(_global);
- execCoreFiles();
- _connectState = ConnectState::External;
+ // install the Mongo function object
+ _mongoExternalProto.install(_global);
+ execCoreFiles();
+ _connectState = ConnectState::External;
+ });
}
void MozJSImplScope::reset() {
unregisterOperation();
_pendingKill.store(false);
_pendingGC.store(false);
+ _requireOwnedObjects = false;
advanceGeneration();
}
@@ -951,6 +958,14 @@ void MozJSImplScope::advanceGeneration() {
_generation++;
}
+void MozJSImplScope::requireOwnedObjects() {
+ _requireOwnedObjects = true;
+}
+
+bool MozJSImplScope::requiresOwnedObjects() const {
+ return _requireOwnedObjects;
+}
+
const std::string& MozJSImplScope::getParentStack() const {
return _parentStack;
}