added moo_pfbase_t and changed some functions to return moo_pfbase_t* instead of moo_pfimpl_t

touched up some modules
This commit is contained in:
hyunghwan.chung
2017-03-30 14:59:55 +00:00
parent 9748410354
commit 0f84a115ae
13 changed files with 159 additions and 256 deletions

View File

@ -79,8 +79,7 @@ static moo_pfrc_t pf_open (moo_t* moo, moo_ooi_t nargs)
int err;
char* term;
con = moo_callocmem (moo, MOO_SIZEOF(*con));
if (!con) return 0;
con = (console_t*)moo_getobjtrailer(moo, MOO_STACK_GETRCV(moo, nargs), MOO_NULL);
if (isatty(1))
{
@ -100,7 +99,7 @@ static moo_pfrc_t pf_open (moo_t* moo, moo_ooi_t nargs)
}
term = getenv ("TERM");
if (term && setupterm (term, con->fd, &err) == OK)
if (term && setupterm (term, con->fd, &err) == OK)
{
}
@ -122,7 +121,7 @@ static moo_pfrc_t pf_open (moo_t* moo, moo_ooi_t nargs)
}
#endif
MOO_STACK_SETRET (moo, nargs, MOO_SMOOI_TO_OOP((moo_oow_t)con));
MOO_STACK_SETRET (moo, nargs, MOO_SMOOI_TO_OOP(con->fd));
#endif
return MOO_PF_SUCCESS;
}
@ -140,12 +139,10 @@ static moo_pfrc_t pf_close (moo_t* moo, moo_ooi_t nargs)
#else
console_t* con;
con = (console_t*)MOO_OOP_TO_SMOOI(MOO_STACK_GETARG (moo, nargs, 0));
/* TODO: sanity check */
con = (console_t*)moo_getobjtrailer(moo, MOO_STACK_GETRCV(moo, nargs), MOO_NULL);
if (con->fd_opened) close (con->fd);
moo_freemem (moo, con);
MOO_STACK_SETRETTORCV (moo, nargs);
return MOO_PF_SUCCESS;
#endif
@ -158,29 +155,25 @@ static moo_pfrc_t pf_write (moo_t* moo, moo_ooi_t nargs)
#else
console_t* con;
moo_oop_char_t oomsg;
moo_oop_char_t msg;
moo_oow_t ucspos, ucsrem, ucslen, bcslen;
moo_bch_t bcs[1024];
int n;
con = MOO_OOP_TO_SMOOI(MOO_STACK_GETARG (moo, nargs, 0));
oomsg = (moo_oop_char_t)MOO_STACK_GETARG (moo, nargs, 1);
con = (console_t*)moo_getobjtrailer(moo, MOO_STACK_GETRCV(moo, nargs), MOO_NULL);
msg = (moo_oop_char_t)MOO_STACK_GETARG (moo, nargs, 0);
if (MOO_CLASSOF(moo,oomsg) != moo->_string)
{
/* TODO: invalid message */
return MOO_PF_FAILURE;
}
if (!MOO_OBJ_IS_CHAR_POINTER(msg)) goto einval;
#if defined(MOO_OOCH_IS_UCH)
ucspos = 0;
ucsrem = MOO_OBJ_GET_SIZE(oomsg);
ucsrem = MOO_OBJ_GET_SIZE(msg);
while (ucsrem > 0)
{
ucslen = ucsrem;
bcslen = MOO_COUNTOF(bcs);
if ((n = moo_convootobchars (moo, &oomsg->slot[ucspos], &ucslen, bcs, &bcslen)) <= -1)
if ((n = moo_convootobchars (moo, &msg->slot[ucspos], &ucslen, bcs, &bcslen)) <= -1)
{
if (n != -2 || ucslen <= 0) return MOO_PF_HARD_FAILURE;
}
@ -191,11 +184,15 @@ static moo_pfrc_t pf_write (moo_t* moo, moo_ooi_t nargs)
ucsrem -= ucslen;
}
#else
write (con->fd, oomsg->slot, MOO_OBJ_GET_SIZE(oomsg)); /* TODO: error handling. incomplete write handling */
write (con->fd, MOO_GET_OBJ_CHAR_SLOT(msg), MOO_OBJ_GET_SIZE(msg)); /* TODO: error handling. incomplete write handling */
#endif
MOO_STACK_SETRETTORCV (moo, nargs); /* TODO: change return code */
return MOO_PF_SUCCESS;
einval:
MOO_STACK_SETRETTOERROR (moo, nargs); /* TODO: be more specific about the error code */
return MOO_PF_SUCCESS;
#endif
}
@ -207,7 +204,7 @@ static moo_pfrc_t pf_clear (moo_t* moo, moo_ooi_t nargs)
#else
console_t* con;
con = MOO_OOP_TO_SMOOI(MOO_STACK_GETARG(moo, nargs, 0));
con = (console_t*)moo_getobjtrailer(moo, MOO_STACK_GETRCV(moo, nargs), MOO_NULL);
write (con->fd, con->clear, strlen(con->clear));
@ -223,23 +220,24 @@ static moo_pfrc_t pf_setcursor (moo_t* moo, moo_ooi_t nargs)
#else
console_t* con;
moo_oop_oop_t point;
moo_oop_t x, y;
char* cup;
con = MOO_OOP_TO_SMOOI(MOO_STACK_GETARG(moo, nargs, 0));
point = MOO_STACK_GETARG(moo, nargs, 1);
con = (console_t*)moo_getobjtrailer(moo, MOO_STACK_GETRCV(moo, nargs), MOO_NULL);
x = MOO_STACK_GETARG(moo, nargs, 0);
y = MOO_STACK_GETARG(moo, nargs, 1);
/* TODO: error check, class check, size check.. */
if (MOO_OBJ_GET_SIZE(point) != 2)
{
return MOO_PF_FAILURE;
}
if (!MOO_OOP_IS_SMOOI(x) || !MOO_OOP_IS_SMOOI(y)) goto einval;
cup = tiparm (con->cup, MOO_OOP_TO_SMOOI(point->slot[1]), MOO_OOP_TO_SMOOI(point->slot[0]));
cup = tiparm (con->cup, MOO_OOP_TO_SMOOI(y), MOO_OOP_TO_SMOOI(x));
write (con->fd, cup, strlen(cup)); /* TODO: error check */
MOO_STACK_SETRETTORCV (moo, nargs);
return MOO_PF_SUCCESS;
einval:
MOO_STACK_SETRETTOERROR (moo, nargs); /* TODO: be more specific about the error code */
return MOO_PF_SUCCESS;
#endif
}
@ -250,18 +248,24 @@ static moo_pfrc_t pf_setcursor (moo_t* moo, moo_ooi_t nargs)
static moo_pfinfo_t pfinfos[] =
{
{ I, { 'c','l','e','a','r','\0' }, 0, pf_clear },
{ I, { 'c','l','o','s','e','\0' }, 0, pf_close },
{ I, { 'o','p','e','n','\0' }, 0, pf_open },
{ I, { 's','e','t','c','u','r','s','o','r','\0' }, 0, pf_setcursor },
{ I, { 'w','r','i','t','e','\0' }, 0, pf_write }
{ I, { '_','c','l','e','a','r','\0' }, 0, { pf_clear, 0, 0 } },
{ I, { '_','c','l','o','s','e','\0' }, 0, { pf_close, 0, 0 } },
{ I, { '_','o','p','e','n','\0' }, 0, { pf_open, 0, 0 } },
{ I, { '_','s','e','t','c','u','r','s','o','r','\0' }, 0, { pf_setcursor, 2, 2 } },
{ I, { '_','w','r','i','t','e','\0' }, 0, { pf_write, 1, 1 } }
};
/* ------------------------------------------------------------------------ */
static moo_pfimpl_t query (moo_t* moo, moo_mod_t* mod, const moo_ooch_t* name)
static int import (moo_t* moo, moo_mod_t* mod, moo_oop_class_t _class)
{
return moo_findpfimpl(moo, pfinfos, MOO_COUNTOF(pfinfos), name);
if (moo_setclasstrsize (moo, _class, MOO_SIZEOF(console_t)) <= -1) return -1;
return 0;
}
static moo_pfbase_t* query (moo_t* moo, moo_mod_t* mod, const moo_ooch_t* name)
{
return moo_findpfbase(moo, pfinfos, MOO_COUNTOF(pfinfos), name);
}
@ -272,6 +276,7 @@ static void unload (moo_t* moo, moo_mod_t* mod)
int moo_mod_console (moo_t* moo, moo_mod_t* mod)
{
mod->import = import;
mod->query = query;
mod->unload = unload;
mod->ctx = MOO_NULL;

View File

@ -83,11 +83,7 @@ static moo_pfrc_t pf_open (moo_t* moo, moo_ooi_t nargs)
DCCallVM* dc;
#endif
if (nargs != 1)
{
moo_seterrnum (moo, MOO_EINVAL);
goto softfail;
}
MOO_ASSERT (moo, nargs == 1);
ffi = (ffi_t*)moo_getobjtrailer(moo, MOO_STACK_GETRCV(moo, nargs), MOO_NULL);
name = MOO_STACK_GETARG(moo, nargs, 0);
@ -142,11 +138,7 @@ static moo_pfrc_t pf_close (moo_t* moo, moo_ooi_t nargs)
{
ffi_t* ffi;
if (nargs != 0)
{
moo_seterrnum (moo, MOO_EINVAL);
goto softfail;
}
MOO_ASSERT (moo, nargs == 0);
ffi = (ffi_t*)moo_getobjtrailer(moo, MOO_STACK_GETRCV(moo, nargs), MOO_NULL);
@ -188,7 +180,7 @@ static moo_pfrc_t pf_call (moo_t* moo, moo_ooi_t nargs)
ffi = (ffi_t*)moo_getobjtrailer(moo, MOO_STACK_GETRCV(moo, nargs), MOO_NULL);
if (nargs < 3) goto inval;
MOO_ASSERT (moo, nargs == 3);
fun = MOO_STACK_GETARG(moo, nargs, 0);
sig = MOO_STACK_GETARG(moo, nargs, 1);
args = MOO_STACK_GETARG(moo, nargs, 2);
@ -490,11 +482,7 @@ static moo_pfrc_t pf_getsym (moo_t* moo, moo_ooi_t nargs)
moo_oop_t name;
void* sym;
if (nargs != 1)
{
moo_seterrnum (moo, MOO_EINVAL);
goto softfail;
}
MOO_ASSERT (moo, nargs == 1);
ffi = (ffi_t*)moo_getobjtrailer(moo, MOO_STACK_GETRCV(moo, nargs), MOO_NULL);
name = MOO_STACK_GETARG(moo, nargs, 0);
@ -539,13 +527,14 @@ struct fnctab_t
#define C MOO_METHOD_CLASS
#define I MOO_METHOD_INSTANCE
#define MA MOO_TYPE_MAX(moo_oow_t)
static moo_pfinfo_t pfinfos[] =
{
{ 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 },
{ I, { 'g','e','t','s','y','m',':','\0' }, 0, pf_getsym },
{ I, { 'o','p','e','n',':','\0' }, 0, pf_open }
{ I, { 'c','a','l','l','\0' }, 0, { pf_call, 3, 3 } },
{ I, { 'c','l','o','s','e','\0' }, 0, { pf_close, 0, 0 } },
{ I, { 'g','e','t','s','y','m','\0' }, 0, { pf_getsym, 1, 1 } },
{ I, { 'o','p','e','n','\0' }, 0, { pf_open, 1, 1 } }
};
/* ------------------------------------------------------------------------ */
@ -553,12 +542,12 @@ static moo_pfinfo_t pfinfos[] =
static int import (moo_t* moo, moo_mod_t* mod, moo_oop_class_t _class)
{
if (moo_setclasstrsize (moo, _class, MOO_SIZEOF(ffi_t)) <= -1) return -1;
return moo_genpfmethods (moo, mod, _class, pfinfos, MOO_COUNTOF(pfinfos));
return 0;
}
static moo_pfimpl_t query (moo_t* moo, moo_mod_t* mod, const moo_ooch_t* name)
static moo_pfbase_t* query (moo_t* moo, moo_mod_t* mod, const moo_ooch_t* name)
{
return moo_findpfimpl (moo, pfinfos, MOO_COUNTOF(pfinfos), name);
return moo_findpfbase (moo, pfinfos, MOO_COUNTOF(pfinfos), name);
}
static void unload (moo_t* moo, moo_mod_t* mod)

View File

@ -208,28 +208,28 @@ static moo_pfrc_t pf_puts (moo_t* moo, moo_ooi_t nargs)
#define C MOO_METHOD_CLASS
#define I MOO_METHOD_INSTANCE
#define MA MOO_TYPE_MAX(moo_oow_t)
static moo_pfinfo_t pfinfos[] =
{
{ 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 },
{ I, { 'p','u','t','c','\0' }, 1, pf_putc },
{ I, { 'p','u','t','c',':','\0' }, 0, pf_putc },
{ I, { 'p','u','t','s','\0' }, 1, pf_puts },
{ I, { 'p','u','t','s',':','\0' }, 0, pf_puts }
{ I, { 'c','l','o','s','e','\0' }, 0, { pf_close, 0, 0 } },
{ I, { 'g','e','t','s','\0' }, 0, { pf_gets, 0, 0 } },
{ I, { 'o','p','e','n','\0' }, 0, { pf_open, 2, 2 } },
{ I, { 'p','u','t','c','\0' }, 1, { pf_putc, 0, MA } },
{ I, { 'p','u','t','s','\0' }, 1, { pf_puts, 0, MA } }
};
/* ------------------------------------------------------------------------ */
static int import (moo_t* moo, moo_mod_t* mod, moo_oop_class_t _class)
{
if (moo_setclasstrsize (moo, _class, MOO_SIZEOF(stdio_t)) <= -1) return -1;
return moo_genpfmethods (moo, mod, _class, pfinfos, MOO_COUNTOF(pfinfos));
return 0;
/*return moo_genpfmethods (moo, mod, _class, pfinfos, MOO_COUNTOF(pfinfos));*/
}
static moo_pfimpl_t query (moo_t* moo, moo_mod_t* mod, const moo_ooch_t* name)
static moo_pfbase_t* query (moo_t* moo, moo_mod_t* mod, const moo_ooch_t* name)
{
return moo_findpfimpl (moo, pfinfos, MOO_COUNTOF(pfinfos), name);
return moo_findpfbase (moo, pfinfos, MOO_COUNTOF(pfinfos), name);
}
#if 0

View File

@ -486,10 +486,10 @@ reterr:
static moo_pfinfo_t x11_pfinfo[] =
{
{ 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 },
{ I, { '_','g','e','t','_','e','v','e','n','t','\0'}, 0, pf_getevent },
{ I, { '_','g','e','t','_','f','d','\0' }, 0, pf_get_fd }
{ I, { '_','c','o','n','n','e','c','t','\0' }, 0, { pf_connect, 0, 0 } },
{ I, { '_','d','i','s','c','o','n','n','e','c','t','\0' }, 0, { pf_disconnect, 0, 0 } },
{ I, { '_','g','e','t','_','e','v','e','n','t','\0'}, 0, { pf_getevent, 0, 0 } },
{ I, { '_','g','e','t','_','f','d','\0' }, 0, { pf_get_fd, 0, 0 } }
};
static int x11_import (moo_t* moo, moo_mod_t* mod, moo_oop_class_t _class)
@ -499,9 +499,9 @@ static int x11_import (moo_t* moo, moo_mod_t* mod, moo_oop_class_t _class)
return 0;
}
static moo_pfimpl_t x11_query (moo_t* moo, moo_mod_t* mod, const moo_ooch_t* name)
static moo_pfbase_t* x11_query (moo_t* moo, moo_mod_t* mod, const moo_ooch_t* name)
{
return moo_findpfimpl (moo, x11_pfinfo, MOO_COUNTOF(x11_pfinfo), name);
return moo_findpfbase (moo, x11_pfinfo, MOO_COUNTOF(x11_pfinfo), name);
}
static void x11_unload (moo_t* moo, moo_mod_t* mod)
@ -558,12 +558,12 @@ int moo_mod_x11 (moo_t* moo, moo_mod_t* mod)
static moo_pfinfo_t x11_gc_pfinfo[] =
{
{ I, { '_','d','r','a','w','L','i','n','e' }, 0, pf_gc_draw_line },
{ I, { '_','d','r','a','w','R','e','c','t' }, 0, pf_gc_draw_rect },
{ I, { '_','f','o','r','e','g','r','o','u','n','d',':','\0' }, 0, pf_gc_set_foreground },
{ I, { '_','g','e','t','_','i','d','\0' }, 0, pf_gc_get_id },
{ I, { '_','k','i','l','l','\0' }, 0, pf_gc_kill },
{ I, { '_','m','a','k','e','_','o','n',':','\0' }, 0, pf_gc_make }
{ I, { '_','d','r','a','w','L','i','n','e' }, 0, { pf_gc_draw_line, 4, 4 } },
{ I, { '_','d','r','a','w','R','e','c','t' }, 0, { pf_gc_draw_rect, 4, 4 } },
{ I, { '_','f','o','r','e','g','r','o','u','n','d',':','\0' }, 0, { pf_gc_set_foreground, 1, 1 } },
{ I, { '_','g','e','t','_','i','d','\0' }, 0, { pf_gc_get_id, 0, 0 } },
{ I, { '_','k','i','l','l','\0' }, 0, { pf_gc_kill, 0, 0 } },
{ I, { '_','m','a','k','e','_','o','n',':','\0' }, 0, { pf_gc_make, 1, 1 } }
};
@ -573,9 +573,9 @@ static int x11_gc_import (moo_t* moo, moo_mod_t* mod, moo_oop_class_t _class)
return 0;
}
static moo_pfimpl_t x11_gc_query (moo_t* moo, moo_mod_t* mod, const moo_ooch_t* name)
static moo_pfbase_t* x11_gc_query (moo_t* moo, moo_mod_t* mod, const moo_ooch_t* name)
{
return moo_findpfimpl(moo, x11_gc_pfinfo, MOO_COUNTOF(x11_gc_pfinfo), name);
return moo_findpfbase(moo, x11_gc_pfinfo, MOO_COUNTOF(x11_gc_pfinfo), name);
}
static void x11_gc_unload (moo_t* moo, moo_mod_t* mod)
@ -599,11 +599,12 @@ int moo_mod_x11_gc (moo_t* moo, moo_mod_t* mod)
static moo_pfinfo_t x11_win_pfinfo[] =
{
{ I, { '_','g','e','t','_','d','w','a','t','o','m','\0'}, 0, pf_win_get_dwatom },
{ I, { '_','g','e','t','_','i','d','\0' }, 0, pf_win_get_id },
{ I, { '_','g','e','t','_','d','w','a','t','o','m','\0'}, 0, { pf_win_get_dwatom, 0, 0 } },
{ I, { '_','g','e','t','_','i','d','\0' }, 0, { pf_win_get_id, 0, 0 } },
{ I, { '_','k','i','l','l','\0' }, 0, pf_win_kill },
{ I, { '_','m','a','k','e','_','o','n',':','\0' }, 0, pf_win_make }
{ I, { '_','k','i','l','l','\0' }, 0, { pf_win_kill, 0, 0 } },
{ I, { '_','m','a','k','e','\0' }, 0, { pf_win_make, 1, 1 } },
{ I, { '_','m','a','k','e','_','o','n',':','\0' }, 0, { pf_win_make, 1, 1 } }
};
static int x11_win_import (moo_t* moo, moo_mod_t* mod, moo_oop_class_t _class)
@ -613,9 +614,9 @@ static int x11_win_import (moo_t* moo, moo_mod_t* mod, moo_oop_class_t _class)
return 0;
}
static moo_pfimpl_t x11_win_query (moo_t* moo, moo_mod_t* mod, const moo_ooch_t* name)
static moo_pfbase_t* x11_win_query (moo_t* moo, moo_mod_t* mod, const moo_ooch_t* name)
{
return moo_findpfimpl(moo, x11_win_pfinfo, MOO_COUNTOF(x11_win_pfinfo), name);
return moo_findpfbase(moo, x11_win_pfinfo, MOO_COUNTOF(x11_win_pfinfo), name);
}
static void x11_win_unload (moo_t* moo, moo_mod_t* mod)