Skip to content

Commit

Permalink
Place assertions for handles after init check
Browse files Browse the repository at this point in the history
This lets automated testing check that GLFW_NOT_INITIALIZED is emitted
for every public function.
  • Loading branch information
elmindreda committed Mar 28, 2024
1 parent 38ec7ab commit 68dcea0
Show file tree
Hide file tree
Showing 18 changed files with 226 additions and 180 deletions.
6 changes: 3 additions & 3 deletions src/cocoa_monitor.m
Expand Up @@ -628,9 +628,6 @@ void _glfwSetGammaRampCocoa(_GLFWmonitor* monitor, const GLFWgammaramp* ramp)

GLFWAPI CGDirectDisplayID glfwGetCocoaMonitor(GLFWmonitor* handle)
{
_GLFWmonitor* monitor = (_GLFWmonitor*) handle;
assert(monitor != NULL);

_GLFW_REQUIRE_INIT_OR_RETURN(kCGNullDirectDisplay);

if (_glfw.platform.platformID != GLFW_PLATFORM_COCOA)
Expand All @@ -639,6 +636,9 @@ GLFWAPI CGDirectDisplayID glfwGetCocoaMonitor(GLFWmonitor* handle)
return kCGNullDirectDisplay;
}

_GLFWmonitor* monitor = (_GLFWmonitor*) handle;
assert(monitor != NULL);

return monitor->ns.displayID;
}

Expand Down
12 changes: 6 additions & 6 deletions src/cocoa_window.m
Expand Up @@ -2041,9 +2041,6 @@ VkResult _glfwCreateWindowSurfaceCocoa(VkInstance instance,

GLFWAPI id glfwGetCocoaWindow(GLFWwindow* handle)
{
_GLFWwindow* window = (_GLFWwindow*) handle;
assert(window != NULL);

_GLFW_REQUIRE_INIT_OR_RETURN(nil);

if (_glfw.platform.platformID != GLFW_PLATFORM_COCOA)
Expand All @@ -2053,14 +2050,14 @@ GLFWAPI id glfwGetCocoaWindow(GLFWwindow* handle)
return nil;
}

_GLFWwindow* window = (_GLFWwindow*) handle;
assert(window != NULL);

return window->ns.object;
}

GLFWAPI id glfwGetCocoaView(GLFWwindow* handle)
{
_GLFWwindow* window = (_GLFWwindow*) handle;
assert(window != NULL);

_GLFW_REQUIRE_INIT_OR_RETURN(nil);

if (_glfw.platform.platformID != GLFW_PLATFORM_COCOA)
Expand All @@ -2070,6 +2067,9 @@ GLFWAPI id glfwGetCocoaView(GLFWwindow* handle)
return nil;
}

_GLFWwindow* window = (_GLFWwindow*) handle;
assert(window != NULL);

return window->ns.view;
}

