<commit_before>#include <dlfcn.h>
#include <GL/gl.h>
#include <inttypes.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "khash.h"
#include "types.h"

#ifdef __linux__
#include <linux/limits.h>
#endif

#ifndef PATH_MAX
#define PATH_MAX 254
#endif

#ifdef __ARM_NEON__
#include <arm_neon.h>
#endif

#ifndef GL_H
#define GL_H

#include "../config.h"
#include "wrap/es.h"

#define checkError(code)                          \
    {int error; while ((error = glGetError())) {} \
    code                                          \
    if ((error = glGetError()))                   \
        printf(#code " -> %i\n", error);}

#define printError(file, line)              \
    {int error; if ((error = glGetError())) \
        printf(file ":%i -> %i\n", line, error);}

#define GLdouble double

// will become a reference to dlopen'd gles
extern void *gles;

static const char *gles_ext[] = {
    "so",
    "dylib",
    "dll",
    NULL,
};

static const char *gles_lib[] = {
#ifdef USE_ES2
    "libGLESv2_CM",
    "libGLESv2",
#else
    "libGLESv1_CM",
    "libGLES_CM",
#endif // USE_ES2
    NULL
};

#ifndef MIN
#define MIN(a, b) (((a) < (b) ? (a) : (b)))
#endif

#ifndef MAX
#define MAX(a, b) (((a) > (b) ? (a) : (b)))
#endif

#define WARN_NULL(name) if (name == NULL) printf("libGL: warning, " #name " is NULL\n");

static void load_gles_lib() {
    if (gles) {
        return;
    }
    char gles_name[PATH_MAX + 1];
    char *override = getenv("LIBGL_GLES");
    int flags = RTLD_LOCAL | RTLD_NOW;
#ifdef RTLD_DEEPBIND
    flags |= RTLD_DEEPBIND;
#endif
    if (override) {
        if ((gles = dlopen(override, flags))) {
            strncpy(gles_name, override, PATH_MAX);
            printf("libGL backend: %s\n", gles_name);
            return;
        }
    }
    for (int i = 0; gles_lib[i]; i++) {
        for (int e = 0; gles_ext[e]; e++) {
            snprintf(gles_name, PATH_MAX, "%s.%s", gles_lib[i], gles_ext[e]);
            gles = dlopen(gles_name, flags);
            if (gles) {
                printf("libGL backend: %s\n", gles_name);
                return;
            }
        }
    }
    WARN_NULL(gles);
}

#define LOAD_GLES(name)                                   \
    static name##_PTR gles_##name;                        \
    {                                                     \
        static bool first = true;                         \
        if (first) {                                      \
            first = false;                                \
            if (gles == NULL) {                           \
                load_gles_lib();                          \
            }                                             \
            gles_##name = (name##_PTR)dlsym(gles, #name); \
            WARN_NULL(gles_##name);                       \
        }                                                 \
    }

#define PUSH_IF_COMPILING_EXT(name, ...)             \
    if (state.list.active) {                         \
        push_##name(__VA_ARGS__);                    \
        return (name##_RETURN)0;                     \
    }

#define PUSH_IF_COMPILING(name) PUSH_IF_COMPILING_EXT(name, name##_ARG_NAMES)

#define PROXY_GLES(name) \
    LOAD_GLES(name); \
    if (gles_##name != NULL) { \
        return gles_##name(name##_ARG_NAMES); \
    }

#define CURRENT (state.list.active ? &state.list.current : &state.current)

#include "gl_helpers.h"

#include "wrap/stub.h"
#include "wrap/gl.h"
#include "block.h"
#include "eval.h"
#include "light.h"
#include "line.h"
#include "list.h"
#include "pixel.h"
#include "raster.h"
#include "stack.h"
#include "texgen.h"
#include "texture.h"
#include "array.h"

#include "state.h"
extern glstate_t state;<commit_msg>improve defines in gl.h for test mocking<commit_after>#include <dlfcn.h>
#include <GL/gl.h>
#include <inttypes.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "khash.h"
#include "types.h"

#ifdef __linux__
#include <linux/limits.h>
#endif

#ifndef PATH_MAX
#define PATH_MAX 254
#endif

#ifdef __ARM_NEON__
#include <arm_neon.h>
#endif

#ifndef GL_H
#define GL_H

#include "../config.h"
#include "wrap/es.h"

#define CHECK_ERROR(code)                         \
    {int error; while ((error = glGetError())) {} \
    code                                          \
    if ((error = glGetError()))                   \
        printf(#code " -> %i\n", error);}

#define PRINT_ERROR(file, line)             \
    {int error; if ((error = glGetError())) \
        printf(file ":%i -> %i\n", line, error);}

#define GLdouble double

// will become a reference to dlopen'd gles
extern void *gles;

static const char *gles_ext[] = {
    "so",
    "dylib",
    "dll",
    NULL,
};

static const char *gles_lib[] = {
#ifdef USE_ES2
    "libGLESv2_CM",
    "libGLESv2",
#else
    "libGLESv1_CM",
    "libGLES_CM",
#endif // USE_ES2
    NULL
};

#ifndef MIN
#define MIN(a, b) (((a) < (b) ? (a) : (b)))
#endif

#ifndef MAX
#define MAX(a, b) (((a) > (b) ? (a) : (b)))
#endif

#ifndef WARN_NULL
#define WARN_NULL(name) if (name == NULL) printf("libGL: warning, " #name " is NULL\n");
#endif

static void load_gles_lib() {
    if (gles) {
        return;
    }
    char gles_name[PATH_MAX + 1];
    char *override = getenv("LIBGL_GLES");
    int flags = RTLD_LOCAL | RTLD_NOW;
#ifdef RTLD_DEEPBIND
    flags |= RTLD_DEEPBIND;
#endif
    if (override) {
        if ((gles = dlopen(override, flags))) {
            strncpy(gles_name, override, PATH_MAX);
            printf("libGL backend: %s\n", gles_name);
            return;
        }
    }
    for (int i = 0; gles_lib[i]; i++) {
        for (int e = 0; gles_ext[e]; e++) {
            snprintf(gles_name, PATH_MAX, "%s.%s", gles_lib[i], gles_ext[e]);
            gles = dlopen(gles_name, flags);
            if (gles) {
                printf("libGL backend: %s\n", gles_name);
                return;
            }
        }
    }
    WARN_NULL(gles);
}

#ifndef LOAD_GLES
#define LOAD_GLES(name)                                   \
    static name##_PTR gles_##name;                        \
    {                                                     \
        static bool first = true;                         \
        if (first) {                                      \
            first = false;                                \
            if (gles == NULL) {                           \
                load_gles_lib();                          \
            }                                             \
            gles_##name = (name##_PTR)dlsym(gles, #name); \
            WARN_NULL(gles_##name);                       \
        }                                                 \
    }
#endif

#ifndef PUSH_IF_COMPILING_EXT
#define PUSH_IF_COMPILING_EXT(name, ...)             \
    if (state.list.active) {                         \
        push_##name(__VA_ARGS__);                    \
        return (name##_RETURN)0;                     \
    }
#endif

#ifndef PUSH_IF_COMPILING
#define PUSH_IF_COMPILING(name) PUSH_IF_COMPILING_EXT(name, name##_ARG_NAMES)
#endif

#ifndef PROXY_GLES
#define PROXY_GLES(name) \
    LOAD_GLES(name); \
    if (gles_##name != NULL) { \
        return gles_##name(name##_ARG_NAMES); \
    }
#endif

#define CURRENT (state.list.active ? &state.list.current : &state.current)

#include "gl_helpers.h"

#include "wrap/stub.h"
#include "wrap/gl.h"
#include "block.h"
#include "eval.h"
#include "light.h"
#include "line.h"
#include "list.h"
#include "pixel.h"
#include "raster.h"
#include "stack.h"
#include "texgen.h"
#include "texture.h"
#include "array.h"

#include "state.h"
extern glstate_t state;
{'fraction_non_alphanumeric': 0.14518844709234555, 'fraction_numerical': 0.0031084056469369253, 'mean_word_length': 1.6655160510873317, 'pattern_counts': {'":': 2, '<': 25, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4170692', 'n_tokens_mistral': 2703, 'n_tokens_neox': 2653, 'n_words': 574}