From c396a863bc25e7c542bc4d0a7e68e604de31f1ee Mon Sep 17 00:00:00 2001 From: Hugo Heuzard Date: Wed, 26 Apr 2023 22:33:09 +0200 Subject: [PATCH 1/3] Runtime: fix marshal sharing strings --- runtime/marshal.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/runtime/marshal.js b/runtime/marshal.js index 9c5bb6eb22..9fe2d6c4d9 100644 --- a/runtime/marshal.js +++ b/runtime/marshal.js @@ -552,7 +552,7 @@ function caml_marshal_data_size (s, ofs) { //Provides: MlObjectTable var MlObjectTable; -if (typeof globalThis.WeakMap === 'undefined') { +if (typeof globalThis.Map === 'undefined') { MlObjectTable = function() { /* polyfill (using linear search) */ function NaiveLookup(objs) { this.objs = objs; } @@ -572,7 +572,7 @@ if (typeof globalThis.WeakMap === 'undefined') { } else { MlObjectTable = function MlObjectTable() { - this.objs = []; this.lookup = new globalThis.WeakMap(); + this.objs = []; this.lookup = new globalThis.Map(); }; } @@ -715,6 +715,7 @@ var caml_output_val = function (){ writer.size_32 += 1 + (((len + 4) / 4)|0); writer.size_64 += 1 + (((len + 8) / 8)|0); } else if (caml_is_ml_string(v)) { + if (memo(v)) return; var len = caml_ml_string_length(v); if (len < 0x20) writer.write (8, 0x20 /*cst.PREFIX_SMALL_STRING*/ + len); From bc6fd5a0169e94d8410a4d5d708cfcca68279ccc Mon Sep 17 00:00:00 2001 From: Hugo Heuzard Date: Wed, 26 Apr 2023 22:38:19 +0200 Subject: [PATCH 2/3] Tests: new test, marshaling with sharing of strings --- compiler/tests-jsoo/test_marshal.ml | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/compiler/tests-jsoo/test_marshal.ml b/compiler/tests-jsoo/test_marshal.ml index f0fd7330d4..e9d622a545 100644 --- a/compiler/tests-jsoo/test_marshal.ml +++ b/compiler/tests-jsoo/test_marshal.ml @@ -154,3 +154,15 @@ let%expect_test _ = in Printf.printf "%s ... (%d)\n" (String.sub s 0 20) (String.length s); [%expect {| cccccccccccccccccccc ... (10000) |}] + + +let%expect_test "test sharing of string" = + let s = "AString" in + let p = s,s in + let obj = [p; p] in + Printf.printf "%S" (Marshal.to_string s []); + [%expect + {| "\132\149\166\190\000\000\000\b\000\000\000\001\000\000\000\003\000\000\000\002'AString" |}]; + Printf.printf "%S" (Marshal.to_string obj []); + [%expect + {| "\132\149\166\190\000\000\000\016\000\000\000\004\000\000\000\012\000\000\000\011\160\160'AString\004\001\160\004\003@" |}] From 8e75371d841feec27904ef035436f34dc20c89fb Mon Sep 17 00:00:00 2001 From: Hugo Heuzard Date: Wed, 26 Apr 2023 23:34:19 +0200 Subject: [PATCH 3/3] fix --- CHANGES.md | 1 + compiler/tests-jsoo/test_marshal.ml | 5 ++--- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 64da495910..b594e10d8a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -9,6 +9,7 @@ * Compiler: reduce memory consumption when parsing js * Compiler: parsing js can return a list of token, the list was sometime incorrect * Sourcemap: stop producing sourcemaps mappings with negative lines or columns +* Runtime: fix marshalling with sharing and string (use-js-string) # 5.1.1 (2023-03-15) - Lille ## Bug fixes diff --git a/compiler/tests-jsoo/test_marshal.ml b/compiler/tests-jsoo/test_marshal.ml index e9d622a545..734f0c2145 100644 --- a/compiler/tests-jsoo/test_marshal.ml +++ b/compiler/tests-jsoo/test_marshal.ml @@ -155,11 +155,10 @@ let%expect_test _ = Printf.printf "%s ... (%d)\n" (String.sub s 0 20) (String.length s); [%expect {| cccccccccccccccccccc ... (10000) |}] - let%expect_test "test sharing of string" = let s = "AString" in - let p = s,s in - let obj = [p; p] in + let p = s, s in + let obj = [ p; p ] in Printf.printf "%S" (Marshal.to_string s []); [%expect {| "\132\149\166\190\000\000\000\b\000\000\000\001\000\000\000\003\000\000\000\002'AString" |}];