3 static JSValue
liba_hash_bkdr(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
9 if (JS_ToUint32(ctx
, &x
, argv
[1])) { return JS_EXCEPTION
; }
13 char const *str
= JS_ToCString(ctx
, argv
[0]);
14 x
= a_hash_bkdr(str
, x
);
15 JS_FreeCString(ctx
, str
);
17 return JS_NewUint32(ctx
, x
);
20 static JSValue
liba_hash_sdbm(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
26 if (JS_ToUint32(ctx
, &x
, argv
[1])) { return JS_EXCEPTION
; }
30 char const *str
= JS_ToCString(ctx
, argv
[0]);
31 x
= a_hash_sdbm(str
, x
);
32 JS_FreeCString(ctx
, str
);
34 return JS_NewUint32(ctx
, x
);
39 static JSValue
liba_isqrt(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
44 if (JS_ToIndex(ctx
, &x
, argv
[0])) { return JS_EXCEPTION
; }
45 return JS_NewUint32(ctx
, a_u64_sqrt(x
));
48 static JSValue
liba_rsqrt(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
53 if (JS_ToFloat64(ctx
, &x
, argv
[0])) { return JS_EXCEPTION
; }
54 return JS_NewFloat64(ctx
, a_f64_rsqrt(x
));
59 static JSCFunctionListEntry
const liba_mf_proto
[] = {
60 JS_PROP_INT32_DEF("NUL", A_MF_NUL
, 0),
61 JS_PROP_INT32_DEF("GAUSS", A_MF_GAUSS
, 0),
62 JS_PROP_INT32_DEF("GAUSS2", A_MF_GAUSS2
, 0),
63 JS_PROP_INT32_DEF("GBELL", A_MF_GBELL
, 0),
64 JS_PROP_INT32_DEF("SIG", A_MF_SIG
, 0),
65 JS_PROP_INT32_DEF("DSIG", A_MF_DSIG
, 0),
66 JS_PROP_INT32_DEF("PSIG", A_MF_PSIG
, 0),
67 JS_PROP_INT32_DEF("TRAP", A_MF_TRAP
, 0),
68 JS_PROP_INT32_DEF("TRI", A_MF_TRI
, 0),
69 JS_PROP_INT32_DEF("LINS", A_MF_LINS
, 0),
70 JS_PROP_INT32_DEF("LINZ", A_MF_LINZ
, 0),
71 JS_PROP_INT32_DEF("S", A_MF_S
, 0),
72 JS_PROP_INT32_DEF("Z", A_MF_Z
, 0),
73 JS_PROP_INT32_DEF("PI", A_MF_PI
, 0),
74 JS_CFUNC_DEF("gauss", 3, liba_mf_gauss
),
75 JS_CFUNC_DEF("gauss2", 5, liba_mf_gauss2
),
76 JS_CFUNC_DEF("gbell", 4, liba_mf_gbell
),
77 JS_CFUNC_DEF("sig", 3, liba_mf_sig
),
78 JS_CFUNC_DEF("dsig", 5, liba_mf_dsig
),
79 JS_CFUNC_DEF("psig", 5, liba_mf_psig
),
80 JS_CFUNC_DEF("trap", 5, liba_mf_trap
),
81 JS_CFUNC_DEF("tri", 4, liba_mf_tri
),
82 JS_CFUNC_DEF("lins", 3, liba_mf_lins
),
83 JS_CFUNC_DEF("linz", 3, liba_mf_linz
),
84 JS_CFUNC_DEF("s", 3, liba_mf_s
),
85 JS_CFUNC_DEF("z", 3, liba_mf_z
),
86 JS_CFUNC_DEF("pi", 5, liba_mf_pi
),
89 #if !defined A_VERSION
90 #include "a/version.h"
91 #endif /* A_VERSION */
93 static JSCFunctionListEntry
const liba_proto
[] = {
94 JS_OBJECT_DEF("mf", liba_mf_proto
, A_LEN(liba_mf_proto
), 0),
95 JS_PROP_STRING_DEF("VERSION", A_VERSION
, 0),
96 JS_CFUNC_DEF("hash_sdbm", 1, liba_hash_sdbm
),
97 JS_CFUNC_DEF("hash_bkdr", 1, liba_hash_bkdr
),
98 JS_CFUNC_DEF("isqrt", 1, liba_isqrt
),
99 JS_CFUNC_DEF("rsqrt", 1, liba_rsqrt
),
102 int js_liba_init(JSContext
*ctx
, JSModuleDef
*m
)
104 js_liba_crc16_init(ctx
, m
);
105 js_liba_crc32_init(ctx
, m
);
106 js_liba_crc64_init(ctx
, m
);
107 js_liba_crc8_init(ctx
, m
);
108 js_liba_hpf_init(ctx
, m
);
109 js_liba_lpf_init(ctx
, m
);
110 js_liba_pid_init(ctx
, m
);
111 js_liba_pid_fuzzy_init(ctx
, m
);
112 js_liba_pid_neuro_init(ctx
, m
);
113 js_liba_tf_init(ctx
, m
);
114 js_liba_trajbell_init(ctx
, m
);
115 js_liba_trajpoly3_init(ctx
, m
);
116 js_liba_trajpoly5_init(ctx
, m
);
117 js_liba_trajpoly7_init(ctx
, m
);
118 js_liba_trajtrap_init(ctx
, m
);
119 js_liba_version_init(ctx
, m
);
120 return JS_SetModuleExportList(ctx
, m
, liba_proto
, A_LEN(liba_proto
));
123 JSModuleDef
*js_init_module(JSContext
*ctx
, char const *module_name
)
125 JSModuleDef
*m
= JS_NewCModule(ctx
, module_name
, js_liba_init
);
128 JS_AddModuleExport(ctx
, m
, "crc16");
129 JS_AddModuleExport(ctx
, m
, "crc32");
130 JS_AddModuleExport(ctx
, m
, "crc64");
131 JS_AddModuleExport(ctx
, m
, "crc8");
132 JS_AddModuleExport(ctx
, m
, "hpf");
133 JS_AddModuleExport(ctx
, m
, "lpf");
134 JS_AddModuleExport(ctx
, m
, "pid");
135 JS_AddModuleExport(ctx
, m
, "pid_fuzzy");
136 JS_AddModuleExport(ctx
, m
, "pid_neuro");
137 JS_AddModuleExport(ctx
, m
, "tf");
138 JS_AddModuleExport(ctx
, m
, "trajbell");
139 JS_AddModuleExport(ctx
, m
, "trajpoly3");
140 JS_AddModuleExport(ctx
, m
, "trajpoly5");
141 JS_AddModuleExport(ctx
, m
, "trajpoly7");
142 JS_AddModuleExport(ctx
, m
, "trajtrap");
143 JS_AddModuleExport(ctx
, m
, "version");
144 JS_AddModuleExportList(ctx
, m
, liba_proto
, A_LEN(liba_proto
));