Repository: AssemblyScript/assemblyscript

Issue #1469: Failure compiling: `abort(AssertionError: assertion failed`
Created: 2020-09-21T08:07:53Z
Hi guys, I'm getting the following log when trying to compile and it doesn't make much sense to me.
> wasm-example@0.0.0 asbuild:dev /Users/james/code/wasm-example
> asc assembly/index.ts --target debug

Error [AssertionError]: assertion failed
    at t.assert (/Users/james/code/wasm-example/node_modules/assemblyscript/dist/assemblyscript.js:7:635183)
    at y.finalizeVirtualStub (/Users/james/code/wasm-example/node_modules/assemblyscript/dist/assemblyscript.js:7:479249)
    at y.compile (/Users/james/code/wasm-example/node_modules/assemblyscript/dist/assemblyscript.js:7:382854)
    at Object.t.compile (/Users/james/code/wasm-example/node_modules/assemblyscript/dist/assemblyscript.js:7:655800)
    at /Users/james/code/wasm-example/node_modules/assemblyscript/cli/asc.js:713:29
    at measure (/Users/james/code/wasm-example/node_modules/assemblyscript/cli/asc.js:1101:3)
    at Object.main (/Users/james/code/wasm-example/node_modules/assemblyscript/cli/asc.js:712:24)
    at /Users/james/code/wasm-example/node_modules/assemblyscript/bin/asc:21:47
/Users/james/code/wasm-example/node_modules/binaryen/index.js:57
function q(b){if(a.onAbort)a.onAbort(b);v(b);Aa=!0;throw new za("abort("+b+"). Build with -s ASSERTIONS=1 for more info.");}function Xa(b){var d=Ya;return String.prototype.startsWith?b.startsWith(d):0===b.indexOf(d)}var Ya="data:application/octet-stream;base64,",Za="";if(!Xa(Za)){var $a=Za;Za=a.locateFile?a.locateFile($a,p):p+$a}var ab,bb;La.push({Rs:function(){cb()}});
                                                   ^

Error: abort(AssertionError: assertion failed). Build with -s ASSERTIONS=1 for more info.
    at process.q (/Users/james/code/wasm-example/node_modules/binaryen/index.js:57:58)
    at process.emit (events.js:314:20)
    at processPromiseRejections (internal/process/promises.js:245:33)
    at processTicksAndRejections (internal/process/task_queues.js:94:32)

The index.ts AS code is as follows. I've narrowed it down to being the store calls which cause the issue (I'm trying to follow the WASM by Example guide and make it more readable — their code compiles btw).
const BOARD_SIZE: i32 = 20

interface RGBA {
  r: i8
  g: i8
  b: i8
  a: i8
}

export const BOARD_BUFFER_PTR: i32 = 0
export const BOARD_BUFFER_SIZE: i32 = BOARD_SIZE * BOARD_SIZE * 4

export function generate (
  dark: RGBA,
  light: RGBA,
): void {

  // Loop over each square on the board
  for (let x: i32 = 0; x < BOARD_SIZE; x++) {
    for (let y: i32 = 0; x < BOARD_SIZE; y++) {
      let isDark: boolean = true

      if (y % 2 === 0) {
        isDark = false
      }

      if (x % 2 === 0) {
        isDark = !isDark
      }

      const value: RGBA = isDark ? dark : light

      const squareNumber = y * BOARD_SIZE + x
      const ptrSquareRgba = BOARD_BUFFER_PTR + squareNumber * 4

      store<u8>(ptrSquareRgba + 0, value.r)
      store<u8>(ptrSquareRgba + 1, value.g)
      store<u8>(ptrSquareRgba + 2, value.b)
      store<u8>(ptrSquareRgba + 3, value.a)
    }
  }
}

Duplicate issue #1621: How to debug compile time assertion error?
Created: 2021-01-15T16:50:49Z
Hi all - one thing I encountered a couple times when using AS is that the compiler could throw AssertionError on correct typescript code.
Of course, correct TS code is not correct AssemblyScript code. However, when that happens, the compiler usually just say Error [AssertionError]: assertion failed, with some minimized code stack/context. Whenever that happens I have zero clue in terms of which line of my code is triggering that or how to fix it.
Is there a good way to get more debugging info from the error message?

Closing comment (2021-01-15T17:44:41Z): It will be great have minimal example which reproduce this assert error. Covering all possible unsupported cases with proper error in compiler is quite hard

The actual issue was similar to #1469 so I ended up using abstract class instead.
I'm not familiar with the compiler, but if it can point to the line of code triggering the assertion error, that would probably be sufficient. But of course, depending on the compiler this could be impossible as well.