Problem with deserializing large ints on Flash9

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem with deserializing large ints on Flash9

Michael Pliskin

Hello all,

 

Was out of the list for a long time and now back to report a bug J We’ve met quite a weird problem recently: if you’re sending a really large number (say Unix time) from JS to Flash9, it arrives broken. The problem is that even if you store time in a Float variable on js side, it is sent as int because there is no reliable way to make a different between int and float for JS (and Flash8 I think). Then Flash9 simply encounters an overflow when deserializing it.

 

There are two ways to fix:

-          Make sure large numbers are always floats

-          Make unserializer do a simple check that the value fits into an integer and return float otherwise (of course only for platforms where it matters – neko and flash9)

 

I prefer the second option, so attaching patch for it (done against recent version, tested for 1.19).

 

 


--
haXe - an open source web programming language
http://haxe.org

Unserializer.hx.patch (760 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Problem with deserializing large ints on Flash9

Nicolas Cannasse
Michael Pliskin a écrit :

> Hello all,
>
>  
>
> Was out of the list for a long time and now back to report a bug J We’ve
> met quite a weird problem recently: if you’re sending a really large
> number (say Unix time) from JS to Flash9, it arrives broken. The problem
> is that even if you store time in a Float variable on js side, it is
> sent as int because there is no reliable way to make a different between
> int and float for JS (and Flash8 I think). Then Flash9 simply encounters
> an overflow when deserializing it.

The best way to fix that is to have Type.typeof(1e10) returns TFloat
instead of TInt for Flash6-8 and JS.

On Type.hx line 562, try replacing :

   if( Math.ceil(v) == v )
     return TInt;

By :

   if( Math.ceil(v) == v%2147483648.0 )
     return TInt;

That should do the trick.

Best,
Nicolas

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

RE: Problem with deserializing large ints on Flash9

Michael Pliskin
Hello Nicolas,

  Thanks for the tip, I actually followed this path initially and it worked but I thought it is a bad way to go as it makes one platform take another platform into account, which is weird. Also as Neko ints are 31-bit, the constant should probably be different.

Mike


-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of Nicolas Cannasse
Sent: Wednesday, October 22, 2008 7:22 PM
To: The haXe compiler list
Subject: Re: [haXe] Problem with deserializing large ints on Flash9

Michael Pliskin a écrit :

> Hello all,
>
>
>
> Was out of the list for a long time and now back to report a bug J We've
> met quite a weird problem recently: if you're sending a really large
> number (say Unix time) from JS to Flash9, it arrives broken. The problem
> is that even if you store time in a Float variable on js side, it is
> sent as int because there is no reliable way to make a different between
> int and float for JS (and Flash8 I think). Then Flash9 simply encounters
> an overflow when deserializing it.

The best way to fix that is to have Type.typeof(1e10) returns TFloat
instead of TInt for Flash6-8 and JS.

On Type.hx line 562, try replacing :

   if( Math.ceil(v) == v )
     return TInt;

By :

   if( Math.ceil(v) == v%2147483648.0 )
     return TInt;

That should do the trick.

Best,
Nicolas

--
haXe - an open source web programming language
http://haxe.org

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Problem with deserializing large ints on Flash9

Nicolas Cannasse
Michael Pliskin a écrit :
> Hello Nicolas,
>
>   Thanks for the tip, I actually followed this path initially and it worked but I thought it is a bad way to go as it makes one platform take another platform into account, which is weird. Also as Neko ints are 31-bit, the constant should probably be different.

I think that having Int being 32-bits signed is pretty common, and makes
things much more crossplatform (there is actually no proper Int in JS or
Flash8, only Numbers/Float so we can define it the way we want or need).

The problem remains if you're trying to exchange integers that are too
large for Neko, but that's a separate problem : since they can't be
represented in Neko, you'll have to use Float anyway in these cases.

Best,
Nicolas

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

RE: Problem with deserializing large ints on Flash9

Michael Pliskin
Yes I will have to use float for that, BUT even if I send smth as Float from
Flash8, it will be encoded as Int when serializing! So there is no way then
to send large numbers from flash/js at all! That's the problem I think.

Mike


-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Nicolas Cannasse
Sent: Thursday, October 23, 2008 7:36 PM
To: The haXe compiler list
Subject: Re: [haXe] Problem with deserializing large ints on Flash9

Michael Pliskin a écrit :
> Hello Nicolas,
>
>   Thanks for the tip, I actually followed this path initially and it
worked but I thought it is a bad way to go as it makes one platform take
another platform into account, which is weird. Also as Neko ints are 31-bit,
the constant should probably be different.

I think that having Int being 32-bits signed is pretty common, and makes
things much more crossplatform (there is actually no proper Int in JS or
Flash8, only Numbers/Float so we can define it the way we want or need).

The problem remains if you're trying to exchange integers that are too
large for Neko, but that's a separate problem : since they can't be
represented in Neko, you'll have to use Float anyway in these cases.

Best,
Nicolas

--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

RE: Problem with deserializing large ints on Flash9

James W. Hofmann
It's a bad hack but you can use toString() and then Std.parseFloat()  
to get a maximally accurate representation on all platforms.

Quoting "Michael Pliskin" <[hidden email]>:

> Yes I will have to use float for that, BUT even if I send smth as Float from
> Flash8, it will be encoded as Int when serializing! So there is no way then
> to send large numbers from flash/js at all! That's the problem I think.
>
> Mike
>
>
> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of Nicolas Cannasse
> Sent: Thursday, October 23, 2008 7:36 PM
> To: The haXe compiler list
> Subject: Re: [haXe] Problem with deserializing large ints on Flash9
>
> Michael Pliskin a écrit :
>> Hello Nicolas,
>>
>>   Thanks for the tip, I actually followed this path initially and it
> worked but I thought it is a bad way to go as it makes one platform take
> another platform into account, which is weird. Also as Neko ints are 31-bit,
> the constant should probably be different.
>
> I think that having Int being 32-bits signed is pretty common, and makes
> things much more crossplatform (there is actually no proper Int in JS or
> Flash8, only Numbers/Float so we can define it the way we want or need).
>
> The problem remains if you're trying to exchange integers that are too
> large for Neko, but that's a separate problem : since they can't be
> represented in Neko, you'll have to use Float anyway in these cases.
>
> Best,
> Nicolas
>
> --
> haXe - an open source web programming language
> http://haxe.org
>
>
> --
> haXe - an open source web programming language
> http://haxe.org
>



--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Problem with deserializing large ints on Flash9

Nicolas Cannasse
In reply to this post by Michael Pliskin
Michael Pliskin a écrit :
> Yes I will have to use float for that, BUT even if I send smth as Float from
> Flash8, it will be encoded as Int when serializing! So there is no way then
> to send large numbers from flash/js at all! That's the problem I think.

For Flash/JS, I think that the proposed fix in Type.typeof should work.
As for Flash8->Neko or JS->Neko, you're right there's still a problem
when an integer which would be in-range in Flash but out-range in Neko.
That's a typical overflow.

The only possibility I can see is a bit a hack, since it consists to add
0.1 to your Flash number in order to force the float serialization.

Please note also that Serializer natively supports Date type, which
would maybe be much more easy for you to use.

Best,
Nicolas


--
haXe - an open source web programming language
http://haxe.org