From 90ee4684ef914b154d7b84a060b719b3ba0b51c9 Mon Sep 17 00:00:00 2001 From: Diogo Mafra Date: Tue, 30 Jan 2024 18:11:48 -0300 Subject: [PATCH 1/3] feat: changing runtime representation of extension and exceptions --- jscomp/runtime/caml_exceptions.res | 28 +++++++++++++++++++++++++--- lib/es6/caml_exceptions.js | 24 +++++++++++++++++------- lib/es6/caml_js_exceptions.js | 2 +- lib/js/caml_exceptions.js | 24 +++++++++++++++++------- lib/js/caml_js_exceptions.js | 2 +- 5 files changed, 61 insertions(+), 19 deletions(-) diff --git a/jscomp/runtime/caml_exceptions.res b/jscomp/runtime/caml_exceptions.res index d4e53adb36..796e8178c9 100644 --- a/jscomp/runtime/caml_exceptions.res +++ b/jscomp/runtime/caml_exceptions.res @@ -22,6 +22,16 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +module Map = { + type t<'k, 'v> + + @new external make: unit => t<'k, 'v> = "Map" + + @send external set: (t<'k, 'v>, 'k, 'v) => unit = "set" + + @send external get: (t<'k, 'v>, 'k) => option<'v> = "get" +} + type t = {@as("RE_EXN_ID") id: string} /** @@ -31,11 +41,23 @@ type t = {@as("RE_EXN_ID") id: string} This can be inlined as {[ a = caml_set_oo_id([248,"string", caml_oo_last_id++]) ]} */ -let id = ref(0) +let idMap: Map.t = Map.make() let create = (str: string): string => { - id.contents = id.contents + 1 - str ++ ("/" ++ (Obj.magic((id.contents: int)): string)) + let id = switch idMap->Map.get(str) { + | Some(v) => { + let id = v + 1 + idMap->Map.set(str, id) + id + } + | None => { + let id = 1 + idMap->Map.set(str, id) + id + } + } + + str ++ ("/" ++ (Obj.magic((id: int)): string)) } /** diff --git a/lib/es6/caml_exceptions.js b/lib/es6/caml_exceptions.js index b25ad9ea52..ad9f57dcf4 100644 --- a/lib/es6/caml_exceptions.js +++ b/lib/es6/caml_exceptions.js @@ -1,13 +1,22 @@ -var id = { - contents: 0 -}; +var $$Map = {}; + +var idMap = new Map(); function create(str) { - id.contents = id.contents + 1 | 0; - return str + ("/" + id.contents); + var v = idMap.get(str); + var id; + if (v !== undefined) { + var id$1 = v + 1 | 0; + idMap.set(str, id$1); + id = id$1; + } else { + idMap.set(str, 1); + id = 1; + } + return str + ("/" + id); } function is_extension(e) { @@ -23,9 +32,10 @@ function exn_slot_name(x) { } export { - id , + $$Map , + idMap , create , is_extension , exn_slot_name , } -/* No side effect */ +/* idMap Not a pure module */ diff --git a/lib/es6/caml_js_exceptions.js b/lib/es6/caml_js_exceptions.js index 00a007ab8f..91f383ca2c 100644 --- a/lib/es6/caml_js_exceptions.js +++ b/lib/es6/caml_js_exceptions.js @@ -28,4 +28,4 @@ export { internalToOCamlException , as_js_exn , } -/* No side effect */ +/* Caml_exceptions Not a pure module */ diff --git a/lib/js/caml_exceptions.js b/lib/js/caml_exceptions.js index 36797aaf82..b167cf2d57 100644 --- a/lib/js/caml_exceptions.js +++ b/lib/js/caml_exceptions.js @@ -1,13 +1,22 @@ 'use strict'; -var id = { - contents: 0 -}; +var $$Map = {}; + +var idMap = new Map(); function create(str) { - id.contents = id.contents + 1 | 0; - return str + ("/" + id.contents); + var v = idMap.get(str); + var id; + if (v !== undefined) { + var id$1 = v + 1 | 0; + idMap.set(str, id$1); + id = id$1; + } else { + idMap.set(str, 1); + id = 1; + } + return str + ("/" + id); } function is_extension(e) { @@ -22,8 +31,9 @@ function exn_slot_name(x) { return x.RE_EXN_ID; } -exports.id = id; +exports.$$Map = $$Map; +exports.idMap = idMap; exports.create = create; exports.is_extension = is_extension; exports.exn_slot_name = exn_slot_name; -/* No side effect */ +/* idMap Not a pure module */ diff --git a/lib/js/caml_js_exceptions.js b/lib/js/caml_js_exceptions.js index aae2e4a375..e8a1073354 100644 --- a/lib/js/caml_js_exceptions.js +++ b/lib/js/caml_js_exceptions.js @@ -26,4 +26,4 @@ function as_js_exn(exn) { exports.$$Error = $$Error; exports.internalToOCamlException = internalToOCamlException; exports.as_js_exn = as_js_exn; -/* No side effect */ +/* Caml_exceptions Not a pure module */ From 9172ed4b17992afab33f9ba2bddb25770c8ef75b Mon Sep 17 00:00:00 2001 From: Diogo Mafra Date: Mon, 5 Feb 2024 19:14:47 -0300 Subject: [PATCH 2/3] chore: update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 00bf3af9f8..059bd58f92 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ - Generic JSX transform: Set default config params for `jsxConfig`. https://github.com/rescript-lang/rescript-compiler/pull/6606 - Generic JSX transform: Handle namespaced names. https://github.com/rescript-lang/rescript-compiler/pull/6606 - Fix issue with doc comment in recursive module. https://github.com/rescript-lang/rescript-compiler/pull/6613 +- Fix issue with Exceptions and Extensible types runtime generation. https://github.com/rescript-lang/rescript-compiler/pull/6570 #### :house: Internal From d0dc3a79ebf48e3ba99025fad35269df711b69a3 Mon Sep 17 00:00:00 2001 From: Diogo Mafra Date: Mon, 5 Feb 2024 19:20:24 -0300 Subject: [PATCH 3/3] feat: not exposing map and idmap in caml_exceptions --- jscomp/runtime/caml_exceptions.resi | 31 +++++++++++++++++++++++++++++ jscomp/runtime/release.ninja | 5 +++-- lib/es6/caml_exceptions.js | 4 ---- lib/js/caml_exceptions.js | 4 ---- 4 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 jscomp/runtime/caml_exceptions.resi diff --git a/jscomp/runtime/caml_exceptions.resi b/jscomp/runtime/caml_exceptions.resi new file mode 100644 index 0000000000..da55d4d07b --- /dev/null +++ b/jscomp/runtime/caml_exceptions.resi @@ -0,0 +1,31 @@ +/* Copyright (C) 2015-2016 Bloomberg Finance L.P. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * In addition to the permissions granted to you by the LGPL, you may combine + * or link a "work that uses the Library" with a publicly distributed version + * of this file to produce a combined library or application, then distribute + * that combined work under the terms of your choosing, with no requirement + * to comply with the obligations normally placed on you by section 4 of the + * LGPL version 3 (or the corresponding section of a later version of the LGPL + * should you choose to use a later version). + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + +type t = {@as("RE_EXN_ID") id: string} + +let create: string => string + +let is_extension: 'a => bool + +let exn_slot_name: t => string diff --git a/jscomp/runtime/release.ninja b/jscomp/runtime/release.ninja index b7b44625e4..864f9c5a77 100644 --- a/jscomp/runtime/release.ninja +++ b/jscomp/runtime/release.ninja @@ -19,6 +19,8 @@ o runtime/caml_array.cmj : cc_cmi runtime/caml_array.res | runtime/caml_array.cm o runtime/caml_array.cmi : cc runtime/caml_array.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj o runtime/caml_bytes.cmj : cc_cmi runtime/caml_bytes.res | runtime/caml_bytes.cmi o runtime/caml_bytes.cmi : cc runtime/caml_bytes.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj +o runtime/caml_exceptions.cmj : cc_cmi runtime/caml_exceptions.res | runtime/caml_exceptions.cmi runtime/js.cmj +o runtime/caml_exceptions.cmi : cc runtime/caml_exceptions.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj o runtime/caml_float.cmj : cc_cmi runtime/caml_float.res | runtime/caml_float.cmi runtime/caml_float_extern.cmj o runtime/caml_float.cmi : cc runtime/caml_float.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj o runtime/caml_format.cmj : cc_cmi runtime/caml_format.ml | runtime/caml.cmj runtime/caml_float.cmj runtime/caml_float_extern.cmj runtime/caml_format.cmi runtime/caml_int64.cmj runtime/caml_int64_extern.cmj runtime/caml_nativeint_extern.cmj runtime/caml_string_extern.cmj @@ -50,7 +52,6 @@ o runtime/caml_string.cmi : cc runtime/caml_string.resi | runtime/bs_stdlib_mini o runtime/caml_sys.cmj : cc_cmi runtime/caml_sys.res | runtime/caml_array_extern.cmj runtime/caml_sys.cmi runtime/caml_undefined_extern.cmj runtime/js.cmj o runtime/caml_sys.cmi : cc runtime/caml_sys.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj o runtime/caml_array_extern.cmi runtime/caml_array_extern.cmj : cc runtime/caml_array_extern.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj -o runtime/caml_exceptions.cmi runtime/caml_exceptions.cmj : cc runtime/caml_exceptions.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj o runtime/caml_external_polyfill.cmi runtime/caml_external_polyfill.cmj : cc runtime/caml_external_polyfill.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj o runtime/caml_float_extern.cmi runtime/caml_float_extern.cmj : cc runtime/caml_float_extern.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj o runtime/caml_int64_extern.cmi runtime/caml_int64_extern.cmj : cc runtime/caml_int64_extern.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj @@ -59,4 +60,4 @@ o runtime/caml_nativeint_extern.cmi runtime/caml_nativeint_extern.cmj : cc runti o runtime/caml_string_extern.cmi runtime/caml_string_extern.cmj : cc runtime/caml_string_extern.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj o runtime/caml_undefined_extern.cmi runtime/caml_undefined_extern.cmj : cc runtime/caml_undefined_extern.res | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj o runtime/curry.cmi runtime/curry.cmj : cc runtime/curry.res | runtime/bs_stdlib_mini.cmi runtime/caml_array.cmj runtime/caml_array_extern.cmj runtime/js.cmi runtime/js.cmj -o runtime : phony runtime/bs_stdlib_mini.cmi runtime/js.cmj runtime/js.cmi runtime/caml.cmi runtime/caml.cmj runtime/caml_array.cmi runtime/caml_array.cmj runtime/caml_bytes.cmi runtime/caml_bytes.cmj runtime/caml_float.cmi runtime/caml_float.cmj runtime/caml_format.cmi runtime/caml_format.cmj runtime/caml_hash.cmi runtime/caml_hash.cmj runtime/caml_hash_primitive.cmi runtime/caml_hash_primitive.cmj runtime/caml_int32.cmi runtime/caml_int32.cmj runtime/caml_int64.cmi runtime/caml_int64.cmj runtime/caml_lexer.cmi runtime/caml_lexer.cmj runtime/caml_md5.cmi runtime/caml_md5.cmj runtime/caml_module.cmi runtime/caml_module.cmj runtime/caml_obj.cmi runtime/caml_obj.cmj runtime/caml_option.cmi runtime/caml_option.cmj runtime/caml_parser.cmi runtime/caml_parser.cmj runtime/caml_splice_call.cmi runtime/caml_splice_call.cmj runtime/caml_string.cmi runtime/caml_string.cmj runtime/caml_sys.cmi runtime/caml_sys.cmj runtime/caml_array_extern.cmi runtime/caml_array_extern.cmj runtime/caml_exceptions.cmi runtime/caml_exceptions.cmj runtime/caml_external_polyfill.cmi runtime/caml_external_polyfill.cmj runtime/caml_float_extern.cmi runtime/caml_float_extern.cmj runtime/caml_int64_extern.cmi runtime/caml_int64_extern.cmj runtime/caml_js_exceptions.cmi runtime/caml_js_exceptions.cmj runtime/caml_nativeint_extern.cmi runtime/caml_nativeint_extern.cmj runtime/caml_string_extern.cmi runtime/caml_string_extern.cmj runtime/caml_undefined_extern.cmi runtime/caml_undefined_extern.cmj runtime/curry.cmi runtime/curry.cmj +o runtime : phony runtime/bs_stdlib_mini.cmi runtime/js.cmj runtime/js.cmi runtime/caml.cmi runtime/caml.cmj runtime/caml_array.cmi runtime/caml_array.cmj runtime/caml_bytes.cmi runtime/caml_bytes.cmj runtime/caml_exceptions.cmi runtime/caml_exceptions.cmj runtime/caml_float.cmi runtime/caml_float.cmj runtime/caml_format.cmi runtime/caml_format.cmj runtime/caml_hash.cmi runtime/caml_hash.cmj runtime/caml_hash_primitive.cmi runtime/caml_hash_primitive.cmj runtime/caml_int32.cmi runtime/caml_int32.cmj runtime/caml_int64.cmi runtime/caml_int64.cmj runtime/caml_lexer.cmi runtime/caml_lexer.cmj runtime/caml_md5.cmi runtime/caml_md5.cmj runtime/caml_module.cmi runtime/caml_module.cmj runtime/caml_obj.cmi runtime/caml_obj.cmj runtime/caml_option.cmi runtime/caml_option.cmj runtime/caml_parser.cmi runtime/caml_parser.cmj runtime/caml_splice_call.cmi runtime/caml_splice_call.cmj runtime/caml_string.cmi runtime/caml_string.cmj runtime/caml_sys.cmi runtime/caml_sys.cmj runtime/caml_array_extern.cmi runtime/caml_array_extern.cmj runtime/caml_external_polyfill.cmi runtime/caml_external_polyfill.cmj runtime/caml_float_extern.cmi runtime/caml_float_extern.cmj runtime/caml_int64_extern.cmi runtime/caml_int64_extern.cmj runtime/caml_js_exceptions.cmi runtime/caml_js_exceptions.cmj runtime/caml_nativeint_extern.cmi runtime/caml_nativeint_extern.cmj runtime/caml_string_extern.cmi runtime/caml_string_extern.cmj runtime/caml_undefined_extern.cmi runtime/caml_undefined_extern.cmj runtime/curry.cmi runtime/curry.cmj diff --git a/lib/es6/caml_exceptions.js b/lib/es6/caml_exceptions.js index ad9f57dcf4..8fc99455d9 100644 --- a/lib/es6/caml_exceptions.js +++ b/lib/es6/caml_exceptions.js @@ -1,8 +1,6 @@ -var $$Map = {}; - var idMap = new Map(); function create(str) { @@ -32,8 +30,6 @@ function exn_slot_name(x) { } export { - $$Map , - idMap , create , is_extension , exn_slot_name , diff --git a/lib/js/caml_exceptions.js b/lib/js/caml_exceptions.js index b167cf2d57..e440b2e406 100644 --- a/lib/js/caml_exceptions.js +++ b/lib/js/caml_exceptions.js @@ -1,8 +1,6 @@ 'use strict'; -var $$Map = {}; - var idMap = new Map(); function create(str) { @@ -31,8 +29,6 @@ function exn_slot_name(x) { return x.RE_EXN_ID; } -exports.$$Map = $$Map; -exports.idMap = idMap; exports.create = create; exports.is_extension = is_extension; exports.exn_slot_name = exn_slot_name;