fix atan2,rename {hypot,hypot3} to {norm2,norm3}
[liba.git] / quickjs / src / trajpoly5.c
blobd2c549ca1ea834f69372fd2c95bc464a00ebe4bb
1 #include "a.h"
2 #include "a/trajpoly5.h"
4 static JSClassID liba_trajpoly5_class_id;
6 static void liba_trajpoly5_finalizer(JSRuntime *rt, JSValue val)
8 js_free_rt(rt, JS_GetOpaque(val, liba_trajpoly5_class_id));
11 static JSValue liba_trajpoly5_ctor(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst *argv)
13 double arg[] = {0, 0, 0, 0, 0, 0, 0};
14 int i = (int)A_LEN(arg);
15 JSValue proto, clazz = JS_UNDEFINED;
16 a_trajpoly5 *const self = (a_trajpoly5 *)js_mallocz(ctx, sizeof(a_trajpoly5));
17 if (!self) { return JS_EXCEPTION; }
18 if (argc > i) { argc = i; }
19 for (i = 0; i < 3; ++i)
21 if (JS_ToFloat64(ctx, &arg[i], argv[i])) { goto fail; }
23 for (i = 3; i < argc; ++i)
25 if (JS_ToFloat64(ctx, &arg[i], argv[i])) { goto fail; }
27 a_trajpoly5_gen(self, (a_float)arg[0],
28 (a_float)arg[1], (a_float)arg[2],
29 (a_float)arg[3], (a_float)arg[4],
30 (a_float)arg[5], (a_float)arg[6]);
31 proto = JS_GetPropertyStr(ctx, new_target, "prototype");
32 if (JS_IsException(proto)) { goto fail; }
33 clazz = JS_NewObjectProtoClass(ctx, proto, liba_trajpoly5_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_trajpoly5_gen(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
46 double arg[] = {0, 0, 0, 0, 0, 0, 0};
47 int i = (int)A_LEN(arg);
48 a_trajpoly5 *const self = (a_trajpoly5 *)JS_GetOpaque2(ctx, this_val, liba_trajpoly5_class_id);
49 if (!self) { return JS_EXCEPTION; }
50 if (argc > i) { argc = i; }
51 for (i = 0; i < 3; ++i)
53 if (JS_ToFloat64(ctx, &arg[i], argv[i])) { return JS_EXCEPTION; }
55 for (i = 3; i < argc; ++i)
57 if (JS_ToFloat64(ctx, &arg[i], argv[i])) { return JS_EXCEPTION; }
59 a_trajpoly5_gen(self, (a_float)arg[0],
60 (a_float)arg[1], (a_float)arg[2],
61 (a_float)arg[3], (a_float)arg[4],
62 (a_float)arg[5], (a_float)arg[6]);
63 return JS_UNDEFINED;
66 static JSValue liba_trajpoly5_pos(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
68 double x;
69 a_float pos;
70 a_trajpoly5 *const self = (a_trajpoly5 *)JS_GetOpaque2(ctx, this_val, liba_trajpoly5_class_id);
71 if (!self) { return JS_EXCEPTION; }
72 if (JS_ToFloat64(ctx, &x, argv[0])) { return JS_EXCEPTION; }
73 pos = a_trajpoly5_pos(self, (a_float)x);
74 (void)argc;
75 return JS_NewFloat64(ctx, (double)pos);
78 static JSValue liba_trajpoly5_vel(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
80 double x;
81 a_float vel;
82 a_trajpoly5 *const self = (a_trajpoly5 *)JS_GetOpaque2(ctx, this_val, liba_trajpoly5_class_id);
83 if (!self) { return JS_EXCEPTION; }
84 if (JS_ToFloat64(ctx, &x, argv[0])) { return JS_EXCEPTION; }
85 vel = a_trajpoly5_vel(self, (a_float)x);
86 (void)argc;
87 return JS_NewFloat64(ctx, (double)vel);
90 static JSValue liba_trajpoly5_acc(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
92 double x;
93 a_float acc;
94 a_trajpoly5 *const self = (a_trajpoly5 *)JS_GetOpaque2(ctx, this_val, liba_trajpoly5_class_id);
95 if (!self) { return JS_EXCEPTION; }
96 if (JS_ToFloat64(ctx, &x, argv[0])) { return JS_EXCEPTION; }
97 acc = a_trajpoly5_acc(self, (a_float)x);
98 (void)argc;
99 return JS_NewFloat64(ctx, (double)acc);
102 enum
104 self_p,
105 self_v,
106 self_a
109 static JSValue liba_trajpoly5_get(JSContext *ctx, JSValueConst this_val, int magic)
111 a_trajpoly5 *const self = (a_trajpoly5 *)JS_GetOpaque2(ctx, this_val, liba_trajpoly5_class_id);
112 if (!self) { return JS_EXCEPTION; }
113 switch (magic)
115 case self_p: return js_array_num_new(ctx, self->p, A_LEN(self->p));
116 case self_v: return js_array_num_new(ctx, self->v, A_LEN(self->v));
117 case self_a: return js_array_num_new(ctx, self->a, A_LEN(self->a));
118 default: return JS_UNDEFINED;
122 static JSClassDef liba_trajpoly5_class;
123 static JSCFunctionListEntry const liba_trajpoly5_proto[] = {
124 JS_PROP_STRING_DEF("[Symbol.toStringTag]", "a.trajpoly5", 0),
125 JS_CGETSET_MAGIC_DEF("p", liba_trajpoly5_get, NULL, self_p),
126 JS_CGETSET_MAGIC_DEF("v", liba_trajpoly5_get, NULL, self_v),
127 JS_CGETSET_MAGIC_DEF("a", liba_trajpoly5_get, NULL, self_a),
128 JS_CFUNC_DEF("gen", 7, liba_trajpoly5_gen),
129 JS_CFUNC_DEF("pos", 1, liba_trajpoly5_pos),
130 JS_CFUNC_DEF("vel", 1, liba_trajpoly5_vel),
131 JS_CFUNC_DEF("acc", 1, liba_trajpoly5_acc),
134 int js_liba_trajpoly5_init(JSContext *ctx, JSModuleDef *m)
136 JSValue proto, clazz;
137 liba_trajpoly5_class.class_name = "trajpoly5";
138 liba_trajpoly5_class.finalizer = liba_trajpoly5_finalizer;
140 JS_NewClassID(&liba_trajpoly5_class_id);
141 JS_NewClass(JS_GetRuntime(ctx), liba_trajpoly5_class_id, &liba_trajpoly5_class);
143 proto = JS_NewObject(ctx);
144 JS_SetPropertyFunctionList(ctx, proto, liba_trajpoly5_proto, A_LEN(liba_trajpoly5_proto));
146 clazz = JS_NewCFunction2(ctx, liba_trajpoly5_ctor, "trajpoly5", 7, JS_CFUNC_constructor, 0);
147 JS_SetClassProto(ctx, liba_trajpoly5_class_id, proto);
148 JS_SetConstructor(ctx, clazz, proto);
150 return JS_SetModuleExport(ctx, m, "trajpoly5", clazz);