1995-08-09 08:06:35 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
2011-07-27 07:17:02 -07:00
|
|
|
/* OCaml */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Copyright 1996 Institut National de Recherche en Informatique et */
|
1999-11-17 10:59:06 -08:00
|
|
|
/* en Automatique. All rights reserved. This file is distributed */
|
2001-12-07 05:41:02 -08:00
|
|
|
/* under the terms of the GNU Library General Public License, with */
|
|
|
|
/* the special exception on linking described in file ../LICENSE. */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
1995-07-10 02:48:27 -07:00
|
|
|
/* Raising exceptions from C. */
|
|
|
|
|
2013-11-08 08:18:21 -08:00
|
|
|
#include <stdio.h>
|
1996-02-01 07:02:04 -08:00
|
|
|
#include <signal.h>
|
1995-07-10 02:48:27 -07:00
|
|
|
#include "alloc.h"
|
|
|
|
#include "fail.h"
|
1997-08-29 08:37:22 -07:00
|
|
|
#include "io.h"
|
1995-07-10 02:48:27 -07:00
|
|
|
#include "gc.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "mlvalues.h"
|
2001-06-15 07:22:49 -07:00
|
|
|
#include "printexc.h"
|
1995-07-10 02:48:27 -07:00
|
|
|
#include "signals.h"
|
1995-12-20 02:40:34 -08:00
|
|
|
#include "stack.h"
|
1997-11-21 05:46:23 -08:00
|
|
|
#include "roots.h"
|
2013-11-15 07:09:51 -08:00
|
|
|
#include "callback.h"
|
1995-07-10 02:48:27 -07:00
|
|
|
|
|
|
|
/* The globals holding predefined exceptions */
|
|
|
|
|
1998-11-18 10:10:53 -08:00
|
|
|
typedef value caml_generated_constant[1];
|
1995-07-18 02:45:16 -07:00
|
|
|
|
2003-11-21 07:54:03 -08:00
|
|
|
extern caml_generated_constant
|
2004-01-04 06:32:34 -08:00
|
|
|
caml_exn_Out_of_memory,
|
|
|
|
caml_exn_Sys_error,
|
|
|
|
caml_exn_Failure,
|
|
|
|
caml_exn_Invalid_argument,
|
|
|
|
caml_exn_End_of_file,
|
|
|
|
caml_exn_Division_by_zero,
|
|
|
|
caml_exn_Not_found,
|
|
|
|
caml_exn_Match_failure,
|
|
|
|
caml_exn_Sys_blocked_io,
|
2011-09-08 01:34:43 -07:00
|
|
|
caml_exn_Stack_overflow,
|
|
|
|
caml_exn_Assert_failure,
|
2013-11-04 07:17:29 -08:00
|
|
|
caml_exn_Undefined_recursive_module;
|
1995-07-10 02:48:27 -07:00
|
|
|
|
1998-11-02 07:08:40 -08:00
|
|
|
/* Exception raising */
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
extern void caml_raise_exception (value bucket) Noreturn;
|
1998-11-02 07:08:40 -08:00
|
|
|
|
|
|
|
char * caml_exception_pointer = NULL;
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
void caml_raise(value v)
|
1998-11-02 07:08:40 -08:00
|
|
|
{
|
1997-08-29 08:37:22 -07:00
|
|
|
Unlock_exn();
|
2004-01-01 08:42:43 -08:00
|
|
|
if (caml_exception_pointer == NULL) caml_fatal_uncaught_exception(v);
|
1997-05-26 10:16:31 -07:00
|
|
|
|
1995-12-20 02:40:34 -08:00
|
|
|
#ifndef Stack_grows_upwards
|
1997-05-26 10:16:31 -07:00
|
|
|
#define PUSHED_AFTER <
|
1995-12-20 02:40:34 -08:00
|
|
|
#else
|
1997-05-26 10:16:31 -07:00
|
|
|
#define PUSHED_AFTER >
|
1995-12-20 02:40:34 -08:00
|
|
|
#endif
|
2006-11-28 07:45:24 -08:00
|
|
|
while (caml_local_roots != NULL &&
|
2004-01-01 08:42:43 -08:00
|
|
|
(char *) caml_local_roots PUSHED_AFTER caml_exception_pointer) {
|
|
|
|
caml_local_roots = caml_local_roots->next;
|
1997-05-26 10:16:31 -07:00
|
|
|
}
|
|
|
|
#undef PUSHED_AFTER
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_raise_exception(v);
|
1995-07-10 02:48:27 -07:00
|
|
|
}
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
void caml_raise_constant(value tag)
|
1995-07-10 02:48:27 -07:00
|
|
|
{
|
2013-10-18 07:15:27 -07:00
|
|
|
caml_raise(tag);
|
1995-07-10 02:48:27 -07:00
|
|
|
}
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
void caml_raise_with_arg(value tag, value arg)
|
1995-07-10 02:48:27 -07:00
|
|
|
{
|
2004-01-03 04:51:20 -08:00
|
|
|
CAMLparam2 (tag, arg);
|
|
|
|
CAMLlocal1 (bucket);
|
|
|
|
|
|
|
|
bucket = caml_alloc_small (2, 0);
|
|
|
|
Field(bucket, 0) = tag;
|
|
|
|
Field(bucket, 1) = arg;
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_raise(bucket);
|
2004-05-17 10:25:52 -07:00
|
|
|
CAMLnoreturn;
|
1995-07-10 02:48:27 -07:00
|
|
|
}
|
|
|
|
|
2008-09-18 04:23:28 -07:00
|
|
|
void caml_raise_with_args(value tag, int nargs, value args[])
|
|
|
|
{
|
|
|
|
CAMLparam1 (tag);
|
|
|
|
CAMLxparamN (args, nargs);
|
|
|
|
value bucket;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
Assert(1 + nargs <= Max_young_wosize);
|
|
|
|
bucket = caml_alloc_small (1 + nargs, 0);
|
|
|
|
Field(bucket, 0) = tag;
|
|
|
|
for (i = 0; i < nargs; i++) Field(bucket, 1 + i) = args[i];
|
|
|
|
caml_raise(bucket);
|
|
|
|
CAMLnoreturn;
|
|
|
|
}
|
|
|
|
|
2006-11-28 07:45:24 -08:00
|
|
|
void caml_raise_with_string(value tag, char const *msg)
|
1995-07-10 02:48:27 -07:00
|
|
|
{
|
2014-05-09 08:33:02 -07:00
|
|
|
CAMLparam1(tag);
|
|
|
|
value v_msg = caml_copy_string(msg);
|
|
|
|
caml_raise_with_arg(tag, v_msg);
|
|
|
|
CAMLnoreturn;
|
1995-07-10 02:48:27 -07:00
|
|
|
}
|
|
|
|
|
2006-11-28 07:45:24 -08:00
|
|
|
void caml_failwith (char const *msg)
|
1995-07-10 02:48:27 -07:00
|
|
|
{
|
2004-01-04 06:32:34 -08:00
|
|
|
caml_raise_with_string((value) caml_exn_Failure, msg);
|
1995-07-10 02:48:27 -07:00
|
|
|
}
|
|
|
|
|
2006-11-28 07:45:24 -08:00
|
|
|
void caml_invalid_argument (char const *msg)
|
1995-07-10 02:48:27 -07:00
|
|
|
{
|
2004-01-04 06:32:34 -08:00
|
|
|
caml_raise_with_string((value) caml_exn_Invalid_argument, msg);
|
1995-07-10 02:48:27 -07:00
|
|
|
}
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
void caml_raise_out_of_memory(void)
|
1995-07-10 02:48:27 -07:00
|
|
|
{
|
2013-10-18 08:14:20 -07:00
|
|
|
caml_raise_constant((value) caml_exn_Out_of_memory);
|
1995-07-10 02:48:27 -07:00
|
|
|
}
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
void caml_raise_stack_overflow(void)
|
2001-08-08 01:30:26 -07:00
|
|
|
{
|
2013-10-18 08:14:20 -07:00
|
|
|
caml_raise_constant((value) caml_exn_Stack_overflow);
|
2001-08-08 01:30:26 -07:00
|
|
|
}
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
void caml_raise_sys_error(value msg)
|
1995-07-10 02:48:27 -07:00
|
|
|
{
|
2004-01-04 06:32:34 -08:00
|
|
|
caml_raise_with_arg((value) caml_exn_Sys_error, msg);
|
1995-07-10 02:48:27 -07:00
|
|
|
}
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
void caml_raise_end_of_file(void)
|
1995-07-10 02:48:27 -07:00
|
|
|
{
|
2004-01-04 06:32:34 -08:00
|
|
|
caml_raise_constant((value) caml_exn_End_of_file);
|
1995-07-10 02:48:27 -07:00
|
|
|
}
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
void caml_raise_zero_divide(void)
|
1995-07-10 02:48:27 -07:00
|
|
|
{
|
2004-01-04 06:32:34 -08:00
|
|
|
caml_raise_constant((value) caml_exn_Division_by_zero);
|
1995-07-10 02:48:27 -07:00
|
|
|
}
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
void caml_raise_not_found(void)
|
1995-07-10 02:48:27 -07:00
|
|
|
{
|
2004-01-04 06:32:34 -08:00
|
|
|
caml_raise_constant((value) caml_exn_Not_found);
|
1995-07-10 02:48:27 -07:00
|
|
|
}
|
1998-11-11 07:35:48 -08:00
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
void caml_raise_sys_blocked_io(void)
|
1998-11-20 07:38:09 -08:00
|
|
|
{
|
2004-01-04 06:32:34 -08:00
|
|
|
caml_raise_constant((value) caml_exn_Sys_blocked_io);
|
1998-11-20 07:38:09 -08:00
|
|
|
}
|
|
|
|
|
2013-11-08 08:18:21 -08:00
|
|
|
/* We use a pre-allocated exception because we can't
|
1998-11-11 07:35:48 -08:00
|
|
|
do a GC before the exception is raised (lack of stack descriptors
|
2013-11-08 08:18:21 -08:00
|
|
|
for the ccall to [caml_array_bound_error]). */
|
1998-11-11 07:35:48 -08:00
|
|
|
|
2013-11-08 08:18:21 -08:00
|
|
|
static value * caml_array_bound_error_exn = NULL;
|
2008-03-14 06:47:24 -07:00
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
void caml_array_bound_error(void)
|
1998-11-11 07:35:48 -08:00
|
|
|
{
|
2013-11-08 08:18:21 -08:00
|
|
|
if (caml_array_bound_error_exn == NULL) {
|
2014-04-12 03:17:02 -07:00
|
|
|
caml_array_bound_error_exn =
|
|
|
|
caml_named_value("Pervasives.array_bound_error");
|
2013-11-08 08:18:21 -08:00
|
|
|
if (caml_array_bound_error_exn == NULL) {
|
2014-04-12 03:17:02 -07:00
|
|
|
fprintf(stderr, "Fatal error: exception "
|
|
|
|
"Invalid_argument(\"index out of bounds\")\n");
|
2013-11-08 08:18:21 -08:00
|
|
|
exit(2);
|
|
|
|
}
|
2008-03-14 06:47:24 -07:00
|
|
|
}
|
2013-11-08 08:18:21 -08:00
|
|
|
caml_raise(*caml_array_bound_error_exn);
|
1998-11-11 07:35:48 -08:00
|
|
|
}
|
2011-09-08 01:34:43 -07:00
|
|
|
|
|
|
|
int caml_is_special_exception(value exn) {
|
2012-02-05 01:56:23 -08:00
|
|
|
return exn == (value) caml_exn_Match_failure
|
|
|
|
|| exn == (value) caml_exn_Assert_failure
|
|
|
|
|| exn == (value) caml_exn_Undefined_recursive_module;
|
2011-09-08 01:34:43 -07:00
|
|
|
}
|