Skip to content

Logging in recursive function leads to display issue #53

Open
@0xE282B0

Description

@0xE282B0

Hi,
during the development of the solution for the reverse-string exercise, I noticed that the logs which were printed from within a recursive function did not end up under the right test case in the UI.

I was printing the remaining bytes of the string to see if the recursive function has the right start and end points for the string. The problem appears especially in the later test cases, like in the picture attached.

(module
    (import "console" "log_mem_as_utf8" (func $log_mem_as_utf8 (param $byteOffset i32) (param $length i32)))

  (memory (export "mem") 1)
 
  (func (export "reverseString") (param $offset i32) (param $length i32) (result i32 i32)
    (call $recursion (local.get $offset)(local.get $offset)(local.get $length))
    (return (local.get $offset) (local.get $length))
  )
  (func $recursion (param $offset i32)(param $pointer i32) (param $remaining i32)
    ;; Store byte
    (local $byte i32) (local.set $byte (i32.load8_s(local.get $pointer)))
    (call $log_mem_as_utf8 (local.get $offset)(local.get $remaining))
    (if (i32.le_u(local.get $remaining)(i32.const 1))
      ;; Store last byte at offset
      (then (i32.store8(local.get $offset)(local.get $byte)))
      (else 
        ;; Call recursion if not last byte
        (call $recursion 
          (local.get $offset)
          (i32.add(local.get $pointer)(i32.const 1))
          (i32.sub(local.get $remaining)(i32.const 1)))
        ;; Store byte at offset + remaining -1 
        (i32.store8(i32.add(local.get $offset)(i32.sub(local.get $remaining)(i32.const 1)))(local.get $byte))
      )
    )
  )
) 

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions