9 static JSClassID liba_crc8_class_id
;
11 static void liba_crc8_finalizer(JSRuntime
*rt
, JSValue val
)
13 js_free_rt(rt
, JS_GetOpaque(val
, liba_crc8_class_id
));
16 static JSClassDef liba_crc8_class
= {"crc8", .finalizer
= liba_crc8_finalizer
};
18 static JSValue
liba_crc8_ctor(JSContext
*ctx
, JSValueConst new_target
, int argc
, JSValueConst
*argv
)
20 JSValue proto
, clazz
= JS_UNDEFINED
;
21 struct crc8
*const self
= (struct crc8
*)js_mallocz(ctx
, sizeof(struct crc8
));
22 if (!self
) { return JS_EXCEPTION
; }
24 if (JS_ToUint32(ctx
, &poly
, argv
[0])) { goto fail
; }
28 reversed
= JS_ToBool(ctx
, argv
[1]);
29 if (reversed
< 0) { goto fail
; }
32 ? a_crc8l_init(self
->table
, (a_u8
)poly
)
33 : a_crc8m_init(self
->table
, (a_u8
)poly
);
34 proto
= JS_GetPropertyStr(ctx
, new_target
, "prototype");
35 if (JS_IsException(proto
)) { goto fail
; }
36 clazz
= JS_NewObjectProtoClass(ctx
, proto
, liba_crc8_class_id
);
37 JS_FreeValue(ctx
, proto
);
38 if (JS_IsException(clazz
)) { goto fail
; }
39 JS_SetOpaque(clazz
, self
);
43 JS_FreeValue(ctx
, clazz
);
47 static JSValue
liba_crc8_get(JSContext
*ctx
, JSValueConst this_val
, int magic
)
49 struct crc8
*const self
= (struct crc8
*)JS_GetOpaque2(ctx
, this_val
, liba_crc8_class_id
);
50 if (!self
) { return JS_EXCEPTION
; }
53 return js_array_u8_new(ctx
, self
->table
, 0x100);
58 static JSValue
liba_crc8_gen(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
60 struct crc8
*const self
= (struct crc8
*)JS_GetOpaque2(ctx
, this_val
, liba_crc8_class_id
);
61 if (!self
) { return JS_EXCEPTION
; }
63 if (JS_ToUint32(ctx
, &poly
, argv
[0])) { return JS_EXCEPTION
; }
67 reversed
= JS_ToBool(ctx
, argv
[1]);
68 if (reversed
< 0) { return JS_EXCEPTION
; }
71 ? a_crc8l_init(self
->table
, (a_u8
)poly
)
72 : a_crc8m_init(self
->table
, (a_u8
)poly
);
76 static JSValue
liba_crc8_eval(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
78 struct crc8
*const self
= (struct crc8
*)JS_GetOpaque2(ctx
, this_val
, liba_crc8_class_id
);
79 if (!self
) { return JS_EXCEPTION
; }
84 if (JS_ToUint32(ctx
, &x
, argv
[1])) { return JS_EXCEPTION
; }
88 if (JS_IsArray(ctx
, argv
[0]))
90 a_byte
*p
= JS_GetArrayBuffer(ctx
, &n
, argv
[0]);
91 if (p
) { value
= a_crc8(self
->table
, p
, n
, value
); }
95 char const *const p
= JS_ToCStringLen(ctx
, &n
, argv
[0]);
96 value
= a_crc8(self
->table
, p
, n
, value
);
97 JS_FreeCString(ctx
, p
);
99 return JS_NewUint32(ctx
, value
);
102 static JSValue
liba_crc8_pack(JSContext
*ctx
, JSValueConst this_val
, int argc
, JSValueConst
*argv
)
104 JSValue val
= JS_UNDEFINED
;
105 struct crc8
*const self
= (struct crc8
*)JS_GetOpaque2(ctx
, this_val
, liba_crc8_class_id
);
106 if (!self
) { return JS_EXCEPTION
; }
111 if (JS_ToUint32(ctx
, &x
, argv
[1])) { return JS_EXCEPTION
; }
115 char const *const s
= JS_ToCStringLen(ctx
, &n
, argv
[0]);
116 value
= a_crc8(self
->table
, s
, n
, value
);
117 a_byte
*p
= (a_byte
*)js_malloc(ctx
, n
+ 1);
118 if (p
) { a_copy(p
, s
, n
); }
121 val
= js_array_u8_new(ctx
, p
, (uint32_t)n
+ 1);
123 JS_FreeCString(ctx
, s
);
128 static JSCFunctionListEntry
const liba_crc8_proto
[] = {
129 JS_PROP_STRING_DEF("[Symbol.toStringTag]", "a.crc8", 0),
130 JS_CGETSET_MAGIC_DEF("table", liba_crc8_get
, NULL
, 0),
131 JS_CFUNC_DEF("gen", 2, liba_crc8_gen
),
132 JS_CFUNC_DEF("eval", 2, liba_crc8_eval
),
133 JS_CFUNC_DEF("pack", 2, liba_crc8_pack
),
136 int js_liba_crc8_init(JSContext
*ctx
, JSModuleDef
*m
)
138 JS_NewClassID(&liba_crc8_class_id
);
139 JS_NewClass(JS_GetRuntime(ctx
), liba_crc8_class_id
, &liba_crc8_class
);
141 JSValue
const proto
= JS_NewObject(ctx
);
142 JS_SetPropertyFunctionList(ctx
, proto
, liba_crc8_proto
, A_LEN(liba_crc8_proto
));
144 JSValue
const clazz
= JS_NewCFunction2(ctx
, liba_crc8_ctor
, "crc8", 2, JS_CFUNC_constructor
, 0);
145 JS_SetConstructor(ctx
, clazz
, proto
);
146 JS_SetClassProto(ctx
, liba_crc8_class_id
, proto
);
148 return JS_SetModuleExport(ctx
, m
, "crc8", clazz
);