asm-js / validator

A reference validator for asm.js.
Apache License 2.0
1.78k stars 148 forks source link

Support global constants in initializers #77

Open kripken opened 10 years ago

kripken commented 10 years ago

Currently we require variable definitions be something like

function x() {
  var x = 0, y = 0.0;
  // ...
}

It would be nice to support

var zero = 0;
var zeroF = 0.0;

function x() {
  var x = zero, y = zeroF;
  // ...
}

This seems a little silly for ints, but for floats can reduce code size (with a better name than zeroF ;). A bigger benefit of this would be for new things coming to JS like SIMD, that we would likely want to add to asm.js. For a SIMD zero variable we need a function call

  var x = float32x4.zero();

so reusing a global constant would be much better for code size and likely also speed in some JITs. (Of course this only makes sense if the global constant is immutable, but that is indeed the case with the SIMD spec for JS).

To make parsing this simple, we could require such global constants be const,

const zero = 0;

but the downside there is that it would only work in IE11+. Alternatively we could make them normal vars, and validation would fail if they are assigned to.

Perhaps as a side issue, it would be nice to support non-zero values as well

var c = 123;
var d = 456.789;

function x() {
  var x = c, y = d;
  // ...
}

but this is a separate matter I suppose.

aidanhs commented 8 years ago

http://discourse.wicg.io/t/allow-const-global-variables/684/4

kripken commented 8 years ago

This is implemented so it should probably be added to the spec.