rename a_version.alpha to a_version.alpha_
[liba.git] / quickjs / src / version.c
blobf82b34d86c83b3a0133b9aa06ee8a319ad817397
1 #include "a.h"
2 #include "a/version.h"
4 static JSClassID liba_version_class_id;
6 static void liba_version_finalizer(JSRuntime *rt, JSValue val)
8 js_free_rt(rt, JS_GetOpaque(val, liba_version_class_id));
11 static JSClassDef liba_version_class = {"version", .finalizer = liba_version_finalizer};
13 static JSValue liba_version_ctor(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst *argv)
15 JSValue proto, clazz = JS_UNDEFINED;
16 a_version *const self = (a_version *)js_mallocz(ctx, sizeof(a_version));
17 if (!self) { return JS_EXCEPTION; }
18 char const *ver = NULL;
19 a_u32 args[] = {0, 0, 0, 0};
20 if (argc > (int)A_LEN(args)) { argc = (int)A_LEN(args); }
21 for (int i = 0; i < argc; ++i)
23 if (JS_ToUint32(ctx, &args[i], argv[i]))
25 if (i == 0)
27 ver = JS_ToCString(ctx, argv[0]);
28 if (ver) { break; }
30 goto fail;
33 self->alpha_[0] = '.';
34 if (ver)
36 a_version_parse(self, ver);
37 JS_FreeCString(ctx, ver);
39 else
41 self->major = (unsigned int)args[0];
42 self->minor = (unsigned int)args[1];
43 self->third = (unsigned int)args[2];
44 self->extra = (unsigned int)args[3];
46 proto = JS_GetPropertyStr(ctx, new_target, "prototype");
47 if (JS_IsException(proto)) { goto fail; }
48 clazz = JS_NewObjectProtoClass(ctx, proto, liba_version_class_id);
49 JS_FreeValue(ctx, proto);
50 if (JS_IsException(clazz)) { goto fail; }
51 JS_SetOpaque(clazz, self);
52 return clazz;
53 fail:
54 js_free(ctx, self);
55 JS_FreeValue(ctx, clazz);
56 return JS_EXCEPTION;
59 static JSValue liba_version_check(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
61 (void)this_val;
62 a_u32 args[] = {0, 0, 0};
63 if (argc > (int)A_LEN(args)) { argc = (int)A_LEN(args); }
64 for (int i = 0; i < argc; ++i)
66 if (JS_ToUint32(ctx, &args[i], argv[i])) { return JS_EXCEPTION; }
68 return JS_NewInt32(ctx, a_version_check((unsigned int)args[0], (unsigned int)args[1], (unsigned int)args[2]));
71 static JSValue liba_version_tostring(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
73 (void)argc;
74 (void)argv;
75 char str[48];
76 a_version *const self = (a_version *)JS_GetOpaque2(ctx, this_val, liba_version_class_id);
77 if (!self) { return JS_EXCEPTION; }
78 a_version_tostr(self, str, sizeof(str));
79 return JS_NewString(ctx, str);
82 static JSValue liba_version_parse(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
84 (void)argc;
85 a_version *const self = (a_version *)JS_GetOpaque2(ctx, this_val, liba_version_class_id);
86 if (!self) { return JS_EXCEPTION; }
87 char const *ver = JS_ToCString(ctx, argv[0]);
88 if (!ver) { return JS_EXCEPTION; }
89 a_version_parse(self, ver);
90 JS_FreeCString(ctx, ver);
91 return JS_UNDEFINED;
94 static JSValue liba_version_cmp(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
96 (void)argc;
97 a_version *const self = (a_version *)JS_GetOpaque2(ctx, this_val, liba_version_class_id);
98 if (!self) { return JS_EXCEPTION; }
99 a_version *const that = (a_version *)JS_GetOpaque2(ctx, argv[0], liba_version_class_id);
100 if (!that) { return JS_EXCEPTION; }
101 return JS_NewInt32(ctx, a_version_cmp(self, that));
104 static JSValue liba_version_lt(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
106 (void)argc;
107 a_version *const self = (a_version *)JS_GetOpaque2(ctx, this_val, liba_version_class_id);
108 if (!self) { return JS_EXCEPTION; }
109 a_version *const that = (a_version *)JS_GetOpaque2(ctx, argv[0], liba_version_class_id);
110 if (!that) { return JS_EXCEPTION; }
111 return JS_NewBool(ctx, a_version_lt(self, that));
114 static JSValue liba_version_gt(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
116 (void)argc;
117 a_version *const self = (a_version *)JS_GetOpaque2(ctx, this_val, liba_version_class_id);
118 if (!self) { return JS_EXCEPTION; }
119 a_version *const that = (a_version *)JS_GetOpaque2(ctx, argv[0], liba_version_class_id);
120 if (!that) { return JS_EXCEPTION; }
121 return JS_NewBool(ctx, a_version_gt(self, that));
124 static JSValue liba_version_le(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
126 (void)argc;
127 a_version *const self = (a_version *)JS_GetOpaque2(ctx, this_val, liba_version_class_id);
128 if (!self) { return JS_EXCEPTION; }
129 a_version *const that = (a_version *)JS_GetOpaque2(ctx, argv[0], liba_version_class_id);
130 if (!that) { return JS_EXCEPTION; }
131 return JS_NewBool(ctx, a_version_le(self, that));
134 static JSValue liba_version_ge(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
136 (void)argc;
137 a_version *const self = (a_version *)JS_GetOpaque2(ctx, this_val, liba_version_class_id);
138 if (!self) { return JS_EXCEPTION; }
139 a_version *const that = (a_version *)JS_GetOpaque2(ctx, argv[0], liba_version_class_id);
140 if (!that) { return JS_EXCEPTION; }
141 return JS_NewBool(ctx, a_version_ge(self, that));
144 static JSValue liba_version_eq(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
146 (void)argc;
147 a_version *const self = (a_version *)JS_GetOpaque2(ctx, this_val, liba_version_class_id);
148 if (!self) { return JS_EXCEPTION; }
149 a_version *const that = (a_version *)JS_GetOpaque2(ctx, argv[0], liba_version_class_id);
150 if (!that) { return JS_EXCEPTION; }
151 return JS_NewBool(ctx, a_version_eq(self, that));
154 static JSValue liba_version_ne(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
156 (void)argc;
157 a_version *const self = (a_version *)JS_GetOpaque2(ctx, this_val, liba_version_class_id);
158 if (!self) { return JS_EXCEPTION; }
159 a_version *const that = (a_version *)JS_GetOpaque2(ctx, argv[0], liba_version_class_id);
160 if (!that) { return JS_EXCEPTION; }
161 return JS_NewBool(ctx, a_version_ne(self, that));
164 enum
166 self_major,
167 self_minor,
168 self_third,
169 self_extra,
170 self_alpha,
173 static JSValue liba_version_get(JSContext *ctx, JSValueConst this_val, int magic)
175 a_version *const self = (a_version *)JS_GetOpaque2(ctx, this_val, liba_version_class_id);
176 if (!self) { return JS_EXCEPTION; }
177 if (magic == self_alpha)
179 char alpha[sizeof(self->alpha_) + 1];
180 a_version_alpha(self, alpha);
181 return JS_NewString(ctx, alpha);
183 a_u32 x;
184 switch (magic)
186 case self_major: x = self->major; break;
187 case self_minor: x = self->minor; break;
188 case self_third: x = self->third; break;
189 case self_extra: x = self->extra; break;
190 default: return JS_UNDEFINED;
192 return JS_NewUint32(ctx, x);
195 static JSValue liba_version_set(JSContext *ctx, JSValueConst this_val, JSValueConst val, int magic)
197 a_version *const self = (a_version *)JS_GetOpaque2(ctx, this_val, liba_version_class_id);
198 if (!self) { return JS_EXCEPTION; }
199 if (magic == self_alpha)
201 char const *alpha = JS_ToCString(ctx, val);
202 a_version_set_alpha(self, alpha);
203 JS_FreeCString(ctx, alpha);
204 return JS_UNDEFINED;
206 a_u32 x;
207 if (JS_ToUint32(ctx, &x, val)) { return JS_EXCEPTION; }
208 switch (magic)
210 case self_major: self->major = (unsigned int)x; break;
211 case self_minor: self->minor = (unsigned int)x; break;
212 case self_third: self->third = (unsigned int)x; break;
213 case self_extra: self->extra = (unsigned int)x; break;
214 default: break;
216 return JS_UNDEFINED;
219 static JSCFunctionListEntry const liba_version_proto[] = {
220 JS_CGETSET_MAGIC_DEF("major", liba_version_get, liba_version_set, self_major),
221 JS_CGETSET_MAGIC_DEF("minor", liba_version_get, liba_version_set, self_minor),
222 JS_CGETSET_MAGIC_DEF("third", liba_version_get, liba_version_set, self_third),
223 JS_CGETSET_MAGIC_DEF("extra", liba_version_get, liba_version_set, self_extra),
224 JS_CGETSET_MAGIC_DEF("alpha", liba_version_get, liba_version_set, self_alpha),
225 JS_CFUNC_DEF("toString", 0, liba_version_tostring),
226 JS_CFUNC_DEF("parse", 1, liba_version_parse),
227 JS_CFUNC_DEF("cmp", 1, liba_version_cmp),
228 JS_CFUNC_DEF("lt", 1, liba_version_lt),
229 JS_CFUNC_DEF("gt", 1, liba_version_gt),
230 JS_CFUNC_DEF("le", 1, liba_version_le),
231 JS_CFUNC_DEF("ge", 1, liba_version_ge),
232 JS_CFUNC_DEF("eq", 1, liba_version_eq),
233 JS_CFUNC_DEF("ne", 1, liba_version_ne),
236 int js_liba_version_init(JSContext *ctx, JSModuleDef *m)
238 JS_NewClassID(&liba_version_class_id);
239 JS_NewClass(JS_GetRuntime(ctx), liba_version_class_id, &liba_version_class);
241 JSValue const proto = JS_NewObject(ctx);
242 JS_SetPropertyFunctionList(ctx, proto, liba_version_proto, A_LEN(liba_version_proto));
244 JSValue const clazz = JS_NewCFunction2(ctx, liba_version_ctor, "version", 4, JS_CFUNC_constructor, 0);
245 JS_SetConstructor(ctx, clazz, proto);
246 JS_SetClassProto(ctx, liba_version_class_id, proto);
248 JSValue const check = JS_NewCFunction2(ctx, liba_version_check, "check", 3, JS_CFUNC_generic, 0);
249 JS_DefinePropertyValueStr(ctx, clazz, "MAJOR", JS_NewUint32(ctx, A_VERSION_MAJOR), 0);
250 JS_DefinePropertyValueStr(ctx, clazz, "MINOR", JS_NewUint32(ctx, A_VERSION_MINOR), 0);
251 JS_DefinePropertyValueStr(ctx, clazz, "PATCH", JS_NewUint32(ctx, A_VERSION_PATCH), 0);
252 JS_DefinePropertyValueStr(ctx, clazz, "TWEAK", JS_NewUint32(ctx, A_VERSION_TWEAK), 0);
253 JS_DefinePropertyValueStr(ctx, clazz, "check", check, 0);
255 return JS_SetModuleExport(ctx, m, "version", clazz);