Skip to content

Instantly share code, notes, and snippets.

@apaprocki
Created October 12, 2012 15:17
Show Gist options
  • Save apaprocki/3879707 to your computer and use it in GitHub Desktop.
Save apaprocki/3879707 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
@mraleph
Copy link

mraleph commented Oct 12, 2012

I don't think you can overwrite exception with your own or stop unwinding the stack while there are still JS frames alive... at least this does not seem like an intended variant of usage for this API at the moment.

You should file a bug against V8 and describe your pattern to V8 team so that they might decide to extend functionality to accomodate it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment