Stream: javy

Topic: ✔ Problem running WASM file


view this post on Zulip Tom Wieland (Jun 28 2023 at 15:50):

Hello! I'm trying to get PureScript -> JavaScript -> Javy -> WASM to work.

This is the code. I implemented a lot on the JS side (instead of PureScript) because I'm still learning PureScript. I'm trying to implement the example code of javy here: https://gist.github.com/Industrial/234f029bcb26c4e093e2bc1056806d23

The output is also there: Error while running JS: Uncaught SyntaxError: unexpected token: ''.

Looks like it isn't passed anything. What did I do wrong?

GitHub Gist: instantly share code, notes, and snippets.

view this post on Zulip Tom Wieland (Jun 28 2023 at 18:42):

I got it.

  // const finalBuffer = Uint8Array.from(inputChunks)
  const { finalBuffer } = inputChunks.reduce((context, chunk) => {
      context.finalBuffer.set(chunk, context.bufferOffset);
      context.bufferOffset += chunk.length;
      return context;
  }, { bufferOffset: 0, finalBuffer: new Uint8Array(totalBytes) });

My assumption about Uint8Array.from was incorrect.

view this post on Zulip Tom Wieland (Jun 28 2023 at 19:46):

export const writeOutput = (output) => {
  return () => {
    const encodedOutput = JSON.stringify(new TextEncoder().encode(output))
    console.log('encodedOutput', encodedOutput)
    const buffer = new Uint8Array(encodedOutput)
    Javy.IO.writeSync(1, buffer)
  }
}

produces

encodedOutput {"0":91,"1":111,"2":98,"3":106,"4":101,"5":99,"6":116,"7":32,"8":79,"9":98,"10":106,"11":101,"12":99,"13":116,"14":93}

I checked output as well and it's correct/expected. The weird thing is that I get no standard out. What did I do wrong?

view this post on Zulip Tom Wieland (Jun 28 2023 at 19:49):

Again, wrong copy paste.

    const encodedOutput = JSON.stringify(new TextEncoder().encode(output))
    const encodedOutput = new TextEncoder().encode(JSON.stringify(output))

view this post on Zulip Tom Wieland (Jun 28 2023 at 20:20):

I ended up with this:

const concatenateBuffers = (buffer1, buffer2) =>
  new Uint8Array([...buffer1, ...buffer2])

export const readInput = (chunkSize) => {
  return () => {
    const buffers = []
    while (true) {
      const buffer = new Uint8Array(chunkSize)
      const bytesRead = Javy.IO.readSync(0, buffer)
      if (bytesRead === 0) break
      if (bytesRead < 0) throw new Error("StdIn: Unexpected EOF")
      buffers.push(buffer.subarray(0, bytesRead))
    }
    const finalBuffer = buffers.reduce(concatenateBuffers, new Uint8Array(0))
    return JSON.parse(new TextDecoder().decode(finalBuffer))
  }
}

export const writeOutput = (output) => () =>
  Javy.IO.writeSync(
    1,
    new Uint8Array(new TextEncoder().encode(JSON.stringify(output)))
  )

view this post on Zulip Notification Bot (Jun 29 2023 at 12:19):

Tom Wieland has marked this topic as resolved.


Last updated: Nov 22 2024 at 16:03 UTC