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_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
)
55 a_trajbell
*const self
= (a_trajbell
*)JS_GetOpaque2(ctx
, this_val
, liba_trajbell_class_id
);
56 if (!self
) { return JS_EXCEPTION
; }
58 if (JS_ToFloat64(ctx
, &x
, argv
[0])) { return JS_EXCEPTION
; }
59 a_float pos
= a_trajbell_pos(self
, (a_float
)x
);
60 return JS_NewFloat64(ctx
, (double)pos
);
63 static JSValue
liba_trajbell_vel(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
66 a_trajbell
*const self
= (a_trajbell
*)JS_GetOpaque2(ctx
, this_val
, liba_trajbell_class_id
);
67 if (!self
) { return JS_EXCEPTION
; }
69 if (JS_ToFloat64(ctx
, &x
, argv
[0])) { return JS_EXCEPTION
; }
70 a_float vel
= a_trajbell_vel(self
, (a_float
)x
);
71 return JS_NewFloat64(ctx
, (double)vel
);
74 static JSValue
liba_trajbell_acc(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
77 a_trajbell
*const self
= (a_trajbell
*)JS_GetOpaque2(ctx
, this_val
, liba_trajbell_class_id
);
78 if (!self
) { return JS_EXCEPTION
; }
80 if (JS_ToFloat64(ctx
, &x
, argv
[0])) { return JS_EXCEPTION
; }
81 a_float acc
= a_trajbell_acc(self
, (a_float
)x
);
82 return JS_NewFloat64(ctx
, (double)acc
);
85 static JSValue
liba_trajbell_jer(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
88 a_trajbell
*const self
= (a_trajbell
*)JS_GetOpaque2(ctx
, this_val
, liba_trajbell_class_id
);
89 if (!self
) { return JS_EXCEPTION
; }
91 if (JS_ToFloat64(ctx
, &x
, argv
[0])) { return JS_EXCEPTION
; }
92 a_float acc
= a_trajbell_jer(self
, (a_float
)x
);
93 return JS_NewFloat64(ctx
, (double)acc
);
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
; }
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
);