| 31 | static v8::UniquePersistent<v8::Promise::Resolver> global_resolver; |
| 32 | |
| 33 | int main(int argc, char* argv[]) |
| 34 | { |
| 35 | // Initialize V8. |
| 36 | char Flags[] = "--expose-gc"; |
| 37 | v8::V8::SetFlagsFromString(Flags, sizeof(Flags)); |
| 38 | v8::V8::InitializeICUDefaultLocation(argv[0]); |
| 39 | v8::V8::InitializeExternalStartupData(argv[0]); |
| 40 | std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform(); |
| 41 | v8::V8::InitializePlatform(platform.get()); |
| 42 | v8::V8::Initialize(); |
| 43 | // Create a new Isolate and make it the current one. |
| 44 | v8::Isolate::CreateParams create_params; |
| 45 | create_params.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator(); |
| 46 | v8::Isolate* isolate = v8::Isolate::New(create_params); |
| 47 | { |
| 48 | v8::Isolate::Scope isolate_scope(isolate); |
| 49 | // Create a stack-allocated handle scope. |
| 50 | v8::HandleScope handle_scope(isolate); |
| 51 | // Create global template |
| 52 | v8::Local<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New(isolate); |
| 53 | { |
| 54 | |
| 55 | // bind user data template |
| 56 | static auto my_data_ctor_template = v8::FunctionTemplate::New( |
| 57 | isolate, +[](const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 58 | v8::Isolate* Isolate = info.GetIsolate(); |
| 59 | v8::Isolate::Scope IsolateScope(Isolate); |
| 60 | v8::HandleScope HandleScope(Isolate); |
| 61 | v8::Local<v8::Context> Context = Isolate->GetCurrentContext(); |
| 62 | v8::Context::Scope ContextScope(Context); |
| 63 | |
| 64 | if (info.IsConstructCall()) |
| 65 | { |
| 66 | auto self = info.This(); |
| 67 | auto param_content = info[0]; |
| 68 | auto param_times = info[1]; |
| 69 | auto new_data = new MyData(); |
| 70 | if (!param_content->IsUndefined()) |
| 71 | { |
| 72 | new_data->content = *v8::String::Utf8Value(info.GetIsolate(), param_content); |
| 73 | } |
| 74 | if (!param_times->IsUndefined()) |
| 75 | { |
| 76 | new_data->times = param_times->Uint32Value(Context).ToChecked(); |
| 77 | } |
| 78 | self->SetInternalField(0, v8::External::New(info.GetIsolate(), new_data)); |
| 79 | my_data_instances.try_emplace(new_data); |
| 80 | auto& ref = my_data_instances[new_data]; |
| 81 | ref.Reset(Isolate, self); |
| 82 | ref.SetWeak<MyData>( |
| 83 | new_data, |
| 84 | +[](const v8::WeakCallbackInfo<MyData>& data) { |
| 85 | auto ptr = data.GetParameter(); |
| 86 | printf("==========> call destruct <=========="); |
| 87 | delete ptr; |
| 88 | my_data_instances.erase(my_data_instances.find(ptr)); |
| 89 | }, |
| 90 | v8::WeakCallbackType::kInternalFields); |
nothing calls this directly
no test coverage detected