-
Notifications
You must be signed in to change notification settings - Fork 2
/
position.c
110 lines (84 loc) · 3.17 KB
/
position.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
#include "glfw.h"
#include "position.h"
// Constructor/Destructor
JSValue glfw_position_ctor(JSContext* ctx, JSValueConst new_target, int argc, JSValueConst* argv) {
GLFWPosition* position = js_mallocz(ctx, sizeof(*position));
if (!position) return JS_EXCEPTION;
if (JS_ToInt32(ctx, &position->x, argv[0]))
goto fail;
if (JS_ToInt32(ctx, &position->y, argv[1]))
goto fail;
return glfw_position_new_instance(ctx, position);
fail:
js_free(ctx, position);
return JS_EXCEPTION;
}
void glfw_position_finalizer(JSRuntime* rt, JSValue val) {
GLFWPosition* position = JS_GetOpaque(val, glfw_position_class_id);
js_free_rt(rt, position);
}
// Properties
JSValue glfw_position_get_xy(JSContext* ctx, JSValueConst this_val, int magic) {
GLFWPosition* position = JS_GetOpaque2(ctx, this_val, glfw_position_class_id);
if (!position) return JS_EXCEPTION;
return JS_NewInt32(ctx, magic == 0 ? position->x : position->y);
}
JSValue glfw_position_set_xy(JSContext* ctx, JSValueConst this_val, JSValue val, int magic) {
GLFWPosition* position = JS_GetOpaque2(ctx, this_val, glfw_position_class_id);
if (!position) return JS_EXCEPTION;
int value;
if (JS_ToInt32(ctx, &value, val))
return JS_EXCEPTION;
if (magic == 0)
position->x = value;
else
position->y = value;
return JS_UNDEFINED;
}
// Initialization
JSClassDef glfw_position_class_def = {
"Position",
.finalizer = glfw_position_finalizer,
};
const JSCFunctionListEntry glfw_position_proto_funcs[] = {
JS_CGETSET_MAGIC_DEF("x", glfw_position_get_xy, glfw_position_set_xy, 0),
JS_CGETSET_MAGIC_DEF("y", glfw_position_get_xy, glfw_position_set_xy, 1),
};
JSValue glfw_position_proto, glfw_position_class;
JSValue glfw_position_constructor(JSContext* ctx) {
JSRuntime* rt = JS_GetRuntime(ctx);
if (!JS_IsRegisteredClass(rt, glfw_position_class_id)) {
JS_NewClassID(&glfw_position_class_id);
JS_NewClass(rt, glfw_position_class_id, &glfw_position_class_def);
glfw_position_proto = JS_NewObject(ctx);
JS_SetPropertyFunctionList(ctx, glfw_position_proto, glfw_position_proto_funcs, countof(glfw_position_proto_funcs));
JS_SetClassProto(ctx, glfw_position_class_id, glfw_position_proto);
glfw_position_class = JS_NewCFunction2(ctx, glfw_position_ctor, "Position", 2, JS_CFUNC_constructor, 0);
/* set proto.constructor and ctor.prototype */
JS_SetConstructor(ctx, glfw_position_class, glfw_position_proto);
}
return glfw_position_class;
}
JSValue glfw_position_new_instance(JSContext* ctx, GLFWPosition* position) {
JSValue obj = JS_UNDEFINED;
JSValue proto;
proto = JS_GetPropertyStr(ctx, glfw_position_constructor(ctx), "prototype");
if (JS_IsException(proto))
goto fail;
obj = JS_NewObjectProtoClass(ctx, proto, glfw_position_class_id);
JS_FreeValue(ctx, proto);
if (JS_IsException(obj))
goto fail;
JS_SetOpaque(obj, position);
return obj;
fail:
JS_FreeValue(ctx, obj);
return JS_EXCEPTION;
}
int glfw_position_init(JSContext* ctx, JSModuleDef* m) {
JS_SetModuleExport(ctx, m, "Position", glfw_position_constructor(ctx));
return 0;
}
int glfw_position_export(JSContext* ctx, JSModuleDef* m) {
return JS_AddModuleExport(ctx, m, "Position");
}