Index: /trunk/FACT++/src/InterpreterV8.cc
===================================================================
--- /trunk/FACT++/src/InterpreterV8.cc	(revision 14578)
+++ /trunk/FACT++/src/InterpreterV8.cc	(revision 14579)
@@ -24,130 +24,94 @@
 using namespace v8;
 
-bool InterpreterV8::ReportException(TryCatch* try_catch)
-{
-    if (!try_catch->CanContinue())
-        return false;
+
+// ==========================================================================
+//                            Simple interface
+// ==========================================================================
+
+Handle<Value> InterpreterV8::FuncExit(const Arguments &)
+{
+    V8::TerminateExecution(fThreadId);
+    return ThrowException(String::New("exit"));
+/*
+    if (args.Length()!=1)
+        return ThrowException(String::New("Number of arguments must be exactly 1."));
+
+    if (!args[0]->IsUint32())
+        return ThrowException(String::New("Argument 1 must be an uint32."));
 
     const HandleScope handle_scope;
 
-    const String::Utf8Value exception(try_catch->Exception());
-
-    if (*exception && string(*exception)=="exit")
-        return true;
-    if (*exception && string(*exception)=="null")
-        return false;
-
-    const Handle<Message> message = try_catch->Message();
-    if (message.IsEmpty())
-        return false;
-
-    // Print (filename):(line number): (message).
-    const String::Utf8Value filename(message->GetScriptResourceName());
-
-    ostringstream out;
-
-    if (*filename)
-        out << *filename << ": ";
-    out << "l." << message->GetLineNumber();
-    if (*exception)
-        out << ": " << *exception;
-
-    JsException(out.str());
-
-    // Print line of source code.
-    const String::Utf8Value sourceline(message->GetSourceLine());
-    if (*sourceline)
-        JsException(*sourceline);
-
-    // Print wavy underline (GetUnderline is deprecated).
-    const int start = message->GetStartColumn();
-    const int end   = message->GetEndColumn();
-
-    out.str("");
-    if (start>0)
-        out << setfill(' ') << setw(start) << ' ';
-    out << setfill('^') << setw(end-start) << '^';
-
-    JsException(out.str());
-
-    String::Utf8Value stack_trace(try_catch->StackTrace());
-    if (stack_trace.length()<=0)
-        return false;
-
-    //if (*stack_trace)
-    //    JsException(string("\n")+*stack_trace);
-
-    return false;
-}
-
-// Executes a string within the current v8 context.
-bool InterpreterV8::ExecuteStringNT(const Handle<String> &code, const Handle<Value> &file)
-{
-    if (code.IsEmpty())
-        return true;
-
-    const HandleScope handle_scope;
-
-    const Handle<Script> script = Script::Compile(code, file);
-    if (script.IsEmpty())
-        return false;
-
-    JsSetState(3);
-
-    const Handle<Value> result = script->Run();
-    if (result.IsEmpty())
-        return false;
-
-    // If all went well and the result wasn't undefined then print
-    // the returned value.
-    if (!result->IsUndefined())
-        JsResult(*String::Utf8Value(result));
-
-    return true;
-}
-
-bool InterpreterV8::ExecuteCode(const Handle<String> &code, const Handle<Value> &file)
-{
-    TryCatch exception;
-
-    const bool rc = ExecuteStringNT(code, file);
-
-    // Check if this is a termination exception
-    //if (!exception.CanContinue())
-    //    return false;
-
-    if (exception.HasCaught())
-        return ReportException(&exception);
-
-    return rc;
-}
-
-bool InterpreterV8::ExecuteCode(const string &code, const string &file)
-{
-    return ExecuteCode(String::New(code.c_str(), code.size()),
-                       String::New(file.c_str()));
-}
-
-bool InterpreterV8::ExecuteFile(const string &name)
-{
-    ifstream fin(name.c_str());
-    if (!fin)
-    {
-        JsException("Error - Could not open file '"+name+"'");
-        return false;
-    }
-
-    string buffer;
-    if (!getline(fin, buffer, '\0'))
-        return true;
-
-    if (fin.fail())
-    {
-        JsException("Error - reading file.");
-        return false;
-    }
-
-    return ExecuteCode(buffer, name);
-}
+    JsSleep(args[0]->Int32Value());
+*/
+    return Undefined();
+}
+
+
+Handle<Value> InterpreterV8::FuncSleep(const Arguments& args)
+{
+    if (args.Length()==0)
+    {
+        // Theoretically, the CPU usage can be reduced by maybe a factor
+        // of four using a larger value, but this also means that the
+        // JavaScript is locked for a longer time.
+        usleep(1000);
+        return Undefined();
+    }
+
+    if (args.Length()!=1)
+        return ThrowException(String::New("Number of arguments must be exactly 1."));
+
+    if (!args[0]->IsUint32())
+        return ThrowException(String::New("Argument 1 must be an uint32."));
+
+    // Using a Javascript function has the advantage that it is fully
+    // interruptable without the need of C++ code
+
+    const string code =
+        "(function(){"
+        "var t=new Date();"
+        "while ((new Date()-t)<"+to_string(args[0]->Int32Value())+") dim.sleep();"
+        "})();";
+
+    HandleScope handle_scope;
+
+    const Handle<Script> script = Script::Compile(String::New(code.c_str()));
+
+    return handle_scope.Close(script->Run());
+
+    //JsSleep(args[0]->Int32Value());
+    //return Undefined();
+}
+
+Handle<Value> InterpreterV8::FuncSend(const Arguments& args)
+{
+    if (args.Length()==0)
+        return ThrowException(String::New("Number of arguments must be at least 1."));
+
+    if (!args[0]->IsString())
+        return ThrowException(String::New("Argument 1 must be a string."));
+
+    HandleScope handle_scope;
+
+    const String::Utf8Value str(args[0]);
+
+    string command = *str;
+
+    // Escape all string arguments. All others can be kept as they are.
+    for (int i=1; i<args.Length(); i++)
+    {
+        const String::Utf8Value arg(args[i]);
+        if (args[i]->IsString())
+            command += " \""+string(*arg)+"\"";
+        else
+            command += " "+string(*arg);
+    }
+
+    return handle_scope.Close(Boolean::New(JsSend(command)));
+}
+
+// ==========================================================================
+//                               State control
+// ==========================================================================
 
 Handle<Value> InterpreterV8::FuncWait(const Arguments& args)
@@ -227,71 +191,4 @@
 }
 
