ballercat / walt

:zap: Walt is a JavaScript-like syntax for WebAssembly text format :zap:
https://ballercat.github.io/walt/
MIT License
4.64k stars 122 forks source link

f64 arrays require even-valued indices #171

Closed jtiscione closed 5 years ago

jtiscione commented 5 years ago

Bug Report

Overview

Indices into f64 arrays must be even numbers, twice as large as expected

Example

const walt = require('walt-compiler');

async function test() {
  const SAFE_OFFSET = 65536;
  const { instance } = await WebAssembly.instantiate(walt.compile(`
      export const memory: Memory<{initial: ${1 + (SAFE_OFFSET >> 16)}}>;
      export function populateArray(): void {
        const array: f64[] = ${SAFE_OFFSET};
        array[0] = 10 :f64;
        array[2] = 20 :f64;
        array[4] = 30 :f64;
        array[6] = 40 :f64;
        array[8] = 50 :f64;
      }
  `).buffer(), {});
  const myTypedArray = new Float64Array(instance.exports.memory.buffer, SAFE_OFFSET, 10).fill(42);
  instance.exports.populateArray();
  console.log(myTypedArray);
}
test();

Expected

Should print Float64Array [ 10, 42, 20, 42, 30, 42, 40, 42, 50, 42 ]

Actual

Prints Float64Array [ 10, 20, 30, 40, 50, 42, 42, 42, 42, 42 ].

If both even AND odd indices are used in the Walt code, the Float64Array contains crazy values or NaN.

ballercat commented 5 years ago

Hey there. Thank you for the test case, it was very useful.

This should be fixed shortly. Looks like wide types were not being indexed correctly.

ballercat commented 5 years ago

walt-compiler@0.18.0 should have the correct behavior now. Thanks!