History

* rework _requireResolve error throws (to avoid using lambda w/default args -- which vc++ didn't care for) * add ExceptionEmitters to require/resolve so errors get logged even if invoked indpendently from a script

humbletim committed 9 years ago

Commit 32e450e1 parent efc61c21 file · +24 −24Browse files

Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 1 file). Show all

Unified · Split

libraries/script-engine/src/ScriptEngine.cpp+24−24
@@ -1361,28 +1361,22 @@ void ScriptEngine::print(const QString& message) { · 1360 unmodified lines
13611361 QString ScriptEngine::_requireResolve(const QString& moduleId, const QString& relativeTo) {
13621362 QUrl defaultScriptsLoc = defaultScriptsLocation();
13631363 QUrl url(moduleId);
1364+ ExceptionEmitter tryCatcher(this, __FUNCTION__);
13641365
1365− // helper to generate an exception and return a null string
1366− auto resolverException = [=](const QString& detail, const QString& type = "Error") -> QString {
1367− auto displayId = moduleId;
1368− if (displayId.length() > MAX_DEBUG_VALUE_LENGTH) {
1369− displayId = displayId.mid(0, MAX_DEBUG_VALUE_LENGTH) + "...";
1370− }
1371− currentContext()->throwValue(makeError(
1372− QString("Cannot find module '%1' (%2)")
1373− .arg(displayId)
1374− .arg(detail), type));
1375− return QString();
1376− };
1366+ auto displayId = moduleId;
1367+ if (displayId.length() > MAX_DEBUG_VALUE_LENGTH) {
1368+ displayId = displayId.mid(0, MAX_DEBUG_VALUE_LENGTH) + "...";
1369+ }
1370+ auto message = QString("Cannot find module '%1' (%2)").arg(displayId);
1371+ auto ctx = currentContext();
13771372
13781373 // de-fuzz the input a little by restricting to rational sizes
13791374 auto idLength = url.toString().length();
13801375 if (idLength < 1 || idLength > MAX_MODULE_ID_LENTGH) {
1381− return resolverException(
1382− QString("rejecting invalid module id size (%1 chars [1,%2])")
1383− .arg(idLength).arg(MAX_MODULE_ID_LENTGH),
1384− "RangeError"
1385− );
1376+ auto details = QString("rejecting invalid module id size (%1 chars [1,%2])")
1377+ .arg(idLength).arg(MAX_MODULE_ID_LENTGH);
1378+ ctx->throwValue(makeError(message.arg(details), "RangeError"));
1379+ return nullptr;
13861380 }
13871381
13881382 // this regex matches: absolute, dotted or path-like URLs
@@ -1402,12 +1396,14 @@ QString ScriptEngine::_requireResolve(const QString& moduleId, const QString& re · 13 unmodified lines
14021396 url = defaultScriptsLoc;
14031397 url.setPath(systemModulePath);
14041398 if (!QFileInfo(url.toLocalFile()).isFile()) {
1405− return resolverException("system module not found");
1399+ ctx->throwValue(makeError(message.arg("system module not found")));
1400+ return nullptr;
14061401 }
14071402 }
14081403
14091404 if (url.isRelative()) {
1410− return resolverException("could not resolve module id");
1405+ ctx->throwValue(makeError(message.arg("could not resolve module id")));
1406+ return nullptr;
14111407 }
14121408
14131409 // if it looks like a local file, verify that it's an allowed path and really a file
@@ -1420,18 +1416,21 @@ QString ScriptEngine::_requireResolve(const QString& moduleId, const QString& re · 6 unmodified lines
14201416
14211417 bool disallowOutsideFiles = !defaultScriptsLocation().isParentOf(canonical) && !currentSandboxURL.isLocalFile();
14221418 if (disallowOutsideFiles && !PathUtils::isDescendantOf(canonical, currentSandboxURL)) {
1423− return resolverException(
1419+ ctx->throwValue(makeError(message.arg(
14241420 QString("path '%1' outside of origin script '%2' '%3'")
14251421 .arg(PathUtils::stripFilename(url))
14261422 .arg(PathUtils::stripFilename(currentSandboxURL))
14271423 .arg(canonical.toString())
1428− );
1424+ )));
1425+ return nullptr;
14291426 }
14301427 if (!file.exists()) {
1431− return resolverException("path does not exist: " + url.toLocalFile());
1428+ ctx->throwValue(makeError(message.arg("path does not exist: " + url.toLocalFile())));
1429+ return nullptr;
14321430 }
14331431 if (!file.isFile()) {
1434− return resolverException("path is not a file: " + url.toLocalFile());
1432+ ctx->throwValue(makeError(message.arg("path is not a file: " + url.toLocalFile())));
1433+ return nullptr;
14351434 }
14361435 }
14371436
@@ -1611,6 +1610,7 @@ QScriptValue ScriptEngine::require(const QString& moduleId) { · 173 unmodified lines
16111610 // serialize require calls so the ordering/caching works correctly across multiple Entities
16121611 // note: this is a Recursive mutex so nested require's should not affected
16131612 QMutexLocker locker(&_requireLock);
1613+ ExceptionEmitter tryCatcher(this, __FUNCTION__);
16141614 // _requireDepth++;
16151615 // QObject autoDecrement;connect(&autoDecrement, &QObject::destroyed, this, [this](){ _requireDepth--; });
16161616
@@ -1632,7 +1632,7 @@ QScriptValue ScriptEngine::require(const QString& moduleId) { · 15 unmodified lines
16321632
16331633 // start by resolving the moduleId into a fully-qualified path/URL
16341634 QString modulePath = _requireResolve(moduleId);
1635− if (hasUncaughtException()) {
1635+ if (modulePath.isNull() || tryCatcher.hasPending()) {
16361636 // the resolver already threw an exception -- bail early
16371637 return nullValue();
16381638 }