Skip to content

Commit 02f5770

Browse files
committed
feat: changing runtime representation of extension and exceptions
1 parent aa29841 commit 02f5770

File tree

5 files changed

+61
-19
lines changed

5 files changed

+61
-19
lines changed

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
/**

lib/es6/caml_exceptions.js

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

22

33

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

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

1322
function is_extension(e) {
@@ -23,9 +32,10 @@ function exn_slot_name(x) {
2332
}
2433

2534
export {
26-
id ,
35+
$$Map ,
36+
idMap ,
2737
create ,
2838
is_extension ,
2939
exn_slot_name ,
3040
}
31-
/* No side effect */
41+
/* 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: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
'use strict';
22

33

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

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

1322
function is_extension(e) {
@@ -22,8 +31,9 @@ function exn_slot_name(x) {
2231
return x.RE_EXN_ID;
2332
}
2433

25-
exports.id = id;
34+
exports.$$Map = $$Map;
35+
exports.idMap = idMap;
2636
exports.create = create;
2737
exports.is_extension = is_extension;
2838
exports.exn_slot_name = exn_slot_name;
29-
/* No side effect */
39+
/* 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)