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-05-04 03:15:53 -07:00
|
|
|
/* Raising exceptions from C. */
|
|
|
|
|
2011-07-20 02:17:07 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
1995-05-04 03:15:53 -07:00
|
|
|
#include "alloc.h"
|
|
|
|
#include "fail.h"
|
1997-08-29 08:37:22 -07:00
|
|
|
#include "io.h"
|
1995-05-04 03:15:53 -07:00
|
|
|
#include "gc.h"
|
|
|
|
#include "memory.h"
|
1998-10-01 05:32:20 -07:00
|
|
|
#include "misc.h"
|
1995-05-04 03:15:53 -07:00
|
|
|
#include "mlvalues.h"
|
2001-06-15 07:22:38 -07:00
|
|
|
#include "printexc.h"
|
1995-05-04 03:15:53 -07:00
|
|
|
#include "signals.h"
|
|
|
|
#include "stacks.h"
|
|
|
|
|
2004-01-05 12:26:19 -08:00
|
|
|
CAMLexport struct longjmp_buffer * caml_external_raise = NULL;
|
2004-01-01 08:42:43 -08:00
|
|
|
value caml_exn_bucket;
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
CAMLexport void caml_raise(value v)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
1997-08-29 08:37:22 -07:00
|
|
|
Unlock_exn();
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_exn_bucket = v;
|
|
|
|
if (caml_external_raise == NULL) caml_fatal_uncaught_exception(v);
|
|
|
|
siglongjmp(caml_external_raise->buf, 1);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
CAMLexport void caml_raise_constant(value tag)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
1999-11-29 11:03:05 -08:00
|
|
|
CAMLparam1 (tag);
|
|
|
|
CAMLlocal1 (bucket);
|
|
|
|
|
2003-12-29 14:15:02 -08:00
|
|
|
bucket = caml_alloc_small (1, 0);
|
1999-11-29 11:03:05 -08:00
|
|
|
Field(bucket, 0) = tag;
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_raise(bucket);
|
2004-05-17 10:10:00 -07:00
|
|
|
CAMLnoreturn;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
CAMLexport void caml_raise_with_arg(value tag, value arg)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
1999-11-29 11:03:05 -08:00
|
|
|
CAMLparam2 (tag, arg);
|
|
|
|
CAMLlocal1 (bucket);
|
|
|
|
|
2003-12-29 14:15:02 -08:00
|
|
|
bucket = caml_alloc_small (2, 0);
|
1999-11-29 11:03:05 -08:00
|
|
|
Field(bucket, 0) = tag;
|
|
|
|
Field(bucket, 1) = arg;
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_raise(bucket);
|
2008-09-18 04:23:28 -07:00
|
|
|
CAMLnoreturn;
|
|
|
|
}
|
|
|
|
|
|
|
|
CAMLexport 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);
|
2004-05-17 10:10:00 -07:00
|
|
|
CAMLnoreturn;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2006-11-24 06:40:11 -08:00
|
|
|
CAMLexport void caml_raise_with_string(value tag, char const *msg)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
1999-11-29 11:03:05 -08:00
|
|
|
CAMLparam1 (tag);
|
|
|
|
CAMLlocal1 (vmsg);
|
|
|
|
|
2003-12-29 14:15:02 -08:00
|
|
|
vmsg = caml_copy_string(msg);
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_raise_with_arg(tag, vmsg);
|
2004-05-17 10:10:00 -07:00
|
|
|
CAMLnoreturn;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2011-07-20 02:17:07 -07:00
|
|
|
/* PR#5115: Failure and Invalid_argument can be triggered by
|
|
|
|
input_value while reading the initial value of [caml_global_data]. */
|
|
|
|
|
2006-11-24 06:40:11 -08:00
|
|
|
CAMLexport void caml_failwith (char const *msg)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2011-07-20 02:17:07 -07:00
|
|
|
if (caml_global_data == 0) {
|
|
|
|
fprintf(stderr, "Fatal error: exception Failure(\"%s\")\n", msg);
|
|
|
|
exit(2);
|
|
|
|
}
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_raise_with_string(Field(caml_global_data, FAILURE_EXN), msg);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2006-11-24 06:40:11 -08:00
|
|
|
CAMLexport void caml_invalid_argument (char const *msg)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2011-07-20 02:17:07 -07:00
|
|
|
if (caml_global_data == 0) {
|
|
|
|
fprintf(stderr, "Fatal error: exception Invalid_argument(\"%s\")\n", msg);
|
|
|
|
exit(2);
|
|
|
|
}
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_raise_with_string(Field(caml_global_data, INVALID_EXN), msg);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
CAMLexport void caml_array_bound_error(void)
|
2003-11-21 07:56:33 -08:00
|
|
|
{
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_invalid_argument("index out of bounds");
|
2003-11-21 07:56:33 -08:00
|
|
|
}
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
/* Problem: we can't use [caml_raise_constant], because it allocates and
|
1998-10-01 05:32:20 -07:00
|
|
|
we're out of memory... Here, we allocate statically the exn bucket
|
2004-01-01 08:42:43 -08:00
|
|
|
for [Out_of_memory]. */
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
static struct {
|
|
|
|
header_t hdr;
|
|
|
|
value exn;
|
1998-10-01 05:32:20 -07:00
|
|
|
} out_of_memory_bucket = { 0, 0 };
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
CAMLexport void caml_raise_out_of_memory(void)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2006-11-24 06:40:11 -08:00
|
|
|
if (out_of_memory_bucket.exn == 0)
|
2003-12-29 14:15:02 -08:00
|
|
|
caml_fatal_error
|
|
|
|
("Fatal error: out of memory while raising Out_of_memory\n");
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_raise((value) &(out_of_memory_bucket.exn));
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
CAMLexport void caml_raise_stack_overflow(void)
|
1997-05-15 06:26:08 -07:00
|
|
|
{
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_raise_constant(Field(caml_global_data, STACK_OVERFLOW_EXN));
|
1997-05-15 06:26:08 -07:00
|
|
|
}
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
CAMLexport void caml_raise_sys_error(value msg)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_raise_with_arg(Field(caml_global_data, SYS_ERROR_EXN), msg);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
CAMLexport void caml_raise_end_of_file(void)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_raise_constant(Field(caml_global_data, END_OF_FILE_EXN));
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
CAMLexport void caml_raise_zero_divide(void)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_raise_constant(Field(caml_global_data, ZERO_DIVIDE_EXN));
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
CAMLexport void caml_raise_not_found(void)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_raise_constant(Field(caml_global_data, NOT_FOUND_EXN));
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
CAMLexport void caml_raise_sys_blocked_io(void)
|
1998-11-20 07:36:27 -08:00
|
|
|
{
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_raise_constant(Field(caml_global_data, SYS_BLOCKED_IO));
|
1998-11-20 07:36:27 -08:00
|
|
|
}
|
|
|
|
|
1998-10-01 05:32:20 -07:00
|
|
|
/* Initialization of statically-allocated exception buckets */
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
void caml_init_exceptions(void)
|
1998-10-01 05:32:20 -07:00
|
|
|
{
|
2000-01-02 08:10:00 -08:00
|
|
|
out_of_memory_bucket.hdr = Make_header(1, 0, Caml_white);
|
2003-12-31 06:20:40 -08:00
|
|
|
out_of_memory_bucket.exn = Field(caml_global_data, OUT_OF_MEMORY_EXN);
|
2004-01-02 11:23:29 -08:00
|
|
|
caml_register_global_root(&out_of_memory_bucket.exn);
|
1998-10-01 05:32:20 -07:00
|
|
|
}
|
2011-09-08 01:34:43 -07:00
|
|
|
|
|
|
|
int caml_is_special_exception(value exn) {
|
|
|
|
return exn == Field(caml_global_data, MATCH_FAILURE_EXN)
|
|
|
|
|| exn == Field(caml_global_data, ASSERT_FAILURE_EXN)
|
|
|
|
|| exn == Field(caml_global_data, UNDEFINED_RECURSIVE_MODULE_EXN);
|
|
|
|
}
|