Changeset 14603 for trunk/FACT++
- Timestamp:
- 11/11/12 19:53:36 (12 years ago)
- Location:
- trunk/FACT++/src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/InterpreterV8.cc
r14602 r14603 19 19 20 20 #include <v8.h> 21 22 #include "tools.h" 21 23 22 24 using namespace std; … … 117 119 HandleScope handle_scope; 118 120 119 const Handle<Script> script = Script::Compile(String::New(code.c_str()) );121 const Handle<Script> script = Script::Compile(String::New(code.c_str()), String::New("internal")); 120 122 if (script.IsEmpty()) 121 123 return Undefined(); … … 140 142 TryCatch exception; 141 143 142 const Handle<Script> sleep = Script::Compile(String::New(("dim.sleep("+to_string(ms)+");").c_str()) );144 const Handle<Script> sleep = Script::Compile(String::New(("dim.sleep("+to_string(ms)+");").c_str()), String::New("internal")); 143 145 if (ms==0 || (!sleep.IsEmpty() && !sleep->Run().IsEmpty())) 144 146 { … … 249 251 "{" 250 252 "var s = dim.state(name);" 251 "if(!"+index+")throw 'Waitig for state "+arg1+" of server "+arg0+" failed.';"253 "if(!"+index+")throw new Error('Waitig for state "+arg1+" of server "+arg0+" failed.');" 252 254 "if(state=="+index+")return true;"; 253 255 if (timeout) … … 269 271 TryCatch exception; 270 272 271 const Handle<Script> script = Script::Compile(String::New(code.c_str()) );273 const Handle<Script> script = Script::Compile(String::New(code.c_str()), String::New("internal")); 272 274 if (script.IsEmpty()) 273 275 return Undefined(); … … 874 876 HandleScope handle_scope; 875 877 876 const Handle<Script> sleep = Script::Compile(String::New("dim.sleep();") );878 const Handle<Script> sleep = Script::Compile(String::New("dim.sleep();"), String::New("internal")); 877 879 if (sleep.IsEmpty()) 878 880 return Undefined(); … … 1409 1411 return false; 1410 1412 1411 // Print (filename):(line number): (message).1412 const String::Utf8Value filename(message->GetScriptResourceName());1413 1414 1413 ostringstream out; 1415 1414 1416 if (*filename) 1417 out << *filename << ": "; 1418 out << "l." << message->GetLineNumber(); 1415 if (!message->GetScriptResourceName()->IsUndefined()) 1416 { 1417 // Print (filename):(line number): (message). 1418 const String::Utf8Value filename(message->GetScriptResourceName()); 1419 1420 if (*filename) 1421 out << *filename << ": "; 1422 out << "l." << message->GetLineNumber(); 1423 if (*exception) 1424 out << ": "; 1425 } 1426 1419 1427 if (*exception) 1420 out << ": " <<*exception;1428 out << *exception; 1421 1429 1422 1430 JsException(out.str()); … … 1438 1446 JsException(out.str()); 1439 1447 1440 String::Utf8Value stack_trace(try_catch->StackTrace());1448 const String::Utf8Value stack_trace(try_catch->StackTrace()); 1441 1449 if (stack_trace.length()<=0) 1442 1450 return false; 1443 1451 1444 //if (*stack_trace) 1445 // JsException(string("\n")+*stack_trace); 1452 if (!*stack_trace) 1453 return false; 1454 1455 const string trace(*stack_trace); 1456 1457 typedef boost::char_separator<char> separator; 1458 const boost::tokenizer<separator> tokenizer(trace, separator("\n")); 1459 1460 auto it = tokenizer.begin(); 1461 JsException(""); 1462 while (it!=tokenizer.end()) 1463 JsException(*it++); 1446 1464 1447 1465 return false; … … 1521 1539 // CORE 1522 1540 // ========================================================================== 1541 1542 void InterpreterV8::AddFormatToGlobal() const 1543 { 1544 const string code = 1545 "dim.format = function(str, arr)" 1546 "{" 1547 "var i = -1;" 1548 "function callback(exp, p0, p1, p2, p3, p4/*, pos, str*/)" 1549 "{" 1550 "if (exp=='%%')" 1551 "return '%';" 1552 "" 1553 "if (arr[++i]===undefined)" 1554 "return undefined;" 1555 "" 1556 "var exp = p2 ? parseInt(p2.substr(1)) : undefined;" 1557 "var base = p3 ? parseInt(p3.substr(1)) : undefined;" 1558 "" 1559 "var val;" 1560 "switch (p4)" 1561 "{" 1562 "case 's': val = arr[i]; break;" 1563 "case 'c': val = arr[i][0]; break;" 1564 "case 'f': val = parseFloat(arr[i]).toFixed(exp); break;" 1565 "case 'p': val = parseFloat(arr[i]).toPrecision(exp); break;" 1566 "case 'e': val = parseFloat(arr[i]).toExponential(exp); break;" 1567 "case 'x': val = parseInt(arr[i]).toString(base?base:16); break;" 1568 "case 'd': val = parseFloat(parseInt(arr[i], base?base:10).toPrecision(exp)).toFixed(0); break;" 1569 "default:\n" 1570 " throw new SyntaxError('Conversion specifier '+p4+' unknown.');\n" 1571 "}" 1572 "" 1573 "val = typeof(val)=='object' ? JSON.stringify(val) : val.toString(base);" 1574 "" 1575 "var sz = parseInt(p1); /* padding size */" 1576 "var ch = p1 && p1[0]=='0' ? '0' : ' '; /* isnull? */" 1577 "while (val.length<sz)" 1578 "val = p0 !== undefined ? val+ch : ch+val; /* isminus? */" 1579 "" 1580 "return val;" 1581 "}" 1582 "" 1583 "var regex = /%(-)?(0?[0-9]+)?([.][0-9]+)?([#][0-9]+)?(.)/g;" 1584 "return str.replace(regex, callback);" 1585 "}" 1586 "\n" 1587 "String.prototype.format = function()" 1588 "{" 1589 "return dim.format(this, Array.prototype.slice.call(arguments));" 1590 "}" 1591 "\n" 1592 "var format = function()" 1593 "{" 1594 "return dim.format(arguments[0], Array.prototype.slice.call(arguments,1));" 1595 "}"; 1596 1597 Script::New(String::New(code.c_str()), String::New("internal"))->Run(); 1598 } 1523 1599 1524 1600 bool InterpreterV8::JsRun(const string &filename, const map<string, string> &map) … … 1587 1663 fGlobalContext->Global()->Set(String::New("arg"), args, ReadOnly); 1588 1664 1665 AddFormatToGlobal(); 1666 1589 1667 JsStart(filename); 1590 1668 -
trunk/FACT++/src/InterpreterV8.h
r14598 r14603 171 171 void JsHandleState(const std::string &, const State &); 172 172 173 void AddFormatToGlobal() const; 174 173 175 bool JsRun(const std::string &, const std::map<std::string,std::string> & = std::map<std::string,std::string>()); 174 176 static void JsStop();
Note:
See TracChangeset
for help on using the changeset viewer.