rename other to that
[liba.git] / quickjs / src / hpf.c
blobc7f98646deeb201d0d9bee73ebf2ce846688227f
1 #include "a.h"
2 #include "a/hpf.h"
4 static JSClassID liba_hpf_class_id;
6 static void liba_hpf_finalizer(JSRuntime *rt, JSValue val)
8 js_free_rt(rt, JS_GetOpaque(val, liba_hpf_class_id));
11 static JSClassDef liba_hpf_class = {"hpf", .finalizer = liba_hpf_finalizer};
13 static JSValue liba_hpf_ctor(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst *argv)
15 JSValue proto, clazz = JS_UNDEFINED;
16 a_hpf *const self = (a_hpf *)js_mallocz(ctx, sizeof(a_hpf));
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_hpf_init(self, A_HPF_GEN(args[0], args[1]));
25 else
27 if (JS_ToFloat64(ctx, &args[0], argv[0])) { goto fail; }
28 a_hpf_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_hpf_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_hpf_get(JSContext *ctx, JSValueConst this_val, int magic)
45 a_hpf *const self = (a_hpf *)JS_GetOpaque2(ctx, this_val, liba_hpf_class_id);
46 if (!self) { return JS_EXCEPTION; }
47 double x;
48 switch (magic)
50 case 0: x = (double)self->alpha; break;
51 case 1: x = (double)self->output; break;
52 case 2: x = (double)self->input; break;
53 default: return JS_UNDEFINED;
55 return JS_NewFloat64(ctx, x);
58 static JSValue liba_hpf_gen(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
60 a_hpf *const self = (a_hpf *)JS_GetOpaque2(ctx, this_val, liba_hpf_class_id);
61 if (!self) { return JS_EXCEPTION; }
62 double args[] = {0, 0};
63 if (argc > 1)
65 if (JS_ToFloat64(ctx, &args[0], argv[0])) { return JS_EXCEPTION; }
66 if (JS_ToFloat64(ctx, &args[1], argv[1])) { return JS_EXCEPTION; }
67 self->alpha = A_HPF_GEN(args[0], args[1]);
69 else
71 if (JS_ToFloat64(ctx, &args[0], argv[0])) { return JS_EXCEPTION; }
72 self->alpha = (a_float)args[0];
74 return JS_UNDEFINED;
77 static JSValue liba_hpf_iter(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
79 (void)argc;
80 a_hpf *const self = (a_hpf *)JS_GetOpaque2(ctx, this_val, liba_hpf_class_id);
81 if (!self) { return JS_EXCEPTION; }
82 double x;
83 if (JS_ToFloat64(ctx, &x, argv[0])) { return JS_EXCEPTION; }
84 return JS_NewFloat64(ctx, (double)a_hpf_iter(self, (a_float)x));
87 static JSValue liba_hpf_zero(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
89 (void)argc;
90 (void)argv;
91 a_hpf *const self = (a_hpf *)JS_GetOpaque2(ctx, this_val, liba_hpf_class_id);
92 if (!self) { return JS_EXCEPTION; }
93 a_hpf_zero(self);
94 return JS_UNDEFINED;
97 static JSCFunctionListEntry const liba_hpf_proto[] = {
98 JS_PROP_STRING_DEF("[Symbol.toStringTag]", "a.hpf", 0),
99 JS_CGETSET_MAGIC_DEF("alpha", liba_hpf_get, NULL, 0),
100 JS_CGETSET_MAGIC_DEF("output", liba_hpf_get, NULL, 1),
101 JS_CGETSET_MAGIC_DEF("input", liba_hpf_get, NULL, 2),
102 JS_CFUNC_DEF("gen", 2, liba_hpf_gen),
103 JS_CFUNC_DEF("iter", 1, liba_hpf_iter),
104 JS_CFUNC_DEF("zero", 0, liba_hpf_zero),
107 int js_liba_hpf_init(JSContext *ctx, JSModuleDef *m)
109 JS_NewClassID(&liba_hpf_class_id);
110 JS_NewClass(JS_GetRuntime(ctx), liba_hpf_class_id, &liba_hpf_class);
112 JSValue const proto = JS_NewObject(ctx);
113 JS_SetPropertyFunctionList(ctx, proto, liba_hpf_proto, A_LEN(liba_hpf_proto));
115 JSValue const clazz = JS_NewCFunction2(ctx, liba_hpf_ctor, "hpf", 2, JS_CFUNC_constructor, 0);
116 JS_SetConstructor(ctx, clazz, proto);
117 JS_SetClassProto(ctx, liba_hpf_class_id, proto);
119 return JS_SetModuleExport(ctx, m, "hpf", clazz);