Cheng Shao pushed to branch wip/wasm-minor-improvements at Glasgow Haskell Compiler / GHC
Commits:
-
f098a7b5
by Cheng Shao at 2025-05-09T21:24:20+00:00
1 changed file:
Changes:
... | ... | @@ -184,15 +184,16 @@ newtype JSString |
184 | 184 | -- eagerly once the resulting 'String' is forced, and the argument
|
185 | 185 | -- 'JSString' may be explicitly freed if no longer used.
|
186 | 186 | fromJSString :: JSString -> String
|
187 | -fromJSString s = unsafeDupablePerformIO $ do
|
|
188 | - l <- js_stringLength s
|
|
189 | - fp <- mallocPlainForeignPtrBytes $ l * 3
|
|
190 | - withForeignPtr fp $ \buf -> do
|
|
191 | - l' <- js_encodeInto s buf $ l * 3
|
|
192 | - peekCStringLen utf8 (buf, l')
|
|
187 | +fromJSString s = case js_stringLength s * 3 of
|
|
188 | + 0 -> ""
|
|
189 | + max_len -> unsafePerformIO $ do
|
|
190 | + fptr <- mallocPlainForeignPtrBytes max_len
|
|
191 | + withForeignPtr fptr $ \ptr -> do
|
|
192 | + len <- js_encodeInto s ptr max_len
|
|
193 | + peekCStringLen utf8 (ptr, len)
|
|
193 | 194 | |
194 | 195 | foreign import javascript unsafe "$1.length"
|
195 | - js_stringLength :: JSString -> IO Int
|
|
196 | + js_stringLength :: JSString -> Int
|
|
196 | 197 | |
197 | 198 | foreign import javascript unsafe "(new TextEncoder()).encodeInto($1, new Uint8Array(__exports.memory.buffer, $2, $3)).written"
|
198 | 199 | js_encodeInto :: JSString -> Ptr a -> Int -> IO Int
|