Skip to content

Commit

Permalink
Format all c++ files with clang-format.
Browse files Browse the repository at this point in the history
  • Loading branch information
null77 authored and dhritzkiv committed Jan 13, 2025
1 parent ee42968 commit 22ffabe
Show file tree
Hide file tree
Showing 6 changed files with 774 additions and 984 deletions.
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ColumnLimit: 100
83 changes: 37 additions & 46 deletions src/native/SharedLibrary.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <string>
#include <array>
#include <string>

#ifdef _WIN32
#include <windows.h>
Expand All @@ -8,62 +8,53 @@
#endif

#ifdef _WIN32
std::string Narrow(const std::wstring_view &utf16)
{
if (utf16.empty())
{
return {};
}
int requiredSize = WideCharToMultiByte(CP_UTF8, 0, utf16.data(), static_cast<int>(utf16.size()),
nullptr, 0, nullptr, nullptr);
std::string utf8(requiredSize, '\0');
WideCharToMultiByte(CP_UTF8, 0, utf16.data(), static_cast<int>(utf16.size()), &utf8[0],
requiredSize, nullptr, nullptr);
return utf8;
std::string Narrow(const std::wstring_view &utf16) {
if (utf16.empty()) {
return {};
}
int requiredSize = WideCharToMultiByte(CP_UTF8, 0, utf16.data(), static_cast<int>(utf16.size()),
nullptr, 0, nullptr, nullptr);
std::string utf8(requiredSize, '\0');
WideCharToMultiByte(CP_UTF8, 0, utf16.data(), static_cast<int>(utf16.size()), &utf8[0],
requiredSize, nullptr, nullptr);
return utf8;
}

std::string GetPath(HMODULE module)
{
std::array<wchar_t, MAX_PATH> executableFileBuf;
DWORD executablePathLen = GetModuleFileNameW(module, executableFileBuf.data(),
static_cast<DWORD>(executableFileBuf.size()));
return Narrow(executablePathLen > 0 ? executableFileBuf.data() : L"");
std::string GetPath(HMODULE module) {
std::array<wchar_t, MAX_PATH> executableFileBuf;
DWORD executablePathLen = GetModuleFileNameW(module, executableFileBuf.data(),
static_cast<DWORD>(executableFileBuf.size()));
return Narrow(executablePathLen > 0 ? executableFileBuf.data() : L"");
}

std::string GetModulePath(void *moduleOrSymbol)
{
HMODULE module = nullptr;
if (GetModuleHandleExW(
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
reinterpret_cast<LPCWSTR>(moduleOrSymbol), &module))
{
return GetPath(module);
}
std::string GetModulePath(void *moduleOrSymbol) {
HMODULE module = nullptr;
if (GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
reinterpret_cast<LPCWSTR>(moduleOrSymbol), &module)) {
return GetPath(module);
}

return "";
return "";
}
#else
std::string GetModulePath(void *moduleOrSymbol)
{
Dl_info dlInfo;
if (dladdr(moduleOrSymbol, &dlInfo) == 0)
{
return "";
}
std::string GetModulePath(void *moduleOrSymbol) {
Dl_info dlInfo;
if (dladdr(moduleOrSymbol, &dlInfo) == 0) {
return "";
}

return dlInfo.dli_fname;
return dlInfo.dli_fname;
}
#endif

std::string StripFilenameFromPath(const std::string &path)
{
size_t lastPathSepLoc = path.find_last_of("\\/");
return (lastPathSepLoc != std::string::npos) ? path.substr(0, lastPathSepLoc) : "";
std::string StripFilenameFromPath(const std::string &path) {
size_t lastPathSepLoc = path.find_last_of("\\/");
return (lastPathSepLoc != std::string::npos) ? path.substr(0, lastPathSepLoc) : "";
}

std::string GetModuleDirectory()
{
static int placeholderSymbol = 0;
std::string moduleName = GetModulePath(&placeholderSymbol);
return StripFilenameFromPath(moduleName);
std::string GetModuleDirectory() {
static int placeholderSymbol = 0;
std::string moduleName = GetModulePath(&placeholderSymbol);
return StripFilenameFromPath(moduleName);
}
45 changes: 23 additions & 22 deletions src/native/SharedLibrary.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@

inline std::string GetSharedLibraryExtension() {
#ifdef _WIN32
return ".dll"; // Windows
return ".dll"; // Windows
#elif __APPLE__
return ".dylib"; // OSX
return ".dylib"; // OSX
#elif __linux__
return ".so"; // Linux
return ".so"; // Linux
#else
#error Unsupported platform
#endif
Expand All @@ -26,38 +26,39 @@ std::string GetModuleDirectory();

class SharedLibrary {
public:
SharedLibrary() {}
SharedLibrary() {}

bool open(const std::string& libraryPath) {
const std::string libraryPathWithExt = GetModuleDirectory() + "/" + libraryPath + GetSharedLibraryExtension();
bool open(const std::string &libraryPath) {
const std::string libraryPathWithExt =
GetModuleDirectory() + "/" + libraryPath + GetSharedLibraryExtension();
#ifdef _WIN32
handle = LoadLibraryA(libraryPathWithExt.c_str());
handle = LoadLibraryA(libraryPathWithExt.c_str());
#else
handle = dlopen(libraryPathWithExt.c_str(), RTLD_LAZY);
handle = dlopen(libraryPathWithExt.c_str(), RTLD_LAZY);
#endif
return handle != nullptr;
}
return handle != nullptr;
}

~SharedLibrary() {
if (handle) {
~SharedLibrary() {
if (handle) {
#ifdef _WIN32
FreeLibrary(static_cast<HMODULE>(handle));
FreeLibrary(static_cast<HMODULE>(handle));
#else
dlclose(handle);
dlclose(handle);
#endif
}
}
}

template <typename Func>
Func getFunction(const std::string& functionName) {
template <typename Func> Func getFunction(const std::string &functionName) {
#ifdef _WIN32
auto func = reinterpret_cast<Func>(GetProcAddress(static_cast<HMODULE>(handle), functionName.c_str()));
auto func =
reinterpret_cast<Func>(GetProcAddress(static_cast<HMODULE>(handle), functionName.c_str()));
#else
auto func = reinterpret_cast<Func>(dlsym(handle, functionName.c_str()));
auto func = reinterpret_cast<Func>(dlsym(handle, functionName.c_str()));
#endif
return func;
}
return func;
}

private:
void* handle = nullptr;
void *handle = nullptr;
};
51 changes: 17 additions & 34 deletions src/native/bindings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,25 @@
* Author: ngk437
*/

#include <cstdlib>
#include "webgl.h"
#include <cstdlib>

Nan::Persistent<v8::FunctionTemplate> WEBGL_TEMPLATE;

#define JS_GL_METHOD(webgl_name, method_name) \
Nan::SetPrototypeTemplate(\
webgl_template \
, webgl_name\
, Nan::New<v8::FunctionTemplate>(\
WebGLRenderingContext:: method_name))
#define JS_GL_METHOD(webgl_name, method_name) \
Nan::SetPrototypeTemplate(webgl_template, webgl_name, \
Nan::New<v8::FunctionTemplate>(WebGLRenderingContext::method_name))

#define JS_CONSTANT(x, v) \
Nan::SetPrototypeTemplate( \
webgl_template \
, #x \
, Nan::New<v8::Integer>(v))
#define JS_CONSTANT(x, v) Nan::SetPrototypeTemplate(webgl_template, #x, Nan::New<v8::Integer>(v))

#define JS_GL_CONSTANT(name) JS_CONSTANT(name, GL_ ## name)
#define JS_GL_CONSTANT(name) JS_CONSTANT(name, GL_##name)

NAN_MODULE_INIT(Init) {
v8::Local<v8::FunctionTemplate> webgl_template =
Nan::New<v8::FunctionTemplate>(WebGLRenderingContext::New);
Nan::New<v8::FunctionTemplate>(WebGLRenderingContext::New);

webgl_template->InstanceTemplate()->SetInternalFieldCount(1);
webgl_template->SetClassName(
Nan::New<v8::String>("WebGLRenderingContext").ToLocalChecked());
webgl_template->SetClassName(Nan::New<v8::String>("WebGLRenderingContext").ToLocalChecked());

/* WebGL methods */
JS_GL_METHOD("_drawArraysInstancedANGLE", DrawArraysInstancedANGLE);
Expand Down Expand Up @@ -167,25 +159,18 @@ NAN_MODULE_INIT(Init) {
JS_GL_METHOD("bindVertexArrayOES", BindVertexArrayOES);

// Windows defines a macro called NO_ERROR which messes this up
Nan::SetPrototypeTemplate(
webgl_template,
"NO_ERROR",
Nan::New<v8::Integer>(GL_NO_ERROR));
Nan::SetPrototypeTemplate(webgl_template, "NO_ERROR", Nan::New<v8::Integer>(GL_NO_ERROR));
JS_GL_CONSTANT(INVALID_ENUM);
JS_GL_CONSTANT(INVALID_VALUE);
JS_GL_CONSTANT(INVALID_OPERATION);
JS_GL_CONSTANT(OUT_OF_MEMORY);

// OpenGL ES 2.1 constants
Nan::SetPrototypeTemplate(
webgl_template
, "DEPTH_STENCIL"
, Nan::New<v8::Integer>(GL_DEPTH_STENCIL_OES));
Nan::SetPrototypeTemplate(webgl_template, "DEPTH_STENCIL",
Nan::New<v8::Integer>(GL_DEPTH_STENCIL_OES));

Nan::SetPrototypeTemplate(
webgl_template
, "DEPTH_STENCIL_ATTACHMENT"
, Nan::New<v8::Integer>(0x821A));
Nan::SetPrototypeTemplate(webgl_template, "DEPTH_STENCIL_ATTACHMENT",
Nan::New<v8::Integer>(0x821A));

JS_GL_CONSTANT(MAX_VERTEX_UNIFORM_VECTORS);
JS_GL_CONSTANT(MAX_VARYING_VECTORS);
Expand Down Expand Up @@ -480,14 +465,12 @@ NAN_MODULE_INIT(Init) {
JS_CONSTANT(IMPLEMENTATION_COLOR_READ_TYPE, 0x8B9A);
JS_CONSTANT(IMPLEMENTATION_COLOR_READ_FORMAT, 0x8B9B);

//Export template
// Export template
WEBGL_TEMPLATE.Reset(webgl_template);
Nan::Set(
target
, Nan::New<v8::String>("WebGLRenderingContext").ToLocalChecked()
, Nan::GetFunction(webgl_template).ToLocalChecked());
Nan::Set(target, Nan::New<v8::String>("WebGLRenderingContext").ToLocalChecked(),
Nan::GetFunction(webgl_template).ToLocalChecked());

//Export helper methods for clean up and error handling
// Export helper methods for clean up and error handling
Nan::Export(target, "cleanup", WebGLRenderingContext::DisposeAll);
Nan::Export(target, "setError", WebGLRenderingContext::SetError);
}
Expand Down
Loading

0 comments on commit 22ffabe

Please sign in to comment.