Changeset 14629 for trunk/FACT++
- Timestamp:
- 11/16/12 23:05:41 (12 years ago)
- Location:
- trunk/FACT++/src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/InterpreterV8.cc
r14626 r14629 24 24 using namespace std; 25 25 using namespace v8; 26 27 v8::Handle<v8::FunctionTemplate> InterpreterV8::fTemplateLocal; 28 v8::Handle<v8::FunctionTemplate> InterpreterV8::fTemplateSky; 29 v8::Handle<v8::FunctionTemplate> InterpreterV8::fTemplateMoon; 30 v8::Handle<v8::FunctionTemplate> InterpreterV8::fTemplateDatabase; 26 31 27 32 … … 327 332 return ThrowException(String::New("State name must not contain : or =.")); 328 333 329 if (JsHasState(index) || JsHasState(name)) 334 struct Find : State 335 { 336 Find(int idx, const string &n) : State(idx, n) { } 337 bool operator()(const pair<int, string> &p) { return index==p.first || name==p.second; } 338 }; 339 340 if (find_if(fStates.begin(), fStates.end(), Find(index, name))!=fStates.end()) 330 341 { 331 342 const string what = … … 363 374 364 375 return Boolean::New(JsSetState(index)); 376 } 377 378 Handle<Value> InterpreterV8::FuncGetState(const Arguments& args) 379 { 380 if (args.Length()>0) 381 return ThrowException(String::New("getState must not take arguments.")); 382 383 const State state = JsGetCurrentState(); 384 385 HandleScope handle_scope; 386 387 Handle<Object> rc = Object::New(); 388 if (rc.IsEmpty()) 389 return Undefined(); 390 391 rc->Set(String::New("index"), Integer::New(state.index), ReadOnly); 392 rc->Set(String::New("name"), String::New(state.name.c_str()), ReadOnly); 393 rc->Set(String::New("description"), String::New(state.comment.c_str()), ReadOnly); 394 395 return handle_scope.Close(rc); 365 396 } 366 397 … … 517 548 for (vector<mysqlpp::Row>::const_iterator it=res.begin(); it<res.end(); it++) 518 549 { 519 Handle< Array> row = Array::New();550 Handle<Object> row = Object::New(); 520 551 if (row.IsEmpty()) 521 552 return Undefined(); … … 567 598 else 568 599 row->Set(name, Integer::New((int32_t)(*it)[i]), ReadOnly); 600 continue; 569 601 } 570 602 … … 649 681 fDatabases.push_back(db); 650 682 651 Handle<ObjectTemplate> tem = ObjectTemplate::New(); 652 tem->Set(String::New("user"), String::New(db->user.c_str()), ReadOnly); 653 tem->Set(String::New("server"), String::New(db->server.c_str()), ReadOnly); 654 tem->Set(String::New("database"), String::New(db->db.c_str()), ReadOnly); 655 tem->Set(String::New("port"), db->port==0?Undefined():Integer::NewFromUnsigned(db->port), ReadOnly); 656 tem->Set(String::New("query"), FunctionTemplate::New(WrapDbQuery), ReadOnly); 657 tem->Set(String::New("close"), FunctionTemplate::New(WrapDbClose), ReadOnly); 658 tem->SetInternalFieldCount(1); 659 660 Handle<Object> obj = tem->NewInstance(); 661 if (obj.IsEmpty()) 683 Handle<Object> This = args.This(); 684 if (!args.IsConstructCall()) 685 { 686 Handle<ObjectTemplate> T = fTemplateDatabase->PrototypeTemplate(); 687 if (T.IsEmpty()) 688 return Undefined(); 689 690 This = T->NewInstance(); 691 } 692 if (This.IsEmpty()) 662 693 return Undefined(); 663 694 664 obj->SetInternalField(0, External::New(db)); 665 666 return handle_scope.Close(obj); 695 This->Set(String::New("user"), String::New(db->user.c_str()), ReadOnly); 696 This->Set(String::New("server"), String::New(db->server.c_str()), ReadOnly); 697 This->Set(String::New("database"), String::New(db->db.c_str()), ReadOnly); 698 This->Set(String::New("port"), db->port==0?Undefined():Integer::NewFromUnsigned(db->port), ReadOnly); 699 This->Set(String::New("query"), FunctionTemplate::New(WrapDbQuery)->GetFunction(), ReadOnly); 700 This->Set(String::New("close"), FunctionTemplate::New(WrapDbClose)->GetFunction(), ReadOnly); 701 This->SetInternalField(0, External::New(db)); 702 703 return handle_scope.Close(This); 667 704 } 668 705 catch (const exception &e) … … 1143 1180 } 1144 1181 1145 Handle<Value> InterpreterV8::LocalToString(const Arguments &/*args*/)1146 {1147 return String::New("[object Local]");1148 /*1149 HandleScope handle_scope;1150 1151 const Handle<Object> This = args.This();1152 1153 Handle<String> zd = This->Get(String::New("zd"))->ToString();1154 Handle<String> az = This->Get(String::New("az"))->ToString();1155 1156 return String::New("TEST");1157 */1158 }1159 1160 Handle<Value> InterpreterV8::SkyToString(const Arguments &/*args*/)1161 {1162 return String::New("[object Sky]");1163 }1164 1165 Handle<Value> InterpreterV8::MoonToString(const Arguments &/*args*/)1166 {1167 return String::New("[object Moon]");1168 }1169 1170 Handle<Value> InterpreterV8::ConstructLocal(double zd, double az, Handle<Value> time)1171 {1172 Handle<ObjectTemplate> loc = ObjectTemplate::New();1173 1174 loc->Set(String::New("zd"), Number::New(zd), ReadOnly);1175 loc->Set(String::New("az"), Number::New(az), ReadOnly);1176 loc->Set(String::New("toSky"), FunctionTemplate::New(LocalToSky), ReadOnly);1177 loc->Set(String::New("toString"), FunctionTemplate::New(LocalToString), ReadOnly);1178 if (!time.IsEmpty())1179 loc->Set(String::New("time"), time);1180 1181 return loc->NewInstance();1182 }1183 1184 Handle<Value> InterpreterV8::ConstructSky(double ra, double dec, Handle<Value> time, bool ismoon)1185 {1186 Handle<ObjectTemplate> sky = ObjectTemplate::New();1187 1188 sky->Set(String::New("ra"), Number::New(ra), ReadOnly);1189 sky->Set(String::New("dec"), Number::New(dec), ReadOnly);1190 sky->Set(String::New("toLocal"), FunctionTemplate::New(ismoon?MoonToLocal :SkyToLocal), ReadOnly);1191 sky->Set(String::New("toString"), FunctionTemplate::New(ismoon?MoonToString:SkyToString), ReadOnly);1192 if (!time.IsEmpty())1193 sky->Set(String::New("time"), time);1194 1195 return sky->NewInstance();1196 }1197 1198 1182 Handle<Value> InterpreterV8::LocalDist(const Arguments &args) 1199 1183 { … … 1285 1269 ln_get_equ_from_hrz(&hrz, &obs, utc.JD(), &equ); 1286 1270 1287 return handle_scope.Close(ConstructSky(equ.ra/15, equ.dec, date)); 1271 // ----------------------------- 1272 1273 Handle<Value> arg[] = { Number::New(equ.ra/15), Number::New(equ.dec), date }; 1274 return fTemplateSky->GetFunction()->NewInstance(3, arg); 1275 1276 //return handle_scope.Close(ConstructSky(equ.ra/15, equ.dec, date)); 1288 1277 } 1289 1278 … … 1317 1306 ln_get_hrz_from_equ(&equ, &obs, utc.JD(), &hrz); 1318 1307 1319 return handle_scope.Close(ConstructLocal(90-hrz.alt, hrz.az, date)); 1308 Handle<Value> arg[] = { Number::New(90-hrz.alt), Number::New(hrz.az), date }; 1309 return fTemplateLocal->GetFunction()->NewInstance(3, arg); 1320 1310 } 1321 1311 … … 1334 1324 HandleScope handle_scope; 1335 1325 1336 const Local<Value> date = 1337 args.Length()==0 ? Date::New(Time().JavaDate()) : args.This()->Get(String::New("time")); 1338 if (date.IsEmpty()) 1326 const Local<Value> date = args.This()->Get(String::New("time")); 1327 if (date.IsEmpty() || date->IsUndefined() ) 1339 1328 return Undefined(); 1340 1329 … … 1349 1338 ln_get_hrz_from_equ(&equ, &obs, utc.JD(), &hrz); 1350 1339 1351 return handle_scope.Close(ConstructLocal(90-hrz.alt, hrz.az, date)); 1340 Handle<Value> arg[] = { Number::New(90-hrz.alt), Number::New(hrz.az), date }; 1341 return fTemplateLocal->GetFunction()->NewInstance(3, arg); 1352 1342 } 1353 1343 … … 1370 1360 ln_get_lunar_equ_coords_prec(utc.JD(), &equ, 0.01); 1371 1361 1372 return handle_scope.Close(ConstructSky(equ.ra/15, equ.dec, date, true)); 1362 // ---------------------------- 1363 1364 Handle<Object> This = args.This(); 1365 if (!args.IsConstructCall()) 1366 { 1367 Handle<ObjectTemplate> T = fTemplateMoon->PrototypeTemplate(); 1368 if (T.IsEmpty()) 1369 return Undefined(); 1370 1371 This = T->NewInstance(); 1372 } 1373 if (This.IsEmpty()) 1374 return Undefined(); 1375 1376 Handle<Function> function = 1377 FunctionTemplate::New(MoonToLocal)->GetFunction(); 1378 if (function.IsEmpty()) 1379 return Undefined(); 1380 1381 This->Set(String::New("ra"), Number::New(equ.ra/15), ReadOnly); 1382 This->Set(String::New("dec"), Number::New(equ.dec), ReadOnly); 1383 This->Set(String::New("toLocal"), function, ReadOnly); 1384 This->Set(String::New("time"), date, ReadOnly); 1385 1386 return This; 1387 1388 //return handle_scope.Close(ConstructSky(equ.ra/15, equ.dec, date, true)); 1373 1389 } 1374 1390 1375 1391 Handle<Value> InterpreterV8::ConstructorSky(const Arguments &args) 1376 1392 { 1377 if (args.Length()!=2) 1378 return ThrowException(String::New("Sky constructor takes two arguments.")); 1393 if (args.Length()<2 || args.Length()>3) 1394 return ThrowException(String::New("Sky constructor takes two or three arguments.")); 1395 1396 if (args.Length()==3 && !args[2]->IsDate()) 1397 return ThrowException(String::New("Third argument must be a Date.")); 1379 1398 1380 1399 const double ra = args[0]->NumberValue(); … … 1384 1403 return ThrowException(String::New("Both arguments to Sky must be valid numbers.")); 1385 1404 1386 return ConstructSky(ra, dec); 1405 // ---------------------------- 1406 1407 Handle<Object> This = args.This(); 1408 if (!args.IsConstructCall()) 1409 { 1410 Handle<ObjectTemplate> T = fTemplateSky->PrototypeTemplate(); 1411 if (T.IsEmpty()) 1412 return Undefined(); 1413 1414 This = T->NewInstance(); 1415 } 1416 if (This.IsEmpty()) 1417 return Undefined(); 1418 1419 Handle<Function> function = 1420 FunctionTemplate::New(SkyToLocal)->GetFunction(); 1421 if (function.IsEmpty()) 1422 return Undefined(); 1423 1424 This->Set(String::New("ra"), Number::New(ra), ReadOnly); 1425 This->Set(String::New("dec"), Number::New(dec), ReadOnly); 1426 This->Set(String::New("toLocal"), function, ReadOnly); 1427 if (args.Length()==3) 1428 This->Set(String::New("time"), args[2], ReadOnly); 1429 1430 return This; 1387 1431 } 1388 1432 1389 1433 Handle<Value> InterpreterV8::ConstructorLocal(const Arguments &args) 1390 1434 { 1391 if (args.Length()!=2) 1392 return ThrowException(String::New("Local constructor takes two arguments.")); 1435 if (args.Length()<2 || args.Length()>3) 1436 return ThrowException(String::New("Local constructor takes two or three arguments.")); 1437 1438 if (args.Length()==3 && !args[2]->IsDate()) 1439 return ThrowException(String::New("Third argument must be a Date.")); 1440 1393 1441 1394 1442 const double zd = args[0]->NumberValue(); … … 1398 1446 return ThrowException(String::New("Both arguments to Local must be valid numbers.")); 1399 1447 1400 return ConstructLocal(zd, az); 1448 // -------------------- 1449 1450 Handle<Object> This = args.This(); 1451 if (!args.IsConstructCall()) 1452 { 1453 Handle<ObjectTemplate> T = fTemplateLocal->PrototypeTemplate(); 1454 if (T.IsEmpty()) 1455 return Undefined(); 1456 1457 This = T->NewInstance(); 1458 } 1459 if (This.IsEmpty()) 1460 return Undefined(); 1461 1462 Handle<Function> function = 1463 FunctionTemplate::New(LocalToSky)->GetFunction(); 1464 if (function.IsEmpty()) 1465 return Undefined(); 1466 1467 This->Set(String::New("zd"), Number::New(zd), ReadOnly); 1468 This->Set(String::New("az"), Number::New(az), ReadOnly); 1469 This->Set(String::New("toSky"), function, ReadOnly); 1470 if (args.Length()==3) 1471 This->Set(String::New("time"), args[2], ReadOnly); 1472 1473 return This; 1474 1475 // ---------------------- 1401 1476 } 1402 1477 #endif … … 1606 1681 "}"*/; 1607 1682 1608 Script::New(String::New(code.c_str()), String::New("internal"))->Run(); 1683 Handle<Script> script = Script::New(String::New(code.c_str()), String::New("internal")); 1684 if (!script.IsEmpty()) 1685 script->Run(); 1609 1686 } 1610 1687 … … 1633 1710 dim->Set(String::New("newState"), FunctionTemplate::New(WrapNewState), ReadOnly); 1634 1711 dim->Set(String::New("setState"), FunctionTemplate::New(WrapSetState), ReadOnly); 1712 dim->Set(String::New("getState"), FunctionTemplate::New(WrapGetState), ReadOnly); 1635 1713 dim->Set(String::New("sleep"), FunctionTemplate::New(WrapSleep), ReadOnly); 1636 1714 dim->Set(String::New("timeout"), FunctionTemplate::New(WrapTimeout), ReadOnly); 1637 1715 dim->Set(String::New("subscribe"), FunctionTemplate::New(WrapSubscribe), ReadOnly); 1638 dim->Set(String::New("database"), FunctionTemplate::New(WrapDatabase), ReadOnly); 1639 //dim->Set(String::New("file"), FunctionTemplate::New(WrapFile), ReadOnly); 1716 dim->Set(String::New("file"), FunctionTemplate::New(WrapFile), ReadOnly); 1640 1717 1641 1718 Handle<ObjectTemplate> onchange = ObjectTemplate::New(); … … 1645 1722 Handle<ObjectTemplate> global = ObjectTemplate::New(); 1646 1723 global->Set(String::New("dim"), dim, ReadOnly); 1647 global->Set(String::New("include"), FunctionTemplate::New(WrapInclude), ReadOnly); 1648 global->Set(String::New("exit"), FunctionTemplate::New(WrapExit), ReadOnly); 1649 global->Set(String::New("version"), FunctionTemplate::New(InterpreterV8::FuncVersion), ReadOnly); 1724 global->Set(String::New("include"), FunctionTemplate::New(WrapInclude), ReadOnly); 1725 global->Set(String::New("exit"), FunctionTemplate::New(WrapExit), ReadOnly); 1726 global->Set(String::New("version"), FunctionTemplate::New(InterpreterV8::FuncVersion), ReadOnly); 1727 1728 Handle<FunctionTemplate> db = FunctionTemplate::New(WrapDatabase); 1729 db->SetClassName(String::New("Database")); 1730 db->InstanceTemplate()->SetInternalFieldCount(1); 1731 global->Set(String::New("Database"), db, ReadOnly); 1732 1733 fTemplateDatabase = db; 1650 1734 1651 1735 #ifdef HAVE_NOVA 1652 1736 Handle<FunctionTemplate> sky = FunctionTemplate::New(ConstructorSky); 1737 sky->SetClassName(String::New("Sky")); 1653 1738 global->Set(String::New("Sky"), sky, ReadOnly); 1654 1739 1655 1740 Handle<FunctionTemplate> loc = FunctionTemplate::New(ConstructorLocal); 1656 loc->Set(String::New("dist"), FunctionTemplate::New(LocalDist), ReadOnly); 1741 loc->SetClassName(String::New("Local")); 1742 loc->Set(String::New("dist"), FunctionTemplate::New(LocalDist), ReadOnly); 1657 1743 global->Set(String::New("Local"), loc, ReadOnly); 1658 1744 1659 1745 Handle<FunctionTemplate> moon = FunctionTemplate::New(ConstructorMoon); 1746 moon->SetClassName(String::New("Moon")); 1660 1747 moon->Set(String::New("disk"), FunctionTemplate::New(MoonDisk), ReadOnly); 1661 1748 global->Set(String::New("Moon"), moon, ReadOnly); 1749 1750 fTemplateLocal = loc; 1751 fTemplateSky = sky; 1752 fTemplateMoon = moon; 1662 1753 #endif 1663 1754 … … 1744 1835 #endif 1745 1836 1837 fStates.clear(); 1838 1839 fGlobalContext.Dispose(); 1840 1746 1841 JsEnd(filename); 1747 1842 -
trunk/FACT++/src/InterpreterV8.h
r14627 r14629 41 41 std::vector<std::thread> fTimeout; 42 42 43 // List of all states already set 44 std::vector<std::pair<int, std::string>> fStates; 45 46 static v8::Handle<v8::FunctionTemplate> fTemplateLocal; 47 static v8::Handle<v8::FunctionTemplate> fTemplateSky; 48 static v8::Handle<v8::FunctionTemplate> fTemplateMoon; 49 static v8::Handle<v8::FunctionTemplate> fTemplateDatabase; 50 43 51 #ifdef HAVE_SQL 44 52 std::list<Database*> fDatabases; … … 67 75 v8::Handle<v8::Value> FuncState(const v8::Arguments& args); 68 76 v8::Handle<v8::Value> FuncSetState(const v8::Arguments& args); 77 v8::Handle<v8::Value> FuncGetState(const v8::Arguments& args); 69 78 v8::Handle<v8::Value> FuncNewState(const v8::Arguments& args); 70 79 //v8::Handle<v8::Value> FuncOpen(const v8::Arguments& args); … … 83 92 static v8::Handle<v8::Value> SkyToString(const v8::Arguments &args); 84 93 static v8::Handle<v8::Value> MoonToString(const v8::Arguments &args); 85 static v8::Handle<v8::Value> ConstructLocal(double zd, double az, v8::Handle<v8::Value> time=v8::Handle<v8::Value>());86 static v8::Handle<v8::Value> ConstructSky(double ra, double dec, v8::Handle<v8::Value> time=v8::Handle<v8::Value>(), bool ismoon=false);87 94 static v8::Handle<v8::Value> LocalDist(const v8::Arguments &args); 88 95 static v8::Handle<v8::Value> MoonDisk(const v8::Arguments &args); … … 108 115 static v8::Handle<v8::Value> WrapNewState(const v8::Arguments &args) { if (This) return This->FuncNewState(args); else return v8::Undefined(); } 109 116 static v8::Handle<v8::Value> WrapSetState(const v8::Arguments &args) { if (This) return This->FuncSetState(args); else return v8::Undefined(); } 117 static v8::Handle<v8::Value> WrapGetState(const v8::Arguments &args) { if (This) return This->FuncGetState(args); else return v8::Undefined(); } 110 118 //static v8::Handle<v8::Value> WrapOpen(const v8::Arguments &args) { if (This) return This->FuncOpen(args); else return v8::Undefined(); } 111 119 static v8::Handle<v8::Value> WrapSubscribe(const v8::Arguments &args){ if (This) return This->FuncSubscribe(args);else return v8::Undefined(); } … … 160 168 virtual bool JsUnsubscribe(const std::string &) { return false; }; 161 169 162 virtual bool JsNewState(int, const std::string&, const std::string&) { return false; } 163 virtual bool JsSetState(int) { return false; } 164 virtual bool JsHasState(int) const { return false; } 165 virtual bool JsHasState(const std::string &) const { return false; } 166 virtual int JsGetState(const std::string &) const { return -2; } 170 virtual bool JsNewState(int, const std::string&, const std::string&) { return false; } 171 virtual bool JsSetState(int) { return false; } 172 virtual bool JsHasState(int) const { return false; } 173 virtual bool JsHasState(const std::string &) const { return false; } 174 virtual int JsGetState(const std::string &) const { return -2; } 175 virtual State JsGetCurrentState() const { return State(); } 167 176 168 177 virtual std::vector<Description> JsDescription(const std::string &) { return std::vector<Description>(); };
Note:
See TracChangeset
for help on using the changeset viewer.