Paul Praet
Paul Praet

Reputation: 1387

How to overcome javascript's 56-bit limitation

We are writing a Webkit-based software framework which has a C-backend. We want to pass references from the C layer to the javascript layer at the top. Unfortunately C pointers may be up to 64-bit while javascript only supports up to 56-bit.

How can we overcome this javascript-limitation ?

Our current approach is creating a hashmap in C between our 64-bit pointers and 32-bit integers. The latter are passed on to the Javascript layer.

Upvotes: 2

Views: 577

Answers (2)

Esailija
Esailija

Reputation: 140230

64-bit pointers on x64 ever use only the lower 48 bits, in fact spidermonkey and other browsers are relying on this for fast integer/double representation through NaN-boxing.

Upvotes: 1

Denys Séguret
Denys Séguret

Reputation: 382150

You might simply convert the pointers to doubles in the C side (see this related question) , and communicate with JavaScript only doubles. There won't be any loss this way as you'll only deal with IEEE754 double precision numbers whose possible values are the same both side.

But experience shows that exchanging pointers between programs is usually a bad idea (obviously it introduces a strong coupling, for a start), so I would use some kind of ID, which is what you do with your hashmap.

Upvotes: 1

Related Questions