Hacker News new | past | comments | ask | show | jobs | submit login

> There are a ton of implicit casts

Very easy to avoid if you're compiling a statically typed language to JS.

> No integers

Not in the JS specification (outside typed arrays), but every JS JIT works in a way you can actually declare variables as 32 bit signed integers, and made its way into the asm.js specification. They are declared like this:

    var a = value|0;
Where the |0 is a no-op so not actually done, just a type hint.

If what worries you is precision and not performance, doubles allow 53 bit integers (not counting sign bit) with full precision.

> Say you have a demo page for some language, and someone using the page writes an infinite loop. Don't want the page to crash? Welcome to advanced compilation techniques like CPS transformations and trampolining.

Or just use a web worker, which you can terminate if you haven't heard back in a while (pun intended).

> and any part of the language you target for which you don't fully understand the spec is a potential bug

It heavily depends on what type of language you do. If your compiler tracks the types and doesn't mix them, edge cases are much, much easier to avoid.

Source: I did make a compiler for my own language that targeted JS.




>Not in the JS specification (outside typed arrays), but every JS JIT works in a way you can actually declare variables as 32 bit signed integers, and made its way into the asm.js specification. They are declared like this:

The "|0" trick you mention is not for javascript; it is for asm.js; to be able to declare such a "true integer" variable, your code would need to be in asm.js, not javascript.

Javascript has no integers, only floating point numbers. This is a very strong limitation.


What would entail to declare a "true integer"?

Correctness? The |0 after each operation makes it correct. All bitwise operations in JS operate on signed 32 bit integers.

Speed? All JS JITs have optimizations for integers, and it's the reason asm.js uses that trick, not the other way around.

Precision? If you don't use bitwise operations, you have up to 53 bits of perfect integer precision, plus bit sign. Guaranteed by the standard.


> Where the |0 is a no-op so not actually done, just a type hint.

if value is null / undefined / empty object / empty array, empty string... | 0 wouldn't be no-op it would actually assign 0 to a.


When JS is a compiler target, that truck will generally only be used if the source language excludes all those edge cases, i.e. the value is guaranteed to be an integer.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: