Index: trunk/FACT++/src/InterpreterV8.cc
===================================================================
--- trunk/FACT++/src/InterpreterV8.cc	(revision 14687)
+++ trunk/FACT++/src/InterpreterV8.cc	(revision 14688)
@@ -20,4 +20,5 @@
 #include <v8.h>
 
+#include "dim.h"
 #include "tools.h"
 #include "externals/izstream.h"
@@ -118,5 +119,5 @@
         "(function(){"
         "var t=new Date();"
-        "while ((new Date()-t)<"+to_string(args[0]->Int32Value())+") dim.sleep();"
+        "while ((new Date()-t)<"+to_string(args[0]->Int32Value())+") v8.sleep();"
         "})();";
 
@@ -143,5 +144,5 @@
     TryCatch exception;
 
-    const bool rc = ms==0 || !ExecuteInternal("dim.sleep("+to_string(ms)+");").IsEmpty();
+    const bool rc = ms==0 || !ExecuteInternal("v8.sleep("+to_string(ms)+");").IsEmpty();
     if (rc)
         func->Call(func, 0, NULL);
@@ -190,5 +191,5 @@
     Handle<Object> self = args.This();
 
-    self->SetInternalField(0, Integer::New(id));
+    self->Set(String::New("id"), Integer::NewFromUnsigned(id), ReadOnly);
     self->Set(String::New("kill"), FunctionTemplate::New(WrapKill)->GetFunction(), ReadOnly);
 
@@ -198,5 +199,5 @@
 Handle<Value> InterpreterV8::FuncKill(const Arguments& args)
 {
-    const int id = args.This()->GetInternalField(0)->Int32Value();
+    const uint32_t id = args.This()->Get(String::New("id"))->Uint32Value();
 
     V8::TerminateExecution(id);
@@ -250,5 +251,4 @@
         return ThrowException(String::New(e.what()));
     }
-
 }
 
@@ -268,6 +268,6 @@
         return ThrowException(String::New("Argument 2 not an int32 and not a string."));
 
-    if (args.Length()==3 && !args[2]->IsUint32())
-        return ThrowException(String::New("Argument 3 not an uint32."));
+    if (args.Length()==3 && !args[2]->IsInt32())
+        return ThrowException(String::New("Argument 3 not an int32."));
 
     // Using a Javascript function has the advantage that it is fully
@@ -294,11 +294,14 @@
                       "{"
                          "var s = dim.state(name);"
-                         "if(!"+index+")throw new Error('Waitig for state "+arg1+" of server "+arg0+" failed.');"
+                         "if(!s)throw new Error('Waitig for state "+arg1+" of server "+arg0+" failed.');"
                          "if(state=="+index+")return true;";
     if (timeout)
-        code +=          "if((new Date()-t)>ms)return false;";
-
-    code +=              "dim.sleep();"
-                      "}"
+        code +=          "if((new Date()-t)>Math.abs(ms))break;";
+
+    code +=              "v8.sleep();"
+                      "}";
+    if (timeout)
+        code +=    "if(ms>0)throw new Error('Waitig for state "+arg1+" of server "+arg0+" timed out.');";
+    code +=        "return false;"
                    "})('"+arg0+"',"+arg1;
     if (timeout)
@@ -486,6 +489,10 @@
 
         if (!ExecuteFile(*file))
-            return Boolean::New(false);
-    }
+        {
+            V8::TerminateExecution(fThreadId);
+            return ThrowException(Null());
+        }
+    }
+
     return Boolean::New(true);
 }
@@ -541,9 +548,4 @@
 
     return handle_scope.Close(arr);
-}
-
-Handle<Value> InterpreterV8::FuncVersion(const Arguments&)
-{
-    return String::New(V8::GetVersion());
 }
 
@@ -797,5 +799,4 @@
     case 'S':  { Handle<Value> v=Integer::NewFromUnsigned(*reinterpret_cast<const uint16_t*>(ptr)); ptr += 2; return v; }
     case 'C':  { Handle<Value> v=Integer::NewFromUnsigned((uint16_t)*reinterpret_cast<const uint8_t*>(ptr));  ptr += 1; return v; }
-    case ':':  { Handle<Value> v=String::New(ptr); return v; }
     }
     return Undefined();
@@ -808,5 +809,5 @@
     //const void *ptr = Local<External>::Cast(args.Holder()->GetInternalField(0))->Value();
 
-    const String::AsciiValue str(args.Holder()->Get(String::New("name")));
+    const String::AsciiValue str(args.This()->Get(String::New("name")));
 
     const auto it = fReverseMap.find(*str);
