Skip to content
Snippets Groups Projects
Commit 95d80f52 authored by Cheng Shao's avatar Cheng Shao
Browse files

wasm: add zero length fast path for fromJSString

This patch adds a zero length fast path for `fromJSString`; when
marshaling a zero-length `JSString` we don't need to allocate an empty
`ByteArray#` at all.

(cherry picked from commit eac196df)
parent 507c3d4f
No related branches found
No related tags found
No related merge requests found
...@@ -184,15 +184,16 @@ newtype JSString ...@@ -184,15 +184,16 @@ newtype JSString
-- eagerly once the resulting 'String' is forced, and the argument -- eagerly once the resulting 'String' is forced, and the argument
-- 'JSString' may be explicitly freed if no longer used. -- 'JSString' may be explicitly freed if no longer used.
fromJSString :: JSString -> String fromJSString :: JSString -> String
fromJSString s = unsafeDupablePerformIO $ do fromJSString s = case js_stringLength s * 3 of
l <- js_stringLength s 0 -> ""
fp <- mallocPlainForeignPtrBytes $ l * 3 max_len -> unsafePerformIO $ do
withForeignPtr fp $ \buf -> do fptr <- mallocPlainForeignPtrBytes max_len
l' <- js_encodeInto s buf $ l * 3 withForeignPtr fptr $ \ptr -> do
peekCStringLen utf8 (buf, l') len <- js_encodeInto s ptr max_len
peekCStringLen utf8 (ptr, len)
foreign import javascript unsafe "$1.length" foreign import javascript unsafe "$1.length"
js_stringLength :: JSString -> IO Int js_stringLength :: JSString -> Int
foreign import javascript unsafe "(new TextEncoder()).encodeInto($1, new Uint8Array(__exports.memory.buffer, $2, $3)).written" foreign import javascript unsafe "(new TextEncoder()).encodeInto($1, new Uint8Array(__exports.memory.buffer, $2, $3)).written"
js_encodeInto :: JSString -> Ptr a -> Int -> IO Int js_encodeInto :: JSString -> Ptr a -> Int -> IO Int
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment