File: fail.c

package info (click to toggle)
ocaml 5.3.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 43,124 kB
  • sloc: ml: 355,439; ansic: 51,636; sh: 25,098; asm: 5,413; makefile: 3,673; python: 919; javascript: 273; awk: 253; perl: 59; fortran: 21; cs: 9
file content (151 lines) | stat: -rw-r--r-- 4,292 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**************************************************************************/
/*                                                                        */
/*                                 OCaml                                  */
/*                                                                        */
/*             Xavier Leroy, projet Cristal, INRIA Rocquencourt           */
/*                                                                        */
/*   Copyright 1996 Institut National de Recherche en Informatique et     */
/*     en Automatique.                                                    */
/*                                                                        */
/*   All rights reserved.  This file is distributed under the terms of    */
/*   the GNU Lesser General Public License version 2.1, with the          */
/*   special exception on linking described in the file LICENSE.          */
/*                                                                        */
/**************************************************************************/

#define CAML_INTERNALS

/* Raising exceptions from C. */

#include "caml/alloc.h"
#include "caml/fail.h"
#include "caml/memory.h"
#include "caml/mlvalues.h"

#define Assert_is_exn_constructor(v)                                    \
  (CAMLassert(Is_block(v)), CAMLassert(Tag_val(v) == Object_tag))

CAMLexport value caml_exception_constant(value exn_constr)
{
  Assert_is_exn_constructor(exn_constr);
  return exn_constr;
}

CAMLexport value caml_exception_with_arg(value exn_constr, value arg)
{
  CAMLparam2 (exn_constr, arg);
  CAMLlocal1 (bucket);
  Assert_is_exn_constructor(exn_constr);

  bucket = caml_alloc_small (2, 0);
  Field(bucket, 0) = exn_constr;
  Field(bucket, 1) = arg;
  CAMLreturn(bucket);
}

CAMLexport value caml_exception_with_args(value exn_constr,
                                          int nargs, value args[])
{
  CAMLparam1 (exn_constr);
  CAMLxparamN (args, nargs);
  Assert_is_exn_constructor(exn_constr);

  value bucket;

  CAMLassert(1 + nargs <= Max_young_wosize);
  bucket = caml_alloc_small (1 + nargs, 0);
  Field(bucket, 0) = exn_constr;
  for (int i = 0; i < nargs; i++) Field(bucket, 1 + i) = args[i];
  CAMLreturn(bucket);
}

CAMLexport value caml_exception_with_string(value exn_constr, char const *msg)
{
  CAMLparam1(exn_constr);
  value v_msg = caml_copy_string(msg);
  CAMLreturn(caml_exception_with_arg(exn_constr, v_msg));
}


/* Used by the stack overflow handler -> deactivate ASAN (see
   segv_handler in signals_nat.c). */
CAMLno_asan
CAMLexport void caml_raise_constant(value exn_constr)
{
  caml_raise(caml_exception_constant(exn_constr));
}

CAMLexport void caml_raise_with_arg(value exn_constr, value arg)
{
  caml_raise(caml_exception_with_arg(exn_constr, arg));
}

CAMLexport void caml_raise_with_args(value exn_constr, int nargs, value arg[])
{
  caml_raise(caml_exception_with_args(exn_constr, nargs, arg));
}

CAMLexport void caml_raise_with_string(value exn_constr, char const * msg)
{
  caml_raise(caml_exception_with_string(exn_constr, msg));
}

CAMLexport void caml_failwith(char const *msg)
{
  caml_raise(caml_exception_failure(msg));
}

CAMLexport void caml_failwith_value(value msg)
{
  caml_raise(caml_exception_failure_value(msg));
}

CAMLexport void caml_invalid_argument(char const *msg)
{
  caml_raise(caml_exception_invalid_argument(msg));
}

CAMLexport void caml_invalid_argument_value(value msg)
{
  caml_raise(caml_exception_invalid_argument_value(msg));
}

CAMLexport void caml_raise_out_of_memory(void)
{
  caml_raise(caml_exception_out_of_memory());
}

CAMLexport void caml_raise_stack_overflow(void)
{
  caml_raise(caml_exception_stack_overflow());
}

CAMLexport void caml_raise_sys_error(value msg)
{
  caml_raise(caml_exception_sys_error(msg));
}

CAMLexport void caml_raise_end_of_file(void)
{
  caml_raise(caml_exception_end_of_file());
}

CAMLexport void caml_raise_zero_divide(void)
{
  caml_raise(caml_exception_zero_divide());
}

CAMLexport void caml_raise_not_found(void)
{
  caml_raise(caml_exception_not_found());
}

CAMLexport void caml_array_bound_error(void)
{
  caml_raise(caml_exception_array_bound_error());
}

CAMLexport void caml_raise_sys_blocked_io(void)
{
  caml_raise(caml_exception_sys_blocked_io());
}