|
22 | 22 | * along with this program; if not, write to the Free Software
|
23 | 23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
|
24 | 24 |
|
25 |
| -module Map = { |
26 |
| - type t<'k, 'v> |
| 25 | +type t = {@as("RE_EXN_ID") id: string} |
27 | 26 |
|
28 |
| - @new external make: unit => t<'k, 'v> = "Map" |
| 27 | +module Dict = { |
| 28 | + @obj |
| 29 | + external empty: unit => dict<'a> = "" |
29 | 30 |
|
30 |
| - @send external set: (t<'k, 'v>, 'k, 'v) => unit = "set" |
| 31 | + @set_index |
| 32 | + external set: (dict<'a>, string, 'a) => unit = "" |
31 | 33 |
|
32 |
| - @send external get: (t<'k, 'v>, 'k) => option<'v> = "get" |
| 34 | + @get_index |
| 35 | + /** |
| 36 | + It's the same as `Js.Dict.get` but it doesn't have runtime overhead to check if the key exists. |
| 37 | + */ |
| 38 | + external dangerouslyGetNonOption: (dict<'a>, string) => option<'a> = "" |
33 | 39 | }
|
34 | 40 |
|
35 |
| -type t = {@as("RE_EXN_ID") id: string} |
36 |
| - |
37 | 41 | /**
|
38 |
| - Could be exported for better inlining |
39 |
| - It's common that we have |
40 |
| - {[ a = caml_set_oo_id([248,"string",0]) ]} |
41 |
| - This can be inlined as |
42 |
| - {[ a = caml_set_oo_id([248,"string", caml_oo_last_id++]) ]} |
| 42 | + Needs to have unique extension ids when used with functors. |
| 43 | + See discussion in https://github.com/rescript-lang/rescript-compiler/pull/6570 |
43 | 44 | */
|
44 |
| -let idMap: Map.t<string, int> = Map.make() |
| 45 | +let idMap = Dict.empty() |
45 | 46 |
|
46 | 47 | let create = (str: string): string => {
|
47 |
| - let id = switch idMap->Map.get(str) { |
| 48 | + switch idMap->Dict.dangerouslyGetNonOption(str) { |
48 | 49 | | Some(v) => {
|
49 | 50 | let id = v + 1
|
50 |
| - idMap->Map.set(str, id) |
51 |
| - id |
| 51 | + idMap->Dict.set(str, id) |
| 52 | + str ++ ("/" ++ (Obj.magic((id: int)): string)) |
52 | 53 | }
|
53 | 54 | | None => {
|
54 |
| - let id = 1 |
55 |
| - idMap->Map.set(str, id) |
56 |
| - id |
| 55 | + idMap->Dict.set(str, 1) |
| 56 | + str |
57 | 57 | }
|
58 | 58 | }
|
59 |
| - |
60 |
| - str ++ ("/" ++ (Obj.magic((id: int)): string)) |
61 | 59 | }
|
62 | 60 |
|
63 | 61 | /**
|
|
0 commit comments