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 JSClassDef liba_trajpoly5_class
= {"trajpoly5", .finalizer
= liba_trajpoly5_finalizer
};
13 static JSValue
liba_trajpoly5_ctor(JSContext
*ctx
, JSValueConst new_target
, int argc
, JSValueConst
*argv
)
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 double args
[] = {0, 0, 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_trajpoly5_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 (a_float
)args
[5], (a_float
)args
[6]);
32 proto
= JS_GetPropertyStr(ctx
, new_target
, "prototype");
33 if (JS_IsException(proto
)) { goto fail
; }
34 clazz
= JS_NewObjectProtoClass(ctx
, proto
, liba_trajpoly5_class_id
);
35 JS_FreeValue(ctx
, proto
);
36 if (JS_IsException(clazz
)) { goto fail
; }
37 JS_SetOpaque(clazz
, self
);
41 JS_FreeValue(ctx
, clazz
);
45 static JSValue
liba_trajpoly5_gen(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
47 a_trajpoly5
*const self
= (a_trajpoly5
*)JS_GetOpaque2(ctx
, this_val
, liba_trajpoly5_class_id
);
48 if (!self
) { return JS_EXCEPTION
; }
49 double args
[] = {0, 0, 0, 0, 0, 0, 0};
50 if (argc
> (int)A_LEN(args
)) { argc
= (int)A_LEN(args
); }
51 for (int i
= 0; i
< 3; ++i
)
53 if (JS_ToFloat64(ctx
, &args
[i
], argv
[i
])) { return JS_EXCEPTION
; }
55 for (int i
= 3; i
< argc
; ++i
)
57 if (JS_ToFloat64(ctx
, &args
[i
], argv
[i
])) { return JS_EXCEPTION
; }
59 a_trajpoly5_gen(self
, (a_float
)args
[0],
60 (a_float
)args
[1], (a_float
)args
[2],
61 (a_float
)args
[3], (a_float
)args
[4],
62 (a_float
)args
[5], (a_float
)args
[6]);
66 static JSValue
liba_trajpoly5_pos(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
69 a_trajpoly5
*const self
= (a_trajpoly5
*)JS_GetOpaque2(ctx
, this_val
, liba_trajpoly5_class_id
);
70 if (!self
) { return JS_EXCEPTION
; }
72 if (JS_ToFloat64(ctx
, &x
, argv
[0])) { return JS_EXCEPTION
; }
73 a_float pos
= a_trajpoly5_pos(self
, (a_float
)x
);
74 return JS_NewFloat64(ctx
, (double)pos
);
77 static JSValue
liba_trajpoly5_vel(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
80 a_trajpoly5
*const self
= (a_trajpoly5
*)JS_GetOpaque2(ctx
, this_val
, liba_trajpoly5_class_id
);
81 if (!self
) { return JS_EXCEPTION
; }
83 if (JS_ToFloat64(ctx
, &x
, argv
[0])) { return JS_EXCEPTION
; }
84 a_float vel
= a_trajpoly5_vel(self
, (a_float
)x
);
85 return JS_NewFloat64(ctx
, (double)vel
);
88 static JSValue
liba_trajpoly5_acc(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
91 a_trajpoly5
*const self
= (a_trajpoly5
*)JS_GetOpaque2(ctx
, this_val
, liba_trajpoly5_class_id
);
92 if (!self
) { return JS_EXCEPTION
; }
94 if (JS_ToFloat64(ctx
, &x
, argv
[0])) { return JS_EXCEPTION
; }
95 a_float acc
= a_trajpoly5_acc(self
, (a_float
)x
);
96 return JS_NewFloat64(ctx
, (double)acc
);
106 static JSValue
liba_trajpoly5_get(JSContext
*ctx
, JSValueConst this_val
, int magic
)
108 a_trajpoly5
*const self
= (a_trajpoly5
*)JS_GetOpaque2(ctx
, this_val
, liba_trajpoly5_class_id
);
109 if (!self
) { return JS_EXCEPTION
; }
112 case self_p
: return js_array_num_new(ctx
, self
->p
, A_LEN(self
->p
));
113 case self_v
: return js_array_num_new(ctx
, self
->v
, A_LEN(self
->v
));
114 case self_a
: return js_array_num_new(ctx
, self
->a
, A_LEN(self
->a
));
115 default: return JS_UNDEFINED
;
119 static JSCFunctionListEntry
const liba_trajpoly5_proto
[] = {
120 JS_PROP_STRING_DEF("[Symbol.toStringTag]", "a.trajpoly5", 0),
121 JS_CGETSET_MAGIC_DEF("p", liba_trajpoly5_get
, NULL
, self_p
),
122 JS_CGETSET_MAGIC_DEF("v", liba_trajpoly5_get
, NULL
, self_v
),
123 JS_CGETSET_MAGIC_DEF("a", liba_trajpoly5_get
, NULL
, self_a
),
124 JS_CFUNC_DEF("gen", 7, liba_trajpoly5_gen
),
125 JS_CFUNC_DEF("pos", 1, liba_trajpoly5_pos
),
126 JS_CFUNC_DEF("vel", 1, liba_trajpoly5_vel
),
127 JS_CFUNC_DEF("acc", 1, liba_trajpoly5_acc
),
130 int js_liba_trajpoly5_init(JSContext
*ctx
, JSModuleDef
*m
)
132 JS_NewClassID(&liba_trajpoly5_class_id
);
133 JS_NewClass(JS_GetRuntime(ctx
), liba_trajpoly5_class_id
, &liba_trajpoly5_class
);
135 JSValue
const proto
= JS_NewObject(ctx
);
136 JS_SetPropertyFunctionList(ctx
, proto
, liba_trajpoly5_proto
, A_LEN(liba_trajpoly5_proto
));
138 JSValue
const clazz
= JS_NewCFunction2(ctx
, liba_trajpoly5_ctor
, "trajpoly5", 7, JS_CFUNC_constructor
, 0);
139 JS_SetConstructor(ctx
, clazz
, proto
);
140 JS_SetClassProto(ctx
, liba_trajpoly5_class_id
, proto
);
142 return JS_SetModuleExport(ctx
, m
, "trajpoly5", clazz
);