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
);
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
))
23 int ret
= JS_ToUint32(ctx
, plen
, len
);
24 JS_FreeValue(ctx
, len
);
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
))
38 int ret
= JS_ToFloat64(ctx
, &x
, tmp
);
39 JS_FreeValue(ctx
, val
);
44 ptr
[i
] = (a_float_t
)x
;
49 static JSValue
js_hash_bkdr(JSContext
*const ctx
, JSValueConst
const this_val
, int const argc
, JSValueConst
*const argv
)
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
);
64 static JSValue
js_rsqrt(JSContext
*const ctx
, JSValueConst
const this_val
, int const argc
, JSValueConst
*const argv
)
69 if (JS_ToFloat64(ctx
, &x
, argv
[0]))
73 return JS_NewFloat64(ctx
, a_f64_rsqrt(x
));
78 static JSValue
js_mf_gauss(JSContext
*const ctx
, JSValueConst
const this_val
, int argc
, JSValueConst
*const argv
)
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
]))
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
)
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
]))
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
)
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
]))
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
)
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
]))
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
)
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
]))
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
)
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
]))
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
)
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
]))
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
)
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
]))
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
)
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
]))
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
)
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
]))
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
)
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
]))
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
)
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
]))
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
)
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
]))
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
),
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
)
331 JSValue clazz
= JS_UNDEFINED
;
332 a_pid_s
*const self
= (a_pid_s
*)js_mallocz(ctx
, sizeof(a_pid_s
));
338 self
->summax
= +A_FLOAT_INF
;
339 self
->summin
= -A_FLOAT_INF
;
340 self
->outmax
= +A_FLOAT_INF
;
341 self
->outmin
= -A_FLOAT_INF
;
343 JSValue proto
= JS_GetPropertyStr(ctx
, new_target
, "prototype");
344 if (JS_IsException(proto
))
348 clazz
= JS_NewObjectProtoClass(ctx
, proto
, js_pid_class_id
);
349 JS_FreeValue(ctx
, proto
);
350 if (JS_IsException(clazz
))
354 JS_SetOpaque(clazz
, self
);
358 JS_FreeValue(ctx
, clazz
);
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
);
373 x
= (double)self
->kp
;
376 x
= (double)self
->ki
;
379 x
= (double)self
->kd
;
382 x
= (double)self
->summax
;
385 x
= (double)self
->summin
;
388 x
= (double)self
->outmax
;
391 x
= (double)self
->outmin
;
394 x
= (double)self
->out
;
397 x
= (double)self
->fdb
;
400 x
= (double)self
->err
;
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
);
416 if (JS_ToFloat64(ctx
, &x
, val
))
423 self
->kp
= (a_float_t
)x
;
426 self
->ki
= (a_float_t
)x
;
429 self
->kd
= (a_float_t
)x
;
432 self
->summax
= (a_float_t
)x
;
435 self
->summin
= (a_float_t
)x
;
438 self
->outmax
= (a_float_t
)x
;
441 self
->outmin
= (a_float_t
)x
;
449 static JSValue
js_pid_zero(JSContext
*const ctx
, JSValueConst
const this_val
, int argc
, JSValueConst
*const argv
)
453 a_pid_s
*const self
= (a_pid_s
*)JS_GetOpaque2(ctx
, this_val
, js_pid_class_id
);
462 static JSValue
js_pid_kpid(JSContext
*const ctx
, JSValueConst
const this_val
, int argc
, JSValueConst
*const argv
)
465 a_pid_s
*const self
= (a_pid_s
*)JS_GetOpaque2(ctx
, this_val
, js_pid_class_id
);
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
]))
478 a_pid_kpid(self
, (a_float_t
)args
[0], (a_float_t
)args
[1], (a_float_t
)args
[2]);
482 static JSValue
js_pid_off(JSContext
*const ctx
, JSValueConst
const this_val
, int argc
, JSValueConst
*const argv
)
485 a_pid_s
*const self
= (a_pid_s
*)JS_GetOpaque2(ctx
, this_val
, js_pid_class_id
);
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
]))
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
)
504 a_pid_s
*const self
= (a_pid_s
*)JS_GetOpaque2(ctx
, this_val
, js_pid_class_id
);
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
]))
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
)
523 a_pid_s
*const self
= (a_pid_s
*)JS_GetOpaque2(ctx
, this_val
, js_pid_class_id
);
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
]))
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
);
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
);
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
)
605 JSValue clazz
= JS_UNDEFINED
;
606 a_pid_fuzzy_s
*const self
= (a_pid_fuzzy_s
*)js_mallocz(ctx
, sizeof(a_pid_fuzzy_s
));
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
;
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
))
624 clazz
= JS_NewObjectProtoClass(ctx
, proto
, js_pid_fuzzy_class_id
);
625 JS_FreeValue(ctx
, proto
);
626 if (JS_IsException(clazz
))
630 JS_SetOpaque(clazz
, self
);
634 JS_FreeValue(ctx
, clazz
);
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
);
649 return JS_NewUint32(ctx
, self
->joint
);
651 x
= (double)self
->kp
;
654 x
= (double)self
->ki
;
657 x
= (double)self
->kd
;
660 x
= (double)self
->pid
.summax
;
663 x
= (double)self
->pid
.summin
;
666 x
= (double)self
->pid
.outmax
;
669 x
= (double)self
->pid
.outmin
;
672 x
= (double)self
->pid
.out
;
675 x
= (double)self
->pid
.fdb
;
678 x
= (double)self
->pid
.err
;
681 return JS_NewUint32(ctx
, self
->order
);
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
));
699 a_pid_fuzzy_set_joint(self
, ptr
, joint
);
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
);
713 if (JS_ToUint32(ctx
, &u
, val
))
717 if (js_pid_fuzzy_joint_(ctx
, self
, (unsigned int)u
))
724 if (JS_ToFloat64(ctx
, &x
, val
))
731 self
->pid
.kp
= self
->kp
= (a_float_t
)x
;
734 self
->pid
.ki
= self
->ki
= (a_float_t
)x
;
737 self
->pid
.kd
= self
->kd
= (a_float_t
)x
;
740 self
->pid
.summax
= (a_float_t
)x
;
743 self
->pid
.summin
= (a_float_t
)x
;
746 self
->pid
.outmax
= (a_float_t
)x
;
749 self
->pid
.outmin
= (a_float_t
)x
;
757 static JSValue
js_pid_fuzzy_op(JSContext
*const ctx
, JSValueConst
const this_val
, int argc
, JSValueConst
*const argv
)
760 a_pid_fuzzy_s
*const self
= (a_pid_fuzzy_s
*)JS_GetOpaque2(ctx
, this_val
, js_pid_fuzzy_class_id
);
766 if (JS_ToUint32(ctx
, &op
, argv
[0]))
770 a_pid_fuzzy_set_op(self
, (unsigned int)op
);
774 static JSValue
js_pid_fuzzy_rule(JSContext
*const ctx
, JSValueConst
const this_val
, int argc
, JSValueConst
*const argv
)
777 a_pid_fuzzy_s
*const self
= (a_pid_fuzzy_s
*)JS_GetOpaque2(ctx
, this_val
, js_pid_fuzzy_class_id
);
790 JSValue res
= JS_UNDEFINED
;
791 if (JS_IsArray(ctx
, argv
[0]))
793 if (ArrayLength(ctx
, argv
[0], &order
))
797 res
= Concat(ctx
, argv
[0]);
798 if (ArrayLength(ctx
, res
, &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
);
810 if (ArrayFloat(ctx
, res
, me
, len
))
815 JS_FreeValue(ctx
, res
);
818 if (JS_IsArray(ctx
, argv
[1]))
820 if (ArrayLength(ctx
, argv
[1], &row
) || row
!= order
)
824 res
= Concat(ctx
, argv
[1]);
825 if (ArrayLength(ctx
, res
, &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
);
837 if (ArrayFloat(ctx
, res
, mec
, len
))
842 JS_FreeValue(ctx
, res
);
845 if (JS_IsArray(ctx
, argv
[2]))
847 if (ArrayLength(ctx
, argv
[2], &row
) || row
!= order
)
851 res
= Concat(ctx
, argv
[2]);
852 if (ArrayLength(ctx
, res
, &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
);
864 if (ArrayFloat(ctx
, res
, mkp
, len
))
869 JS_FreeValue(ctx
, res
);
872 if (JS_IsArray(ctx
, argv
[3]))
874 if (ArrayLength(ctx
, argv
[3], &row
) || row
!= order
)
878 res
= Concat(ctx
, argv
[3]);
879 if (ArrayLength(ctx
, res
, &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
);
891 if (ArrayFloat(ctx
, res
, mki
, len
))
896 JS_FreeValue(ctx
, res
);
899 if (JS_IsArray(ctx
, argv
[4]))
901 if (ArrayLength(ctx
, argv
[4], &row
) || row
!= order
)
905 res
= Concat(ctx
, argv
[4]);
906 if (ArrayLength(ctx
, res
, &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
);
918 if (ArrayFloat(ctx
, res
, mkd
, len
))
923 JS_FreeValue(ctx
, res
);
928 JS_FreeValue(ctx
, res
);
932 static JSValue
js_pid_fuzzy_joint(JSContext
*const ctx
, JSValueConst
const this_val
, int argc
, JSValueConst
*const argv
)
935 a_pid_fuzzy_s
*const self
= (a_pid_fuzzy_s
*)JS_GetOpaque2(ctx
, this_val
, js_pid_fuzzy_class_id
);
941 if (JS_ToUint32(ctx
, &joint
, argv
[0]))
945 if (js_pid_fuzzy_joint_(ctx
, self
, joint
))
952 static JSValue
js_pid_fuzzy_kpid(JSContext
*const ctx
, JSValueConst
const this_val
, int argc
, JSValueConst
*const argv
)
955 a_pid_fuzzy_s
*const self
= (a_pid_fuzzy_s
*)JS_GetOpaque2(ctx
, this_val
, js_pid_fuzzy_class_id
);
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
]))
968 a_pid_fuzzy_kpid(self
, (a_float_t
)args
[0], (a_float_t
)args
[1], (a_float_t
)args
[2]);
972 static JSValue
js_pid_fuzzy_zero(JSContext
*const ctx
, JSValueConst
const this_val
, int argc
, JSValueConst
*const argv
)
976 a_pid_fuzzy_s
*const self
= (a_pid_fuzzy_s
*)JS_GetOpaque2(ctx
, this_val
, js_pid_fuzzy_class_id
);
981 a_pid_fuzzy_zero(self
);
985 static JSValue
js_pid_fuzzy_off(JSContext
*const ctx
, JSValueConst
const this_val
, int argc
, JSValueConst
*const argv
)
988 a_pid_fuzzy_s
*const self
= (a_pid_fuzzy_s
*)JS_GetOpaque2(ctx
, this_val
, js_pid_fuzzy_class_id
);
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
]))
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
)
1007 a_pid_fuzzy_s
*const self
= (a_pid_fuzzy_s
*)JS_GetOpaque2(ctx
, this_val
, js_pid_fuzzy_class_id
);
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
)
1026 a_pid_fuzzy_s
*const self
= (a_pid_fuzzy_s
*)JS_GetOpaque2(ctx
, this_val
, js_pid_fuzzy_class_id
);
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
);
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
)
1105 JSValue clazz
= JS_UNDEFINED
;
1106 a_pid_neuron_s
*const self
= (a_pid_neuron_s
*)js_mallocz(ctx
, sizeof(a_pid_neuron_s
));
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
;
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
))
1125 clazz
= JS_NewObjectProtoClass(ctx
, proto
, js_pid_neuron_class_id
);
1126 JS_FreeValue(ctx
, proto
);
1127 if (JS_IsException(clazz
))
1131 JS_SetOpaque(clazz
, 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
);
1144 return JS_EXCEPTION
;
1150 x
= (double)self
->k
;
1153 x
= (double)self
->pid
.kp
;
1156 x
= (double)self
->pid
.ki
;
1159 x
= (double)self
->pid
.kd
;
1162 x
= (double)self
->wp
;
1165 x
= (double)self
->wi
;
1168 x
= (double)self
->wd
;
1171 x
= (double)self
->pid
.outmax
;
1174 x
= (double)self
->pid
.outmin
;
1177 x
= (double)self
->pid
.out
;
1180 x
= (double)self
->pid
.fdb
;
1183 x
= (double)self
->pid
.err
;
1186 x
= (double)self
->ec
;
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
);
1199 return JS_EXCEPTION
;
1202 if (JS_ToFloat64(ctx
, &x
, val
))
1204 return JS_EXCEPTION
;
1209 self
->k
= (a_float_t
)x
;
1212 self
->pid
.kp
= (a_float_t
)x
;
1215 self
->pid
.ki
= (a_float_t
)x
;
1218 self
->pid
.kd
= (a_float_t
)x
;
1221 self
->wp
= (a_float_t
)x
;
1224 self
->wi
= (a_float_t
)x
;
1227 self
->wd
= (a_float_t
)x
;
1230 self
->pid
.outmax
= (a_float_t
)x
;
1233 self
->pid
.outmin
= (a_float_t
)x
;
1238 return JS_UNDEFINED
;
1241 static JSValue
js_pid_neuron_kpid(JSContext
*const ctx
, JSValueConst
const this_val
, int argc
, JSValueConst
*const argv
)
1244 a_pid_neuron_s
*const self
= (a_pid_neuron_s
*)JS_GetOpaque2(ctx
, this_val
, js_pid_neuron_class_id
);
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
)
1264 a_pid_neuron_s
*const self
= (a_pid_neuron_s
*)JS_GetOpaque2(ctx
, this_val
, js_pid_neuron_class_id
);
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
)
1285 a_pid_neuron_s
*const self
= (a_pid_neuron_s
*)JS_GetOpaque2(ctx
, this_val
, js_pid_neuron_class_id
);
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
)
1297 a_pid_neuron_s
*const self
= (a_pid_neuron_s
*)JS_GetOpaque2(ctx
, this_val
, js_pid_neuron_class_id
);
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
)
1316 a_pid_neuron_s
*const self
= (a_pid_neuron_s
*)JS_GetOpaque2(ctx
, this_val
, js_pid_neuron_class_id
);
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
);
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
));
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
]))
1401 for (int i
= 4; i
< argc
; ++i
)
1403 if (JS_ToFloat64(ctx
, &args
[i
], argv
[i
]))
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
))
1417 clazz
= JS_NewObjectProtoClass(ctx
, proto
, js_polytraj3_class_id
);
1418 JS_FreeValue(ctx
, proto
);
1419 if (JS_IsException(clazz
))
1423 JS_SetOpaque(clazz
, 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
);
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
)
1467 a_polytraj3_s
*const self
= (a_polytraj3_s
*)JS_GetOpaque2(ctx
, this_val
, js_polytraj3_class_id
);
1470 return JS_EXCEPTION
;
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
)
1484 a_polytraj3_s
*const self
= (a_polytraj3_s
*)JS_GetOpaque2(ctx
, this_val
, js_polytraj3_class_id
);
1487 return JS_EXCEPTION
;
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
)
1501 a_polytraj3_s
*const self
= (a_polytraj3_s
*)JS_GetOpaque2(ctx
, this_val
, js_polytraj3_class_id
);
1504 return JS_EXCEPTION
;
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
);
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
));
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
]))
1568 for (int i
= 4; i
< argc
; ++i
)
1570 if (JS_ToFloat64(ctx
, &args
[i
], argv
[i
]))
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
))
1585 clazz
= JS_NewObjectProtoClass(ctx
, proto
, js_polytraj5_class_id
);
1586 JS_FreeValue(ctx
, proto
);
1587 if (JS_IsException(clazz
))
1591 JS_SetOpaque(clazz
, 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
);
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
)
1636 a_polytraj5_s
*const self
= (a_polytraj5_s
*)JS_GetOpaque2(ctx
, this_val
, js_polytraj5_class_id
);
1639 return JS_EXCEPTION
;
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
)
1653 a_polytraj5_s
*const self
= (a_polytraj5_s
*)JS_GetOpaque2(ctx
, this_val
, js_polytraj5_class_id
);
1656 return JS_EXCEPTION
;
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
)
1670 a_polytraj5_s
*const self
= (a_polytraj5_s
*)JS_GetOpaque2(ctx
, this_val
, js_polytraj5_class_id
);
1673 return JS_EXCEPTION
;
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
);
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
));
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
]))
1737 for (int i
= 4; i
< argc
; ++i
)
1739 if (JS_ToFloat64(ctx
, &args
[i
], argv
[i
]))
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
))
1755 clazz
= JS_NewObjectProtoClass(ctx
, proto
, js_polytraj7_class_id
);
1756 JS_FreeValue(ctx
, proto
);
1757 if (JS_IsException(clazz
))
1761 JS_SetOpaque(clazz
, 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
);
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
)
1807 a_polytraj7_s
*const self
= (a_polytraj7_s
*)JS_GetOpaque2(ctx
, this_val
, js_polytraj7_class_id
);
1810 return JS_EXCEPTION
;
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
)
1824 a_polytraj7_s
*const self
= (a_polytraj7_s
*)JS_GetOpaque2(ctx
, this_val
, js_polytraj7_class_id
);
1827 return JS_EXCEPTION
;
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
)
1841 a_polytraj7_s
*const self
= (a_polytraj7_s
*)JS_GetOpaque2(ctx
, this_val
, js_polytraj7_class_id
);
1844 return JS_EXCEPTION
;
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
)
1858 a_polytraj7_s
*const self
= (a_polytraj7_s
*)JS_GetOpaque2(ctx
, this_val
, js_polytraj7_class_id
);
1861 return JS_EXCEPTION
;
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
);
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
)
1914 a_float_t
*num_p
= self
->input
;
1915 int ret
= ArrayLength(ctx
, num
, &num_n
);
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);
1927 self
->input
= num_p
;
1929 self
->num_p
= num_p
;
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
)
1943 a_float_t
*den_p
= self
->output
;
1944 int ret
= ArrayLength(ctx
, den
, &den_n
);
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);
1956 self
->output
= den_p
;
1958 self
->den_p
= den_p
;
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
)
1972 JSValue clazz
= JS_UNDEFINED
;
1973 a_tf_s
*const self
= (a_tf_s
*)js_mallocz(ctx
, sizeof(a_tf_s
));
1976 return JS_EXCEPTION
;
1978 if (JS_IsObject(argv
[0]) && js_tf_set_num(ctx
, self
, argv
[0]))
1982 if (JS_IsObject(argv
[1]) && js_tf_set_den(ctx
, self
, argv
[1]))
1986 JSValue proto
= JS_GetPropertyStr(ctx
, new_target
, "prototype");
1987 if (JS_IsException(proto
))
1991 clazz
= JS_NewObjectProtoClass(ctx
, proto
, js_tf_class_id
);
1992 JS_FreeValue(ctx
, proto
);
1993 if (JS_IsException(clazz
))
1997 JS_SetOpaque(clazz
, 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
);
2010 return JS_EXCEPTION
;
2012 JSValue val
= JS_NewArray(ctx
);
2013 if (JS_IsException(val
))
2017 a_float_t
const *val_p
;
2022 val_p
= self
->num_p
;
2023 val_n
= self
->num_n
;
2026 val_p
= self
->den_p
;
2027 val_n
= self
->den_n
;
2030 val_p
= self
->input
;
2031 val_n
= self
->num_n
;
2034 val_p
= self
->output
;
2035 val_n
= self
->den_n
;
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
);
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
);
2053 return JS_EXCEPTION
;
2058 if (JS_IsObject(val
))
2060 js_tf_set_num(ctx
, self
, val
);
2064 if (JS_IsObject(val
))
2066 js_tf_set_den(ctx
, self
, val
);
2072 return JS_UNDEFINED
;
2075 static JSValue
js_tf_iter(JSContext
*const ctx
, JSValueConst
const this_val
, int argc
, JSValueConst
*const argv
)
2078 a_tf_s
*const self
= (a_tf_s
*)JS_GetOpaque2(ctx
, this_val
, js_tf_class_id
);
2081 return JS_EXCEPTION
;
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
)
2095 a_tf_s
*const self
= (a_tf_s
*)JS_GetOpaque2(ctx
, this_val
, js_tf_class_id
);
2098 return JS_EXCEPTION
;
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
);
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
));
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
))
2168 a_version_parse(self
, ver
);
2169 JS_FreeCString(ctx
, ver
);
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
))
2182 clazz
= JS_NewObjectProtoClass(ctx
, proto
, js_version_class_id
);
2183 JS_FreeValue(ctx
, proto
);
2184 if (JS_IsException(clazz
))
2188 JS_SetOpaque(clazz
, 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
);
2201 return JS_EXCEPTION
;
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
);
2226 return JS_EXCEPTION
;
2229 if (JS_ToUint32(ctx
, &ver
, val
))
2231 return JS_EXCEPTION
;
2236 self
->major
= (unsigned int)ver
;
2239 self
->minor
= (unsigned int)ver
;
2242 self
->patch
= (unsigned int)ver
;
2247 return JS_UNDEFINED
;
2250 static JSValue
js_version_check(JSContext
*const ctx
, JSValueConst
const this_val
, int argc
, JSValueConst
*const argv
)
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
)
2272 a_version_s
*const self
= (a_version_s
*)JS_GetOpaque2(ctx
, this_val
, js_version_class_id
);
2275 return JS_EXCEPTION
;
2277 char const *ver
= JS_ToCString(ctx
, argv
[0]);
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
)
2290 a_version_s
*const self
= (a_version_s
*)JS_GetOpaque2(ctx
, this_val
, js_version_class_id
);
2293 return JS_EXCEPTION
;
2295 a_version_s
*const other
= (a_version_s
*)JS_GetOpaque2(ctx
, argv
[0], js_version_class_id
);
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
)
2306 a_version_s
*const self
= (a_version_s
*)JS_GetOpaque2(ctx
, this_val
, js_version_class_id
);
2309 return JS_EXCEPTION
;
2311 a_version_s
*const other
= (a_version_s
*)JS_GetOpaque2(ctx
, argv
[0], js_version_class_id
);
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
)
2322 a_version_s
*const self
= (a_version_s
*)JS_GetOpaque2(ctx
, this_val
, js_version_class_id
);
2325 return JS_EXCEPTION
;
2327 a_version_s
*const other
= (a_version_s
*)JS_GetOpaque2(ctx
, argv
[0], js_version_class_id
);
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
)
2338 a_version_s
*const self
= (a_version_s
*)JS_GetOpaque2(ctx
, this_val
, js_version_class_id
);
2341 return JS_EXCEPTION
;
2343 a_version_s
*const other
= (a_version_s
*)JS_GetOpaque2(ctx
, argv
[0], js_version_class_id
);
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
)
2354 a_version_s
*const self
= (a_version_s
*)JS_GetOpaque2(ctx
, this_val
, js_version_class_id
);
2357 return JS_EXCEPTION
;
2359 a_version_s
*const other
= (a_version_s
*)JS_GetOpaque2(ctx
, argv
[0], js_version_class_id
);
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
)
2370 a_version_s
*const self
= (a_version_s
*)JS_GetOpaque2(ctx
, this_val
, js_version_class_id
);
2373 return JS_EXCEPTION
;
2375 a_version_s
*const other
= (a_version_s
*)JS_GetOpaque2(ctx
, argv
[0], js_version_class_id
);
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
)
2386 a_version_s
*const self
= (a_version_s
*)JS_GetOpaque2(ctx
, this_val
, js_version_class_id
);
2389 return JS_EXCEPTION
;
2391 a_version_s
*const other
= (a_version_s
*)JS_GetOpaque2(ctx
, argv
[0], js_version_class_id
);
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
);
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
);
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
));