@@ -3,7 +3,7 @@ extern crate global_alloc;
3
3
4
4
use napi:: * ;
5
5
use napi_derive:: * ;
6
- use xxhash_rust:: { xxh32, xxh64} ;
6
+ use xxhash_rust:: { xxh3 , xxh32, xxh64} ;
7
7
8
8
#[ module_exports]
9
9
fn init ( mut exports : JsObject , env : Env ) -> Result < ( ) > {
@@ -16,6 +16,7 @@ fn init(mut exports: JsObject, env: Env) -> Result<()> {
16
16
& [
17
17
Property :: new ( & env, "update" ) ?. with_method ( update_xxh32) ,
18
18
Property :: new ( & env, "digest" ) ?. with_method ( digest_xxh32) ,
19
+ Property :: new ( & env, "reset" ) ?. with_method ( reset_xxh32) ,
19
20
] ,
20
21
) ?;
21
22
let xxh64_class = env. define_class (
@@ -24,10 +25,23 @@ fn init(mut exports: JsObject, env: Env) -> Result<()> {
24
25
& [
25
26
Property :: new ( & env, "update" ) ?. with_method ( update_xxh64) ,
26
27
Property :: new ( & env, "digest" ) ?. with_method ( digest_xxh64) ,
28
+ Property :: new ( & env, "reset" ) ?. with_method ( reset_xxh64) ,
27
29
] ,
28
30
) ?;
29
31
exports. set_named_property ( "Xxh32" , xxh32_class) ?;
30
32
exports. set_named_property ( "Xxh64" , xxh64_class) ?;
33
+
34
+ let mut xxh3 = env. create_object ( ) ?;
35
+ xxh3. create_named_method ( "xxh64" , xxh3_xxh64) ?;
36
+ xxh3. create_named_method ( "xxh64WithSecret" , xxh3_xxh64_with_secret) ?;
37
+ xxh3. create_named_method ( "xxh128" , xxh3_xxh128) ?;
38
+ xxh3. create_named_method ( "xxh128WithSecret" , xxh3_xxh128_with_secret) ?;
39
+ xxh3. create_named_method ( "createXxh3WithSeed" , create_xxh3_with_seed) ?;
40
+ xxh3. create_named_method ( "createXxh3WithSecret" , create_xxh3_with_secret) ?;
41
+ xxh3. create_named_method ( "update" , update_xxh3) ?;
42
+ xxh3. create_named_method ( "digest" , digest_xxh3) ?;
43
+ xxh3. create_named_method ( "reset" , reset_xxh3) ?;
44
+ exports. set_named_property ( "xxh3" , xxh3) ?;
31
45
Ok ( ( ) )
32
46
}
33
47
@@ -69,6 +83,19 @@ fn digest_xxh32(ctx: CallContext) -> Result<JsNumber> {
69
83
ctx. env . create_uint32 ( native. digest ( ) )
70
84
}
71
85
86
+ #[ js_function( 1 ) ]
87
+ fn reset_xxh32 ( ctx : CallContext ) -> Result < JsUndefined > {
88
+ let this = ctx. this_unchecked :: < JsObject > ( ) ;
89
+ let native = ctx. env . unwrap :: < xxh32:: Xxh32 > ( & this) ?;
90
+ let seed = if ctx. length == 1 {
91
+ ctx. get :: < JsNumber > ( 0 ) ?. get_uint32 ( ) ?
92
+ } else {
93
+ 0
94
+ } ;
95
+ native. reset ( seed) ;
96
+ ctx. env . get_undefined ( )
97
+ }
98
+
72
99
#[ js_function( 2 ) ]
73
100
fn xxh64 ( ctx : CallContext ) -> Result < JsBigint > {
74
101
let input = ctx. get :: < JsBuffer > ( 0 ) ?. into_value ( ) ?;
@@ -106,3 +133,108 @@ fn digest_xxh64(ctx: CallContext) -> Result<JsBigint> {
106
133
let native = ctx. env . unwrap :: < xxh64:: Xxh64 > ( & this) ?;
107
134
ctx. env . create_bigint_from_u64 ( native. digest ( ) )
108
135
}
136
+
137
+ #[ js_function( 1 ) ]
138
+ fn reset_xxh64 ( ctx : CallContext ) -> Result < JsUndefined > {
139
+ let this = ctx. this_unchecked :: < JsObject > ( ) ;
140
+ let native = ctx. env . unwrap :: < xxh64:: Xxh64 > ( & this) ?;
141
+ let seed = if ctx. length == 1 {
142
+ ctx. get :: < JsBigint > ( 0 ) ?. get_u64 ( ) ?. 0
143
+ } else {
144
+ 0
145
+ } ;
146
+ native. reset ( seed) ;
147
+ ctx. env . get_undefined ( )
148
+ }
149
+
150
+ #[ js_function( 2 ) ]
151
+ fn xxh3_xxh64 ( ctx : CallContext ) -> Result < JsBigint > {
152
+ let input = ctx. get :: < JsBuffer > ( 0 ) ?. into_value ( ) ?;
153
+ let seed = if ctx. length == 2 {
154
+ ctx. get :: < JsBigint > ( 1 ) ?. get_u64 ( ) ?. 0
155
+ } else {
156
+ 0
157
+ } ;
158
+ ctx
159
+ . env
160
+ . create_bigint_from_u64 ( xxh3:: xxh3_64_with_seed ( input. as_ref ( ) , seed) )
161
+ }
162
+
163
+ #[ js_function( 2 ) ]
164
+ fn xxh3_xxh64_with_secret ( ctx : CallContext ) -> Result < JsBigint > {
165
+ let input = ctx. get :: < JsBuffer > ( 0 ) ?. into_value ( ) ?;
166
+ let secret = ctx. get :: < JsBuffer > ( 1 ) ?. into_value ( ) ?;
167
+ ctx
168
+ . env
169
+ . create_bigint_from_u64 ( xxh3:: xxh3_64_with_secret ( input. as_ref ( ) , secret. as_ref ( ) ) )
170
+ }
171
+
172
+ #[ js_function( 2 ) ]
173
+ fn xxh3_xxh128 ( ctx : CallContext ) -> Result < JsBigint > {
174
+ let input = ctx. get :: < JsBuffer > ( 0 ) ?. into_value ( ) ?;
175
+ let seed = if ctx. length == 2 {
176
+ ctx. get :: < JsBigint > ( 1 ) ?. get_u64 ( ) ?. 0
177
+ } else {
178
+ 0
179
+ } ;
180
+ ctx
181
+ . env
182
+ . create_bigint_from_u128 ( xxh3:: xxh3_128_with_seed ( input. as_ref ( ) , seed) )
183
+ }
184
+
185
+ #[ js_function( 2 ) ]
186
+ fn xxh3_xxh128_with_secret ( ctx : CallContext ) -> Result < JsBigint > {
187
+ let input = ctx. get :: < JsBuffer > ( 0 ) ?. into_value ( ) ?;
188
+ let secret = ctx. get :: < JsBuffer > ( 1 ) ?. into_value ( ) ?;
189
+ ctx
190
+ . env
191
+ . create_bigint_from_u128 ( xxh3:: xxh3_128_with_secret ( input. as_ref ( ) , secret. as_ref ( ) ) )
192
+ }
193
+
194
+ #[ js_function( 2 ) ]
195
+ fn create_xxh3_with_seed ( ctx : CallContext ) -> Result < JsUndefined > {
196
+ let mut js_this = ctx. get :: < JsObject > ( 0 ) ?;
197
+ let seed = if ctx. length == 2 {
198
+ ctx. get :: < JsBigint > ( 1 ) ?. get_u64 ( ) ?. 0
199
+ } else {
200
+ 0
201
+ } ;
202
+ let xxh3_instance = xxh3:: Xxh3 :: with_seed ( seed) ;
203
+ ctx. env . wrap ( & mut js_this, xxh3_instance) ?;
204
+ ctx. env . get_undefined ( )
205
+ }
206
+
207
+ #[ js_function( 2 ) ]
208
+ fn create_xxh3_with_secret ( ctx : CallContext ) -> Result < JsUndefined > {
209
+ let mut js_this = ctx. get :: < JsObject > ( 0 ) ?;
210
+ let secret = ctx. get :: < JsBuffer > ( 1 ) ?. into_value ( ) ?;
211
+ let mut sec = [ 0u8 ; 192 ] ;
212
+ sec. copy_from_slice ( secret. as_ref ( ) ) ;
213
+ let xxh3_instance = xxh3:: Xxh3 :: with_secret ( sec) ;
214
+ ctx. env . wrap ( & mut js_this, xxh3_instance) ?;
215
+ ctx. env . get_undefined ( )
216
+ }
217
+
218
+ #[ js_function( 2 ) ]
219
+ fn update_xxh3 ( ctx : CallContext ) -> Result < JsObject > {
220
+ let this = ctx. this_unchecked :: < JsObject > ( ) ;
221
+ let native = ctx. env . unwrap :: < xxh3:: Xxh3 > ( & this) ?;
222
+ let input = ctx. get :: < JsBuffer > ( 0 ) ?. into_value ( ) ?;
223
+ native. update ( input. as_ref ( ) ) ;
224
+ Ok ( this)
225
+ }
226
+
227
+ #[ js_function]
228
+ fn digest_xxh3 ( ctx : CallContext ) -> Result < JsBigint > {
229
+ let this = ctx. this_unchecked :: < JsObject > ( ) ;
230
+ let native = ctx. env . unwrap :: < xxh3:: Xxh3 > ( & this) ?;
231
+ ctx. env . create_bigint_from_u64 ( native. digest ( ) )
232
+ }
233
+
234
+ #[ js_function]
235
+ fn reset_xxh3 ( ctx : CallContext ) -> Result < JsUndefined > {
236
+ let this = ctx. this_unchecked :: < JsObject > ( ) ;
237
+ let native = ctx. env . unwrap :: < xxh3:: Xxh3 > ( & this) ?;
238
+ native. reset ( ) ;
239
+ ctx. env . get_undefined ( )
240
+ }
0 commit comments