1995-08-09 08:06:35 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Objective Caml */
|
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
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
1995-07-10 02:48:27 -07:00
|
|
|
/* Raising exceptions from C. */
|
|
|
|
|
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"
|
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,
|
|
|
|
caml_exn_Stack_overflow;
|
|
|
|
extern caml_generated_constant
|
|
|
|
caml_bucket_Out_of_memory,
|
|
|
|
caml_bucket_Stack_overflow;
|
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
|
2004-01-01 08:42:43 -08:00
|
|
|
while (caml_local_roots != NULL &&
|
|
|
|
(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
|
|
|
{
|
2004-01-03 04:51:20 -08:00
|
|
|
CAMLparam1 (tag);
|
|
|
|
CAMLlocal1 (bucket);
|
|
|
|
|
|
|
|
bucket = caml_alloc_small (1, 0);
|
|
|
|
Field(bucket, 0) = tag;
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
void caml_raise_with_string(value tag, char *msg)
|
1995-07-10 02:48:27 -07:00
|
|
|
{
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_raise_with_arg(tag, caml_copy_string(msg));
|
1995-07-10 02:48:27 -07:00
|
|
|
}
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
void caml_failwith (char *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
|
|
|
}
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
void caml_invalid_argument (char *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
|
|
|
/* To raise [Out_of_memory], we can't use [caml_raise_constant],
|
1995-07-10 02:48:27 -07:00
|
|
|
because it allocates and we're out of memory...
|
2003-11-21 07:54:03 -08:00
|
|
|
We therefore use a statically-allocated bucket constructed
|
|
|
|
by the ocamlopt linker.
|
2004-01-01 08:42:43 -08:00
|
|
|
This works OK because the exception value for [Out_of_memory] is also
|
2001-08-08 01:30:26 -07:00
|
|
|
statically allocated out of the heap.
|
|
|
|
The same applies to Stack_overflow. */
|
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
|
|
|
{
|
2004-01-03 04:51:20 -08:00
|
|
|
caml_raise((value) &caml_bucket_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
|
|
|
{
|
2004-01-03 04:51:20 -08:00
|
|
|
caml_raise((value) &caml_bucket_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
|
|
|
}
|
|
|
|
|
1998-11-11 07:35:48 -08:00
|
|
|
/* We allocate statically the bucket for the exception because we can't
|
|
|
|
do a GC before the exception is raised (lack of stack descriptors
|
2004-01-02 11:23:29 -08:00
|
|
|
for the ccall to [caml_array_bound_error]. */
|
1998-11-11 07:35:48 -08:00
|
|
|
|
2003-11-21 07:54:03 -08:00
|
|
|
#define BOUND_MSG "index out of bounds"
|
1998-11-11 07:35:48 -08:00
|
|
|
#define BOUND_MSG_LEN (sizeof(BOUND_MSG) - 1)
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
header_t hdr;
|
|
|
|
value exn;
|
|
|
|
value arg;
|
|
|
|
} array_bound_error_bucket;
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
header_t hdr;
|
|
|
|
char data[BOUND_MSG_LEN + sizeof(value)];
|
|
|
|
} array_bound_error_msg = { 0, BOUND_MSG };
|
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
void caml_array_bound_error(void)
|
1998-11-11 07:35:48 -08:00
|
|
|
{
|
|
|
|
mlsize_t wosize = (BOUND_MSG_LEN + sizeof(value)) / sizeof(value);
|
|
|
|
mlsize_t offset_index = Bsize_wsize(wosize) - 1;
|
2000-01-03 08:15:14 -08:00
|
|
|
array_bound_error_msg.hdr = Make_header(wosize, String_tag, Caml_white);
|
1998-11-11 07:35:48 -08:00
|
|
|
array_bound_error_msg.data[offset_index] = offset_index - BOUND_MSG_LEN;
|
2000-01-03 08:15:14 -08:00
|
|
|
array_bound_error_bucket.hdr = Make_header(2, 0, Caml_white);
|
2004-01-04 06:32:34 -08:00
|
|
|
array_bound_error_bucket.exn = (value) caml_exn_Invalid_argument;
|
1998-11-11 07:35:48 -08:00
|
|
|
array_bound_error_bucket.arg = (value) array_bound_error_msg.data;
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_raise((value) &array_bound_error_bucket.exn);
|
1998-11-11 07:35:48 -08:00
|
|
|
}
|