opt: rename polytrack to polytraj
[liba.git] / quickjs / src / a.c
blobc3cead6516ea0e7663b7306e98b6c8e4c94f18f5
1 #include "a.h"
3 static JSValue Concat(JSContext *const ctx, JSValueConst const val)
5 JSValue this_val = JS_NewArray(ctx);
6 JSValueConst argv[] = {this_val, val};
7 JSValue concat = JS_GetPropertyStr(ctx, this_val, "concat");
8 JSValue apply = JS_GetPropertyStr(ctx, concat, "apply");
9 JSValue res = JS_Call(ctx, apply, this_val, A_LEN(argv), argv);
10 JS_FreeValue(ctx, this_val);
11 JS_FreeValue(ctx, concat);
12 JS_FreeValue(ctx, apply);
13 return res;
16 static int ArrayLength(JSContext *const ctx, JSValueConst const val, a_u32_t *const plen)
18 JSValue len = JS_GetPropertyStr(ctx, val, "length");
19 if (JS_IsException(len))
21 return ~0;
23 int ret = JS_ToUint32(ctx, plen, len);
24 JS_FreeValue(ctx, len);
25 return ret;
28 static int ArrayFloat(JSContext *const ctx, JSValueConst const val, a_float_t *const ptr, a_u32_t const len)
30 for (unsigned int i = 0; i < len; ++i)
32 JSValue tmp = JS_GetPropertyUint32(ctx, val, i);
33 if (JS_IsException(tmp))
35 return ~0;
37 double x;
38 int ret = JS_ToFloat64(ctx, &x, tmp);
39 JS_FreeValue(ctx, val);
40 if (ret)
42 return ret;
44 ptr[i] = (a_float_t)x;
46 return 0;
49 static JSValue js_hash_bkdr(JSContext *const ctx, JSValueConst const this_val, int const argc, JSValueConst *const argv)
51 (void)this_val;
52 a_umax_t x = 0;
53 for (int i = 0; i < argc; ++i)
55 char const *str = JS_ToCString(ctx, argv[i]);
56 x = a_hash_bkdr(str, x);
57 JS_FreeCString(ctx, str);
59 return JS_NewUint32(ctx, (a_u32_t)x);
62 #include "a/math.h"
64 static JSValue js_rsqrt(JSContext *const ctx, JSValueConst const this_val, int const argc, JSValueConst *const argv)
66 (void)this_val;
67 (void)argc;
68 double x;
69 if (JS_ToFloat64(ctx, &x, argv[0]))
71 return JS_EXCEPTION;
73 return JS_NewFloat64(ctx, a_f64_rsqrt(x));
76 #include "a/mf.h"
78 static JSValue js_mf_gauss(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
80 (void)argc;
81 (void)this_val;
82 double args[] = {0, 0, 0};
83 for (unsigned int i = 0; i < A_LEN(args); ++i)
85 if (JS_ToFloat64(ctx, &args[i], argv[i]))
87 return JS_EXCEPTION;
90 a_float_t x = a_mf_gauss((a_float_t)args[0], (a_float_t)args[1], (a_float_t)args[2]);
91 return JS_NewFloat64(ctx, (double)x);
94 static JSValue js_mf_gauss2(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
96 (void)argc;
97 (void)this_val;
98 double args[] = {0, 0, 0, 0, 0};
99 for (unsigned int i = 0; i < A_LEN(args); ++i)
101 if (JS_ToFloat64(ctx, &args[i], argv[i]))
103 return JS_EXCEPTION;
106 a_float_t x = a_mf_gauss2((a_float_t)args[0], (a_float_t)args[1], (a_float_t)args[2], (a_float_t)args[3], (a_float_t)args[4]);
107 return JS_NewFloat64(ctx, (double)x);
110 static JSValue js_mf_gbell(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
112 (void)argc;
113 (void)this_val;
114 double args[] = {0, 0, 0, 0};
115 for (unsigned int i = 0; i < A_LEN(args); ++i)
117 if (JS_ToFloat64(ctx, &args[i], argv[i]))
119 return JS_EXCEPTION;
122 a_float_t x = a_mf_gbell((a_float_t)args[0], (a_float_t)args[1], (a_float_t)args[2], (a_float_t)args[3]);
123 return JS_NewFloat64(ctx, (double)x);
126 static JSValue js_mf_sig(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
128 (void)argc;
129 (void)this_val;
130 double args[] = {0, 0, 0};
131 for (unsigned int i = 0; i < A_LEN(args); ++i)
133 if (JS_ToFloat64(ctx, &args[i], argv[i]))
135 return JS_EXCEPTION;
138 a_float_t x = a_mf_sig((a_float_t)args[0], (a_float_t)args[1], (a_float_t)args[2]);
139 return JS_NewFloat64(ctx, (double)x);
142 static JSValue js_mf_dsig(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
144 (void)argc;
145 (void)this_val;
146 double args[] = {0, 0, 0, 0, 0};
147 for (unsigned int i = 0; i < A_LEN(args); ++i)
149 if (JS_ToFloat64(ctx, &args[i], argv[i]))
151 return JS_EXCEPTION;
154 a_float_t x = a_mf_dsig((a_float_t)args[0], (a_float_t)args[1], (a_float_t)args[2], (a_float_t)args[3], (a_float_t)args[4]);
155 return JS_NewFloat64(ctx, (double)x);
158 static JSValue js_mf_psig(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
160 (void)argc;
161 (void)this_val;
162 double args[] = {0, 0, 0, 0, 0};
163 for (unsigned int i = 0; i < A_LEN(args); ++i)
165 if (JS_ToFloat64(ctx, &args[i], argv[i]))
167 return JS_EXCEPTION;
170 a_float_t x = a_mf_psig((a_float_t)args[0], (a_float_t)args[1], (a_float_t)args[2], (a_float_t)args[3], (a_float_t)args[4]);
171 return JS_NewFloat64(ctx, (double)x);
174 static JSValue js_mf_trap(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
176 (void)argc;
177 (void)this_val;
178 double args[] = {0, 0, 0, 0, 0};
179 for (unsigned int i = 0; i < A_LEN(args); ++i)
181 if (JS_ToFloat64(ctx, &args[i], argv[i]))
183 return JS_EXCEPTION;
186 a_float_t x = a_mf_trap((a_float_t)args[0], (a_float_t)args[1], (a_float_t)args[2], (a_float_t)args[3], (a_float_t)args[4]);
187 return JS_NewFloat64(ctx, (double)x);
190 static JSValue js_mf_tri(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
192 (void)argc;
193 (void)this_val;
194 double args[] = {0, 0, 0, 0};
195 for (unsigned int i = 0; i < A_LEN(args); ++i)
197 if (JS_ToFloat64(ctx, &args[i], argv[i]))
199 return JS_EXCEPTION;
202 a_float_t x = a_mf_tri((a_float_t)args[0], (a_float_t)args[1], (a_float_t)args[2], (a_float_t)args[3]);
203 return JS_NewFloat64(ctx, (double)x);
206 static JSValue js_mf_lins(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
208 (void)argc;
209 (void)this_val;
210 double args[] = {0, 0, 0};
211 for (unsigned int i = 0; i < A_LEN(args); ++i)
213 if (JS_ToFloat64(ctx, &args[i], argv[i]))
215 return JS_EXCEPTION;
218 a_float_t x = a_mf_lins((a_float_t)args[0], (a_float_t)args[1], (a_float_t)args[2]);
219 return JS_NewFloat64(ctx, (double)x);
222 static JSValue js_mf_linz(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
224 (void)argc;
225 (void)this_val;
226 double args[] = {0, 0, 0};
227 for (unsigned int i = 0; i < A_LEN(args); ++i)
229 if (JS_ToFloat64(ctx, &args[i], argv[i]))
231 return JS_EXCEPTION;
234 a_float_t x = a_mf_linz((a_float_t)args[0], (a_float_t)args[1], (a_float_t)args[2]);
235 return JS_NewFloat64(ctx, (double)x);
238 static JSValue js_mf_s(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
240 (void)argc;
241 (void)this_val;
242 double args[] = {0, 0, 0};
243 for (unsigned int i = 0; i < A_LEN(args); ++i)
245 if (JS_ToFloat64(ctx, &args[i], argv[i]))
247 return JS_EXCEPTION;
250 a_float_t x = a_mf_s((a_float_t)args[0], (a_float_t)args[1], (a_float_t)args[2]);
251 return JS_NewFloat64(ctx, (double)x);
254 static JSValue js_mf_z(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
256 (void)argc;
257 (void)this_val;
258 double args[] = {0, 0, 0};
259 for (unsigned int i = 0; i < A_LEN(args); ++i)
261 if (JS_ToFloat64(ctx, &args[i], argv[i]))
263 return JS_EXCEPTION;
266 a_float_t x = a_mf_z((a_float_t)args[0], (a_float_t)args[1], (a_float_t)args[2]);
267 return JS_NewFloat64(ctx, (double)x);
270 static JSValue js_mf_pi(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
272 (void)argc;
273 (void)this_val;
274 double args[] = {0, 0, 0, 0, 0};
275 for (unsigned int i = 0; i < A_LEN(args); ++i)
277 if (JS_ToFloat64(ctx, &args[i], argv[i]))
279 return JS_EXCEPTION;
282 a_float_t x = a_mf_pi((a_float_t)args[0], (a_float_t)args[1], (a_float_t)args[2], (a_float_t)args[3], (a_float_t)args[4]);
283 return JS_NewFloat64(ctx, (double)x);
286 static JSCFunctionListEntry const js_liba_mf_funcs[] = {
287 JS_PROP_INT32_DEF("NUL", A_MF_NUL, 0),
288 JS_PROP_INT32_DEF("GAUSS", A_MF_GAUSS, 0),
289 JS_PROP_INT32_DEF("GAUSS2", A_MF_GAUSS2, 0),
290 JS_PROP_INT32_DEF("GBELL", A_MF_GBELL, 0),
291 JS_PROP_INT32_DEF("SIG", A_MF_SIG, 0),
292 JS_PROP_INT32_DEF("DSIG", A_MF_DSIG, 0),
293 JS_PROP_INT32_DEF("PSIG", A_MF_PSIG, 0),
294 JS_PROP_INT32_DEF("TRAP", A_MF_TRAP, 0),
295 JS_PROP_INT32_DEF("TRI", A_MF_TRI, 0),
296 JS_PROP_INT32_DEF("LINS", A_MF_LINS, 0),
297 JS_PROP_INT32_DEF("LINZ", A_MF_LINZ, 0),
298 JS_PROP_INT32_DEF("S", A_MF_S, 0),
299 JS_PROP_INT32_DEF("Z", A_MF_Z, 0),
300 JS_PROP_INT32_DEF("PI", A_MF_PI, 0),
301 JS_CFUNC_DEF("gauss", 3, js_mf_gauss),
302 JS_CFUNC_DEF("gauss2", 5, js_mf_gauss2),
303 JS_CFUNC_DEF("gbell", 4, js_mf_gbell),
304 JS_CFUNC_DEF("sig", 3, js_mf_sig),
305 JS_CFUNC_DEF("dsig", 5, js_mf_dsig),
306 JS_CFUNC_DEF("psig", 5, js_mf_psig),
307 JS_CFUNC_DEF("trap", 5, js_mf_trap),
308 JS_CFUNC_DEF("tri", 4, js_mf_tri),
309 JS_CFUNC_DEF("lins", 3, js_mf_lins),
310 JS_CFUNC_DEF("linz", 3, js_mf_linz),
311 JS_CFUNC_DEF("s", 3, js_mf_s),
312 JS_CFUNC_DEF("z", 3, js_mf_z),
313 JS_CFUNC_DEF("pi", 5, js_mf_pi),
316 #include "a/pid.h"
318 static JSClassID js_pid_class_id;
320 static void js_pid_finalizer(JSRuntime *const rt, JSValue const val)
322 js_free_rt(rt, JS_GetOpaque(val, js_pid_class_id));
325 static JSClassDef js_pid_class = {"pid", .finalizer = js_pid_finalizer};
327 static JSValue js_pid_ctor(JSContext *const ctx, JSValueConst const new_target, int argc, JSValueConst *const argv)
329 (void)argc;
330 (void)argv;
331 JSValue clazz = JS_UNDEFINED;
332 a_pid_s *const self = (a_pid_s *)js_mallocz(ctx, sizeof(a_pid_s));
333 if (!self)
335 return JS_EXCEPTION;
337 self->kp = 1;
338 self->summax = +A_FLOAT_INF;
339 self->summin = -A_FLOAT_INF;
340 self->outmax = +A_FLOAT_INF;
341 self->outmin = -A_FLOAT_INF;
342 a_pid_init(self);
343 JSValue proto = JS_GetPropertyStr(ctx, new_target, "prototype");
344 if (JS_IsException(proto))
346 goto fail;
348 clazz = JS_NewObjectProtoClass(ctx, proto, js_pid_class_id);
349 JS_FreeValue(ctx, proto);
350 if (JS_IsException(clazz))
352 goto fail;
354 JS_SetOpaque(clazz, self);
355 return clazz;
356 fail:
357 js_free(ctx, self);
358 JS_FreeValue(ctx, clazz);
359 return JS_EXCEPTION;
362 static JSValue js_pid_get(JSContext *const ctx, JSValueConst const this_val, int magic)
364 a_pid_s *const self = (a_pid_s *)JS_GetOpaque2(ctx, this_val, js_pid_class_id);
365 if (!self)
367 return JS_EXCEPTION;
369 double x;
370 switch (magic)
372 case 0:
373 x = (double)self->kp;
374 break;
375 case 1:
376 x = (double)self->ki;
377 break;
378 case 2:
379 x = (double)self->kd;
380 break;
381 case 3:
382 x = (double)self->summax;
383 break;
384 case 4:
385 x = (double)self->summin;
386 break;
387 case 5:
388 x = (double)self->outmax;
389 break;
390 case 6:
391 x = (double)self->outmin;
392 break;
393 case 7:
394 x = (double)self->out;
395 break;
396 case 8:
397 x = (double)self->fdb;
398 break;
399 case 9:
400 x = (double)self->err;
401 break;
402 default:
403 return JS_UNDEFINED;
405 return JS_NewFloat64(ctx, x);
408 static JSValue js_pid_set(JSContext *const ctx, JSValueConst const this_val, JSValueConst const val, int magic)
410 a_pid_s *const self = (a_pid_s *)JS_GetOpaque2(ctx, this_val, js_pid_class_id);
411 if (!self)
413 return JS_EXCEPTION;
415 double x;
416 if (JS_ToFloat64(ctx, &x, val))
418 return JS_EXCEPTION;
420 switch (magic)
422 case 0:
423 self->kp = (a_float_t)x;
424 break;
425 case 1:
426 self->ki = (a_float_t)x;
427 break;
428 case 2:
429 self->kd = (a_float_t)x;
430 break;
431 case 3:
432 self->summax = (a_float_t)x;
433 break;
434 case 4:
435 self->summin = (a_float_t)x;
436 break;
437 case 5:
438 self->outmax = (a_float_t)x;
439 break;
440 case 6:
441 self->outmin = (a_float_t)x;
442 break;
443 default:
444 break;
446 return JS_UNDEFINED;
449 static JSValue js_pid_zero(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
451 (void)argc;
452 (void)argv;
453 a_pid_s *const self = (a_pid_s *)JS_GetOpaque2(ctx, this_val, js_pid_class_id);
454 if (!self)
456 return JS_EXCEPTION;
458 a_pid_zero(self);
459 return JS_UNDEFINED;
462 static JSValue js_pid_kpid(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
464 (void)argc;
465 a_pid_s *const self = (a_pid_s *)JS_GetOpaque2(ctx, this_val, js_pid_class_id);
466 if (!self)
468 return JS_EXCEPTION;
470 double args[] = {0, 0, 0};
471 for (unsigned int i = 0; i < A_LEN(args); ++i)
473 if (JS_ToFloat64(ctx, &args[i], argv[i]))
475 return JS_EXCEPTION;
478 a_pid_kpid(self, (a_float_t)args[0], (a_float_t)args[1], (a_float_t)args[2]);
479 return JS_UNDEFINED;
482 static JSValue js_pid_off(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
484 (void)argc;
485 a_pid_s *const self = (a_pid_s *)JS_GetOpaque2(ctx, this_val, js_pid_class_id);
486 if (!self)
488 return JS_EXCEPTION;
490 double args[] = {0, 0};
491 for (unsigned int i = 0; i < A_LEN(args); ++i)
493 if (JS_ToFloat64(ctx, &args[i], argv[i]))
495 return JS_EXCEPTION;
498 return JS_NewFloat64(ctx, (double)a_pid_off(self, (a_float_t)args[0], (a_float_t)args[1]));
501 static JSValue js_pid_pos(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
503 (void)argc;
504 a_pid_s *const self = (a_pid_s *)JS_GetOpaque2(ctx, this_val, js_pid_class_id);
505 if (!self)
507 return JS_EXCEPTION;
509 double args[] = {0, 0};
510 for (unsigned int i = 0; i < A_LEN(args); ++i)
512 if (JS_ToFloat64(ctx, &args[i], argv[i]))
514 return JS_EXCEPTION;
517 return JS_NewFloat64(ctx, (double)a_pid_pos(self, (a_float_t)args[0], (a_float_t)args[1]));
520 static JSValue js_pid_inc(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
522 (void)argc;
523 a_pid_s *const self = (a_pid_s *)JS_GetOpaque2(ctx, this_val, js_pid_class_id);
524 if (!self)
526 return JS_EXCEPTION;
528 double args[] = {0, 0};
529 for (unsigned int i = 0; i < A_LEN(args); ++i)
531 if (JS_ToFloat64(ctx, &args[i], argv[i]))
533 return JS_EXCEPTION;
536 return JS_NewFloat64(ctx, (double)a_pid_inc(self, (a_float_t)args[0], (a_float_t)args[1]));
539 static JSCFunctionListEntry const js_pid_proto[] = {
540 JS_PROP_STRING_DEF("[Symbol.toStringTag]", "a.pid", 0),
541 JS_CGETSET_MAGIC_DEF("kp", js_pid_get, js_pid_set, 0),
542 JS_CGETSET_MAGIC_DEF("ki", js_pid_get, js_pid_set, 1),
543 JS_CGETSET_MAGIC_DEF("kd", js_pid_get, js_pid_set, 2),
544 JS_CGETSET_MAGIC_DEF("summax", js_pid_get, js_pid_set, 3),
545 JS_CGETSET_MAGIC_DEF("summin", js_pid_get, js_pid_set, 4),
546 JS_CGETSET_MAGIC_DEF("outmax", js_pid_get, js_pid_set, 5),
547 JS_CGETSET_MAGIC_DEF("outmin", js_pid_get, js_pid_set, 6),
548 JS_CGETSET_MAGIC_DEF("out", js_pid_get, NULL, 7),
549 JS_CGETSET_MAGIC_DEF("fdb", js_pid_get, NULL, 8),
550 JS_CGETSET_MAGIC_DEF("err", js_pid_get, NULL, 9),
551 JS_CFUNC_DEF("kpid", 3, js_pid_kpid),
552 JS_CFUNC_DEF("zero", 0, js_pid_zero),
553 JS_CFUNC_DEF("off", 2, js_pid_off),
554 JS_CFUNC_DEF("pos", 2, js_pid_pos),
555 JS_CFUNC_DEF("inc", 2, js_pid_inc),
558 static A_INLINE int js_liba_pid_init(JSContext *const ctx, JSModuleDef *const m)
560 JS_NewClassID(&js_pid_class_id);
561 JS_NewClass(JS_GetRuntime(ctx), js_pid_class_id, &js_pid_class);
563 JSValue const proto = JS_NewObject(ctx);
564 JS_SetPropertyFunctionList(ctx, proto, js_pid_proto, A_LEN(js_pid_proto));
566 JSValue const clazz = JS_NewCFunction2(ctx, js_pid_ctor, "pid", 3, JS_CFUNC_constructor, 0);
567 JS_SetConstructor(ctx, clazz, proto);
568 JS_SetClassProto(ctx, js_pid_class_id, proto);
570 JS_DefinePropertyValueStr(ctx, clazz, "OFF", JS_NewUint32(ctx, A_PID_OFF), 0);
571 JS_DefinePropertyValueStr(ctx, clazz, "POS", JS_NewUint32(ctx, A_PID_POS), 0);
572 JS_DefinePropertyValueStr(ctx, clazz, "INC", JS_NewUint32(ctx, A_PID_INC), 0);
574 JS_SetModuleExport(ctx, m, "pid", clazz);
575 return 0;
578 #include "a/pid/fuzzy.h"
580 static JSClassID js_pid_fuzzy_class_id;
582 static void js_pid_fuzzy_finalizer(JSRuntime *const rt, JSValue const val)
584 a_pid_fuzzy_s *self = (a_pid_fuzzy_s *)JS_GetOpaque(val, js_pid_fuzzy_class_id);
585 union
587 a_float_t const *p;
588 a_float_t *o;
589 } u;
590 js_free_rt(rt, ((void)(u.p = self->me), u.o));
591 js_free_rt(rt, ((void)(u.p = self->mec), u.o));
592 js_free_rt(rt, ((void)(u.p = self->mkp), u.o));
593 js_free_rt(rt, ((void)(u.p = self->mki), u.o));
594 js_free_rt(rt, ((void)(u.p = self->mkd), u.o));
595 js_free_rt(rt, a_pid_fuzzy_joint(self));
596 js_free_rt(rt, self);
599 static JSClassDef js_pid_fuzzy_class = {"pid_fuzzy", .finalizer = js_pid_fuzzy_finalizer};
601 static JSValue js_pid_fuzzy_ctor(JSContext *const ctx, JSValueConst const new_target, int argc, JSValueConst *const argv)
603 (void)argc;
604 (void)argv;
605 JSValue clazz = JS_UNDEFINED;
606 a_pid_fuzzy_s *const self = (a_pid_fuzzy_s *)js_mallocz(ctx, sizeof(a_pid_fuzzy_s));
607 if (!self)
609 return JS_EXCEPTION;
611 self->pid.kp = 1;
612 self->pid.summax = +A_FLOAT_INF;
613 self->pid.summin = -A_FLOAT_INF;
614 self->pid.outmax = +A_FLOAT_INF;
615 self->pid.outmin = -A_FLOAT_INF;
616 self->kp = 1;
617 self->op = a_pid_fuzzy_op(A_PID_FUZZY_EQU);
618 a_pid_fuzzy_init(self);
619 JSValue proto = JS_GetPropertyStr(ctx, new_target, "prototype");
620 if (JS_IsException(proto))
622 goto fail;
624 clazz = JS_NewObjectProtoClass(ctx, proto, js_pid_fuzzy_class_id);
625 JS_FreeValue(ctx, proto);
626 if (JS_IsException(clazz))
628 goto fail;
630 JS_SetOpaque(clazz, self);
631 return clazz;
632 fail:
633 js_free(ctx, self);
634 JS_FreeValue(ctx, clazz);
635 return JS_EXCEPTION;
638 static JSValue js_pid_fuzzy_get(JSContext *const ctx, JSValueConst const this_val, int magic)
640 a_pid_fuzzy_s *const self = (a_pid_fuzzy_s *)JS_GetOpaque2(ctx, this_val, js_pid_fuzzy_class_id);
641 if (!self)
643 return JS_EXCEPTION;
645 double x;
646 switch (magic)
648 case 0:
649 return JS_NewUint32(ctx, self->joint);
650 case 1:
651 x = (double)self->kp;
652 break;
653 case 2:
654 x = (double)self->ki;
655 break;
656 case 3:
657 x = (double)self->kd;
658 break;
659 case 4:
660 x = (double)self->pid.summax;
661 break;
662 case 5:
663 x = (double)self->pid.summin;
664 break;
665 case 6:
666 x = (double)self->pid.outmax;
667 break;
668 case 7:
669 x = (double)self->pid.outmin;
670 break;
671 case 8:
672 x = (double)self->pid.out;
673 break;
674 case 9:
675 x = (double)self->pid.fdb;
676 break;
677 case 10:
678 x = (double)self->pid.err;
679 break;
680 case 11:
681 return JS_NewUint32(ctx, self->order);
682 default:
683 return JS_UNDEFINED;
685 return JS_NewFloat64(ctx, x);
688 static int js_pid_fuzzy_joint_(JSContext *const ctx, a_pid_fuzzy_s *const self, unsigned int joint)
690 void *ptr = a_pid_fuzzy_joint(self);
691 if (joint > self->joint)
693 ptr = js_realloc(ctx, ptr, A_PID_FUZZY_JOINT(joint));
694 if (!ptr)
696 return ~0;
699 a_pid_fuzzy_set_joint(self, ptr, joint);
700 return 0;
703 static JSValue js_pid_fuzzy_set(JSContext *const ctx, JSValueConst const this_val, JSValueConst const val, int magic)
705 a_pid_fuzzy_s *const self = (a_pid_fuzzy_s *)JS_GetOpaque2(ctx, this_val, js_pid_fuzzy_class_id);
706 if (!self)
708 return JS_EXCEPTION;
710 a_u32_t u;
711 if (magic == 0)
713 if (JS_ToUint32(ctx, &u, val))
715 return JS_EXCEPTION;
717 if (js_pid_fuzzy_joint_(ctx, self, (unsigned int)u))
719 return JS_EXCEPTION;
721 return JS_UNDEFINED;
723 double x;
724 if (JS_ToFloat64(ctx, &x, val))
726 return JS_EXCEPTION;
728 switch (magic)
730 case 1:
731 self->pid.kp = self->kp = (a_float_t)x;
732 break;
733 case 2:
734 self->pid.ki = self->ki = (a_float_t)x;
735 break;
736 case 3:
737 self->pid.kd = self->kd = (a_float_t)x;
738 break;
739 case 4:
740 self->pid.summax = (a_float_t)x;
741 break;
742 case 5:
743 self->pid.summin = (a_float_t)x;
744 break;
745 case 6:
746 self->pid.outmax = (a_float_t)x;
747 break;
748 case 7:
749 self->pid.outmin = (a_float_t)x;
750 break;
751 default:
752 break;
754 return JS_UNDEFINED;
757 static JSValue js_pid_fuzzy_op(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
759 (void)argc;
760 a_pid_fuzzy_s *const self = (a_pid_fuzzy_s *)JS_GetOpaque2(ctx, this_val, js_pid_fuzzy_class_id);
761 if (!self)
763 return JS_EXCEPTION;
765 a_u32_t op;
766 if (JS_ToUint32(ctx, &op, argv[0]))
768 return JS_EXCEPTION;
770 a_pid_fuzzy_set_op(self, (unsigned int)op);
771 return JS_UNDEFINED;
774 static JSValue js_pid_fuzzy_rule(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
776 (void)argc;
777 a_pid_fuzzy_s *const self = (a_pid_fuzzy_s *)JS_GetOpaque2(ctx, this_val, js_pid_fuzzy_class_id);
778 if (!self)
780 return JS_EXCEPTION;
782 union
784 a_float_t const *p;
785 a_float_t *o;
786 } u;
787 a_u32_t row;
788 a_u32_t len = 0;
789 a_u32_t order = 0;
790 JSValue res = JS_UNDEFINED;
791 if (JS_IsArray(ctx, argv[0]))
793 if (ArrayLength(ctx, argv[0], &order))
795 goto fail;
797 res = Concat(ctx, argv[0]);
798 if (ArrayLength(ctx, res, &len))
800 goto fail;
802 if (len)
804 a_float_t *const me = (a_float_t *)js_realloc(ctx, ((void)(u.p = self->me), u.o), sizeof(a_float_t) * len);
805 if (!me)
807 goto fail;
809 self->me = me;
810 if (ArrayFloat(ctx, res, me, len))
812 goto fail;
815 JS_FreeValue(ctx, res);
816 res = JS_UNDEFINED;
818 if (JS_IsArray(ctx, argv[1]))
820 if (ArrayLength(ctx, argv[1], &row) || row != order)
822 goto fail;
824 res = Concat(ctx, argv[1]);
825 if (ArrayLength(ctx, res, &len))
827 goto fail;
829 if (len)
831 a_float_t *const mec = (a_float_t *)js_realloc(ctx, ((void)(u.p = self->mec), u.o), sizeof(a_float_t) * len);
832 if (!mec)
834 goto fail;
836 self->mec = mec;
837 if (ArrayFloat(ctx, res, mec, len))
839 goto fail;
842 JS_FreeValue(ctx, res);
843 res = JS_UNDEFINED;
845 if (JS_IsArray(ctx, argv[2]))
847 if (ArrayLength(ctx, argv[2], &row) || row != order)
849 goto fail;
851 res = Concat(ctx, argv[2]);
852 if (ArrayLength(ctx, res, &len))
854 goto fail;
856 if (len)
858 a_float_t *const mkp = (a_float_t *)js_realloc(ctx, ((void)(u.p = self->mkp), u.o), sizeof(a_float_t) * len);
859 if (!mkp)
861 goto fail;
863 self->mkp = mkp;
864 if (ArrayFloat(ctx, res, mkp, len))
866 goto fail;
869 JS_FreeValue(ctx, res);
870 res = JS_UNDEFINED;
872 if (JS_IsArray(ctx, argv[3]))
874 if (ArrayLength(ctx, argv[3], &row) || row != order)
876 goto fail;
878 res = Concat(ctx, argv[3]);
879 if (ArrayLength(ctx, res, &len))
881 goto fail;
883 if (len)
885 a_float_t *const mki = (a_float_t *)js_realloc(ctx, ((void)(u.p = self->mki), u.o), sizeof(a_float_t) * len);
886 if (!mki)
888 goto fail;
890 self->mki = mki;
891 if (ArrayFloat(ctx, res, mki, len))
893 goto fail;
896 JS_FreeValue(ctx, res);
897 res = JS_UNDEFINED;
899 if (JS_IsArray(ctx, argv[4]))
901 if (ArrayLength(ctx, argv[4], &row) || row != order)
903 goto fail;
905 res = Concat(ctx, argv[4]);
906 if (ArrayLength(ctx, res, &len))
908 goto fail;
910 if (len)
912 a_float_t *const mkd = (a_float_t *)js_realloc(ctx, ((void)(u.p = self->mkd), u.o), sizeof(a_float_t) * len);
913 if (!mkd)
915 goto fail;
917 self->mkd = mkd;
918 if (ArrayFloat(ctx, res, mkd, len))
920 goto fail;
923 JS_FreeValue(ctx, res);
925 self->order = order;
926 return JS_UNDEFINED;
927 fail:
928 JS_FreeValue(ctx, res);
929 return JS_UNDEFINED;
932 static JSValue js_pid_fuzzy_joint(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
934 (void)argc;
935 a_pid_fuzzy_s *const self = (a_pid_fuzzy_s *)JS_GetOpaque2(ctx, this_val, js_pid_fuzzy_class_id);
936 if (!self)
938 return JS_EXCEPTION;
940 a_u32_t joint;
941 if (JS_ToUint32(ctx, &joint, argv[0]))
943 return JS_EXCEPTION;
945 if (js_pid_fuzzy_joint_(ctx, self, joint))
947 return JS_EXCEPTION;
949 return JS_UNDEFINED;
952 static JSValue js_pid_fuzzy_kpid(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
954 (void)argc;
955 a_pid_fuzzy_s *const self = (a_pid_fuzzy_s *)JS_GetOpaque2(ctx, this_val, js_pid_fuzzy_class_id);
956 if (!self)
958 return JS_EXCEPTION;
960 double args[] = {0, 0, 0};
961 for (unsigned int i = 0; i < A_LEN(args); ++i)
963 if (JS_ToFloat64(ctx, &args[i], argv[i]))
965 return JS_EXCEPTION;
968 a_pid_fuzzy_kpid(self, (a_float_t)args[0], (a_float_t)args[1], (a_float_t)args[2]);
969 return JS_UNDEFINED;
972 static JSValue js_pid_fuzzy_zero(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
974 (void)argc;
975 (void)argv;
976 a_pid_fuzzy_s *const self = (a_pid_fuzzy_s *)JS_GetOpaque2(ctx, this_val, js_pid_fuzzy_class_id);
977 if (!self)
979 return JS_EXCEPTION;
981 a_pid_fuzzy_zero(self);
982 return JS_UNDEFINED;
985 static JSValue js_pid_fuzzy_off(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
987 (void)argc;
988 a_pid_fuzzy_s *const self = (a_pid_fuzzy_s *)JS_GetOpaque2(ctx, this_val, js_pid_fuzzy_class_id);
989 if (!self)
991 return JS_EXCEPTION;
993 double args[] = {0, 0};
994 for (unsigned int i = 0; i < A_LEN(args); ++i)
996 if (JS_ToFloat64(ctx, &args[i], argv[i]))
998 return JS_EXCEPTION;
1001 return JS_NewFloat64(ctx, (double)a_pid_fuzzy_off(self, (a_float_t)args[0], (a_float_t)args[1]));
1004 static JSValue js_pid_fuzzy_pos(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
1006 (void)argc;
1007 a_pid_fuzzy_s *const self = (a_pid_fuzzy_s *)JS_GetOpaque2(ctx, this_val, js_pid_fuzzy_class_id);
1008 if (!self)
1010 return JS_EXCEPTION;
1012 double args[] = {0, 0};
1013 for (unsigned int i = 0; i < A_LEN(args); ++i)
1015 if (JS_ToFloat64(ctx, &args[i], argv[i]))
1017 return JS_EXCEPTION;
1020 return JS_NewFloat64(ctx, (double)a_pid_fuzzy_pos(self, (a_float_t)args[0], (a_float_t)args[1]));
1023 static JSValue js_pid_fuzzy_inc(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
1025 (void)argc;
1026 a_pid_fuzzy_s *const self = (a_pid_fuzzy_s *)JS_GetOpaque2(ctx, this_val, js_pid_fuzzy_class_id);
1027 if (!self)
1029 return JS_EXCEPTION;
1031 double args[] = {0, 0};
1032 for (unsigned int i = 0; i < A_LEN(args); ++i)
1034 if (JS_ToFloat64(ctx, &args[i], argv[i]))
1036 return JS_EXCEPTION;
1039 return JS_NewFloat64(ctx, (double)a_pid_fuzzy_inc(self, (a_float_t)args[0], (a_float_t)args[1]));
1042 static JSCFunctionListEntry const js_pid_fuzzy_proto[] = {
1043 JS_PROP_STRING_DEF("[Symbol.toStringTag]", "a.pid.fuzzy", 0),
1044 JS_CGETSET_MAGIC_DEF("joint", js_pid_fuzzy_get, js_pid_fuzzy_set, 0),
1045 JS_CGETSET_MAGIC_DEF("kp", js_pid_fuzzy_get, js_pid_fuzzy_set, 1),
1046 JS_CGETSET_MAGIC_DEF("ki", js_pid_fuzzy_get, js_pid_fuzzy_set, 2),
1047 JS_CGETSET_MAGIC_DEF("kd", js_pid_fuzzy_get, js_pid_fuzzy_set, 3),
1048 JS_CGETSET_MAGIC_DEF("summax", js_pid_fuzzy_get, js_pid_fuzzy_set, 4),
1049 JS_CGETSET_MAGIC_DEF("summin", js_pid_fuzzy_get, js_pid_fuzzy_set, 5),
1050 JS_CGETSET_MAGIC_DEF("outmax", js_pid_fuzzy_get, js_pid_fuzzy_set, 6),
1051 JS_CGETSET_MAGIC_DEF("outmin", js_pid_fuzzy_get, js_pid_fuzzy_set, 7),
1052 JS_CGETSET_MAGIC_DEF("out", js_pid_fuzzy_get, NULL, 8),
1053 JS_CGETSET_MAGIC_DEF("fdb", js_pid_fuzzy_get, NULL, 9),
1054 JS_CGETSET_MAGIC_DEF("err", js_pid_fuzzy_get, NULL, 10),
1055 JS_CGETSET_MAGIC_DEF("order", js_pid_fuzzy_get, NULL, 11),
1056 JS_CFUNC_DEF("op", 1, js_pid_fuzzy_op),
1057 JS_CFUNC_DEF("rule", 5, js_pid_fuzzy_rule),
1058 JS_CFUNC_DEF("set_joint", 1, js_pid_fuzzy_joint),
1059 JS_CFUNC_DEF("kpid", 3, js_pid_fuzzy_kpid),
1060 JS_CFUNC_DEF("zero", 0, js_pid_fuzzy_zero),
1061 JS_CFUNC_DEF("off", 2, js_pid_fuzzy_off),
1062 JS_CFUNC_DEF("pos", 2, js_pid_fuzzy_pos),
1063 JS_CFUNC_DEF("inc", 2, js_pid_fuzzy_inc),
1066 static A_INLINE int js_liba_pid_fuzzy_init(JSContext *const ctx, JSModuleDef *const m)
1068 JS_NewClassID(&js_pid_fuzzy_class_id);
1069 JS_NewClass(JS_GetRuntime(ctx), js_pid_fuzzy_class_id, &js_pid_fuzzy_class);
1071 JSValue const proto = JS_NewObject(ctx);
1072 JS_SetPropertyFunctionList(ctx, proto, js_pid_fuzzy_proto, A_LEN(js_pid_fuzzy_proto));
1074 JSValue const clazz = JS_NewCFunction2(ctx, js_pid_fuzzy_ctor, "pid_fuzzy", 3, JS_CFUNC_constructor, 0);
1075 JS_SetConstructor(ctx, clazz, proto);
1076 JS_SetClassProto(ctx, js_pid_fuzzy_class_id, proto);
1078 JS_DefinePropertyValueStr(ctx, clazz, "CAP", JS_NewUint32(ctx, A_PID_FUZZY_CAP), 0);
1079 JS_DefinePropertyValueStr(ctx, clazz, "CAP_ALGEBRA", JS_NewUint32(ctx, A_PID_FUZZY_CAP_ALGEBRA), 0);
1080 JS_DefinePropertyValueStr(ctx, clazz, "CAP_BOUNDED", JS_NewUint32(ctx, A_PID_FUZZY_CAP_BOUNDED), 0);
1081 JS_DefinePropertyValueStr(ctx, clazz, "CUP", JS_NewUint32(ctx, A_PID_FUZZY_CUP), 0);
1082 JS_DefinePropertyValueStr(ctx, clazz, "CUP_ALGEBRA", JS_NewUint32(ctx, A_PID_FUZZY_CUP_ALGEBRA), 0);
1083 JS_DefinePropertyValueStr(ctx, clazz, "CUP_BOUNDED", JS_NewUint32(ctx, A_PID_FUZZY_CUP_BOUNDED), 0);
1084 JS_DefinePropertyValueStr(ctx, clazz, "EQU", JS_NewUint32(ctx, A_PID_FUZZY_EQU), 0);
1086 JS_SetModuleExport(ctx, m, "pid_fuzzy", clazz);
1087 return 0;
1090 #include "a/pid/neuron.h"
1092 static JSClassID js_pid_neuron_class_id;
1094 static void js_pid_neuron_finalizer(JSRuntime *const rt, JSValue const val)
1096 js_free_rt(rt, JS_GetOpaque(val, js_pid_neuron_class_id));
1099 static JSClassDef js_pid_neuron_class = {"pid_neuron", .finalizer = js_pid_neuron_finalizer};
1101 static JSValue js_pid_neuron_ctor(JSContext *const ctx, JSValueConst const new_target, int argc, JSValueConst *const argv)
1103 (void)argc;
1104 (void)argv;
1105 JSValue clazz = JS_UNDEFINED;
1106 a_pid_neuron_s *const self = (a_pid_neuron_s *)js_mallocz(ctx, sizeof(a_pid_neuron_s));
1107 if (!self)
1109 return JS_EXCEPTION;
1111 self->pid.summax = +A_FLOAT_INF;
1112 self->pid.summin = -A_FLOAT_INF;
1113 self->pid.outmax = +A_FLOAT_INF;
1114 self->pid.outmin = -A_FLOAT_INF;
1115 self->k = 1;
1116 self->wp = A_FLOAT_C(0.1);
1117 self->wi = A_FLOAT_C(0.1);
1118 self->wd = A_FLOAT_C(0.1);
1119 a_pid_neuron_init(self);
1120 JSValue proto = JS_GetPropertyStr(ctx, new_target, "prototype");
1121 if (JS_IsException(proto))
1123 goto fail;
1125 clazz = JS_NewObjectProtoClass(ctx, proto, js_pid_neuron_class_id);
1126 JS_FreeValue(ctx, proto);
1127 if (JS_IsException(clazz))
1129 goto fail;
1131 JS_SetOpaque(clazz, self);
1132 return clazz;
1133 fail:
1134 js_free(ctx, self);
1135 JS_FreeValue(ctx, clazz);
1136 return JS_EXCEPTION;
1139 static JSValue js_pid_neuron_get(JSContext *const ctx, JSValueConst const this_val, int magic)
1141 a_pid_neuron_s *const self = (a_pid_neuron_s *)JS_GetOpaque2(ctx, this_val, js_pid_neuron_class_id);
1142 if (!self)
1144 return JS_EXCEPTION;
1146 double x;
1147 switch (magic)
1149 case 0:
1150 x = (double)self->k;
1151 break;
1152 case 1:
1153 x = (double)self->pid.kp;
1154 break;
1155 case 2:
1156 x = (double)self->pid.ki;
1157 break;
1158 case 3:
1159 x = (double)self->pid.kd;
1160 break;
1161 case 4:
1162 x = (double)self->wp;
1163 break;
1164 case 5:
1165 x = (double)self->wi;
1166 break;
1167 case 6:
1168 x = (double)self->wd;
1169 break;
1170 case 7:
1171 x = (double)self->pid.outmax;
1172 break;
1173 case 8:
1174 x = (double)self->pid.outmin;
1175 break;
1176 case 9:
1177 x = (double)self->pid.out;
1178 break;
1179 case 10:
1180 x = (double)self->pid.fdb;
1181 break;
1182 case 11:
1183 x = (double)self->pid.err;
1184 break;
1185 case 12:
1186 x = (double)self->ec;
1187 break;
1188 default:
1189 return JS_UNDEFINED;
1191 return JS_NewFloat64(ctx, x);
1194 static JSValue js_pid_neuron_set(JSContext *const ctx, JSValueConst const this_val, JSValueConst const val, int magic)
1196 a_pid_neuron_s *const self = (a_pid_neuron_s *)JS_GetOpaque2(ctx, this_val, js_pid_neuron_class_id);
1197 if (!self)
1199 return JS_EXCEPTION;
1201 double x;
1202 if (JS_ToFloat64(ctx, &x, val))
1204 return JS_EXCEPTION;
1206 switch (magic)
1208 case 0:
1209 self->k = (a_float_t)x;
1210 break;
1211 case 1:
1212 self->pid.kp = (a_float_t)x;
1213 break;
1214 case 2:
1215 self->pid.ki = (a_float_t)x;
1216 break;
1217 case 3:
1218 self->pid.kd = (a_float_t)x;
1219 break;
1220 case 4:
1221 self->wp = (a_float_t)x;
1222 break;
1223 case 5:
1224 self->wi = (a_float_t)x;
1225 break;
1226 case 6:
1227 self->wd = (a_float_t)x;
1228 break;
1229 case 7:
1230 self->pid.outmax = (a_float_t)x;
1231 break;
1232 case 8:
1233 self->pid.outmin = (a_float_t)x;
1234 break;
1235 default:
1236 break;
1238 return JS_UNDEFINED;
1241 static JSValue js_pid_neuron_kpid(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
1243 (void)argc;
1244 a_pid_neuron_s *const self = (a_pid_neuron_s *)JS_GetOpaque2(ctx, this_val, js_pid_neuron_class_id);
1245 if (!self)
1247 return JS_EXCEPTION;
1249 double args[] = {0, 0, 0, 0};
1250 for (unsigned int i = 0; i < A_LEN(args); ++i)
1252 if (JS_ToFloat64(ctx, &args[i], argv[i]))
1254 return JS_EXCEPTION;
1257 a_pid_neuron_kpid(self, (a_float_t)args[0], (a_float_t)args[1], (a_float_t)args[2], (a_float_t)args[3]);
1258 return JS_UNDEFINED;
1261 static JSValue js_pid_neuron_wpid(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
1263 (void)argc;
1264 a_pid_neuron_s *const self = (a_pid_neuron_s *)JS_GetOpaque2(ctx, this_val, js_pid_neuron_class_id);
1265 if (!self)
1267 return JS_EXCEPTION;
1269 double args[] = {0, 0, 0};
1270 for (unsigned int i = 0; i < A_LEN(args); ++i)
1272 if (JS_ToFloat64(ctx, &args[i], argv[i]))
1274 return JS_EXCEPTION;
1277 a_pid_neuron_wpid(self, (a_float_t)args[0], (a_float_t)args[1], (a_float_t)args[2]);
1278 return JS_UNDEFINED;
1281 static JSValue js_pid_neuron_zero(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
1283 (void)argc;
1284 (void)argv;
1285 a_pid_neuron_s *const self = (a_pid_neuron_s *)JS_GetOpaque2(ctx, this_val, js_pid_neuron_class_id);
1286 if (!self)
1288 return JS_EXCEPTION;
1290 a_pid_neuron_zero(self);
1291 return JS_UNDEFINED;
1294 static JSValue js_pid_neuron_off(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
1296 (void)argc;
1297 a_pid_neuron_s *const self = (a_pid_neuron_s *)JS_GetOpaque2(ctx, this_val, js_pid_neuron_class_id);
1298 if (!self)
1300 return JS_EXCEPTION;
1302 double args[] = {0, 0};
1303 for (unsigned int i = 0; i < A_LEN(args); ++i)
1305 if (JS_ToFloat64(ctx, &args[i], argv[i]))
1307 return JS_EXCEPTION;
1310 return JS_NewFloat64(ctx, (double)a_pid_neuron_off(self, (a_float_t)args[0], (a_float_t)args[1]));
1313 static JSValue js_pid_neuron_inc(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
1315 (void)argc;
1316 a_pid_neuron_s *const self = (a_pid_neuron_s *)JS_GetOpaque2(ctx, this_val, js_pid_neuron_class_id);
1317 if (!self)
1319 return JS_EXCEPTION;
1321 double args[] = {0, 0};
1322 for (unsigned int i = 0; i < A_LEN(args); ++i)
1324 if (JS_ToFloat64(ctx, &args[i], argv[i]))
1326 return JS_EXCEPTION;
1329 return JS_NewFloat64(ctx, (double)a_pid_neuron_inc(self, (a_float_t)args[0], (a_float_t)args[1]));
1332 static JSCFunctionListEntry const js_pid_neuron_proto[] = {
1333 JS_PROP_STRING_DEF("[Symbol.toStringTag]", "a.pid.neuron", 0),
1334 JS_CGETSET_MAGIC_DEF("k", js_pid_neuron_get, js_pid_neuron_set, 0),
1335 JS_CGETSET_MAGIC_DEF("kp", js_pid_neuron_get, js_pid_neuron_set, 1),
1336 JS_CGETSET_MAGIC_DEF("ki", js_pid_neuron_get, js_pid_neuron_set, 2),
1337 JS_CGETSET_MAGIC_DEF("kd", js_pid_neuron_get, js_pid_neuron_set, 3),
1338 JS_CGETSET_MAGIC_DEF("wp", js_pid_neuron_get, js_pid_neuron_set, 4),
1339 JS_CGETSET_MAGIC_DEF("wi", js_pid_neuron_get, js_pid_neuron_set, 5),
1340 JS_CGETSET_MAGIC_DEF("wd", js_pid_neuron_get, js_pid_neuron_set, 6),
1341 JS_CGETSET_MAGIC_DEF("outmax", js_pid_neuron_get, js_pid_neuron_set, 7),
1342 JS_CGETSET_MAGIC_DEF("outmin", js_pid_neuron_get, js_pid_neuron_set, 8),
1343 JS_CGETSET_MAGIC_DEF("out", js_pid_neuron_get, NULL, 9),
1344 JS_CGETSET_MAGIC_DEF("fdb", js_pid_neuron_get, NULL, 10),
1345 JS_CGETSET_MAGIC_DEF("err", js_pid_neuron_get, NULL, 11),
1346 JS_CGETSET_MAGIC_DEF("ec", js_pid_neuron_get, NULL, 12),
1347 JS_CFUNC_DEF("kpid", 4, js_pid_neuron_kpid),
1348 JS_CFUNC_DEF("wpid", 3, js_pid_neuron_wpid),
1349 JS_CFUNC_DEF("zero", 0, js_pid_neuron_zero),
1350 JS_CFUNC_DEF("off", 2, js_pid_neuron_off),
1351 JS_CFUNC_DEF("inc", 2, js_pid_neuron_inc),
1354 static A_INLINE int js_liba_pid_neuron_init(JSContext *const ctx, JSModuleDef *const m)
1356 JS_NewClassID(&js_pid_neuron_class_id);
1357 JS_NewClass(JS_GetRuntime(ctx), js_pid_neuron_class_id, &js_pid_neuron_class);
1359 JSValue const proto = JS_NewObject(ctx);
1360 JS_SetPropertyFunctionList(ctx, proto, js_pid_neuron_proto, A_LEN(js_pid_neuron_proto));
1362 JSValue const clazz = JS_NewCFunction2(ctx, js_pid_neuron_ctor, "pid_neuron", 3, JS_CFUNC_constructor, 0);
1363 JS_SetConstructor(ctx, clazz, proto);
1364 JS_SetClassProto(ctx, js_pid_neuron_class_id, proto);
1366 JS_SetModuleExport(ctx, m, "pid_neuron", clazz);
1367 return 0;
1370 #include "a/polytraj.h"
1372 static JSClassID js_polytraj3_class_id;
1374 static void js_polytraj3_finalizer(JSRuntime *const rt, JSValue const val)
1376 js_free_rt(rt, JS_GetOpaque(val, js_polytraj3_class_id));
1379 static JSClassDef js_polytraj3_class = {"polytraj3", .finalizer = js_polytraj3_finalizer};
1381 static JSValue js_polytraj3_ctor(JSContext *const ctx, JSValueConst const new_target, int argc, JSValueConst *const argv)
1383 JSValue clazz = JS_UNDEFINED;
1384 a_polytraj3_s *const self = (a_polytraj3_s *)js_mallocz(ctx, sizeof(a_polytraj3_s));
1385 if (!self)
1387 return JS_EXCEPTION;
1389 double args[] = {0, 0, 0, 0, 0, 0};
1390 if (argc > (int)A_LEN(args))
1392 argc = (int)A_LEN(args);
1394 for (int i = 0; i < 4; ++i)
1396 if (JS_ToFloat64(ctx, &args[i], argv[i]))
1398 goto fail;
1401 for (int i = 4; i < argc; ++i)
1403 if (JS_ToFloat64(ctx, &args[i], argv[i]))
1405 goto fail;
1408 a_polytraj3_gen(self,
1409 (a_float_t)args[0], (a_float_t)args[1],
1410 (a_float_t)args[2], (a_float_t)args[3],
1411 (a_float_t)args[4], (a_float_t)args[5]);
1412 JSValue proto = JS_GetPropertyStr(ctx, new_target, "prototype");
1413 if (JS_IsException(proto))
1415 goto fail;
1417 clazz = JS_NewObjectProtoClass(ctx, proto, js_polytraj3_class_id);
1418 JS_FreeValue(ctx, proto);
1419 if (JS_IsException(clazz))
1421 goto fail;
1423 JS_SetOpaque(clazz, self);
1424 return clazz;
1425 fail:
1426 js_free(ctx, self);
1427 JS_FreeValue(ctx, clazz);
1428 return JS_EXCEPTION;
1431 static JSValue js_polytraj3_gen(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
1433 a_polytraj3_s *const self = (a_polytraj3_s *)JS_GetOpaque2(ctx, this_val, js_polytraj3_class_id);
1434 if (!self)
1436 return JS_EXCEPTION;
1438 double args[] = {0, 0, 0, 0, 0, 0};
1439 if (argc > (int)A_LEN(args))
1441 argc = (int)A_LEN(args);
1443 for (int i = 0; i < 4; ++i)
1445 if (JS_ToFloat64(ctx, &args[i], argv[i]))
1447 return JS_EXCEPTION;
1450 for (int i = 4; i < argc; ++i)
1452 if (JS_ToFloat64(ctx, &args[i], argv[i]))
1454 return JS_EXCEPTION;
1457 a_polytraj3_gen(self,
1458 (a_float_t)args[0], (a_float_t)args[1],
1459 (a_float_t)args[2], (a_float_t)args[3],
1460 (a_float_t)args[4], (a_float_t)args[5]);
1461 return JS_UNDEFINED;
1464 static JSValue js_polytraj3_pos(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
1466 (void)argc;
1467 a_polytraj3_s *const self = (a_polytraj3_s *)JS_GetOpaque2(ctx, this_val, js_polytraj3_class_id);
1468 if (!self)
1470 return JS_EXCEPTION;
1472 double dt;
1473 if (JS_ToFloat64(ctx, &dt, argv[0]))
1475 return JS_EXCEPTION;
1477 a_float_t pos = a_polytraj3_pos(self, (a_float_t)dt);
1478 return JS_NewFloat64(ctx, (double)pos);
1481 static JSValue js_polytraj3_vel(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
1483 (void)argc;
1484 a_polytraj3_s *const self = (a_polytraj3_s *)JS_GetOpaque2(ctx, this_val, js_polytraj3_class_id);
1485 if (!self)
1487 return JS_EXCEPTION;
1489 double dt;
1490 if (JS_ToFloat64(ctx, &dt, argv[0]))
1492 return JS_EXCEPTION;
1494 a_float_t vel = a_polytraj3_vel(self, (a_float_t)dt);
1495 return JS_NewFloat64(ctx, (double)vel);
1498 static JSValue js_polytraj3_acc(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
1500 (void)argc;
1501 a_polytraj3_s *const self = (a_polytraj3_s *)JS_GetOpaque2(ctx, this_val, js_polytraj3_class_id);
1502 if (!self)
1504 return JS_EXCEPTION;
1506 double dt;
1507 if (JS_ToFloat64(ctx, &dt, argv[0]))
1509 return JS_EXCEPTION;
1511 a_float_t acc = a_polytraj3_acc(self, (a_float_t)dt);
1512 return JS_NewFloat64(ctx, (double)acc);
1515 static JSCFunctionListEntry const js_polytraj3_proto[] = {
1516 JS_PROP_STRING_DEF("[Symbol.toStringTag]", "a.polytraj3", 0),
1517 JS_CFUNC_DEF("gen", 6, js_polytraj3_gen),
1518 JS_CFUNC_DEF("pos", 1, js_polytraj3_pos),
1519 JS_CFUNC_DEF("vel", 1, js_polytraj3_vel),
1520 JS_CFUNC_DEF("acc", 1, js_polytraj3_acc),
1523 static A_INLINE int js_liba_polytraj3_init(JSContext *const ctx, JSModuleDef *const m)
1525 JS_NewClassID(&js_polytraj3_class_id);
1526 JS_NewClass(JS_GetRuntime(ctx), js_polytraj3_class_id, &js_polytraj3_class);
1528 JSValue const proto = JS_NewObject(ctx);
1529 JS_SetPropertyFunctionList(ctx, proto, js_polytraj3_proto, A_LEN(js_polytraj3_proto));
1531 JSValue const clazz = JS_NewCFunction2(ctx, js_polytraj3_ctor, "polytraj3", 6, JS_CFUNC_constructor, 0);
1532 JS_SetConstructor(ctx, clazz, proto);
1533 JS_SetClassProto(ctx, js_polytraj3_class_id, proto);
1535 JS_SetModuleExport(ctx, m, "polytraj3", clazz);
1536 return 0;
1539 static JSClassID js_polytraj5_class_id;
1541 static void js_polytraj5_finalizer(JSRuntime *const rt, JSValue const val)
1543 js_free_rt(rt, JS_GetOpaque(val, js_polytraj5_class_id));
1546 static JSClassDef js_polytraj5_class = {"polytraj5", .finalizer = js_polytraj5_finalizer};
1548 static JSValue js_polytraj5_ctor(JSContext *const ctx, JSValueConst const new_target, int argc, JSValueConst *const argv)
1550 JSValue clazz = JS_UNDEFINED;
1551 a_polytraj5_s *const self = (a_polytraj5_s *)js_mallocz(ctx, sizeof(a_polytraj5_s));
1552 if (!self)
1554 return JS_EXCEPTION;
1556 double args[] = {0, 0, 0, 0, 0, 0, 0, 0};
1557 if (argc > (int)A_LEN(args))
1559 argc = (int)A_LEN(args);
1561 for (int i = 0; i < 4; ++i)
1563 if (JS_ToFloat64(ctx, &args[i], argv[i]))
1565 goto fail;
1568 for (int i = 4; i < argc; ++i)
1570 if (JS_ToFloat64(ctx, &args[i], argv[i]))
1572 goto fail;
1575 a_polytraj5_gen(self,
1576 (a_float_t)args[0], (a_float_t)args[1],
1577 (a_float_t)args[2], (a_float_t)args[3],
1578 (a_float_t)args[4], (a_float_t)args[5],
1579 (a_float_t)args[6], (a_float_t)args[7]);
1580 JSValue proto = JS_GetPropertyStr(ctx, new_target, "prototype");
1581 if (JS_IsException(proto))
1583 goto fail;
1585 clazz = JS_NewObjectProtoClass(ctx, proto, js_polytraj5_class_id);
1586 JS_FreeValue(ctx, proto);
1587 if (JS_IsException(clazz))
1589 goto fail;
1591 JS_SetOpaque(clazz, self);
1592 return clazz;
1593 fail:
1594 js_free(ctx, self);
1595 JS_FreeValue(ctx, clazz);
1596 return JS_EXCEPTION;
1599 static JSValue js_polytraj5_gen(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
1601 a_polytraj5_s *const self = (a_polytraj5_s *)JS_GetOpaque2(ctx, this_val, js_polytraj5_class_id);
1602 if (!self)
1604 return JS_EXCEPTION;
1606 double args[] = {0, 0, 0, 0, 0, 0, 0, 0};
1607 if (argc > (int)A_LEN(args))
1609 argc = (int)A_LEN(args);
1611 for (int i = 0; i < 4; ++i)
1613 if (JS_ToFloat64(ctx, &args[i], argv[i]))
1615 return JS_EXCEPTION;
1618 for (int i = 4; i < argc; ++i)
1620 if (JS_ToFloat64(ctx, &args[i], argv[i]))
1622 return JS_EXCEPTION;
1625 a_polytraj5_gen(self,
1626 (a_float_t)args[0], (a_float_t)args[1],
1627 (a_float_t)args[2], (a_float_t)args[3],
1628 (a_float_t)args[4], (a_float_t)args[5],
1629 (a_float_t)args[6], (a_float_t)args[7]);
1630 return JS_UNDEFINED;
1633 static JSValue js_polytraj5_pos(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
1635 (void)argc;
1636 a_polytraj5_s *const self = (a_polytraj5_s *)JS_GetOpaque2(ctx, this_val, js_polytraj5_class_id);
1637 if (!self)
1639 return JS_EXCEPTION;
1641 double dt;
1642 if (JS_ToFloat64(ctx, &dt, argv[0]))
1644 return JS_EXCEPTION;
1646 a_float_t pos = a_polytraj5_pos(self, (a_float_t)dt);
1647 return JS_NewFloat64(ctx, (double)pos);
1650 static JSValue js_polytraj5_vel(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
1652 (void)argc;
1653 a_polytraj5_s *const self = (a_polytraj5_s *)JS_GetOpaque2(ctx, this_val, js_polytraj5_class_id);
1654 if (!self)
1656 return JS_EXCEPTION;
1658 double dt;
1659 if (JS_ToFloat64(ctx, &dt, argv[0]))
1661 return JS_EXCEPTION;
1663 a_float_t vel = a_polytraj5_vel(self, (a_float_t)dt);
1664 return JS_NewFloat64(ctx, (double)vel);
1667 static JSValue js_polytraj5_acc(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
1669 (void)argc;
1670 a_polytraj5_s *const self = (a_polytraj5_s *)JS_GetOpaque2(ctx, this_val, js_polytraj5_class_id);
1671 if (!self)
1673 return JS_EXCEPTION;
1675 double dt;
1676 if (JS_ToFloat64(ctx, &dt, argv[0]))
1678 return JS_EXCEPTION;
1680 a_float_t acc = a_polytraj5_acc(self, (a_float_t)dt);
1681 return JS_NewFloat64(ctx, (double)acc);
1684 static JSCFunctionListEntry const js_polytraj5_proto[] = {
1685 JS_PROP_STRING_DEF("[Symbol.toStringTag]", "a.polytraj5", 0),
1686 JS_CFUNC_DEF("gen", 8, js_polytraj5_gen),
1687 JS_CFUNC_DEF("pos", 1, js_polytraj5_pos),
1688 JS_CFUNC_DEF("vel", 1, js_polytraj5_vel),
1689 JS_CFUNC_DEF("acc", 1, js_polytraj5_acc),
1692 static A_INLINE int js_liba_polytraj5_init(JSContext *const ctx, JSModuleDef *const m)
1694 JS_NewClassID(&js_polytraj5_class_id);
1695 JS_NewClass(JS_GetRuntime(ctx), js_polytraj5_class_id, &js_polytraj5_class);
1697 JSValue const proto = JS_NewObject(ctx);
1698 JS_SetPropertyFunctionList(ctx, proto, js_polytraj5_proto, A_LEN(js_polytraj5_proto));
1700 JSValue const clazz = JS_NewCFunction2(ctx, js_polytraj5_ctor, "polytraj5", 8, JS_CFUNC_constructor, 0);
1701 JS_SetConstructor(ctx, clazz, proto);
1702 JS_SetClassProto(ctx, js_polytraj5_class_id, proto);
1704 JS_SetModuleExport(ctx, m, "polytraj5", clazz);
1705 return 0;
1708 static JSClassID js_polytraj7_class_id;
1710 static void js_polytraj7_finalizer(JSRuntime *const rt, JSValue const val)
1712 js_free_rt(rt, JS_GetOpaque(val, js_polytraj7_class_id));
1715 static JSClassDef js_polytraj7_class = {"polytraj7", .finalizer = js_polytraj7_finalizer};
1717 static JSValue js_polytraj7_ctor(JSContext *const ctx, JSValueConst const new_target, int argc, JSValueConst *const argv)
1719 JSValue clazz = JS_UNDEFINED;
1720 a_polytraj7_s *const self = (a_polytraj7_s *)js_mallocz(ctx, sizeof(a_polytraj7_s));
1721 if (!self)
1723 return JS_EXCEPTION;
1725 double args[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
1726 if (argc > (int)A_LEN(args))
1728 argc = (int)A_LEN(args);
1730 for (int i = 0; i < 4; ++i)
1732 if (JS_ToFloat64(ctx, &args[i], argv[i]))
1734 goto fail;
1737 for (int i = 4; i < argc; ++i)
1739 if (JS_ToFloat64(ctx, &args[i], argv[i]))
1741 goto fail;
1744 a_polytraj7_gen(self,
1745 (a_float_t)args[0], (a_float_t)args[1],
1746 (a_float_t)args[2], (a_float_t)args[3],
1747 (a_float_t)args[4], (a_float_t)args[5],
1748 (a_float_t)args[6], (a_float_t)args[7],
1749 (a_float_t)args[8], (a_float_t)args[9]);
1750 JSValue proto = JS_GetPropertyStr(ctx, new_target, "prototype");
1751 if (JS_IsException(proto))
1753 goto fail;
1755 clazz = JS_NewObjectProtoClass(ctx, proto, js_polytraj7_class_id);
1756 JS_FreeValue(ctx, proto);
1757 if (JS_IsException(clazz))
1759 goto fail;
1761 JS_SetOpaque(clazz, self);
1762 return clazz;
1763 fail:
1764 js_free(ctx, self);
1765 JS_FreeValue(ctx, clazz);
1766 return JS_EXCEPTION;
1769 static JSValue js_polytraj7_gen(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
1771 a_polytraj7_s *const self = (a_polytraj7_s *)JS_GetOpaque2(ctx, this_val, js_polytraj7_class_id);
1772 if (!self)
1774 return JS_EXCEPTION;
1776 double args[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
1777 if (argc > (int)A_LEN(args))
1779 argc = (int)A_LEN(args);
1781 for (int i = 0; i < 4; ++i)
1783 if (JS_ToFloat64(ctx, &args[i], argv[i]))
1785 return JS_EXCEPTION;
1788 for (int i = 4; i < argc; ++i)
1790 if (JS_ToFloat64(ctx, &args[i], argv[i]))
1792 return JS_EXCEPTION;
1795 a_polytraj7_gen(self,
1796 (a_float_t)args[0], (a_float_t)args[1],
1797 (a_float_t)args[2], (a_float_t)args[3],
1798 (a_float_t)args[4], (a_float_t)args[5],
1799 (a_float_t)args[6], (a_float_t)args[7],
1800 (a_float_t)args[8], (a_float_t)args[9]);
1801 return JS_UNDEFINED;
1804 static JSValue js_polytraj7_pos(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
1806 (void)argc;
1807 a_polytraj7_s *const self = (a_polytraj7_s *)JS_GetOpaque2(ctx, this_val, js_polytraj7_class_id);
1808 if (!self)
1810 return JS_EXCEPTION;
1812 double dt;
1813 if (JS_ToFloat64(ctx, &dt, argv[0]))
1815 return JS_EXCEPTION;
1817 a_float_t pos = a_polytraj7_pos(self, (a_float_t)dt);
1818 return JS_NewFloat64(ctx, (double)pos);
1821 static JSValue js_polytraj7_vel(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
1823 (void)argc;
1824 a_polytraj7_s *const self = (a_polytraj7_s *)JS_GetOpaque2(ctx, this_val, js_polytraj7_class_id);
1825 if (!self)
1827 return JS_EXCEPTION;
1829 double dt;
1830 if (JS_ToFloat64(ctx, &dt, argv[0]))
1832 return JS_EXCEPTION;
1834 a_float_t vel = a_polytraj7_vel(self, (a_float_t)dt);
1835 return JS_NewFloat64(ctx, (double)vel);
1838 static JSValue js_polytraj7_acc(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
1840 (void)argc;
1841 a_polytraj7_s *const self = (a_polytraj7_s *)JS_GetOpaque2(ctx, this_val, js_polytraj7_class_id);
1842 if (!self)
1844 return JS_EXCEPTION;
1846 double dt;
1847 if (JS_ToFloat64(ctx, &dt, argv[0]))
1849 return JS_EXCEPTION;
1851 a_float_t acc = a_polytraj7_acc(self, (a_float_t)dt);
1852 return JS_NewFloat64(ctx, (double)acc);
1855 static JSValue js_polytraj7_jer(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
1857 (void)argc;
1858 a_polytraj7_s *const self = (a_polytraj7_s *)JS_GetOpaque2(ctx, this_val, js_polytraj7_class_id);
1859 if (!self)
1861 return JS_EXCEPTION;
1863 double dt;
1864 if (JS_ToFloat64(ctx, &dt, argv[0]))
1866 return JS_EXCEPTION;
1868 a_float_t jer = a_polytraj7_jer(self, (a_float_t)dt);
1869 return JS_NewFloat64(ctx, (double)jer);
1872 static JSCFunctionListEntry const js_polytraj7_proto[] = {
1873 JS_PROP_STRING_DEF("[Symbol.toStringTag]", "a.polytraj7", 0),
1874 JS_CFUNC_DEF("gen", 10, js_polytraj7_gen),
1875 JS_CFUNC_DEF("pos", 1, js_polytraj7_pos),
1876 JS_CFUNC_DEF("vel", 1, js_polytraj7_vel),
1877 JS_CFUNC_DEF("acc", 1, js_polytraj7_acc),
1878 JS_CFUNC_DEF("jer", 1, js_polytraj7_jer),
1881 static A_INLINE int js_liba_polytraj7_init(JSContext *const ctx, JSModuleDef *const m)
1883 JS_NewClassID(&js_polytraj7_class_id);
1884 JS_NewClass(JS_GetRuntime(ctx), js_polytraj7_class_id, &js_polytraj7_class);
1886 JSValue const proto = JS_NewObject(ctx);
1887 JS_SetPropertyFunctionList(ctx, proto, js_polytraj7_proto, A_LEN(js_polytraj7_proto));
1889 JSValue const clazz = JS_NewCFunction2(ctx, js_polytraj7_ctor, "polytraj7", 10, JS_CFUNC_constructor, 0);
1890 JS_SetConstructor(ctx, clazz, proto);
1891 JS_SetClassProto(ctx, js_polytraj7_class_id, proto);
1893 JS_SetModuleExport(ctx, m, "polytraj7", clazz);
1894 return 0;
1897 #include "a/tf.h"
1899 static JSClassID js_tf_class_id;
1901 static void js_tf_finalizer(JSRuntime *const rt, JSValue const val)
1903 a_tf_s *const self = (a_tf_s *)JS_GetOpaque(val, js_tf_class_id);
1904 js_free_rt(rt, self->output);
1905 js_free_rt(rt, self->input);
1906 js_free_rt(rt, self);
1909 static JSClassDef js_tf_class = {"tf", .finalizer = js_tf_finalizer};
1911 static int js_tf_set_num(JSContext *const ctx, a_tf_s *const self, JSValueConst num)
1913 a_u32_t num_n = 0;
1914 a_float_t *num_p = self->input;
1915 int ret = ArrayLength(ctx, num, &num_n);
1916 if (ret)
1918 return ret;
1920 if (num_n > self->num_n)
1922 num_p = (a_float_t *)js_realloc(ctx, num_p, sizeof(a_float_t) * num_n * 2);
1923 if (!num_p)
1925 return ~0;
1927 self->input = num_p;
1928 num_p += num_n;
1929 self->num_p = num_p;
1931 else
1933 num_p += self->num_n;
1935 self->num_n = (unsigned int)num_n;
1936 a_zero(self->input, sizeof(a_float_t) * num_n);
1937 return ArrayFloat(ctx, num, num_p, num_n);
1940 static int js_tf_set_den(JSContext *const ctx, a_tf_s *const self, JSValueConst den)
1942 a_u32_t den_n = 0;
1943 a_float_t *den_p = self->output;
1944 int ret = ArrayLength(ctx, den, &den_n);
1945 if (ret)
1947 return ret;
1949 if (den_n > self->den_n)
1951 den_p = (a_float_t *)js_realloc(ctx, den_p, sizeof(a_float_t) * den_n * 2);
1952 if (!den_p)
1954 return ~0;
1956 self->output = den_p;
1957 den_p += den_n;
1958 self->den_p = den_p;
1960 else
1962 den_p += self->den_n;
1964 self->den_n = (unsigned int)den_n;
1965 a_zero(self->output, sizeof(a_float_t) * den_n);
1966 return ArrayFloat(ctx, den, den_p, den_n);
1969 static JSValue js_tf_ctor(JSContext *const ctx, JSValueConst const new_target, int argc, JSValueConst *const argv)
1971 (void)argc;
1972 JSValue clazz = JS_UNDEFINED;
1973 a_tf_s *const self = (a_tf_s *)js_mallocz(ctx, sizeof(a_tf_s));
1974 if (!self)
1976 return JS_EXCEPTION;
1978 if (JS_IsObject(argv[0]) && js_tf_set_num(ctx, self, argv[0]))
1980 goto fail;
1982 if (JS_IsObject(argv[1]) && js_tf_set_den(ctx, self, argv[1]))
1984 goto fail;
1986 JSValue proto = JS_GetPropertyStr(ctx, new_target, "prototype");
1987 if (JS_IsException(proto))
1989 goto fail;
1991 clazz = JS_NewObjectProtoClass(ctx, proto, js_tf_class_id);
1992 JS_FreeValue(ctx, proto);
1993 if (JS_IsException(clazz))
1995 goto fail;
1997 JS_SetOpaque(clazz, self);
1998 return clazz;
1999 fail:
2000 js_free(ctx, self);
2001 JS_FreeValue(ctx, clazz);
2002 return JS_UNDEFINED;
2005 static JSValue js_tf_get(JSContext *const ctx, JSValueConst const this_val, int magic)
2007 a_tf_s *const self = (a_tf_s *)JS_GetOpaque2(ctx, this_val, js_tf_class_id);
2008 if (!self)
2010 return JS_EXCEPTION;
2012 JSValue val = JS_NewArray(ctx);
2013 if (JS_IsException(val))
2015 return val;
2017 a_float_t const *val_p;
2018 a_size_t val_n;
2019 switch (magic)
2021 case 0:
2022 val_p = self->num_p;
2023 val_n = self->num_n;
2024 break;
2025 case 1:
2026 val_p = self->den_p;
2027 val_n = self->den_n;
2028 break;
2029 case 2:
2030 val_p = self->input;
2031 val_n = self->num_n;
2032 break;
2033 case 3:
2034 val_p = self->output;
2035 val_n = self->den_n;
2036 break;
2037 default:
2038 return JS_UNDEFINED;
2040 for (unsigned int i = 0; i < val_n; ++i)
2042 JSValue x = JS_NewFloat64(ctx, (double)val_p[i]);
2043 JS_SetPropertyUint32(ctx, val, i, x);
2045 return val;
2048 static JSValue js_tf_set(JSContext *const ctx, JSValueConst const this_val, JSValueConst const val, int magic)
2050 a_tf_s *const self = (a_tf_s *)JS_GetOpaque2(ctx, this_val, js_tf_class_id);
2051 if (!self)
2053 return JS_EXCEPTION;
2055 switch (magic)
2057 case 0:
2058 if (JS_IsObject(val))
2060 js_tf_set_num(ctx, self, val);
2062 break;
2063 case 1:
2064 if (JS_IsObject(val))
2066 js_tf_set_den(ctx, self, val);
2068 break;
2069 default:
2070 break;
2072 return JS_UNDEFINED;
2075 static JSValue js_tf_iter(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
2077 (void)argc;
2078 a_tf_s *const self = (a_tf_s *)JS_GetOpaque2(ctx, this_val, js_tf_class_id);
2079 if (!self)
2081 return JS_EXCEPTION;
2083 double x;
2084 if (JS_ToFloat64(ctx, &x, argv[0]))
2086 return JS_EXCEPTION;
2088 return JS_NewFloat64(ctx, (double)a_tf_iter(self, (a_float_t)x));
2091 static JSValue js_tf_zero(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
2093 (void)argc;
2094 (void)argv;
2095 a_tf_s *const self = (a_tf_s *)JS_GetOpaque2(ctx, this_val, js_tf_class_id);
2096 if (!self)
2098 return JS_EXCEPTION;
2100 a_tf_zero(self);
2101 return JS_UNDEFINED;
2104 static JSCFunctionListEntry const js_tf_proto[] = {
2105 JS_PROP_STRING_DEF("[Symbol.toStringTag]", "a.tf", 0),
2106 JS_CGETSET_MAGIC_DEF("num", js_tf_get, js_tf_set, 0),
2107 JS_CGETSET_MAGIC_DEF("den", js_tf_get, js_tf_set, 1),
2108 JS_CGETSET_MAGIC_DEF("input", js_tf_get, js_tf_set, 2),
2109 JS_CGETSET_MAGIC_DEF("output", js_tf_get, js_tf_set, 3),
2110 JS_CFUNC_DEF("iter", 1, js_tf_iter),
2111 JS_CFUNC_DEF("zero", 0, js_tf_zero),
2114 static A_INLINE int js_liba_tf_init(JSContext *const ctx, JSModuleDef *const m)
2116 JS_NewClassID(&js_tf_class_id);
2117 JS_NewClass(JS_GetRuntime(ctx), js_tf_class_id, &js_tf_class);
2119 JSValue const proto = JS_NewObject(ctx);
2120 JS_SetPropertyFunctionList(ctx, proto, js_tf_proto, A_LEN(js_tf_proto));
2122 JSValue const clazz = JS_NewCFunction2(ctx, js_tf_ctor, "tf", 2, JS_CFUNC_constructor, 0);
2123 JS_SetConstructor(ctx, clazz, proto);
2124 JS_SetClassProto(ctx, js_tf_class_id, proto);
2126 JS_SetModuleExport(ctx, m, "tf", clazz);
2127 return 0;
2130 #include "a/version.h"
2132 static JSClassID js_version_class_id;
2134 static void js_version_finalizer(JSRuntime *const rt, JSValue const val)
2136 js_free_rt(rt, JS_GetOpaque(val, js_version_class_id));
2139 static JSClassDef js_version_class = {"version", .finalizer = js_version_finalizer};
2141 static JSValue js_version_ctor(JSContext *const ctx, JSValueConst const new_target, int argc, JSValueConst *const argv)
2143 JSValue clazz = JS_UNDEFINED;
2144 a_version_s *const self = (a_version_s *)js_mallocz(ctx, sizeof(a_version_s));
2145 if (!self)
2147 return JS_EXCEPTION;
2149 char const *ver = NULL;
2150 a_u32_t args[] = {0, 0, 0};
2151 if (argc > (int)A_LEN(args))
2153 argc = (int)A_LEN(args);
2155 for (int i = 0; i < argc; ++i)
2157 if (JS_ToUint32(ctx, &args[i], argv[i]))
2159 if (!i && ((void)(ver = JS_ToCString(ctx, argv[0])), ver))
2161 break;
2163 goto fail;
2166 if (ver)
2168 a_version_parse(self, ver);
2169 JS_FreeCString(ctx, ver);
2171 else
2173 self->major = (unsigned int)args[0];
2174 self->minor = (unsigned int)args[1];
2175 self->patch = (unsigned int)args[2];
2177 JSValue proto = JS_GetPropertyStr(ctx, new_target, "prototype");
2178 if (JS_IsException(proto))
2180 goto fail;
2182 clazz = JS_NewObjectProtoClass(ctx, proto, js_version_class_id);
2183 JS_FreeValue(ctx, proto);
2184 if (JS_IsException(clazz))
2186 goto fail;
2188 JS_SetOpaque(clazz, self);
2189 return clazz;
2190 fail:
2191 js_free(ctx, self);
2192 JS_FreeValue(ctx, clazz);
2193 return JS_EXCEPTION;
2196 static JSValue js_version_get(JSContext *const ctx, JSValueConst const this_val, int magic)
2198 a_version_s *const self = (a_version_s *)JS_GetOpaque2(ctx, this_val, js_version_class_id);
2199 if (!self)
2201 return JS_EXCEPTION;
2203 a_u32_t ver;
2204 switch (magic)
2206 case 0:
2207 ver = self->major;
2208 break;
2209 case 1:
2210 ver = self->minor;
2211 break;
2212 case 2:
2213 ver = self->patch;
2214 break;
2215 default:
2216 return JS_UNDEFINED;
2218 return JS_NewUint32(ctx, ver);
2221 static JSValue js_version_set(JSContext *const ctx, JSValueConst const this_val, JSValueConst const val, int magic)
2223 a_version_s *const self = (a_version_s *)JS_GetOpaque2(ctx, this_val, js_version_class_id);
2224 if (!self)
2226 return JS_EXCEPTION;
2228 a_u32_t ver;
2229 if (JS_ToUint32(ctx, &ver, val))
2231 return JS_EXCEPTION;
2233 switch (magic)
2235 case 0:
2236 self->major = (unsigned int)ver;
2237 break;
2238 case 1:
2239 self->minor = (unsigned int)ver;
2240 break;
2241 case 2:
2242 self->patch = (unsigned int)ver;
2243 break;
2244 default:
2245 break;
2247 return JS_UNDEFINED;
2250 static JSValue js_version_check(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
2252 (void)this_val;
2253 a_u32_t args[] = {0, 0, 0};
2254 if (argc > (int)A_LEN(args))
2256 argc = (int)A_LEN(args);
2258 for (int i = 0; i < argc; ++i)
2260 if (JS_ToUint32(ctx, &args[i], argv[i]))
2262 return JS_EXCEPTION;
2265 #undef a_version_check
2266 return JS_NewInt32(ctx, a_version_check((unsigned int)args[0], (unsigned int)args[1], (unsigned int)args[2]));
2269 static JSValue js_version_parse(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
2271 (void)argc;
2272 a_version_s *const self = (a_version_s *)JS_GetOpaque2(ctx, this_val, js_version_class_id);
2273 if (!self)
2275 return JS_EXCEPTION;
2277 char const *ver = JS_ToCString(ctx, argv[0]);
2278 if (!ver)
2280 return JS_EXCEPTION;
2282 a_version_parse(self, ver);
2283 JS_FreeCString(ctx, ver);
2284 return JS_UNDEFINED;
2287 static JSValue js_version_cmp(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
2289 (void)argc;
2290 a_version_s *const self = (a_version_s *)JS_GetOpaque2(ctx, this_val, js_version_class_id);
2291 if (!self)
2293 return JS_EXCEPTION;
2295 a_version_s *const other = (a_version_s *)JS_GetOpaque2(ctx, argv[0], js_version_class_id);
2296 if (!other)
2298 return JS_EXCEPTION;
2300 return JS_NewInt32(ctx, a_version_cmp(self, other));
2303 static JSValue js_version_lt(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
2305 (void)argc;
2306 a_version_s *const self = (a_version_s *)JS_GetOpaque2(ctx, this_val, js_version_class_id);
2307 if (!self)
2309 return JS_EXCEPTION;
2311 a_version_s *const other = (a_version_s *)JS_GetOpaque2(ctx, argv[0], js_version_class_id);
2312 if (!other)
2314 return JS_EXCEPTION;
2316 return JS_NewBool(ctx, a_version_lt(self, other));
2319 static JSValue js_version_gt(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
2321 (void)argc;
2322 a_version_s *const self = (a_version_s *)JS_GetOpaque2(ctx, this_val, js_version_class_id);
2323 if (!self)
2325 return JS_EXCEPTION;
2327 a_version_s *const other = (a_version_s *)JS_GetOpaque2(ctx, argv[0], js_version_class_id);
2328 if (!other)
2330 return JS_EXCEPTION;
2332 return JS_NewBool(ctx, a_version_gt(self, other));
2335 static JSValue js_version_le(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
2337 (void)argc;
2338 a_version_s *const self = (a_version_s *)JS_GetOpaque2(ctx, this_val, js_version_class_id);
2339 if (!self)
2341 return JS_EXCEPTION;
2343 a_version_s *const other = (a_version_s *)JS_GetOpaque2(ctx, argv[0], js_version_class_id);
2344 if (!other)
2346 return JS_EXCEPTION;
2348 return JS_NewBool(ctx, a_version_le(self, other));
2351 static JSValue js_version_ge(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
2353 (void)argc;
2354 a_version_s *const self = (a_version_s *)JS_GetOpaque2(ctx, this_val, js_version_class_id);
2355 if (!self)
2357 return JS_EXCEPTION;
2359 a_version_s *const other = (a_version_s *)JS_GetOpaque2(ctx, argv[0], js_version_class_id);
2360 if (!other)
2362 return JS_EXCEPTION;
2364 return JS_NewBool(ctx, a_version_ge(self, other));
2367 static JSValue js_version_eq(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
2369 (void)argc;
2370 a_version_s *const self = (a_version_s *)JS_GetOpaque2(ctx, this_val, js_version_class_id);
2371 if (!self)
2373 return JS_EXCEPTION;
2375 a_version_s *const other = (a_version_s *)JS_GetOpaque2(ctx, argv[0], js_version_class_id);
2376 if (!other)
2378 return JS_EXCEPTION;
2380 return JS_NewBool(ctx, a_version_eq(self, other));
2383 static JSValue js_version_ne(JSContext *const ctx, JSValueConst const this_val, int argc, JSValueConst *const argv)
2385 (void)argc;
2386 a_version_s *const self = (a_version_s *)JS_GetOpaque2(ctx, this_val, js_version_class_id);
2387 if (!self)
2389 return JS_EXCEPTION;
2391 a_version_s *const other = (a_version_s *)JS_GetOpaque2(ctx, argv[0], js_version_class_id);
2392 if (!other)
2394 return JS_EXCEPTION;
2396 return JS_NewBool(ctx, a_version_ne(self, other));
2399 static JSCFunctionListEntry const js_version_proto[] = {
2400 JS_PROP_STRING_DEF("[Symbol.toStringTag]", "a.version", 0),
2401 JS_CGETSET_MAGIC_DEF("major", js_version_get, js_version_set, 0),
2402 JS_CGETSET_MAGIC_DEF("minor", js_version_get, js_version_set, 1),
2403 JS_CGETSET_MAGIC_DEF("patch", js_version_get, js_version_set, 2),
2404 JS_CFUNC_DEF("parse", 1, js_version_parse),
2405 JS_CFUNC_DEF("cmp", 1, js_version_cmp),
2406 JS_CFUNC_DEF("lt", 1, js_version_lt),
2407 JS_CFUNC_DEF("gt", 1, js_version_gt),
2408 JS_CFUNC_DEF("le", 1, js_version_le),
2409 JS_CFUNC_DEF("ge", 1, js_version_ge),
2410 JS_CFUNC_DEF("eq", 1, js_version_eq),
2411 JS_CFUNC_DEF("ne", 1, js_version_ne),
2414 static A_INLINE int js_liba_version_init(JSContext *const ctx, JSModuleDef *const m)
2416 JS_NewClassID(&js_version_class_id);
2417 JS_NewClass(JS_GetRuntime(ctx), js_version_class_id, &js_version_class);
2419 JSValue const proto = JS_NewObject(ctx);
2420 JS_SetPropertyFunctionList(ctx, proto, js_version_proto, A_LEN(js_version_proto));
2422 JSValue const clazz = JS_NewCFunction2(ctx, js_version_ctor, "version", 3, JS_CFUNC_constructor, 0);
2423 JS_SetConstructor(ctx, clazz, proto);
2424 JS_SetClassProto(ctx, js_version_class_id, proto);
2426 JS_DefinePropertyValueStr(ctx, clazz, "MAJOR", JS_NewUint32(ctx, A_VERSION_MAJOR), 0);
2427 JS_DefinePropertyValueStr(ctx, clazz, "MINOR", JS_NewUint32(ctx, A_VERSION_MINOR), 0);
2428 JS_DefinePropertyValueStr(ctx, clazz, "PATCH", JS_NewUint32(ctx, A_VERSION_PATCH), 0);
2429 JS_DefinePropertyValueStr(ctx, clazz, "TWEAK", JS_NewUint32(ctx, A_VERSION_TWEAK), 0);
2430 JSValue const version_check = JS_NewCFunction2(ctx, js_version_check, "check", 3, JS_CFUNC_generic, 0);
2431 JS_DefinePropertyValueStr(ctx, clazz, "check", version_check, 0);
2433 JS_SetModuleExport(ctx, m, "version", clazz);
2434 return 0;
2437 static JSCFunctionListEntry const js_liba_funcs[] = {
2438 JS_OBJECT_DEF("mf", js_liba_mf_funcs, A_LEN(js_liba_mf_funcs), 0),
2439 JS_PROP_STRING_DEF("VERSION", A_VERSION, 0),
2440 JS_CFUNC_DEF("hash_bkdr", 1, js_hash_bkdr),
2441 JS_CFUNC_DEF("rsqrt", 1, js_rsqrt),
2444 static int js_liba_init(JSContext *const ctx, JSModuleDef *const m)
2446 js_liba_pid_init(ctx, m);
2447 js_liba_pid_fuzzy_init(ctx, m);
2448 js_liba_pid_neuron_init(ctx, m);
2449 js_liba_polytraj3_init(ctx, m);
2450 js_liba_polytraj5_init(ctx, m);
2451 js_liba_polytraj7_init(ctx, m);
2452 js_liba_tf_init(ctx, m);
2453 js_liba_version_init(ctx, m);
2454 return JS_SetModuleExportList(ctx, m, js_liba_funcs, A_LEN(js_liba_funcs));
2457 JSModuleDef *js_init_module(JSContext *const ctx, char const *const module_name)
2459 JSModuleDef *m = JS_NewCModule(ctx, module_name, js_liba_init);
2460 if (m)
2462 JS_AddModuleExport(ctx, m, "pid");
2463 JS_AddModuleExport(ctx, m, "pid_fuzzy");
2464 JS_AddModuleExport(ctx, m, "pid_neuron");
2465 JS_AddModuleExport(ctx, m, "polytraj3");
2466 JS_AddModuleExport(ctx, m, "polytraj5");
2467 JS_AddModuleExport(ctx, m, "polytraj7");
2468 JS_AddModuleExport(ctx, m, "tf");
2469 JS_AddModuleExport(ctx, m, "version");
2470 JS_AddModuleExportList(ctx, m, js_liba_funcs, A_LEN(js_liba_funcs));
2472 return m;