added more code to ffi

This commit is contained in:
hyunghwan.chung 2017-01-11 15:33:03 +00:00
parent f23486468d
commit d130ec02ec
5 changed files with 259 additions and 206 deletions

View File

@ -88,7 +88,8 @@ class MyObject(Object)
(ffi isError) (ffi isError)
ifTrue: [System logNl: 'cannot open libc.so' ] ifTrue: [System logNl: 'cannot open libc.so' ]
ifFalse: [ ifFalse: [
(ffi call: #getpid signature: 'i' arguments: nil) dump. (ffi call: #getpid signature: ')i' arguments: nil) dump.
(ffi call: #printf signature: 's|iis)i' arguments: #(S'A=>%d B=>%d Hello, world %s\n' 1 2 'fly away')) dump.
ffi close. ffi close.
] ]
} }

View File

@ -29,14 +29,14 @@
#include "moo-cmn.h" #include "moo-cmn.h"
/**@file /**\file
* This file provides a red-black tree encapsulated in the #moo_rbt_t type that * This file provides a red-black tree encapsulated in the #moo_rbt_t type that
* implements a self-balancing binary search tree.Its interface is very close * implements a self-balancing binary search tree.Its interface is very close
* to #moo_htb_t. * to #moo_htb_t.
* *
* This sample code adds a series of keys and values and print them * This sample code adds a series of keys and values and print them
* in descending key order. * in descending key order.
* @code * \code
* #include <moo/cmn/rbt.h> * #include <moo/cmn/rbt.h>
* #include <moo/cmn/mem.h> * #include <moo/cmn/mem.h>
* #include <moo/cmn/sio.h> * #include <moo/cmn/sio.h>
@ -67,7 +67,7 @@
* moo_rbt_close (s1); * moo_rbt_close (s1);
* return 0; * return 0;
* } * }
* @endcode * \endcode
*/ */
typedef struct moo_rbt_t moo_rbt_t; typedef struct moo_rbt_t moo_rbt_t;
@ -99,9 +99,9 @@ typedef enum moo_rbt_id_t moo_rbt_id_t;
* The moo_rbt_copier_t type defines a pair contruction callback. * The moo_rbt_copier_t type defines a pair contruction callback.
*/ */
typedef void* (*moo_rbt_copier_t) ( typedef void* (*moo_rbt_copier_t) (
moo_rbt_t* rbt /* red-black tree */, moo_rbt_t* rbt /**< red-black tree */,
void* dptr /* pointer to a key or a value */, void* dptr /**< pointer to a key or a value */,
moo_oow_t dlen /* length of a key or a value */ moo_oow_t dlen /**< length of a key or a value */
); );
/** /**
@ -152,8 +152,8 @@ typedef moo_rbt_walk_t (*moo_rbt_walker_t) (
/** /**
* The moo_rbt_cbserter_t type defines a callback function for moo_rbt_cbsert(). * The moo_rbt_cbserter_t type defines a callback function for moo_rbt_cbsert().
* The moo_rbt_cbserter() function calls it to allocate a new pair for the * The moo_rbt_cbserter() function calls it to allocate a new pair for the
* key pointed to by @a kptr of the length @a klen and the callback context * key pointed to by \a kptr of the length \a klen and the callback context
* @a ctx. The second parameter @a pair is passed the pointer to the existing * \a ctx. The second parameter \a pair is passed the pointer to the existing
* pair for the key or #MOO_NULL in case of no existing key. The callback * pair for the key or #MOO_NULL in case of no existing key. The callback
* must return a pointer to a new or a reallocated pair. When reallocating the * must return a pointer to a new or a reallocated pair. When reallocating the
* existing pair, this callback must destroy the existing pair and return the * existing pair, this callback must destroy the existing pair and return the
@ -290,7 +290,7 @@ MOO_EXPORT const moo_rbt_style_t* moo_getrbtstyle (
/** /**
* The moo_rbt_open() function creates a red-black tree. * The moo_rbt_open() function creates a red-black tree.
* @return moo_rbt_t pointer on success, MOO_NULL on failure. * \return moo_rbt_t pointer on success, MOO_NULL on failure.
*/ */
MOO_EXPORT moo_rbt_t* moo_rbt_open ( MOO_EXPORT moo_rbt_t* moo_rbt_open (
moo_t* moo, moo_t* moo,
@ -357,7 +357,7 @@ MOO_EXPORT moo_oow_t moo_rbt_getsize (
* The moo_rbt_search() function searches red-black tree to find a pair with a * The moo_rbt_search() function searches red-black tree to find a pair with a
* matching key. It returns the pointer to the pair found. If it fails * matching key. It returns the pointer to the pair found. If it fails
* to find one, it returns MOO_NULL. * to find one, it returns MOO_NULL.
* @return pointer to the pair with a maching key, * \return pointer to the pair with a maching key,
* or MOO_NULL if no match is found. * or MOO_NULL if no match is found.
*/ */
MOO_EXPORT moo_rbt_pair_t* moo_rbt_search ( MOO_EXPORT moo_rbt_pair_t* moo_rbt_search (
@ -371,7 +371,7 @@ MOO_EXPORT moo_rbt_pair_t* moo_rbt_search (
* matching key. If one is found, it updates the pair. Otherwise, it inserts * matching key. If one is found, it updates the pair. Otherwise, it inserts
* a new pair with the key and the value given. It returns the pointer to the * a new pair with the key and the value given. It returns the pointer to the
* pair updated or inserted. * pair updated or inserted.
* @return a pointer to the updated or inserted pair on success, * \return a pointer to the updated or inserted pair on success,
* MOO_NULL on failure. * MOO_NULL on failure.
*/ */
MOO_EXPORT moo_rbt_pair_t* moo_rbt_upsert ( MOO_EXPORT moo_rbt_pair_t* moo_rbt_upsert (
@ -386,7 +386,7 @@ MOO_EXPORT moo_rbt_pair_t* moo_rbt_upsert (
* The moo_rbt_ensert() function inserts a new pair with the key and the value * The moo_rbt_ensert() function inserts a new pair with the key and the value
* given. If there exists a pair with the key given, the function returns * given. If there exists a pair with the key given, the function returns
* the pair containing the key. * the pair containing the key.
* @return pointer to a pair on success, MOO_NULL on failure. * \return pointer to a pair on success, MOO_NULL on failure.
*/ */
MOO_EXPORT moo_rbt_pair_t* moo_rbt_ensert ( MOO_EXPORT moo_rbt_pair_t* moo_rbt_ensert (
moo_rbt_t* rbt, /**< red-black tree */ moo_rbt_t* rbt, /**< red-black tree */
@ -400,7 +400,7 @@ MOO_EXPORT moo_rbt_pair_t* moo_rbt_ensert (
* The moo_rbt_insert() function inserts a new pair with the key and the value * The moo_rbt_insert() function inserts a new pair with the key and the value
* given. If there exists a pair with the key given, the function returns * given. If there exists a pair with the key given, the function returns
* MOO_NULL without channging the value. * MOO_NULL without channging the value.
* @return pointer to the pair created on success, MOO_NULL on failure. * \return pointer to the pair created on success, MOO_NULL on failure.
*/ */
MOO_EXPORT moo_rbt_pair_t* moo_rbt_insert ( MOO_EXPORT moo_rbt_pair_t* moo_rbt_insert (
moo_rbt_t* rbt, /**< red-black tree */ moo_rbt_t* rbt, /**< red-black tree */
@ -413,7 +413,7 @@ MOO_EXPORT moo_rbt_pair_t* moo_rbt_insert (
/** /**
* The moo_rbt_update() function updates the value of an existing pair * The moo_rbt_update() function updates the value of an existing pair
* with a matching key. * with a matching key.
* @return pointer to the pair on success, MOO_NULL on no matching pair * \return pointer to the pair on success, MOO_NULL on no matching pair
*/ */
MOO_EXPORT moo_rbt_pair_t* moo_rbt_update ( MOO_EXPORT moo_rbt_pair_t* moo_rbt_update (
moo_rbt_t* rbt, /**< red-black tree */ moo_rbt_t* rbt, /**< red-black tree */
@ -431,7 +431,7 @@ MOO_EXPORT moo_rbt_pair_t* moo_rbt_update (
* a new pair if the key is not found and appends the new value to the * a new pair if the key is not found and appends the new value to the
* existing value delimited by a comma if the key is found. * existing value delimited by a comma if the key is found.
* *
* @code * \code
* moo_rbt_walk_t print_map_pair (moo_rbt_t* map, moo_rbt_pair_t* pair, void* ctx) * moo_rbt_walk_t print_map_pair (moo_rbt_t* map, moo_rbt_pair_t* pair, void* ctx)
* { * {
* moo_printf (MOO_T("%.*s[%d] => %.*s[%d]\n"), * moo_printf (MOO_T("%.*s[%d] => %.*s[%d]\n"),
@ -508,7 +508,7 @@ MOO_EXPORT moo_rbt_pair_t* moo_rbt_update (
* moo_rbt_close (s1); * moo_rbt_close (s1);
* return 0; * return 0;
* } * }
* @endcode * \endcode
*/ */
MOO_EXPORT moo_rbt_pair_t* moo_rbt_cbsert ( MOO_EXPORT moo_rbt_pair_t* moo_rbt_cbsert (
moo_rbt_t* rbt, /**< red-black tree */ moo_rbt_t* rbt, /**< red-black tree */
@ -520,7 +520,7 @@ MOO_EXPORT moo_rbt_pair_t* moo_rbt_cbsert (
/** /**
* The moo_rbt_delete() function deletes a pair with a matching key * The moo_rbt_delete() function deletes a pair with a matching key
* @return 0 on success, -1 on failure * \return 0 on success, -1 on failure
*/ */
MOO_EXPORT int moo_rbt_delete ( MOO_EXPORT int moo_rbt_delete (
moo_rbt_t* rbt, /**< red-black tree */ moo_rbt_t* rbt, /**< red-black tree */
@ -557,14 +557,14 @@ MOO_EXPORT void moo_rbt_rwalk (
/** /**
* The moo_rbt_allocpair() function allocates a pair for a key and a value * The moo_rbt_allocpair() function allocates a pair for a key and a value
* given. But it does not chain the pair allocated into the red-black tree @a rbt. * given. But it does not chain the pair allocated into the red-black tree \a rbt.
* Use this function at your own risk. * Use this function at your own risk.
* *
* Take note of he following special behavior when the copier is * Take note of he following special behavior when the copier is
* #MOO_RBT_COPIER_INLINE. * #MOO_RBT_COPIER_INLINE.
* - If @a kptr is #MOO_NULL, the key space of the size @a klen is reserved but * - If \a kptr is #MOO_NULL, the key space of the size \a klen is reserved but
* not propagated with any data. * not propagated with any data.
* - If @a vptr is #MOO_NULL, the value space of the size @a vlen is reserved * - If \a vptr is #MOO_NULL, the value space of the size \a vlen is reserved
* but not propagated with any data. * but not propagated with any data.
*/ */
MOO_EXPORT moo_rbt_pair_t* moo_rbt_allocpair ( MOO_EXPORT moo_rbt_pair_t* moo_rbt_allocpair (
@ -577,7 +577,7 @@ MOO_EXPORT moo_rbt_pair_t* moo_rbt_allocpair (
/** /**
* The moo_rbt_freepair() function destroys a pair. But it does not detach * The moo_rbt_freepair() function destroys a pair. But it does not detach
* the pair destroyed from the red-black tree @a rbt. Use this function at your * the pair destroyed from the red-black tree \a rbt. Use this function at your
* own risk. * own risk.
*/ */
MOO_EXPORT void moo_rbt_freepair ( MOO_EXPORT void moo_rbt_freepair (

View File

@ -122,12 +122,36 @@ reterr:
MOO_STACK_SETRETTOERROR (moo, nargs); MOO_STACK_SETRETTOERROR (moo, nargs);
return MOO_PF_SUCCESS; return MOO_PF_SUCCESS;
} }
/*
struct bcstr_node_t
{
stix_bch_t* ptr;
bcstr_node_t* next;
};
static bcstr_node_t* dupbcstr (moo_t* moo, bcstr_node_t* bcstr, const moo_bch_t* ptr)
{
moo_freemem (
}
static void free_bcstr_node (moo_t* moo, bcstr_node_t* bcstr)
{
moo_freemem (moo, bcstr->ptr);
moo_freemem (moo, bcstr);
}
*/
static moo_pfrc_t pf_call (moo_t* moo, moo_ooi_t nargs) static moo_pfrc_t pf_call (moo_t* moo, moo_ooi_t nargs)
{ {
#if defined(USE_DYNCALL) #if defined(USE_DYNCALL)
ffi_t* rcv; ffi_t* rcv;
moo_oop_t fun, sig, args; moo_oop_t fun, sig, args;
DCCallVM* dc = MOO_NULL;
moo_oow_t i, j;
void* f;
moo_oop_oop_t arr;
int ellipsis = 0;
struct bcstr_node_t* bcstr;
if (nargs < 3) if (nargs < 3)
{ {
@ -146,23 +170,18 @@ static moo_pfrc_t pf_call (moo_t* moo, moo_ooi_t nargs)
goto reterr; goto reterr;
} }
#if 0
if (MOO_OBJ_GET_SIZE(sig) > 1 && MOO_CLASSOF(moo,args) != moo->_array) /* 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) /* TODO: check if arr is a kind of array??? or check if it's indexed */
{ {
moo_seterrnum (moo, MOO_EINVAL); moo_seterrnum (moo, MOO_EINVAL);
goto reterr; goto reterr;
} }
#endif
{
moo_oow_t i;
DCCallVM* dc;
void* f;
moo_oop_oop_t arr;
int mode_set;
f = MOO_OOP_TO_SMPTR(fun); f = MOO_OOP_TO_SMPTR(fun);
arr = (moo_oop_oop_t)args; arr = (moo_oop_oop_t)args;
dc = dcNewCallVM (4096); dc = dcNewCallVM (4096); /* TODO: right size? */
if (!dc) if (!dc)
{ {
moo_seterrnum (moo, moo_syserrtoerrnum(errno)); moo_seterrnum (moo, moo_syserrtoerrnum(errno));
@ -174,49 +193,79 @@ static moo_pfrc_t pf_call (moo_t* moo, moo_ooi_t nargs)
/*dcMode (dc, DC_CALL_C_DEFAULT); /*dcMode (dc, DC_CALL_C_DEFAULT);
dcReset (dc);*/ dcReset (dc);*/
/*for (i = 1; i < MOO_OBJ_GET_SIZE(sig); i++) i = 0;
{ if (i < MOO_OBJ_GET_SIZE(sig) && ((moo_oop_char_t)sig)->slot[i] == '|')
if (((moo_oop_char_t)sig)->slot[i] == '|')
{ {
dcMode (dc, DC_CALL_C_ELLIPSIS); dcMode (dc, DC_CALL_C_ELLIPSIS);
MOO_DEBUG0 (moo, "CALL MODE 111 ERROR %d %d\n", dcGetError (dc), DC_ERROR_UNSUPPORTED_MODE); if (dcGetError(dc) != DC_ERROR_NONE)
mode_set = 1; {
/* the error code should be DC_ERROR_UNSUPPORTED_MODE */
moo_seterrnum (moo, MOO_ENOIMPL);
goto reterr;
}
dcReset (dc);
ellipsis = 1;
i++;
}
for (j = 0; i < MOO_OBJ_GET_SIZE(sig); i++)
{
moo_ooch_t fmtc;
fmtc = ((moo_oop_char_t)sig)->slot[i];
if (fmtc == ')')
{
i++;
break; break;
} }
} else if (fmtc == '|')
if (!mode_set) */ dcMode (dc, DC_CALL_C_DEFAULT);
for (i = 1; i < MOO_OBJ_GET_SIZE(sig); i++)
{ {
MOO_DEBUG1 (moo, "FFI: CALLING ARG %c\n", ((moo_oop_char_t)sig)->slot[i]); if (ellipsis)
switch (((moo_oop_char_t)sig)->slot[i]) {
dcMode (dc, DC_CALL_C_ELLIPSIS_VARARGS);
if (dcGetError(dc) != DC_ERROR_NONE)
{
/* the error code should be DC_ERROR_UNSUPPORTED_MODE */
moo_seterrnum (moo, MOO_ENOIMPL);
goto reterr;
}
}
continue;
}
if (j >= MOO_OBJ_GET_SIZE(arr))
{
moo_seterrnum (moo, MOO_EINVAL);
goto reterr;
}
switch (fmtc)
{ {
/* TODO: support more types... */ /* TODO: support more types... */
/*
case '|':
dcMode (dc, DC_CALL_C_ELLIPSIS_VARARGS);
MOO_DEBUG2 (moo, "CALL MODE 222 ERROR %d %d\n", dcGetError (dc), DC_ERROR_UNSUPPORTED_MODE);
break;
*/
case 'c': case 'c':
/* TODO: sanity check on the argument type */ /* TODO: sanity check on the argument type */
dcArgChar (dc, MOO_OOP_TO_CHAR(arr->slot[i - 1])); dcArgChar (dc, MOO_OOP_TO_CHAR(arr->slot[j]));
j++;
break; break;
case 'i': case 'i':
/* TODO: use moo_inttoooi () */ /* TODO: use moo_inttoooi () */
dcArgInt (dc, MOO_OOP_TO_SMOOI(arr->slot[i - 1])); dcArgInt (dc, MOO_OOP_TO_SMOOI(arr->slot[j]));
j++;
break; break;
case 'l': case 'l':
/* TODO: use moo_inttoooi () */ /* TODO: use moo_inttoooi () */
dcArgLong (dc, MOO_OOP_TO_SMOOI(arr->slot[i - 1])); dcArgLong (dc, MOO_OOP_TO_SMOOI(arr->slot[j]));
j++;
break; break;
case 'L': case 'L':
/* TODO: use moo_inttoooi () */ /* TODO: use moo_inttoooi () */
dcArgLongLong (dc, MOO_OOP_TO_SMOOI(arr->slot[i - 1])); dcArgLongLong (dc, MOO_OOP_TO_SMOOI(arr->slot[j]));
j++;
break; break;
#if 0 #if 0
@ -225,13 +274,15 @@ MOO_DEBUG2 (moo, "CALL MODE 222 ERROR %d %d\n", dcGetError (dc), DC_ERROR_UNSUPP
case 's': case 's':
{ {
moo_oow_t bcslen, ucslen; moo_oow_t bcslen, ucslen;
moo_bch_t bcs[1024]; moo_bch_t bcs[1024]; /*TODO: dynamic length.... */
ucslen = MOO_OBJ_GET_SIZE(arr->slot[i - 1]); ucslen = MOO_OBJ_GET_SIZE(arr->slot[j]);
moo_convootobchars (moo, ((moo_oop_char_t)arr->slot[i - 2])->slot, &ucslen, bcs, &bcslen); /* proper string conversion */ bcslen = MOO_COUNTOF(bcs);
moo_convootobcstr (moo, ((moo_oop_char_t)arr->slot[j])->slot, &ucslen, bcs, &bcslen); /* proper string conversion */
bcs[bcslen] = '\0'; bcs[bcslen] = '\0';
dcArgPointer (dc, bcs); dcArgPointer (dc, bcs);
j++;
break; break;
} }
@ -239,10 +290,11 @@ MOO_DEBUG2 (moo, "CALL MODE 222 ERROR %d %d\n", dcGetError (dc), DC_ERROR_UNSUPP
/* TODO: ERROR HANDLING */ /* TODO: ERROR HANDLING */
break; break;
} }
} }
switch (((moo_oop_char_t)sig)->slot[0]) if (i >= MOO_OBJ_GET_SIZE(sig)) goto call_void;
switch (((moo_oop_char_t)sig)->slot[i])
{ {
/* TODO: support more types... */ /* TODO: support more types... */
/* TODO: proper return value conversion */ /* TODO: proper return value conversion */
@ -256,8 +308,9 @@ MOO_DEBUG2 (moo, "CALL MODE 222 ERROR %d %d\n", dcGetError (dc), DC_ERROR_UNSUPP
case 'i': case 'i':
{ {
moo_oop_t r; moo_oop_t r;
r = moo_ooitoint (moo, dcCallInt (dc, f)); r = moo_ooitoint (moo, dcCallInt (dc, f));
if (!r) return MOO_PF_HARD_FAILURE; if (!r) goto oops;
MOO_STACK_SETRET (moo, nargs, r); MOO_STACK_SETRET (moo, nargs, r);
break; break;
} }
@ -266,7 +319,7 @@ MOO_DEBUG2 (moo, "CALL MODE 222 ERROR %d %d\n", dcGetError (dc), DC_ERROR_UNSUPP
{ {
moo_oop_t r; moo_oop_t r;
r = moo_ooitoint (moo, dcCallLong (dc, f)); r = moo_ooitoint (moo, dcCallLong (dc, f));
if (!r) return MOO_PF_HARD_FAILURE; if (!r) goto oops;
MOO_STACK_SETRET (moo, nargs, r); MOO_STACK_SETRET (moo, nargs, r);
break; break;
} }
@ -282,7 +335,7 @@ MOO_DEBUG2 (moo, "CALL MODE 222 ERROR %d %d\n", dcGetError (dc), DC_ERROR_UNSUPP
#else #else
# error TODO:... # error TODO:...
#endif #endif
if (!rr) return MOD_PF_HARD_FAILURE; if (!rr) goto oops;
MOO_STACK_SETRET (moo, nargs, r); MOO_STACK_SETRET (moo, nargs, r);
break; break;
@ -290,15 +343,15 @@ MOO_DEBUG2 (moo, "CALL MODE 222 ERROR %d %d\n", dcGetError (dc), DC_ERROR_UNSUPP
#endif #endif
#if 0 #if 0
case 'b': /* byte array */ case 'B': /* byte array */
{ {
} }
#endif #endif
case 'B': case 's':
{ {
moo_oow_t bcslen, ucslen; moo_oow_t bcslen, ucslen;
moo_ooch_t ucs[1024]; moo_ooch_t ucs[1024]; /* TOOD: buffer size... */
moo_oop_t s; moo_oop_t s;
char* r = dcCallPointer (dc, f); char* r = dcCallPointer (dc, f);
@ -306,32 +359,31 @@ MOO_DEBUG2 (moo, "CALL MODE 222 ERROR %d %d\n", dcGetError (dc), DC_ERROR_UNSUPP
moo_convbtooochars (moo, r, &bcslen, ucs, &ucslen); /* error check... */ moo_convbtooochars (moo, r, &bcslen, ucs, &ucslen); /* error check... */
s = moo_makestring(moo, ucs, ucslen); s = moo_makestring(moo, ucs, ucslen);
if (!s) if (!s) goto oops;
{
dcFree (dc);
return MOO_PF_HARD_FAILURE; /* TODO: proper error h andling */
}
MOO_STACK_SETRET (moo, nargs, s); MOO_STACK_SETRET (moo, nargs, s);
break; break;
} }
default: default:
call_void:
moo_seterrnum (moo, MOO_EINVAL); dcCallVoid (dc, f);
goto reterr; MOO_STACK_SETRETTORCV (moo, nargs);
break; break;
} }
dcFree (dc); dcFree (dc);
}
return MOO_PF_SUCCESS; return MOO_PF_SUCCESS;
reterr: reterr:
if (dc) dcFree(dc);
MOO_STACK_SETRETTOERROR (moo, nargs); MOO_STACK_SETRETTOERROR (moo, nargs);
return MOO_PF_SUCCESS; return MOO_PF_SUCCESS;
oops:
if (dc) dcFree(dc);
return MOO_PF_HARD_FAILURE;
#else #else
moo_seterrnum (moo, MOO_ENOIMPL); moo_seterrnum (moo, MOO_ENOIMPL);
MOO_STACK_SETRETTOERROR (moo, nargs); MOO_STACK_SETRETTOERROR (moo, nargs);