Expand Down
8 changes: 4 additions & 4 deletions src/context.c
Expand Up @@ -615,11 +615,11 @@ GLFWbool _glfwStringInExtensionString(const char* string, const char* extensions

GLFWAPI void glfwMakeContextCurrent(GLFWwindow* handle)
{
_GLFW_REQUIRE_INIT();

_GLFWwindow* window = (_GLFWwindow*) handle;
_GLFWwindow* previous;

_GLFW_REQUIRE_INIT();

previous = _glfwPlatformGetTls(&_glfw.contextSlot);

if (window && window->context.client == GLFW_NO_API)
Expand Down Expand Up @@ -647,11 +647,11 @@ GLFWAPI GLFWwindow* glfwGetCurrentContext(void)

GLFWAPI void glfwSwapBuffers(GLFWwindow* handle)
{
_GLFW_REQUIRE_INIT();

_GLFWwindow* window = (_GLFWwindow*) handle;
assert(window != NULL);

_GLFW_REQUIRE_INIT();

if (window->context.client == GLFW_NO_API)
{
_glfwInputError(GLFW_NO_WINDOW_CONTEXT,
Expand Down
8 changes: 4 additions & 4 deletions src/egl_context.c
Expand Up @@ -908,11 +908,11 @@ GLFWAPI EGLDisplay glfwGetEGLDisplay(void)

GLFWAPI EGLContext glfwGetEGLContext(GLFWwindow* handle)
{
_GLFW_REQUIRE_INIT_OR_RETURN(EGL_NO_CONTEXT);

_GLFWwindow* window = (_GLFWwindow*) handle;
assert(window != NULL);

_GLFW_REQUIRE_INIT_OR_RETURN(EGL_NO_CONTEXT);

if (window->context.source != GLFW_EGL_CONTEXT_API)
{
_glfwInputError(GLFW_NO_WINDOW_CONTEXT, NULL);
Expand All @@ -924,11 +924,11 @@ GLFWAPI EGLContext glfwGetEGLContext(GLFWwindow* handle)

GLFWAPI EGLSurface glfwGetEGLSurface(GLFWwindow* handle)
{
_GLFW_REQUIRE_INIT_OR_RETURN(EGL_NO_SURFACE);

_GLFWwindow* window = (_GLFWwindow*) handle;
assert(window != NULL);

_GLFW_REQUIRE_INIT_OR_RETURN(EGL_NO_SURFACE);

if (window->context.source != GLFW_EGL_CONTEXT_API)
{
_glfwInputError(GLFW_NO_WINDOW_CONTEXT, NULL);
Expand Down
12 changes: 6 additions & 6 deletions src/glx_context.c
Expand Up @@ -677,9 +677,6 @@ GLFWbool _glfwChooseVisualGLX(const _GLFWwndconfig* wndconfig,

GLFWAPI GLXContext glfwGetGLXContext(GLFWwindow* handle)
{
_GLFWwindow* window = (_GLFWwindow*) handle;
assert(window != NULL);

_GLFW_REQUIRE_INIT_OR_RETURN(NULL);

if (_glfw.platform.platformID != GLFW_PLATFORM_X11)
Expand All @@ -688,6 +685,9 @@ GLFWAPI GLXContext glfwGetGLXContext(GLFWwindow* handle)
return NULL;
}

_GLFWwindow* window = (_GLFWwindow*) handle;
assert(window != NULL);

if (window->context.source != GLFW_NATIVE_CONTEXT_API)
{
_glfwInputError(GLFW_NO_WINDOW_CONTEXT, NULL);
Expand All @@ -699,9 +699,6 @@ GLFWAPI GLXContext glfwGetGLXContext(GLFWwindow* handle)

GLFWAPI GLXWindow glfwGetGLXWindow(GLFWwindow* handle)
{
_GLFWwindow* window = (_GLFWwindow*) handle;
assert(window != NULL);

_GLFW_REQUIRE_INIT_OR_RETURN(None);

if (_glfw.platform.platformID != GLFW_PLATFORM_X11)
Expand All @@ -710,6 +707,9 @@ GLFWAPI GLXWindow glfwGetGLXWindow(GLFWwindow* handle)
return None;
}

_GLFWwindow* window = (_GLFWwindow*) handle;
assert(window != NULL);

if (window->context.source != GLFW_NATIVE_CONTEXT_API)
{
_glfwInputError(GLFW_NO_WINDOW_CONTEXT, NULL);
Expand Down
58 changes: 33 additions & 25 deletions src/input.c
Expand Up @@ -561,11 +561,11 @@ void _glfwCenterCursorInContentArea(_GLFWwindow* window)

GLFWAPI int glfwGetInputMode(GLFWwindow* handle, int mode)
{
_GLFW_REQUIRE_INIT_OR_RETURN(0);

_GLFWwindow* window = (_GLFWwindow*) handle;
assert(window != NULL);

_GLFW_REQUIRE_INIT_OR_RETURN(0);

switch (mode)
{
case GLFW_CURSOR:
Expand All @@ -588,11 +588,11 @@ GLFWAPI int glfwGetInputMode(GLFWwindow* handle, int mode)

GLFWAPI void glfwSetInputMode(GLFWwindow* handle, int mode, int value)
{
_GLFW_REQUIRE_INIT();

_GLFWwindow* window = (_GLFWwindow*) handle;
assert(window != NULL);

_GLFW_REQUIRE_INIT();

switch (mode)
{
case GLFW_CURSOR:
Expand Down Expand Up @@ -744,11 +744,11 @@ GLFWAPI int glfwGetKeyScancode(int key)

GLFWAPI int glfwGetKey(GLFWwindow* handle, int key)
{
_GLFW_REQUIRE_INIT_OR_RETURN(GLFW_RELEASE);

_GLFWwindow* window = (_GLFWwindow*) handle;
assert(window != NULL);

_GLFW_REQUIRE_INIT_OR_RETURN(GLFW_RELEASE);

if (key < GLFW_KEY_SPACE || key > GLFW_KEY_LAST)
{
_glfwInputError(GLFW_INVALID_ENUM, "Invalid key %i", key);
Expand All @@ -767,11 +767,11 @@ GLFWAPI int glfwGetKey(GLFWwindow* handle, int key)

GLFWAPI int glfwGetMouseButton(GLFWwindow* handle, int button)
{
_GLFW_REQUIRE_INIT_OR_RETURN(GLFW_RELEASE);

_GLFWwindow* window = (_GLFWwindow*) handle;
assert(window != NULL);

_GLFW_REQUIRE_INIT_OR_RETURN(GLFW_RELEASE);

if (button < GLFW_MOUSE_BUTTON_1 || button > GLFW_MOUSE_BUTTON_LAST)
{
_glfwInputError(GLFW_INVALID_ENUM, "Invalid mouse button %i", button);
Expand All @@ -790,16 +790,16 @@ GLFWAPI int glfwGetMouseButton(GLFWwindow* handle, int button)

GLFWAPI void glfwGetCursorPos(GLFWwindow* handle, double* xpos, double* ypos)
{
_GLFWwindow* window = (_GLFWwindow*) handle;
assert(window != NULL);

if (xpos)
*xpos = 0;
if (ypos)
*ypos = 0;

_GLFW_REQUIRE_INIT();

_GLFWwindow* window = (_GLFWwindow*) handle;
assert(window != NULL);

if (window->cursorMode == GLFW_CURSOR_DISABLED)
{
if (xpos)
Expand All @@ -813,11 +813,11 @@ GLFWAPI void glfwGetCursorPos(GLFWwindow* handle, double* xpos, double* ypos)

GLFWAPI void glfwSetCursorPos(GLFWwindow* handle, double xpos, double ypos)
{
_GLFW_REQUIRE_INIT();

_GLFWwindow* window = (_GLFWwindow*) handle;
assert(window != NULL);

_GLFW_REQUIRE_INIT();

if (xpos != xpos || xpos < -DBL_MAX || xpos > DBL_MAX ||
ypos != ypos || ypos < -DBL_MAX || ypos > DBL_MAX)
{
Expand Down Expand Up @@ -907,10 +907,10 @@ GLFWAPI GLFWcursor* glfwCreateStandardCursor(int shape)

GLFWAPI void glfwDestroyCursor(GLFWcursor* handle)
{
_GLFWcursor* cursor = (_GLFWcursor*) handle;

_GLFW_REQUIRE_INIT();

_GLFWcursor* cursor = (_GLFWcursor*) handle;

if (cursor == NULL)
return;

Expand Down Expand Up @@ -942,97 +942,105 @@ GLFWAPI void glfwDestroyCursor(GLFWcursor* handle)

GLFWAPI void glfwSetCursor(GLFWwindow* windowHandle, GLFWcursor* cursorHandle)
{
_GLFW_REQUIRE_INIT();

_GLFWwindow* window = (_GLFWwindow*) windowHandle;
_GLFWcursor* cursor = (_GLFWcursor*) cursorHandle;
assert(window != NULL);

_GLFW_REQUIRE_INIT();

window->cursor = cursor;

_glfw.platform.setCursor(window, cursor);
}

GLFWAPI GLFWkeyfun glfwSetKeyCallback(GLFWwindow* handle, GLFWkeyfun cbfun)
{
_GLFW_REQUIRE_INIT_OR_RETURN(NULL);

_GLFWwindow* window = (_GLFWwindow*) handle;
assert(window != NULL);

_GLFW_REQUIRE_INIT_OR_RETURN(NULL);
_GLFW_SWAP(GLFWkeyfun, window->callbacks.key, cbfun);
return cbfun;
}

GLFWAPI GLFWcharfun glfwSetCharCallback(GLFWwindow* handle, GLFWcharfun cbfun)
{
_GLFW_REQUIRE_INIT_OR_RETURN(NULL);

_GLFWwindow* window = (_GLFWwindow*) handle;
assert(window != NULL);

_GLFW_REQUIRE_INIT_OR_RETURN(NULL);
_GLFW_SWAP(GLFWcharfun, window->callbacks.character, cbfun);
return cbfun;
}

GLFWAPI GLFWcharmodsfun glfwSetCharModsCallback(GLFWwindow* handle, GLFWcharmodsfun cbfun)
{
_GLFW_REQUIRE_INIT_OR_RETURN(NULL);

_GLFWwindow* window = (_GLFWwindow*) handle;
assert(window != NULL);

_GLFW_REQUIRE_INIT_OR_RETURN(NULL);
_GLFW_SWAP(GLFWcharmodsfun, window->callbacks.charmods, cbfun);
return cbfun;
}

GLFWAPI GLFWmousebuttonfun glfwSetMouseButtonCallback(GLFWwindow* handle,
GLFWmousebuttonfun cbfun)
{
_GLFW_REQUIRE_INIT_OR_RETURN(NULL);

_GLFWwindow* window = (_GLFWwindow*) handle;
assert(window != NULL);

_GLFW_REQUIRE_INIT_OR_RETURN(NULL);
_GLFW_SWAP(GLFWmousebuttonfun, window->callbacks.mouseButton, cbfun);
return cbfun;
}

GLFWAPI GLFWcursorposfun glfwSetCursorPosCallback(GLFWwindow* handle,
GLFWcursorposfun cbfun)
{
_GLFW_REQUIRE_INIT_OR_RETURN(NULL);

_GLFWwindow* window = (_GLFWwindow*) handle;
assert(window != NULL);

_GLFW_REQUIRE_INIT_OR_RETURN(NULL);
_GLFW_SWAP(GLFWcursorposfun, window->callbacks.cursorPos, cbfun);
return cbfun;
}

GLFWAPI GLFWcursorenterfun glfwSetCursorEnterCallback(GLFWwindow* handle,
GLFWcursorenterfun cbfun)
{
_GLFW_REQUIRE_INIT_OR_RETURN(NULL);

_GLFWwindow* window = (_GLFWwindow*) handle;
assert(window != NULL);

_GLFW_REQUIRE_INIT_OR_RETURN(NULL);
_GLFW_SWAP(GLFWcursorenterfun, window->callbacks.cursorEnter, cbfun);
return cbfun;
}

GLFWAPI GLFWscrollfun glfwSetScrollCallback(GLFWwindow* handle,
GLFWscrollfun cbfun)
{
_GLFW_REQUIRE_INIT_OR_RETURN(NULL);

_GLFWwindow* window = (_GLFWwindow*) handle;
assert(window != NULL);

_GLFW_REQUIRE_INIT_OR_RETURN(NULL);
_GLFW_SWAP(GLFWscrollfun, window->callbacks.scroll, cbfun);
return cbfun;
}

GLFWAPI GLFWdropfun glfwSetDropCallback(GLFWwindow* handle, GLFWdropfun cbfun)
{
_GLFW_REQUIRE_INIT_OR_RETURN(NULL);

_GLFWwindow* window = (_GLFWwindow*) handle;
assert(window != NULL);

_GLFW_REQUIRE_INIT_OR_RETURN(NULL);
_GLFW_SWAP(GLFWdropfun, window->callbacks.drop, cbfun);
return cbfun;
}
Expand Down

0 comments on commit 68dcea0

Please sign in to comment.