Search code examples
javascriptnode.jswebassemblyassemblyscript

Calling list elements by index in assembly script map function causing compilation error


I'm using nodeJS along with assemblyscript to test out webassembly. I tried writing a simple python style zip function hoping it would work as intended. But for some reason every time I hit compile, it throws an error with basically no description of why it breaks.

export function zip(list1:Array<string>, list2:Array<string>):string[][]{
  return list1.map<string[]>((i:string,d:i32):string[] => [i, list2[d]])
}

Attaching image of the error message on compilation on running: yarn asbuild

terminal output after compilation


Solution

  • this querstion was resolved here. https://github.com/AssemblyScript/assemblyscript/issues/2571

    Explanation of why this doesnt work. As the error suggests the code doesnt allow access to certain variables within closure (in this case; the map function). hence the solution is to copy the value onto a temporary variable outside the function and then calls the value from there.

    like so:

    let tempList: string[] = []
    
    export function zip(list1: string[], list2: string[]): string[][] {
        tempList = list2
        return list1.map((i: string, d: i32): string[] => [i, tempList[d]])
    }