-
Notifications
You must be signed in to change notification settings - Fork 321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Any chance for JavaScript version of _js_decode_string
#471
Comments
We discussed having a flag to toggle between the C-version and normal haskell (which ghcjs could use), but @winterland1989 said it might be tricky to do. I'd happily accept the addition of such a flag. The other option, like you say, is translating the C code to JS. I don't know if this would be easier than the alternative a bove. If I understand correctly ghcjs has a bunch of patches like this for libraries, so if someone creates this patch it doesn't really matter for users whether it's included in aeson or in the ghcjs repo, right? |
Should it be something like "PURE_HASKELL" set via |
I got the feeling that someone really should make an aeson fork specialized for javascript following how elm did, because js runtimes have already provided extremely fast parser from string to aeson So it's not |
I ran into this issue myself (ghcjs/ghcjs-base#71) trying to move from a patched clone of |
Either one works but if the shim is written in javascript i think it fits better inside ghcjs-shims. |
Is there any solution to this? |
If you don't expect any escape codes in the input, you can drop this into your code. It requires TextDecoder which is not available on all browsers, but there is a shim if cross-browser compatibility is an issue. I don't know how to deal with escape codes. I tried to roundtrip through JSON.stringify/JSON.parse but that didn't work. window['h$_js_decode_string'] = function(dest, destOffPtr, destOffPtrOff, s, sOff, send, sendOff) {
// Decode the input as UTF-8. The resulting string may still contain escape codes though!
const input = new Uint8Array(s.buf, sOff, sendOff - sOff);
const str = new TextDecoder('utf-8').decode(input);
// Write the string as UTF-16 into the dest buffer.
var i = 0;
for (let ch of str) {
if (ch.length === 1) {
dest.dv.setUint16(i, ch.charCodeAt(0), true);
i += 2;
} else if (ch.length === 2) {
dest.dv.setUint16(i, ch.charCodeAt(0), true);
dest.dv.setUint16(i + 2, ch.charCodeAt(1), true);
i += 4;
}
}
// Write how many UTF-16 code units we've written into the destination buffer.
destOffPtr.dv.setUint32(0, i / 2, true);
return 0;
}; |
How do you add the above JavaScript to the cabal package? |
@tolysz this should go into the ghcjs/shims package, ideally. I have no idea how else to add it so that ghcjs picks it up automatically. You probably need such a yaml file for aeson, somewhere: https://github.com/ghcjs/shims/blob/master/bytestring.yaml |
This was resolved with a haskell implementation in 1.1.1.0! |
Hi there
I have been playing with
ghcjs
compiled againststack nightly
withaeson: "1.0.1.0"
. Compiles fine, however during the runtime I have:Which translates to JavaScript
shim
forcbits
implementing this function being missing.The text was updated successfully, but these errors were encountered: