Skip to content

Instantly share code, notes, and snippets.

@subzey
Created August 1, 2017 10:17
Show Gist options
  • Select an option

  • Save subzey/31cee9361bc54340f2fe2daf3d71c4a5 to your computer and use it in GitHub Desktop.

Select an option

Save subzey/31cee9361bc54340f2fe2daf3d71c4a5 to your computer and use it in GitHub Desktop.
(module
(memory 1)
(func (export "duplicateText") (param $ptr i32)
(local $len i32)
(local $max i32)
(loop $continueLen
(if
(i32.load8_u
(i32.add (get_local $ptr) (get_local $len))
)
(then
(set_local $len
(i32.add (get_local $len) (i32.const 1))
)
(br $continueLen)
)
)
)
(i32.sub
(get_local $ptr)
(i32.add (get_local $len))
(i32.add (get_local $len))
(i32.add (i32.const 1))
(i32.shr_u (i32.const 16))
(current_memory)
)
(tee_local $max)
(if
(then
(grow_memory (get_local $max))
(drop)
)
)
(set_local $max (i32.add (get_local $ptr) (get_local $len)))
(loop $continueCopy
(if
(i32.lt_u (get_local $ptr) (get_local $max))
(then
(i32.store8
(i32.add (get_local $ptr) (get_local $len))
(i32.load8_u (get_local $ptr))
)
(set_local $ptr
(i32.add (get_local $ptr) (i32.const 1))
)
(br $continueCopy)
)
)
)
(i32.store8
(i32.add (get_local $max) (get_local $len))
(i32.const 0)
)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment