Changeset 14643
- Timestamp:
- 11/18/12 15:14:26 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/InterpreterV8.cc
r14642 r14643 21 21 22 22 #include "tools.h" 23 #include "externals/izstream.h" 23 24 24 25 using namespace std; … … 27 28 v8::Handle<v8::FunctionTemplate> InterpreterV8::fTemplateLocal; 28 29 v8::Handle<v8::FunctionTemplate> InterpreterV8::fTemplateSky; 29 //v8::Handle<v8::FunctionTemplate> InterpreterV8::fTemplateMoon;30 v8::Handle<v8::FunctionTemplate> InterpreterV8::fTemplateEvent; 30 31 //v8::Handle<v8::FunctionTemplate> InterpreterV8::fTemplateDatabase; 31 32 … … 73 74 // until it goes out of scope. Handles which are not needed anymore are 74 75 // 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 and76 // Close(). E.g., String::AsciiValue does not create a new handle and 76 77 // hence does not need a HandleScope. Any ::New will need a handle scope. 77 78 // Forgetting the HandleScope could in principle fill your memory, … … 212 213 return ThrowException(String::New("Argument 1 must be a string.")); 213 214 214 const String:: Utf8Value str(args[0]);215 const String::AsciiValue str(args[0]); 215 216 216 217 string command = *str; … … 219 220 for (int i=1; i<args.Length(); i++) 220 221 { 221 const String:: Utf8Value arg(args[i]);222 const String::AsciiValue arg(args[i]); 222 223 if (args[i]->IsString()) 223 224 command += " \""+string(*arg)+"\""; … … 252 253 const string index = args[1]->IsInt32() ? "s.index" : "s.name"; 253 254 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]) : ""; 256 257 const string arg1 = args[1]->IsString() ? ("\""+state+"\"") : to_string(args[1]->Int32Value()); 257 258 … … 295 296 // Return state.name/state.index 296 297 297 const String:: Utf8Value str(args[0]);298 const String::AsciiValue str(args[0]); 298 299 299 300 const State rc = JsState(*str); 300 301 //if (rc.first<=-256) 302 // return Undefined(); 301 if (rc.index<=-256) 302 return Object::New(); 303 303 304 304 HandleScope handle_scope; 305 305 306 306 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); 309 309 310 310 const Local<Value> date = Date::New(rc.time.JavaDate()); … … 328 328 329 329 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]); 332 332 333 333 if (index<10 || index>255) … … 372 372 else 373 373 { 374 const string name = *String:: Utf8Value(args[0]);374 const string name = *String::AsciiValue(args[0]); 375 375 index = JsGetState(name); 376 376 if (index==-2) … … 416 416 for (int i=0; i<args.Length(); i++) 417 417 { 418 const String:: Utf8Value str(args[i]);418 const String::AsciiValue str(args[i]); 419 419 if (*str) 420 420 JsPrint(*str); … … 427 427 for (int i=0; i<args.Length(); i++) 428 428 { 429 const String:: Utf8Value str(args[i]);429 const String::AsciiValue str(args[i]); 430 430 if (*str) 431 431 JsAlarm(*str); … … 442 442 for (int i=0; i<args.Length(); i++) 443 443 { 444 const String:: Utf8Value str(args[i]);444 const String::AsciiValue str(args[i]); 445 445 if (*str) 446 446 JsOut(*str); … … 456 456 for (int i=0; i<args.Length(); i++) 457 457 { 458 const String:: Utf8Value file(args[i]);458 const String::AsciiValue file(args[i]); 459 459 if (*file == NULL) 460 460 return ThrowException(String::New("File name missing")); … … 468 468 Handle<Value> InterpreterV8::FuncFile(const Arguments& args) 469 469 { 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 { 480 493 string buffer; 481 494 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); 491 518 } 492 519 … … 531 558 string query; 532 559 for (int i=0; i<args.Length(); i++) 533 query += string(" ") + *String:: Utf8Value(args[i]);560 query += string(" ") + *String::AsciiValue(args[i]); 534 561 query.erase(0, 1); 535 562 … … 693 720 // return Constructor(fTemplateDatabase, args); 694 721 695 Database *db = new Database(*String:: Utf8Value(args[0]));722 Database *db = new Database(*String::AsciiValue(args[0])); 696 723 fDatabases.push_back(db); 697 724 … … 756 783 //const void *ptr = Local<External>::Cast(args.Holder()->GetInternalField(0))->Value(); 757 784 758 const String:: Utf8Value str(args.Holder()->Get(String::New("name")));785 const String::AsciiValue str(args.Holder()->Get(String::New("name"))); 759 786 760 787 const auto it = fReverseMap.find(*str); … … 774 801 const vector<Description> vec = JsDescription(str); 775 802 776 Handle<Object> ret = Object::New();803 Handle<Object> ret = fTemplateEvent->GetFunction()->NewInstance();//Object::New(); 777 804 if (ret.IsEmpty()) 778 805 return Undefined(); … … 782 809 return Undefined(); 783 810 784 ret->Set(String::New("name"), String::New(str), ReadOnly);811 ret->Set(String::New("name"), String::New(str), ReadOnly); 785 812 ret->Set(String::New("format"), String::New(evt->GetFormat().c_str()), ReadOnly); 786 813 ret->Set(String::New("qos"), Integer::New(evt->GetQoS()), ReadOnly); 787 814 ret->Set(String::New("size"), Integer::New(evt->GetSize()), ReadOnly); 788 815 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); 795 818 796 819 // If names are available data will also be provided as an … … 801 824 // obj!==undefined, obj.length>0: names available, data received 802 825 Handle<Object> named = Object::New(); 803 if (named.IsEmpty())804 return Undefined();805 806 826 if (vec.size()>0) 807 827 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; 808 833 809 834 // If valid data was received, but the size was zero, then … … 895 920 HandleScope handle_scope; 896 921 897 const String:: Utf8Value str(args.Holder()->Get(String::New("name")));922 const String::AsciiValue str(args.Holder()->Get(String::New("name"))); 898 923 899 924 const pair<uint64_t, EventImp *> p = JsGetEvent(*str); … … 936 961 const Handle<String> object = String::New("obj"); 937 962 938 const String:: Utf8Value name(args.Holder()->Get(String::New("name")));963 const String::AsciiValue name(args.Holder()->Get(String::New("name"))); 939 964 940 965 TryCatch exception; … … 1038 1063 if (ret->IsNativeError()) 1039 1064 { 1040 JsException(service+".onchange callback - "+*String:: Utf8Value(ret));1065 JsException(service+".onchange callback - "+*String::AsciiValue(ret)); 1041 1066 V8::TerminateExecution(fThreadId); 1042 1067 } … … 1046 1071 { 1047 1072 // 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); 1049 1074 auto it = fStateCallbacks.find(server); 1050 1075 … … 1084 1109 1085 1110 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);1089 1111 obj->Set(String::New("server"), String::New(server.c_str()), ReadOnly); 1090 1112 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 } 1094 1122 1095 1123 // ------------------------------------------------------------------- … … 1124 1152 return ThrowException(String::New("Argument 1 must be a string.")); 1125 1153 1126 const String:: Utf8Value str(args[0]);1154 const String::AsciiValue str(args[0]); 1127 1155 1128 1156 if (!args.IsConstructCall()) … … 1452 1480 return true; 1453 1481 1454 const String:: Utf8Value exception(except);1482 const String::AsciiValue exception(except); 1455 1483 1456 1484 const Handle<Message> message = try_catch.Message(); … … 1463 1491 { 1464 1492 // Print (filename):(line number): (message). 1465 const String:: Utf8Value filename(message->GetScriptResourceName());1493 const String::AsciiValue filename(message->GetScriptResourceName()); 1466 1494 1467 1495 out << *filename; … … 1480 1508 1481 1509 // Print line of source code. 1482 const String:: Utf8Value sourceline(message->GetSourceLine());1510 const String::AsciiValue sourceline(message->GetSourceLine()); 1483 1511 if (*sourceline) 1484 1512 JsException(*sourceline); … … 1495 1523 JsException(out.str()); 1496 1524 1497 const String:: Utf8Value stack_trace(try_catch.StackTrace());1525 const String::AsciiValue stack_trace(try_catch.StackTrace()); 1498 1526 if (stack_trace.length()<=0) 1499 1527 return false; … … 1563 1591 // the returned value. 1564 1592 if (!result.IsEmpty() && result->IsUndefined()) 1565 JsResult(*String:: Utf8Value(result));1593 JsResult(*String::AsciiValue(result)); 1566 1594 1567 1595 return handle_scope.Close(result); … … 1608 1636 { 1609 1637 const string code = 1610 " dim.format= function(str, arr)"1638 "String.form = function(str, arr)" 1611 1639 "{" 1612 1640 "var i = -1;" … … 1652 1680 "String.prototype.$ = function()" 1653 1681 "{" 1654 "return dim.format(this, Array.prototype.slice.call(arguments));"1682 "return String.form(this, Array.prototype.slice.call(arguments));" 1655 1683 "}"/* 1656 1684 "\n" … … 1689 1717 dim->Set(String::New("send"), FunctionTemplate::New(WrapSend), ReadOnly); 1690 1718 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);1694 1719 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 1698 1726 // new class State ? 1699 // newState -> return State?1700 // setState -> return State?1701 // getState -> return State?1702 1727 1703 1728 Handle<ObjectTemplate> onchange = ObjectTemplate::New(); … … 1706 1731 1707 1732 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); 1709 1735 global->Set(String::New("include"), FunctionTemplate::New(WrapInclude), ReadOnly); 1710 1736 global->Set(String::New("exit"), FunctionTemplate::New(WrapExit), ReadOnly); … … 1725 1751 thread->InstanceTemplate()->SetInternalFieldCount(1); 1726 1752 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; 1727 1764 1728 1765 #ifdef HAVE_NOVA
Note:
See TracChangeset
for help on using the changeset viewer.