Skip to content

Commit 7d1fed8

Browse files
authored
Deterministic runtime representation for extensible types and exceptions (#6570)
* feat: changing runtime representation of extension and exceptions * chore: update changelog * feat: not exposing map and idmap in caml_exceptions
1 parent 3f22f9a commit 7d1fed8

8 files changed

+88
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
- Generic JSX transform: Set default config params for `jsxConfig`. https://github.com/rescript-lang/rescript-compiler/pull/6606
2020
- Generic JSX transform: Handle namespaced names. https://github.com/rescript-lang/rescript-compiler/pull/6606
2121
- Fix issue with doc comment in recursive module. https://github.com/rescript-lang/rescript-compiler/pull/6613
22+
- Fix issue with Exceptions and Extensible types runtime generation. https://github.com/rescript-lang/rescript-compiler/pull/6570
2223

2324
#### :house: Internal
2425

jscomp/runtime/caml_exceptions.res

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
2424

25+
module Map = {
26+
type t<'k, 'v>
27+
28+
@new external make: unit => t<'k, 'v> = "Map"
29+
30+
@send external set: (t<'k, 'v>, 'k, 'v) => unit = "set"
31+
32+
@send external get: (t<'k, 'v>, 'k) => option<'v> = "get"
33+
}
34+
2535
type t = {@as("RE_EXN_ID") id: string}
2636

2737
/**
@@ -31,11 +41,23 @@ type t = {@as("RE_EXN_ID") id: string}
3141
This can be inlined as
3242
{[ a = caml_set_oo_id([248,"string", caml_oo_last_id++]) ]}
3343
*/
34-
let id = ref(0)
44+
let idMap: Map.t<string, int> = Map.make()
3545

3646
let create = (str: string): string => {
37-
id.contents = id.contents + 1
38-
str ++ ("/" ++ (Obj.magic((id.contents: int)): string))
47+
let id = switch idMap->Map.get(str) {
48+
| Some(v) => {
49+
let id = v + 1
50+
idMap->Map.set(str, id)
51+
id
52+
}
53+
| None => {
54+
let id = 1
55+
idMap->Map.set(str, id)
56+
id
57+
}
58+
}
59+
60+
str ++ ("/" ++ (Obj.magic((id: int)): string))
3961
}
4062

4163
/**

jscomp/runtime/caml_exceptions.resi

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* Copyright (C) 2015-2016 Bloomberg Finance L.P.
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24+
25+
type t = {@as("RE_EXN_ID") id: string}
26+
27+
let create: string => string
28+
29+
let is_extension: 'a => bool
30+
31+
let exn_slot_name: t => string

jscomp/runtime/release.ninja

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ o runtime/caml_array.cmj : cc_cmi runtime/caml_array.res | runtime/caml_array.cm
1919
o runtime/caml_array.cmi : cc runtime/caml_array.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
2020
o runtime/caml_bytes.cmj : cc_cmi runtime/caml_bytes.res | runtime/caml_bytes.cmi
2121
o runtime/caml_bytes.cmi : cc runtime/caml_bytes.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
22+
o runtime/caml_exceptions.cmj : cc_cmi runtime/caml_exceptions.res | runtime/caml_exceptions.cmi runtime/js.cmj
23+
o runtime/caml_exceptions.cmi : cc runtime/caml_exceptions.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
2224
o runtime/caml_float.cmj : cc_cmi runtime/caml_float.res | runtime/caml_float.cmi runtime/caml_float_extern.cmj
2325
o runtime/caml_float.cmi : cc runtime/caml_float.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
2426
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
5052
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
5153
o runtime/caml_sys.cmi : cc runtime/caml_sys.resi | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
5254
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
53-
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
5455
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
5556
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
5657
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
5960
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
6061
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
6162
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
62-
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
63+
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

lib/es6/caml_exceptions.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11

22

33

4-
var id = {
5-
contents: 0
6-
};
4+
var idMap = new Map();
75

86
function create(str) {
9-
id.contents = id.contents + 1 | 0;
10-
return str + ("/" + id.contents);
7+
var v = idMap.get(str);
8+
var id;
9+
if (v !== undefined) {
10+
var id$1 = v + 1 | 0;
11+
idMap.set(str, id$1);
12+
id = id$1;
13+
} else {
14+
idMap.set(str, 1);
15+
id = 1;
16+
}
17+
return str + ("/" + id);
1118
}
1219

1320
function is_extension(e) {
@@ -23,9 +30,8 @@ function exn_slot_name(x) {
2330
}
2431

2532
export {
26-
id ,
2733
create ,
2834
is_extension ,
2935
exn_slot_name ,
3036
}
31-
/* No side effect */
37+
/* idMap Not a pure module */

lib/es6/caml_js_exceptions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ export {
2828
internalToOCamlException ,
2929
as_js_exn ,
3030
}
31-
/* No side effect */
31+
/* Caml_exceptions Not a pure module */

lib/js/caml_exceptions.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
'use strict';
22

33

4-
var id = {
5-
contents: 0
6-
};
4+
var idMap = new Map();
75

86
function create(str) {
9-
id.contents = id.contents + 1 | 0;
10-
return str + ("/" + id.contents);
7+
var v = idMap.get(str);
8+
var id;
9+
if (v !== undefined) {
10+
var id$1 = v + 1 | 0;
11+
idMap.set(str, id$1);
12+
id = id$1;
13+
} else {
14+
idMap.set(str, 1);
15+
id = 1;
16+
}
17+
return str + ("/" + id);
1118
}
1219

1320
function is_extension(e) {
@@ -22,8 +29,7 @@ function exn_slot_name(x) {
2229
return x.RE_EXN_ID;
2330
}
2431

25-
exports.id = id;
2632
exports.create = create;
2733
exports.is_extension = is_extension;
2834
exports.exn_slot_name = exn_slot_name;
29-
/* No side effect */
35+
/* idMap Not a pure module */

lib/js/caml_js_exceptions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ function as_js_exn(exn) {
2626
exports.$$Error = $$Error;
2727
exports.internalToOCamlException = internalToOCamlException;
2828
exports.as_js_exn = as_js_exn;
29-
/* No side effect */
29+
/* Caml_exceptions Not a pure module */

0 commit comments

Comments
 (0)