SamJakob / xxh3

A Dart implementation (port) of the XXH3 hashing algorithm from xxHash.
https://pub.dev/packages/xxh3
MIT License
6 stars 2 forks source link

Web support #5

Open bradintheusa opened 1 month ago

bradintheusa commented 1 month ago

I get this runtime error in my web project after adding the package:

Launching lib/main_dev.dart on Chrome in debug mode...
/C:/Users/bradi/AppData/Local/Pub/Cache/hosted/pub.dev/xxh3-1.1.0/lib/src/xxh3.dart:14:27: Error: The integer literal 0x9E3779B185EBCA87 can't be represented exactly in JavaScript.
Try changing the literal to something that can be represented in JavaScript. In JavaScript 0x9e3779b185ebc800 is the nearest value that can be represented exactly.
const int kXXHPrime64_1 = 0x9E3779B185EBCA87;
                          ^^^^^^^^^^^^^^^^^^

Not sure if it can be fixed or this is not a web compatible package.

SamJakob commented 2 weeks ago

Is your intention just to compile (but not use) this in a project for web? (i.e., your app uses xxh3 on native platforms only) or do you intend to use this package on the web?

This package isn't currently web compatible. Even if you manage to get it to compile it would produce blatantly wrong values (because of the JavaScript integer limit being 2^53 not 2^64).

I could make it compile and throw at runtime if you just wanted to include the package in a project that used it on native platforms but not the web.

It's probably not possible to enable the package to be functional and compilable to JavaScript without taking a massive performance hit by using BigInt everywhere (as xxh3 works on 64-bit numbers).

I've just done some investigation and looks like WebAssembly should be viable. In fact, I've just written an npm package that uses WebAssembly generated from this package.

I'm not sure what that would look like in the context of, say a Flutter application (I would probably just bundle the Wasm module) but if you're actually interested in using this package on the web, I can start looking at trying to add web support using WebAssembly instead.

bradintheusa commented 2 weeks ago

I think just change it to not support web for now https://docs.flutter.dev/packages-and-plugins/developing-packages#plugin-platforms

Runtime errors would be the worst solution.