Changeset 14643


Ignore:
Timestamp:
11/18/12 15:14:26 (12 years ago)
Author:
tbretz
Message:
Changed Utf8Value to AsciiValue; some minor changes and fixed about how recived events and state information looks like; added function template for Event; renamed dim.format to String.form; Changed dim.file to File and implemented the possibility to split the file into an array
File:
1 edited

Legend:

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

    r14642 r14643  
    2121
    2222#include "tools.h"
     23#include "externals/izstream.h"
    2324
    2425using namespace std;
     
    2728v8::Handle<v8::FunctionTemplate> InterpreterV8::fTemplateLocal;
    2829v8::Handle<v8::FunctionTemplate> InterpreterV8::fTemplateSky;
    29 //v8::Handle<v8::FunctionTemplate> InterpreterV8::fTemplateMoon;
     30v8::Handle<v8::FunctionTemplate> InterpreterV8::fTemplateEvent;
    3031//v8::Handle<v8::FunctionTemplate> InterpreterV8::fTemplateDatabase;
    3132
     
    7374// until it goes out of scope. Handles which are not needed anymore are
    7475// then deleted. To return a handle from a HandleScope you need to use
    75 // Close(). E.g., String::Utf8Value does not create a new handle and
     76// Close(). E.g., String::AsciiValue does not create a new handle and
    7677// hence does not need a HandleScope. Any ::New will need a handle scope.
    7778// Forgetting the HandleScope could in principle fill your memory,
     
    212213        return ThrowException(String::New("Argument 1 must be a string."));
    213214
    214     const String::Utf8Value str(args[0]);
     215    const String::AsciiValue str(args[0]);
    215216
    216217    string command = *str;
     
    219220    for (int i=1; i<args.Length(); i++)
    220221    {
    221         const String::Utf8Value arg(args[i]);
     222        const String::AsciiValue arg(args[i]);
    222223        if (args[i]->IsString())
    223224            command += " \""+string(*arg)+"\"";
     
    252253    const string index   = args[1]->IsInt32() ? "s.index" : "s.name";
    253254    const bool   timeout = args.Length()==3;
    254     const string arg0    = *String::Utf8Value(args[0]);
    255     const string state   = args[1]->IsString() ? *String::Utf8Value(args[1]) : "";
     255    const string arg0    = *String::AsciiValue(args[0]);
     256    const string state   = args[1]->IsString() ? *String::AsciiValue(args[1]) : "";
    256257    const string arg1    = args[1]->IsString() ? ("\""+state+"\"") : to_string(args[1]->Int32Value());
    257258
     
    295296    // Return state.name/state.index
    296297
    297     const String::Utf8Value str(args[0]);
     298    const String::AsciiValue str(args[0]);
    298299
    299300    const State rc = JsState(*str);
    300 
    301     //if (rc.first<=-256)
    302     //    return Undefined();
     301    if (rc.index<=-256)
     302        return Object::New();
    303303
    304304    HandleScope handle_scope;
    305305
    306306    Handle<ObjectTemplate> obj = ObjectTemplate::New();
    307     obj->Set(String::New("index"), rc.index<=-256?Undefined():Integer::New(rc.index),       ReadOnly);
    308     obj->Set(String::New("name"),  rc.index<=-256?Undefined():String::New(rc.name.c_str()), ReadOnly);
     307    obj->Set(String::New("index"), Integer::New(rc.index),       ReadOnly);
     308    obj->Set(String::New("name"),  String::New(rc.name.c_str()), ReadOnly);
    309309
    310310    const Local<Value> date = Date::New(rc.time.JavaDate());
     
    328328
    329329    const uint32_t index   = args[0]->Int32Value();
    330     const string   name    = *String::Utf8Value(args[1]);
    331     const string   comment = *String::Utf8Value(args[2]);
     330    const string   name    = *String::AsciiValue(args[1]);
     331    const string   comment = *String::AsciiValue(args[2]);
    332332
    333333    if (index<10 || index>255)
     
    372372    else
    373373    {
    374         const string name = *String::Utf8Value(args[0]);
     374        const string name = *String::AsciiValue(args[0]);
    375375        index = JsGetState(name);
    376376        if (index==-2)
     
    416416    for (int i=0; i<args.Length(); i++)
    417417    {
    418         const String::Utf8Value str(args[i]);
     418        const String::AsciiValue str(args[i]);
    419419        if (*str)
    420420            JsPrint(*str);
     
    427427    for (int i=0; i<args.Length(); i++)
    428428    {
    429         const String::Utf8Value str(args[i]);
     429        const String::AsciiValue str(args[i]);
    430430        if (*str)
    431431            JsAlarm(*str);
     
    442442    for (int i=0; i<args.Length(); i++)
    443443    {
    444         const String::Utf8Value str(args[i]);
     444        const String::AsciiValue str(args[i]);
    445445        if (*str)
    446446            JsOut(*str);
     
    456456    for (int i=0; i<args.Length(); i++)
    457457    {
    458         const String::Utf8Value file(args[i]);
     458        const String::AsciiValue file(args[i]);
    459459        if (*file == NULL)
    460460            return ThrowException(String::New("File name missing"));
     
    468468Handle<Value> InterpreterV8::FuncFile(const Arguments& args)
    469469{
    470     for (int i=0; i<1; i++)
    471     {
    472         const String::Utf8Value file(args[i]);
    473         if (*file == NULL)
    474             return ThrowException(String::New("File name missing"));
    475 
    476         ifstream fin(*file);
    477         if (!fin)
    478             return ThrowException(String::New(("Error - Could not open file '"+string(*file)+"'").c_str()));
    479 
     470    if (args.Length()!=1 && args.Length()!=2)
     471        return ThrowException(String::New("Number of arguments must be one or two."));
     472
     473    const String::AsciiValue file(args[0]);
     474    if (*file == NULL)
     475        return ThrowException(String::New("File name missing"));
     476
     477    if (args.Length()==2 && !args[1]->IsString())
     478        return ThrowException(String::New("Second argument must be a string."));
     479
     480    const string delim = args.Length()==2 ? *String::AsciiValue(args[1]) : "";
     481
     482    if (args.Length()==2 && delim.size()!=1)
     483        return ThrowException(String::New("Second argument must be a string of length 1."));
     484
     485    HandleScope handle_scope;
     486
     487    izstream fin(*file);
     488    if (!fin)
     489        return ThrowException(String::New(errno!=0?strerror(errno):"Insufficient memory for decompression"));
     490
     491    if (args.Length()==1)
     492    {
    480493        string buffer;
    481494        if (!getline(fin, buffer, '\0'))
    482             return ThrowException(String::New(("Error - Could read file '"+string(*file)+"'").c_str()));
    483 
    484         if (fin.fail())
    485             return ThrowException(String::New(("Error - Could read file '"+string(*file)+"'").c_str()));
    486 
    487         return String::New(buffer.c_str());
    488 
    489     }
    490     return Boolean::New(true);
     495            return ThrowException(String::New(strerror(errno)));
     496
     497        Handle<Value> str = StringObject::New(String::New(buffer.c_str()));
     498        StringObject::Cast(*str)->Set(String::New("name"), String::New(*file));
     499        return handle_scope.Close(str);
     500    }
     501
     502    Handle<Array> arr = Array::New();
     503    if (arr.IsEmpty())
     504        return Undefined();
     505
     506    int i=0;
     507    string buffer;
     508    while (getline(fin, buffer, delim[0]))
     509        arr->Set(i++, String::New(buffer.c_str()));
     510
     511    if ((fin.fail() && !fin.eof()) || fin.bad())
     512        return ThrowException(String::New(strerror(errno)));
     513
     514    arr->Set(String::New("name"),  String::New(*file));
     515    arr->Set(String::New("delim"), String::New(delim.c_str(), 1));
     516
     517    return handle_scope.Close(arr);
    491518}
    492519
     
    531558    string query;
    532559    for (int i=0; i<args.Length(); i++)
    533         query += string(" ") + *String::Utf8Value(args[i]);
     560        query += string(" ") + *String::AsciiValue(args[i]);
    534561    query.erase(0, 1);
    535562
     
    693720        //    return Constructor(fTemplateDatabase, args);
    694721
    695         Database *db = new Database(*String::Utf8Value(args[0]));
     722        Database *db = new Database(*String::AsciiValue(args[0]));
    696723        fDatabases.push_back(db);
    697724
     
    756783    //const void *ptr = Local<External>::Cast(args.Holder()->GetInternalField(0))->Value();
    757784
    758     const String::Utf8Value str(args.Holder()->Get(String::New("name")));
     785    const String::AsciiValue str(args.Holder()->Get(String::New("name")));
    759786
    760787    const auto it = fReverseMap.find(*str);
     
    774801    const vector<Description> vec = JsDescription(str);
    775802
    776     Handle<Object> ret = Object::New();
     803    Handle<Object> ret = fTemplateEvent->GetFunction()->NewInstance();//Object::New();
    777804    if (ret.IsEmpty())
    778805        return Undefined();
     
    782809        return Undefined();
    783810
    784     ret->Set(String::New("name"),    String::New(str),             ReadOnly);
     811    ret->Set(String::New("name"),    String::New(str),              ReadOnly);
    785812    ret->Set(String::New("format"),  String::New(evt->GetFormat().c_str()), ReadOnly);
    786813    ret->Set(String::New("qos"),     Integer::New(evt->GetQoS()),   ReadOnly);
    787814    ret->Set(String::New("size"),    Integer::New(evt->GetSize()),  ReadOnly);
    788815    ret->Set(String::New("counter"), Integer::New(counter),         ReadOnly);
    789     ret->Set(String::New("time"),    date,  ReadOnly);
    790 
    791     // If no event was received (usually a disconnection event in
    792     // the context of FACT++), no data is returned
    793     if (evt->IsEmpty())
    794         return ret;
     816    if (evt->GetJavaDate()>0)
     817        ret->Set(String::New("time"), date, ReadOnly);
    795818
    796819    // If names are available data will also be provided as an
     
    801824    // obj!==undefined, obj.length>0: names available, data received
    802825    Handle<Object> named = Object::New();
    803     if (named.IsEmpty())
    804         return Undefined();
    805 
    806826    if (vec.size()>0)
    807827        ret->Set(String::New("obj"), named, ReadOnly);
     828
     829    // If no event was received (usually a disconnection event in
     830    // the context of FACT++), no data is returned
     831    if (evt->IsEmpty())
     832        return ret;
    808833
    809834    // If valid data was received, but the size was zero, then
     
    895920    HandleScope handle_scope;
    896921
    897     const String::Utf8Value str(args.Holder()->Get(String::New("name")));
     922    const String::AsciiValue str(args.Holder()->Get(String::New("name")));
    898923
    899924    const pair<uint64_t, EventImp *> p = JsGetEvent(*str);
     
    936961    const Handle<String> object = String::New("obj");
    937962
    938     const String::Utf8Value name(args.Holder()->Get(String::New("name")));
     963    const String::AsciiValue name(args.Holder()->Get(String::New("name")));
    939964
    940965    TryCatch exception;
     
    10381063    if (ret->IsNativeError())
    10391064    {
    1040         JsException(service+".onchange callback - "+*String::Utf8Value(ret));
     1065        JsException(service+".onchange callback - "+*String::AsciiValue(ret));
    10411066        V8::TerminateExecution(fThreadId);
    10421067    }
     
    10461071{
    10471072    // Returns the value if the setter intercepts the request. Otherwise, returns an empty handle.
    1048     const string server = *String::Utf8Value(prop);
     1073    const string server = *String::AsciiValue(prop);
    10491074    auto it = fStateCallbacks.find(server);
    10501075
     
    10841109
    10851110    Handle<ObjectTemplate> obj = ObjectTemplate::New();
    1086     obj->Set(String::New("index"),   state.index<=-256?Undefined():Integer::New(state.index),          ReadOnly);
    1087     obj->Set(String::New("name"),    state.index<=-256?Undefined():String::New(state.name.c_str()),    ReadOnly);
    1088     obj->Set(String::New("comment"), state.index<=-256?Undefined():String::New(state.comment.c_str()), ReadOnly);
    10891111    obj->Set(String::New("server"),  String::New(server.c_str()), ReadOnly);
    10901112
    1091     const Local<Value> date = Date::New(state.time.JavaDate());
    1092     if (state.index>-256 && !date.IsEmpty())
    1093         obj->Set(String::New("time"), date);
     1113    if (state.index>-256)
     1114    {
     1115        obj->Set(String::New("index"),   Integer::New(state.index),          ReadOnly);
     1116        obj->Set(String::New("name"),    String::New(state.name.c_str()),    ReadOnly);
     1117        obj->Set(String::New("comment"), String::New(state.comment.c_str()), ReadOnly);
     1118        const Local<Value> date = Date::New(state.time.JavaDate());
     1119        if (!date.IsEmpty())
     1120            obj->Set(String::New("time"), date);
     1121    }
    10941122
    10951123    // -------------------------------------------------------------------
     
    11241152        return ThrowException(String::New("Argument 1 must be a string."));
    11251153
    1126     const String::Utf8Value str(args[0]);
     1154    const String::AsciiValue str(args[0]);
    11271155
    11281156    if (!args.IsConstructCall())
     
    14521480        return true;
    14531481
    1454     const String::Utf8Value exception(except);
     1482    const String::AsciiValue exception(except);
    14551483
    14561484    const Handle<Message> message = try_catch.Message();
     
    14631491    {
    14641492        // Print (filename):(line number): (message).
    1465         const String::Utf8Value filename(message->GetScriptResourceName());
     1493        const String::AsciiValue filename(message->GetScriptResourceName());
    14661494
    14671495        out << *filename;
     
    14801508
    14811509    // Print line of source code.
    1482     const String::Utf8Value sourceline(message->GetSourceLine());
     1510    const String::AsciiValue sourceline(message->GetSourceLine());
    14831511    if (*sourceline)
    14841512        JsException(*sourceline);
     
    14951523    JsException(out.str());
    14961524
    1497     const String::Utf8Value stack_trace(try_catch.StackTrace());
     1525    const String::AsciiValue stack_trace(try_catch.StackTrace());
    14981526    if (stack_trace.length()<=0)
    14991527        return false;
     
    15631591    // the returned value.
    15641592    if (!result.IsEmpty() && result->IsUndefined())
    1565         JsResult(*String::Utf8Value(result));
     1593        JsResult(*String::AsciiValue(result));
    15661594
    15671595    return handle_scope.Close(result);
     
    16081636{
    16091637    const string code =
    1610         "dim.format = function(str, arr)"
     1638        "String.form = function(str, arr)"
    16111639        "{"
    16121640            "var i = -1;"
     
    16521680        "String.prototype.$ = function()"
    16531681        "{"
    1654             "return dim.format(this, Array.prototype.slice.call(arguments));"
     1682            "return String.form(this, Array.prototype.slice.call(arguments));"
    16551683        "}"/*
    16561684        "\n"
     
    16891717    dim->Set(String::New("send"),      FunctionTemplate::New(WrapSend),      ReadOnly);
    16901718    dim->Set(String::New("state"),     FunctionTemplate::New(WrapState),     ReadOnly);
    1691     dim->Set(String::New("newState"),  FunctionTemplate::New(WrapNewState),  ReadOnly);
    1692     dim->Set(String::New("setState"),  FunctionTemplate::New(WrapSetState),  ReadOnly);
    1693     dim->Set(String::New("getState"),  FunctionTemplate::New(WrapGetState),  ReadOnly);
    16941719    dim->Set(String::New("sleep"),     FunctionTemplate::New(WrapSleep),     ReadOnly);
    1695     dim->Set(String::New("file"),      FunctionTemplate::New(WrapFile),      ReadOnly);
    1696 
    1697     // file      -> File    - class?
     1720
     1721    Handle<ObjectTemplate> dimctrl = ObjectTemplate::New();
     1722    dimctrl->Set(String::New("newState"),  FunctionTemplate::New(WrapNewState),  ReadOnly);
     1723    dimctrl->Set(String::New("setState"),  FunctionTemplate::New(WrapSetState),  ReadOnly);
     1724    dimctrl->Set(String::New("getState"),  FunctionTemplate::New(WrapGetState),  ReadOnly);
     1725
    16981726    // new class State ?
    1699     // newState -> return State?
    1700     // setState -> return State?
    1701     // getState -> return State?
    17021727
    17031728    Handle<ObjectTemplate> onchange = ObjectTemplate::New();
     
    17061731
    17071732    Handle<ObjectTemplate> global = ObjectTemplate::New();
    1708     global->Set(String::New("dim"), dim, ReadOnly);
     1733    global->Set(String::New("dim"),      dim,     ReadOnly);
     1734    global->Set(String::New("dimctrl"),  dimctrl, ReadOnly);
    17091735    global->Set(String::New("include"),  FunctionTemplate::New(WrapInclude),                ReadOnly);
    17101736    global->Set(String::New("exit"),     FunctionTemplate::New(WrapExit),                   ReadOnly);
     
    17251751    thread->InstanceTemplate()->SetInternalFieldCount(1);
    17261752    global->Set(String::New("Thread"), thread, ReadOnly);
     1753
     1754    Handle<FunctionTemplate> file = FunctionTemplate::New(WrapFile);
     1755    file->SetClassName(String::New("File"));
     1756    file->InstanceTemplate()->SetInternalFieldCount(1);
     1757    global->Set(String::New("File"), file, ReadOnly);
     1758
     1759    Handle<FunctionTemplate> evt = FunctionTemplate::New();
     1760    evt->SetClassName(String::New("Event"));
     1761    global->Set(String::New("Event"), evt, ReadOnly);
     1762
     1763    fTemplateEvent = evt;
    17271764
    17281765#ifdef HAVE_NOVA
Note: See TracChangeset for help on using the changeset viewer.