-Handle<Value> InterpreterV8::FuncSend(const Arguments& args)
-{
-    if (args.Length()==0)
-        return ThrowException(String::New("Number of arguments must be at least 1."));
-
-    if (!args[0]->IsString())
-        return ThrowException(String::New("Argument 1 must be a string."));
-
-    HandleScope handle_scope;
-
-    const String::Utf8Value str(args[0]);
-
-    string command = *str;
-
-    // Escape all string arguments. All others can be kept as they are.
-    for (int i=1; i<args.Length(); i++)
-    {
-        const String::Utf8Value arg(args[i]);
-        if (args[i]->IsString())
-            command += " \""+string(*arg)+"\"";
-        else
-            command += " "+string(*arg);
-    }
-
-    return handle_scope.Close(Boolean::New(JsSend(command)));
-}
-
-Handle<Value> InterpreterV8::FuncSleep(const Arguments& args)
-{
-    if (args.Length()==0)
-    {
-        // Theoretically, the CPU usage can be reduced by maybe a factor
-        // of four using a larger value, but this also means that the
-        // JavaScript is locked for a longer time.
-        usleep(1000);
-        return Undefined();
-    }
-
-    if (args.Length()!=1)
-        return ThrowException(String::New("Number of arguments must be exactly 1."));
-
-    if (!args[0]->IsUint32())
-        return ThrowException(String::New("Argument 1 must be an uint32."));
-
-    // Using a Javascript function has the advantage that it is fully
-    // interruptable without the need of C++ code
-
-    const string code =
-        "(function(){"
-        "var t=new Date();"
-        "while ((new Date()-t)<"+to_string(args[0]->Int32Value())+") dim.sleep();"
-        "})();";
-
-    HandleScope handle_scope;
-
-    const Handle<Script> script = Script::Compile(String::New(code.c_str()));
-
-    return handle_scope.Close(script->Run());
-
-    //JsSleep(args[0]->Int32Value());
-    //return Undefined();
-}
-
-// ==========================================================================
-//                               State control
-// ==========================================================================
-
 Handle<Value> InterpreterV8::FuncState(const Arguments& args)
 {
@@ -404,21 +301,4 @@
 // ==========================================================================
 
-Handle<Value> InterpreterV8::FuncExit(const Arguments &)
-{
-    V8::TerminateExecution(fThreadId);
-    return ThrowException(String::New("exit"));
-/*
-    if (args.Length()!=1)
-        return ThrowException(String::New("Number of arguments must be exactly 1."));
-
-    if (!args[0]->IsUint32())
-        return ThrowException(String::New("Argument 1 must be an uint32."));
-
-    const HandleScope handle_scope;
-
-    JsSleep(args[0]->Int32Value());
-*/
-    return Undefined();
-}
 
 // The callback that is invoked by v8 whenever the JavaScript 'print'
@@ -1151,5 +1031,5 @@
 }
 
