Skip to content
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

Big integers are truncated #285

Closed
ztlpn opened this issue Apr 29, 2015 · 6 comments
Closed

Big integers are truncated #285

ztlpn opened this issue Apr 29, 2015 · 6 comments

Comments

@ztlpn
Copy link

ztlpn commented Apr 29, 2015

I am having trouble using bin_prot library with js_of_ocaml. The following code:

Printf.printf "Size: %d\n" (Bin_prot.Size.bin_size_int64 (Int64.of_int 0x8000))

when run in the browser produces Size: 9 instead of correct Size: 5. Here is the relevant block of bin_prot source code: https://github.com/janestreet/bin_prot/blob/master/lib/size.ml#L16-L22

js_of_ocaml compiler produces a bunch of warnings:

Warning: integer overflow: integer 0xffffffff truncated to 0xffffffff; the generated code might be incorrect.
Warning: integer overflow: integer 0x80000000 truncated to 0x80000000; the generated code might be incorrect.
Warning: integer overflow: integer 0x100000000 truncated to 0x0; the generated code might be incorrect.
Warning: integer overflow: integer 0x80000000 truncated to 0x80000000; the generated code might be incorrect.
Warning: integer overflow: integer 0x100000000 truncated to 0x0; the generated code might be incorrect.
Warning: integer overflow: integer 0x80000000 truncated to 0x80000000; the generated code might be incorrect.

and in this case the generated code is indeed incorrect. The most obvious solution is to get rid of big integers in the source code but it is in third-party library and thus not easily modifiable. Am I out of luck?

@ztlpn
Copy link
Author

ztlpn commented Apr 29, 2015

As a side note, printing hex representations of integers in the warnings is a bit misleading in this case because they are exactly the same (but integers themselves and their signed decimal representations differ)

@Drup
Copy link
Member

Drup commented Apr 29, 2015

Integers are not 64 bits in javascript, so yes, you will have to change bin_prot I think. Note that the way it's currently done in bin_prot could be considered a bug, given how hostile to cross-compilation (and js_of_ocaml) it is.

@samoht
Copy link

samoht commented Apr 29, 2015

/cc @diml

@hhugo
Copy link
Member

hhugo commented Apr 29, 2015

There are some work in progress to solve this inside core & co (including bin_prot).
janestreet/core_kernel#8.
The easiest workaround for now is to use a 32bit compiler opam switch 4.02.1+32bit

@hhugo
Copy link
Member

hhugo commented May 19, 2015

error message improved by f39398b

@hhugo hhugo mentioned this issue Mar 28, 2016
5 tasks
@hhugo
Copy link
Member

hhugo commented Jun 18, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants