2019-08-10 04:10:13 +00:00
|
|
|
/*
|
2017-01-09 13:41:11 +00:00
|
|
|
* $Id$
|
|
|
|
*
|
2019-11-19 09:40:26 +00:00
|
|
|
Copyright (c) 2014-2019 Chung, Hyung-Hwan. All rights reserved.
|
2017-01-09 13:41:11 +00:00
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
|
|
|
|
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "_ffi.h"
|
|
|
|
#include <moo-utl.h>
|
|
|
|
|
2017-01-10 10:50:26 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2019-09-04 15:06:24 +00:00
|
|
|
#if defined(HAVE_FFI_LIB) && defined(HAVE_FFI_H)
|
2019-08-09 18:19:13 +00:00
|
|
|
# define USE_LIBFFI
|
2019-09-04 15:06:24 +00:00
|
|
|
#elif defined(HAVE_DYNCALL_LIB) && defined(HAVE_DYNCALL_H)
|
|
|
|
# define USE_DYNCALL
|
2017-01-10 14:27:31 +00:00
|
|
|
#endif
|
2017-01-10 10:50:26 +00:00
|
|
|
|
2019-09-04 15:06:24 +00:00
|
|
|
#if defined(USE_LIBFFI)
|
2019-08-09 18:19:13 +00:00
|
|
|
# include <ffi.h>
|
|
|
|
# if (MOO_SIZEOF_LONG_LONG > 0) && !defined(ffi_type_ulonglong)
|
|
|
|
# if MOO_SIZEOF_LONG_LONG == MOO_SIZEOF_INT32_T
|
|
|
|
# define ffi_type_ulonglong ffi_type_uint32
|
|
|
|
# define ffi_type_slonglong ffi_type_sint32
|
|
|
|
# elif MOO_SIZEOF_LONG_LONG == MOO_SIZEOF_INT64_T
|
|
|
|
# define ffi_type_ulonglong ffi_type_uint64
|
|
|
|
# define ffi_type_slonglong ffi_type_sint64
|
|
|
|
# endif
|
|
|
|
# endif
|
2019-09-04 15:06:24 +00:00
|
|
|
#elif defined(USE_DYNCALL)
|
|
|
|
# include <dyncall.h>
|
2019-09-26 07:48:02 +00:00
|
|
|
|
|
|
|
#define __dcArgInt8 dcArgChar
|
|
|
|
#define __dcCallInt8 dcCallChar
|
|
|
|
|
|
|
|
#define __dcArgInt16 dcArgShort
|
|
|
|
#define __dcCallInt16 dcCallShort
|
|
|
|
|
|
|
|
#if (MOO_SIZEOF_INT32_T == MOO_SIZEOF_INT)
|
|
|
|
#define __dcArgInt32 dcArgInt
|
|
|
|
#define __dcCallInt32 dcCallInt
|
|
|
|
#elif (MOO_SIZEOF_INT32_T == MOO_SIZEOF_LONG)
|
|
|
|
#define __dcArgInt32 dcArgLong
|
|
|
|
#define __dcCallInt32 dcCallLong
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (MOO_SIZEOF_INT64_T == MOO_SIZEOF_LONG)
|
|
|
|
#define __dcArgInt64 dcArgLong
|
|
|
|
#define __dcCallInt64 dcCallLong
|
|
|
|
#elif (MOO_SIZEOF_INT64_T == MOO_SIZEOF_LONG_LONG)
|
|
|
|
#define __dcArgInt64 dcArgLongLong
|
|
|
|
#define __dcCallInt64 dcCallLongLong
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2017-01-09 13:41:11 +00:00
|
|
|
#endif
|
|
|
|
|
2019-08-09 18:19:13 +00:00
|
|
|
#define FMTC_NULL '\0' /* internal use only */
|
2019-09-26 07:48:02 +00:00
|
|
|
|
2019-08-09 18:19:13 +00:00
|
|
|
#define FMTC_CHAR 'c'
|
|
|
|
#define FMTC_SHORT 'h'
|
|
|
|
#define FMTC_INT 'i'
|
|
|
|
#define FMTC_LONG 'l'
|
|
|
|
#define FMTC_LONGLONG 'L'
|
|
|
|
#define FMTC_BCS 's'
|
|
|
|
#define FMTC_UCS 'S'
|
2019-08-11 10:11:54 +00:00
|
|
|
#define FMTC_BLOB 'b'
|
2019-08-10 04:08:49 +00:00
|
|
|
#define FMTC_POINTER 'p'
|
2019-08-09 18:19:13 +00:00
|
|
|
|
2019-09-26 07:48:02 +00:00
|
|
|
#define FMTC_INT8 '1'
|
|
|
|
#define FMTC_INT16 '2'
|
|
|
|
#define FMTC_INT32 '4'
|
|
|
|
#define FMTC_INT64 '8'
|
|
|
|
|
2017-01-15 17:53:37 +00:00
|
|
|
typedef struct link_t link_t;
|
|
|
|
struct link_t
|
|
|
|
{
|
|
|
|
link_t* next;
|
|
|
|
};
|
2017-01-09 13:41:11 +00:00
|
|
|
|
2019-08-09 18:19:13 +00:00
|
|
|
#if defined(USE_LIBFFI)
|
|
|
|
typedef union ffi_sv_t ffi_sv_t;
|
|
|
|
union ffi_sv_t
|
|
|
|
{
|
|
|
|
void* p;
|
|
|
|
unsigned char uc;
|
|
|
|
char c;
|
|
|
|
unsigned short int uh;
|
|
|
|
short h;
|
|
|
|
unsigned int ui;
|
|
|
|
int i;
|
|
|
|
unsigned long int ul;
|
|
|
|
long int l;
|
|
|
|
#if (MOO_SIZEOF_LONG_LONG > 0)
|
|
|
|
unsigned long long int ull;
|
|
|
|
long long int ll;
|
|
|
|
#endif
|
2019-09-26 07:48:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
moo_uint8_t ui8;
|
|
|
|
moo_int8_t i8;
|
|
|
|
moo_uint16_t ui16;
|
|
|
|
moo_int16_t i16;
|
|
|
|
moo_uint32_t ui32;
|
|
|
|
moo_int32_t i32;
|
|
|
|
#if (MOO_SIZEOF_INT64_T > 0)
|
|
|
|
moo_uint64_t ui64;
|
|
|
|
moo_int64_t i64;
|
|
|
|
#endif
|
2019-08-09 18:19:13 +00:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2017-01-10 10:50:26 +00:00
|
|
|
typedef struct ffi_t ffi_t;
|
|
|
|
struct ffi_t
|
|
|
|
{
|
|
|
|
void* handle;
|
2017-01-15 17:53:37 +00:00
|
|
|
|
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
DCCallVM* dc;
|
2019-08-09 18:19:13 +00:00
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
moo_oow_t arg_count;
|
|
|
|
moo_oow_t arg_max;
|
|
|
|
ffi_type** arg_types;
|
|
|
|
void** arg_values;
|
|
|
|
ffi_sv_t* arg_svs;
|
|
|
|
|
|
|
|
ffi_sv_t ret_sv;
|
|
|
|
ffi_cif cif;
|
|
|
|
ffi_type* fmtc_to_type[2][128];
|
2017-01-15 17:53:37 +00:00
|
|
|
#endif
|
2019-08-09 18:19:13 +00:00
|
|
|
|
2017-01-15 17:53:37 +00:00
|
|
|
link_t* ca; /* call arguments duplicated */
|
2017-01-10 10:50:26 +00:00
|
|
|
};
|
2017-01-09 13:41:11 +00:00
|
|
|
|
2017-01-15 17:53:37 +00:00
|
|
|
|
|
|
|
static MOO_INLINE void link_ca (ffi_t* ffi, void* ptr)
|
|
|
|
{
|
|
|
|
link_t* l = (link_t*)((moo_oob_t*)ptr - MOO_SIZEOF_VOID_P);
|
|
|
|
l->next = ffi->ca;
|
|
|
|
ffi->ca = l;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_linked_cas (moo_t* moo, ffi_t* ffi)
|
|
|
|
{
|
|
|
|
while (ffi->ca)
|
|
|
|
{
|
|
|
|
link_t* ptr;
|
|
|
|
ptr = ffi->ca;
|
|
|
|
ffi->ca = ptr->next;
|
|
|
|
moo_freemem (moo, ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-07 16:53:16 +00:00
|
|
|
static moo_pfrc_t pf_open (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs)
|
2017-01-09 13:41:11 +00:00
|
|
|
{
|
2017-02-14 08:29:30 +00:00
|
|
|
ffi_t* ffi;
|
2017-01-10 13:56:19 +00:00
|
|
|
moo_oop_t name;
|
2017-01-15 17:53:37 +00:00
|
|
|
void* handle;
|
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
DCCallVM* dc;
|
|
|
|
#endif
|
2017-01-09 13:41:11 +00:00
|
|
|
|
2017-03-30 14:59:55 +00:00
|
|
|
MOO_ASSERT (moo, nargs == 1);
|
2017-01-09 13:41:11 +00:00
|
|
|
|
2017-02-14 08:29:30 +00:00
|
|
|
ffi = (ffi_t*)moo_getobjtrailer(moo, MOO_STACK_GETRCV(moo, nargs), MOO_NULL);
|
2017-01-10 13:56:19 +00:00
|
|
|
name = MOO_STACK_GETARG(moo, nargs, 0);
|
2017-01-09 13:41:11 +00:00
|
|
|
|
2017-02-14 08:29:30 +00:00
|
|
|
if (!MOO_OBJ_IS_CHAR_POINTER(name))
|
2017-01-09 13:41:11 +00:00
|
|
|
{
|
2017-01-10 10:50:26 +00:00
|
|
|
moo_seterrnum (moo, MOO_EINVAL);
|
2017-01-12 16:33:26 +00:00
|
|
|
goto softfail;
|
2017-01-09 13:41:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!moo->vmprim.dl_open)
|
|
|
|
{
|
2017-01-10 10:50:26 +00:00
|
|
|
moo_seterrnum (moo, MOO_ENOIMPL);
|
2017-01-12 16:33:26 +00:00
|
|
|
goto softfail;
|
2017-01-09 13:41:11 +00:00
|
|
|
}
|
|
|
|
|
2017-02-14 08:29:30 +00:00
|
|
|
if (ffi->handle)
|
2017-01-15 17:53:37 +00:00
|
|
|
{
|
|
|
|
moo_seterrnum (moo, MOO_EPERM); /* no allowed to open again */
|
|
|
|
goto softfail;
|
|
|
|
}
|
|
|
|
|
2019-08-09 18:19:13 +00:00
|
|
|
handle = moo->vmprim.dl_open(moo, MOO_OBJ_GET_CHAR_SLOT(name), 0);
|
2017-01-15 17:53:37 +00:00
|
|
|
if (!handle) goto softfail;
|
|
|
|
|
|
|
|
#if defined(USE_DYNCALL)
|
2019-08-09 18:19:13 +00:00
|
|
|
dc = dcNewCallVM(4096); /* TODO: right size? */
|
2017-01-15 17:53:37 +00:00
|
|
|
if (!dc)
|
|
|
|
{
|
2018-11-02 05:57:27 +00:00
|
|
|
moo_seterrwithsyserr (moo, 0, errno);
|
2017-01-15 17:53:37 +00:00
|
|
|
moo->vmprim.dl_close (moo, handle);
|
|
|
|
goto softfail;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-02-14 08:29:30 +00:00
|
|
|
ffi->handle = handle;
|
2017-01-16 14:46:07 +00:00
|
|
|
|
|
|
|
#if defined(USE_DYNCALL)
|
2017-02-14 08:29:30 +00:00
|
|
|
ffi->dc = dc;
|
2019-08-09 18:19:13 +00:00
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
ffi->fmtc_to_type[0][FMTC_NULL] = &ffi_type_void;
|
|
|
|
ffi->fmtc_to_type[1][FMTC_NULL] = &ffi_type_void;
|
|
|
|
|
|
|
|
ffi->fmtc_to_type[0][FMTC_CHAR] = &ffi_type_schar;
|
|
|
|
ffi->fmtc_to_type[1][FMTC_CHAR] = &ffi_type_uchar;
|
|
|
|
ffi->fmtc_to_type[0][FMTC_SHORT] = &ffi_type_sshort;
|
|
|
|
ffi->fmtc_to_type[1][FMTC_SHORT] = &ffi_type_ushort;
|
|
|
|
ffi->fmtc_to_type[0][FMTC_INT] = &ffi_type_sint;
|
|
|
|
ffi->fmtc_to_type[1][FMTC_INT] = &ffi_type_uint;
|
|
|
|
ffi->fmtc_to_type[0][FMTC_LONG] = &ffi_type_slong;
|
|
|
|
ffi->fmtc_to_type[1][FMTC_LONG] = &ffi_type_ulong;
|
|
|
|
#if (MOO_SIZEOF_LONG_LONG > 0)
|
|
|
|
ffi->fmtc_to_type[0][FMTC_LONGLONG] = &ffi_type_slonglong;
|
|
|
|
ffi->fmtc_to_type[1][FMTC_LONGLONG] = &ffi_type_ulonglong;
|
|
|
|
#endif
|
2019-09-26 07:48:02 +00:00
|
|
|
ffi->fmtc_to_type[0][FMTC_INT8] = &ffi_type_sint8;
|
|
|
|
ffi->fmtc_to_type[1][FMTC_INT8] = &ffi_type_uint8;
|
|
|
|
ffi->fmtc_to_type[0][FMTC_INT16] = &ffi_type_sint16;
|
|
|
|
ffi->fmtc_to_type[1][FMTC_INT16] = &ffi_type_uint16;
|
|
|
|
ffi->fmtc_to_type[0][FMTC_INT32] = &ffi_type_sint32;
|
|
|
|
ffi->fmtc_to_type[1][FMTC_INT32] = &ffi_type_uint32;
|
|
|
|
ffi->fmtc_to_type[0][FMTC_INT64] = &ffi_type_sint64;
|
|
|
|
ffi->fmtc_to_type[1][FMTC_INT64] = &ffi_type_uint64;
|
2019-08-09 18:19:13 +00:00
|
|
|
|
|
|
|
ffi->fmtc_to_type[0][FMTC_POINTER] = &ffi_type_pointer;
|
|
|
|
ffi->fmtc_to_type[1][FMTC_POINTER] = &ffi_type_pointer;
|
|
|
|
ffi->fmtc_to_type[0][FMTC_BCS] = &ffi_type_pointer;
|
|
|
|
ffi->fmtc_to_type[1][FMTC_BCS] = &ffi_type_pointer;
|
|
|
|
ffi->fmtc_to_type[0][FMTC_UCS] = &ffi_type_pointer;
|
|
|
|
ffi->fmtc_to_type[1][FMTC_UCS] = &ffi_type_pointer;
|
2017-01-16 14:46:07 +00:00
|
|
|
#endif
|
2017-01-09 13:41:11 +00:00
|
|
|
|
2018-12-12 15:05:18 +00:00
|
|
|
MOO_DEBUG3 (moo, "<ffi.open> %.*js => %p\n", MOO_OBJ_GET_SIZE(name), MOO_OBJ_GET_CHAR_SLOT(name), ffi->handle);
|
2017-01-10 10:50:26 +00:00
|
|
|
MOO_STACK_SETRETTORCV (moo, nargs);
|
|
|
|
return MOO_PF_SUCCESS;
|
2017-01-09 13:41:11 +00:00
|
|
|
|
2017-01-12 16:33:26 +00:00
|
|
|
softfail:
|
2017-04-03 05:43:50 +00:00
|
|
|
MOO_STACK_SETRETTOERRNUM (moo, nargs);
|
2017-01-09 13:41:11 +00:00
|
|
|
return MOO_PF_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-05-07 16:53:16 +00:00
|
|
|
static moo_pfrc_t pf_close (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs)
|
2017-01-09 13:41:11 +00:00
|
|
|
{
|
2017-02-14 08:29:30 +00:00
|
|
|
ffi_t* ffi;
|
2017-01-09 13:41:11 +00:00
|
|
|
|
2017-03-30 14:59:55 +00:00
|
|
|
MOO_ASSERT (moo, nargs == 0);
|
2017-01-09 13:41:11 +00:00
|
|
|
|
2017-02-14 08:29:30 +00:00
|
|
|
ffi = (ffi_t*)moo_getobjtrailer(moo, MOO_STACK_GETRCV(moo, nargs), MOO_NULL);
|
2017-01-09 13:41:11 +00:00
|
|
|
|
2017-01-10 10:50:26 +00:00
|
|
|
if (!moo->vmprim.dl_open)
|
2017-01-09 13:41:11 +00:00
|
|
|
{
|
2017-01-10 10:50:26 +00:00
|
|
|
moo_seterrnum (moo, MOO_ENOIMPL);
|
2017-01-12 16:33:26 +00:00
|
|
|
goto softfail;
|
2017-01-09 13:41:11 +00:00
|
|
|
}
|
|
|
|
|
2017-02-14 08:29:30 +00:00
|
|
|
MOO_DEBUG1 (moo, "<ffi.close> %p\n", ffi->handle);
|
2017-01-10 13:56:19 +00:00
|
|
|
|
2017-02-14 08:29:30 +00:00
|
|
|
free_linked_cas (moo, ffi);
|
2017-01-15 17:53:37 +00:00
|
|
|
|
|
|
|
#if defined(USE_DYNCALL)
|
2017-02-14 08:29:30 +00:00
|
|
|
dcFree (ffi->dc);
|
|
|
|
ffi->dc = MOO_NULL;
|
2019-08-10 04:08:49 +00:00
|
|
|
#elif defined(USE_LIBFFI)
|
2019-08-09 18:19:13 +00:00
|
|
|
if (ffi->arg_types)
|
|
|
|
{
|
|
|
|
moo_freemem (moo, ffi->arg_types);
|
|
|
|
ffi->arg_types = MOO_NULL;
|
|
|
|
}
|
|
|
|
if (ffi->arg_values)
|
|
|
|
{
|
|
|
|
moo_freemem (moo, ffi->arg_values);
|
|
|
|
ffi->arg_values = MOO_NULL;
|
|
|
|
}
|
|
|
|
if (ffi->arg_svs)
|
|
|
|
{
|
|
|
|
moo_freemem (moo, ffi->arg_svs);
|
|
|
|
ffi->arg_svs = MOO_NULL;
|
|
|
|
}
|
|
|
|
ffi->arg_max = 0;
|
|
|
|
ffi->arg_count = 0;
|
|
|
|
#endif
|
2017-01-15 17:53:37 +00:00
|
|
|
|
2017-02-14 08:29:30 +00:00
|
|
|
moo->vmprim.dl_close (moo, ffi->handle);
|
|
|
|
ffi->handle = MOO_NULL;
|
2017-01-09 13:41:11 +00:00
|
|
|
|
2017-01-10 10:50:26 +00:00
|
|
|
MOO_STACK_SETRETTORCV (moo, nargs);
|
2017-01-09 13:41:11 +00:00
|
|
|
return MOO_PF_SUCCESS;
|
2017-01-10 10:50:26 +00:00
|
|
|
|
2017-01-12 16:33:26 +00:00
|
|
|
softfail:
|
2017-04-03 05:43:50 +00:00
|
|
|
MOO_STACK_SETRETTOERRNUM (moo, nargs);
|
2017-01-10 13:56:19 +00:00
|
|
|
return MOO_PF_SUCCESS;
|
2017-01-09 13:41:11 +00:00
|
|
|
}
|
2017-01-12 10:06:43 +00:00
|
|
|
|
2019-08-10 04:08:49 +00:00
|
|
|
static MOO_INLINE int add_ffi_arg (moo_t* moo, ffi_t* ffi, moo_ooch_t fmtc, int _unsigned, moo_oop_t arg)
|
2019-08-09 18:19:13 +00:00
|
|
|
{
|
|
|
|
#if defined(USE_LIBFFI)
|
|
|
|
if (ffi->arg_count >= ffi->arg_max)
|
|
|
|
{
|
|
|
|
ffi_type** ttmp;
|
|
|
|
void** vtmp;
|
|
|
|
ffi_sv_t* stmp;
|
|
|
|
|
|
|
|
moo_oow_t newmax;
|
|
|
|
|
|
|
|
newmax = ffi->arg_max + 16; /* TODO: adjust this? */
|
|
|
|
ttmp = moo_reallocmem(moo, ffi->arg_types, MOO_SIZEOF(*ttmp) * newmax);
|
|
|
|
if (!ttmp) goto oops;
|
2020-12-27 18:52:07 +00:00
|
|
|
ffi->arg_types = ttmp;
|
|
|
|
|
2019-08-09 18:19:13 +00:00
|
|
|
vtmp = moo_reallocmem(moo, ffi->arg_values, MOO_SIZEOF(*vtmp) * newmax);
|
|
|
|
if (!vtmp) goto oops;
|
2020-12-27 18:52:07 +00:00
|
|
|
ffi->arg_values = vtmp;
|
|
|
|
|
2019-08-09 18:19:13 +00:00
|
|
|
stmp = moo_reallocmem(moo, ffi->arg_svs, MOO_SIZEOF(*stmp) * newmax);
|
|
|
|
if (!stmp) goto oops;
|
|
|
|
ffi->arg_svs = stmp;
|
2020-12-27 18:52:07 +00:00
|
|
|
|
2019-08-09 18:19:13 +00:00
|
|
|
ffi->arg_max = newmax;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
switch (fmtc)
|
|
|
|
{
|
2019-09-26 07:48:02 +00:00
|
|
|
case FMTC_INT8:
|
|
|
|
if (_unsigned)
|
|
|
|
{
|
|
|
|
moo_oow_t v;
|
2019-11-01 09:15:53 +00:00
|
|
|
if (MOO_OOP_IS_CHAR(arg)) v = MOO_OOP_TO_CHAR(arg);
|
|
|
|
else if (moo_inttooow_noseterr(moo, arg, &v) <= 0) goto inval_arg_value;;
|
2019-09-26 07:48:02 +00:00
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
__dcArgInt8 (ffi->dc, v);
|
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
ffi->arg_values[ffi->arg_count] = &ffi->arg_svs[ffi->arg_count].ui8;
|
|
|
|
ffi->arg_svs[ffi->arg_count].ui8 = v;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
moo_ooi_t v;
|
2019-11-01 09:15:53 +00:00
|
|
|
if (MOO_OOP_IS_CHAR(arg)) v = MOO_OOP_TO_CHAR(arg);
|
|
|
|
else if (moo_inttoooi_noseterr(moo, arg, &v) == 0) goto inval_arg_value;;
|
2019-09-26 07:48:02 +00:00
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
__dcArgInt8 (ffi->dc, v);
|
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
ffi->arg_values[ffi->arg_count] = &ffi->arg_svs[ffi->arg_count].i8;
|
|
|
|
ffi->arg_svs[ffi->arg_count].i8 = v;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FMTC_INT16:
|
|
|
|
if (_unsigned)
|
|
|
|
{
|
|
|
|
moo_oow_t v;
|
2019-11-01 09:15:53 +00:00
|
|
|
if (moo_inttooow_noseterr(moo, arg, &v) <= 0) goto inval_arg_value;;
|
2019-09-26 07:48:02 +00:00
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
__dcArgInt16 (ffi->dc, v);
|
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
ffi->arg_values[ffi->arg_count] = &ffi->arg_svs[ffi->arg_count].ui16;
|
|
|
|
ffi->arg_svs[ffi->arg_count].ui16 = v;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
moo_ooi_t v;
|
2019-11-01 09:15:53 +00:00
|
|
|
if (moo_inttoooi_noseterr(moo, arg, &v) == 0) goto inval_arg_value;;
|
2019-09-26 07:48:02 +00:00
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
__dcArgInt16 (ffi->dc, v);
|
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
ffi->arg_values[ffi->arg_count] = &ffi->arg_svs[ffi->arg_count].i16;
|
|
|
|
ffi->arg_svs[ffi->arg_count].i16 = v;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case FMTC_INT32:
|
|
|
|
if (_unsigned)
|
|
|
|
{
|
|
|
|
moo_oow_t v;
|
2019-11-01 09:15:53 +00:00
|
|
|
if (moo_inttooow_noseterr(moo, arg, &v) <= 0) goto inval_arg_value;;
|
2019-09-26 07:48:02 +00:00
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
__dcArgInt32 (ffi->dc, v);
|
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
ffi->arg_values[ffi->arg_count] = &ffi->arg_svs[ffi->arg_count].ui32;
|
|
|
|
ffi->arg_svs[ffi->arg_count].ui32 = v;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
moo_ooi_t v;
|
2019-11-01 09:15:53 +00:00
|
|
|
if (moo_inttoooi_noseterr(moo, arg, &v) == 0) goto inval_arg_value;;
|
2019-09-26 07:48:02 +00:00
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
__dcArgInt32 (ffi->dc, v);
|
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
ffi->arg_values[ffi->arg_count] = &ffi->arg_svs[ffi->arg_count].i32;
|
|
|
|
ffi->arg_svs[ffi->arg_count].i32 = v;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case FMTC_INT64:
|
|
|
|
#if (MOO_SIZEOF_INT64_T > 0)
|
|
|
|
if (_unsigned)
|
|
|
|
{
|
|
|
|
moo_oow_t v;
|
2019-11-01 09:15:53 +00:00
|
|
|
if (moo_inttooow_noseterr(moo, arg, &v) <= 0) goto inval_arg_value;
|
2019-09-26 07:48:02 +00:00
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
__dcArgInt64 (ffi->dc, v);
|
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
ffi->arg_values[ffi->arg_count] = &ffi->arg_svs[ffi->arg_count].ui64;
|
|
|
|
ffi->arg_svs[ffi->arg_count].ui64 = v;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
moo_ooi_t v;
|
2019-11-01 09:15:53 +00:00
|
|
|
if (moo_inttoooi_noseterr(moo, arg, &v) == 0) goto inval_arg_value;
|
2019-09-26 07:48:02 +00:00
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
__dcArgInt64 (ffi->dc, v);
|
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
ffi->arg_values[ffi->arg_count] = &ffi->arg_svs[ffi->arg_count].i64;
|
|
|
|
ffi->arg_svs[ffi->arg_count].i64 = v;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#else
|
|
|
|
goto inval_sig_ch;
|
|
|
|
#endif
|
|
|
|
|
2019-11-01 09:15:53 +00:00
|
|
|
case FMTC_CHAR: /* this is a byte */
|
2019-08-09 18:19:13 +00:00
|
|
|
if (_unsigned)
|
|
|
|
{
|
2019-11-01 09:15:53 +00:00
|
|
|
moo_oow_t v;
|
|
|
|
if (MOO_OOP_IS_CHAR(arg)) v = MOO_OOP_TO_CHAR(arg);
|
|
|
|
else if (moo_inttooow_noseterr(moo, arg, &v) <= 0) goto inval_arg_value;;
|
2019-08-09 18:19:13 +00:00
|
|
|
#if defined(USE_DYNCALL)
|
2019-11-01 09:15:53 +00:00
|
|
|
dcArgChar (ffi->dc, v);
|
2019-08-09 18:19:13 +00:00
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
ffi->arg_values[ffi->arg_count] = &ffi->arg_svs[ffi->arg_count].uc;
|
2019-11-01 09:15:53 +00:00
|
|
|
ffi->arg_svs[ffi->arg_count].uc = v;
|
2019-08-09 18:19:13 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-01 09:15:53 +00:00
|
|
|
moo_ooi_t v;
|
|
|
|
if (MOO_OOP_IS_CHAR(arg)) v = MOO_OOP_TO_CHAR(arg);
|
|
|
|
else if (moo_inttoooi_noseterr(moo, arg, &v) == 0) goto inval_arg_value;;
|
2019-08-09 18:19:13 +00:00
|
|
|
#if defined(USE_DYNCALL)
|
2019-11-01 09:15:53 +00:00
|
|
|
dcArgChar (ffi->dc, v);
|
2019-08-09 18:19:13 +00:00
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
ffi->arg_values[ffi->arg_count] = &ffi->arg_svs[ffi->arg_count].c;
|
2019-11-01 09:15:53 +00:00
|
|
|
ffi->arg_svs[ffi->arg_count].c = v;
|
2019-08-09 18:19:13 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FMTC_SHORT:
|
|
|
|
if (_unsigned)
|
|
|
|
{
|
|
|
|
moo_oow_t v;
|
2019-11-01 09:15:53 +00:00
|
|
|
if (moo_inttooow_noseterr(moo, arg, &v) <= 0) goto inval_arg_value;
|
2019-08-09 18:19:13 +00:00
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
dcArgShort (ffi->dc, v);
|
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
ffi->arg_values[ffi->arg_count] = &ffi->arg_svs[ffi->arg_count].uh;
|
|
|
|
ffi->arg_svs[ffi->arg_count].uh = v;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
moo_ooi_t v;
|
2019-11-01 09:15:53 +00:00
|
|
|
if (moo_inttoooi_noseterr(moo, arg, &v) == 0) goto inval_arg_value;
|
2019-08-09 18:19:13 +00:00
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
dcArgShort (ffi->dc, v);
|
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
ffi->arg_values[ffi->arg_count] = &ffi->arg_svs[ffi->arg_count].h;
|
|
|
|
ffi->arg_svs[ffi->arg_count].h = v;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FMTC_INT:
|
|
|
|
if (_unsigned)
|
|
|
|
{
|
|
|
|
moo_oow_t v;
|
2019-11-01 09:15:53 +00:00
|
|
|
if (moo_inttooow_noseterr(moo, arg, &v) <= 0) goto inval_arg_value;
|
2019-08-09 18:19:13 +00:00
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
dcArgInt (ffi->dc, v);
|
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
ffi->arg_values[ffi->arg_count] = &ffi->arg_svs[ffi->arg_count].ui;
|
|
|
|
ffi->arg_svs[ffi->arg_count].ui = v;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
moo_ooi_t v;
|
2019-11-01 09:15:53 +00:00
|
|
|
if (moo_inttoooi_noseterr(moo, arg, &v) == 0) goto inval_arg_value;
|
2019-08-09 18:19:13 +00:00
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
dcArgInt (ffi->dc, v);
|
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
ffi->arg_values[ffi->arg_count] = &ffi->arg_svs[ffi->arg_count].i;
|
|
|
|
ffi->arg_svs[ffi->arg_count].i = v;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FMTC_LONG:
|
|
|
|
arg_as_long:
|
|
|
|
if (_unsigned)
|
|
|
|
{
|
|
|
|
moo_oow_t v;
|
2019-11-01 09:15:53 +00:00
|
|
|
if (moo_inttooow_noseterr(moo, arg, &v) <= 0) goto inval_arg_value;
|
2019-08-09 18:19:13 +00:00
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
dcArgLong (ffi->dc, v);
|
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
ffi->arg_values[ffi->arg_count] = &ffi->arg_svs[ffi->arg_count].ul;
|
|
|
|
ffi->arg_svs[ffi->arg_count].ul = v;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
moo_ooi_t v;
|
2019-11-01 09:15:53 +00:00
|
|
|
if (moo_inttoooi_noseterr(moo, arg, &v) == 0) goto inval_arg_value;;
|
2019-08-09 18:19:13 +00:00
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
dcArgLong (ffi->dc, v);
|
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
ffi->arg_values[ffi->arg_count] = &ffi->arg_svs[ffi->arg_count].l;
|
|
|
|
ffi->arg_svs[ffi->arg_count].l = v;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FMTC_LONGLONG:
|
2019-08-10 17:36:34 +00:00
|
|
|
#if (MOO_SIZEOF_LONG_LONG <= MOO_SIZEOF_LONG)
|
2019-08-09 18:19:13 +00:00
|
|
|
goto arg_as_long;
|
|
|
|
#else
|
|
|
|
if (_unsigned)
|
|
|
|
{
|
2019-08-10 17:36:34 +00:00
|
|
|
moo_uintmax_t v;
|
2019-11-01 09:15:53 +00:00
|
|
|
if (moo_inttouintmax_noseterr(moo, arg, &v) <= 0) goto inval_arg_value;
|
2019-08-09 18:19:13 +00:00
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
dcArgLongLong (ffi->dc, v);
|
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
ffi->arg_values[ffi->arg_count] = &ffi->arg_svs[ffi->arg_count].ull;
|
|
|
|
ffi->arg_svs[ffi->arg_count].ull = v;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-08-10 17:36:34 +00:00
|
|
|
moo_intmax_t v;
|
2019-11-01 09:15:53 +00:00
|
|
|
if (moo_inttointmax_noseterr(moo, arg, &v) == 0) goto inval_arg_value;
|
2019-08-09 18:19:13 +00:00
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
dcArgLongLong (ffi->dc, v);
|
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
ffi->arg_values[ffi->arg_count] = &ffi->arg_svs[ffi->arg_count].ll;
|
|
|
|
ffi->arg_svs[ffi->arg_count].ll = v;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case FMTC_BCS:
|
|
|
|
{
|
|
|
|
moo_bch_t* ptr;
|
|
|
|
|
2019-08-10 04:08:49 +00:00
|
|
|
if (!MOO_OBJ_IS_CHAR_POINTER(arg)) goto inval_arg_value;
|
2019-08-09 18:19:13 +00:00
|
|
|
|
|
|
|
#if defined(MOO_OOCH_IS_UCH)
|
|
|
|
ptr = moo_dupootobcharswithheadroom(moo, MOO_SIZEOF_VOID_P, MOO_OBJ_GET_CHAR_SLOT(arg), MOO_OBJ_GET_SIZE(arg), MOO_NULL);
|
|
|
|
if (!ptr) goto oops; /* out of system memory or conversion error - soft failure */
|
|
|
|
link_ca (ffi, ptr);
|
|
|
|
#else
|
|
|
|
ptr = MOO_OBJ_GET_CHAR_SLOT(arg);
|
|
|
|
/*ptr = moo_dupoochars(moo, MOO_OBJ_GET_CHAR_SLOT(arg), MOO_OBJ_GET_SIZE(arg));
|
|
|
|
if (!ptr) goto oops;*/ /* out of system memory or conversion error - soft failure */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
dcArgPointer (ffi->dc, ptr);
|
|
|
|
#elif defined(USE_LIBFFI)
|
2019-08-10 04:08:49 +00:00
|
|
|
ffi->arg_values[ffi->arg_count] = &ffi->arg_svs[ffi->arg_count].p;
|
|
|
|
ffi->arg_svs[ffi->arg_count].p = ptr;
|
2019-08-09 18:19:13 +00:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case FMTC_UCS:
|
|
|
|
{
|
|
|
|
moo_uch_t* ptr;
|
|
|
|
|
2019-08-10 04:08:49 +00:00
|
|
|
if (!MOO_OBJ_IS_CHAR_POINTER(arg)) goto inval_arg_value;
|
2019-08-09 18:19:13 +00:00
|
|
|
|
|
|
|
#if defined(MOO_OOCH_IS_UCH)
|
|
|
|
ptr = MOO_OBJ_GET_CHAR_SLOT(arg);
|
|
|
|
/*ptr = moo_dupoochars(moo, MOO_OBJ_GET_CHAR_SLOT(arg), MOO_OBJ_GET_SIZE(arg));
|
|
|
|
if (!ptr) goto oops; */ /* out of system memory or conversion error - soft failure */
|
|
|
|
#else
|
|
|
|
ptr = moo_dupootoucharswithheadroom(moo, MOO_SIZEOF_VOID_P, MOO_OBJ_GET_CHAR_SLOT(arg), MOO_OBJ_GET_SIZE(arg), MOO_NULL);
|
|
|
|
if (!ptr) goto oops; /* out of system memory or conversion error - soft failure */
|
|
|
|
link_ca (ffi, ptr);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
dcArgPointer (ffi->dc, ptr);
|
|
|
|
#elif defined(USE_LIBFFI)
|
2019-08-10 04:08:49 +00:00
|
|
|
ffi->arg_values[ffi->arg_count] = &ffi->arg_svs[ffi->arg_count].p;
|
|
|
|
ffi->arg_svs[ffi->arg_count].p = ptr;
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-08-11 10:11:54 +00:00
|
|
|
case FMTC_BLOB:
|
|
|
|
{
|
|
|
|
void* ptr;
|
|
|
|
|
|
|
|
if (MOO_OBJ_IS_BYTE_POINTER(arg)) goto inval_arg_value;
|
|
|
|
ptr = MOO_OBJ_GET_BYTE_SLOT(arg);
|
|
|
|
|
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
dcArgPointer (ffi->dc, ptr);
|
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
ffi->arg_values[ffi->arg_count] = &ffi->arg_svs[ffi->arg_count].p;
|
|
|
|
ffi->arg_svs[ffi->arg_count].p = ptr;
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-08-10 04:08:49 +00:00
|
|
|
case FMTC_POINTER:
|
|
|
|
{
|
|
|
|
void* ptr;
|
|
|
|
|
|
|
|
/* TODO: map nil to NULL? or should #\p0 be enough? */
|
|
|
|
if (!MOO_OOP_IS_SMPTR(arg)) goto inval_arg_value;
|
|
|
|
ptr = MOO_OOP_TO_SMPTR(arg);
|
|
|
|
|
|
|
|
#if defined(USE_DYNCALL)
|
2019-08-24 06:20:36 +00:00
|
|
|
dcArgPointer (ffi->dc, ptr);
|
2019-08-10 04:08:49 +00:00
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
ffi->arg_values[ffi->arg_count] = &ffi->arg_svs[ffi->arg_count].p;
|
|
|
|
ffi->arg_svs[ffi->arg_count].p = ptr;
|
2019-08-09 18:19:13 +00:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2019-09-26 07:48:02 +00:00
|
|
|
inval_sig_ch:
|
2019-08-09 18:19:13 +00:00
|
|
|
/* invalid argument signature specifier */
|
2019-09-26 07:48:02 +00:00
|
|
|
moo_seterrbfmt (moo, MOO_EINVAL, "invalid argument type signature - %jc", fmtc);
|
2019-08-10 04:08:49 +00:00
|
|
|
goto oops;
|
2019-08-09 18:19:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(USE_LIBFFI)
|
|
|
|
ffi->arg_types[ffi->arg_count] = ffi->fmtc_to_type[_unsigned][fmtc];
|
|
|
|
ffi->arg_count++;
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
|
2019-08-10 04:08:49 +00:00
|
|
|
inval_arg_value:
|
|
|
|
moo_seterrbfmt (moo, MOO_EINVAL, "invalid argument value - %O", arg);
|
2019-08-09 18:19:13 +00:00
|
|
|
|
|
|
|
oops:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-07 16:53:16 +00:00
|
|
|
static moo_pfrc_t pf_call (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs)
|
2017-01-09 13:41:11 +00:00
|
|
|
{
|
2019-08-09 18:19:13 +00:00
|
|
|
#if defined(USE_DYNCALL) || defined(USE_LIBFFI)
|
2017-02-14 08:29:30 +00:00
|
|
|
ffi_t* ffi;
|
2017-01-10 10:50:26 +00:00
|
|
|
moo_oop_t fun, sig, args;
|
2019-09-26 07:48:02 +00:00
|
|
|
moo_oow_t i, j, nfixedargs, _unsigned;
|
2017-01-11 15:33:03 +00:00
|
|
|
void* f;
|
|
|
|
moo_oop_oop_t arr;
|
2019-08-09 18:19:13 +00:00
|
|
|
int vbar = 0;
|
|
|
|
moo_ooch_t fmtc;
|
|
|
|
#if defined(USE_LIBFFI)
|
|
|
|
ffi_status fs;
|
|
|
|
#endif
|
2017-01-09 13:41:11 +00:00
|
|
|
|
2017-02-14 08:29:30 +00:00
|
|
|
ffi = (ffi_t*)moo_getobjtrailer(moo, MOO_STACK_GETRCV(moo, nargs), MOO_NULL);
|
2017-01-15 17:53:37 +00:00
|
|
|
|
2017-01-10 10:50:26 +00:00
|
|
|
fun = MOO_STACK_GETARG(moo, nargs, 0);
|
|
|
|
sig = MOO_STACK_GETARG(moo, nargs, 1);
|
|
|
|
args = MOO_STACK_GETARG(moo, nargs, 2);
|
|
|
|
|
2019-09-06 03:33:34 +00:00
|
|
|
if (moo_ptrtooow(moo, fun, (moo_oow_t*)&f) <= -1) goto softfail;
|
2018-12-22 08:37:18 +00:00
|
|
|
|
2017-01-16 14:42:39 +00:00
|
|
|
/* the signature must not be empty. at least the return type must be
|
|
|
|
* specified */
|
2017-02-14 08:29:30 +00:00
|
|
|
if (!MOO_OBJ_IS_CHAR_POINTER(sig) || MOO_OBJ_GET_SIZE(sig) <= 0) goto inval;
|
2017-01-09 13:41:11 +00:00
|
|
|
|
2017-01-11 15:33:03 +00:00
|
|
|
#if 0
|
2017-01-12 16:33:26 +00:00
|
|
|
/* TODO: check if arr is a kind of array??? or check if it's indexed */
|
|
|
|
if (MOO_OBJ_GET_SIZE(sig) > 1 && MOO_CLASSOF(moo,args) != moo->_array) goto inval;
|
2017-01-11 15:33:03 +00:00
|
|
|
#endif
|
2017-01-09 13:41:11 +00:00
|
|
|
|
2017-01-11 15:33:03 +00:00
|
|
|
arr = (moo_oop_oop_t)args;
|
2019-04-08 01:18:00 +00:00
|
|
|
/*MOO_DEBUG2 (moo, "<ffi.call> %p in %p\n", f, ffi->handle);*/
|
2017-01-09 13:41:11 +00:00
|
|
|
|
2019-08-09 18:19:13 +00:00
|
|
|
#if defined(USE_DYNCALL)
|
2017-02-14 08:29:30 +00:00
|
|
|
dcMode (ffi->dc, DC_CALL_C_DEFAULT);
|
|
|
|
dcReset (ffi->dc);
|
2017-01-11 15:33:03 +00:00
|
|
|
|
2019-08-09 18:19:13 +00:00
|
|
|
for (i = 0; i < MOO_OBJ_GET_SIZE(sig); i++)
|
2017-01-11 15:33:03 +00:00
|
|
|
{
|
2019-08-09 18:19:13 +00:00
|
|
|
fmtc = MOO_OBJ_GET_CHAR_VAL(sig, i);
|
|
|
|
if (fmtc == '>') break; /* end of arguments. start of return type */
|
|
|
|
if (fmtc == '|')
|
|
|
|
{
|
|
|
|
dcMode (ffi->dc, DC_CALL_C_ELLIPSIS); /* variadic. for arguments before ... */
|
|
|
|
/* the error code should be DC_ERROR_UNSUPPORTED_MODE */
|
|
|
|
if (dcGetError(ffi->dc) != DC_ERROR_NONE) goto noimpl;
|
|
|
|
dcReset (ffi->dc);
|
|
|
|
break;
|
|
|
|
}
|
2017-01-11 15:33:03 +00:00
|
|
|
}
|
2019-08-09 18:19:13 +00:00
|
|
|
#else
|
|
|
|
ffi->arg_count = 0;
|
|
|
|
#endif
|
2017-01-09 13:41:11 +00:00
|
|
|
|
2019-04-07 17:54:44 +00:00
|
|
|
/* check argument signature */
|
2019-09-26 07:48:02 +00:00
|
|
|
for (i = 0, j = 0, nfixedargs = 0, _unsigned = 0; i < MOO_OBJ_GET_SIZE(sig); i++)
|
2017-01-11 15:33:03 +00:00
|
|
|
{
|
2018-12-19 15:47:52 +00:00
|
|
|
fmtc = MOO_OBJ_GET_CHAR_VAL(sig, i);
|
2019-08-11 10:11:54 +00:00
|
|
|
if (fmtc == ' ')
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2019-08-09 18:19:13 +00:00
|
|
|
if (fmtc == '>')
|
2017-01-09 13:41:11 +00:00
|
|
|
{
|
2017-01-11 15:33:03 +00:00
|
|
|
i++;
|
2019-08-09 18:19:13 +00:00
|
|
|
if (!vbar) nfixedargs = j;
|
2017-01-11 15:33:03 +00:00
|
|
|
break;
|
2017-01-09 13:41:11 +00:00
|
|
|
}
|
2017-01-11 15:33:03 +00:00
|
|
|
else if (fmtc == '|')
|
2017-01-09 13:41:11 +00:00
|
|
|
{
|
2019-08-09 18:19:13 +00:00
|
|
|
if (!vbar)
|
2017-01-09 13:41:11 +00:00
|
|
|
{
|
2019-08-09 18:19:13 +00:00
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
dcMode (ffi->dc, DC_CALL_C_ELLIPSIS_VARARGS); /* start of arguments that fall to the ... part */
|
2017-01-12 16:33:26 +00:00
|
|
|
/* the error code should be DC_ERROR_UNSUPPORTED_MODE */
|
2017-02-14 08:29:30 +00:00
|
|
|
if (dcGetError(ffi->dc) != DC_ERROR_NONE) goto noimpl;
|
2019-08-09 18:19:13 +00:00
|
|
|
#endif
|
|
|
|
nfixedargs = j;
|
|
|
|
vbar = 1;
|
2017-01-09 13:41:11 +00:00
|
|
|
}
|
2017-01-11 15:33:03 +00:00
|
|
|
continue;
|
|
|
|
}
|
2019-09-26 07:48:02 +00:00
|
|
|
else if (fmtc == 'u')
|
|
|
|
{
|
|
|
|
_unsigned = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2017-01-09 13:41:11 +00:00
|
|
|
|
2019-08-09 18:19:13 +00:00
|
|
|
/* more items in signature than the actual argument */
|
2017-01-12 16:33:26 +00:00
|
|
|
if (j >= MOO_OBJ_GET_SIZE(arr)) goto inval;
|
2017-01-09 13:41:11 +00:00
|
|
|
|
2019-09-26 07:48:02 +00:00
|
|
|
if (add_ffi_arg(moo, ffi, fmtc, _unsigned, MOO_OBJ_GET_OOP_VAL(arr, j)) <= -1) goto softfail;
|
|
|
|
_unsigned = 0;
|
2019-08-09 18:19:13 +00:00
|
|
|
j++;
|
|
|
|
}
|
2017-01-09 13:41:11 +00:00
|
|
|
|
2019-08-11 14:58:04 +00:00
|
|
|
while (i < MOO_OBJ_GET_SIZE(sig) && MOO_OBJ_GET_CHAR_VAL(sig, i) == ' ') i++; /* skip all spaces after > */
|
2019-08-09 18:19:13 +00:00
|
|
|
fmtc = (i >= MOO_OBJ_GET_SIZE(sig)? FMTC_NULL: MOO_OBJ_GET_CHAR_VAL(sig, i));
|
2020-12-27 16:57:54 +00:00
|
|
|
if (fmtc == 'u')
|
|
|
|
{
|
|
|
|
_unsigned = 1;
|
|
|
|
i++;
|
|
|
|
fmtc = (i >= MOO_OBJ_GET_SIZE(sig)? FMTC_NULL: MOO_OBJ_GET_CHAR_VAL(sig, i));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_unsigned = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-09 18:19:13 +00:00
|
|
|
#if defined(USE_LIBFFI)
|
2020-12-27 16:57:54 +00:00
|
|
|
if (!ffi->fmtc_to_type[0][fmtc]) goto inval; /* invalid return value signature */
|
|
|
|
|
2019-08-09 18:19:13 +00:00
|
|
|
fs = (nfixedargs == j)? ffi_prep_cif(&ffi->cif, FFI_DEFAULT_ABI, j, ffi->fmtc_to_type[0][fmtc], ffi->arg_types):
|
|
|
|
ffi_prep_cif_var(&ffi->cif, FFI_DEFAULT_ABI, nfixedargs, j, ffi->fmtc_to_type[0][fmtc], ffi->arg_types);
|
|
|
|
if (fs != FFI_OK)
|
|
|
|
{
|
|
|
|
moo_seterrnum (moo, MOO_ESYSERR);
|
|
|
|
goto softfail;
|
2017-01-11 15:33:03 +00:00
|
|
|
}
|
|
|
|
|
2019-08-09 18:19:13 +00:00
|
|
|
ffi_call (&ffi->cif, FFI_FN(f), &ffi->ret_sv, ffi->arg_values);
|
|
|
|
#endif
|
2017-01-11 15:33:03 +00:00
|
|
|
|
2019-09-26 07:48:02 +00:00
|
|
|
|
2019-04-07 17:54:44 +00:00
|
|
|
/* check the return value type in signature */
|
2019-08-09 18:19:13 +00:00
|
|
|
switch (fmtc)
|
2017-01-11 15:33:03 +00:00
|
|
|
{
|
|
|
|
/* TODO: support more types... */
|
|
|
|
/* TODO: proper return value conversion */
|
2019-09-26 07:48:02 +00:00
|
|
|
case FMTC_INT8:
|
|
|
|
{
|
|
|
|
moo_oop_t r;
|
|
|
|
if (_unsigned)
|
|
|
|
{
|
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
r = moo_oowtoint(moo, __dcCallInt8(ffi->dc, f));
|
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
r = moo_oowtoint(moo, ffi->ret_sv.ui8);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
r = moo_ooitoint(moo, __dcCallInt8(ffi->dc, f));
|
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
r = moo_ooitoint(moo, ffi->ret_sv.i8);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (!r) goto hardfail;
|
|
|
|
|
|
|
|
MOO_STACK_SETRET (moo, nargs, r);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case FMTC_INT16:
|
|
|
|
{
|
|
|
|
moo_oop_t r;
|
|
|
|
if (_unsigned)
|
|
|
|
{
|
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
r = moo_oowtoint(moo, __dcCallInt16(ffi->dc, f));
|
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
r = moo_oowtoint(moo, ffi->ret_sv.ui16);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
r = moo_ooitoint(moo, __dcCallInt16(ffi->dc, f));
|
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
r = moo_ooitoint(moo, ffi->ret_sv.i16);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (!r) goto hardfail;
|
|
|
|
|
|
|
|
MOO_STACK_SETRET (moo, nargs, r);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case FMTC_INT32:
|
|
|
|
{
|
|
|
|
moo_oop_t r;
|
|
|
|
if (_unsigned)
|
|
|
|
{
|
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
r = moo_oowtoint(moo, __dcCallInt32(ffi->dc, f));
|
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
r = moo_oowtoint(moo, ffi->ret_sv.ui32);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
r = moo_ooitoint(moo, __dcCallInt32(ffi->dc, f));
|
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
r = moo_ooitoint(moo, ffi->ret_sv.i32);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (!r) goto hardfail;
|
|
|
|
|
|
|
|
MOO_STACK_SETRET (moo, nargs, r);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case FMTC_INT64:
|
|
|
|
{
|
|
|
|
#if (MOO_SIZEOF_INT64_T > 0)
|
|
|
|
moo_oop_t r;
|
|
|
|
if (_unsigned)
|
|
|
|
{
|
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
r = moo_oowtoint(moo, __dcCallInt64(ffi->dc, f));
|
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
r = moo_oowtoint(moo, ffi->ret_sv.ui64);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
r = moo_ooitoint(moo, __dcCallInt64(ffi->dc, f));
|
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
r = moo_ooitoint(moo, ffi->ret_sv.i64);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (!r) goto hardfail;
|
|
|
|
|
|
|
|
MOO_STACK_SETRET (moo, nargs, r);
|
|
|
|
break;
|
|
|
|
#else
|
|
|
|
moo_seterrbfmt (moo, MOO_EINVAL, "invalid return type signature - %jc", fmtc);
|
|
|
|
goto softfail;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-09 18:19:13 +00:00
|
|
|
case FMTC_CHAR:
|
2017-01-11 15:33:03 +00:00
|
|
|
{
|
2019-08-09 18:19:13 +00:00
|
|
|
#if defined(USE_DYNCALL)
|
2018-12-20 16:08:56 +00:00
|
|
|
char r = dcCallChar(ffi->dc, f);
|
2017-01-11 15:33:03 +00:00
|
|
|
MOO_STACK_SETRET (moo, nargs, MOO_CHAR_TO_OOP(r));
|
2019-08-09 18:19:13 +00:00
|
|
|
#elif defined(USE_LIBFFI)
|
2019-09-26 07:48:02 +00:00
|
|
|
if (_unsigned)
|
|
|
|
{
|
|
|
|
MOO_STACK_SETRET (moo, nargs, MOO_CHAR_TO_OOP(ffi->ret_sv.uc));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MOO_STACK_SETRET (moo, nargs, MOO_CHAR_TO_OOP(ffi->ret_sv.c));
|
|
|
|
}
|
2019-08-09 18:19:13 +00:00
|
|
|
#endif
|
2017-01-11 15:33:03 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-08-09 18:19:13 +00:00
|
|
|
case FMTC_SHORT:
|
2017-01-11 15:33:03 +00:00
|
|
|
{
|
|
|
|
moo_oop_t r;
|
2019-09-26 07:48:02 +00:00
|
|
|
if (_unsigned)
|
|
|
|
{
|
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
r = moo_oowtoint(moo, dcCallShort(ffi->dc, f));
|
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
r = moo_oowtoint(moo, ffi->ret_sv.uh);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
r = moo_ooitoint(moo, dcCallShort(ffi->dc, f));
|
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
r = moo_ooitoint(moo, ffi->ret_sv.h);
|
|
|
|
#endif
|
|
|
|
}
|
2019-08-09 18:19:13 +00:00
|
|
|
if (!r) goto hardfail;
|
2019-09-26 07:48:02 +00:00
|
|
|
|
2019-08-09 18:19:13 +00:00
|
|
|
MOO_STACK_SETRET (moo, nargs, r);
|
|
|
|
break;
|
|
|
|
}
|
2017-01-11 15:33:03 +00:00
|
|
|
|
2019-08-09 18:19:13 +00:00
|
|
|
case FMTC_INT:
|
|
|
|
{
|
|
|
|
moo_oop_t r;
|
2019-09-26 07:48:02 +00:00
|
|
|
if (_unsigned)
|
|
|
|
{
|
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
r = moo_oowtoint(moo, dcCallInt(ffi->dc, f));
|
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
r = moo_oowtoint(moo, ffi->ret_sv.ui);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
r = moo_ooitoint(moo, dcCallInt(ffi->dc, f));
|
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
r = moo_ooitoint(moo, ffi->ret_sv.i);
|
|
|
|
#endif
|
|
|
|
}
|
2017-01-12 16:33:26 +00:00
|
|
|
if (!r) goto hardfail;
|
2017-01-11 15:33:03 +00:00
|
|
|
MOO_STACK_SETRET (moo, nargs, r);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-08-09 18:19:13 +00:00
|
|
|
case FMTC_LONG:
|
2017-01-11 15:33:03 +00:00
|
|
|
{
|
|
|
|
moo_oop_t r;
|
2017-01-15 17:53:37 +00:00
|
|
|
ret_as_long:
|
2019-09-26 07:48:02 +00:00
|
|
|
if (_unsigned)
|
|
|
|
{
|
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
r = moo_oowtoint(moo, dcCallLong(ffi->dc, f));
|
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
r = moo_oowtoint(moo, ffi->ret_sv.ul);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
r = moo_ooitoint(moo, dcCallLong(ffi->dc, f));
|
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
r = moo_ooitoint(moo, ffi->ret_sv.l);
|
|
|
|
#endif
|
|
|
|
}
|
2017-01-12 16:33:26 +00:00
|
|
|
if (!r) goto hardfail;
|
2017-01-11 15:33:03 +00:00
|
|
|
MOO_STACK_SETRET (moo, nargs, r);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-08-09 18:19:13 +00:00
|
|
|
case FMTC_LONGLONG:
|
2017-01-11 15:33:03 +00:00
|
|
|
{
|
2019-08-10 17:36:34 +00:00
|
|
|
#if (MOO_SIZEOF_LONG_LONG <= MOO_SIZEOF_LONG)
|
2019-08-09 18:19:13 +00:00
|
|
|
goto ret_as_long;
|
|
|
|
#else
|
2017-01-15 17:53:37 +00:00
|
|
|
moo_oop_t r;
|
2019-09-26 07:48:02 +00:00
|
|
|
if (_unsigned)
|
|
|
|
{
|
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
r = moo_uintmaxtoint(moo, dcCallLongLong(ffi->dc, f));
|
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
r = moo_uintmaxtoint(moo, ffi->ret_sv.ull);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
r = moo_intmaxtoint(moo, dcCallLongLong(ffi->dc, f));
|
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
r = moo_intmaxtoint(moo, ffi->ret_sv.ll);
|
|
|
|
#endif
|
|
|
|
}
|
2017-01-15 17:53:37 +00:00
|
|
|
if (!r) goto hardfail;
|
2017-01-11 15:33:03 +00:00
|
|
|
MOO_STACK_SETRET (moo, nargs, r);
|
|
|
|
break;
|
2017-01-15 17:53:37 +00:00
|
|
|
#endif
|
2017-01-11 15:33:03 +00:00
|
|
|
}
|
|
|
|
|
2019-08-09 18:19:13 +00:00
|
|
|
case FMTC_BCS:
|
2017-01-11 15:33:03 +00:00
|
|
|
{
|
|
|
|
moo_oop_t s;
|
2017-01-12 16:33:26 +00:00
|
|
|
moo_bch_t* r;
|
2017-01-09 13:41:11 +00:00
|
|
|
|
2019-08-09 18:19:13 +00:00
|
|
|
#if defined(USE_DYNCALL)
|
2019-04-08 04:48:57 +00:00
|
|
|
r = dcCallPointer(ffi->dc, f);
|
2019-08-10 04:08:49 +00:00
|
|
|
#elif defined(USE_LIBFFI)
|
2019-08-09 18:19:13 +00:00
|
|
|
r = ffi->ret_sv.p;
|
|
|
|
#endif
|
2017-01-11 15:33:03 +00:00
|
|
|
|
2017-01-12 16:33:26 +00:00
|
|
|
#if defined(MOO_OOCH_IS_UCH)
|
2019-04-08 04:48:57 +00:00
|
|
|
s = moo_makestringwithbchars(moo, r, moo_count_bcstr(r));
|
2017-01-12 16:33:26 +00:00
|
|
|
#else
|
2018-04-09 02:16:50 +00:00
|
|
|
s = moo_makestring(moo, r, moo_count_bcstr(r));
|
2017-01-12 16:33:26 +00:00
|
|
|
#endif
|
2017-01-15 17:53:37 +00:00
|
|
|
if (!s)
|
|
|
|
{
|
|
|
|
if (moo->errnum == MOO_EOOMEM) goto hardfail; /* out of object memory - hard failure*/
|
|
|
|
goto softfail;
|
|
|
|
}
|
2017-01-12 16:33:26 +00:00
|
|
|
|
|
|
|
MOO_STACK_SETRET (moo, nargs, s);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-08-09 18:19:13 +00:00
|
|
|
case FMTC_UCS:
|
2017-01-12 16:33:26 +00:00
|
|
|
{
|
|
|
|
moo_oop_t s;
|
|
|
|
moo_uch_t* r;
|
|
|
|
|
2019-08-09 18:19:13 +00:00
|
|
|
#if defined(USE_DYNCALL)
|
2019-04-08 04:48:57 +00:00
|
|
|
r = dcCallPointer(ffi->dc, f);
|
2019-08-10 04:08:49 +00:00
|
|
|
#elif defined(USE_LIBFFI)
|
2019-08-09 18:19:13 +00:00
|
|
|
r = ffi->ret_sv.p;
|
|
|
|
#endif
|
2017-01-12 16:33:26 +00:00
|
|
|
|
|
|
|
#if defined(MOO_OOCH_IS_UCH)
|
2018-04-09 02:16:50 +00:00
|
|
|
s = moo_makestring(moo, r, moo_count_ucstr(r));
|
2017-01-12 16:33:26 +00:00
|
|
|
#else
|
2018-04-09 02:16:50 +00:00
|
|
|
s = moo_makestringwithuchars(moo, r, moo_count_ucstr(r));
|
2017-01-12 16:33:26 +00:00
|
|
|
#endif
|
2017-01-15 17:53:37 +00:00
|
|
|
if (!s)
|
|
|
|
{
|
|
|
|
if (moo->errnum == MOO_EOOMEM) goto hardfail; /* out of object memory - hard failure*/
|
|
|
|
goto softfail;
|
|
|
|
}
|
2017-01-11 15:33:03 +00:00
|
|
|
|
|
|
|
MOO_STACK_SETRET (moo, nargs, s);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-08-11 10:11:54 +00:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
case FMTC_BLOB:
|
|
|
|
{
|
|
|
|
/* blob as a return type isn't sufficient as it lacks the size information.
|
|
|
|
* blob as an argument is just a pointer and the size can be yet another argument.
|
|
|
|
* it there a good way to represent this here?... TODO: */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-08-10 04:08:49 +00:00
|
|
|
case FMTC_POINTER:
|
|
|
|
{
|
|
|
|
void* r;
|
|
|
|
|
|
|
|
#if defined(USE_DYNCALL)
|
|
|
|
r = dcCallPointer(ffi->dc, f);
|
|
|
|
#elif defined(USE_LIBFFI)
|
|
|
|
r = ffi->ret_sv.p;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!MOO_IN_SMPTR_RANGE(r))
|
|
|
|
{
|
|
|
|
/* the returned pointer is not aligned */
|
2019-09-05 08:45:04 +00:00
|
|
|
goto inval;
|
2019-08-10 04:08:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MOO_STACK_SETRET (moo, nargs, MOO_SMPTR_TO_OOP(r));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-01-11 15:33:03 +00:00
|
|
|
default:
|
2019-08-09 18:19:13 +00:00
|
|
|
#if defined(USE_DYNCALL)
|
2017-02-14 08:29:30 +00:00
|
|
|
dcCallVoid (ffi->dc, f);
|
2019-08-09 18:19:13 +00:00
|
|
|
#endif
|
2017-01-11 15:33:03 +00:00
|
|
|
MOO_STACK_SETRETTORCV (moo, nargs);
|
|
|
|
break;
|
2017-01-09 13:41:11 +00:00
|
|
|
}
|
|
|
|
|
2017-02-14 08:29:30 +00:00
|
|
|
free_linked_cas (moo, ffi);
|
2017-01-09 13:41:11 +00:00
|
|
|
return MOO_PF_SUCCESS;
|
2017-01-10 10:50:26 +00:00
|
|
|
|
2017-01-12 16:33:26 +00:00
|
|
|
noimpl:
|
|
|
|
moo_seterrnum (moo, MOO_ENOIMPL);
|
|
|
|
goto softfail;
|
|
|
|
|
|
|
|
inval:
|
|
|
|
moo_seterrnum (moo, MOO_EINVAL);
|
|
|
|
goto softfail;
|
|
|
|
|
|
|
|
softfail:
|
2017-02-14 08:29:30 +00:00
|
|
|
free_linked_cas (moo, ffi);
|
2017-04-03 05:43:50 +00:00
|
|
|
MOO_STACK_SETRETTOERRNUM (moo, nargs);
|
2017-01-10 10:50:26 +00:00
|
|
|
return MOO_PF_SUCCESS;
|
|
|
|
|
2017-01-12 16:33:26 +00:00
|
|
|
hardfail:
|
2017-02-14 08:29:30 +00:00
|
|
|
free_linked_cas (moo, ffi);
|
2017-01-11 15:33:03 +00:00
|
|
|
return MOO_PF_HARD_FAILURE;
|
|
|
|
|
2017-01-09 13:41:11 +00:00
|
|
|
#else
|
2017-01-10 10:50:26 +00:00
|
|
|
moo_seterrnum (moo, MOO_ENOIMPL);
|
2017-04-03 05:43:50 +00:00
|
|
|
MOO_STACK_SETRETTOERRNUM (moo, nargs);
|
2017-01-10 14:27:31 +00:00
|
|
|
return MOO_PF_SUCCESS;
|
2017-01-09 13:41:11 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-05-07 16:53:16 +00:00
|
|
|
static moo_pfrc_t pf_getsym (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs)
|
2017-01-09 13:41:11 +00:00
|
|
|
{
|
2017-02-14 08:29:30 +00:00
|
|
|
ffi_t* ffi;
|
2019-09-05 08:45:04 +00:00
|
|
|
moo_oop_t name, ret;
|
2017-01-09 13:41:11 +00:00
|
|
|
void* sym;
|
|
|
|
|
2017-03-30 14:59:55 +00:00
|
|
|
MOO_ASSERT (moo, nargs == 1);
|
2017-01-09 13:41:11 +00:00
|
|
|
|
2017-02-14 08:29:30 +00:00
|
|
|
ffi = (ffi_t*)moo_getobjtrailer(moo, MOO_STACK_GETRCV(moo, nargs), MOO_NULL);
|
2017-01-10 13:56:19 +00:00
|
|
|
name = MOO_STACK_GETARG(moo, nargs, 0);
|
2017-01-09 13:41:11 +00:00
|
|
|
|
2017-02-14 08:29:30 +00:00
|
|
|
if (!MOO_OBJ_IS_CHAR_POINTER(name))
|
2017-01-09 13:41:11 +00:00
|
|
|
{
|
2017-01-10 10:50:26 +00:00
|
|
|
moo_seterrnum (moo, MOO_EINVAL);
|
2017-01-12 16:33:26 +00:00
|
|
|
goto softfail;
|
2017-01-09 13:41:11 +00:00
|
|
|
}
|
|
|
|
|
2017-01-10 10:50:26 +00:00
|
|
|
if (!moo->vmprim.dl_getsym)
|
2017-01-09 13:41:11 +00:00
|
|
|
{
|
2017-01-10 10:50:26 +00:00
|
|
|
moo_seterrnum (moo, MOO_ENOIMPL);
|
2017-01-12 16:33:26 +00:00
|
|
|
goto softfail;
|
2017-01-09 13:41:11 +00:00
|
|
|
}
|
|
|
|
|
2018-12-20 16:08:56 +00:00
|
|
|
sym = moo->vmprim.dl_getsym(moo, ffi->handle, MOO_OBJ_GET_CHAR_SLOT(name));
|
2017-01-12 16:33:26 +00:00
|
|
|
if (!sym) goto softfail;
|
2017-01-10 10:50:26 +00:00
|
|
|
|
2018-12-12 15:05:18 +00:00
|
|
|
MOO_DEBUG4 (moo, "<ffi.getsym> %.*js => %p in %p\n", MOO_OBJ_GET_SIZE(name), MOO_OBJ_GET_CHAR_SLOT(name), sym, ffi->handle);
|
2017-01-10 13:56:19 +00:00
|
|
|
|
2019-09-05 08:45:04 +00:00
|
|
|
ret = moo_oowtoptr(moo, (moo_oow_t)sym);
|
|
|
|
if (!ret) goto softfail;
|
|
|
|
|
|
|
|
MOO_STACK_SETRET (moo, nargs, ret);
|
2017-01-10 10:50:26 +00:00
|
|
|
return MOO_PF_SUCCESS;
|
|
|
|
|
2017-01-12 16:33:26 +00:00
|
|
|
softfail:
|
2017-04-03 05:43:50 +00:00
|
|
|
MOO_STACK_SETRETTOERRNUM (moo, nargs);
|
2017-01-10 10:50:26 +00:00
|
|
|
return MOO_PF_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
|
|
|
|
#define C MOO_METHOD_CLASS
|
|
|
|
#define I MOO_METHOD_INSTANCE
|
|
|
|
|
2017-03-30 14:59:55 +00:00
|
|
|
#define MA MOO_TYPE_MAX(moo_oow_t)
|
|
|
|
|
2017-02-14 10:25:26 +00:00
|
|
|
static moo_pfinfo_t pfinfos[] =
|
2017-01-10 10:50:26 +00:00
|
|
|
{
|
2019-10-28 12:55:32 +00:00
|
|
|
{ I, "call", { pf_call, 3, 3 } },
|
|
|
|
{ I, "close", { pf_close, 0, 0 } },
|
|
|
|
{ I, "getsym", { pf_getsym, 1, 1 } },
|
|
|
|
{ I, "open", { pf_open, 1, 1 } }
|
2017-01-10 10:50:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
|
2017-02-15 11:57:24 +00:00
|
|
|
static int import (moo_t* moo, moo_mod_t* mod, moo_oop_class_t _class)
|
2017-01-10 10:50:26 +00:00
|
|
|
{
|
2018-05-07 16:53:16 +00:00
|
|
|
if (moo_setclasstrsize(moo, _class, MOO_SIZEOF(ffi_t), MOO_NULL) <= -1) return -1;
|
2017-03-30 14:59:55 +00:00
|
|
|
return 0;
|
2017-01-10 10:50:26 +00:00
|
|
|
}
|
|
|
|
|
2019-10-25 08:44:05 +00:00
|
|
|
static moo_pfbase_t* querypf (moo_t* moo, moo_mod_t* mod, const moo_ooch_t* name, moo_oow_t namelen)
|
2017-01-10 10:50:26 +00:00
|
|
|
{
|
2017-03-31 14:21:22 +00:00
|
|
|
return moo_findpfbase (moo, pfinfos, MOO_COUNTOF(pfinfos), name, namelen);
|
2017-01-10 10:50:26 +00:00
|
|
|
}
|
2017-01-09 13:41:11 +00:00
|
|
|
|
2017-01-10 10:50:26 +00:00
|
|
|
static void unload (moo_t* moo, moo_mod_t* mod)
|
|
|
|
{
|
2017-01-10 13:56:19 +00:00
|
|
|
/* TODO: anything? close open open dll handles? For that, pf_open must store the value it returns to mod->ctx or somewhere..*/
|
2019-08-10 04:08:49 +00:00
|
|
|
/* TODO: track all opened shared objects... clear all external memories/resources created but not cleared (for lacking the call to 'close') */
|
2017-01-10 10:50:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int moo_mod_ffi (moo_t* moo, moo_mod_t* mod)
|
|
|
|
{
|
|
|
|
mod->import = import;
|
2019-10-25 08:44:05 +00:00
|
|
|
mod->querypf = querypf;
|
|
|
|
mod->querypv = MOO_NULL;
|
2017-01-10 10:50:26 +00:00
|
|
|
mod->unload = unload;
|
|
|
|
mod->ctx = MOO_NULL;
|
|
|
|
return 0;
|
2017-01-09 13:41:11 +00:00
|
|
|
}
|