Showing the changes to libraries/script-engine/src/ScriptEngine.cpp (1 of 38 files). Show all
Unified · Split
| @@ -0,0 +1,67 @@ | ||
| 1 | +// | |
| 2 | +// ScriptEngine.cpp | |
| 3 | +// libraries/script-engine/src | |
| 4 | +// | |
| 5 | +// Created by Brad Hefta-Gaub on 12/14/13. | |
| 6 | +// Copyright 2013 High Fidelity, Inc. | |
| 7 | +// Copyright 2020 Vircadia contributors. | |
| 8 | +// | |
| 9 | +// Distributed under the Apache License, Version 2.0. | |
| 10 | +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html | |
| 11 | +// | |
| 12 | + | |
| 13 | +#include "ScriptEngine.h" | |
| 14 | + | |
| 15 | +#include "ScriptEngineLogging.h" | |
| 16 | +#include "ScriptValue.h" | |
| 17 | +#include "qtscript/ScriptEngineQtScript.h" | |
| 18 | + | |
| 19 | +ScriptEnginePointer newScriptEngine(ScriptManager* manager) { | |
| 20 | + return QSharedPointer<ScriptEngineQtScript>(new ScriptEngineQtScript(manager)); | |
| 21 | +} | |
| 22 | + | |
| 23 | +ScriptValuePointer makeScopedHandlerObject(ScriptValuePointer scopeOrCallback, ScriptValuePointer methodOrName) { | |
| 24 | + auto engine = scopeOrCallback->engine(); | |
| 25 | + if (!engine) { | |
| 26 | + return scopeOrCallback; | |
| 27 | + } | |
| 28 | + ScriptValuePointer scope; | |
| 29 | + ScriptValuePointer callback = scopeOrCallback; | |
| 30 | + if (scopeOrCallback->isObject()) { | |
| 31 | + if (methodOrName->isString()) { | |
| 32 | + scope = scopeOrCallback; | |
| 33 | + callback = scope->property(methodOrName->toString()); | |
| 34 | + } else if (methodOrName->isFunction()) { | |
| 35 | + scope = scopeOrCallback; | |
| 36 | + callback = methodOrName; | |
| 37 | + } else if (!methodOrName->isValid()) { | |
| 38 | + // instantiate from an existing scoped handler object | |
| 39 | + if (scopeOrCallback->property("callback")->isFunction()) { | |
| 40 | + scope = scopeOrCallback->property("scope"); | |
| 41 | + callback = scopeOrCallback->property("callback"); | |
| 42 | + } | |
| 43 | + } | |
| 44 | + } | |
| 45 | + auto handler = engine->newObject(); | |
| 46 | + handler->setProperty("scope", scope); | |
| 47 | + handler->setProperty("callback", callback); | |
| 48 | + return handler; | |
| 49 | +} | |
| 50 | + | |
| 51 | +ScriptValuePointer callScopedHandlerObject(ScriptValuePointer handler, ScriptValuePointer err, ScriptValuePointer result) { | |
| 52 | + return handler->property("callback")->call(handler->property("scope"), ScriptValueList({ err, result })); | |
| 53 | +} | |
| 54 | + | |
| 55 | +bool ScriptEngine::IS_THREADSAFE_INVOCATION(const QString& method) { | |
| 56 | + QThread* thread = this->thread(); | |
| 57 | + if (QThread::currentThread() == thread) { | |
| 58 | + return true; | |
| 59 | + } | |
| 60 | + qCCritical(scriptengine) << QString("Scripting::%1 @ %2 -- ignoring thread-unsafe call from %3") | |
| 61 | + .arg(method) | |
| 62 | + .arg(thread ? thread->objectName() : "(!thread)") | |
| 63 | + .arg(QThread::currentThread()->objectName()); | |
| 64 | + qCDebug(scriptengine) << "(please resolve on the calling side by using invokeMethod, executeOnScriptThread, etc.)"; | |
| 65 | + Q_ASSERT(false); | |
| 66 | + return false; | |
| 67 | +} | |