2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
gbx_extern.c
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
(c) 2000-2012 Benoît Minisini <gambas@users.sourceforge.net>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
MA 02110-1301, USA.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __GBX_EXTERN_C
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "gb_common.h"
|
2008-01-17 22:39:26 +01:00
|
|
|
|
2010-07-26 20:10:51 +02:00
|
|
|
#ifdef HAVE_FFI_COMPONENT
|
2010-07-14 13:55:32 +02:00
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
#include <ffi.h>
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
#include "gb_common_buffer.h"
|
|
|
|
#include "gb_table.h"
|
2011-03-13 03:01:16 +01:00
|
|
|
#include "gb_hash.h"
|
2007-12-30 17:41:49 +01:00
|
|
|
#include "gbx_type.h"
|
|
|
|
#include "gbx_value.h"
|
2010-07-14 03:50:18 +02:00
|
|
|
#include "gbx_class_desc.h"
|
2007-12-30 17:41:49 +01:00
|
|
|
#include "gbx_class.h"
|
|
|
|
#include "gbx_exec.h"
|
|
|
|
#include "gbx_api.h"
|
2010-07-09 13:16:49 +02:00
|
|
|
#include "gbx_c_array.h"
|
|
|
|
#include "gbx_struct.h"
|
2007-12-30 17:41:49 +01:00
|
|
|
#include "gbx_extern.h"
|
|
|
|
|
|
|
|
typedef
|
2010-07-14 13:55:32 +02:00
|
|
|
struct {
|
|
|
|
SYMBOL sym;
|
|
|
|
lt_dlhandle handle;
|
|
|
|
}
|
|
|
|
EXTERN_SYMBOL;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-07-14 13:55:32 +02:00
|
|
|
typedef
|
|
|
|
struct {
|
|
|
|
ffi_cif cif;
|
|
|
|
ffi_type **types;
|
|
|
|
ffi_type *rtype;
|
|
|
|
}
|
|
|
|
EXTERN_CIF;
|
2012-06-15 03:27:31 +02:00
|
|
|
|
2010-07-14 03:50:18 +02:00
|
|
|
typedef
|
|
|
|
struct EXTERN_CALLBACK {
|
|
|
|
struct EXTERN_CALLBACK *next;
|
|
|
|
EXEC_GLOBAL exec;
|
|
|
|
void *closure;
|
2011-03-13 03:01:16 +01:00
|
|
|
void *code;
|
2010-07-14 03:50:18 +02:00
|
|
|
int nparam;
|
|
|
|
TYPE *sign;
|
|
|
|
TYPE ret;
|
2010-07-14 13:55:32 +02:00
|
|
|
EXTERN_CIF info;
|
2010-07-14 03:50:18 +02:00
|
|
|
}
|
|
|
|
EXTERN_CALLBACK;
|
2012-06-15 03:27:31 +02:00
|
|
|
|
2010-07-14 13:55:32 +02:00
|
|
|
typedef
|
|
|
|
struct EXTERN_FUNC {
|
|
|
|
struct EXTERN_FUNC *next;
|
2012-06-15 03:27:31 +02:00
|
|
|
char *alias;
|
2010-07-14 13:55:32 +02:00
|
|
|
void *call;
|
|
|
|
EXTERN_CIF info;
|
|
|
|
}
|
|
|
|
EXTERN_FUNC;
|
2010-07-14 03:50:18 +02:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
static TABLE *_table = NULL;
|
2011-03-13 03:01:16 +01:00
|
|
|
//static EXTERN_CALLBACK *_callbacks = NULL;
|
|
|
|
static HASH_TABLE *_callbacks = NULL;
|
2010-07-14 13:55:32 +02:00
|
|
|
static EXTERN_FUNC *_functions = NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
static ffi_type *_to_ffi_type[17] = {
|
|
|
|
&ffi_type_void, &ffi_type_sint32, &ffi_type_sint32, &ffi_type_sint32,
|
|
|
|
&ffi_type_sint32, &ffi_type_sint64, &ffi_type_float, &ffi_type_double,
|
2010-06-13 17:00:32 +02:00
|
|
|
&ffi_type_void, &ffi_type_pointer, &ffi_type_pointer, &ffi_type_pointer,
|
2008-01-17 22:39:26 +01:00
|
|
|
&ffi_type_void, &ffi_type_void, &ffi_type_void, &ffi_type_pointer,
|
|
|
|
&ffi_type_pointer
|
|
|
|
};
|
2008-05-10 17:17:07 +02:00
|
|
|
|
|
|
|
|
2012-06-01 02:18:38 +02:00
|
|
|
static void prepare_cif(EXTERN_CIF *info, int nsign, TYPE *sign, TYPE ret, int nparam, VALUE *value)
|
2010-07-14 13:55:32 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
TYPE t;
|
2008-05-10 17:17:07 +02:00
|
|
|
|
2010-08-15 16:47:18 +02:00
|
|
|
info->types = NULL;
|
|
|
|
|
2010-07-14 13:55:32 +02:00
|
|
|
if (nparam > 0)
|
|
|
|
{
|
|
|
|
ALLOC(&info->types, sizeof(ffi_type *) * nparam, "prepare_cif");
|
|
|
|
|
|
|
|
for (i = 0; i < nparam; i++)
|
|
|
|
{
|
2012-06-01 02:18:38 +02:00
|
|
|
if (i < nsign)
|
|
|
|
t = sign[i];
|
2010-07-14 13:55:32 +02:00
|
|
|
else
|
2012-06-01 02:18:38 +02:00
|
|
|
t = value[i].type;
|
|
|
|
|
|
|
|
if (TYPE_is_object(t))
|
|
|
|
t = T_OBJECT;
|
2010-07-14 13:55:32 +02:00
|
|
|
|
|
|
|
info->types[i] = _to_ffi_type[t];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TYPE_is_object(ret))
|
|
|
|
t = T_OBJECT;
|
|
|
|
else
|
2012-06-01 02:18:38 +02:00
|
|
|
t = ret;
|
2010-07-14 13:55:32 +02:00
|
|
|
|
|
|
|
info->rtype = _to_ffi_type[t];
|
2008-05-10 17:17:07 +02:00
|
|
|
|
2010-07-14 13:55:32 +02:00
|
|
|
if (ffi_prep_cif(&info->cif, FFI_DEFAULT_ABI, nparam, info->rtype, info->types) != FFI_OK)
|
|
|
|
THROW(E_EXTCB, "Unable to prepare function description");
|
2008-05-10 17:17:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-22 17:07:53 +02:00
|
|
|
static lt_dlhandle get_library(const char *name)
|
2010-07-14 13:55:32 +02:00
|
|
|
{
|
|
|
|
EXTERN_SYMBOL *esym;
|
|
|
|
char *p;
|
|
|
|
int index;
|
|
|
|
|
|
|
|
if (!_table)
|
|
|
|
TABLE_create(&_table, sizeof(EXTERN_SYMBOL), TF_NORMAL);
|
|
|
|
|
|
|
|
TABLE_add_symbol(_table, name, strlen(name), &index);
|
|
|
|
esym = (EXTERN_SYMBOL *)TABLE_get_symbol(_table, index);
|
|
|
|
if (!esym->handle)
|
|
|
|
{
|
|
|
|
/* !!! Must add the suffix !!! */
|
|
|
|
|
|
|
|
p = strrchr(name, ':');
|
|
|
|
if (!p)
|
|
|
|
sprintf(COMMON_buffer, "%s." SHARED_LIBRARY_EXT, name);
|
|
|
|
else
|
|
|
|
sprintf(COMMON_buffer, "%.*s." SHARED_LIBRARY_EXT ".%s", (int)(p - name), name, p + 1);
|
|
|
|
|
|
|
|
name = COMMON_buffer;
|
|
|
|
|
|
|
|
#ifndef DONT_USE_LTDL
|
|
|
|
/* no more available in libltld ?
|
|
|
|
lt_dlopen_flag = RTLD_LAZY;
|
|
|
|
*/
|
|
|
|
esym->handle = lt_dlopenext(name);
|
|
|
|
#else
|
|
|
|
esym->handle = dlopen(name, RTLD_LAZY);
|
|
|
|
#endif
|
2011-03-14 23:10:47 +01:00
|
|
|
|
|
|
|
if (esym->handle == NULL)
|
|
|
|
{
|
|
|
|
name = GB_RealFileName(name, strlen(name));
|
|
|
|
#ifndef DONT_USE_LTDL
|
|
|
|
esym->handle = lt_dlopenext(name);
|
|
|
|
#else
|
|
|
|
esym->handle = dlopen(name, RTLD_LAZY);
|
|
|
|
#endif
|
|
|
|
}
|
2010-07-14 13:55:32 +02:00
|
|
|
|
|
|
|
if (esym->handle == NULL)
|
|
|
|
THROW(E_EXTLIB, name, lt_dlerror());
|
|
|
|
|
|
|
|
//fprintf(stderr, "%s loaded.\n", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return esym->handle;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-05-18 17:02:49 +02:00
|
|
|
void *EXTERN_get_symbol(const char *library, const char *symbol)
|
|
|
|
{
|
|
|
|
lt_dlhandle handle = get_library(library);
|
|
|
|
return lt_dlsym(handle, symbol);
|
|
|
|
}
|
|
|
|
|
2010-07-14 13:55:32 +02:00
|
|
|
static EXTERN_FUNC *get_function(CLASS_EXTERN *ext)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-07-14 13:55:32 +02:00
|
|
|
EXTERN_FUNC *func;
|
|
|
|
void *call;
|
|
|
|
lt_dlhandle handle;
|
|
|
|
|
|
|
|
if (ext->loaded)
|
|
|
|
return (EXTERN_FUNC *)ext->alias;
|
|
|
|
|
|
|
|
handle = get_library(ext->library);
|
|
|
|
call = lt_dlsym(handle, ext->alias);
|
|
|
|
|
|
|
|
if (call == NULL)
|
|
|
|
{
|
|
|
|
lt_dlclose(handle);
|
|
|
|
THROW(E_EXTSYM, ext->library, ext->alias);
|
|
|
|
}
|
|
|
|
|
2012-06-15 03:27:31 +02:00
|
|
|
ALLOC_ZERO(&func, sizeof(EXTERN_FUNC), "get_function");
|
2010-07-14 13:55:32 +02:00
|
|
|
func->next = _functions;
|
2012-06-15 03:27:31 +02:00
|
|
|
func->alias = ext->alias;
|
2010-07-14 13:55:32 +02:00
|
|
|
_functions = func;
|
|
|
|
|
|
|
|
func->call = call;
|
2012-06-01 02:18:38 +02:00
|
|
|
|
|
|
|
if (!ext->vararg)
|
|
|
|
prepare_cif(&func->info, ext->n_param, (TYPE *)ext->param, ext->type, ext->n_param, NULL);
|
2010-07-14 13:55:32 +02:00
|
|
|
|
|
|
|
//ext->library = (char *)handle;
|
|
|
|
ext->alias = (char *)func;
|
|
|
|
ext->loaded = TRUE;
|
|
|
|
|
|
|
|
return func;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
2008-01-17 22:39:26 +01:00
|
|
|
|
2012-06-15 03:27:31 +02:00
|
|
|
EXTERN_FUNC_INFO EXTERN_get_function_info(CLASS_EXTERN *ext)
|
|
|
|
{
|
|
|
|
EXTERN_FUNC *func = get_function(ext);
|
2012-06-19 23:23:49 +02:00
|
|
|
EXTERN_FUNC_INFO func_info = { func->call, func->alias };
|
2012-06-15 03:27:31 +02:00
|
|
|
|
|
|
|
return func_info;
|
|
|
|
}
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
/*
|
2010-07-14 13:55:32 +02:00
|
|
|
EXEC.class : the class
|
|
|
|
EXEC.index : the extern function index
|
|
|
|
EXEC.nparam : the number of parameters to the call
|
|
|
|
EXEC.drop : if the return value should be dropped.
|
2007-12-30 17:41:49 +01:00
|
|
|
*/
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void EXTERN_call(void)
|
|
|
|
{
|
2010-07-14 13:55:32 +02:00
|
|
|
static const void *jump[17] = {
|
|
|
|
&&__VOID, &&__BOOLEAN, &&__BYTE, &&__SHORT, &&__INTEGER, &&__LONG, &&__SINGLE, &&__FLOAT, &&__DATE,
|
|
|
|
&&__STRING, &&__STRING, &&__POINTER, &&__VARIANT, &&__FUNCTION, &&__CLASS, &&__NULL, &&__OBJECT
|
|
|
|
};
|
2010-11-16 02:49:18 +01:00
|
|
|
static const int use_temp[17] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, sizeof(char *), sizeof(char *), 0, 0, 0, 0, 0, sizeof(void *) };
|
2010-09-24 11:32:46 +02:00
|
|
|
static char temp[16 * sizeof(void *)];
|
2010-07-14 13:55:32 +02:00
|
|
|
static void *null = 0;
|
|
|
|
|
|
|
|
CLASS_EXTERN *ext = &EXEC.class->load->ext[EXEC.index];
|
|
|
|
EXTERN_FUNC *func;
|
2012-06-01 02:18:38 +02:00
|
|
|
EXTERN_CIF cif;
|
|
|
|
bool vararg;
|
2010-07-14 13:55:32 +02:00
|
|
|
int nparam = EXEC.nparam;
|
|
|
|
void *args[nparam];
|
|
|
|
TYPE *sign;
|
|
|
|
VALUE *value;
|
|
|
|
char *tmp = NULL;
|
|
|
|
char *next_tmp;
|
|
|
|
int i, t;
|
|
|
|
union {
|
|
|
|
int _integer;
|
|
|
|
float _single;
|
|
|
|
double _float;
|
|
|
|
char *_string;
|
|
|
|
int64_t _long;
|
2010-06-13 14:03:29 +02:00
|
|
|
void *_pointer;
|
2010-07-14 13:55:32 +02:00
|
|
|
}
|
|
|
|
rvalue;
|
|
|
|
|
|
|
|
if (!ext->loaded)
|
|
|
|
{
|
|
|
|
if (nparam < ext->n_param)
|
|
|
|
THROW(E_NEPARAM);
|
2012-06-01 02:18:38 +02:00
|
|
|
if (!ext->vararg && nparam > ext->n_param)
|
2010-07-14 13:55:32 +02:00
|
|
|
THROW(E_TMPARAM);
|
|
|
|
}
|
|
|
|
|
|
|
|
func = get_function(ext);
|
|
|
|
sign = (TYPE *)ext->param;
|
2012-06-01 02:18:38 +02:00
|
|
|
vararg = ext->vararg;
|
2010-07-14 13:55:32 +02:00
|
|
|
value = &SP[-nparam];
|
|
|
|
next_tmp = temp;
|
2012-06-01 02:18:38 +02:00
|
|
|
|
|
|
|
for (i = 0; i < nparam; i++)
|
|
|
|
{
|
|
|
|
if (i < ext->n_param)
|
|
|
|
VALUE_conv(&value[i], sign[i]);
|
|
|
|
else
|
|
|
|
VARIANT_undo(&value[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vararg)
|
|
|
|
prepare_cif(&cif, ext->n_param, (TYPE *)ext->param, ext->type, nparam, value);
|
2010-07-14 13:55:32 +02:00
|
|
|
|
|
|
|
for (i = 0; i < nparam; i++, value++, sign++)
|
|
|
|
{
|
2008-01-17 22:39:26 +01:00
|
|
|
if (TYPE_is_object(value->type))
|
|
|
|
t = T_OBJECT;
|
|
|
|
else
|
|
|
|
t = (int)value->type;
|
|
|
|
|
|
|
|
if (use_temp[t])
|
|
|
|
{
|
|
|
|
tmp = next_tmp;
|
|
|
|
if ((tmp + use_temp[t]) > &temp[sizeof(temp)])
|
|
|
|
THROW(E_TMPARAM);
|
|
|
|
args[i] = tmp;
|
|
|
|
next_tmp = tmp + use_temp[t];
|
|
|
|
}
|
|
|
|
goto *jump[t];
|
|
|
|
|
|
|
|
__BOOLEAN:
|
|
|
|
__BYTE:
|
|
|
|
__SHORT:
|
|
|
|
__INTEGER:
|
|
|
|
args[i] = &value->_integer.value;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
__LONG:
|
|
|
|
args[i] = &value->_long.value;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
__SINGLE:
|
2010-11-16 02:49:18 +01:00
|
|
|
args[i] = &value->_single.value;
|
2008-01-17 22:39:26 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
__FLOAT:
|
|
|
|
args[i] = &value->_float.value;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
__STRING:
|
|
|
|
*((char **)tmp) = (char *)(value->_string.addr + value->_string.start);
|
|
|
|
continue;
|
|
|
|
|
|
|
|
__OBJECT:
|
|
|
|
{
|
|
|
|
void *ob = value->_object.object;
|
2010-07-09 13:16:49 +02:00
|
|
|
void *addr;
|
2012-07-10 21:57:43 +02:00
|
|
|
CLASS *class;
|
|
|
|
|
|
|
|
if (!ob)
|
|
|
|
goto __NULL;
|
|
|
|
|
|
|
|
class = OBJECT_class(ob);
|
2008-01-17 22:39:26 +01:00
|
|
|
|
2012-06-01 02:18:38 +02:00
|
|
|
if (class == CLASS_Class && !CLASS_is_native((CLASS *)ob))
|
2012-06-01 18:41:45 +02:00
|
|
|
addr = ((CLASS *)ob)->stat;
|
2010-07-09 13:16:49 +02:00
|
|
|
else if (CLASS_is_array(class))
|
|
|
|
addr = ((CARRAY *)ob)->data;
|
|
|
|
else if (CLASS_is_struct(class))
|
|
|
|
{
|
|
|
|
if (((CSTRUCT *)ob)->ref)
|
|
|
|
addr = (char *)((CSTATICSTRUCT *)ob)->addr;
|
|
|
|
else
|
|
|
|
addr = (char *)ob + sizeof(CSTRUCT);
|
|
|
|
}
|
2008-01-17 22:39:26 +01:00
|
|
|
else
|
2010-07-09 13:16:49 +02:00
|
|
|
addr = (char *)ob + sizeof(OBJECT);
|
|
|
|
|
|
|
|
*((void **)tmp) = addr;
|
2008-01-17 22:39:26 +01:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
|
2010-05-19 14:43:57 +02:00
|
|
|
__POINTER:
|
|
|
|
args[i] = &value->_pointer.value;
|
|
|
|
continue;
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
__NULL:
|
|
|
|
args[i] = &null;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
__DATE:
|
|
|
|
__VARIANT:
|
|
|
|
__VOID:
|
|
|
|
__CLASS:
|
|
|
|
__FUNCTION:
|
|
|
|
|
2012-06-01 02:18:38 +02:00
|
|
|
THROW(E_UTYPE);
|
2010-07-14 13:55:32 +02:00
|
|
|
}
|
|
|
|
|
2012-06-01 02:18:38 +02:00
|
|
|
if (vararg)
|
|
|
|
{
|
|
|
|
ffi_call(&cif.cif, func->call, &rvalue, args);
|
|
|
|
FREE(&cif.types, "EXTERN_call");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ffi_call(&func->info.cif, func->call, &rvalue, args);
|
|
|
|
|
2010-07-14 13:55:32 +02:00
|
|
|
|
|
|
|
switch (ext->type)
|
|
|
|
{
|
|
|
|
case T_BOOLEAN:
|
|
|
|
case T_BYTE:
|
|
|
|
case T_SHORT:
|
|
|
|
case T_INTEGER:
|
|
|
|
GB_ReturnInteger(rvalue._integer);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_LONG:
|
|
|
|
GB_ReturnLong(rvalue._long);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_SINGLE:
|
2010-11-16 02:49:18 +01:00
|
|
|
GB_ReturnSingle(rvalue._single);
|
2010-07-14 13:55:32 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_FLOAT:
|
|
|
|
GB_ReturnFloat(rvalue._float);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_STRING:
|
2010-09-25 20:18:48 +02:00
|
|
|
GB_ReturnConstZeroString(rvalue._string);
|
2010-07-14 13:55:32 +02:00
|
|
|
break;
|
2010-06-13 14:03:29 +02:00
|
|
|
|
|
|
|
case T_POINTER:
|
|
|
|
GB_ReturnPointer(rvalue._pointer);
|
|
|
|
break;
|
2010-06-23 20:03:04 +02:00
|
|
|
|
|
|
|
case T_VOID:
|
2010-07-14 13:55:32 +02:00
|
|
|
default:
|
2011-11-25 23:26:09 +01:00
|
|
|
|
|
|
|
if (TYPE_is_pure_object(ext->type))
|
|
|
|
{
|
|
|
|
CLASS *class = (CLASS *)(ext->type);
|
|
|
|
if (CLASS_is_struct(class))
|
|
|
|
{
|
|
|
|
GB_ReturnObject(CSTRUCT_create_static(STRUCT_CONST, class, rvalue._pointer));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-23 20:03:04 +02:00
|
|
|
TEMP.type = T_VOID;
|
|
|
|
break;
|
2010-07-14 13:55:32 +02:00
|
|
|
}
|
2008-01-17 22:39:26 +01:00
|
|
|
|
2010-07-14 13:55:32 +02:00
|
|
|
while (nparam)
|
|
|
|
{
|
|
|
|
nparam--;
|
|
|
|
POP();
|
|
|
|
}
|
2008-01-17 22:39:26 +01:00
|
|
|
|
2010-07-14 13:55:32 +02:00
|
|
|
POP(); /* extern function */
|
|
|
|
|
|
|
|
/* from EXEC_native() */
|
|
|
|
|
|
|
|
BORROW(&TEMP);
|
2010-06-18 02:14:53 +02:00
|
|
|
VALUE_conv(&TEMP, ext->type);
|
|
|
|
*SP = TEMP;
|
|
|
|
SP++;
|
2008-01-17 22:39:26 +01:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-07-14 18:33:29 +02:00
|
|
|
void EXTERN_release(void)
|
|
|
|
{
|
2011-03-13 03:01:16 +01:00
|
|
|
EXTERN_CALLBACK *cb;
|
|
|
|
HASH_ENUM iter;
|
|
|
|
|
|
|
|
if (!_callbacks)
|
|
|
|
return;
|
2010-07-14 18:33:29 +02:00
|
|
|
|
2011-03-13 03:01:16 +01:00
|
|
|
CLEAR(&iter);
|
|
|
|
|
|
|
|
for(;;)
|
2010-07-14 18:33:29 +02:00
|
|
|
{
|
2011-03-13 03:01:16 +01:00
|
|
|
cb = HASH_TABLE_next(_callbacks, &iter);
|
|
|
|
if (!cb)
|
|
|
|
break;
|
2010-07-14 18:33:29 +02:00
|
|
|
if (cb->exec.object)
|
2011-03-12 17:53:27 +01:00
|
|
|
{
|
2011-03-13 03:01:16 +01:00
|
|
|
OBJECT_UNREF(cb->exec.object, "EXTERN_release");
|
2011-03-12 17:53:27 +01:00
|
|
|
cb->exec.object = NULL;
|
|
|
|
}
|
2010-07-14 18:33:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void EXTERN_exit(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-07-14 13:55:32 +02:00
|
|
|
int i;
|
|
|
|
EXTERN_SYMBOL *esym;
|
2010-07-14 03:50:18 +02:00
|
|
|
EXTERN_CALLBACK *cb;
|
2010-07-14 13:55:32 +02:00
|
|
|
EXTERN_FUNC *func;
|
2011-03-13 03:01:16 +01:00
|
|
|
HASH_ENUM iter;
|
2010-07-14 13:55:32 +02:00
|
|
|
|
|
|
|
if (_table)
|
|
|
|
{
|
|
|
|
for (i = 0; i < TABLE_count(_table); i++)
|
|
|
|
{
|
|
|
|
esym = (EXTERN_SYMBOL *)TABLE_get_symbol(_table, i);
|
|
|
|
if (esym->handle)
|
|
|
|
lt_dlclose(esym->handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
TABLE_delete(&_table);
|
|
|
|
}
|
2010-07-14 03:50:18 +02:00
|
|
|
|
2011-03-13 03:01:16 +01:00
|
|
|
if (_callbacks)
|
2010-07-14 03:50:18 +02:00
|
|
|
{
|
2011-03-13 03:01:16 +01:00
|
|
|
CLEAR(&iter);
|
2010-07-14 03:50:18 +02:00
|
|
|
|
2011-03-13 03:01:16 +01:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
cb = HASH_TABLE_next(_callbacks, &iter);
|
|
|
|
if (!cb)
|
|
|
|
break;
|
|
|
|
if (cb->exec.object)
|
|
|
|
OBJECT_UNREF(cb->exec.object, "EXTERN_exit");
|
|
|
|
FREE(&cb->info.types, "EXTERN_exit");
|
|
|
|
ffi_closure_free(cb->closure);
|
|
|
|
}
|
2010-07-14 13:55:32 +02:00
|
|
|
|
2011-03-13 03:01:16 +01:00
|
|
|
HASH_TABLE_delete(&_callbacks);
|
|
|
|
}
|
|
|
|
|
2010-07-14 13:55:32 +02:00
|
|
|
while (_functions)
|
|
|
|
{
|
|
|
|
func = _functions;
|
|
|
|
_functions = func->next;
|
|
|
|
|
|
|
|
FREE(&func->info.types, "EXTERN_exit");
|
|
|
|
FREE(&func, "EXTERN_exit");
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
2010-07-14 03:50:18 +02:00
|
|
|
|
|
|
|
static void callback(ffi_cif *cif, void *result, void **args, void *user_data)
|
|
|
|
{
|
2010-07-14 13:55:32 +02:00
|
|
|
static const void *jump[17] = {
|
|
|
|
&&__VOID, &&__BOOLEAN, &&__BYTE, &&__SHORT, &&__INTEGER, &&__LONG, &&__SINGLE, &&__FLOAT, &&__DATE,
|
|
|
|
&&__STRING, &&__STRING, &&__POINTER, &&__VARIANT, &&__FUNCTION, &&__CLASS, &&__NULL, &&__OBJECT
|
|
|
|
};
|
2010-07-14 03:50:18 +02:00
|
|
|
|
|
|
|
EXTERN_CALLBACK *cb = (EXTERN_CALLBACK *)user_data;
|
2010-07-14 03:57:13 +02:00
|
|
|
//VALUE_FUNCTION *value = &cb->func;
|
2010-07-14 03:50:18 +02:00
|
|
|
int i;
|
|
|
|
VALUE *arg;
|
|
|
|
|
|
|
|
STACK_check(cb->nparam);
|
|
|
|
|
|
|
|
for (i = 0; i < cb->nparam; i++)
|
|
|
|
{
|
|
|
|
arg = SP++;
|
|
|
|
arg->type = cb->sign[i];
|
|
|
|
|
|
|
|
if (TYPE_is_object(cb->sign[i]))
|
|
|
|
goto __OBJECT;
|
|
|
|
else
|
|
|
|
goto *jump[cb->sign[i]];
|
|
|
|
|
|
|
|
__BOOLEAN:
|
|
|
|
arg->_integer.value = *((char *)args[i]) ? -1 : 0;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
__BYTE:
|
|
|
|
arg->_integer.value = *((char *)args[i]);
|
|
|
|
continue;
|
|
|
|
|
|
|
|
__SHORT:
|
|
|
|
arg->_integer.value = *((short *)args[i]);
|
|
|
|
continue;
|
|
|
|
|
|
|
|
__INTEGER:
|
|
|
|
arg->_integer.value = *((int *)args[i]);
|
|
|
|
continue;
|
|
|
|
|
|
|
|
__LONG:
|
2010-11-16 02:49:18 +01:00
|
|
|
arg->_long.value = *((int64_t *)args[i]);
|
2010-07-14 03:50:18 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
__SINGLE:
|
2010-11-16 02:49:18 +01:00
|
|
|
arg->_single.value = *((float *)args[i]);
|
2010-07-14 03:50:18 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
__FLOAT:
|
2010-11-16 02:49:18 +01:00
|
|
|
arg->_float.value = *((double *)args[i]);
|
2010-07-14 03:50:18 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
__STRING:
|
|
|
|
arg->type = T_CSTRING;
|
|
|
|
arg->_string.addr = *((char **)args[i]);
|
|
|
|
arg->_string.start = 0;
|
|
|
|
arg->_string.len = arg->_string.addr ? strlen(arg->_string.addr) : 0;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
__OBJECT:
|
|
|
|
arg->_object.object = *((void **)args[i]);
|
|
|
|
continue;
|
|
|
|
|
|
|
|
__POINTER:
|
|
|
|
arg->_pointer.value = *((void **)args[i]);
|
|
|
|
continue;
|
|
|
|
|
|
|
|
__NULL:
|
|
|
|
__DATE:
|
|
|
|
__VARIANT:
|
|
|
|
__VOID:
|
|
|
|
__CLASS:
|
|
|
|
__FUNCTION:
|
|
|
|
arg->type = T_NULL;
|
|
|
|
}
|
|
|
|
|
2010-07-14 03:57:13 +02:00
|
|
|
EXEC = cb->exec;
|
|
|
|
|
|
|
|
if (!EXEC.native)
|
2010-07-14 03:50:18 +02:00
|
|
|
{
|
|
|
|
EXEC_function_keep();
|
|
|
|
|
|
|
|
// Do that later, within a TRY/CATCH: VALUE_conv(RP, cb->ret);
|
|
|
|
|
|
|
|
switch (cb->ret)
|
|
|
|
{
|
|
|
|
case T_BOOLEAN:
|
|
|
|
case T_BYTE:
|
|
|
|
case T_SHORT:
|
|
|
|
case T_INTEGER:
|
|
|
|
*((ffi_arg *)result) = RP->_integer.value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_LONG:
|
|
|
|
*((int64_t *)result) = RP->_long.value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_SINGLE:
|
|
|
|
*((float *)result) = RP->_single.value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_FLOAT:
|
|
|
|
*((double *)result) = RP->_float.value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_STRING:
|
|
|
|
if (!RP->_string.len)
|
|
|
|
*((char **)result) = NULL;
|
|
|
|
else
|
|
|
|
*((char **)result) = RP->_string.addr + RP->_string.start;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_OBJECT:
|
|
|
|
*((void **)result) = RP->_object.object;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_POINTER:
|
|
|
|
*((void **)result) = RP->_pointer.value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
EXEC_release_return_value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void prepare_cif_from_gambas(EXTERN_CALLBACK *cb, FUNCTION *func)
|
|
|
|
{
|
|
|
|
if (func->npmin != func->n_param || func->vararg)
|
|
|
|
THROW(E_EXTCB, "The function must take a fixed number of arguments");
|
|
|
|
|
|
|
|
cb->nparam = func->npmin;
|
|
|
|
cb->sign = (TYPE *)func->param;
|
|
|
|
cb->ret = func->type;
|
|
|
|
}
|
|
|
|
|
2011-03-13 03:01:16 +01:00
|
|
|
/*static void prepare_cif_from_native(EXTERN_CALLBACK *cb, CLASS_DESC_METHOD *desc)
|
2010-07-14 03:50:18 +02:00
|
|
|
{
|
|
|
|
THROW(E_EXTCB, "Not implemented yet");
|
2011-03-13 03:01:16 +01:00
|
|
|
}*/
|
2010-07-14 03:50:18 +02:00
|
|
|
|
|
|
|
void *EXTERN_make_callback(VALUE_FUNCTION *value)
|
|
|
|
{
|
2011-10-24 14:16:40 +02:00
|
|
|
EXEC_GLOBAL exec;
|
|
|
|
FUNCTION *func;
|
2011-03-13 03:01:16 +01:00
|
|
|
EXTERN_CALLBACK *cb;
|
|
|
|
union {
|
|
|
|
char key[sizeof(void *)];
|
|
|
|
void *addr;
|
|
|
|
}
|
|
|
|
cb_key;
|
2010-07-14 03:50:18 +02:00
|
|
|
|
|
|
|
if (value->kind == FUNCTION_EXTERN)
|
|
|
|
{
|
|
|
|
CLASS_EXTERN *ext = &value->class->load->ext[value->index];
|
2011-03-13 03:01:16 +01:00
|
|
|
return get_function(ext)->call;
|
2010-07-14 03:50:18 +02:00
|
|
|
}
|
|
|
|
|
2011-03-13 03:01:16 +01:00
|
|
|
if (!_callbacks)
|
|
|
|
HASH_TABLE_create(&_callbacks, sizeof(EXTERN_CALLBACK), HF_NORMAL);
|
2010-07-14 03:50:18 +02:00
|
|
|
|
2011-03-13 03:01:16 +01:00
|
|
|
//ALLOC(&cb, sizeof(EXTERN_CALLBACK), "EXTERN_make_callback");
|
|
|
|
|
2010-07-14 03:50:18 +02:00
|
|
|
// See gbx_exec_loop.c, at the _CALL label, to understand the following.
|
|
|
|
|
|
|
|
if (value->kind == FUNCTION_PRIVATE)
|
|
|
|
{
|
2011-10-24 14:16:40 +02:00
|
|
|
exec.object = value->object;
|
|
|
|
exec.class = value->class;
|
|
|
|
exec.native = FALSE;
|
|
|
|
exec.index = value->index;
|
2010-07-14 03:50:18 +02:00
|
|
|
}
|
|
|
|
else if (value->kind == FUNCTION_PUBLIC)
|
|
|
|
{
|
2011-10-24 14:16:40 +02:00
|
|
|
exec.object = value->object;
|
|
|
|
exec.native = FALSE;
|
|
|
|
exec.desc = &value->class->table[value->index].desc->method;
|
|
|
|
exec.index = (int)(intptr_t)(exec.desc->exec);
|
|
|
|
exec.class = exec.desc->class;
|
2010-07-14 03:50:18 +02:00
|
|
|
}
|
2011-03-13 03:01:16 +01:00
|
|
|
/*else if (value->kind == FUNCTION_NATIVE)
|
2010-07-14 03:50:18 +02:00
|
|
|
{
|
|
|
|
cb->exec.object = value->object;
|
|
|
|
cb->exec.class = value->class;
|
|
|
|
cb->exec.native = TRUE;
|
2010-07-14 13:55:32 +02:00
|
|
|
cb->exec.index = value->index;
|
|
|
|
cb->exec.desc = &value->class->table[value->index].desc->method;
|
2010-07-14 03:50:18 +02:00
|
|
|
//cb->desc = &value->class->table[value->index].desc->method;
|
2011-03-12 17:53:27 +01:00
|
|
|
|
2010-07-14 03:50:18 +02:00
|
|
|
prepare_cif_from_native(cb, cb->exec.desc);
|
2011-03-13 03:01:16 +01:00
|
|
|
}*/
|
2010-07-14 03:50:18 +02:00
|
|
|
else
|
2011-10-24 14:16:40 +02:00
|
|
|
THROW(E_EXTCB, "Not supported");
|
|
|
|
|
|
|
|
func = &exec.class->load->func[exec.index];
|
|
|
|
cb_key.addr = func;
|
|
|
|
|
|
|
|
cb = (EXTERN_CALLBACK *)HASH_TABLE_insert(_callbacks, cb_key.key, sizeof(void *));
|
|
|
|
if (cb->code)
|
|
|
|
{
|
|
|
|
OBJECT_UNREF(value->object, "EXTERN_make_callback");
|
|
|
|
return cb->code;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not reference value->_function.object, as it has been already referenced
|
|
|
|
// when put on the stack in exec_loop.c
|
|
|
|
|
|
|
|
/*if (value->object)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "EXTERN_make_callback: ref: %p\n", value->object);
|
|
|
|
OBJECT_REF(value->object, "EXTERN_make_callback");
|
|
|
|
}*/
|
|
|
|
|
|
|
|
cb->exec = exec;
|
|
|
|
prepare_cif_from_gambas(cb, func);
|
|
|
|
|
2010-07-14 03:50:18 +02:00
|
|
|
cb->exec.nparam = cb->nparam;
|
|
|
|
|
2012-06-01 02:18:38 +02:00
|
|
|
prepare_cif(&cb->info, cb->nparam, cb->sign, cb->ret, cb->nparam, NULL);
|
2010-07-14 03:50:18 +02:00
|
|
|
|
2011-03-13 03:01:16 +01:00
|
|
|
cb->closure = ffi_closure_alloc(sizeof(ffi_closure), &cb->code);
|
2010-07-14 03:50:18 +02:00
|
|
|
|
2011-03-13 03:01:16 +01:00
|
|
|
if (ffi_prep_closure_loc(cb->closure, &cb->info.cif, callback, cb, cb->code) != FFI_OK)
|
2010-07-14 03:50:18 +02:00
|
|
|
THROW(E_EXTCB, "Unable to create closure");
|
|
|
|
|
2011-03-13 03:01:16 +01:00
|
|
|
return cb->code;
|
2010-07-14 03:50:18 +02:00
|
|
|
}
|
|
|
|
|
2010-07-14 13:55:32 +02:00
|
|
|
#else /* HAVE_FFI_COMPONENT */
|
|
|
|
|
2010-07-26 20:10:51 +02:00
|
|
|
#include "gbx_value.h"
|
2012-07-21 02:27:39 +02:00
|
|
|
#include "gbx_extern.h"
|
2010-07-26 20:10:51 +02:00
|
|
|
|
2010-07-14 13:55:32 +02:00
|
|
|
void EXTERN_call(void)
|
|
|
|
{
|
|
|
|
THROW_ILLEGAL();
|
|
|
|
}
|
|
|
|
|
2010-07-14 18:33:29 +02:00
|
|
|
void EXTERN_release(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-07-14 13:55:32 +02:00
|
|
|
void EXTERN_exit(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void *EXTERN_make_callback(VALUE_FUNCTION *value)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-05-19 01:28:25 +02:00
|
|
|
void *EXTERN_get_symbol(const char *library, const char *symbol)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-07-19 21:27:28 +02:00
|
|
|
|
|
|
|
EXTERN_FUNC_INFO EXTERN_get_function_info(CLASS_EXTERN *ext)
|
|
|
|
{
|
|
|
|
EXTERN_FUNC_INFO func_info = { NULL, NULL };
|
|
|
|
|
|
|
|
return func_info;
|
|
|
|
}
|
2010-07-14 13:55:32 +02:00
|
|
|
#endif
|
|
|
|
|