2 #include "a/trajtrap.h"
4 static JSClassID liba_trajtrap_class_id
;
6 static void liba_trajtrap_finalizer(JSRuntime
*rt
, JSValue val
)
8 js_free_rt(rt
, JS_GetOpaque(val
, liba_trajtrap_class_id
));
11 static JSClassDef liba_trajtrap_class
= {"trajtrap", .finalizer
= liba_trajtrap_finalizer
};
13 static JSValue
liba_trajtrap_ctor(JSContext
*ctx
, JSValueConst new_target
, int argc
, JSValueConst
*argv
)
17 JSValue proto
, clazz
= JS_UNDEFINED
;
18 a_trajtrap
*const self
= (a_trajtrap
*)js_mallocz(ctx
, sizeof(a_trajtrap
));
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_trajtrap_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_trajtrap_gen(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
35 a_trajtrap
*const self
= (a_trajtrap
*)JS_GetOpaque2(ctx
, this_val
, liba_trajtrap_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_trajtrap_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_trajtrap_pos(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
55 a_trajtrap
*const self
= (a_trajtrap
*)JS_GetOpaque2(ctx
, this_val
, liba_trajtrap_class_id
);
56 if (!self
) { return JS_EXCEPTION
; }
58 if (JS_ToFloat64(ctx
, &x
, argv
[0])) { return JS_EXCEPTION
; }
59 a_float pos
= a_trajtrap_pos(self
, (a_float
)x
);
60 return JS_NewFloat64(ctx
, (double)pos
);
63 static JSValue
liba_trajtrap_vel(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
66 a_trajtrap
*const self
= (a_trajtrap
*)JS_GetOpaque2(ctx
, this_val
, liba_trajtrap_class_id
);
67 if (!self
) { return JS_EXCEPTION
; }
69 if (JS_ToFloat64(ctx
, &x
, argv
[0])) { return JS_EXCEPTION
; }
70 a_float vel
= a_trajtrap_vel(self
, (a_float
)x
);
71 return JS_NewFloat64(ctx
, (double)vel
);
74 static JSValue
liba_trajtrap_acc(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
77 a_trajtrap
*const self
= (a_trajtrap
*)JS_GetOpaque2(ctx
, this_val
, liba_trajtrap_class_id
);
78 if (!self
) { return JS_EXCEPTION
; }
80 if (JS_ToFloat64(ctx
, &x
, argv
[0])) { return JS_EXCEPTION
; }
81 a_float acc
= a_trajtrap_acc(self
, (a_float
)x
);
82 return JS_NewFloat64(ctx
, (double)acc
);
101 static JSValue
liba_trajtrap_get(JSContext
*ctx
, JSValueConst this_val
, int magic
)
103 a_trajtrap
*const self
= (a_trajtrap
*)JS_GetOpaque2(ctx
, this_val
, liba_trajtrap_class_id
);
104 if (!self
) { return JS_EXCEPTION
; }
108 case self_t_
: x
= (double)self
->t
; break;
109 case self_p0_
: x
= (double)self
->p0
; break;
110 case self_p1_
: x
= (double)self
->p1
; break;
111 case self_v0_
: x
= (double)self
->v0
; break;
112 case self_v1_
: x
= (double)self
->v1
; break;
113 case self_vc_
: x
= (double)self
->vc
; break;
114 case self_ta_
: x
= (double)self
->ta
; break;
115 case self_td_
: x
= (double)self
->td
; break;
116 case self_pa_
: x
= (double)self
->pa
; break;
117 case self_pd_
: x
= (double)self
->pd
; break;
118 case self_ac_
: x
= (double)self
->ac
; break;
119 case self_de_
: x
= (double)self
->de
; break;
120 default: return JS_UNDEFINED
;
122 return JS_NewFloat64(ctx
, x
);
125 static JSCFunctionListEntry
const liba_trajtrap_proto
[] = {
126 JS_PROP_STRING_DEF("[Symbol.toStringTag]", "a.trajtrap", 0),
127 JS_CGETSET_MAGIC_DEF("t", liba_trajtrap_get
, NULL
, self_t_
),
128 JS_CGETSET_MAGIC_DEF("p0", liba_trajtrap_get
, NULL
, self_p0_
),
129 JS_CGETSET_MAGIC_DEF("p1", liba_trajtrap_get
, NULL
, self_p1_
),
130 JS_CGETSET_MAGIC_DEF("v0", liba_trajtrap_get
, NULL
, self_v0_
),
131 JS_CGETSET_MAGIC_DEF("v1", liba_trajtrap_get
, NULL
, self_v1_
),
132 JS_CGETSET_MAGIC_DEF("vc", liba_trajtrap_get
, NULL
, self_vc_
),
133 JS_CGETSET_MAGIC_DEF("ta", liba_trajtrap_get
, NULL
, self_ta_
),
134 JS_CGETSET_MAGIC_DEF("td", liba_trajtrap_get
, NULL
, self_td_
),
135 JS_CGETSET_MAGIC_DEF("pa", liba_trajtrap_get
, NULL
, self_pa_
),
136 JS_CGETSET_MAGIC_DEF("pd", liba_trajtrap_get
, NULL
, self_pd_
),
137 JS_CGETSET_MAGIC_DEF("ac", liba_trajtrap_get
, NULL
, self_ac_
),
138 JS_CGETSET_MAGIC_DEF("de", liba_trajtrap_get
, NULL
, self_de_
),
139 JS_CFUNC_DEF("gen", 7, liba_trajtrap_gen
),
140 JS_CFUNC_DEF("pos", 1, liba_trajtrap_pos
),
141 JS_CFUNC_DEF("vel", 1, liba_trajtrap_vel
),
142 JS_CFUNC_DEF("acc", 1, liba_trajtrap_acc
),
145 int js_liba_trajtrap_init(JSContext
*ctx
, JSModuleDef
*m
)
147 JS_NewClassID(&liba_trajtrap_class_id
);
148 JS_NewClass(JS_GetRuntime(ctx
), liba_trajtrap_class_id
, &liba_trajtrap_class
);
150 JSValue
const proto
= JS_NewObject(ctx
);
151 JS_SetPropertyFunctionList(ctx
, proto
, liba_trajtrap_proto
, A_LEN(liba_trajtrap_proto
));
153 JSValue
const clazz
= JS_NewCFunction2(ctx
, liba_trajtrap_ctor
, "trajtrap", 0, JS_CFUNC_constructor
, 0);
154 JS_SetConstructor(ctx
, clazz
, proto
);
155 JS_SetClassProto(ctx
, liba_trajtrap_class_id
, proto
);
157 return JS_SetModuleExport(ctx
, m
, "trajtrap", clazz
);