added moo_genpfmethods() and moo_findpfimpl() for convenience of writing primitive modules

This commit is contained in:
hyunghwan.chung
2017-02-14 10:25:26 +00:00
parent 71aa1110ed
commit f89d809cdc
10 changed files with 121 additions and 208 deletions

View File

@ -245,45 +245,23 @@ static moo_pfrc_t pf_setcursor (moo_t* moo, moo_ooi_t nargs)
/* ------------------------------------------------------------------------ */
typedef struct fnctab_t fnctab_t;
struct fnctab_t
{
const moo_bch_t* name;
moo_pfimpl_t handler;
};
#define C MOO_METHOD_CLASS
#define I MOO_METHOD_INSTANCE
static fnctab_t fnctab[] =
static moo_pfinfo_t pfinfos[] =
{
{ "clear", pf_clear },
{ "close", pf_close },
{ "open", pf_open },
{ "setcursor", pf_setcursor },
{ "write", pf_write },
{ 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 }
};
/* ------------------------------------------------------------------------ */
static moo_pfimpl_t query (moo_t* moo, moo_mod_t* mod, const moo_ooch_t* name)
{
int left, right, mid, n;
left = 0; right = MOO_COUNTOF(fnctab) - 1;
while (left <= right)
{
mid = (left + right) / 2;
n = moo_compoocbcstr (name, fnctab[mid].name);
if (n < 0) right = mid - 1;
else if (n > 0) left = mid + 1;
else
{
return fnctab[mid].handler;
}
}
moo->errnum = MOO_ENOENT;
return MOO_NULL;
return moo_findpfimpl(moo, pfinfos, MOO_COUNTOF(pfinfos), name);
}

View File

@ -539,7 +539,7 @@ struct fnctab_t
#define C MOO_METHOD_CLASS
#define I MOO_METHOD_INSTANCE
static fnctab_t fnctab[] =
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 },
@ -552,47 +552,13 @@ static fnctab_t fnctab[] =
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++)
{
if (moo_genpfmethod (moo, mod, _class, fnctab[i].type, fnctab[i].mthname, fnctab[i].variadic, MOO_NULL) <= -1)
{
/* TODO: delete pfmethod generated??? */
ret = -1;
break;
}
}
moo_poptmp (moo);
return ret;
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)
{
int left, right, mid, n;
left = 0; right = MOO_COUNTOF(fnctab) - 1;
while (left <= right)
{
mid = (left + right) / 2;
n = moo_compoocstr (name, fnctab[mid].mthname);
if (n < 0) right = mid - 1;
else if (n > 0) left = mid + 1;
else
{
return fnctab[mid].handler;
}
}
moo->errnum = MOO_ENOENT;
return MOO_NULL;
return moo_findpfimpl (moo, pfinfos, MOO_COUNTOF(pfinfos), name);
}
static void unload (moo_t* moo, moo_mod_t* mod)

View File

@ -206,19 +206,10 @@ static moo_pfrc_t pf_puts (moo_t* moo, moo_ooi_t nargs)
/* ------------------------------------------------------------------------ */
typedef struct fnctab_t fnctab_t;
struct fnctab_t
{
moo_method_type_t type;
moo_ooch_t mthname[15];
int variadic;
moo_pfimpl_t handler;
};
#define C MOO_METHOD_CLASS
#define I MOO_METHOD_INSTANCE
static fnctab_t fnctab[] =
static moo_pfinfo_t pfinfos[] =
{
{ I, { 'c','l','o','s','e','\0' }, 0, pf_close },
{ I, { 'g','e','t','s','\0' }, 0, pf_gets },
@ -230,50 +221,15 @@ static fnctab_t fnctab[] =
};
/* ------------------------------------------------------------------------ */
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++)
{
if (moo_genpfmethod (moo, mod, _class, fnctab[i].type, fnctab[i].mthname, fnctab[i].variadic, MOO_NULL) <= -1)
{
/* TODO: delete pfmethod generated??? */
ret = -1;
break;
}
}
moo_poptmp (moo);
return ret;
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)
{
int left, right, mid, n;
left = 0; right = MOO_COUNTOF(fnctab) - 1;
while (left <= right)
{
mid = (left + right) / 2;
n = moo_compoocstr (name, fnctab[mid].mthname);
if (n < 0) right = mid - 1;
else if (n > 0) left = mid + 1;
else
{
return fnctab[mid].handler;
}
}
moo->errnum = MOO_ENOENT;
return MOO_NULL;
return moo_findpfimpl (moo, pfinfos, MOO_COUNTOF(pfinfos), name);
}
#if 0

