added moo_seterrwithsyserro()let the compier to remove the leading underscores when resolving a primitive function in a separate module

This commit is contained in:
hyunghwan.chung 2017-12-17 15:20:58 +00:00
parent cf88ac40ec
commit e63596688a
8 changed files with 65 additions and 65 deletions

View File

@ -4,10 +4,9 @@ class Socket(Object) from 'sck'
{
var handle := -1.
method(#primitive) _open(domain, type, proto).
method(#primitive) open(domain, type, proto).
method(#primitive) _close.
method(#primitive) _connect(a,b,c).
method(#primitive) connect(a,b,c).
}
(* TODO: generate these domain and type from the C header *)
@ -28,17 +27,9 @@ extend Socket
method(#class) new { self messageProhibited: #new }
method(#class) new: size { self messageProhibited: #new: }
method(#class) _domain: domain _type: type
{
^super new _open(domain, type, 0).
}
method(#class) domain: domain type: type
{
| s |
s := super new _open(domain, type, 0).
if (s isError) { Exception signal: 'Cannot open socket' }.
^s.
^super new open(domain, type, 0).
}
method close
@ -46,7 +37,7 @@ extend Socket
if (self.handle >= 0)
{
## this primitive method may return failure.
## but ignore it in this normal method.
## but ignore it here.
self _close.
self.handle := -1.
}
@ -77,10 +68,7 @@ extend Socket
System addAsyncSemaphore: s1.
System addAsyncSemaphore: s2.
if (self _connect(1, 2, 3) isError)
{
Exception signal: 'Cannot connect to 1,2,3'.
}.
self connect(1, 2, 3).
}
method asyncRead: readBlock

View File

@ -6435,6 +6435,8 @@ static int __compile_method_definition (moo_t* moo)
if (moo->c->mth.primitive)
{
moo_oocs_t mthname;
/* the primitive method must be of this form
* method(#primitive) method_name.
*/
@ -6445,6 +6447,26 @@ static int __compile_method_definition (moo_t* moo)
return -1;
}
/*
* remove all leading underscores from the method name when building a primitive
* identifer. multiple methods can map to the same primitive handler.
* for class X, you may have method(#primitive) aa and method(#primitive) _aa
* to map to the X_aa primitive handler.
*/
mthname = moo->c->mth.name;
while (mthname.len > 0)
{
if (*mthname.ptr != '_') break;
mthname.ptr++;
mthname.len--;
}
if (mthname.len == 0)
{
MOO_DEBUG2 (moo, "Invalid primitive function name - %.*js\n", moo->c->mth.name.len, moo->c->mth.name.ptr);
set_syntax_error (moo, MOO_SYNERR_PFIDINVAL, &moo->c->mth.name_loc, &moo->c->mth.name);
return -1;
}
if (moo->c->cls.self_oop->modname == moo->_nil)
{
/* no module name specified in the class definition using 'from'.
@ -6453,32 +6475,9 @@ static int __compile_method_definition (moo_t* moo)
moo_oow_t savedlen;
moo_ooi_t pfnum;
moo_pfbase_t* pfbase;
moo_oocs_t mthname;
/* primitive identifer = classname_methodname */
/*
* remove all leading underscores from the method name when building a primitive
* identifer. multiple methods can map to the same primitive handler.
* for class X, you may have method(#primitive) aa and method(#primitive) _aa
* to map to the X_aa primitive handler.
*/
mthname = moo->c->mth.name;
while (mthname.len > 0)
{
if (*mthname.ptr != '_') break;
mthname.ptr++;
mthname.len--;
}
if (mthname.len == 0)
{
MOO_DEBUG2 (moo, "Invalid primitive function name - %.*js\n", moo->c->mth.name.len, moo->c->mth.name.ptr);
set_syntax_error (moo, MOO_SYNERR_PFIDINVAL, &moo->c->mth.name_loc, &moo->c->mth.name);
return -1;
}
/* compose the identifer into the back of the cls.modname buffer.
* i'll revert it when done. */
savedlen = moo->c->cls.modname.len;
@ -6532,7 +6531,7 @@ static int __compile_method_definition (moo_t* moo)
tmp.len = MOO_OBJ_GET_SIZE(moo->c->cls.self_oop->modname);
if (copy_string_to (moo, &tmp, &moo->c->cls.modname, &moo->c->cls.modname_capa, 1, '\0') <= -1 ||
copy_string_to (moo, &moo->c->mth.name, &moo->c->cls.modname, &moo->c->cls.modname_capa, 1, '.') <= -1 ||
copy_string_to (moo, &mthname, &moo->c->cls.modname, &moo->c->cls.modname_capa, 1, '.') <= -1 ||
add_symbol_literal(moo, &moo->c->cls.modname, savedlen, &litidx) <= -1)
{
moo->c->cls.modname.len = savedlen;

View File

@ -290,6 +290,28 @@ moo_errnum_t moo_syserr_to_errnum (int e)
#endif
}
/* --------------------------------------------------------------------------
* ERROR NUMBER/MESSAGE HANDLING
* -------------------------------------------------------------------------- */
const moo_ooch_t* moo_geterrstr (moo_t* moo)
{
return moo_errnum_to_errstr (moo->errnum);
}
const moo_ooch_t* moo_geterrmsg (moo_t* moo)
{
if (moo->errmsg.len <= 0) return moo_errnum_to_errstr (moo->errnum);
return moo->errmsg.buf;
}
void moo_seterrwithsyserr (moo_t* moo, int syserr)
{
moo_bch_t msgbuf[64];
strerror_r (errno, msgbuf, MOO_COUNTOF(msgbuf));
moo_seterrbfmt (moo, moo_syserr_to_errnum(errno), "%s", msgbuf);
}
/* --------------------------------------------------------------------------
* ASSERTION FAILURE HANDLER
* -------------------------------------------------------------------------- */

View File

@ -1745,8 +1745,6 @@ static moo_pfrc_t pf_hash (moo_t* moo, moo_ooi_t nargs)
}
static moo_pfrc_t pf_perform (moo_t* moo, moo_ooi_t nargs)
{
moo_oop_t /*rcv,*/ selector;

View File

@ -2348,7 +2348,7 @@ int main (int argc, char* argv[])
if (synerr.tgt.len > 0)
{
moo_logbfmt (moo, MOO_LOG_ERROR | MOO_LOG_STDERR, " in place of %.*js", synerr.tgt.len, synerr.tgt.ptr);
moo_logbfmt (moo, MOO_LOG_ERROR | MOO_LOG_STDERR, " - %.*js", synerr.tgt.len, synerr.tgt.ptr);
}
if (moo_geterrmsg(moo) != moo_geterrstr(moo))

View File

@ -230,17 +230,6 @@ void moo_fini (moo_t* moo)
}
}
const moo_ooch_t* moo_geterrstr (moo_t* moo)
{
return moo_errnum_to_errstr (moo->errnum);
}
const moo_ooch_t* moo_geterrmsg (moo_t* moo)
{
if (moo->errmsg.len <= 0) return moo_errnum_to_errstr (moo->errnum);
return moo->errmsg.buf;
}
int moo_setoption (moo_t* moo, moo_option_t id, const void* value)
{
switch (id)

View File

@ -1594,20 +1594,24 @@ MOO_EXPORT void moo_fini (
#endif
void moo_seterrbfmt (
MOO_EXPORT void moo_seterrbfmt (
moo_t* moo,
moo_errnum_t errnum,
const moo_bch_t* fmt,
...
);
void moo_seterrufmt (
MOO_EXPORT void moo_seterrufmt (
moo_t* moo,
moo_errnum_t errnum,
const moo_uch_t* fmt,
...
);
MOO_EXPORT void moo_seterrwithsyserr (
moo_t* moo,
int syserr
);
MOO_EXPORT const moo_ooch_t* moo_geterrmsg (
moo_t* moo

View File

@ -58,14 +58,14 @@ static moo_pfrc_t pf_open_socket (moo_t* moo, moo_ooi_t nargs)
fd = socket (MOO_OOP_TO_SMOOI(dom), MOO_OOP_TO_SMOOI(type), MOO_OOP_TO_SMOOI(proto));
if (fd == -1)
{
errnum = moo_syserr_to_errnum(errno);
moo_seterrwithsyserr (moo, errno);
goto oops;
}
if (!MOO_IN_SMOOI_RANGE(fd))
{
/* the file descriptor is too big to be represented as a small integer */
errnum = MOO_ERANGE;
moo_seterrbfmt (moo, MOO_ERANGE, "socket handle %d not in the permitted range", fd);
goto oops;
}
@ -76,7 +76,6 @@ static moo_pfrc_t pf_open_socket (moo_t* moo, moo_ooi_t nargs)
oops:
if (fd >= 0) close (fd);
moo_seterrnum (moo, errnum);
return MOO_PF_FAILURE;
}
@ -97,14 +96,15 @@ static moo_pfrc_t pf_close_socket (moo_t* moo, moo_ooi_t nargs)
{
if (close(MOO_OOP_TO_SMOOI(sck->handle)) == -1)
{
MOO_STACK_SETRETTOERROR (moo, nargs, moo_syserr_to_errnum(errno));
moo_seterrwithsyserr (moo, errno);
return MOO_PF_FAILURE;
}
else
{
sck->handle = MOO_SMOOI_TO_OOP(-1);
MOO_STACK_SETRETTORCV (moo, nargs);
return MOO_PF_SUCCESS;
}
return MOO_PF_SUCCESS;
}
moo_seterrbfmt (moo, MOO_EBADHND, "bad socket handle - %O", sck->handle);
@ -176,9 +176,9 @@ struct fnctab_t
static moo_pfinfo_t pfinfos[] =
{
{ I, { '_','c','l','o','s','e','\0' }, 0, { pf_close_socket, 0, 0 } },
{ I, { '_','c','o','n','n','e','c','t','\0' }, 0, { pf_connect, 3, 3 } },
{ I, { '_','o','p','e','n','\0' }, 0, { pf_open_socket, 3, 3 } },
{ I, { 'c','l','o','s','e','\0' }, 0, { pf_close_socket, 0, 0 } },
{ I, { 'c','o','n','n','e','c','t','\0' }, 0, { pf_connect, 3, 3 } },
{ I, { 'o','p','e','n','\0' }, 0, { pf_open_socket, 3, 3 } },
};
/* ------------------------------------------------------------------------ */