diff --git a/Changes.md b/Changes.md index 8e46981d2a..326e2ea686 100644 --- a/Changes.md +++ b/Changes.md @@ -25,7 +25,8 @@ Unreleased ([#472](https://github.com/melange-re/melange/pull/472)) - melange: Make `Pervasives` exactly match the `Stdlib` behavior ([#476](https://github.com/melange-re/melange/pull/476)) -- melange: +- melange: fix unbound error when trying to use `Printexc.exn_slot_id` + ([#482](https://github.com/melange-re/melange/pull/482)) 0.3.2 2022-11-19 --------------- diff --git a/jscomp/runtime/caml_exceptions.ml b/jscomp/runtime/caml_exceptions.ml index 3bc728a2c1..53874ee111 100644 --- a/jscomp/runtime/caml_exceptions.ml +++ b/jscomp/runtime/caml_exceptions.ml @@ -78,7 +78,7 @@ let create (str : string) : string = This is not a problem in `try .. with` since the logic above is not expressible, see more design in [destruct_exn.md] *) let caml_is_extension (type a ) (e : a) : bool = - if Js.testAny e then false + if Js.testAny e then false else Js.typeof (Obj.magic e : t) .id = "string" @@ -86,3 +86,18 @@ let caml_is_extension (type a ) (e : a) : bool = (**FIXME: remove the trailing `/` *) let caml_exn_slot_name (x : t) : string = x.id +let caml_exn_slot_id : t -> int = [%raw{|function(x){ + if (x.RE_EXN_ID != null) { + var parts = x.RE_EXN_ID.split("/"); + if (parts.length > 1) { + return Number(parts[parts.length - 1]) + } else { + return -1; + } + } else { + return -1; + } +} +|}] + + diff --git a/test/print-exn.t b/test/print-exn.t index ececabcb68..ffbfb83472 100644 --- a/test/print-exn.t +++ b/test/print-exn.t @@ -9,6 +9,11 @@ An example that uses exceptions runtime > try f () with > | exn -> Printexc.exn_slot_id exn > let result = run (fun _ -> failwith "oops") + > + > exception Custom + > let () = + > Js.log2 (Printexc.exn_slot_id Custom) (Printexc.exn_slot_id Not_found) + > > EOF $ cat > dune-project < (lang dune 3.7) @@ -21,6 +26,5 @@ An example that uses exceptions runtime > (module_system commonjs)) > EOF $ dune build @melange - $ node ./_build/default/melange/x.js 2>&1 | awk '/exn_slot_id/' - return Printexc.exn_slot_id(Caml_js_exceptions.internalToOCamlException(raw_exn)); - TypeError: Printexc.exn_slot_id is not a function + $ node ./_build/default/melange/x.js + 7 -1