Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DobrinGanev/7f36b604f7f0ff80427557299e28e91d to your computer and use it in GitHub Desktop.
Save DobrinGanev/7f36b604f7f0ff80427557299e28e91d to your computer and use it in GitHub Desktop.
V8::TerminateException with TryCatch
#include <pthread.h>
#include <unistd.h>
#include <iostream>
#include <v8.h>
using namespace v8;
static void *run(void *ptr) {
// Terminate the execution on the main thread in one second.
sleep(1);
V8::TerminateExecution();
pthread_exit(0);
}
int main(void) {
pthread_t thread;
HandleScope scope;
Persistent<Context> context = Context::New();
Context::Scope contextscope(context);
Local<String> source = String::New("while(true) {}");
Local<Script> script = Script::Compile(source);
// Catch any exceptions, kick off thread, and execute infinite loop.
TryCatch trycatch;
pthread_create(&thread, 0, &run, 0);
Local<Value> result = script->Run();
// Result will be that execution is *not* terminating and an
// exception has been caught.
std::cout << "IsExecutionTerminating "
<< V8::IsExecutionTerminating() << std::endl;
std::cout << "TryCatch.HasCaught() "
<< trycatch.HasCaught() << std::endl;
if (trycatch.HasCaught()) {
// Inspection of the exception is that it is simply null.
// There is no real way to tell that execution terminated,
// unless you implicitly check for null and empty message/stack??
std::cout << "TryCatch.Exception->IsNull() " <<
trycatch.Exception()->IsNull() << std::endl;
std::cout << "TryCatch.Message.IsEmpty() " <<
trycatch.Message().IsEmpty() << std::endl;
std::cout << "TryCatch.StackTrace.IsEmpty() " <<
trycatch.StackTrace().IsEmpty() << std::endl;
}
context.Dispose();
pthread_join(thread, 0);
return 0;
}
// Output:
// IsExecutionTerminating 0
// TryCatch.HasCaught() 1
// TryCatch.Exception->IsNull() 1
// TryCatch.Message.IsEmpty() 1
// TryCatch.StackTrace.IsEmpty() 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment