added more preamable codes.
enhanced the system to be able to attach trailing spaces to an object upon instantiation, especially for external modules to be able to secure GC-safe free workspace inside an instantiated object added moo_setclasstrsize(), moo_getobjtrailer() for the trailer feature
This commit is contained in:
113
moo/mod/ffi.c
113
moo/mod/ffi.c
@ -47,7 +47,6 @@ struct link_t
|
||||
typedef struct ffi_t ffi_t;
|
||||
struct ffi_t
|
||||
{
|
||||
MOO_OBJ_HEADER;
|
||||
void* handle;
|
||||
|
||||
#if defined(USE_DYNCALL)
|
||||
@ -75,16 +74,9 @@ static void free_linked_cas (moo_t* moo, ffi_t* ffi)
|
||||
}
|
||||
}
|
||||
|
||||
static moo_pfrc_t pf_newinstsize (moo_t* moo, moo_ooi_t nargs)
|
||||
{
|
||||
moo_ooi_t newinstsize = MOO_SIZEOF(ffi_t) - MOO_SIZEOF(moo_obj_t);
|
||||
MOO_STACK_SETRET (moo, nargs, MOO_SMOOI_TO_OOP(newinstsize));
|
||||
return MOO_PF_SUCCESS;
|
||||
}
|
||||
|
||||
static moo_pfrc_t pf_open (moo_t* moo, moo_ooi_t nargs)
|
||||
{
|
||||
ffi_t* rcv;
|
||||
ffi_t* ffi;
|
||||
moo_oop_t name;
|
||||
void* handle;
|
||||
#if defined(USE_DYNCALL)
|
||||
@ -97,10 +89,10 @@ static moo_pfrc_t pf_open (moo_t* moo, moo_ooi_t nargs)
|
||||
goto softfail;
|
||||
}
|
||||
|
||||
rcv = (ffi_t*)MOO_STACK_GETRCV(moo, nargs);
|
||||
ffi = (ffi_t*)moo_getobjtrailer(moo, MOO_STACK_GETRCV(moo, nargs), MOO_NULL);
|
||||
name = MOO_STACK_GETARG(moo, nargs, 0);
|
||||
|
||||
if (!MOO_ISTYPEOF(moo, name, MOO_OBJ_TYPE_CHAR))
|
||||
if (!MOO_OBJ_IS_CHAR_POINTER(name))
|
||||
{
|
||||
moo_seterrnum (moo, MOO_EINVAL);
|
||||
goto softfail;
|
||||
@ -112,7 +104,7 @@ static moo_pfrc_t pf_open (moo_t* moo, moo_ooi_t nargs)
|
||||
goto softfail;
|
||||
}
|
||||
|
||||
if (rcv->handle)
|
||||
if (ffi->handle)
|
||||
{
|
||||
moo_seterrnum (moo, MOO_EPERM); /* no allowed to open again */
|
||||
goto softfail;
|
||||
@ -131,13 +123,13 @@ static moo_pfrc_t pf_open (moo_t* moo, moo_ooi_t nargs)
|
||||
}
|
||||
#endif
|
||||
|
||||
rcv->handle = handle;
|
||||
ffi->handle = handle;
|
||||
|
||||
#if defined(USE_DYNCALL)
|
||||
rcv->dc = dc;
|
||||
ffi->dc = dc;
|
||||
#endif
|
||||
|
||||
MOO_DEBUG3 (moo, "<ffi.open> %.*js => %p\n", MOO_OBJ_GET_SIZE(name), ((moo_oop_char_t)name)->slot, rcv->handle);
|
||||
MOO_DEBUG3 (moo, "<ffi.open> %.*js => %p\n", MOO_OBJ_GET_SIZE(name), ((moo_oop_char_t)name)->slot, ffi->handle);
|
||||
MOO_STACK_SETRETTORCV (moo, nargs);
|
||||
return MOO_PF_SUCCESS;
|
||||
|
||||
@ -148,7 +140,7 @@ softfail:
|
||||
|
||||
static moo_pfrc_t pf_close (moo_t* moo, moo_ooi_t nargs)
|
||||
{
|
||||
ffi_t* rcv;
|
||||
ffi_t* ffi;
|
||||
|
||||
if (nargs != 0)
|
||||
{
|
||||
@ -156,7 +148,7 @@ static moo_pfrc_t pf_close (moo_t* moo, moo_ooi_t nargs)
|
||||
goto softfail;
|
||||
}
|
||||
|
||||
rcv = (ffi_t*)MOO_STACK_GETRCV(moo, nargs);
|
||||
ffi = (ffi_t*)moo_getobjtrailer(moo, MOO_STACK_GETRCV(moo, nargs), MOO_NULL);
|
||||
|
||||
if (!moo->vmprim.dl_open)
|
||||
{
|
||||
@ -164,17 +156,17 @@ static moo_pfrc_t pf_close (moo_t* moo, moo_ooi_t nargs)
|
||||
goto softfail;
|
||||
}
|
||||
|
||||
MOO_DEBUG1 (moo, "<ffi.close> %p\n", rcv->handle);
|
||||
MOO_DEBUG1 (moo, "<ffi.close> %p\n", ffi->handle);
|
||||
|
||||
free_linked_cas (moo, rcv);
|
||||
free_linked_cas (moo, ffi);
|
||||
|
||||
#if defined(USE_DYNCALL)
|
||||
dcFree (rcv->dc);
|
||||
rcv->dc = MOO_NULL;
|
||||
dcFree (ffi->dc);
|
||||
ffi->dc = MOO_NULL;
|
||||
#endif
|
||||
|
||||
moo->vmprim.dl_close (moo, rcv->handle);
|
||||
rcv->handle = MOO_NULL;
|
||||
moo->vmprim.dl_close (moo, ffi->handle);
|
||||
ffi->handle = MOO_NULL;
|
||||
|
||||
MOO_STACK_SETRETTORCV (moo, nargs);
|
||||
return MOO_PF_SUCCESS;
|
||||
@ -187,14 +179,14 @@ softfail:
|
||||
static moo_pfrc_t pf_call (moo_t* moo, moo_ooi_t nargs)
|
||||
{
|
||||
#if defined(USE_DYNCALL)
|
||||
ffi_t* rcv;
|
||||
ffi_t* ffi;
|
||||
moo_oop_t fun, sig, args;
|
||||
moo_oow_t i, j;
|
||||
void* f;
|
||||
moo_oop_oop_t arr;
|
||||
int ellipsis = 0;
|
||||
|
||||
rcv = (ffi_t*)MOO_STACK_GETRCV(moo, nargs);
|
||||
ffi = (ffi_t*)moo_getobjtrailer(moo, MOO_STACK_GETRCV(moo, nargs), MOO_NULL);
|
||||
|
||||
if (nargs < 3) goto inval;
|
||||
fun = MOO_STACK_GETARG(moo, nargs, 0);
|
||||
@ -203,7 +195,7 @@ static moo_pfrc_t pf_call (moo_t* moo, moo_ooi_t nargs)
|
||||
|
||||
/* the signature must not be empty. at least the return type must be
|
||||
* specified */
|
||||
if (!MOO_ISTYPEOF(moo, sig, MOO_OBJ_TYPE_CHAR) || MOO_OBJ_GET_SIZE(sig) <= 0) goto inval;
|
||||
if (!MOO_OBJ_IS_CHAR_POINTER(sig) || MOO_OBJ_GET_SIZE(sig) <= 0) goto inval;
|
||||
|
||||
#if 0
|
||||
/* TODO: check if arr is a kind of array??? or check if it's indexed */
|
||||
@ -213,19 +205,19 @@ static moo_pfrc_t pf_call (moo_t* moo, moo_ooi_t nargs)
|
||||
f = MOO_OOP_TO_SMPTR(fun);
|
||||
arr = (moo_oop_oop_t)args;
|
||||
|
||||
MOO_DEBUG2 (moo, "<ffi.call> %p in %p\n", f, rcv->handle);
|
||||
MOO_DEBUG2 (moo, "<ffi.call> %p in %p\n", f, ffi->handle);
|
||||
|
||||
dcMode (rcv->dc, DC_CALL_C_DEFAULT);
|
||||
dcReset (rcv->dc);
|
||||
dcMode (ffi->dc, DC_CALL_C_DEFAULT);
|
||||
dcReset (ffi->dc);
|
||||
|
||||
i = 0;
|
||||
if (i < MOO_OBJ_GET_SIZE(sig) && ((moo_oop_char_t)sig)->slot[i] == '|')
|
||||
{
|
||||
dcMode (rcv->dc, DC_CALL_C_ELLIPSIS);
|
||||
dcMode (ffi->dc, DC_CALL_C_ELLIPSIS);
|
||||
|
||||
/* the error code should be DC_ERROR_UNSUPPORTED_MODE */
|
||||
if (dcGetError(rcv->dc) != DC_ERROR_NONE) goto noimpl;
|
||||
dcReset (rcv->dc);
|
||||
if (dcGetError(ffi->dc) != DC_ERROR_NONE) goto noimpl;
|
||||
dcReset (ffi->dc);
|
||||
ellipsis = 1;
|
||||
i++;
|
||||
}
|
||||
@ -246,10 +238,10 @@ static moo_pfrc_t pf_call (moo_t* moo, moo_ooi_t nargs)
|
||||
{
|
||||
if (ellipsis)
|
||||
{
|
||||
dcMode (rcv->dc, DC_CALL_C_ELLIPSIS_VARARGS);
|
||||
dcMode (ffi->dc, DC_CALL_C_ELLIPSIS_VARARGS);
|
||||
|
||||
/* the error code should be DC_ERROR_UNSUPPORTED_MODE */
|
||||
if (dcGetError(rcv->dc) != DC_ERROR_NONE) goto noimpl;
|
||||
if (dcGetError(ffi->dc) != DC_ERROR_NONE) goto noimpl;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
@ -263,7 +255,7 @@ static moo_pfrc_t pf_call (moo_t* moo, moo_ooi_t nargs)
|
||||
/* TODO: support more types... */
|
||||
case 'c':
|
||||
if (!MOO_OOP_IS_CHAR(arg)) goto inval;
|
||||
dcArgChar (rcv->dc, MOO_OOP_TO_CHAR(arr->slot[j]));
|
||||
dcArgChar (ffi->dc, MOO_OOP_TO_CHAR(arr->slot[j]));
|
||||
j++;
|
||||
break;
|
||||
|
||||
@ -272,7 +264,7 @@ static moo_pfrc_t pf_call (moo_t* moo, moo_ooi_t nargs)
|
||||
{
|
||||
moo_ooi_t v;
|
||||
if (moo_inttoooi(moo, arg, &v) == 0) goto inval;
|
||||
dcArgInt (rcv->dc, i);
|
||||
dcArgInt (ffi->dc, i);
|
||||
j++;
|
||||
break;
|
||||
}
|
||||
@ -282,7 +274,7 @@ static moo_pfrc_t pf_call (moo_t* moo, moo_ooi_t nargs)
|
||||
moo_ooi_t v;
|
||||
arg_as_long:
|
||||
if (moo_inttoooi(moo, arg, &v) == 0) goto inval;
|
||||
dcArgLong (rcv->dc, v);
|
||||
dcArgLong (ffi->dc, v);
|
||||
j++;
|
||||
break;
|
||||
}
|
||||
@ -298,7 +290,7 @@ static moo_pfrc_t pf_call (moo_t* moo, moo_ooi_t nargs)
|
||||
if (moo_inttointmax (moo, arg, &v) == 0) goto inval; */
|
||||
# error UNSUPPORTED MOO_SIZEOF_LONG_LONG.
|
||||
# endif
|
||||
dcArgLongLong (rcv->dc, v);
|
||||
dcArgLongLong (ffi->dc, v);
|
||||
j++;
|
||||
break;
|
||||
#else
|
||||
@ -313,19 +305,19 @@ static moo_pfrc_t pf_call (moo_t* moo, moo_ooi_t nargs)
|
||||
{
|
||||
moo_bch_t* ptr;
|
||||
|
||||
if (!MOO_ISTYPEOF(moo,arg,MOO_OBJ_TYPE_CHAR)) goto inval;
|
||||
if (!MOO_OBJ_IS_CHAR_POINTER(arg)) goto inval;
|
||||
|
||||
#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 softfail; /* out of system memory or conversion error - soft failure */
|
||||
link_ca (rcv, ptr);
|
||||
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 softfail;*/ /* out of system memory or conversion error - soft failure */
|
||||
#endif
|
||||
|
||||
dcArgPointer (rcv->dc, ptr);
|
||||
dcArgPointer (ffi->dc, ptr);
|
||||
j++;
|
||||
break;
|
||||
}
|
||||
@ -334,7 +326,7 @@ static moo_pfrc_t pf_call (moo_t* moo, moo_ooi_t nargs)
|
||||
{
|
||||
moo_uch_t* ptr;
|
||||
|
||||
if (!MOO_ISTYPEOF(moo,arg,MOO_OBJ_TYPE_CHAR)) goto inval;
|
||||
if (!MOO_OBJ_IS_CHAR_POINTER(arg)) goto inval;
|
||||
|
||||
#if defined(MOO_OOCH_IS_UCH)
|
||||
ptr = MOO_OBJ_GET_CHAR_SLOT(arg);
|
||||
@ -343,10 +335,10 @@ static moo_pfrc_t pf_call (moo_t* moo, moo_ooi_t nargs)
|
||||
#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 softfail; /* out of system memory or conversion error - soft failure */
|
||||
link_ca (rcv, ptr);
|
||||
link_ca (ffi, ptr);
|
||||
#endif
|
||||
|
||||
dcArgPointer (rcv->dc, ptr);
|
||||
dcArgPointer (ffi->dc, ptr);
|
||||
j++;
|
||||
break;
|
||||
}
|
||||
@ -365,7 +357,7 @@ static moo_pfrc_t pf_call (moo_t* moo, moo_ooi_t nargs)
|
||||
/* TODO: proper return value conversion */
|
||||
case 'c':
|
||||
{
|
||||
char r = dcCallChar (rcv->dc, f);
|
||||
char r = dcCallChar (ffi->dc, f);
|
||||
MOO_STACK_SETRET (moo, nargs, MOO_CHAR_TO_OOP(r));
|
||||
break;
|
||||
}
|
||||
@ -374,7 +366,7 @@ static moo_pfrc_t pf_call (moo_t* moo, moo_ooi_t nargs)
|
||||
{
|
||||
moo_oop_t r;
|
||||
|
||||
r = moo_ooitoint (moo, dcCallInt (rcv->dc, f));
|
||||
r = moo_ooitoint (moo, dcCallInt (ffi->dc, f));
|
||||
if (!r) goto hardfail;
|
||||
MOO_STACK_SETRET (moo, nargs, r);
|
||||
break;
|
||||
@ -384,7 +376,7 @@ static moo_pfrc_t pf_call (moo_t* moo, moo_ooi_t nargs)
|
||||
{
|
||||
moo_oop_t r;
|
||||
ret_as_long:
|
||||
r = moo_ooitoint (moo, dcCallLong (rcv->dc, f));
|
||||
r = moo_ooitoint (moo, dcCallLong (ffi->dc, f));
|
||||
if (!r) goto hardfail;
|
||||
MOO_STACK_SETRET (moo, nargs, r);
|
||||
break;
|
||||
@ -395,7 +387,7 @@ static moo_pfrc_t pf_call (moo_t* moo, moo_ooi_t nargs)
|
||||
#if (MOO_SIZEOF_LONG_LONG > 0)
|
||||
moo_oop_t r;
|
||||
# if (MOO_SIZEOF_LONG_LONG <= MOO_SIZEOF_OOI_T)
|
||||
r = moo_ooitoint (moo, dcCallLongLong (rcv->dc, f));
|
||||
r = moo_ooitoint (moo, dcCallLongLong (ffi->dc, f));
|
||||
# else
|
||||
# error UNSUPPORTED MOO_SIZEOF_LONG_LONG
|
||||
# endif
|
||||
@ -419,7 +411,7 @@ static moo_pfrc_t pf_call (moo_t* moo, moo_ooi_t nargs)
|
||||
moo_oop_t s;
|
||||
moo_bch_t* r;
|
||||
|
||||
r = dcCallPointer (rcv->dc, f);
|
||||
r = dcCallPointer (ffi->dc, f);
|
||||
|
||||
#if defined(MOO_OOCH_IS_UCH)
|
||||
s = moo_makestringwithbchars (moo, r, moo_countbcstr(r));
|
||||
@ -441,7 +433,7 @@ static moo_pfrc_t pf_call (moo_t* moo, moo_ooi_t nargs)
|
||||
moo_oop_t s;
|
||||
moo_uch_t* r;
|
||||
|
||||
r = dcCallPointer (rcv->dc, f);
|
||||
r = dcCallPointer (ffi->dc, f);
|
||||
|
||||
#if defined(MOO_OOCH_IS_UCH)
|
||||
s = moo_makestring(moo, r, moo_countucstr(r));
|
||||
@ -460,12 +452,12 @@ static moo_pfrc_t pf_call (moo_t* moo, moo_ooi_t nargs)
|
||||
|
||||
default:
|
||||
call_void:
|
||||
dcCallVoid (rcv->dc, f);
|
||||
dcCallVoid (ffi->dc, f);
|
||||
MOO_STACK_SETRETTORCV (moo, nargs);
|
||||
break;
|
||||
}
|
||||
|
||||
free_linked_cas (moo, rcv);
|
||||
free_linked_cas (moo, ffi);
|
||||
return MOO_PF_SUCCESS;
|
||||
|
||||
noimpl:
|
||||
@ -477,12 +469,12 @@ inval:
|
||||
goto softfail;
|
||||
|
||||
softfail:
|
||||
free_linked_cas (moo, rcv);
|
||||
free_linked_cas (moo, ffi);
|
||||
MOO_STACK_SETRETTOERROR (moo, nargs);
|
||||
return MOO_PF_SUCCESS;
|
||||
|
||||
hardfail:
|
||||
free_linked_cas (moo, rcv);
|
||||
free_linked_cas (moo, ffi);
|
||||
return MOO_PF_HARD_FAILURE;
|
||||
|
||||
#else
|
||||
@ -494,7 +486,7 @@ hardfail:
|
||||
|
||||
static moo_pfrc_t pf_getsym (moo_t* moo, moo_ooi_t nargs)
|
||||
{
|
||||
ffi_t* rcv;
|
||||
ffi_t* ffi;
|
||||
moo_oop_t name;
|
||||
void* sym;
|
||||
|
||||
@ -504,10 +496,10 @@ static moo_pfrc_t pf_getsym (moo_t* moo, moo_ooi_t nargs)
|
||||
goto softfail;
|
||||
}
|
||||
|
||||
rcv = (ffi_t*)MOO_STACK_GETRCV(moo, nargs);
|
||||
ffi = (ffi_t*)moo_getobjtrailer(moo, MOO_STACK_GETRCV(moo, nargs), MOO_NULL);
|
||||
name = MOO_STACK_GETARG(moo, nargs, 0);
|
||||
|
||||
if (!MOO_ISTYPEOF(moo,name,MOO_OBJ_TYPE_CHAR))
|
||||
if (!MOO_OBJ_IS_CHAR_POINTER(name))
|
||||
{
|
||||
moo_seterrnum (moo, MOO_EINVAL);
|
||||
goto softfail;
|
||||
@ -519,10 +511,10 @@ static moo_pfrc_t pf_getsym (moo_t* moo, moo_ooi_t nargs)
|
||||
goto softfail;
|
||||
}
|
||||
|
||||
sym = moo->vmprim.dl_getsym (moo, rcv->handle, ((moo_oop_char_t)name)->slot);
|
||||
sym = moo->vmprim.dl_getsym (moo, ffi->handle, ((moo_oop_char_t)name)->slot);
|
||||
if (!sym) goto softfail;
|
||||
|
||||
MOO_DEBUG4 (moo, "<ffi.getsym> %.*js => %p in %p\n", MOO_OBJ_GET_SIZE(name), ((moo_oop_char_t)name)->slot, sym, rcv->handle);
|
||||
MOO_DEBUG4 (moo, "<ffi.getsym> %.*js => %p in %p\n", MOO_OBJ_GET_SIZE(name), ((moo_oop_char_t)name)->slot, sym, ffi->handle);
|
||||
|
||||
MOO_ASSERT (moo, MOO_IN_SMPTR_RANGE(sym));
|
||||
MOO_STACK_SETRET (moo, nargs, MOO_SMPTR_TO_OOP(sym));
|
||||
@ -549,7 +541,6 @@ struct fnctab_t
|
||||
|
||||
static fnctab_t fnctab[] =
|
||||
{
|
||||
{ C, { '_','n','e','w','I','n','s','t','S','i','z','e','\0' }, 0, pf_newinstsize },
|
||||
{ I, { 'c','a','l','l','\0' }, 1, pf_call },
|
||||
{ I, { 'c','a','l','l',':','s','i','g',':','w','i','t','h',':','\0' }, 0, pf_call },
|
||||
{ I, { 'c','l','o','s','e','\0' }, 0, pf_close },
|
||||
@ -564,6 +555,8 @@ static int import (moo_t* moo, moo_mod_t* mod, moo_oop_t _class)
|
||||
int ret = 0;
|
||||
moo_oow_t i;
|
||||
|
||||
if (moo_setclasstrsize (moo, _class, MOO_SIZEOF(ffi_t)) <= -1) return -1;
|
||||
|
||||
moo_pushtmp (moo, &_class);
|
||||
for (i = 0; i < MOO_COUNTOF(fnctab); i++)
|
||||
{
|
||||
|
@ -39,30 +39,22 @@
|
||||
typedef struct stdio_t stdio_t;
|
||||
struct stdio_t
|
||||
{
|
||||
MOO_OBJ_HEADER;
|
||||
FILE* fp;
|
||||
};
|
||||
|
||||
static moo_pfrc_t pf_newinstsize (moo_t* moo, moo_ooi_t nargs)
|
||||
{
|
||||
moo_ooi_t newinstsize = MOO_SIZEOF(stdio_t) - MOO_SIZEOF(moo_obj_t);
|
||||
MOO_STACK_SETRET (moo, nargs, MOO_SMOOI_TO_OOP(newinstsize));
|
||||
return MOO_PF_SUCCESS;
|
||||
}
|
||||
|
||||
static moo_pfrc_t pf_open (moo_t* moo, moo_ooi_t nargs)
|
||||
{
|
||||
moo_oop_char_t name;
|
||||
moo_oop_char_t mode;
|
||||
stdio_t* rcv;
|
||||
stdio_t* stdio;
|
||||
|
||||
#if defined(MOO_OOCH_IS_UCH)
|
||||
moo_oow_t ucslen, bcslen;
|
||||
moo_bch_t namebuf[PATH_MAX];
|
||||
moo_bch_t modebuf[32]; /* TODO: dynamic-sized conversion?? */
|
||||
moo_bch_t modebuf[32]; /* TODO: mode should not be long but use dynamic-sized conversion?? */
|
||||
#endif
|
||||
|
||||
rcv = (stdio_t*)MOO_STACK_GETRCV(moo, nargs);
|
||||
stdio = (stdio_t*)moo_getobjtrailer(moo, MOO_STACK_GETRCV(moo, nargs), MOO_NULL);
|
||||
name = (moo_oop_char_t)MOO_STACK_GETARG(moo, nargs, 0);
|
||||
mode = (moo_oop_char_t)MOO_STACK_GETARG(moo, nargs, 1);
|
||||
|
||||
@ -77,11 +69,11 @@ static moo_pfrc_t pf_open (moo_t* moo, moo_ooi_t nargs)
|
||||
if (moo_convootobchars (moo, mode->slot, &ucslen, modebuf, &bcslen) <= -1) goto softfail;
|
||||
modebuf[bcslen] = '\0';
|
||||
|
||||
rcv->fp = fopen (namebuf, modebuf);
|
||||
stdio->fp = fopen (namebuf, modebuf);
|
||||
#else
|
||||
rcv->fp = fopen (name->slot, mode->slot);
|
||||
stdio->fp = fopen (name->slot, mode->slot);
|
||||
#endif
|
||||
if (!rcv->fp)
|
||||
if (!stdio->fp)
|
||||
{
|
||||
moo_seterrnum (moo, moo_syserrtoerrnum(errno));
|
||||
goto softfail;
|
||||
@ -97,13 +89,13 @@ softfail:
|
||||
|
||||
static moo_pfrc_t pf_close (moo_t* moo, moo_ooi_t nargs)
|
||||
{
|
||||
stdio_t* rcv;
|
||||
stdio_t* stdio;
|
||||
|
||||
rcv = (stdio_t*)MOO_STACK_GETRCV(moo, nargs);
|
||||
if (rcv->fp)
|
||||
stdio = (stdio_t*)moo_getobjtrailer(moo, MOO_STACK_GETRCV(moo, nargs), MOO_NULL);
|
||||
if (stdio->fp)
|
||||
{
|
||||
fclose (rcv->fp);
|
||||
rcv->fp = NULL;
|
||||
fclose (stdio->fp);
|
||||
stdio->fp = NULL;
|
||||
}
|
||||
|
||||
MOO_STACK_SETRETTORCV (moo, nargs);
|
||||
@ -112,17 +104,17 @@ static moo_pfrc_t pf_close (moo_t* moo, moo_ooi_t nargs)
|
||||
|
||||
static moo_pfrc_t pf_gets (moo_t* moo, moo_ooi_t nargs)
|
||||
{
|
||||
/* return how many bytes have been written.. */
|
||||
/* TODO: ...*/
|
||||
MOO_STACK_SETRETTORCV (moo, nargs);
|
||||
return MOO_PF_SUCCESS;
|
||||
}
|
||||
|
||||
static moo_pfrc_t __pf_puts (moo_t* moo, moo_ooi_t nargs, moo_oow_t limit)
|
||||
{
|
||||
stdio_t* rcv;
|
||||
stdio_t* stdio;
|
||||
moo_ooi_t i;
|
||||
|
||||
rcv = (stdio_t*)MOO_STACK_GETRCV(moo, nargs);
|
||||
stdio = (stdio_t*)moo_getobjtrailer(moo, MOO_STACK_GETRCV(moo, nargs), MOO_NULL);
|
||||
|
||||
for (i = 0; i < nargs; i++)
|
||||
{
|
||||
@ -163,7 +155,7 @@ static moo_pfrc_t __pf_puts (moo_t* moo, moo_ooi_t nargs, moo_oow_t limit)
|
||||
if (n != -2 || ucslen <= 0) goto softfail;
|
||||
}
|
||||
|
||||
if (fwrite (bcs, 1, bcslen, rcv->fp) < bcslen)
|
||||
if (fwrite (bcs, 1, bcslen, stdio->fp) < bcslen)
|
||||
{
|
||||
moo_seterrnum (moo, moo_syserrtoerrnum(errno));
|
||||
goto softfail;
|
||||
@ -175,7 +167,7 @@ static moo_pfrc_t __pf_puts (moo_t* moo, moo_ooi_t nargs, moo_oow_t limit)
|
||||
}
|
||||
#else
|
||||
puts_string:
|
||||
if (fwrite (x->slot, 1, MOO_OBJ_GET_SIZE(x), rcv->fp) < MOO_OBJ_GET_SIZE(x))
|
||||
if (fwrite (x->slot, 1, MOO_OBJ_GET_SIZE(x), stdio->fp) < MOO_OBJ_GET_SIZE(x))
|
||||
{
|
||||
moo_seterrnum (moo, moo_syserrtoerrnum(errno));
|
||||
moo_seterrnum (moo, moo_syserrtoerrnum(errno));
|
||||
@ -228,7 +220,6 @@ struct fnctab_t
|
||||
|
||||
static fnctab_t fnctab[] =
|
||||
{
|
||||
{ C, { '_','n','e','w','I','n','s','t','S','i','z','e','\0' }, 0, pf_newinstsize },
|
||||
{ I, { 'c','l','o','s','e','\0' }, 0, pf_close },
|
||||
{ I, { 'g','e','t','s','\0' }, 0, pf_gets },
|
||||
{ I, { 'o','p','e','n',':','f','o','r',':','\0' }, 0, pf_open },
|
||||
@ -245,6 +236,8 @@ static int import (moo_t* moo, moo_mod_t* mod, moo_oop_t _class)
|
||||
int ret = 0;
|
||||
moo_oow_t i;
|
||||
|
||||
if (moo_setclasstrsize (moo, _class, MOO_SIZEOF(stdio_t)) <= -1) return -1;
|
||||
|
||||
moo_pushtmp (moo, &_class);
|
||||
for (i = 0; i < MOO_COUNTOF(fnctab); i++)
|
||||
{
|
||||
|
@ -48,26 +48,17 @@ struct fnctab_t
|
||||
typedef struct x11_t x11_t;
|
||||
struct x11_t
|
||||
{
|
||||
MOO_OBJ_HEADER;
|
||||
xcb_connection_t* c;
|
||||
};
|
||||
|
||||
typedef struct x11_window_t x11_window_t;
|
||||
struct x11_window_t
|
||||
typedef struct x11_win_t x11_win_t;
|
||||
struct x11_win_t
|
||||
{
|
||||
MOO_OBJ_HEADER;
|
||||
xcb_window_t w;
|
||||
};
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
||||
static moo_pfrc_t pf_newinstsize (moo_t* moo, moo_ooi_t nargs)
|
||||
{
|
||||
moo_ooi_t newinstsize = MOO_SIZEOF(x11_t) - MOO_SIZEOF(moo_obj_t);
|
||||
MOO_STACK_SETRET (moo, nargs, MOO_SMOOI_TO_OOP(newinstsize));
|
||||
return MOO_PF_SUCCESS;
|
||||
}
|
||||
|
||||
static moo_pfrc_t pf_connect (moo_t* moo, moo_ooi_t nargs)
|
||||
{
|
||||
x11_t* x11;
|
||||
@ -84,7 +75,7 @@ static moo_pfrc_t pf_connect (moo_t* moo, moo_ooi_t nargs)
|
||||
/*
|
||||
name = MOO_STACK_GETARG(moo, nargs, 0);
|
||||
|
||||
if (!MOO_ISTYPEOF(moo, name, MOO_OBJ_TYPE_CHAR))
|
||||
if (!MOO_OBJ_IS_CHAR_POINTER(name))
|
||||
{
|
||||
moo_seterrnum (moo, MOO_EINVAL);
|
||||
goto softfail;
|
||||
@ -135,14 +126,6 @@ softfail:
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
||||
static moo_pfrc_t pf_win_newinstsize (moo_t* moo, moo_ooi_t nargs)
|
||||
{
|
||||
moo_ooi_t newinstsize = MOO_SIZEOF(x11_window_t) - MOO_SIZEOF(moo_obj_t);
|
||||
MOO_STACK_SETRET (moo, nargs, MOO_SMOOI_TO_OOP(newinstsize));
|
||||
MOO_DEBUG0 (moo, "x11.window.newinstsize....\n");
|
||||
return MOO_PF_SUCCESS;
|
||||
}
|
||||
|
||||
static moo_pfrc_t pf_win_create (moo_t* moo, moo_ooi_t nargs)
|
||||
{
|
||||
MOO_STACK_SETRET (moo, nargs, moo->_nil);
|
||||
@ -181,7 +164,6 @@ MOO_DEBUG0 (moo, "x11.window.destroy....\n");
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
||||
|
||||
static moo_pfimpl_t search_fnctab (moo_t* moo, const fnctab_t* fnctab, moo_oow_t fnclen, const moo_ooch_t* name)
|
||||
{
|
||||
int left, right, mid, n;
|
||||
@ -230,13 +212,13 @@ static int import_fnctab (moo_t* moo, moo_mod_t* mod, moo_oop_t _class, const fn
|
||||
|
||||
static fnctab_t x11_fnctab[] =
|
||||
{
|
||||
{ C, { '_','n','e','w','I','n','s','t','S','i','z','e','\0' }, 0, pf_newinstsize },
|
||||
{ I, { 'c','o','n','n','e','c','t','\0' }, 0, pf_connect },
|
||||
{ I, { 'd','i','s','c','o','n','n','e','c','t','\0' }, 0, pf_disconnect }
|
||||
};
|
||||
|
||||
static int x11_import (moo_t* moo, moo_mod_t* mod, moo_oop_t _class)
|
||||
{
|
||||
if (moo_setclasstrsize (moo, _class, MOO_SIZEOF(x11_t)) <= -1) return -1;
|
||||
return import_fnctab (moo, mod, _class, x11_fnctab, MOO_COUNTOF(x11_fnctab));
|
||||
}
|
||||
|
||||
@ -263,13 +245,13 @@ int moo_mod_x11 (moo_t* moo, moo_mod_t* mod)
|
||||
|
||||
static fnctab_t x11_win_fnctab[] =
|
||||
{
|
||||
{ C, { '_','n','e','w','I','n','s','t','S','i','z','e','\0' }, 0, pf_win_newinstsize },
|
||||
{ I, { 'c','r','e','a','t','e','\0' }, 0, pf_win_create },
|
||||
{ I, { 'd','i','s','t','r','o','y','\0' }, 0, pf_win_destroy }
|
||||
};
|
||||
|
||||
static int x11_win_import (moo_t* moo, moo_mod_t* mod, moo_oop_t _class)
|
||||
{
|
||||
if (moo_setclasstrsize (moo, _class, MOO_SIZEOF(x11_win_t)) <= -1) return -1;
|
||||
return import_fnctab (moo, mod, _class, x11_win_fnctab, MOO_COUNTOF(x11_win_fnctab));
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user