-Handle<Value> InterpreterV8::LocalToString(const Arguments &args)
+Handle<Value> InterpreterV8::LocalToString(const Arguments &/*args*/)
 {
     return String::New("[object Local]");
@@ -1166,10 +1046,10 @@
 }
 
-Handle<Value> InterpreterV8::SkyToString(const Arguments &args)
+Handle<Value> InterpreterV8::SkyToString(const Arguments &/*args*/)
 {
     return String::New("[object Sky]");
 }
 
-Handle<Value> InterpreterV8::MoonToString(const Arguments &args)
+Handle<Value> InterpreterV8::MoonToString(const Arguments &/*args*/)
 {
     return String::New("[object Moon]");
@@ -1206,5 +1086,13 @@
 Handle<Value> InterpreterV8::MoonDisk(const Arguments &args)
 {
-    return Undefined();
+    HandleScope handle_scope;
+
+    if (args.Length()>1)
+        return ThrowException(String::New("disk must not be called with more than one argument."));
+
+    const uint64_t v = uint64_t(args[0]->NumberValue());
+    const Time utc = args.Length()==0 ? Time() : Time(v/1000, v%1000);
+
+    handle_scope.Close(Number::New(ln_get_lunar_disk(utc.JD())));
 }
 
@@ -1374,4 +1262,135 @@
 }
 #endif
+
+// ==========================================================================
+//                            Process control
+// ==========================================================================
+
+bool InterpreterV8::ReportException(TryCatch* try_catch)
+{
+    if (!try_catch->CanContinue())
+        return false;
+
+    const HandleScope handle_scope;
+
+    const String::Utf8Value exception(try_catch->Exception());
+
+    if (*exception && string(*exception)=="exit")
+        return true;
+    if (*exception && string(*exception)=="null")
+        return false;
+
+    const Handle<Message> message = try_catch->Message();
+    if (message.IsEmpty())
+        return false;
+
+    // Print (filename):(line number): (message).
+    const String::Utf8Value filename(message->GetScriptResourceName());
+
+    ostringstream out;
+
+    if (*filename)
+        out << *filename << ": ";
+    out << "l." << message->GetLineNumber();
+    if (*exception)
+        out << ": " << *exception;
+
+    JsException(out.str());
+
+    // Print line of source code.
+    const String::Utf8Value sourceline(message->GetSourceLine());
+    if (*sourceline)
+        JsException(*sourceline);
+
+    // Print wavy underline (GetUnderline is deprecated).
+    const int start = message->GetStartColumn();
+    const int end   = message->GetEndColumn();
+
+    out.str("");
+    if (start>0)
+        out << setfill(' ') << setw(start) << ' ';
+    out << setfill('^') << setw(end-start) << '^';
+
+    JsException(out.str());
+
+    String::Utf8Value stack_trace(try_catch->StackTrace());
+    if (stack_trace.length()<=0)
+        return false;
+
+    //if (*stack_trace)
+    //    JsException(string("\n")+*stack_trace);
+
+    return false;
+}
+
+// Executes a string within the current v8 context.
+bool InterpreterV8::ExecuteStringNT(const Handle<String> &code, const Handle<Value> &file)
+{
+    if (code.IsEmpty())
+        return true;
+
+    const HandleScope handle_scope;
+
+    const Handle<Script> script = Script::Compile(code, file);
+    if (script.IsEmpty())
+        return false;
+
+    JsSetState(3);
+
+    const Handle<Value> result = script->Run();
+    if (result.IsEmpty())
+        return false;
+
+    // If all went well and the result wasn't undefined then print
+    // the returned value.
+    if (!result->IsUndefined())
+        JsResult(*String::Utf8Value(result));
+
+    return true;
+}
+
+bool InterpreterV8::ExecuteCode(const Handle<String> &code, const Handle<Value> &file)
+{
+    TryCatch exception;
+
+    const bool rc = ExecuteStringNT(code, file);
+
+    // Check if this is a termination exception
+    //if (!exception.CanContinue())
+    //    return false;
+
+    if (exception.HasCaught())
+        return ReportException(&exception);
+
+    return rc;
+}
+
+bool InterpreterV8::ExecuteCode(const string &code, const string &file)
+{
+    return ExecuteCode(String::New(code.c_str(), code.size()),
+                       String::New(file.c_str()));
+}
+
+bool InterpreterV8::ExecuteFile(const string &name)
+{
+    ifstream fin(name.c_str());
+    if (!fin)
+    {
+        JsException("Error - Could not open file '"+name+"'");
+        return false;
+    }
+
+    string buffer;
+    if (!getline(fin, buffer, '\0'))
+        return true;
+
+    if (fin.fail())
+    {
+        JsException("Error - reading file.");
+        return false;
+    }
+
+    return ExecuteCode(buffer, name);
+}
 
 // ==========================================================================
