Skip to content

Instantly share code, notes, and snippets.

@xaxxon
Last active February 4, 2016 06:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xaxxon/d45ba2bd11675735b4fa to your computer and use it in GitHub Desktop.
Save xaxxon/d45ba2bd11675735b4fa to your computer and use it in GitHub Desktop.
#include <iostream>
#include <fstream>
#include <stdio.h>
#define USE_BOOST
using namespace std;
#define SAMPLE_DEBUG true
#include "include/libplatform/libplatform.h"
#include "include/v8.h"
#include <exception>
void print_maybe_value(v8::MaybeLocal<v8::Value> maybe_value)
{
if (maybe_value.IsEmpty()) {
printf("Maybe value was empty\n");
} else {
auto local = maybe_value.ToLocalChecked();
v8::String::Utf8Value utf8(local);
printf("Maybe value: '%s'\n", *utf8);
}
}
static void exception_thrower(const v8::FunctionCallbackInfo<v8::Value>& args)
{
throw new std::exception();
}
class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
public:
inline virtual void* Allocate(size_t length) override {
void* data = AllocateUninitialized(length);
return data == NULL ? data : memset(data, 0, length);
}
inline virtual void* AllocateUninitialized(size_t length) override { return malloc(length); }
inline virtual void Free(void* data, size_t) override { free(data); }
};
int main(int argc, char* argv[])
{
// parse out any v8-specific command line flags
// Initialize V8.
v8::V8::InitializeICU();
#ifdef USE_SNAPSHOTS
v8::V8::InitializeExternalStartupData(argv[0]);
#endif
v8::Platform* platform = v8::platform::CreateDefaultPlatform();
v8::V8::InitializePlatform(platform);
v8::V8::Initialize();
// Create a new Isolate and make it the current one.
ArrayBufferAllocator allocator;
v8::Isolate::CreateParams create_params;
create_params.array_buffer_allocator = &allocator;
v8::Isolate* isolate = v8::Isolate::New(create_params);
{
v8::HandleScope hs(isolate);
v8::Isolate::Scope is(isolate);
v8::Local<v8::ObjectTemplate> global_templ = v8::ObjectTemplate::New(isolate);
auto ft = v8::FunctionTemplate::New(isolate, exception_thrower);
(void)global_templ->Set(isolate, "throw_exception", ft);
v8::Local<v8::Context> context = v8::Context::New(isolate, NULL, global_templ);
v8::Context::Scope cs(context);
v8::Local<v8::Script> script3 = v8::Script::Compile(context, v8::String::NewFromUtf8(isolate,"throw_exception();")).ToLocalChecked();
try{
v8::TryCatch c(isolate);
(void)script3->Run(context);
if(c.HasCaught()){
printf("C has caught\n");
} else {
printf("C has not caught\n");
}
} catch(...) {
printf("Caught in regular trycatch\n");
}
}
// Dispose the isolate and tear down V8.
isolate->Dispose();
v8::V8::Dispose();
v8::V8::ShutdownPlatform();
delete platform;
return 0;
}
clang++ -std=c++14 -I/users/xaxxon/v8 -std=c++14 -Wall -Werror samples/exception_sample.cpp -o samples/exception_sample -L/users/xaxxon/v8/out/x64.debug libv8toolkit.a -lv8_base -lv8_libbase -licudata -licuuc -licui18n -lv8_base -lv8_libplatform -lv8_nosnapshot
I'm running this on OS X 10.11.3 and using a fresh checkout of v8 from just a few days ago.. 4.10
(lldb) r
Process 30421 launched: '/Users/xaxxon/v8-class-wrapper/samples/exception_sample' (x86_64)
libc++abi.dylib: terminating with uncaught exception of type std::exception*
Process 30421 stopped
* thread #1: tid = 0x13f3bd, 0x00007fff86584002 libsystem_kernel.dylib`__pthread_kill + 10, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
frame #0: 0x00007fff86584002 libsystem_kernel.dylib`__pthread_kill + 10
libsystem_kernel.dylib`__pthread_kill:
-> 0x7fff86584002 <+10>: jae 0x7fff8658400c ; <+20>
0x7fff86584004 <+12>: movq %rax, %rdi
0x7fff86584007 <+15>: jmp 0x7fff8657ebdd ; cerror_nocancel
0x7fff8658400c <+20>: retq
(lldb) bt
* thread #1: tid = 0x13f3bd, 0x00007fff86584002 libsystem_kernel.dylib`__pthread_kill + 10, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
* frame #0: 0x00007fff86584002 libsystem_kernel.dylib`__pthread_kill + 10
frame #1: 0x00007fff86ee95c5 libsystem_pthread.dylib`pthread_kill + 90
frame #2: 0x00007fff971ef6e7 libsystem_c.dylib`abort + 129
frame #3: 0x00007fff9a2b2f81 libc++abi.dylib`abort_message + 257
frame #4: 0x00007fff9a2d8a47 libc++abi.dylib`default_terminate_handler() + 267
frame #5: 0x00007fff998336c3 libobjc.A.dylib`_objc_terminate() + 124
frame #6: 0x00007fff9a2d619e libc++abi.dylib`std::__terminate(void (*)()) + 8
frame #7: 0x00007fff9a2d5c12 libc++abi.dylib`__cxa_throw + 121
frame #8: 0x0000000100001f0f exception_sample`exception_thrower(v8::FunctionCallbackInfo<v8::Value> const&) + 127
frame #9: 0x000000010007c27c exception_sample`v8::internal::FunctionCallbackArguments::Call(this=0x00007fff5fbfeef8, f=(exception_sample`exception_thrower(v8::FunctionCallbackInfo<v8::Value> const&)))(v8::FunctionCallbackInfo<v8::Value> const&)) + 124 at arguments.cc:33
frame #10: 0x00000001000e6c19 exception_sample`v8::internal::MaybeHandle<v8::internal::Object> v8::internal::(anonymous namespace)::HandleApiCallHelper<false>(isolate=0x0000000103802000, args=BuiltinArguments<v8::internal::BuiltinExtraArguments::kTarget> @ 0x00007fff5fbff030)::BuiltinArguments<(v8::internal::BuiltinExtraArguments)1>) + 1449 at builtins.cc:3580
frame #11: 0x000000010010d3f6 exception_sample`v8::internal::Builtin_Impl_HandleApiCall(args=v8::internal::(anonymous namespace)::HandleApiCallArgumentsType @ 0x00007fff5fbff098, isolate=0x0000000103802000)::BuiltinArguments<(v8::internal::BuiltinExtraArguments)1>, v8::internal::Isolate*) + 86 at builtins.cc:3604
frame #12: 0x00000001000e86b7 exception_sample`v8::internal::Builtin_HandleApiCall(args_length=2, args_object=0x00007fff5fbff138, isolate=0x0000000103802000) + 71 at builtins.cc:3601
frame #13: 0x000034be3e9060bb
frame #14: 0x000034be3e967dfa
frame #15: 0x000034be3e91e59b
frame #16: 0x000034be3e919602
frame #17: 0x00000001005bb3bf exception_sample`v8::internal::(anonymous namespace)::Invoke(isolate=0x0000000103802000, is_construct=false, target=Handle<v8::internal::Object> @ 0x00007fff5fbff300, receiver=Handle<v8::internal::Object> @ 0x00007fff5fbff2f8, argc=0, args=0x0000000000000000, new_target=Handle<v8::internal::Object> @ 0x00007fff5fbff2f0) + 607 at execution.cc:97
frame #18: 0x00000001005bb14e exception_sample`v8::internal::Execution::Call(isolate=0x0000000103802000, callable=Handle<v8::internal::Object> @ 0x00007fff5fbff458, receiver=Handle<v8::internal::Object> @ 0x00007fff5fbff450, argc=0, argv=0x0000000000000000) + 1294 at execution.cc:163
frame #19: 0x00000001000094ce exception_sample`v8::Script::Run(this=0x0000000103839e58, context=(val_ = 0x0000000103839e18)) + 494 at api.cc:1713
frame #20: 0x0000000100001bc8 exception_sample`main + 1560
frame #21: 0x00007fff90e5b5ad libdyld.dylib`start + 1
(lldb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment