[Git][ghc/ghc][master] JS: fix h$withCStringOnHeap helper (#25288)
Marge Bot (@marge-bot)
gitlab at gitlab.haskell.org
Wed Sep 25 21:13:28 UTC 2024
Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC
Commits:
97a6c6c3 by Sylvain Henry at 2024-09-25T17:12:18-04:00
JS: fix h$withCStringOnHeap helper (#25288)
strlen returns the length of the string without the \0 terminating byte,
hence CString weren't properly allocated on the heap (ending \0 byte was
missing).
- - - - -
6 changed files:
- rts/js/mem.js
- + testsuite/tests/javascript/js-c-sources/all.T
- + testsuite/tests/javascript/js-c-sources/js-c-sources02.hs
- + testsuite/tests/javascript/js-c-sources/js-c-sources02.stdout
- + testsuite/tests/javascript/js-c-sources/js-c-sources02_c.c
- + testsuite/tests/javascript/js-c-sources/js-c-sources02_js.js
Changes:
=====================================
rts/js/mem.js
=====================================
@@ -1577,7 +1577,8 @@ function h$withCBufferOnHeap(str_d, str_o, len, cont) {
// Temporarily allocate a CString on the heap and pass it to the continuation.
// The string is freed from the heap when the continuation returns.
function h$withCStringOnHeap(str_d, str_o, cont) {
- return h$withCBufferOnHeap(str_d, str_o, str_d === null ? 0 : h$strlen(str_d,str_o), cont);
+ // strlen + 1 for the null terminating byte (#25288)
+ return h$withCBufferOnHeap(str_d, str_o, str_d === null ? 0 : h$strlen(str_d,str_o)+1, cont);
}
// Dereference a heap pointer to a heap pointer (a 32-bit offset in the heap)
=====================================
testsuite/tests/javascript/js-c-sources/all.T
=====================================
@@ -0,0 +1,5 @@
+# These are JavaScript-specific tests
+setTestOpts(when(not(js_arch()),skip))
+
+test('js-c-sources01', extra_files(['js-c-sources01_c.c','js-c-sources01_js.js']), compile_and_run, ['js-c-sources01_c.c js-c-sources01_js.js'])
+test('js-c-sources02', extra_files(['js-c-sources02_c.c','js-c-sources02_js.js']), compile_and_run, ['js-c-sources02_c.c js-c-sources02_js.js'])
=====================================
testsuite/tests/javascript/js-c-sources/js-c-sources02.hs
=====================================
@@ -0,0 +1,6 @@
+import Foreign.C.String
+
+main :: IO ()
+main = withCString "Some string" foo
+
+foreign import javascript "foo" foo :: CString -> IO ()
=====================================
testsuite/tests/javascript/js-c-sources/js-c-sources02.stdout
=====================================
@@ -0,0 +1 @@
+Arg: Some string
=====================================
testsuite/tests/javascript/js-c-sources/js-c-sources02_c.c
=====================================
@@ -0,0 +1,6 @@
+#include<stdio.h>
+#include<stdlib.h>
+
+void foo_c(char * s) {
+ printf("Arg: %s\n", s);
+}
=====================================
testsuite/tests/javascript/js-c-sources/js-c-sources02_js.js
=====================================
@@ -0,0 +1,7 @@
+//#OPTIONS:CPP
+//#OPTIONS:EMCC:EXPORTED_FUNCTIONS=_foo_c
+//#OPTIONS:EMCC:EXPORTED_FUNCTIONS=_malloc,_free,_strlen
+
+function foo(str_d,str_o) {
+ h$withCStringOnHeap(str_d,str_o, (ptr) => _foo_c(ptr));
+}
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/97a6c6c3ded91af838f4376e4ad0781f0e971daf
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/97a6c6c3ded91af838f4376e4ad0781f0e971daf
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/20240925/4b39c3fb/attachment-0001.html>
More information about the ghc-commits
mailing list