Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw as instance of JS new Error() #6611

Merged
merged 7 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

- 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
- Add `rewatch` to the npm package as an alternative build tool. https://github.com/rescript-lang/rescript-compiler/pull/6762
- 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

#### :boom: Breaking Change

Expand Down
40 changes: 30 additions & 10 deletions jscomp/core/js_dump.ml
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,11 @@ type cxt = Ext_pp_scope.t
let semi f = P.string f L.semi
let comma f = P.string f L.comma

let new_error name cause =
E.new_ (E.js_global Js_dump_lit.error) [ name; cause ]

let exn_block_as_obj ~(stack : bool) (el : J.expression list) (ext : J.tag_info)
: J.expression_desc =
: J.expression =
let field_name =
match ext with
| Blk_extension -> (
Expand All @@ -108,12 +111,29 @@ let exn_block_as_obj ~(stack : bool) (el : J.expression list) (ext : J.tag_info)
fun i -> match i with 0 -> Literals.exception_id | i -> ss.(i - 1))
| _ -> assert false
in
Object
(if stack then
Ext_list.mapi_append el
(fun i e -> (Js_op.Lit (field_name i), e))
[ (Js_op.Lit "Error", E.new_ (E.js_global "Error") []) ]
else Ext_list.mapi el (fun i e -> (Js_op.Lit (field_name i), e)))
let cause =
{
J.expression_desc =
Object (List.mapi (fun i e -> (Js_op.Lit (field_name i), e)) el);
comment = None;
}
in
if stack then
new_error (List.hd el)
{
J.expression_desc = Object [ (Lit Js_dump_lit.cause, cause) ];
comment = None;
}
else cause

let exn_ref_as_obj e : J.expression =
let cause = { J.expression_desc = e; comment = None; } in
new_error
(E.record_access cause Js_dump_lit.exception_id 0l)
{
J.expression_desc = Object [ (Lit Js_dump_lit.cause, cause) ];
comment = None;
}

let rec iter_lst cxt (f : P.t) ls element inter =
match ls with
Expand Down Expand Up @@ -730,7 +750,7 @@ and expression_desc cxt ~(level : int) f x : cxt =
])
| _ -> assert false)
| Caml_block (el, _, _, ((Blk_extension | Blk_record_ext _) as ext)) ->
expression_desc cxt ~level f (exn_block_as_obj ~stack:false el ext)
expression cxt ~level f (exn_block_as_obj ~stack:false el ext)
| Caml_block (el, _, tag, Blk_record_inlined p) ->
let untagged = Ast_untagged_variants.process_untagged p.attrs in
let objs =
Expand Down Expand Up @@ -1184,8 +1204,8 @@ and statement_desc top cxt f (s : J.statement_desc) : cxt =
let e =
match e.expression_desc with
| Caml_block (el, _, _, ((Blk_extension | Blk_record_ext _) as ext)) ->
{ e with expression_desc = exn_block_as_obj ~stack:true el ext }
| _ -> e
{ e with expression_desc = (exn_block_as_obj ~stack:true el ext).expression_desc }
| exp -> { e with expression_desc = (exn_ref_as_obj exp).expression_desc }
in
P.string f L.throw;
P.space f;
Expand Down
6 changes: 6 additions & 0 deletions jscomp/core/js_dump_lit.ml
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,9 @@ let block_variant = "variant"
let block_simple_variant = "simpleVariant"

let case = "case"

let cause = "cause"

let error = "Error"

let exception_id = "RE_EXN_ID"
2 changes: 1 addition & 1 deletion jscomp/core/js_exp_make.ml
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ let poly_var_value_access (e : t) =
comment = None;
}

let extension_access (e : t) name (pos : int32) : t =
let extension_access (e : t) ?name (pos : int32) : t =
match e.expression_desc with
| Array (l, _) (* Float i -- should not appear here *)
| Caml_block (l, _, _, _)
Expand Down
2 changes: 1 addition & 1 deletion jscomp/core/js_exp_make.mli
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ val variant_access : t -> int32 -> t

