rename A_VERSION_{C,EX} to A_VERSION_{0,1,2,3,4}
[liba.git] / quickjs / src / trajpoly3.c
blob909a0999096bb9332c3606026b2a7c08efa2a8d6
1 #include "a.h"
2 #include "a/trajpoly3.h"
4 static JSClassID liba_trajpoly3_class_id;
6 static void liba_trajpoly3_finalizer(JSRuntime *rt, JSValue val)
8 js_free_rt(rt, JS_GetOpaque(val, liba_trajpoly3_class_id));
11 static JSClassDef liba_trajpoly3_class = {"trajpoly3", .finalizer = liba_trajpoly3_finalizer};
13 static JSValue liba_trajpoly3_ctor(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst *argv)
15 JSValue proto, clazz = JS_UNDEFINED;
16 a_trajpoly3 *const self = (a_trajpoly3 *)js_mallocz(ctx, sizeof(a_trajpoly3));
17 if (!self) { return JS_EXCEPTION; }
18 double args[] = {0, 0, 0, 0, 0};
19 if (argc > (int)A_LEN(args)) { argc = (int)A_LEN(args); }
20 for (int i = 0; i < 3; ++i)
22 if (JS_ToFloat64(ctx, &args[i], argv[i])) { goto fail; }
24 for (int i = 3; i < argc; ++i)
26 if (JS_ToFloat64(ctx, &args[i], argv[i])) { goto fail; }
28 a_trajpoly3_gen(self, (a_float)args[0],
29 (a_float)args[1], (a_float)args[2],
30 (a_float)args[3], (a_float)args[4]);
31 proto = JS_GetPropertyStr(ctx, new_target, "prototype");
32 if (JS_IsException(proto)) { goto fail; }
33 clazz = JS_NewObjectProtoClass(ctx, proto, liba_trajpoly3_class_id);
34 JS_FreeValue(ctx, proto);
35 if (JS_IsException(clazz)) { goto fail; }
36 JS_SetOpaque(clazz, self);
37 return clazz;
38 fail:
39 js_free(ctx, self);
40 JS_FreeValue(ctx, clazz);
41 return JS_EXCEPTION;
44 static JSValue liba_trajpoly3_gen(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
46 a_trajpoly3 *const self = (a_trajpoly3 *)JS_GetOpaque2(ctx, this_val, liba_trajpoly3_class_id);
47 if (!self) { return JS_EXCEPTION; }
48 double args[] = {0, 0, 0, 0, 0};
49 if (argc > (int)A_LEN(args)) { argc = (int)A_LEN(args); }
50 for (int i = 0; i < 3; ++i)
52 if (JS_ToFloat64(ctx, &args[i], argv[i])) { return JS_EXCEPTION; }
54 for (int i = 3; i < argc; ++i)
56 if (JS_ToFloat64(ctx, &args[i], argv[i])) { return JS_EXCEPTION; }
58 a_trajpoly3_gen(self, (a_float)args[0],
59 (a_float)args[1], (a_float)args[2],
60 (a_float)args[3], (a_float)args[4]);
61 return JS_UNDEFINED;
64 static JSValue liba_trajpoly3_pos(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
66 (void)argc;
67 a_trajpoly3 *const self = (a_trajpoly3 *)JS_GetOpaque2(ctx, this_val, liba_trajpoly3_class_id);
68 if (!self) { return JS_EXCEPTION; }
69 double dt;
70 if (JS_ToFloat64(ctx, &dt, argv[0])) { return JS_EXCEPTION; }
71 a_float pos = a_trajpoly3_pos(self, (a_float)dt);
72 return JS_NewFloat64(ctx, (double)pos);
75 static JSValue liba_trajpoly3_vel(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
77 (void)argc;
78 a_trajpoly3 *const self = (a_trajpoly3 *)JS_GetOpaque2(ctx, this_val, liba_trajpoly3_class_id);
79 if (!self) { return JS_EXCEPTION; }
80 double dt;
81 if (JS_ToFloat64(ctx, &dt, argv[0])) { return JS_EXCEPTION; }
82 a_float vel = a_trajpoly3_vel(self, (a_float)dt);
83 return JS_NewFloat64(ctx, (double)vel);
86 static JSValue liba_trajpoly3_acc(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
88 (void)argc;
89 a_trajpoly3 *const self = (a_trajpoly3 *)JS_GetOpaque2(ctx, this_val, liba_trajpoly3_class_id);
90 if (!self) { return JS_EXCEPTION; }
91 double dt;
92 if (JS_ToFloat64(ctx, &dt, argv[0])) { return JS_EXCEPTION; }
93 a_float acc = a_trajpoly3_acc(self, (a_float)dt);
94 return JS_NewFloat64(ctx, (double)acc);
97 enum
99 self_p_,
100 self_v_,
101 self_a_,
104 static JSValue liba_trajpoly3_get(JSContext *ctx, JSValueConst this_val, int magic)
106 a_trajpoly3 *const self = (a_trajpoly3 *)JS_GetOpaque2(ctx, this_val, liba_trajpoly3_class_id);
107 if (!self) { return JS_EXCEPTION; }
108 switch (magic)
110 case self_p_: return js_array_num_new(ctx, self->p, A_LEN(self->p));
111 case self_v_: return js_array_num_new(ctx, self->v, A_LEN(self->v));
112 case self_a_: return js_array_num_new(ctx, self->a, A_LEN(self->a));
113 default: return JS_UNDEFINED;
117 static JSCFunctionListEntry const liba_trajpoly3_proto[] = {
118 JS_PROP_STRING_DEF("[Symbol.toStringTag]", "a.trajpoly3", 0),
119 JS_CGETSET_MAGIC_DEF("p", liba_trajpoly3_get, NULL, self_p_),
120 JS_CGETSET_MAGIC_DEF("v", liba_trajpoly3_get, NULL, self_v_),
121 JS_CGETSET_MAGIC_DEF("a", liba_trajpoly3_get, NULL, self_a_),
122 JS_CFUNC_DEF("gen", 5, liba_trajpoly3_gen),
123 JS_CFUNC_DEF("pos", 1, liba_trajpoly3_pos),
124 JS_CFUNC_DEF("vel", 1, liba_trajpoly3_vel),
125 JS_CFUNC_DEF("acc", 1, liba_trajpoly3_acc),
128 int js_liba_trajpoly3_init(JSContext *ctx, JSModuleDef *m)
130 JS_NewClassID(&liba_trajpoly3_class_id);
131 JS_NewClass(JS_GetRuntime(ctx), liba_trajpoly3_class_id, &liba_trajpoly3_class);
133 JSValue const proto = JS_NewObject(ctx);
134 JS_SetPropertyFunctionList(ctx, proto, liba_trajpoly3_proto, A_LEN(liba_trajpoly3_proto));
136 JSValue const clazz = JS_NewCFunction2(ctx, liba_trajpoly3_ctor, "trajpoly3", 5, JS_CFUNC_constructor, 0);
137 JS_SetConstructor(ctx, clazz, proto);
138 JS_SetClassProto(ctx, liba_trajpoly3_class_id, proto);
140 return JS_SetModuleExport(ctx, m, "trajpoly3", clazz);