From 6388f573194aaf332956a1a46d38a0b12b8f0fc1 Mon Sep 17 00:00:00 2001 From: tkawata Date: Wed, 30 May 2012 00:40:55 +0900 Subject: [PATCH] - Modified for better error message for the failure of JavaScript execution in initialization time. - Delayed the timing of doInit() invocation until the cell to cell connections are established. Now doInit() method can query the receptors the cell has. --- Source/DNContainerBuilder.cpp | 4 +-- Source/DNEngine.cpp | 2 ++ Source/QtScript/dnqscellcode.cpp | 62 ++++++++++++++++++++++++++++++++++------ Source/TKCell.cpp | 5 +--- Source/TKContainer.cpp | 10 +++++++ Source/TKContainer.h | 1 + 6 files changed, 70 insertions(+), 14 deletions(-) diff --git a/Source/DNContainerBuilder.cpp b/Source/DNContainerBuilder.cpp index ecce3c5..28fa65f 100644 --- a/Source/DNContainerBuilder.cpp +++ b/Source/DNContainerBuilder.cpp @@ -102,13 +102,13 @@ bool DNContainerBuilder::buildContainerFromXHTML(const char* docRoot) { if (!orgCell->connectTo(connInfo->connectionName, tgtCell)) { - std::string message = std::string("Failed to make connection. Receptor name for the target cell may be duplicated. (").append(connInfo->connectionName).append(" cell:").append(connInfo->originCellName).append(" - > ").append(connInfo->targetCellName); + std::string message = std::string("Failed to make connection. Receptor name for the target cell may be duplicated. (").append(connInfo->connectionName).append(") cell:").append(connInfo->originCellName).append(" - > ").append(connInfo->targetCellName); dnNotifyError("Initialization failed", message); } } else { - std::string message = std::string("Failed to make connection (").append(connInfo->connectionName).append(" cell:").append(connInfo->originCellName).append(" - > ").append(connInfo->targetCellName); + std::string message = std::string("Failed to make connection (").append(connInfo->connectionName).append(") cell:").append(connInfo->originCellName).append(" - > ").append(connInfo->targetCellName); dnNotifyError("Initialization failed", message); } mPendingConnections.pop(); diff --git a/Source/DNEngine.cpp b/Source/DNEngine.cpp index b4b5095..8af523b 100644 --- a/Source/DNEngine.cpp +++ b/Source/DNEngine.cpp @@ -267,6 +267,8 @@ void DNEngine::setTickIntervalSec(float interval) bool DNEngine::startEngine() { + mContainer->doInit(); + if (!mDoTickThread) mDoTickThread = DNThread::createThread(DNEngine::doTickThread, this); diff --git a/Source/QtScript/dnqscellcode.cpp b/Source/QtScript/dnqscellcode.cpp index af66c12..d55a04b 100644 --- a/Source/QtScript/dnqscellcode.cpp +++ b/Source/QtScript/dnqscellcode.cpp @@ -26,6 +26,8 @@ #include "dnqscellcodeinstance.h" #include "DNGlobal.h" +#include + DNQSCellCode::DNQSCellCode(std::string name, std::string cellapi, DNQSContainer *container, std::string code): TKCellCode(name,cellapi), mCellContainer(container) { @@ -38,12 +40,34 @@ DNQSCellCode::DNQSCellCode(std::string name, std::string cellapi, DNQSContainer if (scriptEngine->hasUncaughtException()) { QScriptValue error = scriptEngine->uncaughtException(); + int lineNumber = scriptEngine->uncaughtExceptionLineNumber(); + QStringList list = stmt.split("\n", QString::KeepEmptyParts); + QString errorStatement = ""; + if (lineNumber <= list.length()) + { + int i = lineNumber < 2 ? 0 : lineNumber - 2; + int j = i + 2 <= list.length() ? i + 2 : list.length() - 1; + while (i <= j) + { + errorStatement += list.at(i); + errorStatement += "\n"; + i++; + } + } QString errorString = error.toString(); scriptEngine->clearExceptions(); std::string message = "Failed to construct Cell code : "; - message.append(getName()).append("\n"); - message.append("Error Message:").append(errorString.toStdString()); + message += getName(); + message += "\n"; + if (errorStatement.length()>0) + { + message += ("Statement:\n...\n"); + message += errorStatement.toStdString(); + message += "...\n"; + } + message += "Error Message:"; + message += errorString.toStdString(); dnNotifyError("Initialization failed", message); return; } @@ -113,15 +137,37 @@ TKCellCodeInstance* DNQSCellCode::createCellCodeInstance(TKCell *owner, const vo if (scriptEngine->hasUncaughtException()) { QScriptValue error = scriptEngine->uncaughtException(); + int lineNumber = scriptEngine->uncaughtExceptionLineNumber(); + QStringList list = customScriptStr.split("\n", QString::KeepEmptyParts); + QString errorStatement = ""; + if (lineNumber <= list.length()) + { + int i = lineNumber < 2 ? 0 : lineNumber - 2; + int j = i + 2 <= list.length() ? i + 2 : list.length() - 1; + while (i <= j) + { + errorStatement += list.at(i); + errorStatement += "\n"; + i++; + } + } QString errorString = error.toString(); scriptEngine->clearExceptions(); - std::string message = ""; - message.append("Failed to construct Cell code '"); - message.append(owner->getName()).append("'\n"); - message.append("cellcode:"); - message.append(getName()).append("\n"); - message.append("Error Message:").append(errorString.toStdString()); + std::string message = "Failed to construct Cell '"; + message += owner->getName(); + message += "'\n"; + message += "cellcode:"; + message += getName(); + message += "\n"; + if (errorStatement.length()>0) + { + message += ("Statement:\n...\n"); + message += errorStatement.toStdString(); + message += "...\n"; + } + message += "Error Message:"; + message += errorString.toStdString(); dnNotifyError("Initialization failed", message); } diff --git a/Source/TKCell.cpp b/Source/TKCell.cpp index b40cf10..8ef7f5f 100644 --- a/Source/TKCell.cpp +++ b/Source/TKCell.cpp @@ -69,10 +69,7 @@ void TKCell::setAxonValue(float value) bool TKCell::setCellCode(TKCellCode *code, const void *data) { mCellCodeInstance = code->createCellCodeInstance(this, data); - if (mCellCodeInstance) - return doInit(); - else - return false; + return mCellCodeInstance != NULL; } bool TKCell::connectTo(std::string connectionName, TKCell *targetCell) diff --git a/Source/TKContainer.cpp b/Source/TKContainer.cpp index 35f6c0f..6b0ffcf 100644 --- a/Source/TKContainer.cpp +++ b/Source/TKContainer.cpp @@ -28,6 +28,16 @@ TKContainer::~TKContainer() releaseDataStore(); } +bool TKContainer::doInit() +{ + mLock.lock(); + for ( TKCellMap::iterator it = mCells.begin(); it != mCells.end(); ++it ) { + it->second->doInit(); + } + mLock.unlock(); + return true; +} + bool TKContainer::doTick(float time) { mLock.lock(); diff --git a/Source/TKContainer.h b/Source/TKContainer.h index 3d84340..c44829d 100644 --- a/Source/TKContainer.h +++ b/Source/TKContainer.h @@ -51,6 +51,7 @@ public: TKCell* getInterfaceCell(std::string theFQNName); TKCellCode* getCellCode(std::string theCellCodeName); + bool doInit(); bool doTick(float time); bool doDestroy(); -- 2.11.0