Index: trunk/FACT++/src/InterpreterV8-4.cc
===================================================================
--- trunk/FACT++/src/InterpreterV8-4.cc	(revision 20082)
+++ trunk/FACT++/src/InterpreterV8-4.cc	(revision 20083)
@@ -222,31 +222,24 @@
 }
 
-void InterpreterV8::Thread(int &id, Persistent<Object> _this, Persistent<Function> func, uint32_t ms)
-{
-    /*
-    Isolate *isolate = Isolate::GetCurrent();
-
-    const Locker lock(isolate);
-
-    if (fThreadId<0)
-    {
-        id = -1;
-        return;
-    }
-
-    // Warning: As soon as id is set, the parent of this thread might terminate
-    //          and hance the reference to id does not exist anymore. So, id
-    //          is just a kind of return value and must not be used at all
-    //          otherwise.
-
-    const int id_local = V8::GetCurrentThreadId();
-    id = id_local;
-    fThreadIds.insert(id_local);
-
+void InterpreterV8::Thread(int &id, v8::UniquePersistent<Object> &_this, v8::UniquePersistent<v8::Object> &func, uint32_t ms)
+{
+    Isolate *isolate = fMainThread;
+    if (!isolate)
+        return;
+
+    // We want to run as an interrupt in the main thread
+    const Locker locker(isolate);
+    if (!fMainThread)
+        return;
+
+    const Isolate::Scope isolate_scope(isolate);
     const HandleScope handle_scope(isolate);
 
-    func->CreationContext()->Enter();
-
-    TryCatch exception;
+    Handle<Object> fun = func.Get(isolate);
+    const Context::Scope scope(fun->CreationContext());
+
+    TryCatch exception(isolate);
+
+    fun->CreationContext()->Enter();
 
     const bool rc = ms==0 || !ExecuteInternal("v8.sleep("+to_string(ms)+");").IsEmpty();
@@ -254,19 +247,13 @@
     {
         if (_this.IsEmpty())
-            func->Call(func, 0, NULL);
+            Handle<Function>::Cast(fun)->Call(fun->CreationContext(), fun, 0, NULL).IsEmpty();
         else
-            func->Call(_this, 0, NULL);
-    }
-
-    func.Dispose();
-    _this.Dispose();
-
-    fThreadIds.erase(id_local);
-
-    if (!HandleException(exception, "thread"))
-        V8::TerminateExecution(isolate);
-
-    func->CreationContext()->Exit();
-    */
+            Handle<Function>::Cast(fun)->Call(fun->CreationContext(), _this.Get(isolate), 0, NULL).IsEmpty();
+    }
+
+    if (!HandleException(exception, "Thread"))
+        isolate->AddBeforeCallEnteredCallback(TerminateExecution);
+
+    fun->CreationContext()->Exit();
 }
 
@@ -307,21 +294,17 @@
     }
 
-    //*NEW or OLD?*
-    //if (!args.IsConstructCall())
-    //    return Constructor(args);
-/*
     const HandleScope handle_scope(isolate);
 
-    Handle<Function> handle = Handle<Function>::Cast(args[1]);
-
-    Persistent<Function> func = Persistent<Function>::New(isolate, handle);
-    Persistent<Object> _this;
+    UniquePersistent<Object> func = UniquePersistent<Object>(isolate, args[1]->ToObject());
+
+    UniquePersistent<Object> _this;
     if (args.Length()==3)
-        _this = Persistent<Object>::New(args[2]->ToObject());
+        _this = UniquePersistent<Object>(isolate, args[2]->ToObject());
 
     const uint32_t ms = args[0]->Uint32Value();
 
-    int id=-2;
-    fThreads.push_back(thread(bind(&InterpreterV8::Thread, this, ref(id), _this, func, ms)));
+    //int id=__id;
+    fThreads.push_back(thread(bind(&InterpreterV8::Thread, this, ref(id), std::move(_this), std::move(func), ms)));
+    /*
     {
         // Allow the thread to lock, so we can get the thread id.
@@ -330,9 +313,10 @@
             usleep(1);
     }
-*/
-//    Handle<Object> self = args.This();
-//    self->Set(String::NewFromUtf8(isolate, "id"), Integer::NewFromUnsigned(isolate, id)/*, ReadOnly*/);
-//    self->Set(String::NewFromUtf8(isolate, "kill"), FunctionTemplate::New(isolate, WrapKill)->GetFunction()/*, ReadOnly*/);
-//    args.GetReturnValue().Set(self);
+
+    Handle<Object> self = args.This();
+    self->Set(String::NewFromUtf8(isolate, "id"), Integer::NewFromUnsigned(isolate, id));
+    self->Set(String::NewFromUtf8(isolate, "kill"), FunctionTemplate::New(isolate, WrapKill)->GetFunction());
+    args.GetReturnValue().Set(self);
+    */
 }
 
@@ -1946,5 +1930,5 @@
 
     if (value->IsFunction())
-        fStateCallbacks[server] = PersistentCopy<Object>(isolate, value->ToObject());
+        fStateCallbacks[server] = UniquePersistent<Object>(isolate, value->ToObject());
 }
 
