test: vendor node-is-valid-window (#39965)

This commit is contained in:
David Sanders 2023-09-25 03:43:57 -07:00 committed by GitHub
parent fdf1ecec47
commit 18f517d8a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 181 additions and 14 deletions

View file

@ -0,0 +1,12 @@
#ifndef SRC_IMPL_H_
#define SRC_IMPL_H_
#include <cstddef>
namespace impl {
bool IsValidWindow(char* handle, size_t size);
} // namespace impl
#endif // SRC_IMPL_H_

View file

@ -0,0 +1,14 @@
#include "impl.h"
#include <Cocoa/Cocoa.h>
namespace impl {
bool IsValidWindow(char* handle, size_t size) {
if (size != sizeof(NSView*))
return false;
NSView* view = *reinterpret_cast<NSView**>(handle);
return [view isKindOfClass:[NSView class]];
}
} // namespace impl

View file

@ -0,0 +1,9 @@
#include "impl.h"
namespace impl {
bool IsValidWindow(char* handle, size_t size) {
return true;
}
} // namespace impl

View file

@ -0,0 +1,14 @@
#include "impl.h"
#include <windows.h>
namespace impl {
bool IsValidWindow(char* handle, size_t size) {
if (size != sizeof(HWND))
return false;
HWND window = *reinterpret_cast<HWND*>(handle);
return ::IsWindow(window);
}
} // namespace impl

View file

@ -0,0 +1,56 @@
#include <js_native_api.h>
#include <node_api.h>
#include "impl.h"
namespace {
napi_value IsValidWindow(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value args[1], result;
napi_status status;
status = napi_get_cb_info(env, info, &argc, args, NULL, NULL);
if (status != napi_ok)
return NULL;
bool is_buffer;
status = napi_is_buffer(env, args[0], &is_buffer);
if (status != napi_ok)
return NULL;
if (!is_buffer) {
napi_throw_error(env, NULL, "First argument must be Buffer");
return NULL;
}
char* data;
size_t length;
status = napi_get_buffer_info(env, args[0], (void**)&data, &length);
if (status != napi_ok)
return NULL;
status = napi_get_boolean(env, impl::IsValidWindow(data, length), &result);
if (status != napi_ok)
return NULL;
return result;
}
napi_value Init(napi_env env, napi_value exports) {
napi_status status;
napi_property_descriptor descriptors[] = {{"isValidWindow", NULL,
IsValidWindow, 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)