Changeset 14560 for trunk/FACT++


Ignore:
Timestamp:
11/05/12 12:03:43 (12 years ago)
Author:
tbretz
Message:
Added the possibility to change the state of the state machine and define new states; renamed open to subscribe
Location:
trunk/FACT++/src
Files:
2 edited

Legend:

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

    r14556 r14560  
    8787    if (script.IsEmpty())
    8888        return false;
     89
     90    JsSetState(3);
    8991
    9092    const Handle<Value> result = script->Run();
     
    319321
    320322    return handle_scope.Close(obj->NewInstance());
     323}
     324
     325Handle<Value> InterpreterV8::FuncNewState(const Arguments& args)
     326{
     327    if (args.Length()<1 || args.Length()>3)
     328        return ThrowException(String::New("Number of arguments must be 1, 2 or 3."));
     329
     330    if (!args[0]->IsUint32())
     331        return ThrowException(String::New("Argument 1 must be an uint32."));
     332    if (args.Length()>1 && !args[1]->IsString())
     333        return ThrowException(String::New("Argument 2 must be a string."));
     334    if (args.Length()>2 && !args[2]->IsString())
     335        return ThrowException(String::New("Argument 3 must be a string."));
     336
     337    const HandleScope handle_scope;
     338
     339    const uint32_t index   = args[0]->Int32Value();
     340    const string   name    = *String::Utf8Value(args[1]);
     341    const string   comment = *String::Utf8Value(args[2]);
     342
     343    if (index<10 || index>255)
     344        return ThrowException(String::New("State must be in the range [10, 255]."));
     345
     346    if (name.empty())
     347        return ThrowException(String::New("State name must not be empty."));
     348
     349    if (name.find_first_of(':')!=string::npos || name.find_first_of('=')!=string::npos)
     350        return ThrowException(String::New("State name must not contain : or =."));
     351
     352    if (JsHasState(index) || JsHasState(name))
     353    {
     354        const string what =
     355            "State index ["+to_string(index)+"] or name ["+name+"] already defined.";
     356
     357        return ThrowException(String::New(what.c_str()));
     358    }
     359
     360    return Boolean::New(JsNewState(index, name, comment));
     361}
     362
     363Handle<Value> InterpreterV8::FuncSetState(const Arguments& args)
     364{
     365    if (args.Length()!=1)
     366        return ThrowException(String::New("Number of arguments must be exactly 1."));
     367
     368    if (!args[0]->IsUint32() && !args[0]->IsString())
     369        return ThrowException(String::New("Argument must be an unint32 or a  string."));
     370
     371    const HandleScope handle_scope;
     372
     373    int index = -2;
     374    if (args[0]->IsUint32())
     375    {
     376        index = args[0]->Int32Value();
     377    }
     378    else
     379    {
     380        const string name = *String::Utf8Value(args[0]);
     381        index = JsGetState(name);
     382        if (index==-2)
     383            return ThrowException(String::New(("State '"+name+"' not found.").c_str()));
     384    }
     385
     386    if (index<10 || index>255)
     387        return ThrowException(String::New("State must be in the range [10, 255]."));
     388
     389    return Boolean::New(JsSetState(index));
    321390}
    322391
     
    838907}
    839908
    840 Handle<Value> InterpreterV8::OnChangeSet(Local<String> prop, Local< Value > value, const AccessorInfo &info)
     909Handle<Value> InterpreterV8::OnChangeSet(Local<String> prop, Local< Value > value, const AccessorInfo &)
    841910{
    842911    const HandleScope handle_scope;
     
    927996}*/
    928997
    929 Handle<Value> InterpreterV8::FuncOpen(const Arguments &args)
     998Handle<Value> InterpreterV8::FuncSubscribe(const Arguments &args)
    930999{
    9311000    if (args.Length()!=1)
     
    9771046    // Create a template for the global object.
    9781047    Handle<ObjectTemplate> dim = ObjectTemplate::New();
    979     dim->Set(String::New("print"),    FunctionTemplate::New(WrapPrint),    ReadOnly);
    980     dim->Set(String::New("alarm"),    FunctionTemplate::New(WrapAlarm),    ReadOnly);
    981     dim->Set(String::New("out"),      FunctionTemplate::New(WrapOut),      ReadOnly);
    982     dim->Set(String::New("wait"),     FunctionTemplate::New(WrapWait),     ReadOnly);
    983     dim->Set(String::New("send"),     FunctionTemplate::New(WrapSend),     ReadOnly);
    984     dim->Set(String::New("state"),    FunctionTemplate::New(WrapState),    ReadOnly);
    985     dim->Set(String::New("sleep"),    FunctionTemplate::New(WrapSleep),    ReadOnly);
    986     dim->Set(String::New("open"),     FunctionTemplate::New(WrapOpen),     ReadOnly);
    987     dim->Set(String::New("database"), FunctionTemplate::New(WrapDatabase), ReadOnly);
     1048    dim->Set(String::New("print"),     FunctionTemplate::New(WrapPrint),     ReadOnly);
     1049    dim->Set(String::New("alarm"),     FunctionTemplate::New(WrapAlarm),     ReadOnly);
     1050    dim->Set(String::New("out"),       FunctionTemplate::New(WrapOut),       ReadOnly);
     1051    dim->Set(String::New("wait"),      FunctionTemplate::New(WrapWait),      ReadOnly);
     1052    dim->Set(String::New("send"),      FunctionTemplate::New(WrapSend),      ReadOnly);
     1053    dim->Set(String::New("state"),     FunctionTemplate::New(WrapState),     ReadOnly);
     1054    dim->Set(String::New("newState"),  FunctionTemplate::New(WrapNewState),  ReadOnly);
     1055    dim->Set(String::New("setState"),  FunctionTemplate::New(WrapSetState),  ReadOnly);
     1056    dim->Set(String::New("sleep"),     FunctionTemplate::New(WrapSleep),     ReadOnly);
     1057    dim->Set(String::New("subscribe"), FunctionTemplate::New(WrapSubscribe), ReadOnly);
     1058    dim->Set(String::New("database"),  FunctionTemplate::New(WrapDatabase),  ReadOnly);
    9881059
    9891060    Handle<ObjectTemplate> onchange = ObjectTemplate::New();
  • trunk/FACT++/src/InterpreterV8.h

    r14555 r14560  
    5353    v8::Handle<v8::Value> FuncExit(const v8::Arguments& args);
    5454    v8::Handle<v8::Value> FuncState(const v8::Arguments& args);
    55     v8::Handle<v8::Value> FuncOpen(const v8::Arguments& args);
     55    v8::Handle<v8::Value> FuncSetState(const v8::Arguments& args);
     56    v8::Handle<v8::Value> FuncNewState(const v8::Arguments& args);
     57    //v8::Handle<v8::Value> FuncOpen(const v8::Arguments& args);
     58    v8::Handle<v8::Value> FuncSubscribe(const v8::Arguments& args);
    5659    v8::Handle<v8::Value> FuncGetData(const v8::Arguments &args);
    5760    v8::Handle<v8::Value> FuncClose(const v8::Arguments &args);
     
    7275    static v8::Handle<v8::Value> WrapExit(const v8::Arguments &args)     { if (This) return This->FuncExit(args);     else return v8::Undefined(); }
    7376    static v8::Handle<v8::Value> WrapState(const v8::Arguments &args)    { if (This) return This->FuncState(args);    else return v8::Undefined(); }
    74     static v8::Handle<v8::Value> WrapOpen(const v8::Arguments &args)     { if (This) return This->FuncOpen(args);     else return v8::Undefined(); }
     77    static v8::Handle<v8::Value> WrapNewState(const v8::Arguments &args) { if (This) return This->FuncNewState(args); else return v8::Undefined(); }
     78    static v8::Handle<v8::Value> WrapSetState(const v8::Arguments &args) { if (This) return This->FuncSetState(args); else return v8::Undefined(); }
     79    //static v8::Handle<v8::Value> WrapOpen(const v8::Arguments &args)     { if (This) return This->FuncOpen(args);     else return v8::Undefined(); }
     80    static v8::Handle<v8::Value> WrapSubscribe(const v8::Arguments &args){ if (This) return This->FuncSubscribe(args);else return v8::Undefined(); }
    7581    static v8::Handle<v8::Value> WrapGetData(const v8::Arguments &args)  { if (This) return This->FuncGetData(args);  else return v8::Undefined(); }
    7682    static v8::Handle<v8::Value> WrapClose(const v8::Arguments &args)    { if (This) return This->FuncClose(args);    else return v8::Undefined(); }
     
    122128    virtual void *JsSubscribe(const std::string &) { return 0; };
    123129    virtual bool  JsUnsubscribe(const std::string &) { return false; };
     130
     131    virtual bool JsNewState(int, const std::string&, const std::string&) { return false; }
     132    virtual bool JsSetState(int) { return false; }
     133    virtual bool JsHasState(int) const { return false; }
     134    virtual bool JsHasState(const std::string &) const { return false; }
     135    virtual int  JsGetState(const std::string &) const { return -2; }
     136
    124137    virtual std::vector<Description> JsDescription(const std::string &) { return std::vector<Description>(); };
    125138    virtual std::pair<uint64_t, EventImp *> JsGetEvent(const std::string &) { return std::make_pair(0, (EventImp*)0); };
Note: See TracChangeset for help on using the changeset viewer.