@@ -2041,5 +2025,5 @@
 
     // Returns the value if the setter intercepts the request. Otherwise, returns an empty handle.
-    fInterruptCallback = PersistentCopy<Object>(isolate, args[0]->ToObject());
+    fInterruptCallback = UniquePersistent<Object>(isolate, args[0]->ToObject());
 }
 
@@ -2204,5 +2188,5 @@
         self->Set(String::NewFromUtf8(isolate, "onchange"), args[1]);
 
-    fReverseMap[*str] = PersistentCopy<Object>(isolate, self);
+    fReverseMap[*str] = UniquePersistent<Object>(isolate, self);
 
     void *ptr = JsSubscribe(*str);
@@ -2932,5 +2916,4 @@
         // Unlocking is necessary for the preemption to work
         const Unlocker global_unlock(isolate);
-
         const string buffer = Tools::Trim(Readline::StaticPrompt(command.empty() ? "JS> " : " \\> "));
         if (buffer==".q")
@@ -2971,15 +2954,4 @@
         if (!HandleException(exception, "console"))
             lout << endl;
-
-        // Stop all other threads
-        for (auto it=fThreadIsolates.begin(); it!=fThreadIsolates.end(); it++)
-            (*it)->TerminateExecution();
-
-        // Allow the java scripts (threads) to run and hence to terminate
-        const Unlocker unlock(isolate);
-
-        // Wait until all threads are terminated
-        while (!fThreadIsolates.empty())
-            usleep(1000);
 
         // command has been executed, collect new command
@@ -3233,7 +3205,5 @@
         Handle<FunctionTemplate> thread = FunctionTemplate::New(isolate, WrapThread);
         thread->SetClassName(String::NewFromUtf8(isolate, "Thread"));
-#if 0
         global->Set(String::NewFromUtf8(isolate, "Thread"), thread, ReadOnly);
-#endif
 
         Handle<FunctionTemplate> file = FunctionTemplate::New(isolate, WrapFile);
@@ -3378,9 +3348,4 @@
 
             //        Locker::StopPreemption();
-/*OLD*/
-            // Stop all other threads
-            //        for (auto it=fThreadIsolates.begin(); it!=fThreadIsolates.end(); it++)
-            //            (*it)->TerminateExecution();
-            //        fThreadIsolates.clear();
         }
 
@@ -3400,15 +3365,12 @@
     // So we have to unlock (manual preemtion) so that they get
     // the signal to terminate.
-    /*
-    {
-        const Unlocker unlock(isolate);
-
-        for (auto it=fThreads.begin(); it!=fThreads.end(); it++)
-        {
-            it->join();
-            fThreads.clear();
-        }*/
-
-    // Now we can dispose the persistent interrupt handler
+    //{
+    //    const Unlocker unlock(isolate);
+    for (auto it=fThreads.begin(); it!=fThreads.end(); it++)
+        it->join();
+    fThreads.clear();
+    //}
+
+    // Now we can dispose the persistent interrupt handlers
     fInterruptCallback.Reset();
 
@@ -3416,5 +3378,5 @@
     fStateCallbacks.clear();
 
-    // ... and from the onchange callbacks
+    // ... and from the onchange callbacks       
     fReverseMap.clear();
 
Index: trunk/FACT++/src/InterpreterV8-4.h
===================================================================
--- trunk/FACT++/src/InterpreterV8-4.h	(revision 20082)
+++ trunk/FACT++/src/InterpreterV8-4.h	(revision 20083)
@@ -32,9 +32,6 @@
 class InterpreterV8
 {
-    template<class T>
-        using PersistentCopy = v8::Persistent<T, v8::CopyablePersistentTraits<T>>;
-
     template<class T, class S>
-       using PersistentMap = std::map<T, PersistentCopy<S>>;
+       using PersistentMap = std::map<T, v8::UniquePersistent<S>>;
 
     static InterpreterV8 *This;
@@ -44,7 +41,6 @@
     // the thread forcefully from 'the outside'
     v8::Isolate *fMainThread;
-    std::set<v8::Isolate*> fThreadIsolates;
-
-    // A loookup table which allows to indentify the
+
+    // A loookup table which allows to indentify
     // the JavaScript object corrsponding to the
     // service name (for checking of an .onchange
@@ -62,5 +58,5 @@
 
     // Interrupt handler
-    PersistentCopy<v8::Object> fInterruptCallback;
+    v8::UniquePersistent<v8::Object> fInterruptCallback;
 
     static v8::Handle<v8::FunctionTemplate> fTemplateLocal;
@@ -81,10 +77,8 @@
     v8::Handle<v8::Value> ExecuteInternal(const std::string &code);
 
-    void Thread(int &id, v8::Persistent<v8::Object> _this, v8::Persistent<v8::Function> func, uint32_t ms);
+    void Thread(int &id, v8::UniquePersistent<v8::Object> &_this, v8::UniquePersistent<v8::Object> &func, uint32_t ms);
 
     std::vector<std::string> ValueToArray(const v8::Handle<v8::Value> &val, bool only=true);
 
-    //typedef v8::internal::Arguments FactArguments;
-    //typedef v8::FunctionCallback FactArguments;
     typedef v8::FunctionCallbackInfo<v8::Value> FactArguments;
 
