1
使用TerminateExecution的我與此刻的V8試驗。我希望能夠在一個線程中運行一些(可能是長期運行的)javascript,然後能夠從另一個線程隨意「優雅地」終止執行。在V8
我已經寫了這個簡單的代碼片段,測試儲物櫃的概念和TerminateExecution的用法:
void breaker(Isolate* isolate, int tid) {
getchar(); //wait for keyboard input on stdin
std::cout << "Breaking V8 execution" << std::endl;
v8::Locker locker(isolate); //lock the isolate
v8::V8::TerminateExecution(tid); //and terminate it
}
int main(int argc, char **argv) {
if(argc != 2) {
std::cout << "No script name given" << std::endl;
return 1;
}
Isolate* isolate = Isolate::New(); //create a new isolate
Isolate::Scope isolateScope(isolate); //enter it
v8::Locker locker(isolate); //lock the isolate
v8::Locker::StartPreemption(100); //and start preemption
v8::HandleScope handle_scope(isolate); //open a new handle scope
/*** inject a console object into the global context ***/
v8::Handle<v8::ObjectTemplate> globalTemplate = v8::ObjectTemplate::New();
Handle<Context> context = Context::New(NULL, globalTemplate);
v8::Context::Scope context_scope(context);
Console* console = new Console;
Handle<Object> jsConsole = wrap_console(isolate, console);
expose_property(jsConsole, String::New("log"), InvocationCallback(Console::consoleLog));
context->Global()->Set(String::New("console"), jsConsole, v8::ReadOnly);
/*******************************************************/
//read a javascript file supplied via console
std::string contents = read_file(argv[1]);
v8::Handle<v8::String> js = v8::String::New(contents.c_str());
v8::TryCatch try_catch;
v8::Handle<v8::Script> script = v8::Script::Compile(js);
if(script.IsEmpty()) {
report_exception(try_catch);
}
//start the breaker thread
std::thread th(breaker, isolate, v8::V8::GetCurrentThreadId());
log_info("running script");
script->Run();
log_info("Script execution finished");
th.join();
}
但是,我總是在terminateExecution()
呼叫段錯誤。我在這裏做錯了什麼?
感謝您的幫助
太棒了,那就是訣竅!太糟糕了,沒有任何實際的文檔可以告訴你這些東西... – DeX3
這就是爲什麼。現在有:) – xaxxon