OSDN Git Service

- Modified for better error message for the failure of JavaScript execution in initia...
authortkawata <tkawata@users.sourceforge.jp>
Tue, 29 May 2012 15:40:55 +0000 (00:40 +0900)
committertkawata <tkawata@users.sourceforge.jp>
Tue, 29 May 2012 15:40:55 +0000 (00:40 +0900)
- 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
Source/DNEngine.cpp
Source/QtScript/dnqscellcode.cpp
Source/TKCell.cpp
Source/TKContainer.cpp
Source/TKContainer.h

index ecce3c5..28fa65f 100644 (file)
@@ -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();
index b4b5095..8af523b 100644 (file)
@@ -267,6 +267,8 @@ void DNEngine::setTickIntervalSec(float interval)
 
 bool DNEngine::startEngine()
 {
+    mContainer->doInit();
+
     if (!mDoTickThread)
         mDoTickThread = DNThread::createThread(DNEngine::doTickThread, this);
 
index af66c12..d55a04b 100644 (file)
@@ -26,6 +26,8 @@
 #include "dnqscellcodeinstance.h"
 #include "DNGlobal.h"
 
+#include <QStringList>
+
 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);
         }
 
index b40cf10..8ef7f5f 100644 (file)
@@ -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)
index 35f6c0f..6b0ffcf 100644 (file)
@@ -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();
index 3d84340..c44829d 100644 (file)
@@ -51,6 +51,7 @@ public:
     TKCell*     getInterfaceCell(std::string theFQNName);
     TKCellCode* getCellCode(std::string theCellCodeName);
 
+    bool        doInit();
     bool        doTick(float time);
     bool        doDestroy();