2024-05-02 09:28:57 +03:00
|
|
|
/**
|
|
|
|
* @file Provides the Rust ffi interface. The interface should be kept in sync with polyglot ffi inteface {@link module:ffiPolyglot}.
|
|
|
|
*
|
|
|
|
* @module ffi
|
|
|
|
*/
|
|
|
|
|
2024-02-20 02:57:42 +03:00
|
|
|
import { createXXHash128 } from 'hash-wasm'
|
2024-04-09 15:02:11 +03:00
|
|
|
import type { IDataType } from 'hash-wasm/dist/lib/util'
|
2024-02-09 13:22:33 +03:00
|
|
|
import init, { is_ident_or_operator, parse, parse_doc_to_json } from '../../rust-ffi/pkg/rust_ffi'
|
2024-02-20 02:57:42 +03:00
|
|
|
import { assertDefined } from '../util/assert'
|
2024-02-02 12:22:18 +03:00
|
|
|
import { isNode } from '../util/detect'
|
|
|
|
|
2024-02-20 02:57:42 +03:00
|
|
|
let xxHasher128: Awaited<ReturnType<typeof createXXHash128>> | undefined
|
2024-04-09 15:02:11 +03:00
|
|
|
export function xxHash128(input: IDataType) {
|
2024-02-20 02:57:42 +03:00
|
|
|
assertDefined(xxHasher128, 'Module should have been loaded with `initializeFFI`.')
|
|
|
|
xxHasher128.init()
|
|
|
|
xxHasher128.update(input)
|
|
|
|
return xxHasher128.digest()
|
|
|
|
}
|
|
|
|
|
2024-02-02 12:22:18 +03:00
|
|
|
export async function initializeFFI(path?: string | undefined) {
|
2024-05-27 20:32:42 +03:00
|
|
|
if (xxHasher128 != null) return
|
2024-02-02 12:22:18 +03:00
|
|
|
if (isNode) {
|
|
|
|
const fs = await import('node:fs/promises')
|
2024-03-12 04:57:29 +03:00
|
|
|
const { fileURLToPath, URL: nodeURL } = await import('node:url')
|
|
|
|
const buffer = fs.readFile(
|
|
|
|
path ?? fileURLToPath(new nodeURL('../../rust-ffi/pkg/rust_ffi_bg.wasm', import.meta.url)),
|
|
|
|
)
|
2024-02-02 12:22:18 +03:00
|
|
|
await init(buffer)
|
|
|
|
} else {
|
|
|
|
await init()
|
|
|
|
}
|
2024-02-20 02:57:42 +03:00
|
|
|
xxHasher128 = await createXXHash128()
|
2024-02-02 12:22:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO[ao]: We cannot to that, because the ffi is used by cjs modules.
|
|
|
|
// await initializeFFI()
|
|
|
|
|
2024-02-09 13:22:33 +03:00
|
|
|
/* eslint-disable-next-line camelcase */
|
|
|
|
export { is_ident_or_operator, parse_doc_to_json, parse as parse_tree }
|