rename other to that
[liba.git] / quickjs / src / version.c
blobbf43b2f805d3e53ccd5fc61127229c8023a69086
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};
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)
27 ver = JS_ToCString(ctx, argv[0]);
28 if (ver) { break; }
30 goto fail;
33 if (ver)
35 a_version_parse(self, ver);
36 JS_FreeCString(ctx, ver);
38 else
40 self->major = (unsigned int)args[0];
41 self->minor = (unsigned int)args[1];
42 self->third = (unsigned int)args[2];
44 proto = JS_GetPropertyStr(ctx, new_target, "prototype");
45 if (JS_IsException(proto)) { goto fail; }
46 clazz = JS_NewObjectProtoClass(ctx, proto, liba_version_class_id);
47 JS_FreeValue(ctx, proto);
48 if (JS_IsException(clazz)) { goto fail; }
49 JS_SetOpaque(clazz, self);
50 return clazz;
51 fail:
52 js_free(ctx, self);
53 JS_FreeValue(ctx, clazz);
54 return JS_EXCEPTION;
57 static JSValue liba_version_get(JSContext *ctx, JSValueConst this_val, int magic)
59 a_version *const self = (a_version *)JS_GetOpaque2(ctx, this_val, liba_version_class_id);
60 if (!self) { return JS_EXCEPTION; }
61 a_u32 ver;
62 switch (magic)
64 case 0: ver = self->major; break;
65 case 1: ver = self->minor; break;
66 case 2: ver = self->third; break;
67 case 3: ver = self->extra; break;
68 default: return JS_UNDEFINED;
70 return JS_NewUint32(ctx, ver);
73 static JSValue liba_version_set(JSContext *ctx, JSValueConst this_val, JSValueConst val, int magic)
75 a_version *const self = (a_version *)JS_GetOpaque2(ctx, this_val, liba_version_class_id);
76 if (!self) { return JS_EXCEPTION; }
77 a_u32 ver;
78 if (JS_ToUint32(ctx, &ver, val)) { return JS_EXCEPTION; }
79 switch (magic)
81 case 0: self->major = (unsigned int)ver; break;
82 case 1: self->minor = (unsigned int)ver; break;
83 case 2: self->third = (unsigned int)ver; break;
84 case 3: self->extra = (unsigned int)ver; break;
85 default: break;
87 return JS_UNDEFINED;
90 static JSValue liba_version_check(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
92 (void)this_val;
93 a_u32 args[] = {0, 0, 0};
94 if (argc > (int)A_LEN(args)) { argc = (int)A_LEN(args); }
95 for (int i = 0; i < argc; ++i)
97 if (JS_ToUint32(ctx, &args[i], argv[i])) { return JS_EXCEPTION; }
99 #undef a_version_check
100 return JS_NewInt32(ctx, a_version_check((unsigned int)args[0], (unsigned int)args[1], (unsigned int)args[2]));
103 static JSValue liba_version_parse(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
105 (void)argc;
106 a_version *const self = (a_version *)JS_GetOpaque2(ctx, this_val, liba_version_class_id);
107 if (!self) { return JS_EXCEPTION; }
108 char const *ver = JS_ToCString(ctx, argv[0]);
109 if (!ver) { return JS_EXCEPTION; }
110 a_version_parse(self, ver);
111 JS_FreeCString(ctx, ver);
112 return JS_UNDEFINED;
115 static JSValue liba_version_cmp(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
117 (void)argc;
118 a_version *const self = (a_version *)JS_GetOpaque2(ctx, this_val, liba_version_class_id);
119 if (!self) { return JS_EXCEPTION; }
120 a_version *const that = (a_version *)JS_GetOpaque2(ctx, argv[0], liba_version_class_id);
121 if (!that) { return JS_EXCEPTION; }
122 return JS_NewInt32(ctx, a_version_cmp(self, that));
125 static JSValue liba_version_lt(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
127 (void)argc;
128 a_version *const self = (a_version *)JS_GetOpaque2(ctx, this_val, liba_version_class_id);
129 if (!self) { return JS_EXCEPTION; }
130 a_version *const that = (a_version *)JS_GetOpaque2(ctx, argv[0], liba_version_class_id);
131 if (!that) { return JS_EXCEPTION; }
132 return JS_NewBool(ctx, a_version_lt(self, that));
135 static JSValue liba_version_gt(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
137 (void)argc;
138 a_version *const self = (a_version *)JS_GetOpaque2(ctx, this_val, liba_version_class_id);
139 if (!self) { return JS_EXCEPTION; }
140 a_version *const that = (a_version *)JS_GetOpaque2(ctx, argv[0], liba_version_class_id);
141 if (!that) { return JS_EXCEPTION; }
142 return JS_NewBool(ctx, a_version_gt(self, that));
145 static JSValue liba_version_le(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
147 (void)argc;
148 a_version *const self = (a_version *)JS_GetOpaque2(ctx, this_val, liba_version_class_id);
149 if (!self) { return JS_EXCEPTION; }
150 a_version *const that = (a_version *)JS_GetOpaque2(ctx, argv[0], liba_version_class_id);
151 if (!that) { return JS_EXCEPTION; }
152 return JS_NewBool(ctx, a_version_le(self, that));
155 static JSValue liba_version_ge(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
157 (void)argc;
158 a_version *const self = (a_version *)JS_GetOpaque2(ctx, this_val, liba_version_class_id);
159 if (!self) { return JS_EXCEPTION; }
160 a_version *const that = (a_version *)JS_GetOpaque2(ctx, argv[0], liba_version_class_id);
161 if (!that) { return JS_EXCEPTION; }
162 return JS_NewBool(ctx, a_version_ge(self, that));
165 static JSValue liba_version_eq(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
167 (void)argc;
168 a_version *const self = (a_version *)JS_GetOpaque2(ctx, this_val, liba_version_class_id);
169 if (!self) { return JS_EXCEPTION; }
170 a_version *const that = (a_version *)JS_GetOpaque2(ctx, argv[0], liba_version_class_id);
171 if (!that) { return JS_EXCEPTION; }
172 return JS_NewBool(ctx, a_version_eq(self, that));
175 static JSValue liba_version_ne(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
177 (void)argc;
178 a_version *const self = (a_version *)JS_GetOpaque2(ctx, this_val, liba_version_class_id);
179 if (!self) { return JS_EXCEPTION; }
180 a_version *const that = (a_version *)JS_GetOpaque2(ctx, argv[0], liba_version_class_id);
181 if (!that) { return JS_EXCEPTION; }
182 return JS_NewBool(ctx, a_version_ne(self, that));
185 static JSCFunctionListEntry const liba_version_proto[] = {
186 JS_PROP_STRING_DEF("[Symbol.toStringTag]", "a.version", 0),
187 JS_CGETSET_MAGIC_DEF("major", liba_version_get, liba_version_set, 0),
188 JS_CGETSET_MAGIC_DEF("minor", liba_version_get, liba_version_set, 1),
189 JS_CGETSET_MAGIC_DEF("third", liba_version_get, liba_version_set, 2),
190 JS_CGETSET_MAGIC_DEF("extra", liba_version_get, liba_version_set, 3),
191 JS_CFUNC_DEF("parse", 1, liba_version_parse),
192 JS_CFUNC_DEF("cmp", 1, liba_version_cmp),
193 JS_CFUNC_DEF("lt", 1, liba_version_lt),
194 JS_CFUNC_DEF("gt", 1, liba_version_gt),
195 JS_CFUNC_DEF("le", 1, liba_version_le),
196 JS_CFUNC_DEF("ge", 1, liba_version_ge),
197 JS_CFUNC_DEF("eq", 1, liba_version_eq),
198 JS_CFUNC_DEF("ne", 1, liba_version_ne),
201 int js_liba_version_init(JSContext *ctx, JSModuleDef *m)
203 JS_NewClassID(&liba_version_class_id);
204 JS_NewClass(JS_GetRuntime(ctx), liba_version_class_id, &liba_version_class);
206 JSValue const proto = JS_NewObject(ctx);
207 JS_SetPropertyFunctionList(ctx, proto, liba_version_proto, A_LEN(liba_version_proto));
209 JSValue const clazz = JS_NewCFunction2(ctx, liba_version_ctor, "version", 3, JS_CFUNC_constructor, 0);
210 JS_SetConstructor(ctx, clazz, proto);
211 JS_SetClassProto(ctx, liba_version_class_id, proto);
213 JSValue const version_check = JS_NewCFunction2(ctx, liba_version_check, "check", 3, JS_CFUNC_generic, 0);
214 JS_DefinePropertyValueStr(ctx, clazz, "MAJOR", JS_NewUint32(ctx, A_VERSION_MAJOR), 0);
215 JS_DefinePropertyValueStr(ctx, clazz, "MINOR", JS_NewUint32(ctx, A_VERSION_MINOR), 0);
216 JS_DefinePropertyValueStr(ctx, clazz, "PATCH", JS_NewUint32(ctx, A_VERSION_PATCH), 0);
217 JS_DefinePropertyValueStr(ctx, clazz, "TWEAK", JS_NewUint32(ctx, A_VERSION_TWEAK), 0);
218 JS_DefinePropertyValueStr(ctx, clazz, "check", version_check, 0);
220 return JS_SetModuleExport(ctx, m, "version", clazz);