-
Notifications
You must be signed in to change notification settings - Fork 2
/
workarea.c
124 lines (95 loc) · 3.75 KB
/
workarea.c
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "glfw.h"
#include "workarea.h"
// Constructor/Destructor
JSValue glfw_workarea_ctor(JSContext* ctx, JSValueConst new_target, int argc, JSValueConst* argv) {
GLFWWorkArea* workarea = js_mallocz(ctx, sizeof(*workarea));
if (!workarea) return JS_EXCEPTION;
GLFWPosition* position = js_mallocz(ctx, sizeof(*position));
if (!position) return JS_EXCEPTION;
GLFWSize* size = js_mallocz(ctx, sizeof(*size));
if (!size) return JS_EXCEPTION;
workarea->position = position;
workarea->size = size;
if (JS_ToInt32(ctx, &position->x, argv[0]))
goto fail;
if (JS_ToInt32(ctx, &position->y, argv[1]))
goto fail;
if (JS_ToInt32(ctx, &size->width, argv[2]))
goto fail;
if (JS_ToInt32(ctx, &size->height, argv[3]))
goto fail;
return glfw_workarea_new_instance(ctx, workarea);
fail:
js_free(ctx, workarea);
return JS_EXCEPTION;
}
void glfw_workarea_finalizer(JSRuntime* rt, JSValue val) {
GLFWWorkArea* workarea = JS_GetOpaque(val, glfw_workarea_class_id);
js_free_rt(rt, workarea);
}
// Properties
JSValue glfw_workarea_get_position_or_size(JSContext* ctx, JSValueConst this_val, int magic) {
GLFWWorkArea* workarea = JS_GetOpaque2(ctx, this_val, glfw_workarea_class_id);
if (!workarea) return JS_EXCEPTION;
if (magic == 0)
return glfw_position_new_instance(ctx, workarea->position);
else
return glfw_size_new_instance(ctx, workarea->size);
}
JSValue glfw_workarea_set_position_or_size(JSContext* ctx, JSValueConst this_val, JSValue val, int magic) {
GLFWWorkArea* workarea = JS_GetOpaque2(ctx, this_val, glfw_workarea_class_id);
if (!workarea) return JS_EXCEPTION;
if (magic == 0) {
workarea->position = JS_GetOpaque2(ctx, val, glfw_position_class_id);
} else {
workarea->size = JS_GetOpaque2(ctx, val, glfw_size_class_id);
}
return JS_UNDEFINED;
}
// Initialization
JSClassDef glfw_workarea_class_def = {
"WorkArea",
.finalizer = glfw_workarea_finalizer,
};
const JSCFunctionListEntry glfw_workarea_proto_funcs[] = {
JS_CGETSET_MAGIC_DEF("position", glfw_workarea_get_position_or_size, glfw_workarea_set_position_or_size, 0),
JS_CGETSET_MAGIC_DEF("size", glfw_workarea_get_position_or_size, glfw_workarea_set_position_or_size, 1),
};
JSValue glfw_workarea_proto, glfw_workarea_class;
JSValue glfw_workarea_constructor(JSContext* ctx) {
JSRuntime* rt = JS_GetRuntime(ctx);
if (!JS_IsRegisteredClass(rt, glfw_workarea_class_id)) {
JS_NewClassID(&glfw_workarea_class_id);
JS_NewClass(rt, glfw_workarea_class_id, &glfw_workarea_class_def);
glfw_workarea_proto = JS_NewObject(ctx);
JS_SetPropertyFunctionList(ctx, glfw_workarea_proto, glfw_workarea_proto_funcs, countof(glfw_workarea_proto_funcs));
JS_SetClassProto(ctx, glfw_workarea_class_id, glfw_workarea_proto);
glfw_workarea_class = JS_NewCFunction2(ctx, glfw_workarea_ctor, "WorkArea", 2, JS_CFUNC_constructor, 0);
/* set proto.constructor and ctor.prototype */
JS_SetConstructor(ctx, glfw_workarea_class, glfw_workarea_proto);
}
return glfw_workarea_class;
}
JSValue glfw_workarea_new_instance(JSContext* ctx, GLFWWorkArea* workarea) {
JSValue ctor = glfw_workarea_constructor(ctx);
JSValue proto = JS_GetPropertyStr(ctx, ctor, "prototype");
if (JS_IsException(proto)) {
JS_FreeValue(ctx, proto);
return JS_EXCEPTION;
}
JSValue obj = JS_NewObjectProtoClass(ctx, proto, glfw_workarea_class_id);
JS_FreeValue(ctx, proto);
if (JS_IsException(proto)) {
JS_FreeValue(ctx, obj);
return JS_EXCEPTION;
}
JS_SetOpaque(obj, workarea);
return obj;
}
int glfw_workarea_init(JSContext* ctx, JSModuleDef* m) {
JS_SetModuleExport(ctx, m, "WorkArea", glfw_workarea_constructor(ctx));
return 0;
}
int glfw_workarea_export(JSContext* ctx, JSModuleDef* m) {
return JS_AddModuleExport(ctx, m, "WorkArea");
}