Skip to content
Snippets Groups Projects
Commit eac196df authored by Cheng Shao's avatar Cheng Shao :beach: Committed by Marge Bot
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.
parent eaa8093b
No related branches found
No related tags found
No related merge requests found
......@@ -184,15 +184,16 @@ newtype JSString
-- eagerly once the resulting 'String' is forced, and the argument
-- 'JSString' may be explicitly freed if no longer used.
fromJSString :: JSString -> String
fromJSString s = unsafeDupablePerformIO $ do
l <- js_stringLength s
fp <- mallocPlainForeignPtrBytes $ l * 3
withForeignPtr fp $ \buf -> do
l' <- js_encodeInto s buf $ l * 3
peekCStringLen utf8 (buf, l')
fromJSString s = case js_stringLength s * 3 of
0 -> ""
max_len -> unsafePerformIO $ do
fptr <- mallocPlainForeignPtrBytes max_len
withForeignPtr fptr $ \ptr -> do
len <- js_encodeInto s ptr max_len
peekCStringLen utf8 (ptr, len)
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"
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