Changeset 14555


Ignore:
Timestamp:
11/03/12 19:20:55 (12 years ago)
Author:
tbretz
Message:
Added processing of callbacks for state changes; some fixes to the locking to fix some issue around script termination.
Location:
trunk/FACT++/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/InterpreterV8.cc

    r14548 r14555  
    312312
    313313    Handle<ObjectTemplate> obj = ObjectTemplate::New();
    314     obj->Set(String::New("time"),  date);
    315314    obj->Set(String::New("index"), rc.index<=-256?Undefined():Integer::New(rc.index),       ReadOnly);
    316315    obj->Set(String::New("name"),  rc.index<=-256?Undefined():String::New(rc.name.c_str()), ReadOnly);
     316    if (rc.index>-256)
     317        obj->Set(String::New("time"),  date);
    317318    //obj->Set(String::New("toString"),  String::New(("[Object state "+string(*str)+":"+to_string(rc.index)+"]").c_str()));
    318319
     
    663664    }
    664665
     666    args.Holder()->Set(String::New("isOpen"), Boolean::New(false), ReadOnly);
     667
    665668    return Boolean::New(JsUnsubscribe(*str));
    666669}
     
    781784        return;
    782785
     786    Locker locker;
     787
    783788    const auto it = fReverseMap.find(service);
    784789    if (it==fReverseMap.end())
    785790        return;
    786791
    787     Locker locker;
    788 
    789792    const HandleScope handle_scope;
    790793
     
    835838}
    836839
     840Handle<Value> InterpreterV8::OnChangeSet(Local<String> prop, Local< Value > value, const AccessorInfo &info)
     841{
     842    const HandleScope handle_scope;
     843
     844    // Returns the value if the setter intercepts the request. Otherwise, returns an empty handle.
     845    const string server = *String::Utf8Value(prop);
     846    auto it = fStateCallbacks.find(server);
     847
     848    if (it!=fStateCallbacks.end())
     849    {
     850        it->second.Dispose();
     851        fStateCallbacks.erase(it);
     852    }
     853
     854    if (value->IsFunction())
     855        fStateCallbacks[server] = Persistent<Value>::New(value);
     856
     857    return Handle<Value>();
     858}
     859
     860
     861void InterpreterV8::JsHandleState(const std::string &server, const State &state)
     862{
     863    if (fThreadId<0)
     864        return;
     865
     866    Locker locker;
     867
     868    auto it = fStateCallbacks.find(server);
     869    if (it==fStateCallbacks.end())
     870    {
     871        it = fStateCallbacks.find("*");
     872        if (it==fStateCallbacks.end())
     873            return;
     874    }
     875
     876    const HandleScope handle_scope;
     877
     878    if (it->second.IsEmpty() || !it->second->IsFunction())
     879        return;
     880
     881    // -------------------------------------------------------------------
     882    // We are not in a context... we need to get into one for Array::New
     883
     884    Persistent<Context> context = Context::New();
     885    if (context.IsEmpty())
     886        return;
     887
     888    const Context::Scope scope(context);
     889
     890    // -------------------------------------------------------------------
     891
     892    TryCatch exception;
     893
     894    // It is important to catch the exception thrown
     895    // by Date::New in case of thread termination!
     896    Local<Value> date = Date::New(state.time.JavaDate());
     897
     898    if (!exception.HasCaught())
     899    {
     900        Handle<ObjectTemplate> obj = ObjectTemplate::New();
     901        obj->Set(String::New("index"),  state.index<=-256?Undefined():Integer::New(state.index),       ReadOnly);
     902        obj->Set(String::New("name"),   state.index<=-256?Undefined():String::New(state.name.c_str()), ReadOnly);
     903        obj->Set(String::New("server"), String::New(server.c_str()), ReadOnly);
     904        if (state.index>-256)
     905            obj->Set(String::New("time"), date);
     906
     907        Handle<Value> args[] = { obj->NewInstance() };
     908
     909        Handle<Function> fun = Handle<Function>::Cast(it->second);
     910        fun->Call(fun, 1, args);
     911    }
     912
     913    if (exception.HasCaught())
     914        ReportException(&exception);
     915
     916    context.Dispose();
     917
     918    if (exception.HasCaught())
     919        V8::TerminateExecution(fThreadId);
     920}
     921
    837922/*
    838923void Cleanup( Persistent<Value> object, void *parameter )
     
    861946
    862947    Handle<ObjectTemplate> tem = ObjectTemplate::New();
    863     tem->Set(String::New("get"),   FunctionTemplate::New(WrapGetData), ReadOnly);
    864     tem->Set(String::New("close"), FunctionTemplate::New(WrapClose),   ReadOnly);
    865     tem->Set(String::New("name"),  String::New(*str), ReadOnly);
     948    tem->Set(String::New("get"),    FunctionTemplate::New(WrapGetData), ReadOnly);
     949    tem->Set(String::New("close"),  FunctionTemplate::New(WrapClose),   ReadOnly);
     950    tem->Set(String::New("name"),   String::New(*str), ReadOnly);
     951    tem->Set(String::New("isOpen"), Boolean::New(true));
    866952    tem->SetInternalFieldCount(1);
    867953    //tem->Set(String::New("toString"), String::New(("[object Dim "+string(*str)+"]").c_str()), ReadOnly);
     
    900986    dim->Set(String::New("database"), FunctionTemplate::New(WrapDatabase), ReadOnly);
    901987
     988    Handle<ObjectTemplate> onchange = ObjectTemplate::New();
     989    onchange->SetNamedPropertyHandler(OnChangeGet, WrapOnChangeSet);
     990    dim->Set(v8::String::New("onchange"), onchange);
     991
    902992    Handle<ObjectTemplate> global = ObjectTemplate::New();
    903993    global->Set(String::New("dim"), dim, ReadOnly);
     
    9431033    //context->Exit();
    9441034
     1035    for (auto it=fStateCallbacks.begin(); it!=fStateCallbacks.end(); it++)
     1036        it->second.Dispose();
     1037    fStateCallbacks.clear();
     1038
    9451039    for (auto it=fReverseMap.begin(); it!=fReverseMap.end(); it++)
    9461040        it->second.Dispose();
     
    9731067
    9741068#endif
    975 
    976 /*
    977 #0  0x00007ffff5ae0e62 in ?? () from /usr/lib/libv8.so.3.7.12.22
    978 #1  0x00007ffff5ae1969 in ?? () from /usr/lib/libv8.so.3.7.12.22
    979 #2  0x00007ffff5af1976 in v8::V8::Dispose() () from /usr/lib/libv8.so.3.7.12.22
    980 #3  0x0000000000426459 in RemoteControl<Console>::~RemoteControl() ()
    981 #4  0x00007ffff51e9901 in __run_exit_handlers (status=3, listp=0x7ffff5566688, run_list_atexi
    982 #5  0x00007ffff51e9985 in __GI_exit (status=<optimized out>) at exit.c:100
    983 #6  0x00000000004374db in DimErrorRedirecter::exitHandler(int) ()
    984 #7  0x00007ffff66bce72 in exit_user_routine () from /home/fact/FACT++.in-run-fad-loss/.libs/l
    985 #8  0x00007ffff6477387 in recv_dns_dis_rout () from /home/fact/FACT++.in-run-fad-loss/.libs/l
    986 #9  0x00007ffff647b79f in ast_read_h () from /home/fact/FACT++.in-run-fad-loss/.libs/libDim.s
    987 #10 0x00007ffff647f388 in do_read () from /home/fact/FACT++.in-run-fad-loss/.libs/libDim.so
    988 #11 0x00007ffff647ff32 in tcpip_task () from /home/fact/FACT++.in-run-fad-loss/.libs/libDim.s
    989 #12 0x00007ffff6482c57 in dim_tcpip_thread () from /home/fact/FACT++.in-run-fad-loss/.libs/li
    990 #13 0x00007ffff7bc4e9a in start_thread (arg=0x7ffff13be700) at pthread_create.c:308
    991 #14 0x00007ffff52a1cbd in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:112
    992 */
  • trunk/FACT++/src/InterpreterV8.h

    r14549 r14555  
    1515
    1616class Database;
    17 #include<iostream>
     17
    1818class InterpreterV8
    1919{
     
    2929    // subscription exists for that object)
    3030    std::map<std::string, v8::Persistent<v8::Object>> fReverseMap;
     31
     32    // Lookup table for the callbacks in cases of state changes
     33    std::map<std::string, v8::Persistent<v8::Value>>  fStateCallbacks;
    3134
    3235#ifdef HAVE_SQL
     
    5760    v8::Handle<v8::Value> FuncDbQuery(const v8::Arguments &args);
    5861    v8::Handle<v8::Value> FuncDbClose(const v8::Arguments &args);
     62    v8::Handle<v8::Value> OnChangeSet(v8::Local<v8::String>, v8::Local<v8::Value>, const v8::AccessorInfo &);
    5963
    6064    static v8::Handle<v8::Value> FuncVersion(const v8::Arguments&);
     
    7579    static v8::Handle<v8::Value> WrapDbQuery(const v8::Arguments &args)  { if (This) return This->FuncDbQuery(args);  else return v8::Undefined(); }
    7680    static v8::Handle<v8::Value> WrapDbClose(const v8::Arguments &args)  { if (This) return This->FuncDbClose(args);  else return v8::Undefined(); }
     81    static v8::Handle<v8::Value> WrapOnChangeSet(v8::Local<v8::String> prop, v8::Local<v8::Value> value, const v8::AccessorInfo &info)
     82    {
     83        if (This) return This->OnChangeSet(prop, value, info);  else return v8::Undefined();
     84    }
     85
     86    static v8::Handle<v8::Value> OnChangeGet(v8::Local<v8::String>, const v8::AccessorInfo &)
     87    {
     88        return v8::Handle<v8::Value>();
     89    }
    7790
    7891    static v8::Handle<v8::Value> Convert(char type, const char* &ptr);
     
    113126
    114127    void JsHandleEvent(const EventImp &, uint64_t, const std::string &);
     128    void JsHandleState(const std::string &, const State &);
    115129
    116130    bool JsRun(const std::string &, const std::map<std::string,std::string> & = std::map<std::string,std::string>());
Note: See TracChangeset for help on using the changeset viewer.