Posts

Showing posts from March, 2010

Migrated!

Old posts have been migrated from my Live Spaces blog, including pictures and the insignificant number of comments I've solicited over the years.

JavaScript and IEEE754

In C/C++ and... well, practically any other language intended for systems programming, you can get at the bits of a floating point number, either by tricks with unions or binary data packing routines. And all of those languages use the IEEE754 standard for floating point number representation in memory.

JavaScript uses IEEE754 internally as well, but offers neither unions nor general binary data manipulation. So if you want convert to and from bytes - say, dug out of a base64-encoded blob - you need to do some work.

Here's a first crack at conversion functions. They are not thoroughly tested. Caveats: "negative 0" is not preserved, denormalized numbers aren't handled, nor are QNaNs.

Don't use this - see the code in Typed Array Polyfill instead // Convert a JavaScript number to IEEE-754 Double Precision // value represented as an array of 8 bytes (octets) // function toIEEE754(v) { var s, e, f; if (isNaN(v)) { e = 2047; f = 1; s = 0; } else …

JavaScript Cookie API

Adam Barth proposed a new Cookie API for browser JavaScript over on the WHATWG list (API details)

To validate the API I took a stab at implementing it in ECMAScript using the existing Cookie API (document.cookie). The good news is that you can mostly replicate the functionality today, which means that if you like how the API looks you could try it out today.
After some consideration, Hixie put it to sleep (and I agree, it's not really worth it - Web Storage is a more robust direction for the future). But here's the sample implementation if anyone wants to use a close approximation.
Caveats: this is very rough and could probably do a higher fidelity emulation. The intent was not a rock-solid implementation, but to explore the edges of the proposed specification - i.e. is there really a new CookieList type intended? Is there any notification mechanism if an asynchronous cookie lookup fails to find a match? /*jslint browser: true */ /*global Cookie */ (function() { // Intentional e…

Migration

I'll be migrating stuff here from http://inexorabletash.spaces.live.com/ at some point. Wheee.