Changeset 14579 for trunk/FACT++/src
- Timestamp:
- 11/07/12 17:43:27 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/InterpreterV8.cc
r14578 r14579 24 24 using namespace v8; 25 25 26 bool InterpreterV8::ReportException(TryCatch* try_catch) 27 { 28 if (!try_catch->CanContinue()) 29 return false; 26 27 // ========================================================================== 28 // Simple interface 29 // ========================================================================== 30 31 Handle<Value> InterpreterV8::FuncExit(const Arguments &) 32 { 33 V8::TerminateExecution(fThreadId); 34 return ThrowException(String::New("exit")); 35 /* 36 if (args.Length()!=1) 37 return ThrowException(String::New("Number of arguments must be exactly 1.")); 38 39 if (!args[0]->IsUint32()) 40 return ThrowException(String::New("Argument 1 must be an uint32.")); 30 41 31 42 const HandleScope handle_scope; 32 43 33 const String::Utf8Value exception(try_catch->Exception()); 34 35 if (*exception && string(*exception)=="exit") 36 return true; 37 if (*exception && string(*exception)=="null") 38 return false; 39 40 const Handle<Message> message = try_catch->Message(); 41 if (message.IsEmpty()) 42 return false; 43 44 // Print (filename):(line number): (message). 45 const String::Utf8Value filename(message->GetScriptResourceName()); 46 47 ostringstream out; 48 49 if (*filename) 50 out << *filename << ": "; 51 out << "l." << message->GetLineNumber(); 52 if (*exception) 53 out << ": " << *exception; 54 55 JsException(out.str()); 56 57 // Print line of source code. 58 const String::Utf8Value sourceline(message->GetSourceLine()); 59 if (*sourceline) 60 JsException(*sourceline); 61 62 // Print wavy underline (GetUnderline is deprecated). 63 const int start = message->GetStartColumn(); 64 const int end = message->GetEndColumn(); 65 66 out.str(""); 67 if (start>0) 68 out << setfill(' ') << setw(start) << ' '; 69 out << setfill('^') << setw(end-start) << '^'; 70 71 JsException(out.str()); 72 73 String::Utf8Value stack_trace(try_catch->StackTrace()); 74 if (stack_trace.length()<=0) 75 return false; 76 77 //if (*stack_trace) 78 // JsException(string("\n")+*stack_trace); 79 80 return false; 81 } 82 83 // Executes a string within the current v8 context. 84 bool InterpreterV8::ExecuteStringNT(const Handle<String> &code, const Handle<Value> &file) 85 { 86 if (code.IsEmpty()) 87 return true; 88 89 const HandleScope handle_scope; 90 91 const Handle<Script> script = Script::Compile(code, file); 92 if (script.IsEmpty()) 93 return false; 94 95 JsSetState(3); 96 97 const Handle<Value> result = script->Run(); 98 if (result.IsEmpty()) 99 return false; 100 101 // If all went well and the result wasn't undefined then print 102 // the returned value. 103 if (!result->IsUndefined()) 104 JsResult(*String::Utf8Value(result)); 105 106 return true; 107 } 108 109 bool InterpreterV8::ExecuteCode(const Handle<String> &code, const Handle<Value> &file) 110 { 111 TryCatch exception; 112 113 const bool rc = ExecuteStringNT(code, file); 114 115 // Check if this is a termination exception 116 //if (!exception.CanContinue()) 117 // return false; 118 119 if (exception.HasCaught()) 120 return ReportException(&exception); 121 122 return rc; 123 } 124 125 bool InterpreterV8::ExecuteCode(const string &code, const string &file) 126 { 127 return ExecuteCode(String::New(code.c_str(), code.size()), 128 String::New(file.c_str())); 129 } 130 131 bool InterpreterV8::ExecuteFile(const string &name) 132 { 133 ifstream fin(name.c_str()); 134 if (!fin) 135 { 136 JsException("Error - Could not open file '"+name+"'"); 137 return false; 138 } 139 140 string buffer; 141 if (!getline(fin, buffer, '\0')) 142 return true; 143 144 if (fin.fail()) 145 { 146 JsException("Error - reading file."); 147 return false; 148 } 149 150 return ExecuteCode(buffer, name); 151 } 44 JsSleep(args[0]->Int32Value()); 45 */ 46 return Undefined(); 47 } 48 49 50 Handle<Value> InterpreterV8::FuncSleep(const Arguments& args) 51 { 52 if (args.Length()==0) 53 { 54 // Theoretically, the CPU usage can be reduced by maybe a factor 55 // of four using a larger value, but this also means that the 56 // JavaScript is locked for a longer time. 57 usleep(1000); 58 return Undefined(); 59 } 60 61 if (args.Length()!=1) 62 return ThrowException(String::New("Number of arguments must be exactly 1.")); 63 64 if (!args[0]->IsUint32()) 65 return ThrowException(String::New("Argument 1 must be an uint32.")); 66 67 // Using a Javascript function has the advantage that it is fully 68 // interruptable without the need of C++ code 69 70 const string code = 71 "(function(){" 72 "var t=new Date();" 73 "while ((new Date()-t)<"+to_string(args[0]->Int32Value())+") dim.sleep();" 74 "})();"; 75 76 HandleScope handle_scope; 77 78 const Handle<Script> script = Script::Compile(String::New(code.c_str())); 79 80 return handle_scope.Close(script->Run()); 81 82 //JsSleep(args[0]->Int32Value()); 83 //return Undefined(); 84 } 85 86 Handle<Value> InterpreterV8::FuncSend(const Arguments& args) 87 { 88 if (args.Length()==0) 89 return ThrowException(String::New("Number of arguments must be at least 1.")); 90 91 if (!args[0]->IsString()) 92 return ThrowException(String::New("Argument 1 must be a string.")); 93 94 HandleScope handle_scope; 95 96 const String::Utf8Value str(args[0]); 97 98 string command = *str; 99 100 // Escape all string arguments. All others can be kept as they are. 101 for (int i=1; i<args.Length(); i++) 102 { 103 const String::Utf8Value arg(args[i]); 104 if (args[i]->IsString()) 105 command += " \""+string(*arg)+"\""; 106 else 107 command += " "+string(*arg); 108 } 109 110 return handle_scope.Close(Boolean::New(JsSend(command))); 111 } 112 113 // ========================================================================== 114 // State control 115 // ========================================================================== 152 116 153 117 Handle<Value> InterpreterV8::FuncWait(const Arguments& args) … … 227 191 } 228 192 229 Handle<Value> InterpreterV8::FuncSend(const Arguments& args)230 {231 if (args.Length()==0)232 return ThrowException(String::New("Number of arguments must be at least 1."));233 234 if (!args[0]->IsString())235 return ThrowException(String::New("Argument 1 must be a string."));236 237 HandleScope handle_scope;238 239 const String::Utf8Value str(args[0]);240 241 string command = *str;242 243 // Escape all string arguments. All others can be kept as they are.244 for (int i=1; i<args.Length(); i++)245 {246 const String::Utf8Value arg(args[i]);247 if (args[i]->IsString())248 command += " \""+string(*arg)+"\"";249 else250 command += " "+string(*arg);251 }252 253 return handle_scope.Close(Boolean::New(JsSend(command)));254 }255 256 Handle<Value> InterpreterV8::FuncSleep(const Arguments& args)257 {258 if (args.Length()==0)259 {260 // Theoretically, the CPU usage can be reduced by maybe a factor261 // of four using a larger value, but this also means that the262 // JavaScript is locked for a longer time.263 usleep(1000);264 return Undefined();265 }266 267 if (args.Length()!=1)268 return ThrowException(String::New("Number of arguments must be exactly 1."));269 270 if (!args[0]->IsUint32())271 return ThrowException(String::New("Argument 1 must be an uint32."));272 273 // Using a Javascript function has the advantage that it is fully274 // interruptable without the need of C++ code275 276 const string code =277 "(function(){"278 "var t=new Date();"279 "while ((new Date()-t)<"+to_string(args[0]->Int32Value())+") dim.sleep();"280 "})();";281 282 HandleScope handle_scope;283 284 const Handle<Script> script = Script::Compile(String::New(code.c_str()));285 286 return handle_scope.Close(script->Run());287 288 //JsSleep(args[0]->Int32Value());289 //return Undefined();290 }291 292 // ==========================================================================293 // State control294 // ==========================================================================295 296 193 Handle<Value> InterpreterV8::FuncState(const Arguments& args) 297 194 { … … 404 301 // ========================================================================== 405 302 406 Handle<Value> InterpreterV8::FuncExit(const Arguments &)407 {408 V8::TerminateExecution(fThreadId);409 return ThrowException(String::New("exit"));410 /*411 if (args.Length()!=1)412 return ThrowException(String::New("Number of arguments must be exactly 1."));413 414 if (!args[0]->IsUint32())415 return ThrowException(String::New("Argument 1 must be an uint32."));416 417 const HandleScope handle_scope;418 419 JsSleep(args[0]->Int32Value());420 */421 return Undefined();422 }423 303 424 304 // The callback that is invoked by v8 whenever the JavaScript 'print' … … 1151 1031 } 1152 1032 1153 Handle<Value> InterpreterV8::LocalToString(const Arguments & args)1033 Handle<Value> InterpreterV8::LocalToString(const Arguments &/*args*/) 1154 1034 { 1155 1035 return String::New("[object Local]"); … … 1166 1046 } 1167 1047 1168 Handle<Value> InterpreterV8::SkyToString(const Arguments & args)1048 Handle<Value> InterpreterV8::SkyToString(const Arguments &/*args*/) 1169 1049 { 1170 1050 return String::New("[object Sky]"); 1171 1051 } 1172 1052 1173 Handle<Value> InterpreterV8::MoonToString(const Arguments & args)1053 Handle<Value> InterpreterV8::MoonToString(const Arguments &/*args*/) 1174 1054 { 1175 1055 return String::New("[object Moon]"); … … 1206 1086 Handle<Value> InterpreterV8::MoonDisk(const Arguments &args) 1207 1087 { 1208 return Undefined(); 1088 HandleScope handle_scope; 1089 1090 if (args.Length()>1) 1091 return ThrowException(String::New("disk must not be called with more than one argument.")); 1092 1093 const uint64_t v = uint64_t(args[0]->NumberValue()); 1094 const Time utc = args.Length()==0 ? Time() : Time(v/1000, v%1000); 1095 1096 handle_scope.Close(Number::New(ln_get_lunar_disk(utc.JD()))); 1209 1097 } 1210 1098 … … 1374 1262 } 1375 1263 #endif 1264 1265 // ========================================================================== 1266 // Process control 1267 // ========================================================================== 1268 1269 bool InterpreterV8::ReportException(TryCatch* try_catch) 1270 { 1271 if (!try_catch->CanContinue()) 1272 return false; 1273 1274 const HandleScope handle_scope; 1275 1276 const String::Utf8Value exception(try_catch->Exception()); 1277 1278 if (*exception && string(*exception)=="exit") 1279 return true; 1280 if (*exception && string(*exception)=="null") 1281 return false; 1282 1283 const Handle<Message> message = try_catch->Message(); 1284 if (message.IsEmpty()) 1285 return false; 1286 1287 // Print (filename):(line number): (message). 1288 const String::Utf8Value filename(message->GetScriptResourceName()); 1289 1290 ostringstream out; 1291 1292 if (*filename) 1293 out << *filename << ": "; 1294 out << "l." << message->GetLineNumber(); 1295 if (*exception) 1296 out << ": " << *exception; 1297 1298 JsException(out.str()); 1299 1300 // Print line of source code. 1301 const String::Utf8Value sourceline(message->GetSourceLine()); 1302 if (*sourceline) 1303 JsException(*sourceline); 1304 1305 // Print wavy underline (GetUnderline is deprecated). 1306 const int start = message->GetStartColumn(); 1307 const int end = message->GetEndColumn(); 1308 1309 out.str(""); 1310 if (start>0) 1311 out << setfill(' ') << setw(start) << ' '; 1312 out << setfill('^') << setw(end-start) << '^'; 1313 1314 JsException(out.str()); 1315 1316 String::Utf8Value stack_trace(try_catch->StackTrace()); 1317 if (stack_trace.length()<=0) 1318 return false; 1319 1320 //if (*stack_trace) 1321 // JsException(string("\n")+*stack_trace); 1322 1323 return false; 1324 } 1325 1326 // Executes a string within the current v8 context. 1327 bool InterpreterV8::ExecuteStringNT(const Handle<String> &code, const Handle<Value> &file) 1328 { 1329 if (code.IsEmpty()) 1330 return true; 1331 1332 const HandleScope handle_scope; 1333 1334 const Handle<Script> script = Script::Compile(code, file); 1335 if (script.IsEmpty()) 1336 return false; 1337 1338 JsSetState(3); 1339 1340 const Handle<Value> result = script->Run(); 1341 if (result.IsEmpty()) 1342 return false; 1343 1344 // If all went well and the result wasn't undefined then print 1345 // the returned value. 1346 if (!result->IsUndefined()) 1347 JsResult(*String::Utf8Value(result)); 1348 1349 return true; 1350 } 1351 1352 bool InterpreterV8::ExecuteCode(const Handle<String> &code, const Handle<Value> &file) 1353 { 1354 TryCatch exception; 1355 1356 const bool rc = ExecuteStringNT(code, file); 1357 1358 // Check if this is a termination exception 1359 //if (!exception.CanContinue()) 1360 // return false; 1361 1362 if (exception.HasCaught()) 1363 return ReportException(&exception); 1364 1365 return rc; 1366 } 1367 1368 bool InterpreterV8::ExecuteCode(const string &code, const string &file) 1369 { 1370 return ExecuteCode(String::New(code.c_str(), code.size()), 1371 String::New(file.c_str())); 1372 } 1373 1374 bool InterpreterV8::ExecuteFile(const string &name) 1375 { 1376 ifstream fin(name.c_str()); 1377 if (!fin) 1378 { 1379 JsException("Error - Could not open file '"+name+"'"); 1380 return false; 1381 } 1382 1383 string buffer; 1384 if (!getline(fin, buffer, '\0')) 1385 return true; 1386 1387 if (fin.fail()) 1388 { 1389 JsException("Error - reading file."); 1390 return false; 1391 } 1392 1393 return ExecuteCode(buffer, name); 1394 } 1376 1395 1377 1396 // ========================================================================== … … 1497 1516 1498 1517 #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 1518 1519 1520 1521 1522
Note:
See TracChangeset
for help on using the changeset viewer.