View File

@ -35,15 +35,6 @@
#define C MOO_METHOD_CLASS
#define I MOO_METHOD_INSTANCE
typedef struct fnctab_t fnctab_t;
struct fnctab_t
{
moo_method_type_t type;
moo_ooch_t mthname[25];
int variadic;
moo_pfimpl_t handler;
};
typedef struct x11_t x11_t;
struct x11_t
@ -164,53 +155,7 @@ 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;
left = 0; right = fnclen - 1;
while (left <= right)
{
mid = (left + right) / 2;
n = moo_compoocstr (name, fnctab[mid].mthname);
if (n < 0) right = mid - 1;
else if (n > 0) left = mid + 1;
else
{
return fnctab[mid].handler;
}
}
moo->errnum = MOO_ENOENT;
return MOO_NULL;
}
static int import_fnctab (moo_t* moo, moo_mod_t* mod, moo_oop_t _class, const fnctab_t* fnctab, moo_oow_t fnclen)
{
int ret = 0;
moo_oow_t i;
moo_pushtmp (moo, &_class);
for (i = 0; i < fnclen; i++)
{
if (moo_genpfmethod (moo, mod, _class, fnctab[i].type, fnctab[i].mthname, fnctab[i].variadic, MOO_NULL) <= -1)
{
/* TODO: delete pfmethod generated??? */
ret = -1;
break;
}
}
moo_poptmp (moo);
return ret;
}
/* ------------------------------------------------------------------------ */
static fnctab_t x11_fnctab[] =
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 }
@ -218,13 +163,13 @@ static fnctab_t x11_fnctab[] =
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));
if (moo_setclasstrsize(moo, _class, MOO_SIZEOF(x11_t)) <= -1) return -1;
return moo_genpfmethods(moo, mod, _class, x11_pfinfo, MOO_COUNTOF(x11_pfinfo));
}
static moo_pfimpl_t x11_query (moo_t* moo, moo_mod_t* mod, const moo_ooch_t* name)
{
return search_fnctab (moo, x11_fnctab, MOO_COUNTOF(x11_fnctab), name);
return moo_findpfimpl (moo, x11_pfinfo, MOO_COUNTOF(x11_pfinfo), name);
}
static void x11_unload (moo_t* moo, moo_mod_t* mod)
@ -243,7 +188,7 @@ int moo_mod_x11 (moo_t* moo, moo_mod_t* mod)
/* ------------------------------------------------------------------------ */
static fnctab_t x11_win_fnctab[] =
static moo_pfinfo_t x11_win_pfinfo[] =
{
{ 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 }
@ -251,13 +196,13 @@ static fnctab_t x11_win_fnctab[] =
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));
if (moo_setclasstrsize(moo, _class, MOO_SIZEOF(x11_win_t)) <= -1) return -1;
return moo_genpfmethods(moo, mod, _class, x11_win_pfinfo, MOO_COUNTOF(x11_win_pfinfo));
}
static moo_pfimpl_t x11_win_query (moo_t* moo, moo_mod_t* mod, const moo_ooch_t* name)
{
return search_fnctab (moo, x11_win_fnctab, MOO_COUNTOF(x11_win_fnctab), name);
return moo_findpfimpl(moo, x11_win_pfinfo, MOO_COUNTOF(x11_win_pfinfo), name);
}
static void x11_win_unload (moo_t* moo, moo_mod_t* mod)