val cons_access : t -> int32 -> t

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

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

Expand Down
4 changes: 2 additions & 2 deletions jscomp/core/js_of_lam_block.ml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ let field (field_info : Lam_compat.field_dbg_info) e (i : int32) =
e i
| Fld_poly_var_content -> E.poly_var_value_access e
| Fld_poly_var_tag -> E.poly_var_tag_access e
| Fld_record_extension { name } -> E.extension_access e (Some name) i
| Fld_extension -> E.extension_access e None i
| Fld_record_extension { name } -> E.extension_access e ~name i
| Fld_extension -> E.extension_access e i
| Fld_variant -> E.variant_access e i
| Fld_cons -> E.cons_access e i
| Fld_record_inline { name } -> E.inline_record_access e name i
Expand Down
15 changes: 9 additions & 6 deletions jscomp/core/lam_convert.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)

let caml_id_field_info : Lambda.field_dbg_info =
Fld_record { name = Literals.exception_id; mutable_flag = Immutable }

let lam_caml_id : Lam_primitive.t = Pfield (0, caml_id_field_info)
let prim = Lam.prim

let lam_extension_id loc (head : Lam.t) =
prim ~primitive:lam_caml_id ~args:[ head ] loc
let lam_extension_id =
let lam_caml_id : Lam_primitive.t =
let caml_id_field_info : Lambda.field_dbg_info =
Fld_record { name = Js_dump_lit.exception_id; mutable_flag = Immutable }
in
Pfield (0, caml_id_field_info)
in
fun loc (head : Lam.t) ->
prim ~primitive:lam_caml_id ~args:[ head ] loc

let lazy_block_info : Lam_tag_info.t =
Blk_record
Expand Down
2 changes: 1 addition & 1 deletion jscomp/runtime/caml_format.ml
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ let float_of_string : string -> exn -> float =
return Infinity;
if (/^-inf(inity)?$/i.test(s))
return -Infinity;
throw exn;
throw new Error(exn.RE_EXN_ID, { cause: exn });;
}
|}]

Expand Down
6 changes: 3 additions & 3 deletions jscomp/runtime/caml_js_exceptions.res
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

exception Error = JsError

type js_error = { cause : exn }
/**
This function has to be in this module Since
[Error] is defined here
*/
let internalToOCamlException = (e: unknown) =>
if Caml_exceptions.is_extension(e) {
(Obj.magic(e): exn)
if Caml_exceptions.is_extension(((Obj.magic(e): js_error).cause)) {
(Obj.magic(e): js_error).cause
} else {
JsError(e)
}
Expand Down
4 changes: 2 additions & 2 deletions jscomp/runtime/caml_lexer.res
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ let caml_lex_engine_aux: (
if (state < 0) {
lexbuf.lex_curr_pos = lexbuf.lex_last_pos;
if (lexbuf.lex_last_action == -1)
throw exn
throw new Error(exn.RE_EXN_ID, { cause: exn })
else
return lexbuf.lex_last_action;
}
Expand Down Expand Up @@ -308,7 +308,7 @@ let caml_new_lex_engine_aux: (
if (state < 0) {
lexbuf.lex_curr_pos = lexbuf.lex_last_pos;
if (lexbuf.lex_last_action == -1)
throw exn;
throw new Error(exn.RE_EXN_ID, { cause: exn });
else
return lexbuf.lex_last_action;
}
Expand Down
15 changes: 9 additions & 6 deletions jscomp/test/406_primitive_test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 10 additions & 8 deletions jscomp/test/UncurriedExternals.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 10 additions & 9 deletions jscomp/test/adt_optimize_test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 20 additions & 18 deletions jscomp/test/argv_test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 12 additions & 10 deletions jscomp/test/arith_parser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading