MCPcopy Create free account
hub / github.com/ashvardanian/StringZilla / hashAPI

Function hashAPI

javascript/lib.c:315–349  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

313}
314
315napi_value hashAPI(napi_env env, napi_callback_info info) {
316 size_t argc = 2;
317 napi_value args[2];
318 napi_get_cb_info(env, info, &argc, args, NULL, NULL);
319
320 // Get buffer info for data (zero-copy)
321 void *buffer_data;
322 size_t buffer_length;
323 napi_status status = napi_get_buffer_info(env, args[0], &buffer_data, &buffer_length);
324 if (status != napi_ok) {
325 napi_throw_error(env, NULL, "First argument must be a Buffer");
326 return NULL;
327 }
328
329 // Get optional seed parameter (default to 0)
330 sz_u64_t seed = 0;
331 if (argc > 1) {
332 bool lossless;
333 napi_get_value_bigint_uint64(env, args[1], &seed, &lossless);
334 if (!lossless) {
335 // Try regular number if BigInt fails
336 double seed_double;
337 if (napi_get_value_double(env, args[1], &seed_double) == napi_ok) { seed = (sz_u64_t)seed_double; }
338 }
339 }
340
341 // Compute hash using StringZilla
342 sz_u64_t hash_result = sz_hash((sz_cptr_t)buffer_data, buffer_length, seed);
343
344 // Convert result to JavaScript BigInt
345 napi_value js_result;
346 napi_create_bigint_uint64(env, hash_result, &js_result);
347
348 return js_result;
349}
350
351static void hasher_cleanup(napi_env env, void *data, void *hint) { free(data); }
352typedef struct {

Callers

nothing calls this directly

Calls 1

sz_hashFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…