| 1 | #include "InterpreterV8.h"
|
|---|
| 2 |
|
|---|
| 3 | #ifdef HAVE_SQL
|
|---|
| 4 | #include "Database.h"
|
|---|
| 5 | #endif
|
|---|
| 6 |
|
|---|
| 7 | #include <boost/tokenizer.hpp>
|
|---|
| 8 |
|
|---|
| 9 | InterpreterV8 *InterpreterV8::This = 0;
|
|---|
| 10 |
|
|---|
| 11 | #ifdef HAVE_V8
|
|---|
| 12 |
|
|---|
| 13 | #include <v8.h>
|
|---|
| 14 | #include <fstream>
|
|---|
| 15 | #include <sstream>
|
|---|
| 16 | #include <iomanip>
|
|---|
| 17 |
|
|---|
| 18 | using namespace std;
|
|---|
| 19 | using namespace v8;
|
|---|
| 20 |
|
|---|
| 21 | bool InterpreterV8::ReportException(TryCatch* try_catch)
|
|---|
| 22 | {
|
|---|
| 23 | const HandleScope handle_scope;
|
|---|
| 24 |
|
|---|
| 25 | const String::Utf8Value exception(try_catch->Exception());
|
|---|
| 26 |
|
|---|
| 27 | if (*exception && string(*exception)=="exit")
|
|---|
| 28 | return true;
|
|---|
| 29 |
|
|---|
| 30 | const Handle<Message> message = try_catch->Message();
|
|---|
| 31 |
|
|---|
| 32 | // Print (filename):(line number): (message).
|
|---|
| 33 | const String::Utf8Value filename(message->GetScriptResourceName());
|
|---|
| 34 |
|
|---|
| 35 | ostringstream out;
|
|---|
| 36 |
|
|---|
| 37 | if (*filename)
|
|---|
| 38 | out << *filename;
|
|---|
| 39 | if (!message.IsEmpty())
|
|---|
| 40 | out << ": l." << message->GetLineNumber();
|
|---|
| 41 | if (*exception)
|
|---|
| 42 | out << ": " << *exception;
|
|---|
| 43 |
|
|---|
| 44 | JsException(out.str());
|
|---|
| 45 |
|
|---|
| 46 | if (message.IsEmpty())
|
|---|
| 47 | return false;
|
|---|
| 48 |
|
|---|
| 49 | // Print line of source code.
|
|---|
| 50 | const String::Utf8Value sourceline(message->GetSourceLine());
|
|---|
| 51 | if (*sourceline)
|
|---|
| 52 | JsException(*sourceline);
|
|---|
| 53 |
|
|---|
| 54 | // Print wavy underline (GetUnderline is deprecated).
|
|---|
| 55 | const int start = message->GetStartColumn();
|
|---|
| 56 | const int end = message->GetEndColumn();
|
|---|
| 57 |
|
|---|
| 58 | out.str("");
|
|---|
| 59 | if (start>0)
|
|---|
| 60 | out << setfill(' ') << setw(start) << ' ';
|
|---|
| 61 | out << setfill('^') << setw(end-start) << '^';
|
|---|
| 62 |
|
|---|
| 63 | JsException(out.str());
|
|---|
| 64 |
|
|---|
| 65 | String::Utf8Value stack_trace(try_catch->StackTrace());
|
|---|
| 66 | if (stack_trace.length()<=0)
|
|---|
| 67 | return false;
|
|---|
| 68 |
|
|---|
| 69 | //if (*stack_trace)
|
|---|
| 70 | // JsException(string("\n")+*stack_trace);
|
|---|
| 71 |
|
|---|
| 72 | return false;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | // Executes a string within the current v8 context.
|
|---|
| 76 | bool InterpreterV8::ExecuteStringNT(const Handle<String> &code, const Handle<Value> &file)
|
|---|
| 77 | {
|
|---|
| 78 | if (code.IsEmpty())
|
|---|
| 79 | return true;
|
|---|
| 80 |
|
|---|
| 81 | const HandleScope handle_scope;
|
|---|
| 82 |
|
|---|
| 83 | const Handle<Script> script = Script::Compile(code, file);
|
|---|
| 84 | if (script.IsEmpty())
|
|---|
| 85 | return false;
|
|---|
| 86 |
|
|---|
| 87 | const Handle<Value> result = script->Run();
|
|---|
| 88 | if (result.IsEmpty())
|
|---|
| 89 | return false;
|
|---|
| 90 |
|
|---|
| 91 | // If all went well and the result wasn't undefined then print
|
|---|
| 92 | // the returned value.
|
|---|
| 93 | if (!result->IsUndefined())
|
|---|
| 94 | JsResult(*String::Utf8Value(result));
|
|---|
| 95 |
|
|---|
| 96 | return true;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | bool InterpreterV8::ExecuteCode(const Handle<String> &code, const Handle<Value> &file)
|
|---|
| 100 | {
|
|---|
| 101 | TryCatch exception;
|
|---|
| 102 |
|
|---|
| 103 | const bool rc = ExecuteStringNT(code, file);
|
|---|
| 104 |
|
|---|
| 105 | if (!exception.CanContinue())
|
|---|
| 106 | return false;
|
|---|
| 107 |
|
|---|
| 108 | if (exception.HasCaught())
|
|---|
| 109 | return ReportException(&exception);
|
|---|
| 110 |
|
|---|
| 111 | return rc;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | bool InterpreterV8::ExecuteCode(const string &code, const string &file)
|
|---|
| 115 | {
|
|---|
| 116 | return ExecuteCode(String::New(code.c_str(), code.size()),
|
|---|
| 117 | String::New(file.c_str()));
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | bool InterpreterV8::ExecuteFile(const string &name)
|
|---|
| 121 | {
|
|---|
| 122 | ifstream fin(name.c_str());
|
|---|
| 123 | if (!fin)
|
|---|
| 124 | {
|
|---|
| 125 | JsException("Error - Could not open file '"+name+"'");
|
|---|
| 126 | return false;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | string buffer;
|
|---|
| 130 | if (!getline(fin, buffer, '\0'))
|
|---|
| 131 | return true;
|
|---|
| 132 |
|
|---|
| 133 | if (fin.fail())
|
|---|
| 134 | {
|
|---|
| 135 | JsException("Error - reading file.");
|
|---|
| 136 | return false;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | return ExecuteCode(buffer, name);
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | Handle<Value> InterpreterV8::FuncWait(const Arguments& args)
|
|---|
| 143 | {
|
|---|
| 144 | if (args.Length()!=2 && args.Length()!=3)
|
|---|
| 145 | return ThrowException(String::New("Number of arguments must be 2 or 3."));
|
|---|
| 146 |
|
|---|
| 147 | if (!args[0]->IsString())
|
|---|
| 148 | return ThrowException(String::New("Argument 1 not a string."));
|
|---|
| 149 |
|
|---|
| 150 | if (!args[1]->IsInt32())
|
|---|
| 151 | return ThrowException(String::New("Argument 2 not an int32."));
|
|---|
| 152 |
|
|---|
| 153 | if (args.Length()==3 && !args[2]->IsUint32())
|
|---|
| 154 | return ThrowException(String::New("Argument 3 not an uint32."));
|
|---|
| 155 |
|
|---|
| 156 | const string server = *String::Utf8Value(args[0]);
|
|---|
| 157 | const int32_t state = args[1]->Int32Value();
|
|---|
| 158 | const uint32_t millisec = args.Length()==3 ? args[2]->Int32Value() : 0;
|
|---|
| 159 |
|
|---|
| 160 | const int rc = JsWait(server, state, millisec);
|
|---|
| 161 |
|
|---|
| 162 | if (rc==0 || rc==1)
|
|---|
| 163 | return Boolean::New(rc);
|
|---|
| 164 |
|
|---|
| 165 | return ThrowException(String::New(("Waitig for state "+to_string(state)+" of server '"+server+"' failed.").c_str()));
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | Handle<Value> InterpreterV8::FuncSend(const Arguments& args)
|
|---|
| 169 | {
|
|---|
| 170 | if (args.Length()==0)
|
|---|
| 171 | return ThrowException(String::New("Number of arguments must be at least 1."));
|
|---|
| 172 |
|
|---|
| 173 | if (!args[0]->IsString())
|
|---|
| 174 | return ThrowException(String::New("Argument 1 must be a string."));
|
|---|
| 175 |
|
|---|
| 176 | const HandleScope handle_scope;
|
|---|
| 177 |
|
|---|
| 178 | const String::Utf8Value str(args[0]);
|
|---|
| 179 |
|
|---|
| 180 | string command = *str;
|
|---|
| 181 |
|
|---|
| 182 | for (int i=1; i<args.Length(); i++)
|
|---|
| 183 | {
|
|---|
| 184 | const String::Utf8Value arg(args[i]);
|
|---|
| 185 | command += " \""+string(*arg)+"\"";
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | return Boolean::New(JsSend(command));
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | Handle<Value> InterpreterV8::FuncSleep(const Arguments& args)
|
|---|
| 192 | {
|
|---|
| 193 | if (args.Length()!=1)
|
|---|
| 194 | return ThrowException(String::New("Number of arguments must be exactly 1."));
|
|---|
| 195 |
|
|---|
| 196 | if (!args[0]->IsUint32())
|
|---|
| 197 | return ThrowException(String::New("Argument 1 must be an uint32."));
|
|---|
| 198 |
|
|---|
| 199 | JsSleep(args[0]->Int32Value());
|
|---|
| 200 |
|
|---|
| 201 | return Undefined();
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | Handle<Value> InterpreterV8::FuncState(const Arguments& args)
|
|---|
| 205 | {
|
|---|
| 206 | if (args.Length()!=1)
|
|---|
| 207 | return ThrowException(String::New("Number of arguments must be exactly 1."));
|
|---|
| 208 |
|
|---|
| 209 | if (!args[0]->IsString())
|
|---|
| 210 | return ThrowException(String::New("Argument 1 must be a string."));
|
|---|
| 211 |
|
|---|
| 212 | // Return state.name/state.index
|
|---|
| 213 |
|
|---|
| 214 | HandleScope handle_scope;
|
|---|
| 215 |
|
|---|
| 216 | const String::Utf8Value str(args[0]);
|
|---|
| 217 |
|
|---|
| 218 | const pair<int32_t, string> rc = JsState(*str);
|
|---|
| 219 |
|
|---|
| 220 | Handle<ObjectTemplate> obj = ObjectTemplate::New();
|
|---|
| 221 |
|
|---|
| 222 | obj->Set(String::New("index"), rc.first<=-256?Undefined():Integer::New(rc.first), ReadOnly);
|
|---|
| 223 | obj->Set(String::New("name"), rc.first<=-256?Undefined():String::New(rc.second.c_str()), ReadOnly);
|
|---|
| 224 | //obj->Set(String::New("toString"), String::New(("[Object state "+string(*str)+"]").c_str()), ReadOnly);
|
|---|
| 225 |
|
|---|
| 226 | return handle_scope.Close(obj->NewInstance());
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | Handle<Value> InterpreterV8::FuncExit(const Arguments &)
|
|---|
| 230 | {
|
|---|
| 231 | v8::V8::TerminateExecution(fThreadId);
|
|---|
| 232 | return ThrowException(String::New("exit"));
|
|---|
| 233 | /*
|
|---|
| 234 | if (args.Length()!=1)
|
|---|
| 235 | return ThrowException(String::New("Number of arguments must be exactly 1."));
|
|---|
| 236 |
|
|---|
| 237 | if (!args[0]->IsUint32())
|
|---|
| 238 | return ThrowException(String::New("Argument 1 must be an uint32."));
|
|---|
| 239 |
|
|---|
| 240 | const HandleScope handle_scope;
|
|---|
| 241 |
|
|---|
| 242 | JsSleep(args[0]->Int32Value());
|
|---|
| 243 | */
|
|---|
| 244 | return Undefined();
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | // The callback that is invoked by v8 whenever the JavaScript 'print'
|
|---|
| 248 | // function is called. Prints its arguments on stdout separated by
|
|---|
| 249 | // spaces and ending with a newline.
|
|---|
| 250 | Handle<Value> InterpreterV8::FuncPrint(const Arguments& args)
|
|---|
| 251 | {
|
|---|
| 252 | for (int i=0; i<args.Length(); i++)
|
|---|
| 253 | {
|
|---|
| 254 | const HandleScope handle_scope;
|
|---|
| 255 |
|
|---|
| 256 | const String::Utf8Value str(args[i]);
|
|---|
| 257 | if (*str)
|
|---|
| 258 | JsPrint(*str);
|
|---|
| 259 | }
|
|---|
| 260 | return Undefined();
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | Handle<Value> InterpreterV8::FuncOut(const Arguments& args)
|
|---|
| 264 | {
|
|---|
| 265 | for (int i=0; i<args.Length(); i++)
|
|---|
| 266 | {
|
|---|
| 267 | const HandleScope handle_scope;
|
|---|
| 268 |
|
|---|
| 269 | const String::Utf8Value str(args[i]);
|
|---|
| 270 | if (*str)
|
|---|
| 271 | JsOut(*str);
|
|---|
| 272 | }
|
|---|
| 273 | return Undefined();
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| 276 | // The callback that is invoked by v8 whenever the JavaScript 'load'
|
|---|
| 277 | // function is called. Loads, compiles and executes its argument
|
|---|
| 278 | // JavaScript file.
|
|---|
| 279 | Handle<Value> InterpreterV8::FuncInclude(const Arguments& args)
|
|---|
| 280 | {
|
|---|
| 281 | for (int i = 0; i<args.Length(); i++)
|
|---|
| 282 | {
|
|---|
| 283 | const HandleScope handle_scope;
|
|---|
| 284 |
|
|---|
| 285 | const String::Utf8Value file(args[i]);
|
|---|
| 286 | if (*file == NULL)
|
|---|
| 287 | return ThrowException(String::New(("Error loading file '"+string(*file)+"'").c_str()));
|
|---|
| 288 |
|
|---|
| 289 | if (!ExecuteFile(*file))
|
|---|
| 290 | return ThrowException(String::New(("Execution of '"+string(*file)+"' failed").c_str()));
|
|---|
| 291 | }
|
|---|
| 292 | return Undefined();
|
|---|
| 293 | }
|
|---|
| 294 |
|
|---|
| 295 | Handle<Value> InterpreterV8::FuncVersion(const Arguments&)
|
|---|
| 296 | {
|
|---|
| 297 | return String::New(V8::GetVersion());
|
|---|
| 298 | }
|
|---|
| 299 |
|
|---|
| 300 | Handle<Value> InterpreterV8::FuncDbClose(const Arguments &args)
|
|---|
| 301 | {
|
|---|
| 302 | HandleScope handle_scope;
|
|---|
| 303 |
|
|---|
| 304 | void *ptr = Local<External>::Cast(args.This()->GetInternalField(0))->Value();
|
|---|
| 305 | if (!ptr)
|
|---|
| 306 | return Boolean::New(false);
|
|---|
| 307 |
|
|---|
| 308 | #ifdef HAVE_SQL
|
|---|
| 309 | Database *db = reinterpret_cast<Database*>(ptr);
|
|---|
| 310 | auto it = find(fDatabases.begin(), fDatabases.end(), db);
|
|---|
| 311 | fDatabases.erase(it);
|
|---|
| 312 | delete db;
|
|---|
| 313 | #endif
|
|---|
| 314 |
|
|---|
| 315 | args.This()->SetInternalField(0, External::New(0));
|
|---|
| 316 |
|
|---|
| 317 | return Boolean::New(true);
|
|---|
| 318 | }
|
|---|
| 319 | Handle<Value> InterpreterV8::FuncDbQuery(const Arguments &args)
|
|---|
| 320 | {
|
|---|
| 321 | if (args.Length()!=1)
|
|---|
| 322 | return ThrowException(String::New("Number of arguments must be exactly 1."));
|
|---|
| 323 |
|
|---|
| 324 | if (!args[0]->IsString())
|
|---|
| 325 | return ThrowException(String::New("Both arguments must be a string."));
|
|---|
| 326 |
|
|---|
| 327 | HandleScope handle_scope;
|
|---|
| 328 |
|
|---|
| 329 | void *ptr = Local<External>::Cast(args.This()->GetInternalField(0))->Value();
|
|---|
| 330 | if (!ptr)
|
|---|
| 331 | return Undefined();
|
|---|
| 332 |
|
|---|
| 333 | const String::Utf8Value query(args[0]);
|
|---|
| 334 |
|
|---|
| 335 | #ifdef HAVE_SQL
|
|---|
| 336 | try
|
|---|
| 337 | {
|
|---|
| 338 | Database *db = reinterpret_cast<Database*>(ptr);
|
|---|
| 339 |
|
|---|
| 340 | const mysqlpp::StoreQueryResult res = db->query(*query).store();
|
|---|
| 341 |
|
|---|
| 342 | Handle<Array> ret = Array::New();
|
|---|
| 343 | ret->Set(String::New("table"), String::New(res.table()), ReadOnly);
|
|---|
| 344 |
|
|---|
| 345 | Handle<Array> cols = Array::New();
|
|---|
| 346 |
|
|---|
| 347 | int irow=0;
|
|---|
| 348 | for (vector<mysqlpp::Row>::const_iterator it=res.begin(); it<res.end(); it++)
|
|---|
| 349 | {
|
|---|
| 350 | Handle<Array> row = Array::New();
|
|---|
| 351 |
|
|---|
| 352 | const mysqlpp::FieldNames *list = it->field_list().list;
|
|---|
| 353 |
|
|---|
| 354 | for (size_t i=0; i<it->size(); i++)
|
|---|
| 355 | {
|
|---|
| 356 | const Handle<Value> name = String::New((*list)[i].c_str());
|
|---|
| 357 | if (irow==0)
|
|---|
| 358 | cols->Set(Integer::NewFromUnsigned(i), name, ReadOnly);
|
|---|
| 359 |
|
|---|
| 360 | if ((*it)[i].is_null())
|
|---|
| 361 | {
|
|---|
| 362 | row->Set(name, Undefined(), ReadOnly);
|
|---|
| 363 | continue;
|
|---|
| 364 | }
|
|---|
| 365 |
|
|---|
| 366 | const string sql_type = (*it)[i].type().sql_name();
|
|---|
| 367 |
|
|---|
| 368 | const bool uns = sql_type.find("UNSIGNED")==string::npos;
|
|---|
| 369 |
|
|---|
| 370 | if (sql_type.find("BIGINT")!=string::npos)
|
|---|
| 371 | {
|
|---|
| 372 | if (uns)
|
|---|
| 373 | {
|
|---|
| 374 | const uint64_t val = (uint64_t)(*it)[i];
|
|---|
| 375 | if (val>UINT32_MAX)
|
|---|
| 376 | row->Set(name, Number::New(val), ReadOnly);
|
|---|
| 377 | else
|
|---|
| 378 | row->Set(name, Integer::NewFromUnsigned(val), ReadOnly);
|
|---|
| 379 | }
|
|---|
| 380 | else
|
|---|
| 381 | {
|
|---|
| 382 | const int64_t val = (int64_t)(*it)[i];
|
|---|
| 383 | if (val<INT32_MIN || val>INT32_MAX)
|
|---|
| 384 | row->Set(name, Number::New(val), ReadOnly);
|
|---|
| 385 | else
|
|---|
| 386 | row->Set(name, Integer::NewFromUnsigned(val), ReadOnly);
|
|---|
| 387 | }
|
|---|
| 388 | continue;
|
|---|
| 389 | }
|
|---|
| 390 |
|
|---|
| 391 | // 32 bit
|
|---|
| 392 | if (sql_type.find("INT")!=string::npos)
|
|---|
| 393 | {
|
|---|
| 394 | if (uns)
|
|---|
| 395 | row->Set(name, Integer::NewFromUnsigned((uint32_t)(*it)[i]), ReadOnly);
|
|---|
| 396 | else
|
|---|
| 397 | row->Set(name, Integer::New((int32_t)(*it)[i]), ReadOnly);
|
|---|
| 398 | }
|
|---|
| 399 |
|
|---|
| 400 | if (sql_type.find("BOOL")!=string::npos )
|
|---|
| 401 | {
|
|---|
| 402 | row->Set(name, Boolean::New((bool)(*it)[i]), ReadOnly);
|
|---|
| 403 | continue;
|
|---|
| 404 | }
|
|---|
| 405 |
|
|---|
| 406 | if (sql_type.find("FLOAT")!=string::npos)
|
|---|
| 407 | {
|
|---|
| 408 | ostringstream val;
|
|---|
| 409 | val << setprecision(7) << (float)(*it)[i];
|
|---|
| 410 | row->Set(name, Number::New(stod(val.str())), ReadOnly);
|
|---|
| 411 | continue;
|
|---|
| 412 |
|
|---|
| 413 | }
|
|---|
| 414 | if (sql_type.find("DOUBLE")!=string::npos)
|
|---|
| 415 | {
|
|---|
| 416 | row->Set(name, Number::New((double)(*it)[i]), ReadOnly);
|
|---|
| 417 | continue;
|
|---|
| 418 | }
|
|---|
| 419 |
|
|---|
| 420 | if (sql_type.find("CHAR")!=string::npos ||
|
|---|
| 421 | sql_type.find("TEXT")!=string::npos)
|
|---|
| 422 | {
|
|---|
| 423 | row->Set(name, String::New((const char*)(*it)[i]), ReadOnly);
|
|---|
| 424 | continue;
|
|---|
| 425 | }
|
|---|
| 426 |
|
|---|
| 427 | if (sql_type.find("TIMESTAMP")!=string::npos)
|
|---|
| 428 | {
|
|---|
| 429 | row->Set(name, Date::New(time_t(mysqlpp::Time((*it)[i]))*1000), ReadOnly);
|
|---|
| 430 | continue;
|
|---|
| 431 | }
|
|---|
| 432 |
|
|---|
| 433 | if (sql_type.find("DATETIME")!=string::npos)
|
|---|
| 434 | {
|
|---|
| 435 | row->Set(name, Date::New(time_t(mysqlpp::DateTime((*it)[i]))*1000), ReadOnly);
|
|---|
| 436 | continue;
|
|---|
| 437 | }
|
|---|
| 438 |
|
|---|
| 439 | if (sql_type.find(" DATE ")!=string::npos)
|
|---|
| 440 | {
|
|---|
| 441 | row->Set(name, Date::New(time_t((mysqlpp::Date)(*it)[i])*1000), ReadOnly);
|
|---|
| 442 | continue;
|
|---|
| 443 | }
|
|---|
| 444 |
|
|---|
| 445 | }
|
|---|
| 446 |
|
|---|
| 447 | ret->Set(Integer::NewFromUnsigned(irow++), row, ReadOnly);
|
|---|
| 448 | }
|
|---|
| 449 |
|
|---|
| 450 | if (irow>0)
|
|---|
| 451 | ret->Set(String::New("cols"), cols, ReadOnly);
|
|---|
| 452 |
|
|---|
| 453 | return handle_scope.Close(ret);
|
|---|
| 454 | }
|
|---|
| 455 | catch (const exception &e)
|
|---|
| 456 | {
|
|---|
| 457 | return ThrowException(String::New(e.what()));
|
|---|
| 458 | }
|
|---|
| 459 | #endif
|
|---|
| 460 | }
|
|---|
| 461 |
|
|---|
| 462 | Handle<Value> InterpreterV8::FuncDatabase(const Arguments &args)
|
|---|
| 463 | {
|
|---|
| 464 | if (args.Length()!=1)
|
|---|
| 465 | return ThrowException(String::New("Number of arguments must be exactly 1."));
|
|---|
| 466 |
|
|---|
| 467 | if (!args[0]->IsString())
|
|---|
| 468 | return ThrowException(String::New("Argument 1 must be a string."));
|
|---|
| 469 |
|
|---|
| 470 | HandleScope handle_scope;
|
|---|
| 471 |
|
|---|
| 472 | const String::Utf8Value database(args[0]);
|
|---|
| 473 | const String::Utf8Value query (args[1]);
|
|---|
| 474 |
|
|---|
| 475 | #ifdef HAVE_SQL
|
|---|
| 476 | try
|
|---|
| 477 | {
|
|---|
| 478 | Database *db = new Database(*database);
|
|---|
| 479 | fDatabases.push_back(db);
|
|---|
| 480 |
|
|---|
| 481 | Handle<ObjectTemplate> tem = ObjectTemplate::New();
|
|---|
| 482 | tem->Set(String::New("user"), String::New(db->user.c_str()), ReadOnly);
|
|---|
| 483 | tem->Set(String::New("server"), String::New(db->server.c_str()), ReadOnly);
|
|---|
| 484 | tem->Set(String::New("database"), String::New(db->db.c_str()), ReadOnly);
|
|---|
| 485 | tem->Set(String::New("port"), db->port==0?Undefined():Integer::NewFromUnsigned(db->port), ReadOnly);
|
|---|
| 486 | tem->Set(String::New("query"), FunctionTemplate::New(WrapDbQuery), ReadOnly);
|
|---|
| 487 | tem->Set(String::New("close"), FunctionTemplate::New(WrapDbClose), ReadOnly);
|
|---|
| 488 | tem->SetInternalFieldCount(1);
|
|---|
| 489 |
|
|---|
| 490 | Handle<Object> obj = tem->NewInstance();
|
|---|
| 491 | obj->SetInternalField(0, External::New(db));
|
|---|
| 492 |
|
|---|
| 493 | return handle_scope.Close(obj);
|
|---|
| 494 | }
|
|---|
| 495 | catch (const exception &e)
|
|---|
| 496 | {
|
|---|
| 497 | return ThrowException(String::New(e.what()));
|
|---|
| 498 | }
|
|---|
| 499 | #endif
|
|---|
| 500 | }
|
|---|
| 501 |
|
|---|
| 502 | Handle<Value> InterpreterV8::Convert(char type, const char* &ptr)
|
|---|
| 503 | {
|
|---|
| 504 | // Dim values are always unsigned per (FACT++) definition
|
|---|
| 505 | switch (type)
|
|---|
| 506 | {
|
|---|
| 507 | case 'F':
|
|---|
| 508 | {
|
|---|
| 509 | // Remove the "imprecision" effect coming from casting a float to
|
|---|
| 510 | // a double and then showing it with double precision
|
|---|
| 511 | ostringstream val;
|
|---|
| 512 | val << setprecision(7) << *reinterpret_cast<const float*>(ptr);
|
|---|
| 513 | Handle<Value> v=Number::New(stod(val.str()));
|
|---|
| 514 | ptr+=4;
|
|---|
| 515 | return v;
|
|---|
| 516 | }
|
|---|
| 517 | case 'D': { Handle<Value> v=Number::New(*reinterpret_cast<const double*>(ptr)); ptr+=8; return v; }
|
|---|
| 518 | case 'I':
|
|---|
| 519 | case 'L': { Handle<Value> v=Integer::NewFromUnsigned(*reinterpret_cast<const uint32_t*>(ptr)); ptr += 4; return v; }
|
|---|
| 520 | case 'X':
|
|---|
| 521 | {
|
|---|
| 522 | const uint64_t val = *reinterpret_cast<const uint64_t*>(ptr);
|
|---|
| 523 | ptr += 8;
|
|---|
| 524 | if (val>UINT32_MAX)
|
|---|
| 525 | return Number::New(val);
|
|---|
| 526 | return Integer::NewFromUnsigned(val);
|
|---|
| 527 | }
|
|---|
| 528 | case 'S': { Handle<Value> v=Integer::NewFromUnsigned(*reinterpret_cast<const uint16_t*>(ptr)); ptr += 2; return v; }
|
|---|
| 529 | case 'C': { Handle<Value> v=Integer::NewFromUnsigned((uint16_t)*reinterpret_cast<const uint8_t*>(ptr)); ptr += 1; return v; }
|
|---|
| 530 | case ':': { Handle<Value> v=String::New(ptr); return v; }
|
|---|
| 531 | }
|
|---|
| 532 | return Undefined();
|
|---|
| 533 | }
|
|---|
| 534 |
|
|---|
| 535 | Handle<Value> InterpreterV8::FuncClose(const Arguments &args)
|
|---|
| 536 | {
|
|---|
| 537 | HandleScope handle_scope;
|
|---|
| 538 |
|
|---|
| 539 | //const void *ptr = Local<External>::Cast(info.This()->GetInternalField(0))->Value();
|
|---|
| 540 |
|
|---|
| 541 | const String::Utf8Value str(args.Holder()->Get(String::New("name")));
|
|---|
| 542 | return Boolean::New(JsUnsubscribe(*str));
|
|---|
| 543 | }
|
|---|
| 544 |
|
|---|
| 545 | Handle<Value> InterpreterV8::FuncGetData(const Arguments &args)
|
|---|
| 546 | {
|
|---|
| 547 | HandleScope handle_scope;
|
|---|
| 548 |
|
|---|
| 549 | const String::Utf8Value str(args.Holder()->Get(String::New("name")));
|
|---|
| 550 |
|
|---|
| 551 | const pair<uint64_t, EventImp *> p = JsGetEvent(*str);
|
|---|
| 552 |
|
|---|
| 553 | const EventImp *evt = p.second;
|
|---|
| 554 | if (!evt)
|
|---|
| 555 | return Undefined();
|
|---|
| 556 |
|
|---|
| 557 | //if (counter==cnt)
|
|---|
| 558 | // return info.Holder();//Holder()->Get(String::New("data"));
|
|---|
| 559 |
|
|---|
| 560 | const vector<Description> vec = JsDescription(*str);
|
|---|
| 561 |
|
|---|
| 562 | Handle<Array> ret = Array::New();
|
|---|
| 563 | ret->Set(String::New("format"), String::New(evt->GetFormat().c_str()), ReadOnly);
|
|---|
| 564 | ret->Set(String::New("named"), Boolean::New(vec.size()>0), ReadOnly);
|
|---|
| 565 | ret->Set(String::New("counter"), Integer::New(p.first), ReadOnly);
|
|---|
| 566 | ret->Set(String::New("time"), Date::New(evt->GetJavaDate()), ReadOnly);
|
|---|
| 567 | ret->Set(String::New("qos"), Integer::New(evt->GetQoS()), ReadOnly);
|
|---|
| 568 |
|
|---|
| 569 | typedef boost::char_separator<char> separator;
|
|---|
| 570 | const boost::tokenizer<separator> tokenizer(evt->GetFormat(), separator(";:"));
|
|---|
| 571 |
|
|---|
| 572 | const vector<string> tok(tokenizer.begin(), tokenizer.end());
|
|---|
| 573 |
|
|---|
| 574 | Handle<Array> obj = tok.size()==1 ? ret : Array::New();
|
|---|
| 575 |
|
|---|
| 576 | const char *ptr = evt->GetText();
|
|---|
| 577 | try
|
|---|
| 578 | {
|
|---|
| 579 | size_t pos = 1;
|
|---|
| 580 | for (auto it=tok.begin(); it!=tok.end(); it++, pos++)
|
|---|
| 581 | {
|
|---|
| 582 | char type = (*it)[0];
|
|---|
| 583 | it++;
|
|---|
| 584 |
|
|---|
| 585 | if (it==tok.end() && type=='C')
|
|---|
| 586 | type = ':';
|
|---|
| 587 |
|
|---|
| 588 | if (it==tok.end() && type!=':')
|
|---|
| 589 | return ThrowException(String::New(("Format string invalid '"+evt->GetFormat()+"'").c_str()));
|
|---|
| 590 |
|
|---|
| 591 | string name = pos<vec.size() ? vec[pos].name : "";
|
|---|
| 592 | if (tok.size()==1)
|
|---|
| 593 | name = "data";
|
|---|
| 594 |
|
|---|
| 595 | const uint32_t cnt = it==tok.end() ? 1 : stoi(it->c_str());
|
|---|
| 596 |
|
|---|
| 597 | if (cnt==1)
|
|---|
| 598 | {
|
|---|
| 599 | const Handle<Value> v = Convert(type, ptr);
|
|---|
| 600 | if (tok.size()>1)
|
|---|
| 601 | obj->Set(pos-1, v);
|
|---|
| 602 | if (!name.empty())
|
|---|
| 603 | obj->Set(String::New(name.c_str()), v);
|
|---|
| 604 | }
|
|---|
| 605 | else
|
|---|
| 606 | {
|
|---|
| 607 | Handle<Array> a = Array::New(cnt);
|
|---|
| 608 | for (uint32_t i=0; i<cnt; i++)
|
|---|
| 609 | a->Set(i, Convert(type, ptr));
|
|---|
| 610 | if (tok.size()>1)
|
|---|
| 611 | obj->Set(pos-1, a);
|
|---|
| 612 | if (!name.empty())
|
|---|
| 613 | obj->Set(String::New(name.c_str()), a);
|
|---|
| 614 | }
|
|---|
| 615 |
|
|---|
| 616 | if (it==tok.end())
|
|---|
| 617 | break;
|
|---|
| 618 | }
|
|---|
| 619 |
|
|---|
| 620 | if (tok.size()>1)
|
|---|
| 621 | ret->Set(String::New("data"), obj, ReadOnly);
|
|---|
| 622 |
|
|---|
| 623 | return handle_scope.Close(ret);
|
|---|
| 624 | }
|
|---|
| 625 | catch (...)
|
|---|
| 626 | {
|
|---|
| 627 | return ThrowException(String::New(("Format string conversion '"+evt->GetFormat()+"' failed.").c_str()));
|
|---|
| 628 | }
|
|---|
| 629 | }
|
|---|
| 630 |
|
|---|
| 631 | /*
|
|---|
| 632 | void Cleanup( Persistent<Value> object, void *parameter )
|
|---|
| 633 | {
|
|---|
| 634 | cout << "======================> RemoveMyObj()" << endl;
|
|---|
| 635 | }*/
|
|---|
| 636 |
|
|---|
| 637 | Handle<Value> InterpreterV8::FuncOpen(const Arguments &args)
|
|---|
| 638 | {
|
|---|
| 639 | if (args.Length()!=1)
|
|---|
| 640 | return ThrowException(String::New("Number of arguments must be exactly 1."));
|
|---|
| 641 |
|
|---|
| 642 | if (!args[0]->IsString())
|
|---|
| 643 | return ThrowException(String::New("Argument 1 must be a string."));
|
|---|
| 644 |
|
|---|
| 645 | //if (!args.IsConstructCall())
|
|---|
| 646 | // return ThrowException(String::New("Must be used as constructor."));
|
|---|
| 647 |
|
|---|
| 648 | HandleScope handle_scope;
|
|---|
| 649 |
|
|---|
| 650 | const String::Utf8Value str(args[0]);
|
|---|
| 651 |
|
|---|
| 652 | void *ptr = JsSubscribe(*str);
|
|---|
| 653 | if (ptr==0)
|
|---|
| 654 | return ThrowException(String::New(("Subscription to '"+string(*str)+"' already exists.").c_str()));
|
|---|
| 655 |
|
|---|
| 656 | Handle<ObjectTemplate> tem = ObjectTemplate::New();
|
|---|
| 657 | tem->Set(String::New("get"), FunctionTemplate::New(WrapGetData), ReadOnly);
|
|---|
| 658 | tem->Set(String::New("close"), FunctionTemplate::New(WrapClose), ReadOnly);
|
|---|
| 659 | tem->Set(String::New("name"), String::New(*str), ReadOnly);
|
|---|
| 660 | tem->SetInternalFieldCount(1);
|
|---|
| 661 | //tem->Set(String::New("toString"), String::New(("[object Dim "+string(*str)+"]").c_str()), ReadOnly);
|
|---|
| 662 |
|
|---|
| 663 | Handle<Object> obj = tem->NewInstance();
|
|---|
| 664 | obj->SetInternalField(0, External::New(ptr));
|
|---|
| 665 |
|
|---|
| 666 | return handle_scope.Close(obj);
|
|---|
| 667 |
|
|---|
| 668 | // Persistent<Object> p = Persistent<Object>::New(obj->NewInstance());
|
|---|
| 669 | // obj.MakeWeak((void*)1, Cleanup);
|
|---|
| 670 | // return obj;
|
|---|
| 671 | }
|
|---|
| 672 |
|
|---|
| 673 | bool InterpreterV8::JsRun(const string &filename, const map<string, string> &map)
|
|---|
| 674 | {
|
|---|
| 675 | v8::Locker locker;
|
|---|
| 676 | fThreadId = V8::GetCurrentThreadId();
|
|---|
| 677 |
|
|---|
| 678 | JsLoad(filename);
|
|---|
| 679 |
|
|---|
| 680 | HandleScope handle_scope;
|
|---|
| 681 |
|
|---|
| 682 | // Create a template for the global object.
|
|---|
| 683 | Handle<ObjectTemplate> dim = ObjectTemplate::New();
|
|---|
| 684 | dim->Set(String::New("print"), FunctionTemplate::New(WrapPrint), ReadOnly);
|
|---|
| 685 | dim->Set(String::New("out"), FunctionTemplate::New(WrapOut), ReadOnly);
|
|---|
| 686 | dim->Set(String::New("wait"), FunctionTemplate::New(WrapWait), ReadOnly);
|
|---|
| 687 | dim->Set(String::New("send"), FunctionTemplate::New(WrapSend), ReadOnly);
|
|---|
| 688 | dim->Set(String::New("state"), FunctionTemplate::New(WrapState), ReadOnly);
|
|---|
| 689 | dim->Set(String::New("sleep"), FunctionTemplate::New(WrapSleep), ReadOnly);
|
|---|
| 690 | dim->Set(String::New("open"), FunctionTemplate::New(WrapOpen), ReadOnly);
|
|---|
| 691 | dim->Set(String::New("database"), FunctionTemplate::New(WrapDatabase), ReadOnly);
|
|---|
| 692 |
|
|---|
| 693 | Handle<ObjectTemplate> global = ObjectTemplate::New();
|
|---|
| 694 | global->Set(String::New("dim"), dim, ReadOnly);
|
|---|
| 695 | global->Set(String::New("include"), FunctionTemplate::New(WrapInclude), ReadOnly);
|
|---|
| 696 | global->Set(String::New("exit"), FunctionTemplate::New(WrapExit), ReadOnly);
|
|---|
| 697 | global->Set(String::New("version"), FunctionTemplate::New(InterpreterV8::FuncVersion), ReadOnly);
|
|---|
| 698 |
|
|---|
| 699 | // Persistent
|
|---|
| 700 | Persistent<Context> context = Context::New(NULL, global);
|
|---|
| 701 | if (context.IsEmpty())
|
|---|
| 702 | {
|
|---|
| 703 | //printf("Error creating context\n");
|
|---|
| 704 | return false;
|
|---|
| 705 | }
|
|---|
| 706 |
|
|---|
| 707 | v8::Context::Scope scope(context);
|
|---|
| 708 |
|
|---|
| 709 | Local<Array> args = Array::New(map.size());
|
|---|
| 710 | for (auto it=map.begin(); it!=map.end(); it++)
|
|---|
| 711 | args->Set(String::New(it->first.c_str()), String::New(it->second.c_str()));
|
|---|
| 712 | context->Global()->Set(String::New("$"), args, ReadOnly);
|
|---|
| 713 | context->Global()->Set(String::New("arg"), args, ReadOnly);
|
|---|
| 714 |
|
|---|
| 715 | JsStart(filename);
|
|---|
| 716 |
|
|---|
| 717 | //context->Enter();
|
|---|
| 718 | v8::Locker::StartPreemption(10);
|
|---|
| 719 | const bool rc = ExecuteFile(filename);
|
|---|
| 720 |
|
|---|
| 721 | // -----
|
|---|
| 722 | // This is how an exit handler could look like, but there is no way to interrupt it
|
|---|
| 723 | // -----
|
|---|
| 724 | // Handle<Object> obj = Handle<Object>::Cast(context->Global()->Get(String::New("dim")));
|
|---|
| 725 | // if (!obj.IsEmpty())
|
|---|
| 726 | // {
|
|---|
| 727 | // Handle<Value> onexit = obj->Get(String::New("onexit"));
|
|---|
| 728 | // if (!onexit->IsUndefined())
|
|---|
| 729 | // Handle<Function>::Cast(onexit)->NewInstance(0, NULL); // argc, argv
|
|---|
| 730 | // // Handle<Object> result = Handle<Function>::Cast(onexit)->NewInstance(0, NULL); // argc, argv
|
|---|
| 731 | // }
|
|---|
| 732 |
|
|---|
| 733 | v8::Locker::StopPreemption();
|
|---|
| 734 | //context->Exit();
|
|---|
| 735 |
|
|---|
| 736 | context.Dispose();
|
|---|
| 737 |
|
|---|
| 738 | #ifdef HAVE_SQL
|
|---|
| 739 | for (auto it=fDatabases.begin(); it!=fDatabases.end(); it++)
|
|---|
| 740 | delete *it;
|
|---|
| 741 | fDatabases.clear();
|
|---|
| 742 | #endif
|
|---|
| 743 |
|
|---|
| 744 |
|
|---|
| 745 | JsEnd(filename);
|
|---|
| 746 |
|
|---|
| 747 | return rc;
|
|---|
| 748 | }
|
|---|
| 749 |
|
|---|
| 750 | void InterpreterV8::JsStop()
|
|---|
| 751 | {
|
|---|
| 752 | Locker locker;
|
|---|
| 753 | //cout << "Terminate " << fThreadId << endl;
|
|---|
| 754 | if (This->fThreadId>=0)
|
|---|
| 755 | V8::TerminateExecution(This->fThreadId);
|
|---|
| 756 | //cout << "Terminate " << fThreadId << endl;
|
|---|
| 757 | Unlocker unlocker;
|
|---|
| 758 | }
|
|---|
| 759 |
|
|---|
| 760 | #endif
|
|---|
| 761 |
|
|---|