asmble icon indicating copy to clipboard operation
asmble copied to clipboard

Fails to compile simple clang-generated WASM file due to `list not empty`

Open renatoathaydes opened this issue 5 years ago • 2 comments

I've got clang to compile this to WASM:

// Filename: add.c
int add(int a, int b) {
  return a + b;
}

When trying to compile to JVM with asmble, it fails:

Caused by: java.util.NoSuchElementException: List is empty.
        at kotlin.collections.CollectionsKt___CollectionsKt.last(_Collections.kt:360)
        at asmble.io.BinaryToAst.toModule(BinaryToAst.kt:216)
        at asmble.cli.Translate.inToAst(Translate.kt:70)
        at asmble.cli.Compile.run(Compile.kt:54)

This was working with version 0.3.0 but broke in 0.4.0.

This is the code that fails, where you call customSections.last():

if (sectionId != 0) customSections else {
    // If the last section was custom, use the last custom section's after-ID,
    // otherwise just use the last section ID
    val afterSectionId = if (index == 0) 0 else sections[index - 1].let { (prevSectionId, _) ->
    if (prevSectionId == 0) customSections.last().afterSectionId else prevSectionId
}

This code is hard to follow, but looks like customSection starts off empty, so it's likely the last() call is going to be called on the empty list, as is happening to me. Not sure what afterSectionId should be assigned to in that case though, hope you can help find a solution.

renatoathaydes avatar Nov 24 '19 15:11 renatoathaydes

I haven't been able to focus on this library in a while so I am not sure when I will be able to look.

cretz avatar Nov 24 '19 16:11 cretz

No problem, but thanks for letting me know.

renatoathaydes avatar Nov 24 '19 17:11 renatoathaydes