summaryrefslogtreecommitdiff
path: root/src/lib/corelib/language/scriptengine.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/corelib/language/scriptengine.cpp')
-rw-r--r--src/lib/corelib/language/scriptengine.cpp63
1 files changed, 40 insertions, 23 deletions
diff --git a/src/lib/corelib/language/scriptengine.cpp b/src/lib/corelib/language/scriptengine.cpp
index bd7394b16..a8e6cb657 100644
--- a/src/lib/corelib/language/scriptengine.cpp
+++ b/src/lib/corelib/language/scriptengine.cpp
@@ -233,22 +233,7 @@ void ScriptEngine::reset()
void ScriptEngine::import(const FileContextBaseConstPtr &fileCtx, JSValue &targetObject,
ObserveMode observeMode)
{
- installImportFunctions(targetObject);
- m_currentDirPathStack.push(FileInfo::path(fileCtx->filePath()));
- m_extensionSearchPathsStack.push(fileCtx->searchPaths());
- m_observeMode = observeMode;
-
- for (const JsImport &jsImport : fileCtx->jsImports())
- import(jsImport, targetObject);
- if (m_observeMode == ObserveMode::Enabled) {
- for (JSValue &sv : m_requireResults)
- observeImport(sv);
- m_requireResults.clear();
- }
-
- m_currentDirPathStack.pop();
- m_extensionSearchPathsStack.pop();
- uninstallImportFunctions();
+ Importer(*this, fileCtx, targetObject, observeMode).run();
}
void ScriptEngine::import(const JsImport &jsImport, JSValue &targetObject)
@@ -266,9 +251,10 @@ void ScriptEngine::import(const JsImport &jsImport, JSValue &targetObject)
if (debugJSImports)
qDebug() << "[ENGINE] " << jsImport.filePaths << " (cache miss)";
- jsImportValue = JS_NewObject(m_context);
+ ScopedJsValue scopedImportValue(m_context, JS_NewObject(m_context));
for (const QString &filePath : jsImport.filePaths)
- importFile(filePath, jsImportValue);
+ importFile(filePath, scopedImportValue);
+ jsImportValue = scopedImportValue.release();
m_jsImportCache.insert(jsImport, jsImportValue);
std::vector<QString> &filePathsForScriptValue
= m_filePathsPerImport[jsObjectId(jsImportValue)];
@@ -444,7 +430,7 @@ void ScriptEngine::setEnvironment(const QProcessEnvironment &env)
m_environment = env;
}
-void ScriptEngine::importFile(const QString &filePath, JSValue &targetObject)
+void ScriptEngine::importFile(const QString &filePath, JSValue targetObject)
{
AccumulatingTimer importTimer(m_elapsedTimeImporting != -1 ? &m_elapsedTimeImporting : nullptr);
JSValue &evaluationResult = m_jsFileCache[filePath];
@@ -598,9 +584,9 @@ JSValue ScriptEngine::js_require(JSContext *ctx, JSValueConst this_val,
engine->m_logger.qbsDebug()
<< "[require] importing file " << filePath;
}
- JSValue obj = engine->newObject();
+ ScopedJsValue obj(engine->context(), engine->newObject());
engine->importFile(filePath, obj);
- values << obj;
+ values << obj.release();
filePaths.push_back(filePath);
}
} catch (const ErrorInfo &e) {
@@ -631,8 +617,9 @@ JSValue ScriptEngine::js_require(JSContext *ctx, JSValueConst this_val,
result = getJsProperty(ctx, func_data[0], scopeName);
if (JS_IsObject(result))
return result; // Same JS file imported from same qbs file via different JS files (e.g. codesign.js from DarwinGCC.qbs via gcc.js and darwin.js).
- result = engine->newObject();
- engine->importFile(filePath, result);
+ ScopedJsValue scopedResult(engine->context(), engine->newObject());
+ engine->importFile(filePath, scopedResult);
+ result = scopedResult.release();
setJsProperty(ctx, result, StringConstants::importScopeNamePropertyInternal(), scopeName);
setJsProperty(ctx, func_data[0], scopeName, result);
engine->m_requireResults.push_back(result);
@@ -1027,5 +1014,35 @@ void ScriptEngine::takeOwnership(JSValue v)
++m_evalResults[v];
}
+ScriptEngine::Importer::Importer(
+ ScriptEngine &engine, const FileContextBaseConstPtr &fileCtx, JSValue &targetObject,
+ ObserveMode observeMode)
+ : m_engine(engine), m_fileCtx(fileCtx), m_targetObject(targetObject)
+{
+ m_engine.installImportFunctions(targetObject);
+ m_engine.m_currentDirPathStack.push(FileInfo::path(fileCtx->filePath()));
+ m_engine.m_extensionSearchPathsStack.push(fileCtx->searchPaths());
+ m_engine.m_observeMode = observeMode;
+}
+
+ScriptEngine::Importer::~Importer()
+{
+ if (m_engine.m_observeMode == ObserveMode::Enabled)
+ m_engine.m_requireResults.clear();
+ m_engine.m_currentDirPathStack.pop();
+ m_engine.m_extensionSearchPathsStack.pop();
+ m_engine.uninstallImportFunctions();
+}
+
+void ScriptEngine::Importer::run()
+{
+ for (const JsImport &jsImport : m_fileCtx->jsImports())
+ m_engine.import(jsImport, m_targetObject);
+ if (m_engine.m_observeMode == ObserveMode::Enabled) {
+ for (JSValue &sv : m_engine.m_requireResults)
+ m_engine.observeImport(sv);
+ }
+}
+
} // namespace Internal
} // namespace qbs