Changeset 14626 for trunk/FACT++
- Timestamp:
- 11/15/12 17:40:17 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/InterpreterV8.cc
r14607 r14626 427 427 } 428 428 429 Handle<Value> InterpreterV8::FuncFile(const Arguments& args) 430 { 431 for (int i=0; i<1; i++) 432 { 433 const String::Utf8Value file(args[i]); 434 if (*file == NULL) 435 return ThrowException(String::New("File name missing")); 436 437 ifstream fin(*file); 438 if (!fin) 439 return ThrowException(String::New(("Error - Could not open file '"+string(*file)+"'").c_str())); 440 441 string buffer; 442 if (!getline(fin, buffer, '\0')) 443 return ThrowException(String::New(("Error - Could read file '"+string(*file)+"'").c_str())); 444 445 if (fin.fail()) 446 return ThrowException(String::New(("Error - Could read file '"+string(*file)+"'").c_str())); 447 448 return String::New(buffer.c_str()); 449 450 } 451 return Boolean::New(true); 452 } 453 429 454 Handle<Value> InterpreterV8::FuncVersion(const Arguments&) 430 455 { … … 663 688 ostringstream val; 664 689 val << setprecision(7) << *reinterpret_cast<const float*>(ptr); 665 Handle<Value> v=Number::New(stod(val.str())); 666 ptr+=4; 667 return v; 690 ptr += 4; 691 return Number::New(stod(val.str())); 668 692 } 669 693 case 'D': { Handle<Value> v=Number::New(*reinterpret_cast<const double*>(ptr)); ptr+=8; return v; } … … 709 733 const vector<Description> vec = JsDescription(str); 710 734 711 Handle< Array> ret = Array::New();735 Handle<Object> ret = Object::New(); 712 736 if (ret.IsEmpty()) 713 737 return Undefined(); … … 719 743 ret->Set(String::New("name"), String::New(str), ReadOnly); 720 744 ret->Set(String::New("format"), String::New(evt->GetFormat().c_str()), ReadOnly); 721 ret->Set(String::New("named"), Boolean::New(vec.size()>0), ReadOnly);722 745 ret->Set(String::New("qos"), Integer::New(evt->GetQoS()), ReadOnly); 723 746 ret->Set(String::New("size"), Integer::New(evt->GetSize()), ReadOnly); 724 747 ret->Set(String::New("counter"), Integer::New(counter), ReadOnly); 725 ret->Set(String::New("time"), date,ReadOnly);748 ret->Set(String::New("time"), date, ReadOnly); 726 749 727 750 // If no event was received (usually a disconnection event in … … 730 753 return ret; 731 754 755 // If names are available data will also be provided as an 756 // object. If an empty event was received, but names are available, 757 // the object will be empty. Otherwise 'obj' will be undefined. 758 // obj===undefined: no data received 759 // obj!==undefined, length==0: names for event available 760 // obj!==undefined, obj.length>0: names available, data received 761 Handle<Object> named = Object::New(); 762 if (named.IsEmpty()) 763 return Undefined(); 764 765 if (vec.size()>0) 766 ret->Set(String::New("obj"), named, ReadOnly); 767 732 768 // If valid data was received, but the size was zero, then 733 769 // null is returned as data 770 // data===undefined: no data received 771 // data===null: event received, but no data 772 // data.length>0: event received, contains data 734 773 if (evt->GetSize()==0 || evt->GetFormat().empty()) 735 774 { … … 743 782 const vector<string> tok(tokenizer.begin(), tokenizer.end()); 744 783 745 Handle< Array> obj= tok.size()>1 ? Array::New() : ret;746 if ( obj.IsEmpty())784 Handle<Object> arr = tok.size()>1 ? Array::New() : ret; 785 if (arr.IsEmpty()) 747 786 return Undefined(); 748 787 … … 770 809 const uint32_t cnt = it==tok.end() ? 1 : stoi(it->c_str()); 771 810 811 Handle<Value> v; 772 812 if (cnt==1) 773 813 { 774 const Handle<Value> v = Convert(type, ptr); 775 if (tok.size()>1) 776 obj->Set(pos-1, v); 777 if (!name.empty()) 778 obj->Set(String::New(name.c_str()), v); 814 v = Convert(type, ptr); 779 815 } 780 816 else 781 817 { 782 Handle< Array> a = Array::New(cnt);818 Handle<Object> a = Array::New(cnt); 783 819 if (a.IsEmpty()) 784 820 return Undefined(); … … 786 822 for (uint32_t i=0; i<cnt; i++) 787 823 a->Set(i, Convert(type, ptr)); 788 if (tok.size()>1) 789 obj->Set(pos-1, a); 790 if (!name.empty()) 791 obj->Set(String::New(name.c_str()), a); 824 825 v = a; 826 } 827 828 if (tok.size()>1) 829 arr->Set(pos-1, v); 830 831 if (!name.empty()) 832 { 833 const Handle<String> n = String::New(name.c_str()); 834 named->Set(n, v); 792 835 } 793 836 … … 797 840 798 841 if (tok.size()>1) 799 ret->Set(String::New("data"), obj, ReadOnly);842 ret->Set(String::New("data"), arr, ReadOnly); 800 843 801 844 return ret; … … 828 871 Handle<Value> InterpreterV8::FuncGetData(const Arguments &args) 829 872 { 830 if (args.Length()>1) 831 return ThrowException(String::New("Number of arguments must not be greatr than 1.")); 832 833 if (args.Length()==1 && !args[0]->IsInt32() && !args[0]->IsNull()) 834 return ThrowException(String::New("Argument 1 not an int32.")); 873 if (args.Length()>2) 874 return ThrowException(String::New("Number of arguments must not be greater than 2.")); 875 876 if (args.Length()>=1 && !args[0]->IsInt32() && !args[0]->IsNull()) 877 return ThrowException(String::New("Argument 1 not an uint32.")); 878 879 if (args.Length()==2 && !args[1]->IsBoolean()) 880 return ThrowException(String::New("Argument 2 not a boolean.")); 835 881 836 882 // Using a Javascript function has the advantage that it is fully 837 883 // interruptable without the need of C++ code 838 const bool null = args.Length()==1 && args[0]->IsNull(); 839 const int32_t timeout = args.Length()==1 ? args[0]->Int32Value() : 0; 884 const bool null = args.Length()>=1 && args[0]->IsNull(); 885 const int32_t timeout = args.Length()>=1 ? args[0]->Int32Value() : 0; 886 const bool named = args.Length()<2 || args[1]->BooleanValue(); 840 887 841 888 HandleScope handle_scope; … … 845 892 return Undefined(); 846 893 847 const Handle<String> data= String::New("data");848 const Handle<String> named = String::New("named");894 //const Handle<String> data = String::New("data"); 895 const Handle<String> object = String::New("obj"); 849 896 850 897 const String::Utf8Value name(args.Holder()->Get(String::New("name"))); … … 867 914 if (!val.IsEmpty() && val->IsObject()) 868 915 { 869 const Handle<Object> obj = val->ToObject(); 870 if (obj->Has(data) && obj->Get(named)->ToBoolean()->Value()) 916 if (!named) 871 917 return handle_scope.Close(val); 918 919 const Handle<Object> event = val->ToObject(); 920 const Handle<Value> obj = event->Get(object); 921 922 if (!obj.IsEmpty() && obj->IsObject()) 923 { 924 // Has names and data was received? 925 if (obj->ToObject()->GetOwnPropertyNames()->Length()>0) 926 return handle_scope.Close(val); 927 } 872 928 } 873 929 } … … 888 944 return exception.ReThrow(); 889 945 890 if (timeout >=0)946 if (timeout<0) 891 947 return Undefined(); 892 948 … … 1429 1485 Handle<Value> InterpreterV8::ExecuteInternal(const string &code) 1430 1486 { 1487 // Try/catch and re-throw hides our internal code from 1488 // the displayed exception showing the origin and shows 1489 // the user function instead. 1431 1490 TryCatch exception; 1491 1432 1492 const Handle<Value> result = ExecuteCode(code); 1433 return exception.HasCaught() ? exception.ReThrow() : result; 1493 if (exception.HasCaught()) 1494 exception.ReThrow(); 1495 1496 return result; 1434 1497 } 1435 1498 … … 1452 1515 const Handle<Value> result = script->Run(); 1453 1516 if (result.IsEmpty()) 1454 return result;1517 return Handle<Value>(); 1455 1518 1456 1519 // If all went well and the result wasn't undefined then print … … 1548 1611 bool InterpreterV8::JsRun(const string &filename, const map<string, string> &map) 1549 1612 { 1613 //const string argv = "--prof"; 1614 //V8::SetFlagsFromString(argv.c_str(), argv.size()); 1615 1550 1616 const Locker locker; 1551 1617 fThreadId = V8::GetCurrentThreadId(); … … 1571 1637 dim->Set(String::New("subscribe"), FunctionTemplate::New(WrapSubscribe), ReadOnly); 1572 1638 dim->Set(String::New("database"), FunctionTemplate::New(WrapDatabase), ReadOnly); 1639 //dim->Set(String::New("file"), FunctionTemplate::New(WrapFile), ReadOnly); 1573 1640 1574 1641 Handle<ObjectTemplate> onchange = ObjectTemplate::New(); … … 1611 1678 fGlobalContext->Global()->Set(String::New("arg"), args, ReadOnly); 1612 1679 1680 //V8::ResumeProfiler(); 1681 1613 1682 AddFormatToGlobal(); 1614 1683 … … 1621 1690 Locker::StartPreemption(10); 1622 1691 bool rc = ExecuteFile(filename, true); 1692 1623 1693 Locker::StopPreemption(); 1624 1694 … … 1627 1697 if (exception.HasCaught()) 1628 1698 rc = ReportException(&exception); 1699 1700 // IsProfilerPaused() 1701 // V8::PauseProfiler(); 1629 1702 1630 1703 // -----
Note:
See TracChangeset
for help on using the changeset viewer.