@@ -883,27 +884,49 @@
     {
         size_t pos = 1;
-        for (auto it=tok.begin(); it!=tok.end() && ptr<end; it++, pos++)
+        for (auto it=tok.begin(); it<tok.end() && ptr<end; it++, pos++)
         {
             char type = (*it)[0];
             it++;
 
-            if (it==tok.end() && type=='C')
-                type = ':';
-
-            if (it==tok.end() && type!=':')
-                return Exception::Error(String::New(("Format string invalid '"+evt->GetFormat()+"'").c_str()));
-
             string name = pos<vec.size() ? vec[pos].name : "";
             if (tok.size()==1)
                 name = "data";
 
-            const uint32_t cnt = it==tok.end() ? 1 : stoi(it->c_str());
+            // Get element size
+            uint32_t sz = 1;
+            switch (type)
+            {
+            case 'X':
+            case 'D': sz = 8; break;
+            case 'F':
+            case 'I':
+            case 'L': sz = 4; break;
+            case 'S': sz = 2; break;
+            case 'C': sz = 1; break;
+            }
+
+            // Check if no number is attached if the size of the
+            // received data is consistent with the format string
+            if (it==tok.end() && (end-ptr)%sz>0)
+                return Exception::Error(String::New(("Number of received bytes ["+to_string(evt->GetSize())+"] does not match format ["+evt->GetFormat()+"]").c_str()));
+
+            // Check if format has a number attached.
+            // If no number is attached calculate number of elements
+            const uint32_t cnt = it==tok.end() ? (end-ptr)/sz : stoi(it->c_str());
+
+            // is_str: Array of type C but unknown size (String)
+            // is_one: Array of known size, but size is 1 (I:1)
+            const bool is_str = type=='C' && it==tok.end();
+            const bool is_one = cnt==1    && it!=tok.end();
 
             Handle<Value> v;
-            if (cnt==1)
-            {
+
+            if (is_str)
+                v = String::New(ptr);
+            if (is_one)
                 v = Convert(type, ptr);
-            }
-            else
+
+            // Array of known (I:5) or unknown size (I), but no string
+            if (!is_str && !is_one)
             {
                 Handle<Object> a = Array::New(cnt);
@@ -919,4 +942,6 @@
             if (tok.size()>1)
                 arr->Set(pos-1, v);
+            else
+                ret->Set(String::New("data"), v, ReadOnly);
 
             if (!name.empty())
@@ -925,7 +950,4 @@
                 named->Set(n, v);
             }
-
-            if (it==tok.end())
-                break;
         }
 
@@ -979,5 +1001,5 @@
     HandleScope handle_scope;
 
-    const Handle<Script> sleep = Script::Compile(String::New("dim.sleep();"), String::New("internal"));
+    const Handle<Script> sleep = Script::Compile(String::New("v8.sleep();"), String::New("internal"));
     if (sleep.IsEmpty())
         return Undefined();
@@ -1127,7 +1149,7 @@
     }
 
+    const HandleScope handle_scope;
+
     it->second->CreationContext()->Enter();
-
-    const HandleScope handle_scope;
 
     // -------------------------------------------------------------------
@@ -1617,5 +1639,5 @@
 
         HandleException(exception, "code");
-        return Undefined();
+        return Handle<Value>();
     }
 
@@ -1762,11 +1784,10 @@
     // Create a template for the global object.
     Handle<ObjectTemplate> dim = ObjectTemplate::New();
-    dim->Set(String::New("print"),     FunctionTemplate::New(WrapPrint),     ReadOnly);
-    dim->Set(String::New("alarm"),     FunctionTemplate::New(WrapAlarm),     ReadOnly);
-    dim->Set(String::New("out"),       FunctionTemplate::New(WrapOut),       ReadOnly);
-    dim->Set(String::New("wait"),      FunctionTemplate::New(WrapWait),      ReadOnly);
-    dim->Set(String::New("send"),      FunctionTemplate::New(WrapSend),      ReadOnly);
-    dim->Set(String::New("state"),     FunctionTemplate::New(WrapState),     ReadOnly);
-    dim->Set(String::New("sleep"),     FunctionTemplate::New(WrapSleep),     ReadOnly);
+    dim->Set(String::New("print"),   FunctionTemplate::New(WrapPrint), ReadOnly);
+    dim->Set(String::New("alarm"),   FunctionTemplate::New(WrapAlarm), ReadOnly);
+    dim->Set(String::New("wait"),    FunctionTemplate::New(WrapWait),  ReadOnly);
+    dim->Set(String::New("send"),    FunctionTemplate::New(WrapSend),  ReadOnly);
+    dim->Set(String::New("state"),   FunctionTemplate::New(WrapState), ReadOnly);
+    dim->Set(String::New("version"), Integer::New(DIM_VERSION_NUMBER), ReadOnly);
 
     Handle<ObjectTemplate> dimctrl = ObjectTemplate::New();
