Skip to content

Commit 64f9ef1

Browse files
authored
Throw as instance of JS new Error() (#6611)
* throw as instance of JS `new Error()` Adapted from melange-re/melange#1036 Thanks @anmonteiro * uncomment test * update output * update build * [skip ci]: update CHANGELOG.md
1 parent 86633e2 commit 64f9ef1

File tree

245 files changed

+18470
-15872
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

245 files changed

+18470
-15872
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
- Allow `@directive` on functions for emitting function level directive code (`let serverAction = @directive("'use server'") (~name) => {...}`). https://github.com/rescript-lang/rescript-compiler/pull/6756
1818
- Add `rewatch` to the npm package as an alternative build tool. https://github.com/rescript-lang/rescript-compiler/pull/6762
19+
- Throws an instance of JavaScript's `new Error()` and adds the extension payload for `cause` option. https://github.com/rescript-lang/rescript-compiler/pull/6611
1920

2021
#### :boom: Breaking Change
2122

jscomp/core/js_dump.ml

+30-10
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,11 @@ type cxt = Ext_pp_scope.t
9797
let semi f = P.string f L.semi
9898
let comma f = P.string f L.comma
9999

100+
let new_error name cause =
101+
E.new_ (E.js_global Js_dump_lit.error) [ name; cause ]
102+
100103
let exn_block_as_obj ~(stack : bool) (el : J.expression list) (ext : J.tag_info)
101-
: J.expression_desc =
104+
: J.expression =
102105
let field_name =
103106
match ext with
104107
| Blk_extension -> (
@@ -108,12 +111,29 @@ let exn_block_as_obj ~(stack : bool) (el : J.expression list) (ext : J.tag_info)
108111
fun i -> match i with 0 -> Literals.exception_id | i -> ss.(i - 1))
109112
| _ -> assert false
110113
in
111-
Object
112-
(if stack then
113-
Ext_list.mapi_append el
114-
(fun i e -> (Js_op.Lit (field_name i), e))
115-
[ (Js_op.Lit "Error", E.new_ (E.js_global "Error") []) ]
116-
else Ext_list.mapi el (fun i e -> (Js_op.Lit (field_name i), e)))
114+
let cause =
115+
{
116+
J.expression_desc =
117+
Object (List.mapi (fun i e -> (Js_op.Lit (field_name i), e)) el);
118+
comment = None;
119+
}
120+
in
121+
if stack then
122+
new_error (List.hd el)
123+
{
124+
J.expression_desc = Object [ (Lit Js_dump_lit.cause, cause) ];
125+
comment = None;
126+
}
127+
else cause
128+
129+
let exn_ref_as_obj e : J.expression =
130+
let cause = { J.expression_desc = e; comment = None; } in
131+
new_error
132+
(E.record_access cause Js_dump_lit.exception_id 0l)
133+
{
134+
J.expression_desc = Object [ (Lit Js_dump_lit.cause, cause) ];
135+
comment = None;
136+
}
117137

118138
let rec iter_lst cxt (f : P.t) ls element inter =
119139
match ls with
@@ -730,7 +750,7 @@ and expression_desc cxt ~(level : int) f x : cxt =
730750
])
731751
| _ -> assert false)
732752
| Caml_block (el, _, _, ((Blk_extension | Blk_record_ext _) as ext)) ->
733-
expression_desc cxt ~level f (exn_block_as_obj ~stack:false el ext)
753+
expression cxt ~level f (exn_block_as_obj ~stack:false el ext)
734754
| Caml_block (el, _, tag, Blk_record_inlined p) ->
735755
let untagged = Ast_untagged_variants.process_untagged p.attrs in
736756
let objs =
@@ -1184,8 +1204,8 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
11841204
let e =
11851205
match e.expression_desc with
11861206
| Caml_block (el, _, _, ((Blk_extension | Blk_record_ext _) as ext)) ->
1187-
{ e with expression_desc = exn_block_as_obj ~stack:true el ext }
1188-
| _ -> e
1207+
{ e with expression_desc = (exn_block_as_obj ~stack:true el ext).expression_desc }
1208+
| exp -> { e with expression_desc = (exn_ref_as_obj exp).expression_desc }
11891209
in
11901210
P.string f L.throw;
11911211
P.space f;

jscomp/core/js_dump_lit.ml

+6
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,9 @@ let block_variant = "variant"
164164
let block_simple_variant = "simpleVariant"
165165

166166
let case = "case"
167+
168+
let cause = "cause"
169+
170+
let error = "Error"
171+
172+
let exception_id = "RE_EXN_ID"

