4

It seems that WebSharper has some difficulties converting between integer types (say, int32 to uint64). I get this:

error : Failed to translate a method call: ToUInt64(..) [Microsoft.FSharp.Core.Operators]

The same happens to int32 -> uint32, int16 -> int32 and many others (only byte <-> int32 seems to be working).

So, the question is: how do I get around this issue? I've got an integer i (which is int32 since I can't get anything else) and now I want to get i-th element from Uint8Array. Uint8Array.Get wants an uint64. How do I convert my i to uint64?

I was going to use an [<Inline>] cheat but that doesn't work either, because I get this error even if I try to return or to pass as an argument any integer different form int32.

Guy Coder
  • 24,501
  • 8
  • 71
  • 136
kirelagin
  • 13,248
  • 2
  • 42
  • 57
  • 3
    I am curious, what is your motivation for using these types? Currently none of these are properly implemented but are represented as `Number` in JS, so you can always "magic"-convert: `As (x: int32)`. – t0yv0 Mar 26 '13 at 14:21
  • @toyvo I'm parsing a binary file (this will be a virtual machine, but that doesn't really matter). I was hoping that static types will guide me through the maze of this binary format but it turns out that 1) I can't use it to reduce the number of errors; 2) I can't use it at all. I realise that, once translated to JS , that won't make any difference, but I just want compile-time checks. – kirelagin Mar 26 '13 at 18:16
  • @toyvo oh, and by the way, that gets even funnier when I want to read an `uint32`. I have to read it as `int32` byte by byte (with all necessary little-endian bits juggling), since `System.BitConverter` is not working either and then… I get a signed int. So I had to `[]` JS's `>>> 0` trick. – kirelagin Mar 26 '13 at 18:20
  • @mydogisbox I'm not sure what do you want to see there. Basically, I've got an `Uint8Array` from `FileReader`. I've implemented a file-like object around this array with a bunch of methods including `Seek`. Then, for example, I read an `uint32` which is an offset to the data I'm looking for. Now, `Seek` can't have `uint64` in its signature, so it has `int32` and then calls `data.Get (uint64 i)`. `As` actually helps here (I'm not an experienced .Net developer, so I missed it). This means that the question is resolved, but I still want to return different integers for static checks to work. – kirelagin Mar 26 '13 at 18:32

1 Answers1

4

From your comments it sounds like you are on the right track. WebSharper currently does not implement any binary handling .NET API so it is right to implement it yourself. Using numeric types for compile-time sanity while keeping in mind that they are all represented as Number in JavaScript is also a good idea. If you are missing conversion proxies from the standard library, you can add those or use Inline definition, I believe this should work:

[<AutoOpen>]
module Conversions =

    [<Inline "$0">]
    let inline int64 x = int64 x

    [<Inline "$0">]
    let inline int32 x = int32 x

    [<Inline "$0">]
    let inline uint32 x = uint32 x

    [<Inline "$0">]
    let inline uint64 x = uint64 x

Of course the above implementations do not do the truncation that these operators are doing in F#/.NET.

t0yv0
  • 4,714
  • 19
  • 36
  • @kirelagin sounds like you are doing some seriously cool stuff. I like VMs. Please keep posting here or on [FPish](http://fpsih.net) if you stick with WebSharper and have questions. I would also love to see the application you are making - if possible, that is. Cheers. – t0yv0 Mar 27 '13 at 00:47
  • ok, I'll let you know when I feel that it's ready to be pushed to a public repo. As a preview you can check [this](http://kir.elagin.me/~kirrun/bach-rationale.pdf) (it's in Russian, but I hope that's not a problem). – kirelagin Mar 28 '13 at 06:42
  • 1
    @kirelagin Russian's not a problem :) So you want Dalvik emulator in the browser. This sounds crazy (in a good way -- ambitious). Interpreter approach will not work for you though. What you would want is translate Dalvik bytecode to JS (maybe ASM.js), then browser will JIT it to native code again. WebSharper can help with the UI but it sounds like you could benefit from having your cross-compiler in F# proper. You can make it a cloud service very easily these days. – t0yv0 Mar 28 '13 at 12:03
  • heh, actually, I've decided to take the inerpreter path. I'd really like to translate and I've thought a lot about translation… I'm not sure I can express it using words, but it seems to me that bytecode (i.e. not source language) translation won't work as well as one might expect, especially with threads. On the other hand, there is a number of Java VMs written in JS, which makes me believe Dalvik VM is also possible. – kirelagin Mar 29 '13 at 05:07