@@ -1775,16 +1796,22 @@
     dimctrl->Set(String::New("getState"),  FunctionTemplate::New(WrapGetState),  ReadOnly);
 
-    // new class State ?
+    Handle<ObjectTemplate> v8 = ObjectTemplate::New();
+    v8->Set(String::New("sleep"),   FunctionTemplate::New(WrapSleep), ReadOnly);
+    v8->Set(String::New("version"), String::New(V8::GetVersion()),    ReadOnly);
+
+    Handle<ObjectTemplate> console = ObjectTemplate::New();
+    console->Set(String::New("out"), FunctionTemplate::New(WrapOut), ReadOnly);
 
     Handle<ObjectTemplate> onchange = ObjectTemplate::New();
     onchange->SetNamedPropertyHandler(OnChangeGet, WrapOnChangeSet);
-    dim->Set(v8::String::New("onchange"), onchange);
+    dim->Set(String::New("onchange"), onchange);
 
     Handle<ObjectTemplate> global = ObjectTemplate::New();
-    global->Set(String::New("dim"),      dim,     ReadOnly);
-    global->Set(String::New("dimctrl"),  dimctrl, ReadOnly);
-    global->Set(String::New("include"),  FunctionTemplate::New(WrapInclude),                ReadOnly);
-    global->Set(String::New("exit"),     FunctionTemplate::New(WrapExit),                   ReadOnly);
-    global->Set(String::New("version"),  FunctionTemplate::New(InterpreterV8::FuncVersion), ReadOnly);
+    global->Set(String::New("v8"),      v8,      ReadOnly);
+    global->Set(String::New("dim"),     dim,     ReadOnly);
+    global->Set(String::New("dimctrl"), dimctrl, ReadOnly);
+    global->Set(String::New("console"), console, ReadOnly);
+    global->Set(String::New("include"), FunctionTemplate::New(WrapInclude),                ReadOnly);
+    global->Set(String::New("exit"),    FunctionTemplate::New(WrapExit),                   ReadOnly);
 
     Handle<FunctionTemplate> sub = FunctionTemplate::New(WrapSubscription);
@@ -1800,10 +1827,8 @@
     Handle<FunctionTemplate> thread = FunctionTemplate::New(WrapThread);
     thread->SetClassName(String::New("Thread"));
-    thread->InstanceTemplate()->SetInternalFieldCount(1);
     global->Set(String::New("Thread"), thread, ReadOnly);
 
     Handle<FunctionTemplate> file = FunctionTemplate::New(WrapFile);
     file->SetClassName(String::New("File"));
-    file->InstanceTemplate()->SetInternalFieldCount(1);
     global->Set(String::New("File"), file, ReadOnly);
 
@@ -1837,5 +1862,6 @@
     if (context.IsEmpty())
     {
-        //printf("Error creating context\n");
+        JsException("Creation of global context failed...");
+        JsEnd(filename);
         return false;
     }
@@ -1854,10 +1880,9 @@
     //V8::ResumeProfiler();
 
-    bool rc;
-
     TryCatch exception;
 
     AddFormatToGlobal();
 
+    bool rc = true;
     if (!exception.HasCaught())
     {
@@ -1866,5 +1891,5 @@
         Locker::StartPreemption(10);
 
-        rc = ExecuteFile(filename, true);
+        rc &= ExecuteFile(filename, true);
 
         Locker::StopPreemption();
@@ -1877,5 +1902,5 @@
 
     // Handle an exception
-    rc = HandleException(exception, "main");
+    rc &= HandleException(exception, "main");
 
     // IsProfilerPaused()
@@ -1930,5 +1955,5 @@
     JsEnd(filename);
 
-    return rc;
+    return true;
 }
 
Index: trunk/FACT++/src/InterpreterV8.h
===================================================================
--- trunk/FACT++/src/InterpreterV8.h	(revision 14687)
+++ trunk/FACT++/src/InterpreterV8.h	(revision 14688)
@@ -98,5 +98,4 @@
     static v8::Handle<v8::Value> ConstructorLocal(const v8::Arguments &args);
 
-    static v8::Handle<v8::Value> FuncVersion(const v8::Arguments&);
     static v8::Handle<v8::Value> WrapInclude(const v8::Arguments &args)  { if (This) return This->FuncInclude(args);  else return v8::Undefined(); }
     static v8::Handle<v8::Value> WrapFile(const v8::Arguments &args)     { if (This) return This->FuncFile(args);     else return v8::Undefined(); }
