[Hello World]: Possible Issue with Stub Code

Since this is my first foray into WebAssembly, I am not entirely clear this is a bug. Went in today to complete Hello World. After happily changing “Goodby, Mars!” to “Hello, World!”, I was quite perplexed to have the tests fail:

(module
  (memory (export "mem") 1)

  ;; Initializes the WebAssembly Linear Memory with a UTF-8 string of 14 characters starting at offset 64
  (data (i32.const 64) "Hello, World!")
  
  ;; Returns the base offset and length of the greeting
  (func (export "hello") (result i32 i32)
    (i32.const 64) (i32.const 14)
  )
) 

The code above resulted in the following test failure:

expect(currentInstance).toBeTruthy();
const [offset, length] = currentInstance.exports.hello();
expect(length).toBe(13);
const greeting = currentInstance.get_mem_as_utf8(offset, length);
expect(greeting).toBe("Hello, World!");
Error: expect(received).toBe(expected) // Object.is equality

Expected: 13
Received: 14

Changing the export line fixes the issue:

  ;; Returns the base offset and length of the greeting
  (func (export "hello") (result i32 i32)
    (i32.const 64) (i32.const 13)
  )

But someone new to WebAssembly wouldn’t necessarily know to do that, so wondering if either the instructions or the stub need changing?

1 Like

Nice catch. This definitely sounds like it could do with an instructions append and a comment in the stub.

Maybe the stub:

;; Returns the base offset and length of the greeting
;; The final number (currently “13”) must match the length of the string.

And the append:

Note: Unlike most “Hello World” exercises on Exercism, Web Assembly requires an extra change to the code you are provided with. As well as changing the string itself, you must also change the length of the greeting on in the function definition. See the comment in the code for more details.

2 Likes