1996-11-08 06:48:17 -08:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
|
|
|
/* Objective Caml */
|
|
|
|
/* */
|
|
|
|
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
|
|
|
|
/* */
|
|
|
|
/* 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 */
|
|
|
|
/* under the terms of the GNU Library General Public License. */
|
1996-11-08 06:48:17 -08:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
/* Callbacks from C to Caml */
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include "callback.h"
|
1999-02-14 08:48:25 -08:00
|
|
|
#include "fail.h"
|
1996-11-08 06:48:17 -08:00
|
|
|
#include "memory.h"
|
|
|
|
#include "mlvalues.h"
|
|
|
|
|
|
|
|
#ifndef NATIVE_CODE
|
|
|
|
|
1999-01-15 08:58:55 -08:00
|
|
|
/* Bytecode callbacks */
|
|
|
|
|
1996-11-08 06:48:17 -08:00
|
|
|
#include "interp.h"
|
|
|
|
#include "instruct.h"
|
1997-02-02 06:53:44 -08:00
|
|
|
#include "fix_code.h"
|
1996-11-08 06:48:17 -08:00
|
|
|
#include "stacks.h"
|
|
|
|
|
|
|
|
int callback_depth = 0;
|
|
|
|
|
1999-01-15 08:58:55 -08:00
|
|
|
static opcode_t callback_code[] = { ACC, 0, APPLY, 0, POP, 1, STOP };
|
1996-11-08 06:48:17 -08:00
|
|
|
|
1997-02-02 06:53:44 -08:00
|
|
|
#ifdef THREADED_CODE
|
|
|
|
|
|
|
|
static int callback_code_threaded = 0;
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static void thread_callback(void)
|
1997-02-02 06:53:44 -08:00
|
|
|
{
|
1999-01-15 08:58:55 -08:00
|
|
|
thread_code(callback_code, sizeof(callback_code));
|
1997-02-02 06:53:44 -08:00
|
|
|
callback_code_threaded = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define Init_callback() if (!callback_code_threaded) thread_callback()
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define Init_callback()
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport value callbackN_exn(value closure, int narg, value args[])
|
1996-11-08 06:48:17 -08:00
|
|
|
{
|
1999-01-15 08:58:55 -08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
Assert(narg + 4 <= 256);
|
1997-02-02 06:53:44 -08:00
|
|
|
Init_callback();
|
1999-01-15 08:58:55 -08:00
|
|
|
extern_sp -= narg + 4;
|
|
|
|
for (i = 0; i < narg; i++) extern_sp[i] = args[i]; /* arguments */
|
|
|
|
extern_sp[narg] = (value) (callback_code + 4); /* return address */
|
|
|
|
extern_sp[narg + 1] = Val_unit; /* environment */
|
|
|
|
extern_sp[narg + 2] = Val_long(0); /* extra args */
|
|
|
|
extern_sp[narg + 3] = closure;
|
|
|
|
callback_code[1] = narg + 3;
|
|
|
|
callback_code[3] = narg;
|
1999-02-14 08:48:25 -08:00
|
|
|
return interprete(callback_code, sizeof(callback_code));
|
1996-11-08 06:48:17 -08:00
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport value callback_exn(value closure, value arg1)
|
1999-01-15 08:58:55 -08:00
|
|
|
{
|
|
|
|
value arg[1];
|
|
|
|
arg[0] = arg1;
|
1999-06-05 05:02:48 -07:00
|
|
|
return callbackN_exn(closure, 1, arg);
|
1999-01-15 08:58:55 -08:00
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport value callback2_exn(value closure, value arg1, value arg2)
|
1996-11-08 06:48:17 -08:00
|
|
|
{
|
1999-01-15 08:58:55 -08:00
|
|
|
value arg[2];
|
|
|
|
arg[0] = arg1;
|
|
|
|
arg[1] = arg2;
|
1999-06-05 05:02:48 -07:00
|
|
|
return callbackN_exn(closure, 2, arg);
|
1996-11-08 06:48:17 -08:00
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport value callback3_exn(value closure,
|
|
|
|
value arg1, value arg2, value arg3)
|
1999-01-15 08:58:55 -08:00
|
|
|
{
|
|
|
|
value arg[3];
|
|
|
|
arg[0] = arg1;
|
|
|
|
arg[1] = arg2;
|
|
|
|
arg[2] = arg3;
|
1999-06-05 05:02:48 -07:00
|
|
|
return callbackN_exn(closure, 3, arg);
|
1999-01-15 08:58:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2001-05-28 08:34:49 -07:00
|
|
|
/* Native-code callbacks. callback[123]_exn are implemented in asm. */
|
1999-01-15 08:58:55 -08:00
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport value callbackN_exn(value closure, int narg, value args[])
|
1996-11-08 06:48:17 -08:00
|
|
|
{
|
1999-11-29 11:03:05 -08:00
|
|
|
CAMLparam1 (closure);
|
1999-11-29 11:48:02 -08:00
|
|
|
CAMLxparamN (args, narg);
|
1999-11-29 11:03:05 -08:00
|
|
|
CAMLlocal1 (res);
|
1999-01-15 08:58:55 -08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
res = closure;
|
1999-11-29 11:03:05 -08:00
|
|
|
for (i = 0; i < narg; /*nothing*/) {
|
|
|
|
/* Pass as many arguments as possible */
|
|
|
|
switch (narg - i) {
|
|
|
|
case 1:
|
|
|
|
res = callback_exn(res, args[i]);
|
|
|
|
if (Is_exception_result(res)) CAMLreturn (res);
|
|
|
|
i += 1;
|
|
|
|
break;
|
|
|
|
case 2:
|
2001-05-28 08:34:49 -07:00
|
|
|
res = callback2_exn(res, args[i], args[i + 1]);
|
1999-11-29 11:03:05 -08:00
|
|
|
if (Is_exception_result(res)) CAMLreturn (res);
|
|
|
|
i += 2;
|
|
|
|
break;
|
|
|
|
default:
|
2001-05-28 08:34:49 -07:00
|
|
|
res = callback3_exn(res, args[i], args[i + 1], args[i + 2]);
|
1999-11-29 11:03:05 -08:00
|
|
|
if (Is_exception_result(res)) CAMLreturn (res);
|
|
|
|
i += 3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CAMLreturn (res);
|
1996-11-08 06:48:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
1999-02-14 08:48:25 -08:00
|
|
|
/* Exception-propagating variants of the above */
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport value callback (value closure, value arg)
|
1999-02-14 08:48:25 -08:00
|
|
|
{
|
|
|
|
value res = callback_exn(closure, arg);
|
|
|
|
if (Is_exception_result(res)) mlraise(Extract_exception(res));
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport value callback2 (value closure, value arg1, value arg2)
|
1999-02-14 08:48:25 -08:00
|
|
|
{
|
|
|
|
value res = callback2_exn(closure, arg1, arg2);
|
|
|
|
if (Is_exception_result(res)) mlraise(Extract_exception(res));
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport value callback3 (value closure, value arg1, value arg2, value arg3)
|
1999-02-14 08:48:25 -08:00
|
|
|
{
|
|
|
|
value res = callback3_exn(closure, arg1, arg2, arg3);
|
|
|
|
if (Is_exception_result(res)) mlraise(Extract_exception(res));
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport value callbackN (value closure, int narg, value args[])
|
1999-02-14 08:48:25 -08:00
|
|
|
{
|
|
|
|
value res = callbackN_exn(closure, narg, args);
|
|
|
|
if (Is_exception_result(res)) mlraise(Extract_exception(res));
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1996-11-08 06:48:17 -08:00
|
|
|
/* Naming of Caml values */
|
|
|
|
|
|
|
|
struct named_value {
|
|
|
|
value val;
|
|
|
|
struct named_value * next;
|
|
|
|
char name[1];
|
|
|
|
};
|
|
|
|
|
|
|
|
#define Named_value_size 13
|
|
|
|
|
|
|
|
static struct named_value * named_value_table[Named_value_size] = { NULL, };
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static unsigned int hash_value_name(char *name)
|
1996-11-08 06:48:17 -08:00
|
|
|
{
|
|
|
|
unsigned int h;
|
|
|
|
for (h = 0; *name != 0; name++) h = h * 19 + *name;
|
|
|
|
return h % Named_value_size;
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value register_named_value(value vname, value val)
|
1996-11-08 06:48:17 -08:00
|
|
|
{
|
|
|
|
struct named_value * nv;
|
|
|
|
char * name = String_val(vname);
|
|
|
|
unsigned int h = hash_value_name(name);
|
|
|
|
|
|
|
|
nv = (struct named_value *)
|
|
|
|
stat_alloc(sizeof(struct named_value) + strlen(name));
|
|
|
|
strcpy(nv->name, name);
|
|
|
|
nv->val = val;
|
|
|
|
nv->next = named_value_table[h];
|
|
|
|
named_value_table[h] = nv;
|
|
|
|
register_global_root(&nv->val);
|
|
|
|
return Val_unit;
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLexport value * caml_named_value(char *name)
|
1996-11-08 06:48:17 -08:00
|
|
|
{
|
|
|
|
struct named_value * nv;
|
|
|
|
for (nv = named_value_table[hash_value_name(name)];
|
|
|
|
nv != NULL;
|
|
|
|
nv = nv->next) {
|
|
|
|
if (strcmp(name, nv->name) == 0) return &nv->val;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|