@@ -1497,74 +1516,7 @@
 
 #endif
-// ========================================================================
-/*
- const double lon = -(17.+53./60+26.525/3600);
- const double lat =   28.+45./60+42.462/3600;
-
- Time now;
-
- const double disk = ln_get_lunar_disk(now.JD();
-
- ln_lnlat_posn obs;
- obs.lng = lon;
- obs.lat = lat;
-
- ln_equ_posn equ;
- ln_hrz_posn hrz;
-
- // Source
- equ.ra  = ra*15;
- equ.dec = dec;
-
- // Moon
- ln_get_lunar_equ_coords_prec(now.JD(), &moon, 0.01);
-
- ln_get_hrz_from_equ(&equ, &obs, now.JD(), &hrz);
- ln_get_equ_from_hrz(&hrz, &obs, now.JD(), &equ);
-*/
-
-/*
- Sky   { ra, dec, toLocal(time[, obs]), moon(time) }
- Local { zd, az,  toSky  (time[, obs]), moon(time) }
-
- {zd, az} = getLocalCoordinates({ra, dec}[, time [, obs]]);
- {zd, az} = getMoonPosition(time);
- float    = getMoonDisk(time);
-
- // -----------------------------------------------------------------------
-
- void Callack(Arguments &args)
- {
- }
-
- Handle<ObjectTemplate> sky = ObjectTemplate::New();
- sky->SetCallAsFunctionHandler(Callback);
- dim->Set(String::New("Sky"), sky, ReadOnly);
-
- void v8::ObjectTemplate::SetCallAsFunctionHandler	(	InvocationCallback 	callback,
- Handle< Value > 	data = Handle< Value >()
-)			
-
-    Handle<ObjectTemplate> sky = ObjectTemplate::New();
-    sky->Set(String::New("toLocal"), FunctionTemplate::New(WrapToLocal), ReadOnly);
-    sky->Set(String::New("ra"),  Float::New(ra));
-    sky->Set(String::New("dec"), Float::New(dec));
-    return sky::NewInstance();
-
-    Handle<ObjectTemplate> loc = ObjectTemplate::New();
-    loc->Set(String::New("toSky"), FunctionTemplate::New(WrapToSky), ReadOnly);
-    loc->Set(String::New("zd"), Float::New(zd));
-    loc->Set(String::New("az"), Float::New(az));
-    return loc::NewInstance();
-
-
-
-
-
-
- */
-
-
-
-
-
+
+
+
+
+
