Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 258 files). Show all
Unified · Split
| @@ -87,7 +87,6 @@ ScriptEngine::ScriptEngine(const QString& scriptContents, const QString& fileNam · 86 unmodified lines | ||
| 87 | 87 | _isFinished(false), |
| 88 | 88 | _isRunning(false), |
| 89 | 89 | _isInitialized(false), |
| 90 | − _engine(), | |
| 91 | 90 | _isAvatar(false), |
| 92 | 91 | _avatarIdentityTimer(NULL), |
| 93 | 92 | _avatarBillboardTimer(NULL), |
| @@ -113,7 +112,6 @@ ScriptEngine::ScriptEngine(const QUrl& scriptURL, · 19 unmodified lines | ||
| 113 | 112 | _isFinished(false), |
| 114 | 113 | _isRunning(false), |
| 115 | 114 | _isInitialized(false), |
| 116 | − _engine(), | |
| 117 | 115 | _isAvatar(false), |
| 118 | 116 | _avatarIdentityTimer(NULL), |
| 119 | 117 | _avatarBillboardTimer(NULL), |
| @@ -158,7 +156,7 @@ ScriptEngine::ScriptEngine(const QUrl& scriptURL, · 38 unmodified lines | ||
| 158 | 156 | } else { |
| 159 | 157 | NetworkAccessManager& networkAccessManager = NetworkAccessManager::getInstance(); |
| 160 | 158 | QNetworkReply* reply = networkAccessManager.get(QNetworkRequest(url)); |
| 161 | − qDebug() << "Downloading included script at" << url; | |
| 159 | + qDebug() << "Downloading script at" << url; | |
| 162 | 160 | QEventLoop loop; |
| 163 | 161 | QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); |
| 164 | 162 | loop.exec(); |
| @@ -194,7 +192,7 @@ void ScriptEngine::setAvatarData(AvatarData* avatarData, const QString& objectNa · 29 unmodified lines | ||
| 194 | 192 | _avatarData = avatarData; |
| 195 | 193 | |
| 196 | 194 | // remove the old Avatar property, if it exists |
| 197 | − _engine.globalObject().setProperty(objectName, QScriptValue()); | |
| 195 | + globalObject().setProperty(objectName, QScriptValue()); | |
| 198 | 196 | |
| 199 | 197 | // give the script engine the new Avatar script property |
| 200 | 198 | registerGlobalObject(objectName, _avatarData); |
| @@ -202,7 +200,7 @@ void ScriptEngine::setAvatarData(AvatarData* avatarData, const QString& objectNa · 1 unmodified line | ||
| 202 | 200 | |
| 203 | 201 | void ScriptEngine::setAvatarHashMap(AvatarHashMap* avatarHashMap, const QString& objectName) { |
| 204 | 202 | // remove the old Avatar property, if it exists |
| 205 | − _engine.globalObject().setProperty(objectName, QScriptValue()); | |
| 203 | + globalObject().setProperty(objectName, QScriptValue()); | |
| 206 | 204 | |
| 207 | 205 | // give the script engine the new avatar hash map |
| 208 | 206 | registerGlobalObject(objectName, avatarHashMap); |
| @@ -231,48 +229,48 @@ void ScriptEngine::init() { · 22 unmodified lines | ||
| 231 | 229 | _particlesScriptingInterface.init(); |
| 232 | 230 | |
| 233 | 231 | // register various meta-types |
| 234 | − registerMetaTypes(&_engine); | |
| 235 | − registerMIDIMetaTypes(&_engine); | |
| 236 | − registerVoxelMetaTypes(&_engine); | |
| 237 | − registerEventTypes(&_engine); | |
| 238 | − registerMenuItemProperties(&_engine); | |
| 239 | − registerAnimationTypes(&_engine); | |
| 240 | − registerAvatarTypes(&_engine); | |
| 241 | − Bitstream::registerTypes(&_engine); | |
| 242 | − | |
| 243 | − qScriptRegisterMetaType(&_engine, ParticlePropertiesToScriptValue, ParticlePropertiesFromScriptValue); | |
| 244 | − qScriptRegisterMetaType(&_engine, ParticleIDtoScriptValue, ParticleIDfromScriptValue); | |
| 245 | − qScriptRegisterSequenceMetaType<QVector<ParticleID> >(&_engine); | |
| 246 | − | |
| 247 | − qScriptRegisterMetaType(&_engine, ModelItemPropertiesToScriptValue, ModelItemPropertiesFromScriptValue); | |
| 248 | − qScriptRegisterMetaType(&_engine, ModelItemIDtoScriptValue, ModelItemIDfromScriptValue); | |
| 249 | − qScriptRegisterMetaType(&_engine, RayToModelIntersectionResultToScriptValue, RayToModelIntersectionResultFromScriptValue); | |
| 250 | − qScriptRegisterSequenceMetaType<QVector<ModelItemID> >(&_engine); | |
| 251 | − | |
| 252 | − qScriptRegisterSequenceMetaType<QVector<glm::vec2> >(&_engine); | |
| 253 | − qScriptRegisterSequenceMetaType<QVector<glm::quat> >(&_engine); | |
| 254 | − qScriptRegisterSequenceMetaType<QVector<QString> >(&_engine); | |
| 255 | − | |
| 256 | − QScriptValue xmlHttpRequestConstructorValue = _engine.newFunction(XMLHttpRequestClass::constructor); | |
| 257 | − _engine.globalObject().setProperty("XMLHttpRequest", xmlHttpRequestConstructorValue); | |
| 258 | − | |
| 259 | − QScriptValue printConstructorValue = _engine.newFunction(debugPrint); | |
| 260 | − _engine.globalObject().setProperty("print", printConstructorValue); | |
| 261 | − | |
| 262 | − QScriptValue soundConstructorValue = _engine.newFunction(soundConstructor); | |
| 263 | − QScriptValue soundMetaObject = _engine.newQMetaObject(&Sound::staticMetaObject, soundConstructorValue); | |
| 264 | − _engine.globalObject().setProperty("Sound", soundMetaObject); | |
| 265 | − | |
| 266 | − QScriptValue injectionOptionValue = _engine.scriptValueFromQMetaObject<AudioInjectorOptions>(); | |
| 267 | − _engine.globalObject().setProperty("AudioInjectionOptions", injectionOptionValue); | |
| 268 | − | |
| 269 | − QScriptValue localVoxelsValue = _engine.scriptValueFromQMetaObject<LocalVoxels>(); | |
| 270 | − _engine.globalObject().setProperty("LocalVoxels", localVoxelsValue); | |
| 232 | + registerMetaTypes(this); | |
| 233 | + registerMIDIMetaTypes(this); | |
| 234 | + registerVoxelMetaTypes(this); | |
| 235 | + registerEventTypes(this); | |
| 236 | + registerMenuItemProperties(this); | |
| 237 | + registerAnimationTypes(this); | |
| 238 | + registerAvatarTypes(this); | |
| 239 | + Bitstream::registerTypes(this); | |
| 240 | + | |
| 241 | + qScriptRegisterMetaType(this, ParticlePropertiesToScriptValue, ParticlePropertiesFromScriptValue); | |
| 242 | + qScriptRegisterMetaType(this, ParticleIDtoScriptValue, ParticleIDfromScriptValue); | |
| 243 | + qScriptRegisterSequenceMetaType<QVector<ParticleID> >(this); | |
| 244 | + | |
| 245 | + qScriptRegisterMetaType(this, ModelItemPropertiesToScriptValue, ModelItemPropertiesFromScriptValue); | |
| 246 | + qScriptRegisterMetaType(this, ModelItemIDtoScriptValue, ModelItemIDfromScriptValue); | |
| 247 | + qScriptRegisterMetaType(this, RayToModelIntersectionResultToScriptValue, RayToModelIntersectionResultFromScriptValue); | |
| 248 | + qScriptRegisterSequenceMetaType<QVector<ModelItemID> >(this); | |
| 249 | + | |
| 250 | + qScriptRegisterSequenceMetaType<QVector<glm::vec2> >(this); | |
| 251 | + qScriptRegisterSequenceMetaType<QVector<glm::quat> >(this); | |
| 252 | + qScriptRegisterSequenceMetaType<QVector<QString> >(this); | |
| 253 | + | |
| 254 | + QScriptValue xmlHttpRequestConstructorValue = newFunction(XMLHttpRequestClass::constructor); | |
| 255 | + globalObject().setProperty("XMLHttpRequest", xmlHttpRequestConstructorValue); | |
| 256 | + | |
| 257 | + QScriptValue printConstructorValue = newFunction(debugPrint); | |
| 258 | + globalObject().setProperty("print", printConstructorValue); | |
| 259 | + | |
| 260 | + QScriptValue soundConstructorValue = newFunction(soundConstructor); | |
| 261 | + QScriptValue soundMetaObject = newQMetaObject(&Sound::staticMetaObject, soundConstructorValue); | |
| 262 | + globalObject().setProperty("Sound", soundMetaObject); | |
| 263 | + | |
| 264 | + QScriptValue injectionOptionValue = scriptValueFromQMetaObject<AudioInjectorOptions>(); | |
| 265 | + globalObject().setProperty("AudioInjectionOptions", injectionOptionValue); | |
| 266 | + | |
| 267 | + QScriptValue localVoxelsValue = scriptValueFromQMetaObject<LocalVoxels>(); | |
| 268 | + globalObject().setProperty("LocalVoxels", localVoxelsValue); | |
| 271 | 269 | |
| 272 | − qScriptRegisterMetaType(&_engine, injectorToScriptValue, injectorFromScriptValue); | |
| 273 | − qScriptRegisterMetaType( &_engine, injectorToScriptValueInputController, injectorFromScriptValueInputController); | |
| 270 | + qScriptRegisterMetaType(this, injectorToScriptValue, injectorFromScriptValue); | |
| 271 | + qScriptRegisterMetaType( this, injectorToScriptValueInputController, injectorFromScriptValueInputController); | |
| 274 | 272 | |
| 275 | − qScriptRegisterMetaType(&_engine, animationDetailsToScriptValue, animationDetailsFromScriptValue); | |
| 273 | + qScriptRegisterMetaType(this, animationDetailsToScriptValue, animationDetailsFromScriptValue); | |
| 276 | 274 | |
| 277 | 275 | registerGlobalObject("Script", this); |
| 278 | 276 | registerGlobalObject("Audio", &_audioScriptingInterface); |
| @@ -287,15 +285,14 @@ void ScriptEngine::init() { · 8 unmodified lines | ||
| 287 | 285 | registerGlobalObject("Voxels", &_voxelsScriptingInterface); |
| 288 | 286 | |
| 289 | 287 | // constants |
| 290 | − QScriptValue globalObject = _engine.globalObject(); | |
| 291 | − globalObject.setProperty("TREE_SCALE", _engine.newVariant(QVariant(TREE_SCALE))); | |
| 292 | − globalObject.setProperty("COLLISION_GROUP_ENVIRONMENT", _engine.newVariant(QVariant(COLLISION_GROUP_ENVIRONMENT))); | |
| 293 | − globalObject.setProperty("COLLISION_GROUP_AVATARS", _engine.newVariant(QVariant(COLLISION_GROUP_AVATARS))); | |
| 294 | − globalObject.setProperty("COLLISION_GROUP_VOXELS", _engine.newVariant(QVariant(COLLISION_GROUP_VOXELS))); | |
| 295 | − globalObject.setProperty("COLLISION_GROUP_PARTICLES", _engine.newVariant(QVariant(COLLISION_GROUP_PARTICLES))); | |
| 288 | + globalObject().setProperty("TREE_SCALE", newVariant(QVariant(TREE_SCALE))); | |
| 289 | + globalObject().setProperty("COLLISION_GROUP_ENVIRONMENT", newVariant(QVariant(COLLISION_GROUP_ENVIRONMENT))); | |
| 290 | + globalObject().setProperty("COLLISION_GROUP_AVATARS", newVariant(QVariant(COLLISION_GROUP_AVATARS))); | |
| 291 | + globalObject().setProperty("COLLISION_GROUP_VOXELS", newVariant(QVariant(COLLISION_GROUP_VOXELS))); | |
| 292 | + globalObject().setProperty("COLLISION_GROUP_PARTICLES", newVariant(QVariant(COLLISION_GROUP_PARTICLES))); | |
| 296 | 293 | |
| 297 | − globalObject.setProperty("AVATAR_MOTION_OBEY_LOCAL_GRAVITY", _engine.newVariant(QVariant(AVATAR_MOTION_OBEY_LOCAL_GRAVITY))); | |
| 298 | − globalObject.setProperty("AVATAR_MOTION_OBEY_ENVIRONMENTAL_GRAVITY", _engine.newVariant(QVariant(AVATAR_MOTION_OBEY_ENVIRONMENTAL_GRAVITY))); | |
| 294 | + globalObject().setProperty("AVATAR_MOTION_OBEY_LOCAL_GRAVITY", newVariant(QVariant(AVATAR_MOTION_OBEY_LOCAL_GRAVITY))); | |
| 295 | + globalObject().setProperty("AVATAR_MOTION_OBEY_ENVIRONMENTAL_GRAVITY", newVariant(QVariant(AVATAR_MOTION_OBEY_ENVIRONMENTAL_GRAVITY))); | |
| 299 | 296 | |
| 300 | 297 | // let the VoxelPacketSender know how frequently we plan to call it |
| 301 | 298 | _voxelsScriptingInterface.getVoxelPacketSender()->setProcessCallIntervalHint(SCRIPT_DATA_CALLBACK_USECS); |
| @@ -304,8 +301,8 @@ void ScriptEngine::init() { · 2 unmodified lines | ||
| 304 | 301 | |
| 305 | 302 | QScriptValue ScriptEngine::registerGlobalObject(const QString& name, QObject* object) { |
| 306 | 303 | if (object) { |
| 307 | − QScriptValue value = _engine.newQObject(object); | |
| 308 | − _engine.globalObject().setProperty(name, value); | |
| 304 | + QScriptValue value = newQObject(object); | |
| 305 | + globalObject().setProperty(name, value); | |
| 309 | 306 | return value; |
| 310 | 307 | } |
| 311 | 308 | return QScriptValue::NullValue; |
| @@ -313,15 +310,15 @@ QScriptValue ScriptEngine::registerGlobalObject(const QString& name, QObject* ob · 1 unmodified line | ||
| 313 | 310 | |
| 314 | 311 | void ScriptEngine::registerGetterSetter(const QString& name, QScriptEngine::FunctionSignature getter, |
| 315 | 312 | QScriptEngine::FunctionSignature setter, QScriptValue object) { |
| 316 | − QScriptValue setterFunction = _engine.newFunction(setter, 1); | |
| 317 | − QScriptValue getterFunction = _engine.newFunction(getter); | |
| 313 | + QScriptValue setterFunction = newFunction(setter, 1); | |
| 314 | + QScriptValue getterFunction = newFunction(getter); | |
| 318 | 315 | |
| 319 | 316 | if (!object.isNull()) { |
| 320 | 317 | object.setProperty(name, setterFunction, QScriptValue::PropertySetter); |
| 321 | 318 | object.setProperty(name, getterFunction, QScriptValue::PropertyGetter); |
| 322 | 319 | } else { |
| 323 | − _engine.globalObject().setProperty(name, setterFunction, QScriptValue::PropertySetter); | |
| 324 | − _engine.globalObject().setProperty(name, getterFunction, QScriptValue::PropertyGetter); | |
| 320 | + globalObject().setProperty(name, setterFunction, QScriptValue::PropertySetter); | |
| 321 | + globalObject().setProperty(name, getterFunction, QScriptValue::PropertyGetter); | |
| 325 | 322 | } |
| 326 | 323 | } |
| 327 | 324 | |
| @@ -330,25 +327,24 @@ void ScriptEngine::evaluate() { · 2 unmodified lines | ||
| 330 | 327 | init(); |
| 331 | 328 | } |
| 332 | 329 | |
| 333 | − QScriptValue result = _engine.evaluate(_scriptContents); | |
| 330 | + QScriptValue result = evaluate(_scriptContents); | |
| 334 | 331 | |
| 335 | − if (_engine.hasUncaughtException()) { | |
| 336 | − int line = _engine.uncaughtExceptionLineNumber(); | |
| 332 | + if (hasUncaughtException()) { | |
| 333 | + int line = uncaughtExceptionLineNumber(); | |
| 337 | 334 | qDebug() << "Uncaught exception at (" << _fileNameString << ") line" << line << ":" << result.toString(); |
| 338 | 335 | emit errorMessage("Uncaught exception at (" + _fileNameString + ") line" + QString::number(line) + ":" + result.toString()); |
| 339 | − _engine.clearExceptions(); | |
| 336 | + clearExceptions(); | |
| 340 | 337 | } |
| 341 | 338 | } |
| 342 | 339 | |
| 343 | 340 | QScriptValue ScriptEngine::evaluate(const QString& program, const QString& fileName, int lineNumber) { |
| 344 | − QScriptValue result = _engine.evaluate(program, fileName, lineNumber); | |
| 345 | − bool hasUncaughtException = _engine.hasUncaughtException(); | |
| 346 | − if (hasUncaughtException) { | |
| 347 | − int line = _engine.uncaughtExceptionLineNumber(); | |
| 341 | + QScriptValue result = QScriptEngine::evaluate(program, fileName, lineNumber); | |
| 342 | + if (hasUncaughtException()) { | |
| 343 | + int line = uncaughtExceptionLineNumber(); | |
| 348 | 344 | qDebug() << "Uncaught exception at (" << _fileNameString << ") line" << line << ": " << result.toString(); |
| 349 | 345 | } |
| 350 | − emit evaluationFinished(result, hasUncaughtException); | |
| 351 | − _engine.clearExceptions(); | |
| 346 | + emit evaluationFinished(result, hasUncaughtException()); | |
| 347 | + clearExceptions(); | |
| 352 | 348 | return result; |
| 353 | 349 | } |
| 354 | 350 | |
| @@ -372,12 +368,12 @@ void ScriptEngine::run() { · 17 unmodified lines | ||
| 372 | 368 | _isFinished = false; |
| 373 | 369 | emit runningStateChanged(); |
| 374 | 370 | |
| 375 | − QScriptValue result = _engine.evaluate(_scriptContents); | |
| 376 | − if (_engine.hasUncaughtException()) { | |
| 377 | − int line = _engine.uncaughtExceptionLineNumber(); | |
| 371 | + QScriptValue result = evaluate(_scriptContents); | |
| 372 | + if (hasUncaughtException()) { | |
| 373 | + int line = uncaughtExceptionLineNumber(); | |
| 378 | 374 | qDebug() << "Uncaught exception at (" << _fileNameString << ") line" << line << ":" << result.toString(); |
| 379 | 375 | emit errorMessage("Uncaught exception at (" + _fileNameString + ") line" + QString::number(line) + ":" + result.toString()); |
| 380 | − _engine.clearExceptions(); | |
| 376 | + clearExceptions(); | |
| 381 | 377 | } |
| 382 | 378 | |
| 383 | 379 | QElapsedTimer startTime; |
| @@ -532,11 +528,11 @@ void ScriptEngine::run() { · 148 unmodified lines | ||
| 532 | 528 | qint64 now = usecTimestampNow(); |
| 533 | 529 | float deltaTime = (float) (now - lastUpdate) / (float) USECS_PER_SECOND; |
| 534 | 530 | |
| 535 | − if (_engine.hasUncaughtException()) { | |
| 536 | − int line = _engine.uncaughtExceptionLineNumber(); | |
| 537 | − qDebug() << "Uncaught exception at (" << _fileNameString << ") line" << line << ":" << _engine.uncaughtException().toString(); | |
| 538 | − emit errorMessage("Uncaught exception at (" + _fileNameString + ") line" + QString::number(line) + ":" + _engine.uncaughtException().toString()); | |
| 539 | − _engine.clearExceptions(); | |
| 531 | + if (hasUncaughtException()) { | |
| 532 | + int line = uncaughtExceptionLineNumber(); | |
| 533 | + qDebug() << "Uncaught exception at (" << _fileNameString << ") line" << line << ":" << uncaughtException().toString(); | |
| 534 | + emit errorMessage("Uncaught exception at (" + _fileNameString + ") line" + QString::number(line) + ":" + uncaughtException().toString()); | |
| 535 | + clearExceptions(); | |
| 540 | 536 | } |
| 541 | 537 | |
| 542 | 538 | emit update(deltaTime); |
| @@ -685,24 +681,29 @@ void ScriptEngine::include(const QString& includeFile) { · 142 unmodified lines | ||
| 685 | 681 | #endif |
| 686 | 682 | QFile scriptFile(fileName); |
| 687 | 683 | if (scriptFile.open(QFile::ReadOnly | QFile::Text)) { |
| 688 | − qDebug() << "Loading file:" << fileName; | |
| 684 | + qDebug() << "Including file:" << fileName; | |
| 689 | 685 | QTextStream in(&scriptFile); |
| 690 | 686 | includeContents = in.readAll(); |
| 691 | 687 | } else { |
| 692 | − qDebug() << "ERROR Loading file:" << fileName; | |
| 693 | − emit errorMessage("ERROR Loading file:" + fileName); | |
| 688 | + qDebug() << "ERROR Including file:" << fileName; | |
| 689 | + emit errorMessage("ERROR Including file:" + fileName); | |
| 694 | 690 | } |
| 695 | 691 | } |
| 696 | 692 | |
| 697 | − QScriptValue result = _engine.evaluate(includeContents); | |
| 698 | − if (_engine.hasUncaughtException()) { | |
| 699 | − int line = _engine.uncaughtExceptionLineNumber(); | |
| 693 | + QScriptValue result = evaluate(includeContents); | |
| 694 | + if (hasUncaughtException()) { | |
| 695 | + int line = uncaughtExceptionLineNumber(); | |
| 700 | 696 | qDebug() << "Uncaught exception at (" << includeFile << ") line" << line << ":" << result.toString(); |
| 701 | 697 | emit errorMessage("Uncaught exception at (" + includeFile + ") line" + QString::number(line) + ":" + result.toString()); |
| 702 | − _engine.clearExceptions(); | |
| 698 | + clearExceptions(); | |
| 703 | 699 | } |
| 704 | 700 | } |
| 705 | 701 | |
| 702 | +void ScriptEngine::load(const QString& loadFile) { | |
| 703 | + QUrl url = resolveInclude(loadFile); | |
| 704 | + emit loadScript(url.toString()); | |
| 705 | +} | |
| 706 | + | |
| 706 | 707 | void ScriptEngine::nodeKilled(SharedNodePointer node) { |
| 707 | 708 | _outgoingScriptAudioSequenceNumbers.remove(node->getUUID()); |
| 708 | 709 | } |