Changeset 18828
- Timestamp:
- 04/14/17 17:20:35 (8 years ago)
- Location:
- trunk/FACT++/src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/InterpreterV8.cc
r18820 r18828 1837 1837 { 1838 1838 if (args.Length()!=2) 1839 return ThrowException(String::New("dist must not be called withtwo arguments."));1839 return ThrowException(String::New("dist must be called with exactly two arguments.")); 1840 1840 1841 1841 if (!args[0]->IsObject() || !args[1]->IsObject()) 1842 return ThrowException(String::New("at least one argument not an object.")); 1842 return ThrowException(String::New("At least one argument not an object.")); 1843 1844 // FiXME: Add a check for the argument type 1843 1845 1844 1846 HandleScope handle_scope; … … 1908 1910 } 1909 1911 1912 struct AstroArgs 1913 { 1914 string obs; 1915 Nova::LnLatPosn posn; 1916 double jd; 1917 uint64_t jsdate; 1918 1919 AstroArgs() : jsdate(0) { } 1920 }; 1921 1922 AstroArgs EvalAstroArgs(int offset, const Arguments &args, int8_t type=2) 1923 { 1924 const uint8_t max = abs(type); 1925 1926 if (args.Length()>offset+max) 1927 throw runtime_error("Number of arguments must not exceed "+to_string(offset+max)+"."); 1928 1929 if (type==1 && args.Length()==offset+1 && !args[offset]->IsString()) 1930 throw runtime_error("Argument "+to_string(offset+1)+" must be a string."); 1931 if (type==-1 && args.Length()==offset+1 && !args[offset]->IsDate()) 1932 throw runtime_error("Argument "+to_string(offset+1)+" must be a date."); 1933 1934 if (args.Length()==offset+1 && !(args[offset]->IsDate() || args[offset]->IsString())) 1935 throw runtime_error("Argument "+to_string(offset+1)+" must be a string or Date."); 1936 1937 if (args.Length()==offset+2 && 1938 !(args[offset+0]->IsDate() && args[offset+1]->IsString()) && 1939 !(args[offset+1]->IsDate() && args[offset+0]->IsString())) 1940 throw runtime_error("Arguments "+to_string(offset+1)+" and "+to_string(offset+2)+" must be a string/Date or Date/string."); 1941 1942 HandleScope handle_scope; 1943 1944 Local<Value> obs = args.This()->Get(String::New("observatory")); 1945 if (args.Length()>offset && args[offset]->IsString()) 1946 obs = args[offset]; 1947 if (args.Length()>offset+1 && args[offset+1]->IsString()) 1948 obs = args[offset+1]; 1949 1950 AstroArgs rc; 1951 1952 // For constructors, observatory can stay empty if not explicitly given 1953 if (offset<2) 1954 rc.obs = "ORM"; 1955 1956 if (!obs.IsEmpty() && !obs->IsUndefined()) 1957 rc.obs = *String::AsciiValue(obs); 1958 1959 rc.posn = rc.obs; 1960 1961 if ((!rc.obs.empty() || offset==0) && !rc.posn.isValid()) 1962 throw runtime_error("Observatory "+rc.obs+" unknown."); 1963 1964 Local<Value> date = args.This()->Get(String::New("time")); 1965 if (args.Length()>offset && args[offset]->IsDate()) 1966 date = args[offset]; 1967 if (args.Length()>offset+1 && args[offset+1]->IsDate()) 1968 date = args[offset+1]; 1969 1970 // For constructors, time can stay empty if not explicitly given 1971 if (offset<2) 1972 rc.jsdate = Time().JavaDate(); 1973 1974 if (!date.IsEmpty() && !date->IsUndefined()) 1975 rc.jsdate = uint64_t(date->NumberValue()); 1976 1977 rc.jd = Time(rc.jsdate/1000, rc.jsdate%1000).JD(); 1978 1979 return rc; 1980 } 1981 1910 1982 Handle<Value> InterpreterV8::LocalToSky(const Arguments &args) 1911 1983 { 1912 if (args.Length()>1) 1913 return ThrowException(String::New("toSky must not be called with more than one argument.")); 1914 1915 if (args.Length()==1 && !args[0]->IsDate()) 1916 return ThrowException(String::New("Argument must be a Date")); 1984 AstroArgs local; 1985 try 1986 { 1987 local = EvalAstroArgs(0, args, 2); 1988 } 1989 catch (const exception &e) 1990 { 1991 return ThrowException(String::New(e.what())); 1992 } 1917 1993 1918 1994 Nova::ZdAzPosn hrz; … … 1921 1997 1922 1998 if (!finite(hrz.zd) || !finite(hrz.az)) 1923 return ThrowException(String::New("zd and az must be finite.")); 1999 return ThrowException(String::New("Zd and az must be finite.")); 2000 2001 const Nova::EquPosn equ = Nova::GetEquFromHrz(hrz, local.posn, local.jd); 1924 2002 1925 2003 HandleScope handle_scope; 1926 2004 1927 const Local<Value> date = 1928 args.Length()==0 ? Date::New(Time().JavaDate()) : args[0]; 1929 if (date.IsEmpty()) 1930 return Undefined(); 1931 1932 const uint64_t v = uint64_t(date->NumberValue()); 1933 const Time utc(v/1000, v%1000); 1934 1935 const Nova::EquPosn equ = Nova::GetEquFromHrz(hrz, utc.JD()); 1936 1937 // ----------------------------- 1938 1939 Handle<Value> arg[] = { Number::New(equ.ra/15), Number::New(equ.dec), date }; 1940 return handle_scope.Close(fTemplateSky->GetFunction()->NewInstance(3, arg)); 2005 Handle<Value> arg_loc[] = { Number::New(hrz.zd), Number::New(hrz.az), String::New(local.obs.c_str()), Date::New(local.jsdate) }; 2006 Handle<Object> loc = fTemplateLocal->GetFunction()->NewInstance(4, arg_loc); 2007 2008 Handle<Value> arg_sky[] = { Number::New(equ.ra/15), Number::New(equ.dec), loc }; 2009 return handle_scope.Close(fTemplateSky->GetFunction()->NewInstance(3, arg_sky)); 1941 2010 } 1942 2011 1943 2012 Handle<Value> InterpreterV8::SkyToLocal(const Arguments &args) 1944 2013 { 1945 if (args.Length()>1) 1946 return ThrowException(String::New("toLocal must not be called with more than one argument.")); 1947 1948 if (args.Length()==1 && !args[0]->IsDate()) 1949 return ThrowException(String::New("Argument must be a Date")); 2014 AstroArgs local; 2015 try 2016 { 2017 local = EvalAstroArgs(0, args, 2); 2018 } 2019 catch (const exception &e) 2020 { 2021 return ThrowException(String::New(e.what())); 2022 } 1950 2023 1951 2024 Nova::EquPosn equ; … … 1958 2031 HandleScope handle_scope; 1959 2032 1960 const Local<Value> date = 1961 args.Length()==0 ? Date::New(Time().JavaDate()) : args[0]; 1962 if (date.IsEmpty()) 1963 return Undefined(); 1964 1965 const uint64_t v = uint64_t(date->NumberValue()); 1966 const Time utc(v/1000, v%1000); 1967 1968 const Nova::ZdAzPosn hrz = Nova::GetHrzFromEqu(equ, utc.JD()); 1969 1970 Handle<Value> arg[] = { Number::New(hrz.zd), Number::New(hrz.az), date }; 1971 return handle_scope.Close(fTemplateLocal->GetFunction()->NewInstance(3, arg)); 2033 const Nova::ZdAzPosn hrz = Nova::GetHrzFromEqu(equ, local.posn, local.jd); 2034 2035 Handle<Value> arg[] = { Number::New(hrz.zd), Number::New(hrz.az), String::New(local.obs.c_str()), Date::New(local.jsdate) }; 2036 return handle_scope.Close(fTemplateLocal->GetFunction()->NewInstance(4, arg)); 1972 2037 } 1973 2038 1974 2039 Handle<Value> InterpreterV8::MoonToLocal(const Arguments &args) 1975 2040 { 1976 if (args.Length()>0) 1977 return ThrowException(String::New("toLocal must not be called with arguments.")); 2041 AstroArgs local; 2042 try 2043 { 2044 local = EvalAstroArgs(0, args, 1); 2045 } 2046 catch (const exception &e) 2047 { 2048 return ThrowException(String::New(e.what())); 2049 } 1978 2050 1979 2051 Nova::EquPosn equ; … … 1982 2054 1983 2055 if (!finite(equ.ra) || !finite(equ.dec)) 1984 return ThrowException(String::New(" ra and dec must be finite."));2056 return ThrowException(String::New("Ra and dec must be finite.")); 1985 2057 1986 2058 HandleScope handle_scope; 1987 2059 1988 const Local<Value> date = args.This()->Get(String::New("time")); 1989 if (date.IsEmpty() || date->IsUndefined() ) 1990 return Undefined(); 1991 1992 const uint64_t v = uint64_t(date->NumberValue()); 1993 const Time utc(v/1000, v%1000); 1994 1995 const Nova::ZdAzPosn hrz = Nova::GetHrzFromEqu(equ, utc.JD()); 1996 1997 Handle<Value> arg[] = { Number::New(hrz.zd), Number::New(hrz.az), date }; 1998 return handle_scope.Close(fTemplateLocal->GetFunction()->NewInstance(3, arg)); 2060 const Nova::ZdAzPosn hrz = Nova::GetHrzFromEqu(equ, local.posn, local.jd); 2061 2062 Handle<Value> arg[] = { Number::New(hrz.zd), Number::New(hrz.az), String::New(local.obs.c_str()), Date::New(local.jsdate) }; 2063 return handle_scope.Close(fTemplateLocal->GetFunction()->NewInstance(4, arg)); 1999 2064 } 2000 2065 2001 2066 Handle<Value> InterpreterV8::ConstructorMoon(const Arguments &args) 2002 2067 { 2003 if (args.Length()>1) 2004 return ThrowException(String::New("Moon constructor must not be called with more than one argument.")); 2005 2006 if (args.Length()==1 && !args[0]->IsDate()) 2007 return ThrowException(String::New("Argument must be a Date")); 2068 AstroArgs local; 2069 try 2070 { 2071 local = EvalAstroArgs(0, args, -1); 2072 } 2073 catch (const exception &e) 2074 { 2075 return ThrowException(String::New(e.what())); 2076 } 2077 2078 const Nova::EquPosn equ = Nova::GetLunarEquCoords(local.jd, 0.01); 2008 2079 2009 2080 HandleScope handle_scope; 2010 2011 const Local<Value> date =2012 args.Length()==0 ? Date::New(Time().JavaDate()) : args[0];2013 if (date.IsEmpty())2014 return Undefined();2015 2016 const uint64_t v = uint64_t(date->NumberValue());2017 const Time utc(v/1000, v%1000);2018 2019 const Nova::EquPosn equ = Nova::GetLunarEquCoords(utc.JD(), 0.01);2020 2081 2021 2082 // ---------------------------- … … 2033 2094 self->Set(String::New("dec"), Number::New(equ.dec), ReadOnly); 2034 2095 self->Set(String::New("toLocal"), function, ReadOnly); 2035 self->Set(String::New("time"), date,ReadOnly);2096 self->Set(String::New("time"), Date::New(local.jsdate), ReadOnly); 2036 2097 2037 2098 return handle_scope.Close(self); … … 2043 2104 return ThrowException(String::New("Sky constructor takes two or three arguments.")); 2044 2105 2045 if (args.Length()==3 && !args[2]->IsDate()) 2046 return ThrowException(String::New("Third argument must be a Date.")); 2106 if (args.Length()>2 && !args[2]->IsObject()) 2107 { 2108 const string n = *String::AsciiValue(args[2]->ToObject()->GetConstructorName()); 2109 if (n!="Local") 2110 return ThrowException(String::New("Third argument must be of type Local.")); 2111 } 2047 2112 2048 2113 const double ra = args[0]->NumberValue(); … … 2050 2115 2051 2116 if (!finite(ra) || !finite(dec)) 2052 return ThrowException(String::New(" Botharguments to Sky must be valid numbers."));2117 return ThrowException(String::New("The first two arguments to Sky must be valid numbers.")); 2053 2118 2054 2119 // ---------------------------- … … 2069 2134 self->Set(String::New("toLocal"), function, ReadOnly); 2070 2135 if (args.Length()==3) 2071 self->Set(String::New(" time"), args[2], ReadOnly);2136 self->Set(String::New("local"), args[2], ReadOnly); 2072 2137 2073 2138 return handle_scope.Close(self); … … 2076 2141 Handle<Value> InterpreterV8::ConstructorLocal(const Arguments &args) 2077 2142 { 2078 if (args.Length()<2 || args.Length()>3) 2079 return ThrowException(String::New("Local constructor takes two or three arguments.")); 2080 2081 if (args.Length()==3 && !args[2]->IsDate()) 2082 return ThrowException(String::New("Third argument must be a Date.")); 2143 AstroArgs local; 2144 try 2145 { 2146 local = EvalAstroArgs(2, args, 2); 2147 } 2148 catch (const exception &e) 2149 { 2150 return ThrowException(String::New(e.what())); 2151 } 2083 2152 2084 2153 const double zd = args[0]->NumberValue(); … … 2086 2155 2087 2156 if (!finite(zd) || !finite(az)) 2088 return ThrowException(String::New(" Botharguments to Local must be valid numbers."));2157 return ThrowException(String::New("The first two arguments to Local must be valid numbers.")); 2089 2158 2090 2159 // -------------------- … … 2104 2173 self->Set(String::New("az"), Number::New(az), ReadOnly); 2105 2174 self->Set(String::New("toSky"), function, ReadOnly); 2106 if (args.Length()==3) 2107 self->Set(String::New("time"), args[2], ReadOnly); 2175 if (!local.obs.empty()) 2176 self->Set(String::New("observatory"), String::New(local.obs.c_str()), ReadOnly); 2177 if (local.jsdate>0) 2178 self->Set(String::New("time"), Date::New(local.jsdate), ReadOnly); 2108 2179 2109 2180 return handle_scope.Close(self); 2110 2181 } 2111 2182 2112 Handle<Object> InterpreterV8::ConstructRiseSet(const Handle<Value> time, const Nova::RstTime &rst, const bool &rc)2183 Handle<Object> ConstructRiseSet(const AstroArgs &args, const Nova::RstTime &rst, const bool &rc) 2113 2184 { 2114 2185 Handle<Object> obj = Object::New(); 2115 obj->Set(String::New("time"), time, ReadOnly); 2116 2117 const uint64_t v = uint64_t(time->NumberValue()); 2118 const double jd = Time(v/1000, v%1000).JD(); 2186 obj->Set(String::New("time"), Date::New(args.jsdate), ReadOnly); 2187 obj->Set(String::New("observatory"), String::New(args.obs.c_str()), ReadOnly); 2119 2188 2120 2189 const bool isUp = rc>0 || 2121 (rst.rise<rst.set && ( jd>rst.rise &&jd<rst.set)) ||2122 (rst.rise>rst.set && ( jd<rst.set ||jd>rst.rise));2190 (rst.rise<rst.set && (args.jd>rst.rise && args.jd<rst.set)) || 2191 (rst.rise>rst.set && (args.jd<rst.set || args.jd>rst.rise)); 2123 2192 2124 2193 obj->Set(String::New("isUp"), Boolean::New(rc>=0 && isUp), ReadOnly); … … 2142 2211 Handle<Value> InterpreterV8::SunHorizon(const Arguments &args) 2143 2212 { 2144 if (args.Length()>2) 2145 return ThrowException(String::New("Sun.horizon must not be called with one or two arguments.")); 2146 2147 if (args.Length()==2 && !args[1]->IsDate()) 2148 return ThrowException(String::New("Second argument must be a Date")); 2213 AstroArgs local; 2214 try 2215 { 2216 local = EvalAstroArgs(1, args, 2); 2217 } 2218 catch (const exception &e) 2219 { 2220 return ThrowException(String::New(e.what())); 2221 } 2149 2222 2150 2223 HandleScope handle_scope; … … 2173 2246 2174 2247 if (!finite(hrz)) 2175 return ThrowException(String::New("Second argument did not yield a valid number.")); 2176 2177 const Local<Value> date = 2178 args.Length()<2 ? Date::New(Time().JavaDate()) : args[1]; 2179 if (date.IsEmpty()) 2180 return Undefined(); 2181 2182 const uint64_t v = uint64_t(date->NumberValue()); 2183 const Time utc(v/1000, v%1000); 2184 2185 Nova::LnLatPosn obs = Nova::kORM; 2248 return ThrowException(String::New("First argument did not yield a valid number.")); 2186 2249 2187 2250 ln_rst_time sun; 2188 const int rc = ln_get_solar_rst_horizon( utc.JD()-0.5, &obs, hrz, &sun);2189 Handle<Object> rst = ConstructRiseSet( date, sun, rc);2251 const int rc = ln_get_solar_rst_horizon(local.jd-0.5, &local.posn, hrz, &sun); 2252 Handle<Object> rst = ConstructRiseSet(local, sun, rc); 2190 2253 rst->Set(String::New("horizon"), Number::New(hrz)); 2191 2254 return handle_scope.Close(rst); … … 2194 2257 Handle<Value> InterpreterV8::MoonHorizon(const Arguments &args) 2195 2258 { 2196 if (args.Length()>1) 2197 return ThrowException(String::New("Moon.horizon must not be called with more than one argument.")); 2198 2199 if (args.Length()==1 && !args[0]->IsDate()) 2200 return ThrowException(String::New("Argument must be a Date")); 2259 AstroArgs local; 2260 try 2261 { 2262 local = EvalAstroArgs(0, args, 2); 2263 } 2264 catch (const exception &e) 2265 { 2266 return ThrowException(String::New(e.what())); 2267 } 2201 2268 2202 2269 HandleScope handle_scope; 2203 2270 2204 const Local<Value> date =2205 args.Length()==0 ? Date::New(Time().JavaDate()) : args[0];2206 if (date.IsEmpty())2207 return Undefined();2208 2209 const uint64_t v = uint64_t(date->NumberValue());2210 const Time utc(v/1000, v%1000);2211 2212 Nova::LnLatPosn obs = Nova::kORM;2213 2214 2271 ln_rst_time moon; 2215 const int rc = ln_get_lunar_rst( utc.JD()-0.5, &obs, &moon);2216 Handle<Object> rst = ConstructRiseSet( date, moon, rc);2272 const int rc = ln_get_lunar_rst(local.jd-0.5, &local.posn, &moon); 2273 Handle<Object> rst = ConstructRiseSet(local, moon, rc); 2217 2274 return handle_scope.Close(rst); 2218 2275 }; … … 2832 2889 rc.emplace_back("null"); 2833 2890 rc.emplace_back("delete "); 2891 rc.emplace_back("JSON.stringify("); 2892 rc.emplace_back("JSON.parse("); 2834 2893 2835 2894 rc.emplace_back("dim.log("); -
trunk/FACT++/src/InterpreterV8.h
r18426 r18828 127 127 static v8::Handle<v8::Value> MoonHorizon(const v8::Arguments &args); 128 128 static v8::Handle<v8::Value> SunHorizon(const v8::Arguments &args); 129 static v8::Handle<v8::Object> ConstructRiseSet(const v8::Handle<v8::Value>, const ln_rst_time &, const bool &);130 129 #endif 131 130
Note:
See TracChangeset
for help on using the changeset viewer.