Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 306 files). Show all
Unified · Split
| @@ -40,6 +40,7 @@ · 39 unmodified lines | ||
| 40 | 40 | |
| 41 | 41 | #include <shared/LocalFileAccessGate.h> |
| 42 | 42 | #include <shared/QtHelpers.h> |
| 43 | +#include <shared/AbstractLoggerInterface.h> | |
| 43 | 44 | #include <AudioConstants.h> |
| 44 | 45 | #include <AudioEffectOptions.h> |
| 45 | 46 | #include <AvatarData.h> |
| @@ -78,7 +79,6 @@ · 32 unmodified lines | ||
| 78 | 79 | #include "StackTestScriptingInterface.h" |
| 79 | 80 | #include "ModelScriptingInterface.h" |
| 80 | 81 | |
| 81 | − | |
| 82 | 82 | #include <Profile.h> |
| 83 | 83 | |
| 84 | 84 | #include "../../midi/src/Midi.h" // FIXME why won't a simpler include work? |
| @@ -108,7 +108,10 @@ int functionSignatureMetaID = qRegisterMetaType<QScriptEngine::FunctionSignature · 23 unmodified lines | ||
| 108 | 108 | |
| 109 | 109 | int scriptEnginePointerMetaID = qRegisterMetaType<ScriptEnginePointer>(); |
| 110 | 110 | |
| 111 | +Q_DECLARE_METATYPE(ExternalResource::Bucket); | |
| 112 | + | |
| 111 | 113 | static QScriptValue debugPrint(QScriptContext* context, QScriptEngine* engine) { |
| 114 | + // assemble the message by concatenating our arguments | |
| 112 | 115 | QString message = ""; |
| 113 | 116 | for (int i = 0; i < context->argumentCount(); i++) { |
| 114 | 117 | if (i > 0) { |
| @@ -117,12 +120,53 @@ static QScriptValue debugPrint(QScriptContext* context, QScriptEngine* engine) { · 2 unmodified lines | ||
| 117 | 120 | message += context->argument(i).toString(); |
| 118 | 121 | } |
| 119 | 122 | |
| 120 | − if (ScriptEngine *scriptEngine = qobject_cast<ScriptEngine*>(engine)) { | |
| 123 | + // was this generated by a script engine? If we don't recognize it then send the message and exit | |
| 124 | + ScriptEngine* scriptEngine = qobject_cast<ScriptEngine*>(engine); | |
| 125 | + if (!scriptEngine) { | |
| 126 | + qCDebug(scriptengine_script, "%s", qUtf8Printable(message)); | |
| 127 | + return QScriptValue(); | |
| 128 | + } | |
| 129 | + | |
| 130 | + // This message was sent by one of our script engines, let's try to see if we can find the source. | |
| 131 | + // Note that the first entry in the backtrace should be "print" and is somewhat useless to us | |
| 132 | + AbstractLoggerInterface* loggerInterface = AbstractLoggerInterface::get(); | |
| 133 | + if (loggerInterface->showSourceDebugging()) { | |
| 134 | + QScriptContext* userContext = context; | |
| 135 | + while (userContext && QScriptContextInfo(userContext).functionType() == QScriptContextInfo::NativeFunction) { | |
| 136 | + userContext = userContext->parentContext(); | |
| 137 | + } | |
| 138 | + QString location; | |
| 139 | + if (userContext) { | |
| 140 | + QScriptContextInfo contextInfo(userContext); | |
| 141 | + QString fileName = contextInfo.fileName(); | |
| 142 | + int lineNumber = contextInfo.lineNumber(); | |
| 143 | + QString functionName = contextInfo.functionName(); | |
| 144 | + | |
| 145 | + location = functionName; | |
| 146 | + if (!fileName.isEmpty()) { | |
| 147 | + if (location.isEmpty()) { | |
| 148 | + location = fileName; | |
| 149 | + } else { | |
| 150 | + location = QString("%1 at %2").arg(location).arg(fileName); | |
| 151 | + } | |
| 152 | + } | |
| 153 | + if (lineNumber != -1) { | |
| 154 | + location = QString("%1:%2").arg(location).arg(lineNumber); | |
| 155 | + } | |
| 156 | + } | |
| 157 | + if (location.isEmpty()) { | |
| 158 | + location = scriptEngine->getFilename(); | |
| 159 | + } | |
| 160 | + | |
| 161 | + // give the script engine a chance to notify the system about this message | |
| 162 | + scriptEngine->print(message); | |
| 163 | + | |
| 164 | + // send the message to debug log | |
| 165 | + qCDebug(scriptengine_script, "[%s] %s", qUtf8Printable(location), qUtf8Printable(message)); | |
| 166 | + } else { | |
| 121 | 167 | scriptEngine->print(message); |
| 122 | 168 | // prefix the script engine name to help disambiguate messages in the main debug log |
| 123 | 169 | qCDebug(scriptengine_script, "[%s] %s", qUtf8Printable(scriptEngine->getFilename()), qUtf8Printable(message)); |
| 124 | − } else { | |
| 125 | − qCDebug(scriptengine_script, "%s", qUtf8Printable(message)); | |
| 126 | 170 | } |
| 127 | 171 | |
| 128 | 172 | return QScriptValue(); |
| @@ -663,6 +707,14 @@ void avatarDataFromScriptValue(const QScriptValue& object, ScriptAvatarData*& ou · 534 unmodified lines | ||
| 663 | 707 | out = nullptr; |
| 664 | 708 | } |
| 665 | 709 | |
| 710 | +QScriptValue externalResourceBucketToScriptValue(QScriptEngine* engine, ExternalResource::Bucket const& in) { | |
| 711 | + return QScriptValue((int)in); | |
| 712 | +} | |
| 713 | + | |
| 714 | +void externalResourceBucketFromScriptValue(const QScriptValue& object, ExternalResource::Bucket& out) { | |
| 715 | + out = static_cast<ExternalResource::Bucket>(object.toInt32()); | |
| 716 | +} | |
| 717 | + | |
| 666 | 718 | void ScriptEngine::resetModuleCache(bool deleteScriptCache) { |
| 667 | 719 | if (QThread::currentThread() != thread()) { |
| 668 | 720 | executeOnScriptThread([=]() { resetModuleCache(deleteScriptCache); }); |
| @@ -770,6 +822,9 @@ void ScriptEngine::init() { · 101 unmodified lines | ||
| 770 | 822 | resetModuleCache(); |
| 771 | 823 | } |
| 772 | 824 | |
| 825 | + qScriptRegisterMetaType(this, externalResourceBucketToScriptValue, externalResourceBucketFromScriptValue); | |
| 826 | + registerEnum("Script.ExternalPaths", QMetaEnum::fromType<ExternalResource::Bucket>()); | |
| 827 | + | |
| 773 | 828 | registerGlobalObject("Audio", DependencyManager::get<AudioScriptingInterface>().data()); |
| 774 | 829 | |
| 775 | 830 | registerGlobalObject("Midi", DependencyManager::get<Midi>().data()); |
| @@ -827,6 +882,19 @@ void ScriptEngine::init() { · 51 unmodified lines | ||
| 827 | 882 | })); |
| 828 | 883 | } |
| 829 | 884 | |
| 885 | +void ScriptEngine::registerEnum(const QString& enumName, QMetaEnum newEnum) { | |
| 886 | + if (!newEnum.isValid()) { | |
| 887 | + qCCritical(scriptengine) << "registerEnum called on invalid enum with name " << enumName; | |
| 888 | + return; | |
| 889 | + } | |
| 890 | + | |
| 891 | + for (int i = 0; i < newEnum.keyCount(); i++) { | |
| 892 | + const char* keyName = newEnum.key(i); | |
| 893 | + QString fullName = enumName + "." + keyName; | |
| 894 | + registerValue(fullName, newEnum.keyToValue(keyName)); | |
| 895 | + } | |
| 896 | +} | |
| 897 | + | |
| 830 | 898 | void ScriptEngine::registerValue(const QString& valueName, QScriptValue value) { |
| 831 | 899 | if (QThread::currentThread() != thread()) { |
| 832 | 900 | #ifdef THREAD_DEBUGGING |
| @@ -2859,3 +2927,7 @@ void ScriptEngine::callEntityScriptMethod(const EntityItemID& entityID, const QS · 2026 unmodified lines | ||
| 2859 | 2927 | } |
| 2860 | 2928 | } |
| 2861 | 2929 | } |
| 2930 | + | |
| 2931 | +QString ScriptEngine::getExternalPath(ExternalResource::Bucket bucket, const QString& path) { | |
| 2932 | + return ExternalResource::getInstance()->getUrl(bucket, path); | |
| 2933 | +} | |