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
]))
27 ver
= JS_ToCString(ctx
, argv
[0]);
36 a_version_parse(self
, ver
);
37 JS_FreeCString(ctx
, ver
);
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
);
55 JS_FreeValue(ctx
, clazz
);
59 static JSValue
liba_version_check(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
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 #undef a_version_check
69 return JS_NewInt32(ctx
, a_version_check((unsigned int)args
[0], (unsigned int)args
[1], (unsigned int)args
[2]));
72 static JSValue
liba_version_tostring(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
77 a_version
*const self
= (a_version
*)JS_GetOpaque2(ctx
, this_val
, liba_version_class_id
);
78 if (!self
) { return JS_EXCEPTION
; }
79 a_version_tostr(self
, str
, sizeof(str
));
80 return JS_NewString(ctx
, str
);
83 static JSValue
liba_version_parse(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
86 a_version
*const self
= (a_version
*)JS_GetOpaque2(ctx
, this_val
, liba_version_class_id
);
87 if (!self
) { return JS_EXCEPTION
; }
88 char const *ver
= JS_ToCString(ctx
, argv
[0]);
89 if (!ver
) { return JS_EXCEPTION
; }
90 a_version_parse(self
, ver
);
91 JS_FreeCString(ctx
, ver
);
95 static JSValue
liba_version_cmp(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
98 a_version
*const self
= (a_version
*)JS_GetOpaque2(ctx
, this_val
, liba_version_class_id
);
99 if (!self
) { return JS_EXCEPTION
; }
100 a_version
*const that
= (a_version
*)JS_GetOpaque2(ctx
, argv
[0], liba_version_class_id
);
101 if (!that
) { return JS_EXCEPTION
; }
102 return JS_NewInt32(ctx
, a_version_cmp(self
, that
));
105 static JSValue
liba_version_lt(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
108 a_version
*const self
= (a_version
*)JS_GetOpaque2(ctx
, this_val
, liba_version_class_id
);
109 if (!self
) { return JS_EXCEPTION
; }
110 a_version
*const that
= (a_version
*)JS_GetOpaque2(ctx
, argv
[0], liba_version_class_id
);
111 if (!that
) { return JS_EXCEPTION
; }
112 return JS_NewBool(ctx
, a_version_lt(self
, that
));
115 static JSValue
liba_version_gt(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
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_NewBool(ctx
, a_version_gt(self
, that
));
125 static JSValue
liba_version_le(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
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_le(self
, that
));
135 static JSValue
liba_version_ge(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
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_ge(self
, that
));
145 static JSValue
liba_version_eq(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
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_eq(self
, that
));
155 static JSValue
liba_version_ne(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
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_ne(self
, that
));
174 static JSValue
liba_version_get(JSContext
*ctx
, JSValueConst this_val
, int magic
)
176 a_version
*const self
= (a_version
*)JS_GetOpaque2(ctx
, this_val
, liba_version_class_id
);
177 if (!self
) { return JS_EXCEPTION
; }
178 if (magic
== self_alpha_
)
180 char alpha
[sizeof(self
->alpha
) + 1];
181 a_version_alpha(self
, alpha
);
182 return JS_NewString(ctx
, alpha
);
187 case self_major_
: ver
= self
->major
; break;
188 case self_minor_
: ver
= self
->minor
; break;
189 case self_third_
: ver
= self
->third
; break;
190 case self_extra_
: ver
= self
->extra
; break;
191 default: return JS_UNDEFINED
;
193 return JS_NewUint32(ctx
, ver
);
196 static JSValue
liba_version_set(JSContext
*ctx
, JSValueConst this_val
, JSValueConst val
, int magic
)
198 a_version
*const self
= (a_version
*)JS_GetOpaque2(ctx
, this_val
, liba_version_class_id
);
199 if (!self
) { return JS_EXCEPTION
; }
201 if (magic
== self_alpha_
)
203 char const *alpha
= JS_ToCString(ctx
, val
);
204 a_version_set_alpha(self
, alpha
);
205 JS_FreeCString(ctx
, alpha
);
208 if (JS_ToUint32(ctx
, &ver
, val
)) { return JS_EXCEPTION
; }
211 case self_major_
: self
->major
= (unsigned int)ver
; break;
212 case self_minor_
: self
->minor
= (unsigned int)ver
; break;
213 case self_third_
: self
->third
= (unsigned int)ver
; break;
214 case self_extra_
: self
->extra
= (unsigned int)ver
; break;
220 static JSCFunctionListEntry
const liba_version_proto
[] = {
221 JS_CGETSET_MAGIC_DEF("major", liba_version_get
, liba_version_set
, self_major_
),
222 JS_CGETSET_MAGIC_DEF("minor", liba_version_get
, liba_version_set
, self_minor_
),
223 JS_CGETSET_MAGIC_DEF("third", liba_version_get
, liba_version_set
, self_third_
),
224 JS_CGETSET_MAGIC_DEF("extra", liba_version_get
, liba_version_set
, self_extra_
),
225 JS_CGETSET_MAGIC_DEF("alpha", liba_version_get
, liba_version_set
, self_alpha_
),
226 JS_CFUNC_DEF("toString", 0, liba_version_tostring
),
227 JS_CFUNC_DEF("parse", 1, liba_version_parse
),
228 JS_CFUNC_DEF("cmp", 1, liba_version_cmp
),
229 JS_CFUNC_DEF("lt", 1, liba_version_lt
),
230 JS_CFUNC_DEF("gt", 1, liba_version_gt
),
231 JS_CFUNC_DEF("le", 1, liba_version_le
),
232 JS_CFUNC_DEF("ge", 1, liba_version_ge
),
233 JS_CFUNC_DEF("eq", 1, liba_version_eq
),
234 JS_CFUNC_DEF("ne", 1, liba_version_ne
),
237 int js_liba_version_init(JSContext
*ctx
, JSModuleDef
*m
)
239 JS_NewClassID(&liba_version_class_id
);
240 JS_NewClass(JS_GetRuntime(ctx
), liba_version_class_id
, &liba_version_class
);
242 JSValue
const proto
= JS_NewObject(ctx
);
243 JS_SetPropertyFunctionList(ctx
, proto
, liba_version_proto
, A_LEN(liba_version_proto
));
245 JSValue
const clazz
= JS_NewCFunction2(ctx
, liba_version_ctor
, "version", 4, JS_CFUNC_constructor
, 0);
246 JS_SetConstructor(ctx
, clazz
, proto
);
247 JS_SetClassProto(ctx
, liba_version_class_id
, proto
);
249 JSValue
const version_check
= JS_NewCFunction2(ctx
, liba_version_check
, "check", 3, JS_CFUNC_generic
, 0);
250 JS_DefinePropertyValueStr(ctx
, clazz
, "MAJOR", JS_NewUint32(ctx
, A_VERSION_MAJOR
), 0);
251 JS_DefinePropertyValueStr(ctx
, clazz
, "MINOR", JS_NewUint32(ctx
, A_VERSION_MINOR
), 0);
252 JS_DefinePropertyValueStr(ctx
, clazz
, "PATCH", JS_NewUint32(ctx
, A_VERSION_PATCH
), 0);
253 JS_DefinePropertyValueStr(ctx
, clazz
, "TWEAK", JS_NewUint32(ctx
, A_VERSION_TWEAK
), 0);
254 JS_DefinePropertyValueStr(ctx
, clazz
, "check", version_check
, 0);
256 return JS_SetModuleExport(ctx
, m
, "version", clazz
);