From e63596688af709426ead02fac7c9b11714d69028 Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Sun, 17 Dec 2017 15:20:58 +0000 Subject: [PATCH] added moo_seterrwithsyserro()let the compier to remove the leading underscores when resolving a primitive function in a separate module --- moo/kernel/Socket.moo | 22 +++++--------------- moo/lib/comp.c | 47 +++++++++++++++++++++---------------------- moo/lib/err.c | 22 ++++++++++++++++++++ moo/lib/exec.c | 2 -- moo/lib/main.c | 2 +- moo/lib/moo.c | 11 ---------- moo/lib/moo.h | 8 ++++++-- moo/mod/sck.c | 16 +++++++-------- 8 files changed, 65 insertions(+), 65 deletions(-) diff --git a/moo/kernel/Socket.moo b/moo/kernel/Socket.moo index 4f516c5..b153094 100644 --- a/moo/kernel/Socket.moo +++ b/moo/kernel/Socket.moo @@ -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 diff --git a/moo/lib/comp.c b/moo/lib/comp.c index daab8a9..2e35735 100644 --- a/moo/lib/comp.c +++ b/moo/lib/comp.c @@ -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,31 +6475,8 @@ 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. */ @@ -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; diff --git a/moo/lib/err.c b/moo/lib/err.c index 9f856a8..a8eb5ae 100644 --- a/moo/lib/err.c +++ b/moo/lib/err.c @@ -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 * -------------------------------------------------------------------------- */ diff --git a/moo/lib/exec.c b/moo/lib/exec.c index 5e4ede5..f246cd5 100644 --- a/moo/lib/exec.c +++ b/moo/lib/exec.c @@ -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; diff --git a/moo/lib/main.c b/moo/lib/main.c index a4e5930..b75da65 100644 --- a/moo/lib/main.c +++ b/moo/lib/main.c @@ -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)) diff --git a/moo/lib/moo.c b/moo/lib/moo.c index 1689ebd..aa4b0c7 100644 --- a/moo/lib/moo.c +++ b/moo/lib/moo.c @@ -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) diff --git a/moo/lib/moo.h b/moo/lib/moo.h index f71b8e0..35d8dca 100644 --- a/moo/lib/moo.h +++ b/moo/lib/moo.h @@ -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 diff --git a/moo/mod/sck.c b/moo/mod/sck.c index 6437f21..644f25b 100644 --- a/moo/mod/sck.c +++ b/moo/mod/sck.c @@ -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 } }, }; /* ------------------------------------------------------------------------ */