#include #undef NAPI_VERSION #include #include namespace { napi_value CreateBuffer(napi_env env, napi_callback_info info) { v8::Isolate* isolate = v8::Isolate::TryGetCurrent(); if (isolate == nullptr) { return NULL; } const size_t length = 4; uint8_t* data = new uint8_t[length]; for (size_t i = 0; i < 4; i++) { data[i] = static_cast(length); } auto finalizer = [](char* data, void* hint) { delete[] static_cast(reinterpret_cast(data)); }; // NOTE: Buffer API is invoked directly rather than // napi version to trigger the FATAL error from V8. v8::MaybeLocal maybe = node::Buffer::New( isolate, static_cast(reinterpret_cast(data)), length, finalizer, nullptr); return reinterpret_cast(*maybe.ToLocalChecked()); } napi_value Init(napi_env env, napi_value exports) { napi_status status; napi_property_descriptor descriptors[] = {{"createBuffer", NULL, CreateBuffer, NULL, NULL, NULL, napi_default, NULL}}; status = napi_define_properties( env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors); if (status != napi_ok) return NULL; return exports; } } // namespace NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)