source: trunk/FACT++/src/InterpreterV8.cc@ 14524

Last change on this file since 14524 was 14500, checked in by tbretz, 12 years ago
Allow sleep without argument.
File size: 23.7 KB
Line 
1#include "InterpreterV8.h"
2
3#ifdef HAVE_SQL
4#include "Database.h"
5#endif
6
7#include <boost/tokenizer.hpp>
8
9InterpreterV8 *InterpreterV8::This = 0;
10
11#ifdef HAVE_V8
12
13#include <v8.h>
14#include <fstream>
15#include <sstream>
16#include <iomanip>
17
18using namespace std;
19using namespace v8;
20
21bool 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.
76bool 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
99bool 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
114bool 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
120bool 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
142Handle<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
168Handle<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
191Handle<Value> InterpreterV8::FuncSleep(const Arguments& args)
192{
193 if (args.Length()==0)
194 {
195 JsSleep(1);
196 return Undefined();
197 }
198
199 if (args.Length()!=1)
200 return ThrowException(String::New("Number of arguments must be exactly 1."));
201
202 if (!args[0]->IsUint32())
203 return ThrowException(String::New("Argument 1 must be an uint32."));
204
205 JsSleep(args[0]->Int32Value());
206
207 return Undefined();
208}
209
210Handle<Value> InterpreterV8::FuncState(const Arguments& args)
211{
212 if (args.Length()!=1)
213 return ThrowException(String::New("Number of arguments must be exactly 1."));
214
215 if (!args[0]->IsString())
216 return ThrowException(String::New("Argument 1 must be a string."));
217
218 // Return state.name/state.index
219
220 HandleScope handle_scope;
221
222 const String::Utf8Value str(args[0]);
223
224 const pair<int32_t, string> rc = JsState(*str);
225
226 Handle<ObjectTemplate> obj = ObjectTemplate::New();
227
228 obj->Set(String::New("index"), rc.first<=-256?Undefined():Integer::New(rc.first), ReadOnly);
229 obj->Set(String::New("name"), rc.first<=-256?Undefined():String::New(rc.second.c_str()), ReadOnly);
230 //obj->Set(String::New("toString"), String::New(("[Object state "+string(*str)+"]").c_str()), ReadOnly);
231
232 return handle_scope.Close(obj->NewInstance());
233}
234
235Handle<Value> InterpreterV8::FuncExit(const Arguments &)
236{
237 v8::V8::TerminateExecution(fThreadId);
238 return ThrowException(String::New("exit"));
239/*
240 if (args.Length()!=1)
241 return ThrowException(String::New("Number of arguments must be exactly 1."));
242
243 if (!args[0]->IsUint32())
244 return ThrowException(String::New("Argument 1 must be an uint32."));
245
246 const HandleScope handle_scope;
247
248 JsSleep(args[0]->Int32Value());
249*/
250 return Undefined();
251}
252
253// The callback that is invoked by v8 whenever the JavaScript 'print'
254// function is called. Prints its arguments on stdout separated by
255// spaces and ending with a newline.
256Handle<Value> InterpreterV8::FuncPrint(const Arguments& args)
257{
258 for (int i=0; i<args.Length(); i++)
259 {
260 const HandleScope handle_scope;
261
262 const String::Utf8Value str(args[i]);
263 if (*str)
264 JsPrint(*str);
265 }
266 return Undefined();
267}
268
269Handle<Value> InterpreterV8::FuncAlarm(const Arguments& args)
270{
271 for (int i=0; i<args.Length(); i++)
272 {
273 const HandleScope handle_scope;
274
275 const String::Utf8Value str(args[i]);
276 if (*str)
277 JsAlarm(*str);
278 }
279
280 if (args.Length()==0)
281 JsAlarm();
282
283 return Undefined();
284}
285
286Handle<Value> InterpreterV8::FuncOut(const Arguments& args)
287{
288 for (int i=0; i<args.Length(); i++)
289 {
290 const HandleScope handle_scope;
291
292 const String::Utf8Value str(args[i]);
293 if (*str)
294 JsOut(*str);
295 }
296 return Undefined();
297}
298
299// The callback that is invoked by v8 whenever the JavaScript 'load'
300// function is called. Loads, compiles and executes its argument
301// JavaScript file.
302Handle<Value> InterpreterV8::FuncInclude(const Arguments& args)
303{
304 for (int i = 0; i<args.Length(); i++)
305 {
306 const HandleScope handle_scope;
307
308 const String::Utf8Value file(args[i]);
309 if (*file == NULL)
310 return ThrowException(String::New(("Error loading file '"+string(*file)+"'").c_str()));
311
312 if (!ExecuteFile(*file))
313 return ThrowException(String::New(("Execution of '"+string(*file)+"' failed").c_str()));
314 }
315 return Undefined();
316}
317
318Handle<Value> InterpreterV8::FuncVersion(const Arguments&)
319{
320 return String::New(V8::GetVersion());
321}
322
323Handle<Value> InterpreterV8::FuncDbClose(const Arguments &args)
324{
325 HandleScope handle_scope;
326
327 void *ptr = Local<External>::Cast(args.This()->GetInternalField(0))->Value();
328 if (!ptr)
329 return Boolean::New(false);
330
331#ifdef HAVE_SQL
332 Database *db = reinterpret_cast<Database*>(ptr);
333 auto it = find(fDatabases.begin(), fDatabases.end(), db);
334 fDatabases.erase(it);
335 delete db;
336#endif
337
338 args.This()->SetInternalField(0, External::New(0));
339
340 return Boolean::New(true);
341}
342Handle<Value> InterpreterV8::FuncDbQuery(const Arguments &args)
343{
344 if (args.Length()!=1)
345 return ThrowException(String::New("Number of arguments must be exactly 1."));
346
347 if (!args[0]->IsString())
348 return ThrowException(String::New("Both arguments must be a string."));
349
350 HandleScope handle_scope;
351
352 void *ptr = Local<External>::Cast(args.This()->GetInternalField(0))->Value();
353 if (!ptr)
354 return Undefined();
355
356 const String::Utf8Value query(args[0]);
357
358#ifdef HAVE_SQL
359 try
360 {
361 Database *db = reinterpret_cast<Database*>(ptr);
362
363 const mysqlpp::StoreQueryResult res = db->query(*query).store();
364
365 Handle<Array> ret = Array::New();
366 ret->Set(String::New("table"), String::New(res.table()), ReadOnly);
367
368 Handle<Array> cols = Array::New();
369
370 int irow=0;
371 for (vector<mysqlpp::Row>::const_iterator it=res.begin(); it<res.end(); it++)
372 {
373 Handle<Array> row = Array::New();
374
375 const mysqlpp::FieldNames *list = it->field_list().list;
376
377 for (size_t i=0; i<it->size(); i++)
378 {
379 const Handle<Value> name = String::New((*list)[i].c_str());
380 if (irow==0)
381 cols->Set(Integer::NewFromUnsigned(i), name, ReadOnly);
382
383 if ((*it)[i].is_null())
384 {
385 row->Set(name, Undefined(), ReadOnly);
386 continue;
387 }
388
389 const string sql_type = (*it)[i].type().sql_name();
390
391 const bool uns = sql_type.find("UNSIGNED")==string::npos;
392
393 if (sql_type.find("BIGINT")!=string::npos)
394 {
395 if (uns)
396 {
397 const uint64_t val = (uint64_t)(*it)[i];
398 if (val>UINT32_MAX)
399 row->Set(name, Number::New(val), ReadOnly);
400 else
401 row->Set(name, Integer::NewFromUnsigned(val), ReadOnly);
402 }
403 else
404 {
405 const int64_t val = (int64_t)(*it)[i];
406 if (val<INT32_MIN || val>INT32_MAX)
407 row->Set(name, Number::New(val), ReadOnly);
408 else
409 row->Set(name, Integer::NewFromUnsigned(val), ReadOnly);
410 }
411 continue;
412 }
413
414 // 32 bit
415 if (sql_type.find("INT")!=string::npos)
416 {
417 if (uns)
418 row->Set(name, Integer::NewFromUnsigned((uint32_t)(*it)[i]), ReadOnly);
419 else
420 row->Set(name, Integer::New((int32_t)(*it)[i]), ReadOnly);
421 }
422
423 if (sql_type.find("BOOL")!=string::npos )
424 {
425 row->Set(name, Boolean::New((bool)(*it)[i]), ReadOnly);
426 continue;
427 }
428
429 if (sql_type.find("FLOAT")!=string::npos)
430 {
431 ostringstream val;
432 val << setprecision(7) << (float)(*it)[i];
433 row->Set(name, Number::New(stod(val.str())), ReadOnly);
434 continue;
435
436 }
437 if (sql_type.find("DOUBLE")!=string::npos)
438 {
439 row->Set(name, Number::New((double)(*it)[i]), ReadOnly);
440 continue;
441 }
442
443 if (sql_type.find("CHAR")!=string::npos ||
444 sql_type.find("TEXT")!=string::npos)
445 {
446 row->Set(name, String::New((const char*)(*it)[i]), ReadOnly);
447 continue;
448 }
449
450 if (sql_type.find("TIMESTAMP")!=string::npos)
451 {
452 row->Set(name, Date::New(time_t(mysqlpp::Time((*it)[i]))*1000), ReadOnly);
453 continue;
454 }
455
456 if (sql_type.find("DATETIME")!=string::npos)
457 {
458 row->Set(name, Date::New(time_t(mysqlpp::DateTime((*it)[i]))*1000), ReadOnly);
459 continue;
460 }
461
462 if (sql_type.find(" DATE ")!=string::npos)
463 {
464 row->Set(name, Date::New(time_t((mysqlpp::Date)(*it)[i])*1000), ReadOnly);
465 continue;
466 }
467
468 }
469
470 ret->Set(Integer::NewFromUnsigned(irow++), row, ReadOnly);
471 }
472
473 if (irow>0)
474 ret->Set(String::New("cols"), cols, ReadOnly);
475
476 return handle_scope.Close(ret);
477 }
478 catch (const exception &e)
479 {
480 return ThrowException(String::New(e.what()));
481 }
482#endif
483}
484
485Handle<Value> InterpreterV8::FuncDatabase(const Arguments &args)
486{
487 if (args.Length()!=1)
488 return ThrowException(String::New("Number of arguments must be exactly 1."));
489
490 if (!args[0]->IsString())
491 return ThrowException(String::New("Argument 1 must be a string."));
492
493 HandleScope handle_scope;
494
495 const String::Utf8Value database(args[0]);
496 const String::Utf8Value query (args[1]);
497
498#ifdef HAVE_SQL
499 try
500 {
501 Database *db = new Database(*database);
502 fDatabases.push_back(db);
503
504 Handle<ObjectTemplate> tem = ObjectTemplate::New();
505 tem->Set(String::New("user"), String::New(db->user.c_str()), ReadOnly);
506 tem->Set(String::New("server"), String::New(db->server.c_str()), ReadOnly);
507 tem->Set(String::New("database"), String::New(db->db.c_str()), ReadOnly);
508 tem->Set(String::New("port"), db->port==0?Undefined():Integer::NewFromUnsigned(db->port), ReadOnly);
509 tem->Set(String::New("query"), FunctionTemplate::New(WrapDbQuery), ReadOnly);
510 tem->Set(String::New("close"), FunctionTemplate::New(WrapDbClose), ReadOnly);
511 tem->SetInternalFieldCount(1);
512
513 Handle<Object> obj = tem->NewInstance();
514 obj->SetInternalField(0, External::New(db));
515
516 return handle_scope.Close(obj);
517 }
518 catch (const exception &e)
519 {
520 return ThrowException(String::New(e.what()));
521 }
522#endif
523}
524
525Handle<Value> InterpreterV8::Convert(char type, const char* &ptr)
526{
527 // Dim values are always unsigned per (FACT++) definition
528 switch (type)
529 {
530 case 'F':
531 {
532 // Remove the "imprecision" effect coming from casting a float to
533 // a double and then showing it with double precision
534 ostringstream val;
535 val << setprecision(7) << *reinterpret_cast<const float*>(ptr);
536 Handle<Value> v=Number::New(stod(val.str()));
537 ptr+=4;
538 return v;
539 }
540 case 'D': { Handle<Value> v=Number::New(*reinterpret_cast<const double*>(ptr)); ptr+=8; return v; }
541 case 'I':
542 case 'L': { Handle<Value> v=Integer::NewFromUnsigned(*reinterpret_cast<const uint32_t*>(ptr)); ptr += 4; return v; }
543 case 'X':
544 {
545 const uint64_t val = *reinterpret_cast<const uint64_t*>(ptr);
546 ptr += 8;
547 if (val>UINT32_MAX)
548 return Number::New(val);
549 return Integer::NewFromUnsigned(val);
550 }
551 case 'S': { Handle<Value> v=Integer::NewFromUnsigned(*reinterpret_cast<const uint16_t*>(ptr)); ptr += 2; return v; }
552 case 'C': { Handle<Value> v=Integer::NewFromUnsigned((uint16_t)*reinterpret_cast<const uint8_t*>(ptr)); ptr += 1; return v; }
553 case ':': { Handle<Value> v=String::New(ptr); return v; }
554 }
555 return Undefined();
556}
557
558Handle<Value> InterpreterV8::FuncClose(const Arguments &args)
559{
560 HandleScope handle_scope;
561
562 //const void *ptr = Local<External>::Cast(info.This()->GetInternalField(0))->Value();
563
564 const String::Utf8Value str(args.Holder()->Get(String::New("name")));
565 return Boolean::New(JsUnsubscribe(*str));
566}
567
568Handle<Value> InterpreterV8::FuncGetData(const Arguments &args)
569{
570 HandleScope handle_scope;
571
572 const String::Utf8Value str(args.Holder()->Get(String::New("name")));
573
574 const pair<uint64_t, EventImp *> p = JsGetEvent(*str);
575
576 const EventImp *evt = p.second;
577 if (!evt)
578 return Undefined();
579
580 //if (counter==cnt)
581 // return info.Holder();//Holder()->Get(String::New("data"));
582
583 const vector<Description> vec = JsDescription(*str);
584
585 Handle<Array> ret = Array::New();
586 ret->Set(String::New("format"), String::New(evt->GetFormat().c_str()), ReadOnly);
587 ret->Set(String::New("named"), Boolean::New(vec.size()>0), ReadOnly);
588 ret->Set(String::New("counter"), Integer::New(p.first), ReadOnly);
589 ret->Set(String::New("time"), Date::New(evt->GetJavaDate()), ReadOnly);
590 ret->Set(String::New("qos"), Integer::New(evt->GetQoS()), ReadOnly);
591
592 typedef boost::char_separator<char> separator;
593 const boost::tokenizer<separator> tokenizer(evt->GetFormat(), separator(";:"));
594
595 const vector<string> tok(tokenizer.begin(), tokenizer.end());
596
597 Handle<Array> obj = tok.size()==1 ? ret : Array::New();
598
599 const char *ptr = evt->GetText();
600 try
601 {
602 size_t pos = 1;
603 for (auto it=tok.begin(); it!=tok.end(); it++, pos++)
604 {
605 char type = (*it)[0];
606 it++;
607
608 if (it==tok.end() && type=='C')
609 type = ':';
610
611 if (it==tok.end() && type!=':')
612 return ThrowException(String::New(("Format string invalid '"+evt->GetFormat()+"'").c_str()));
613
614 string name = pos<vec.size() ? vec[pos].name : "";
615 if (tok.size()==1)
616 name = "data";
617
618 const uint32_t cnt = it==tok.end() ? 1 : stoi(it->c_str());
619
620 if (cnt==1)
621 {
622 const Handle<Value> v = Convert(type, ptr);
623 if (tok.size()>1)
624 obj->Set(pos-1, v);
625 if (!name.empty())
626 obj->Set(String::New(name.c_str()), v);
627 }
628 else
629 {
630 Handle<Array> a = Array::New(cnt);
631 for (uint32_t i=0; i<cnt; i++)
632 a->Set(i, Convert(type, ptr));
633 if (tok.size()>1)
634 obj->Set(pos-1, a);
635 if (!name.empty())
636 obj->Set(String::New(name.c_str()), a);
637 }
638
639 if (it==tok.end())
640 break;
641 }
642
643 if (tok.size()>1)
644 ret->Set(String::New("data"), obj, ReadOnly);
645
646 return handle_scope.Close(ret);
647 }
648 catch (...)
649 {
650 return ThrowException(String::New(("Format string conversion '"+evt->GetFormat()+"' failed.").c_str()));
651 }
652}
653
654/*
655void Cleanup( Persistent<Value> object, void *parameter )
656{
657 cout << "======================> RemoveMyObj()" << endl;
658}*/
659
660Handle<Value> InterpreterV8::FuncOpen(const Arguments &args)
661{
662 if (args.Length()!=1)
663 return ThrowException(String::New("Number of arguments must be exactly 1."));
664
665 if (!args[0]->IsString())
666 return ThrowException(String::New("Argument 1 must be a string."));
667
668 //if (!args.IsConstructCall())
669 // return ThrowException(String::New("Must be used as constructor."));
670
671 HandleScope handle_scope;
672
673 const String::Utf8Value str(args[0]);
674
675 void *ptr = JsSubscribe(*str);
676 if (ptr==0)
677 return ThrowException(String::New(("Subscription to '"+string(*str)+"' already exists.").c_str()));
678
679 Handle<ObjectTemplate> tem = ObjectTemplate::New();
680 tem->Set(String::New("get"), FunctionTemplate::New(WrapGetData), ReadOnly);
681 tem->Set(String::New("close"), FunctionTemplate::New(WrapClose), ReadOnly);
682 tem->Set(String::New("name"), String::New(*str), ReadOnly);
683 tem->SetInternalFieldCount(1);
684 //tem->Set(String::New("toString"), String::New(("[object Dim "+string(*str)+"]").c_str()), ReadOnly);
685
686 Handle<Object> obj = tem->NewInstance();
687 obj->SetInternalField(0, External::New(ptr));
688
689 return handle_scope.Close(obj);
690
691 // Persistent<Object> p = Persistent<Object>::New(obj->NewInstance());
692 // obj.MakeWeak((void*)1, Cleanup);
693 // return obj;
694}
695
696bool InterpreterV8::JsRun(const string &filename, const map<string, string> &map)
697{
698 v8::Locker locker;
699 fThreadId = V8::GetCurrentThreadId();
700
701 JsLoad(filename);
702
703 HandleScope handle_scope;
704
705 // Create a template for the global object.
706 Handle<ObjectTemplate> dim = ObjectTemplate::New();
707 dim->Set(String::New("print"), FunctionTemplate::New(WrapPrint), ReadOnly);
708 dim->Set(String::New("alarm"), FunctionTemplate::New(WrapAlarm), ReadOnly);
709 dim->Set(String::New("out"), FunctionTemplate::New(WrapOut), ReadOnly);
710 dim->Set(String::New("wait"), FunctionTemplate::New(WrapWait), ReadOnly);
711 dim->Set(String::New("send"), FunctionTemplate::New(WrapSend), ReadOnly);
712 dim->Set(String::New("state"), FunctionTemplate::New(WrapState), ReadOnly);
713 dim->Set(String::New("sleep"), FunctionTemplate::New(WrapSleep), ReadOnly);
714 dim->Set(String::New("open"), FunctionTemplate::New(WrapOpen), ReadOnly);
715 dim->Set(String::New("database"), FunctionTemplate::New(WrapDatabase), ReadOnly);
716
717 Handle<ObjectTemplate> global = ObjectTemplate::New();
718 global->Set(String::New("dim"), dim, ReadOnly);
719 global->Set(String::New("include"), FunctionTemplate::New(WrapInclude), ReadOnly);
720 global->Set(String::New("exit"), FunctionTemplate::New(WrapExit), ReadOnly);
721 global->Set(String::New("version"), FunctionTemplate::New(InterpreterV8::FuncVersion), ReadOnly);
722
723 // Persistent
724 Persistent<Context> context = Context::New(NULL, global);
725 if (context.IsEmpty())
726 {
727 //printf("Error creating context\n");
728 return false;
729 }
730
731 v8::Context::Scope scope(context);
732
733 Local<Array> args = Array::New(map.size());
734 for (auto it=map.begin(); it!=map.end(); it++)
735 args->Set(String::New(it->first.c_str()), String::New(it->second.c_str()));
736 context->Global()->Set(String::New("$"), args, ReadOnly);
737 context->Global()->Set(String::New("arg"), args, ReadOnly);
738
739 JsStart(filename);
740
741 //context->Enter();
742 v8::Locker::StartPreemption(10);
743 const bool rc = ExecuteFile(filename);
744
745 // -----
746 // This is how an exit handler could look like, but there is no way to interrupt it
747 // -----
748 // Handle<Object> obj = Handle<Object>::Cast(context->Global()->Get(String::New("dim")));
749 // if (!obj.IsEmpty())
750 // {
751 // Handle<Value> onexit = obj->Get(String::New("onexit"));
752 // if (!onexit->IsUndefined())
753 // Handle<Function>::Cast(onexit)->NewInstance(0, NULL); // argc, argv
754 // // Handle<Object> result = Handle<Function>::Cast(onexit)->NewInstance(0, NULL); // argc, argv
755 // }
756
757 v8::Locker::StopPreemption();
758 //context->Exit();
759
760 context.Dispose();
761
762#ifdef HAVE_SQL
763 for (auto it=fDatabases.begin(); it!=fDatabases.end(); it++)
764 delete *it;
765 fDatabases.clear();
766#endif
767
768
769 JsEnd(filename);
770
771 return rc;
772}
773
774void InterpreterV8::JsStop()
775{
776 Locker locker;
777 //cout << "Terminate " << fThreadId << endl;
778 if (This->fThreadId>=0)
779 V8::TerminateExecution(This->fThreadId);
780 //cout << "Terminate " << fThreadId << endl;
781 Unlocker unlocker;
782}
783
784#endif
785
Note: See TracBrowser for help on using the repository browser.