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};
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]));
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
);
39 JS_FreeValue(ctx
, clazz
);
43 static JSValue
liba_hpf_gen(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
45 a_hpf
*const self
= (a_hpf
*)JS_GetOpaque2(ctx
, this_val
, liba_hpf_class_id
);
46 if (!self
) { return JS_EXCEPTION
; }
47 double args
[] = {0, 0};
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_HPF_GEN(args
[0], args
[1]);
56 if (JS_ToFloat64(ctx
, &args
[0], argv
[0])) { return JS_EXCEPTION
; }
57 self
->alpha
= (a_float
)args
[0];
62 static JSValue
liba_hpf_iter(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
65 a_hpf
*const self
= (a_hpf
*)JS_GetOpaque2(ctx
, this_val
, liba_hpf_class_id
);
66 if (!self
) { return JS_EXCEPTION
; }
68 if (JS_ToFloat64(ctx
, &x
, argv
[0])) { return JS_EXCEPTION
; }
69 return JS_NewFloat64(ctx
, (double)a_hpf_iter(self
, (a_float
)x
));
72 static JSValue
liba_hpf_zero(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
76 a_hpf
*const self
= (a_hpf
*)JS_GetOpaque2(ctx
, this_val
, liba_hpf_class_id
);
77 if (!self
) { return JS_EXCEPTION
; }
89 static JSValue
liba_hpf_get(JSContext
*ctx
, JSValueConst this_val
, int magic
)
91 a_hpf
*const self
= (a_hpf
*)JS_GetOpaque2(ctx
, this_val
, liba_hpf_class_id
);
92 if (!self
) { return JS_EXCEPTION
; }
96 case self_alpha_
: x
= (double)self
->alpha
; break;
97 case self_output_
: x
= (double)self
->output
; break;
98 case self_input_
: x
= (double)self
->input
; break;
99 default: return JS_UNDEFINED
;
101 return JS_NewFloat64(ctx
, x
);
104 static JSCFunctionListEntry
const liba_hpf_proto
[] = {
105 JS_PROP_STRING_DEF("[Symbol.toStringTag]", "a.hpf", 0),
106 JS_CGETSET_MAGIC_DEF("alpha", liba_hpf_get
, NULL
, self_alpha_
),
107 JS_CGETSET_MAGIC_DEF("output", liba_hpf_get
, NULL
, self_output_
),
108 JS_CGETSET_MAGIC_DEF("input", liba_hpf_get
, NULL
, self_input_
),
109 JS_CFUNC_DEF("gen", 2, liba_hpf_gen
),
110 JS_CFUNC_DEF("iter", 1, liba_hpf_iter
),
111 JS_CFUNC_DEF("zero", 0, liba_hpf_zero
),
114 int js_liba_hpf_init(JSContext
*ctx
, JSModuleDef
*m
)
116 JS_NewClassID(&liba_hpf_class_id
);
117 JS_NewClass(JS_GetRuntime(ctx
), liba_hpf_class_id
, &liba_hpf_class
);
119 JSValue
const proto
= JS_NewObject(ctx
);
120 JS_SetPropertyFunctionList(ctx
, proto
, liba_hpf_proto
, A_LEN(liba_hpf_proto
));
122 JSValue
const clazz
= JS_NewCFunction2(ctx
, liba_hpf_ctor
, "hpf", 2, JS_CFUNC_constructor
, 0);
123 JS_SetConstructor(ctx
, clazz
, proto
);
124 JS_SetClassProto(ctx
, liba_hpf_class_id
, proto
);
126 return JS_SetModuleExport(ctx
, m
, "hpf", clazz
);