rename A_VERSION_{C,EX} to A_VERSION_{0,1,2,3,4}
[liba.git] / quickjs / src / lpf.c
blob8d36a74126086210f3d6c52ff9dfd63e230d2490
1 #include "a.h"
2 #include "a/lpf.h"
4 static JSClassID liba_lpf_class_id;
6 static void liba_lpf_finalizer(JSRuntime *rt, JSValue val)
8 js_free_rt(rt, JS_GetOpaque(val, liba_lpf_class_id));
11 static JSClassDef liba_lpf_class = {"lpf", .finalizer = liba_lpf_finalizer};
13 static JSValue liba_lpf_ctor(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst *argv)
15 JSValue proto, clazz = JS_UNDEFINED;
16 a_lpf *const self = (a_lpf *)js_mallocz(ctx, sizeof(a_lpf));
17 if (!self) { return JS_EXCEPTION; }
18 double args[] = {0, 0};
19 if (argc > 1)
21 if (JS_ToFloat64(ctx, &args[0], argv[0])) { goto fail; }
22 if (JS_ToFloat64(ctx, &args[1], argv[1])) { goto fail; }
23 a_lpf_init(self, A_LPF_GEN(args[0], args[1]));
25 else
27 if (JS_ToFloat64(ctx, &args[0], argv[0])) { goto fail; }
28 a_lpf_init(self, (a_float)args[0]);
30 proto = JS_GetPropertyStr(ctx, new_target, "prototype");
31 if (JS_IsException(proto)) { goto fail; }
32 clazz = JS_NewObjectProtoClass(ctx, proto, liba_lpf_class_id);
33 JS_FreeValue(ctx, proto);
34 if (JS_IsException(clazz)) { goto fail; }
35 JS_SetOpaque(clazz, self);
36 return clazz;
37 fail:
38 js_free(ctx, self);
39 JS_FreeValue(ctx, clazz);
40 return JS_UNDEFINED;
43 static JSValue liba_lpf_gen(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
45 a_lpf *const self = (a_lpf *)JS_GetOpaque2(ctx, this_val, liba_lpf_class_id);
46 if (!self) { return JS_EXCEPTION; }
47 double args[] = {0, 0};
48 if (argc > 1)
50 if (JS_ToFloat64(ctx, &args[0], argv[0])) { return JS_EXCEPTION; }
51 if (JS_ToFloat64(ctx, &args[1], argv[1])) { return JS_EXCEPTION; }
52 self->alpha = A_LPF_GEN(args[0], args[1]);
54 else
56 if (JS_ToFloat64(ctx, &args[0], argv[0])) { return JS_EXCEPTION; }
57 self->alpha = (a_float)args[0];
59 return JS_UNDEFINED;
62 static JSValue liba_lpf_iter(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
64 (void)argc;
65 a_lpf *const self = (a_lpf *)JS_GetOpaque2(ctx, this_val, liba_lpf_class_id);
66 if (!self) { return JS_EXCEPTION; }
67 double x;
68 if (JS_ToFloat64(ctx, &x, argv[0])) { return JS_EXCEPTION; }
69 return JS_NewFloat64(ctx, (double)a_lpf_iter(self, (a_float)x));
72 static JSValue liba_lpf_zero(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
74 (void)argc;
75 (void)argv;
76 a_lpf *const self = (a_lpf *)JS_GetOpaque2(ctx, this_val, liba_lpf_class_id);
77 if (!self) { return JS_EXCEPTION; }
78 a_lpf_zero(self);
79 return JS_UNDEFINED;
82 enum
84 self_alpha_,
85 self_output_,
88 static JSValue liba_lpf_get(JSContext *ctx, JSValueConst this_val, int magic)
90 a_lpf *const self = (a_lpf *)JS_GetOpaque2(ctx, this_val, liba_lpf_class_id);
91 if (!self) { return JS_EXCEPTION; }
92 double x;
93 switch (magic)
95 case self_alpha_: x = (double)self->alpha; break;
96 case self_output_: x = (double)self->output; break;
97 default: return JS_UNDEFINED;
99 return JS_NewFloat64(ctx, x);
102 static JSCFunctionListEntry const liba_lpf_proto[] = {
103 JS_PROP_STRING_DEF("[Symbol.toStringTag]", "a.lpf", 0),
104 JS_CGETSET_MAGIC_DEF("alpha", liba_lpf_get, NULL, self_alpha_),
105 JS_CGETSET_MAGIC_DEF("output", liba_lpf_get, NULL, self_output_),
106 JS_CFUNC_DEF("gen", 2, liba_lpf_gen),
107 JS_CFUNC_DEF("iter", 1, liba_lpf_iter),
108 JS_CFUNC_DEF("zero", 0, liba_lpf_zero),
111 int js_liba_lpf_init(JSContext *ctx, JSModuleDef *m)
113 JS_NewClassID(&liba_lpf_class_id);
114 JS_NewClass(JS_GetRuntime(ctx), liba_lpf_class_id, &liba_lpf_class);
116 JSValue const proto = JS_NewObject(ctx);
117 JS_SetPropertyFunctionList(ctx, proto, liba_lpf_proto, A_LEN(liba_lpf_proto));
119 JSValue const clazz = JS_NewCFunction2(ctx, liba_lpf_ctor, "lpf", 2, JS_CFUNC_constructor, 0);
120 JS_SetConstructor(ctx, clazz, proto);
121 JS_SetClassProto(ctx, liba_lpf_class_id, proto);
123 return JS_SetModuleExport(ctx, m, "lpf", clazz);