Changeset 14578 for trunk/FACT++


Ignore:
Timestamp:
11/07/12 17:33:10 (12 years ago)
Author:
tbretz
Message:
Added a simple astrometry interface.
Location:
trunk/FACT++/src
Files:
2 edited

Legend:

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

    r14576 r14578  
    11#include "InterpreterV8.h"
     2
     3#ifdef HAVE_NOVA
     4#include <libnova/lunar.h>
     5#include <libnova/transform.h>
     6#endif
    27
    38#ifdef HAVE_SQL
     
    195200    code +=        ");";
    196201
    197     const HandleScope handle_scope;
     202    HandleScope handle_scope;
    198203
    199204    // It is not strictly necessary to catch the exception, instead
     
    206211    const Handle<Value>  result = script->Run();
    207212
    208     return exception.HasCaught() ? exception.ReThrow() : result;
     213    return exception.HasCaught() ? exception.ReThrow() : handle_scope.Close(result);
    209214
    210215    /*
     
    230235        return ThrowException(String::New("Argument 1 must be a string."));
    231236
    232     const HandleScope handle_scope;
     237    HandleScope handle_scope;
    233238
    234239    const String::Utf8Value str(args[0]);
     
    246251    }
    247252
    248     return Boolean::New(JsSend(command));
     253    return handle_scope.Close(Boolean::New(JsSend(command)));
    249254}
    250255
     
    275280        "})();";
    276281
    277     const HandleScope handle_scope;
     282    HandleScope handle_scope;
    278283
    279284    const Handle<Script> script = Script::Compile(String::New(code.c_str()));
    280     return script->Run();
     285
     286    return handle_scope.Close(script->Run());
    281287
    282288    //JsSleep(args[0]->Int32Value());
    283289    //return Undefined();
    284290}
     291
     292// ==========================================================================
     293//                               State control
     294// ==========================================================================
    285295
    286296Handle<Value> InterpreterV8::FuncState(const Arguments& args)
     
    335345        return ThrowException(String::New("Argument 3 must be a string."));
    336346
    337     const HandleScope handle_scope;
     347    HandleScope handle_scope;
    338348
    339349    const uint32_t index   = args[0]->Int32Value();
     
    358368    }
    359369
    360     return Boolean::New(JsNewState(index, name, comment));
     370    return handle_scope.Close(Boolean::New(JsNewState(index, name, comment)));
    361371}
    362372
     
    369379        return ThrowException(String::New("Argument must be an unint32 or a  string."));
    370380
    371     const HandleScope handle_scope;
     381    HandleScope handle_scope;
    372382
    373383    int index = -2;
     
    387397        return ThrowException(String::New("State must be in the range [10, 255]."));
    388398
    389     return Boolean::New(JsSetState(index));
    390 }
     399    return handle_scope.Close(Boolean::New(JsSetState(index)));
     400}
     401
     402// ==========================================================================
     403//                             Internal functions
     404// ==========================================================================
    391405
    392406Handle<Value> InterpreterV8::FuncExit(const Arguments &)
     
    478492}
    479493
     494// ==========================================================================
     495//                                 Database
     496// ==========================================================================
     497
    480498Handle<Value> InterpreterV8::FuncDbClose(const Arguments &args)
    481499{
     
    484502    void *ptr = Handle<External>::Cast(args.This()->GetInternalField(0))->Value();
    485503    if (!ptr)
    486         return Boolean::New(false);
     504        return handle_scope.Close(Boolean::New(false));
    487505
    488506#ifdef HAVE_SQL
     
    495513    args.This()->SetInternalField(0, External::New(0));
    496514
    497     return Boolean::New(true);
     515    return handle_scope.Close(Boolean::New(true));
    498516}
    499517Handle<Value> InterpreterV8::FuncDbQuery(const Arguments &args)
     
    684702#endif
    685703}
     704
     705// ==========================================================================
     706//                                 Services
     707// ==========================================================================
    686708
    687709Handle<Value> InterpreterV8::Convert(char type, const char* &ptr)
     
    720742Handle<Value> InterpreterV8::FuncClose(const Arguments &args)
    721743{
    722     const HandleScope handle_scope;
     744    HandleScope handle_scope;
    723745
    724746    //const void *ptr = Local<External>::Cast(args.Holder()->GetInternalField(0))->Value();
     
    735757    args.Holder()->Set(String::New("isOpen"), Boolean::New(false), ReadOnly);
    736758
    737     return Boolean::New(JsUnsubscribe(*str));
     759    return handle_scope.Close(Boolean::New(JsUnsubscribe(*str)));
    738760}
    739761
     
    11191141}
    11201142
     1143// ==========================================================================
     1144//                            Astrometry
     1145// ==========================================================================
     1146#ifdef HAVE_NOVA
     1147
     1148double InterpreterV8::GetDataMember(const Arguments &args, const char *name)
     1149{
     1150    return args.This()->Get(String::New(name))->NumberValue();
     1151}
     1152
     1153Handle<Value> InterpreterV8::LocalToString(const Arguments &args)
     1154{
     1155    return String::New("[object Local]");
     1156    /*
     1157    HandleScope handle_scope;
     1158
     1159    const Handle<Object> This = args.This();
     1160
     1161    Handle<String> zd = This->Get(String::New("zd"))->ToString();
     1162    Handle<String> az = This->Get(String::New("az"))->ToString();
     1163
     1164    return String::New("TEST");
     1165    */
     1166}
     1167
     1168Handle<Value> InterpreterV8::SkyToString(const Arguments &args)
     1169{
     1170    return String::New("[object Sky]");
     1171}
     1172
     1173Handle<Value> InterpreterV8::MoonToString(const Arguments &args)
     1174{
     1175    return String::New("[object Moon]");
     1176}
     1177
     1178Handle<Value> InterpreterV8::ConstructLocal(double zd, double az, Handle<Value> time)
     1179{
     1180    Handle<ObjectTemplate> loc = ObjectTemplate::New();
     1181
     1182    loc->Set(String::New("zd"), Number::New(zd), ReadOnly);
     1183    loc->Set(String::New("az"), Number::New(az), ReadOnly);
     1184    loc->Set(String::New("toSky"),    FunctionTemplate::New(LocalToSky), ReadOnly);
     1185    loc->Set(String::New("toString"), FunctionTemplate::New(LocalToString), ReadOnly);
     1186    if (!time.IsEmpty())
     1187        loc->Set(String::New("time"), time);
     1188
     1189    return loc->NewInstance();
     1190}
     1191
     1192Handle<Value> InterpreterV8::ConstructSky(double ra, double dec, Handle<Value> time, bool ismoon)
     1193{
     1194    Handle<ObjectTemplate> sky = ObjectTemplate::New();
     1195
     1196    sky->Set(String::New("ra"),  Number::New(ra),  ReadOnly);
     1197    sky->Set(String::New("dec"), Number::New(dec), ReadOnly);
     1198    sky->Set(String::New("toLocal"),  FunctionTemplate::New(ismoon?MoonToLocal :SkyToLocal),  ReadOnly);
     1199    sky->Set(String::New("toString"), FunctionTemplate::New(ismoon?MoonToString:SkyToString), ReadOnly);
     1200    if (!time.IsEmpty())
     1201        sky->Set(String::New("time"), time);
     1202
     1203    return sky->NewInstance();
     1204}
     1205
     1206Handle<Value> InterpreterV8::MoonDisk(const Arguments &args)
     1207{
     1208    return Undefined();
     1209}
     1210
     1211Handle<Value> InterpreterV8::LocalToSky(const Arguments &args)
     1212{
     1213    HandleScope handle_scope;
     1214
     1215    if (args.Length()>1)
     1216        return ThrowException(String::New("toSky must not be called with more than one argument."));
     1217
     1218    ln_hrz_posn hrz;
     1219    hrz.alt = 90-GetDataMember(args, "zd");
     1220    hrz.az  =    GetDataMember(args, "az");
     1221
     1222    if (!finite(hrz.alt) || !finite(hrz.az))
     1223        return ThrowException(String::New("zd and az must be finite."));
     1224
     1225    // It is important to catch the exception thrown
     1226    // by Date::New in case of thread termination!
     1227    TryCatch exception;
     1228
     1229    const Local<Value> date =
     1230        args.Length()==0 ? Date::New(Time().JavaDate()) : args[0];
     1231    if (exception.HasCaught())
     1232        return exception.ReThrow();
     1233
     1234    const uint64_t v = uint64_t(args[0]->NumberValue());
     1235    const Time utc(v/1000, v%1000);
     1236
     1237    ln_lnlat_posn obs;
     1238    obs.lng = -(17.+53./60+26.525/3600);
     1239    obs.lat =   28.+45./60+42.462/3600;
     1240
     1241    ln_equ_posn equ;
     1242    ln_get_equ_from_hrz(&hrz, &obs, utc.JD(), &equ);
     1243
     1244    return handle_scope.Close(ConstructSky(equ.ra/15, equ.dec, date));
     1245}
     1246
     1247Handle<Value> InterpreterV8::SkyToLocal(const Arguments &args)
     1248{
     1249    HandleScope handle_scope;
     1250
     1251    if (args.Length()>1)
     1252        return ThrowException(String::New("toLocal must not be called with more than one argument."));
     1253
     1254    ln_equ_posn equ;
     1255    equ.ra  = GetDataMember(args, "ra")*15;
     1256    equ.dec = GetDataMember(args, "dec");
     1257
     1258    if (!finite(equ.ra) || !finite(equ.dec))
     1259        return ThrowException(String::New("Ra and dec must be finite."));
     1260
     1261    // It is important to catch the exception thrown
     1262    // by Date::New in case of thread termination!
     1263    TryCatch exception;
     1264
     1265    const Local<Value> date =
     1266        args.Length()==0 ? Date::New(Time().JavaDate()) : args[0];
     1267    if (exception.HasCaught())
     1268        return exception.ReThrow();
     1269
     1270    const uint64_t v = uint64_t(args[0]->NumberValue());
     1271    const Time utc(v/1000, v%1000);
     1272
     1273    ln_lnlat_posn obs;
     1274    obs.lng = -(17.+53./60+26.525/3600);
     1275    obs.lat =   28.+45./60+42.462/3600;
     1276
     1277    ln_hrz_posn hrz;
     1278    ln_get_hrz_from_equ(&equ, &obs, utc.JD(), &hrz);
     1279
     1280    return handle_scope.Close(ConstructLocal(90-hrz.alt, hrz.az, date));
     1281}
     1282
     1283Handle<Value> InterpreterV8::MoonToLocal(const Arguments &args)
     1284{
     1285    HandleScope handle_scope;
     1286
     1287    if (args.Length()>0)
     1288        return ThrowException(String::New("toLocal must not be called with arguments."));
     1289
     1290    ln_equ_posn equ;
     1291    equ.ra  = GetDataMember(args, "ra")*15;
     1292    equ.dec = GetDataMember(args, "dec");
     1293
     1294    if (!finite(equ.ra) || !finite(equ.dec))
     1295        return ThrowException(String::New("ra and dec must be finite."));
     1296
     1297    // It is important to catch the exception thrown
     1298    // by Date::New in case of thread termination!
     1299    TryCatch exception;
     1300
     1301    const Local<Value> date =
     1302        args.Length()==0 ? Date::New(Time().JavaDate()) : args.This()->Get(String::New("time"));
     1303    if (exception.HasCaught())
     1304        return exception.ReThrow();
     1305
     1306    const uint64_t v = uint64_t(args[0]->NumberValue());
     1307    const Time utc(v/1000, v%1000);
     1308
     1309    ln_lnlat_posn obs;
     1310    obs.lng = -(17.+53./60+26.525/3600);
     1311    obs.lat =   28.+45./60+42.462/3600;
     1312
     1313    ln_hrz_posn hrz;
     1314    ln_get_hrz_from_equ(&equ, &obs, utc.JD(), &hrz);
     1315
     1316    return handle_scope.Close(ConstructLocal(90-hrz.alt, hrz.az, date));
     1317}
     1318
     1319Handle<Value> InterpreterV8::ConstructorMoon(const Arguments &args)
     1320{
     1321    HandleScope handle_scope;
     1322
     1323    if (args.Length()>1)
     1324        return ThrowException(String::New("Moon constructor must not be called with more than one argument."));
     1325
     1326    // It is important to catch the exception thrown
     1327    // by Date::New in case of thread termination!
     1328    TryCatch exception;
     1329
     1330    const Local<Value> date =
     1331        args.Length()==0 ? Date::New(Time().JavaDate()) : args[0];
     1332    if (exception.HasCaught())
     1333        return exception.ReThrow();
     1334
     1335    const uint64_t v = uint64_t(args[0]->NumberValue());
     1336    const Time utc(v/1000, v%1000);
     1337
     1338    ln_equ_posn equ;
     1339    ln_get_lunar_equ_coords_prec(utc.JD(), &equ, 0.01);
     1340
     1341    return handle_scope.Close(ConstructSky(equ.ra/15, equ.dec, date, true));
     1342}
     1343
     1344Handle<Value> InterpreterV8::ConstructorSky(const Arguments &args)
     1345{
     1346    HandleScope handle_scope;
     1347
     1348    if (args.Length()!=2)
     1349        return ThrowException(String::New("Sky constructor takes two arguments."));
     1350
     1351    const double ra  = args[0]->NumberValue();
     1352    const double dec = args[1]->NumberValue();
     1353
     1354    if (!finite(ra) || !finite(dec))
     1355        return ThrowException(String::New("Both arguments to Sky must be valid numbers."));
     1356
     1357    return handle_scope.Close(ConstructSky(ra, dec));
     1358}
     1359
     1360Handle<Value> InterpreterV8::ConstructorLocal(const Arguments &args)
     1361{
     1362    HandleScope handle_scope;
     1363
     1364    if (args.Length()!=2)
     1365        return ThrowException(String::New("Local constructor takes two arguments."));
     1366
     1367    const double zd = args[0]->NumberValue();
     1368    const double az = args[1]->NumberValue();
     1369
     1370    if (!finite(zd) || !finite(az))
     1371        return ThrowException(String::New("Both arguments to Local must be valid numbers."));
     1372
     1373    return handle_scope.Close(ConstructLocal(zd, az));
     1374}
     1375#endif
     1376
     1377// ==========================================================================
     1378//                                  CORE
     1379// ==========================================================================
     1380
    11211381bool InterpreterV8::JsRun(const string &filename, const map<string, string> &map)
    11221382{
     
    11541414    global->Set(String::New("version"), FunctionTemplate::New(InterpreterV8::FuncVersion), ReadOnly);
    11551415
     1416#ifdef HAVE_NOVA
     1417    Handle<FunctionTemplate> sky = FunctionTemplate::New(ConstructorSky);
     1418    global->Set(String::New("Sky"), sky, ReadOnly);
     1419
     1420    Handle<FunctionTemplate> loc = FunctionTemplate::New(ConstructorLocal);
     1421    global->Set(String::New("Local"), loc, ReadOnly);
     1422
     1423    Handle<FunctionTemplate> moon = FunctionTemplate::New(ConstructorMoon);
     1424    moon->Set(String::New("disk"), FunctionTemplate::New(MoonDisk), ReadOnly);
     1425    global->Set(String::New("Moon"), moon, ReadOnly);
     1426#endif
     1427
    11561428    // Persistent
    11571429    Persistent<Context> context = Context::New(NULL, global);
     
    12251497
    12261498#endif
     1499// ========================================================================
     1500/*
     1501 const double lon = -(17.+53./60+26.525/3600);
     1502 const double lat =   28.+45./60+42.462/3600;
     1503
     1504 Time now;
     1505
     1506 const double disk = ln_get_lunar_disk(now.JD();
     1507
     1508 ln_lnlat_posn obs;
     1509 obs.lng = lon;
     1510 obs.lat = lat;
     1511
     1512 ln_equ_posn equ;
     1513 ln_hrz_posn hrz;
     1514
     1515 // Source
     1516 equ.ra  = ra*15;
     1517 equ.dec = dec;
     1518
     1519 // Moon
     1520 ln_get_lunar_equ_coords_prec(now.JD(), &moon, 0.01);
     1521
     1522 ln_get_hrz_from_equ(&equ, &obs, now.JD(), &hrz);
     1523 ln_get_equ_from_hrz(&hrz, &obs, now.JD(), &equ);
     1524*/
     1525
     1526/*
     1527 Sky   { ra, dec, toLocal(time[, obs]), moon(time) }
     1528 Local { zd, az,  toSky  (time[, obs]), moon(time) }
     1529
     1530 {zd, az} = getLocalCoordinates({ra, dec}[, time [, obs]]);
     1531 {zd, az} = getMoonPosition(time);
     1532 float    = getMoonDisk(time);
     1533
     1534 // -----------------------------------------------------------------------
     1535
     1536 void Callack(Arguments &args)
     1537 {
     1538 }
     1539
     1540 Handle<ObjectTemplate> sky = ObjectTemplate::New();
     1541 sky->SetCallAsFunctionHandler(Callback);
     1542 dim->Set(String::New("Sky"), sky, ReadOnly);
     1543
     1544 void v8::ObjectTemplate::SetCallAsFunctionHandler      (       InvocationCallback      callback,
     1545 Handle< Value >        data = Handle< Value >()
     1546)                       
     1547
     1548    Handle<ObjectTemplate> sky = ObjectTemplate::New();
     1549    sky->Set(String::New("toLocal"), FunctionTemplate::New(WrapToLocal), ReadOnly);
     1550    sky->Set(String::New("ra"),  Float::New(ra));
     1551    sky->Set(String::New("dec"), Float::New(dec));
     1552    return sky::NewInstance();
     1553
     1554    Handle<ObjectTemplate> loc = ObjectTemplate::New();
     1555    loc->Set(String::New("toSky"), FunctionTemplate::New(WrapToSky), ReadOnly);
     1556    loc->Set(String::New("zd"), Float::New(zd));
     1557    loc->Set(String::New("az"), Float::New(az));
     1558    return loc::NewInstance();
     1559
     1560
     1561
     1562
     1563
     1564
     1565 */
     1566
     1567
     1568
     1569
     1570
  • trunk/FACT++/src/InterpreterV8.h

    r14560 r14578  
    6464    v8::Handle<v8::Value> FuncDbClose(const v8::Arguments &args);
    6565    v8::Handle<v8::Value> OnChangeSet(v8::Local<v8::String>, v8::Local<v8::Value>, const v8::AccessorInfo &);
     66
     67    static double GetDataMember(const v8::Arguments &args, const char *name);
     68
     69    static v8::Handle<v8::Value> LocalToString(const v8::Arguments &args);
     70    static v8::Handle<v8::Value> SkyToString(const v8::Arguments &args);
     71    static v8::Handle<v8::Value> MoonToString(const v8::Arguments &args);
     72    static v8::Handle<v8::Value> ConstructLocal(double zd, double az, v8::Handle<v8::Value> time=v8::Handle<v8::Value>());
     73    static v8::Handle<v8::Value> ConstructSky(double ra, double dec, v8::Handle<v8::Value> time=v8::Handle<v8::Value>(), bool ismoon=false);
     74    static v8::Handle<v8::Value> MoonDisk(const v8::Arguments &args);
     75    static v8::Handle<v8::Value> LocalToSky(const v8::Arguments &args);
     76    static v8::Handle<v8::Value> SkyToLocal(const v8::Arguments &args);
     77    static v8::Handle<v8::Value> MoonToLocal(const v8::Arguments &args);
     78    static v8::Handle<v8::Value> ConstructorMoon(const v8::Arguments &args);
     79    static v8::Handle<v8::Value> ConstructorSky(const v8::Arguments &args);
     80    static v8::Handle<v8::Value> ConstructorLocal(const v8::Arguments &args);
    6681
    6782    static v8::Handle<v8::Value> FuncVersion(const v8::Arguments&);
Note: See TracChangeset for help on using the changeset viewer.