forked from Qard/quickjs-glfw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglfw.h
100 lines (80 loc) · 2.72 KB
/
glfw.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#ifndef GLFW_MODULE_MAIN
#define GLFW_MODULE_MAIN 1
// Include the Emscripten library only if targetting WebAssembly
#ifdef __EMSCRIPTEN__
#include <emscripten/emscripten.h>
#define GLFW_INCLUDE_ES3
#endif
#include <GLFW/glfw3.h>
#include <quickjs.h>
#include <cutils.h>
#include <stdio.h>
#include <stdlib.h>
#define countof(x) (sizeof(x) / sizeof((x)[0]))
#ifndef JS_SHARED_LIBRARY
#define js_init_module js_init_module_qjsc_glfw
#endif
#if defined(_WIN32) || defined(__MINGW32__)
#define VISIBLE __declspec(dllexport)
#define HIDDEN
#else
#define VISIBLE __attribute__((visibility("default")))
#define HIDDEN __attribute__((visibility("hidden")))
#endif
#ifdef _Thread_local
#define thread_local _Thread_local
#elif defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__SUNPRO_CC) || defined(__IBMCPP__)
#define thread_local __thread
#elif defined(_WIN32)
#define thread_local __declspec(thread)
#else
#error No TLS implementation found.
#endif
#define JS_CGETSET_ENUMERABLE_DEF(prop_name, fgetter, fsetter) \
{ \
.name = prop_name, .prop_flags = JS_PROP_ENUMERABLE | JS_PROP_CONFIGURABLE, .def_type = JS_DEF_CGETSET, .u = {.getset = {.get = {.getter = fgetter}, .set = {.setter = fsetter}} } \
}
#define JS_CGETSET_ENUMERABLE_MAGIC_DEF(prop_name, fgetter, fsetter, magic_num) \
{ \
.name = prop_name, .prop_flags = JS_PROP_ENUMERABLE | JS_PROP_CONFIGURABLE, .def_type = JS_DEF_CGETSET_MAGIC, .magic = magic_num, .u = { \
.getset = {.get = {.getter_magic = fgetter}, .set = {.setter_magic = fsetter}} \
} \
}
static inline JSAtom
js_iterator_atom(JSContext* ctx) {
JSValue global_obj = JS_GetGlobalObject(ctx);
JSValue symbol_constructor = JS_GetPropertyStr(ctx, global_obj, "Symbol");
JSValue symbol_iterator = JS_GetPropertyStr(ctx, symbol_constructor, "iterator");
JSAtom atom = JS_ValueToAtom(ctx, symbol_iterator);
JS_FreeValue(ctx, global_obj);
JS_FreeValue(ctx, symbol_constructor);
JS_FreeValue(ctx, symbol_iterator);
return atom;
}
static inline JSValue
js_newptr(JSContext* ctx, void* ptr) {
if(ptr != NULL) {
char buf[128];
snprintf(buf, sizeof(buf), "%p", ptr);
return JS_NewString(ctx, buf);
}
return JS_NULL;
}
static inline void*
js_getptr(JSContext* ctx, JSValueConst value) {
if(!JS_IsNull(value)) {
const char* str = JS_ToCString(ctx, value);
void* ptr = 0;
sscanf(str, "%p", &ptr);
return ptr;
}
return NULL;
}
#define GLFW_THROW() glfw_throw(ctx, __func__)
extern thread_local BOOL glfw_initialized;
VISIBLE JSModuleDef* js_init_module(JSContext* ctx, const char* module_name);
JSValue glfw_throw(JSContext* ctx, const char* func);
BOOL glfw_initialize(JSContext*);
int glfw_init(JSContext*, JSModuleDef*);
int glfw_export(JSContext*, JSModuleDef*);
#endif