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
)
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
);
29 JS_FreeValue(ctx
, clazz
);
33 static JSValue
liba_trajbell_get(JSContext
*ctx
, JSValueConst this_val
, int magic
)
35 a_trajbell
*const self
= (a_trajbell
*)JS_GetOpaque2(ctx
, this_val
, liba_trajbell_class_id
);
36 if (!self
) { return JS_EXCEPTION
; }
40 case 0: x
= (double)self
->t
; break;
41 case 1: x
= (double)self
->tv
; break;
42 case 2: x
= (double)self
->ta
; break;
43 case 3: x
= (double)self
->td
; break;
44 case 4: x
= (double)self
->taj
; break;
45 case 5: x
= (double)self
->tdj
; break;
46 case 6: x
= (double)self
->p0
; break;
47 case 7: x
= (double)self
->p1
; break;
48 case 8: x
= (double)self
->v0
; break;
49 case 9: x
= (double)self
->v1
; break;
50 case 10: x
= (double)self
->vm
; break;
51 case 11: x
= (double)self
->jm
; break;
52 case 12: x
= (double)self
->am
; break;
53 case 13: x
= (double)self
->dm
; break;
54 default: return JS_UNDEFINED
;
56 return JS_NewFloat64(ctx
, x
);
59 static JSValue
liba_trajbell_gen(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
61 a_trajbell
*const self
= (a_trajbell
*)JS_GetOpaque2(ctx
, this_val
, liba_trajbell_class_id
);
62 if (!self
) { return JS_EXCEPTION
; }
63 double args
[] = {0, 0, 0, 0, 0, 0, 0};
64 if (argc
> (int)A_LEN(args
)) { argc
= (int)A_LEN(args
); }
65 for (int i
= 0; i
< 5; ++i
)
67 if (JS_ToFloat64(ctx
, &args
[i
], argv
[i
])) { return JS_EXCEPTION
; }
69 for (int i
= 5; i
< argc
; ++i
)
71 if (JS_ToFloat64(ctx
, &args
[i
], argv
[i
])) { return JS_EXCEPTION
; }
73 args
[0] = (double)a_trajbell_gen(self
, (a_float
)args
[0], (a_float
)args
[1], (a_float
)args
[2],
74 (a_float
)args
[3], (a_float
)args
[4], (a_float
)args
[5], (a_float
)args
[6]);
75 return JS_NewFloat64(ctx
, args
[0]);
78 static JSValue
liba_trajbell_pos(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
81 a_trajbell
*const self
= (a_trajbell
*)JS_GetOpaque2(ctx
, this_val
, liba_trajbell_class_id
);
82 if (!self
) { return JS_EXCEPTION
; }
84 if (JS_ToFloat64(ctx
, &dt
, argv
[0])) { return JS_EXCEPTION
; }
85 a_float pos
= a_trajbell_pos(self
, (a_float
)dt
);
86 return JS_NewFloat64(ctx
, (double)pos
);
89 static JSValue
liba_trajbell_vel(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
92 a_trajbell
*const self
= (a_trajbell
*)JS_GetOpaque2(ctx
, this_val
, liba_trajbell_class_id
);
93 if (!self
) { return JS_EXCEPTION
; }
95 if (JS_ToFloat64(ctx
, &dt
, argv
[0])) { return JS_EXCEPTION
; }
96 a_float vel
= a_trajbell_vel(self
, (a_float
)dt
);
97 return JS_NewFloat64(ctx
, (double)vel
);
100 static JSValue
liba_trajbell_acc(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
103 a_trajbell
*const self
= (a_trajbell
*)JS_GetOpaque2(ctx
, this_val
, liba_trajbell_class_id
);
104 if (!self
) { return JS_EXCEPTION
; }
106 if (JS_ToFloat64(ctx
, &dt
, argv
[0])) { return JS_EXCEPTION
; }
107 a_float acc
= a_trajbell_acc(self
, (a_float
)dt
);
108 return JS_NewFloat64(ctx
, (double)acc
);
111 static JSValue
liba_trajbell_jer(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
114 a_trajbell
*const self
= (a_trajbell
*)JS_GetOpaque2(ctx
, this_val
, liba_trajbell_class_id
);
115 if (!self
) { return JS_EXCEPTION
; }
117 if (JS_ToFloat64(ctx
, &dt
, argv
[0])) { return JS_EXCEPTION
; }
118 a_float acc
= a_trajbell_jer(self
, (a_float
)dt
);
119 return JS_NewFloat64(ctx
, (double)acc
);
122 static JSCFunctionListEntry
const liba_trajbell_proto
[] = {
123 JS_PROP_STRING_DEF("[Symbol.toStringTag]", "a.trajbell", 0),
124 JS_CGETSET_MAGIC_DEF("t", liba_trajbell_get
, NULL
, 0),
125 JS_CGETSET_MAGIC_DEF("tv", liba_trajbell_get
, NULL
, 1),
126 JS_CGETSET_MAGIC_DEF("ta", liba_trajbell_get
, NULL
, 2),
127 JS_CGETSET_MAGIC_DEF("td", liba_trajbell_get
, NULL
, 3),
128 JS_CGETSET_MAGIC_DEF("taj", liba_trajbell_get
, NULL
, 4),
129 JS_CGETSET_MAGIC_DEF("tdj", liba_trajbell_get
, NULL
, 5),
130 JS_CGETSET_MAGIC_DEF("p0", liba_trajbell_get
, NULL
, 6),
131 JS_CGETSET_MAGIC_DEF("p1", liba_trajbell_get
, NULL
, 7),
132 JS_CGETSET_MAGIC_DEF("v0", liba_trajbell_get
, NULL
, 8),
133 JS_CGETSET_MAGIC_DEF("v1", liba_trajbell_get
, NULL
, 9),
134 JS_CGETSET_MAGIC_DEF("vm", liba_trajbell_get
, NULL
, 10),
135 JS_CGETSET_MAGIC_DEF("jm", liba_trajbell_get
, NULL
, 11),
136 JS_CGETSET_MAGIC_DEF("am", liba_trajbell_get
, NULL
, 12),
137 JS_CGETSET_MAGIC_DEF("dm", liba_trajbell_get
, NULL
, 13),
138 JS_CFUNC_DEF("gen", 7, liba_trajbell_gen
),
139 JS_CFUNC_DEF("pos", 1, liba_trajbell_pos
),
140 JS_CFUNC_DEF("vel", 1, liba_trajbell_vel
),
141 JS_CFUNC_DEF("acc", 1, liba_trajbell_acc
),
142 JS_CFUNC_DEF("jer", 1, liba_trajbell_jer
),
145 int js_liba_trajbell_init(JSContext
*ctx
, JSModuleDef
*m
)
147 JS_NewClassID(&liba_trajbell_class_id
);
148 JS_NewClass(JS_GetRuntime(ctx
), liba_trajbell_class_id
, &liba_trajbell_class
);
150 JSValue
const proto
= JS_NewObject(ctx
);
151 JS_SetPropertyFunctionList(ctx
, proto
, liba_trajbell_proto
, A_LEN(liba_trajbell_proto
));
153 JSValue
const clazz
= JS_NewCFunction2(ctx
, liba_trajbell_ctor
, "trajbell", 0, JS_CFUNC_constructor
, 0);
154 JS_SetConstructor(ctx
, clazz
, proto
);
155 JS_SetClassProto(ctx
, liba_trajbell_class_id
, proto
);
157 return JS_SetModuleExport(ctx
, m
, "trajbell", clazz
);