[Git][ghc/ghc][master] JavaScript: support unicode code points > 2^16 in toJSString using String.fromCodePoint (#23628)

Marge Bot (@marge-bot) gitlab at gitlab.haskell.org
Wed Jul 12 15:25:34 UTC 2023



Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC


Commits:
09a5c6cc by Josh Meredith at 2023-07-12T11:25:13-04:00
JavaScript: support unicode code points > 2^16 in toJSString using String.fromCodePoint (#23628)

- - - - -


3 changed files:

- rts/js/string.js
- testsuite/tests/javascript/js-ffi-string.hs
- testsuite/tests/javascript/js-ffi-string.stdout


Changes:

=====================================
rts/js/string.js
=====================================
@@ -628,7 +628,7 @@ function h$fromHsString(str) {
     var xs = '';
     while(IS_CONS(str)) {
 	var h = CONS_HEAD(str);
-	xs += String.fromCharCode(UNWRAP_NUMBER(h));
+	xs += String.fromCodePoint(UNWRAP_NUMBER(h));
         str = CONS_TAIL(str);
     }
     return xs;


=====================================
testsuite/tests/javascript/js-ffi-string.hs
=====================================
@@ -1,13 +1,49 @@
 import GHC.JS.Prim
+import System.IO
 
 foreign import javascript "((x) => { console.log(x); })"
   log_js_string :: JSVal -> IO ()
 
-foreign import javascript "(() => { return 'a string'; })"
-  a_string :: JSVal
+foreign import javascript "((x, y) => { return x === y; })"
+  eq_JSVal :: JSVal -> JSVal -> Bool
+
+foreign import javascript "(() => { return 'abc\\uD83D\\uDE0A'; })"
+  js_utf16_string :: JSVal
+foreign import javascript "(() => { return 'abc' + String.fromCodePoint(128522); })"
+  js_codepoint_string :: JSVal
+
+-- It's important that this String is defined using a function to avoid rewrite
+-- rules optimising away the use of `toJSString` called on a literal.
+hsString :: String
+hsString = "abc" ++ "\128522"
 
 main :: IO ()
 main = do
-  log_js_string (toJSString "test")
-  putStrLn (fromJSString a_string)
-  putStrLn (fromJSString $ toJSString "test")
+  putStrLn "Does JS `String.fromCodePoint` decode to the expected UTF-16 values? "
+  print (eq_JSVal js_utf16_string js_codepoint_string)
+  hFlush stdout
+  log_js_string js_utf16_string
+  log_js_string js_codepoint_string
+
+  putStrLn "\nDoes `GHC.JS.fromJSString` convert the JavaScript literal string correctly? "
+  print (hsString == fromJSString js_utf16_string)
+  putStrLn hsString
+  putStrLn (fromJSString js_utf16_string)
+
+  putStrLn "\nDoes `GHC.JS.toJSString` convert the Haskell-defined string correctly? "
+  print (eq_JSVal js_utf16_string (toJSString hsString))
+  hFlush stdout
+  log_js_string js_utf16_string
+  log_js_string (toJSString hsString)
+
+  putStrLn "\nDo values survive the Haskell -> JavaScript -> Haskell round-trip? "
+  print (hsString == fromJSString (toJSString hsString))
+  putStrLn hsString
+  putStrLn (fromJSString js_utf16_string)
+
+  putStrLn "\nDo values survive the JavaScript -> Haskell -> JavaScript round-trip? "
+  print (eq_JSVal js_utf16_string (toJSString $ fromJSString js_utf16_string))
+  hFlush stdout
+  log_js_string js_utf16_string
+  log_js_string (toJSString $ fromJSString js_utf16_string)
+


=====================================
testsuite/tests/javascript/js-ffi-string.stdout
=====================================
@@ -1,3 +1,25 @@
-test
-a string
-test
+Does JS `String.fromCodePoint` decode to the expected UTF-16 values? 
+True
+abc😊
+abc😊
+
+Does `GHC.JS.fromJSString` convert the JavaScript literal string correctly? 
+True
+abc😊
+abc😊
+
+Does `GHC.JS.toJSString` convert the Haskell-defined string correctly? 
+True
+abc😊
+abc😊
+
+Do values survive the Haskell -> JavaScript -> Haskell round-trip? 
+True
+abc😊
+abc😊
+
+Do values survive the JavaScript -> Haskell -> JavaScript round-trip? 
+True
+abc😊
+abc😊
+



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/09a5c6cccf8f1b517bc01e8cc924e151d9cbae49

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/09a5c6cccf8f1b517bc01e8cc924e151d9cbae49
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20230712/488ec5c1/attachment-0001.html>


More information about the ghc-commits mailing list