Changeset 14093
- Timestamp:
- 06/06/12 16:27:15 (12 years ago)
- Location:
- trunk/FACT++/src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/InterpreterV8.cc
r14088 r14093 1 1 #include "InterpreterV8.h" 2 3 #include <boost/tokenizer.hpp> 2 4 3 5 InterpreterV8 *InterpreterV8::This = 0; … … 213 215 214 216 Handle<ObjectTemplate> obj = ObjectTemplate::New(); 215 obj->Set(String::New("index"), rc.first <=-256?Undefined():Integer::New(rc.first), ReadOnly); 216 obj->Set(String::New("name"), rc.first <=-256?Undefined():String::New (rc.second.c_str()), ReadOnly); 217 218 219 obj->Set(String::New("index"), rc.first<=-256?Undefined():Integer::New(rc.first), ReadOnly); 220 obj->Set(String::New("name"), rc.first<=-256?Undefined():String::New(rc.second.c_str()), ReadOnly); 221 //obj->Set(String::New("toString"), String::New(("[Object state "+string(*str)+"]").c_str()), ReadOnly); 217 222 218 223 return handle_scope.Close(obj->NewInstance()); … … 275 280 { 276 281 return String::New(V8::GetVersion()); 282 } 283 284 Handle<Value> Convert(char type, const char* &ptr) 285 { 286 switch (type) 287 { 288 case 'F': { Handle<Value> v=Number::New(*reinterpret_cast<const float*>(ptr)); ptr+=4; return v; } 289 case 'D': { Handle<Value> v=Number::New(*reinterpret_cast<const double*>(ptr)); ptr+=8; return v; } 290 case 'I': { 291 case 'L': Handle<Value> v=Integer::New(*reinterpret_cast<const uint32_t*>(ptr)); ptr += 4; return v; } 292 case 'X': { Handle<Value> v=Integer::New(*reinterpret_cast<const uint64_t*>(ptr)); ptr += 8; return v; } 293 case 'S': { Handle<Value> v=Integer::New(*reinterpret_cast<const uint16_t*>(ptr)); ptr += 2; return v; } 294 case 'C': { Handle<Value> v=Integer::New((uint16_t)*reinterpret_cast<const uint8_t*>(ptr)); ptr += 1; return v; } 295 case ':': { Handle<Value> v=String::New(ptr); return v; } 296 } 297 return Undefined(); 298 } 299 300 301 Handle<Value> InterpreterV8::FuncClose(const Arguments &args) 302 { 303 HandleScope handle_scope; 304 305 const String::Utf8Value str(args.Holder()->Get(String::New("name"))); 306 return Boolean::New(JsUnsubscribe(*str)); 307 } 308 309 Handle<Value> InterpreterV8::FuncGetData(const Arguments &args) 310 { 311 HandleScope handle_scope; 312 313 const String::Utf8Value str(args.Holder()->Get(String::New("name"))); 314 315 const pair<uint64_t, EventImp *> p = JsGetEvent(*str); 316 317 const EventImp *evt = p.second; 318 if (!evt) 319 return Undefined(); 320 321 //if (counter==cnt) 322 // return info.Holder();//Holder()->Get(String::New("data")); 323 //cout << counter << " " << cnt << endl; 324 325 //const void *ppp = Local<External>::Cast(info.This()->GetInternalField(0))->Value(); 326 //info.This()->SetInternalField(0, External::New((void*)evt)); 327 //cout << info.This(). 328 329 const vector<Description> vec = JsDescription(*str); 330 331 Handle<Array> ret = Array::New(); 332 ret->Set(String::New("format"), String::New(evt->GetFormat().c_str()), ReadOnly); 333 ret->Set(String::New("named"), Boolean::New(vec.size()>0), ReadOnly); 334 ret->Set(String::New("counter"), Integer::New(p.first), ReadOnly); 335 ret->Set(String::New("time"), Date::New(evt->GetJavaDate()), ReadOnly); 336 337 typedef boost::char_separator<char> separator; 338 const boost::tokenizer<separator> tokenizer(evt->GetFormat(), separator(";:")); 339 340 const vector<string> tok(tokenizer.begin(), tokenizer.end()); 341 342 Handle<Array> obj = tok.size()==1 ? ret : Array::New(); 343 344 const char *ptr = evt->GetText(); 345 try 346 { 347 size_t pos = 1; 348 for (auto it=tok.begin(); it!=tok.end(); it++, pos++) 349 { 350 char type = (*it)[0]; 351 it++; 352 353 if (it==tok.end() && type=='C') 354 type = ':'; 355 356 if (it==tok.end() && type!=':') 357 return ThrowException(String::New(("Format string invalid '"+evt->GetFormat()+"'").c_str())); 358 359 string name = pos<vec.size() ? vec[pos].name : ""; 360 if (tok.size()==1) 361 name = "data"; 362 363 const uint32_t cnt = it==tok.end() ? 1 : stoi(it->c_str()); 364 365 if (cnt==1) 366 { 367 const Handle<Value> v = Convert(type, ptr); 368 if (tok.size()>1) 369 obj->Set(pos-1, v); 370 if (!name.empty()) 371 obj->Set(String::New(name.c_str()), v); 372 } 373 else 374 { 375 Handle<Array> a = Array::New(cnt); 376 for (uint32_t i=0; i<cnt; i++) 377 a->Set(i, Convert(type, ptr)); 378 if (tok.size()>1) 379 obj->Set(pos-1, a); 380 if (!name.empty()) 381 obj->Set(String::New(name.c_str()), a); 382 } 383 384 if (it==tok.end()) 385 break; 386 } 387 388 if (tok.size()>1) 389 ret->Set(String::New("data"), obj, ReadOnly); 390 391 return handle_scope.Close(ret); 392 } 393 catch (...) 394 { 395 return ThrowException(String::New(("Format string conversion '"+evt->GetFormat()+"' failed.").c_str())); 396 } 397 } 398 399 /* 400 void Cleanup( Persistent<Value> object, void *parameter ) 401 { 402 cout << "======================> RemoveMyObj()" << endl; 403 }*/ 404 405 Handle<Value> InterpreterV8::FuncOpen(const Arguments &args) 406 { 407 if (args.Length()!=1) 408 return ThrowException(String::New("Number of arguments must be exactly 1.")); 409 410 if (!args[0]->IsString()) 411 return ThrowException(String::New("Argument 1 must be a string.")); 412 413 //if (!args.IsConstructCall()) 414 // return ThrowException(String::New("Must be used as constructor.")); 415 416 HandleScope handle_scope; 417 418 const String::Utf8Value str(args[0]); 419 420 JsSubscribe(*str); 421 422 //args.This()->SetInternalField(0, External::New((void*)test)); 423 //obj->SetInternalFieldCount(1); 424 425 Handle<ObjectTemplate> obj = ObjectTemplate::New(); 426 obj->Set(String::New("get"), FunctionTemplate::New(WrapGetData), ReadOnly); 427 obj->Set(String::New("close"), FunctionTemplate::New(WrapClose), ReadOnly); 428 obj->Set(String::New("name"), String::New(*str), ReadOnly); 429 //obj->Set(String::New("toString"), String::New(("[object Dim "+string(*str)+"]").c_str()), ReadOnly); 430 431 return obj->NewInstance(); 432 433 // Persistent<Object> p = Persistent<Object>::New(obj->NewInstance()); 434 // obj.MakeWeak((void*)1, Cleanup); 435 // return obj; 277 436 } 278 437 … … 293 452 dim->Set(String::New("state"), FunctionTemplate::New(WrapState), ReadOnly); 294 453 dim->Set(String::New("sleep"), FunctionTemplate::New(WrapSleep), ReadOnly); 454 dim->Set(String::New("open"), FunctionTemplate::New(WrapOpen), ReadOnly); 295 455 296 456 Handle<ObjectTemplate> global = ObjectTemplate::New(); … … 326 486 //context.Dispose(); 327 487 488 cout << "JsEnd" << endl; 328 489 JsEnd(filename); 329 490 -
trunk/FACT++/src/InterpreterV8.h
r14084 r14093 8 8 #include <v8.h> 9 9 #endif 10 11 #include "Description.h" 12 #include "EventImp.h" 10 13 11 14 class InterpreterV8 … … 29 32 v8::Handle<v8::Value> FuncExit(const v8::Arguments& args); 30 33 v8::Handle<v8::Value> FuncState(const v8::Arguments& args); 31 v8::Handle<v8::Value> FuncInfo(const v8::Arguments& args); 34 v8::Handle<v8::Value> FuncOpen(const v8::Arguments& args); 35 v8::Handle<v8::Value> FuncGetData(const v8::Arguments &args); 36 v8::Handle<v8::Value> FuncClose(const v8::Arguments &args); 32 37 33 38 static v8::Handle<v8::Value> FuncVersion(const v8::Arguments&); … … 39 44 static v8::Handle<v8::Value> WrapExit(const v8::Arguments &args) { if (This) return This->FuncExit(args); else return v8::Undefined(); } 40 45 static v8::Handle<v8::Value> WrapState(const v8::Arguments &args) { if (This) return This->FuncState(args); else return v8::Undefined(); } 41 static v8::Handle<v8::Value> WrapInfo(const v8::Arguments &args) { if (This) return This->FuncInfo(args); else return v8::Undefined(); } 46 static v8::Handle<v8::Value> WrapOpen(const v8::Arguments &args) { if (This) return This->FuncOpen(args); else return v8::Undefined(); } 47 static v8::Handle<v8::Value> WrapGetData(const v8::Arguments &args) { if (This) return This->FuncGetData(args); else return v8::Undefined(); } 48 static v8::Handle<v8::Value> WrapClose(const v8::Arguments &args) { if (This) return This->FuncClose(args); else return v8::Undefined(); } 42 49 #endif 43 50 … … 64 71 virtual std::pair<int32_t, std::string> JsState(const std::string &) { return std::make_pair(-256, ""); }; 65 72 virtual void *JsSubscribe(const std::string &) { return 0; }; 73 virtual bool JsUnsubscribe(const std::string &) { return false; }; 74 virtual std::vector<Description> JsDescription(const std::string &) { return std::vector<Description>(); }; 75 virtual std::pair<uint64_t, EventImp *> JsGetEvent(const std::string &) { return std::make_pair(0, (EventImp*)0); }; 66 76 67 77 bool JsRun(const std::string &, const std::map<std::string,std::string> & = std::map<std::string,std::string>());
Note:
See TracChangeset
for help on using the changeset viewer.