jscomp/core/js_exp_make.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ let poly_var_value_access (e : t) =
416416
comment = None;
417417
}
418418

419-
let extension_access (e : t) name (pos : int32) : t =
419+
let extension_access (e : t) ?name (pos : int32) : t =
420420
match e.expression_desc with
421421
| Array (l, _) (* Float i -- should not appear here *)
422422
| Caml_block (l, _, _, _)

jscomp/core/js_exp_make.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ val variant_access : t -> int32 -> t
170170

171171
val cons_access : t -> int32 -> t
172172

173-
val extension_access : t -> string option -> Int32.t -> t
173+
val extension_access : t -> ?name:string -> Int32.t -> t
174174

175175
val record_assign : t -> int32 -> string -> t -> t
176176

jscomp/core/js_of_lam_block.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ let field (field_info : Lam_compat.field_dbg_info) e (i : int32) =
3838
e i
3939
| Fld_poly_var_content -> E.poly_var_value_access e
4040
| Fld_poly_var_tag -> E.poly_var_tag_access e
41-
| Fld_record_extension { name } -> E.extension_access e (Some name) i
42-
| Fld_extension -> E.extension_access e None i
41+
| Fld_record_extension { name } -> E.extension_access e ~name i
42+
| Fld_extension -> E.extension_access e i
4343
| Fld_variant -> E.variant_access e i
4444
| Fld_cons -> E.cons_access e i
4545
| Fld_record_inline { name } -> E.inline_record_access e name i

jscomp/core/lam_convert.ml

+9-6
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,17 @@
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-
let caml_id_field_info : Lambda.field_dbg_info =
26-
Fld_record { name = Literals.exception_id; mutable_flag = Immutable }
27-
28-
let lam_caml_id : Lam_primitive.t = Pfield (0, caml_id_field_info)
2925
let prim = Lam.prim
3026

31-
let lam_extension_id loc (head : Lam.t) =
32-
prim ~primitive:lam_caml_id ~args:[ head ] loc
27+
let lam_extension_id =
28+
let lam_caml_id : Lam_primitive.t =
29+
let caml_id_field_info : Lambda.field_dbg_info =
30+
Fld_record { name = Js_dump_lit.exception_id; mutable_flag = Immutable }
31+
in
32+
Pfield (0, caml_id_field_info)
33+
in
34+
fun loc (head : Lam.t) ->
35+
prim ~primitive:lam_caml_id ~args:[ head ] loc
3336

3437
let lazy_block_info : Lam_tag_info.t =
3538
Blk_record

jscomp/runtime/caml_format.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ let float_of_string : string -> exn -> float =
659659
return Infinity;
660660
if (/^-inf(inity)?$/i.test(s))
661661
return -Infinity;
662-
throw exn;
662+
throw new Error(exn.RE_EXN_ID, { cause: exn });;
663663
}
664664
|}]
665665

jscomp/runtime/caml_js_exceptions.res

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
2424

2525
exception Error = JsError
26-
26+
type js_error = { cause : exn }
2727
/**
2828
This function has to be in this module Since
2929
[Error] is defined here
3030
*/
3131
let internalToOCamlException = (e: unknown) =>
32-
if Caml_exceptions.is_extension(e) {
33-
(Obj.magic(e): exn)
32+
if Caml_exceptions.is_extension(((Obj.magic(e): js_error).cause)) {
33+
(Obj.magic(e): js_error).cause
3434
} else {
3535
JsError(e)
3636
}

jscomp/runtime/caml_lexer.res

+2-2
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ let caml_lex_engine_aux: (
160160
if (state < 0) {
161161
lexbuf.lex_curr_pos = lexbuf.lex_last_pos;
162162
if (lexbuf.lex_last_action == -1)
163-
throw exn
163+
throw new Error(exn.RE_EXN_ID, { cause: exn })
164164
else
165165
return lexbuf.lex_last_action;
166166
}
@@ -308,7 +308,7 @@ let caml_new_lex_engine_aux: (
308308
if (state < 0) {
309309
lexbuf.lex_curr_pos = lexbuf.lex_last_pos;
310310
if (lexbuf.lex_last_action == -1)
311-
throw exn;
311+
throw new Error(exn.RE_EXN_ID, { cause: exn });
312312
else
313313
return lexbuf.lex_last_action;
314314
}

jscomp/test/406_primitive_test.js

+9-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jscomp/test/UncurriedExternals.js

+10-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jscomp/test/adt_optimize_test.js

+10-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jscomp/test/argv_test.js

+20-18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jscomp/test/arith_parser.js

+12-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)