rename A_VERSION_{C,EX} to A_VERSION_{0,1,2,3,4}
[liba.git] / quickjs / src / trajbell.c
blob4e89f8099eb59a4c312c4d61009cf823c8e2c9c0
1 #include "a.h"
2 #include "a/trajbell.h"
4 static JSClassID liba_trajbell_class_id;
6 static void liba_trajbell_finalizer(JSRuntime *rt, JSValue val)
8 js_free_rt(rt, JS_GetOpaque(val, liba_trajbell_class_id));
11 static JSClassDef liba_trajbell_class = {"trajbell", .finalizer = liba_trajbell_finalizer};
13 static JSValue liba_trajbell_ctor(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst *argv)
15 (void)argc;
16 (void)argv;
17 JSValue proto, clazz = JS_UNDEFINED;
18 a_trajbell *const self = (a_trajbell *)js_mallocz(ctx, sizeof(a_trajbell));
19 if (!self) { return JS_EXCEPTION; }
20 proto = JS_GetPropertyStr(ctx, new_target, "prototype");
21 if (JS_IsException(proto)) { goto fail; }
22 clazz = JS_NewObjectProtoClass(ctx, proto, liba_trajbell_class_id);
23 JS_FreeValue(ctx, proto);
24 if (JS_IsException(clazz)) { goto fail; }
25 JS_SetOpaque(clazz, self);
26 return clazz;
27 fail:
28 js_free(ctx, self);
29 JS_FreeValue(ctx, clazz);
30 return JS_EXCEPTION;
33 static JSValue liba_trajbell_gen(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
35 a_trajbell *const self = (a_trajbell *)JS_GetOpaque2(ctx, this_val, liba_trajbell_class_id);
36 if (!self) { return JS_EXCEPTION; }
37 double args[] = {0, 0, 0, 0, 0, 0, 0};
38 if (argc > (int)A_LEN(args)) { argc = (int)A_LEN(args); }
39 for (int i = 0; i < 5; ++i)
41 if (JS_ToFloat64(ctx, &args[i], argv[i])) { return JS_EXCEPTION; }
43 for (int i = 5; i < argc; ++i)
45 if (JS_ToFloat64(ctx, &args[i], argv[i])) { return JS_EXCEPTION; }
47 args[0] = (double)a_trajbell_gen(self, (a_float)args[0], (a_float)args[1], (a_float)args[2],
48 (a_float)args[3], (a_float)args[4], (a_float)args[5], (a_float)args[6]);
49 return JS_NewFloat64(ctx, args[0]);
52 static JSValue liba_trajbell_pos(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
54 (void)argc;
55 a_trajbell *const self = (a_trajbell *)JS_GetOpaque2(ctx, this_val, liba_trajbell_class_id);
56 if (!self) { return JS_EXCEPTION; }
57 double dt;
58 if (JS_ToFloat64(ctx, &dt, argv[0])) { return JS_EXCEPTION; }
59 a_float pos = a_trajbell_pos(self, (a_float)dt);
60 return JS_NewFloat64(ctx, (double)pos);
63 static JSValue liba_trajbell_vel(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
65 (void)argc;
66 a_trajbell *const self = (a_trajbell *)JS_GetOpaque2(ctx, this_val, liba_trajbell_class_id);
67 if (!self) { return JS_EXCEPTION; }
68 double dt;
69 if (JS_ToFloat64(ctx, &dt, argv[0])) { return JS_EXCEPTION; }
70 a_float vel = a_trajbell_vel(self, (a_float)dt);
71 return JS_NewFloat64(ctx, (double)vel);
74 static JSValue liba_trajbell_acc(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
76 (void)argc;
77 a_trajbell *const self = (a_trajbell *)JS_GetOpaque2(ctx, this_val, liba_trajbell_class_id);
78 if (!self) { return JS_EXCEPTION; }
79 double dt;
80 if (JS_ToFloat64(ctx, &dt, argv[0])) { return JS_EXCEPTION; }
81 a_float acc = a_trajbell_acc(self, (a_float)dt);
82 return JS_NewFloat64(ctx, (double)acc);
85 static JSValue liba_trajbell_jer(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
87 (void)argc;
88 a_trajbell *const self = (a_trajbell *)JS_GetOpaque2(ctx, this_val, liba_trajbell_class_id);
89 if (!self) { return JS_EXCEPTION; }
90 double dt;
91 if (JS_ToFloat64(ctx, &dt, argv[0])) { return JS_EXCEPTION; }
92 a_float acc = a_trajbell_jer(self, (a_float)dt);
93 return JS_NewFloat64(ctx, (double)acc);
96 enum
98 self_t_,
99 self_tv_,
100 self_ta_,
101 self_td_,
102 self_taj_,
103 self_tdj_,
104 self_p0_,
105 self_p1_,
106 self_v0_,
107 self_v1_,
108 self_vm_,
109 self_jm_,
110 self_am_,
111 self_dm_,
114 static JSValue liba_trajbell_get(JSContext *ctx, JSValueConst this_val, int magic)
116 a_trajbell *const self = (a_trajbell *)JS_GetOpaque2(ctx, this_val, liba_trajbell_class_id);
117 if (!self) { return JS_EXCEPTION; }
118 double x;
119 switch (magic)
121 case self_t_: x = (double)self->t; break;
122 case self_tv_: x = (double)self->tv; break;
123 case self_ta_: x = (double)self->ta; break;
124 case self_td_: x = (double)self->td; break;
125 case self_taj_: x = (double)self->taj; break;
126 case self_tdj_: x = (double)self->tdj; break;
127 case self_p0_: x = (double)self->p0; break;
128 case self_p1_: x = (double)self->p1; break;
129 case self_v0_: x = (double)self->v0; break;
130 case self_v1_: x = (double)self->v1; break;
131 case self_vm_: x = (double)self->vm; break;
132 case self_jm_: x = (double)self->jm; break;
133 case self_am_: x = (double)self->am; break;
134 case self_dm_: x = (double)self->dm; break;
135 default: return JS_UNDEFINED;
137 return JS_NewFloat64(ctx, x);
140 static JSCFunctionListEntry const liba_trajbell_proto[] = {
141 JS_PROP_STRING_DEF("[Symbol.toStringTag]", "a.trajbell", 0),
142 JS_CGETSET_MAGIC_DEF("t", liba_trajbell_get, NULL, self_t_),
143 JS_CGETSET_MAGIC_DEF("tv", liba_trajbell_get, NULL, self_tv_),
144 JS_CGETSET_MAGIC_DEF("ta", liba_trajbell_get, NULL, self_ta_),
145 JS_CGETSET_MAGIC_DEF("td", liba_trajbell_get, NULL, self_td_),
146 JS_CGETSET_MAGIC_DEF("taj", liba_trajbell_get, NULL, self_taj_),
147 JS_CGETSET_MAGIC_DEF("tdj", liba_trajbell_get, NULL, self_tdj_),
148 JS_CGETSET_MAGIC_DEF("p0", liba_trajbell_get, NULL, self_p0_),
149 JS_CGETSET_MAGIC_DEF("p1", liba_trajbell_get, NULL, self_p1_),
150 JS_CGETSET_MAGIC_DEF("v0", liba_trajbell_get, NULL, self_v0_),
151 JS_CGETSET_MAGIC_DEF("v1", liba_trajbell_get, NULL, self_v1_),
152 JS_CGETSET_MAGIC_DEF("vm", liba_trajbell_get, NULL, self_vm_),
153 JS_CGETSET_MAGIC_DEF("jm", liba_trajbell_get, NULL, self_jm_),
154 JS_CGETSET_MAGIC_DEF("am", liba_trajbell_get, NULL, self_am_),
155 JS_CGETSET_MAGIC_DEF("dm", liba_trajbell_get, NULL, self_dm_),
156 JS_CFUNC_DEF("gen", 7, liba_trajbell_gen),
157 JS_CFUNC_DEF("pos", 1, liba_trajbell_pos),
158 JS_CFUNC_DEF("vel", 1, liba_trajbell_vel),
159 JS_CFUNC_DEF("acc", 1, liba_trajbell_acc),
160 JS_CFUNC_DEF("jer", 1, liba_trajbell_jer),
163 int js_liba_trajbell_init(JSContext *ctx, JSModuleDef *m)
165 JS_NewClassID(&liba_trajbell_class_id);
166 JS_NewClass(JS_GetRuntime(ctx), liba_trajbell_class_id, &liba_trajbell_class);
168 JSValue const proto = JS_NewObject(ctx);
169 JS_SetPropertyFunctionList(ctx, proto, liba_trajbell_proto, A_LEN(liba_trajbell_proto));
171 JSValue const clazz = JS_NewCFunction2(ctx, liba_trajbell_ctor, "trajbell", 0, JS_CFUNC_constructor, 0);
172 JS_SetConstructor(ctx, clazz, proto);
173 JS_SetClassProto(ctx, liba_trajbell_class_id, proto);
175 return JS_SetModuleExport(ctx, m, "trajbell", clazz);