From 68eb5d8db8fee790131b4170d62ca3501ce84db6 Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Sat, 17 Aug 2019 10:08:25 +0000 Subject: [PATCH] added Apex>>basicLastIndex, Apex>>_basicLastIndex. Apex>>basicFirstIndex, Apex>>_basicFirstIndex --- moo/kernel/Apex.moo | 4 ++++ moo/lib/exec.c | 2 ++ moo/lib/moo-prv.h | 2 ++ moo/lib/pf-basic.c | 58 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+) diff --git a/moo/kernel/Apex.moo b/moo/kernel/Apex.moo index 5849cd7..0f42348 100644 --- a/moo/kernel/Apex.moo +++ b/moo/kernel/Apex.moo @@ -119,6 +119,10 @@ extend Apex // ------------------------------------------------------- method(#dual,#primitive,#lenient) _basicSize. method(#dual,#primitive) basicSize. + method(#dual,#primitive,#lenient) _basicFirstIndex. + method(#dual,#primitive) basicFirstIndex. + method(#dual,#primitive,#lenient) _basicLastIndex. + method(#dual,#primitive) basicLastIndex. method(#dual,#primitive) basicAt: index. diff --git a/moo/lib/exec.c b/moo/lib/exec.c index 2a222cd..66723c8 100644 --- a/moo/lib/exec.c +++ b/moo/lib/exec.c @@ -4050,6 +4050,8 @@ static pf_t pftab[] = { "Apex_basicAt:put:", { moo_pf_basic_at_put, 2, 2 } }, { "Apex_basicAt:test:put:", { moo_pf_basic_at_test_put, 3, 3 } }, { "Apex_basicFillFrom:with:count:", { moo_pf_basic_fill, 3, 3 } }, + { "Apex_basicFirstIndex", { moo_pf_basic_first_index, 0, 0 } }, + { "Apex_basicLastIndex", { moo_pf_basic_last_index, 0, 0 } }, { "Apex_basicNew", { moo_pf_basic_new, 0, 0 } }, { "Apex_basicNew:", { moo_pf_basic_new, 1, 1 } }, { "Apex_basicShiftFrom:to:count:", { moo_pf_basic_shift, 3, 3 } }, diff --git a/moo/lib/moo-prv.h b/moo/lib/moo-prv.h index 78cef41..cd54fbe 100644 --- a/moo/lib/moo-prv.h +++ b/moo/lib/moo-prv.h @@ -1612,6 +1612,8 @@ moo_pfrc_t moo_pf_shallow_copy (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs); moo_pfrc_t moo_pf_class (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs); moo_pfrc_t moo_pf_basic_size (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs); +moo_pfrc_t moo_pf_basic_first_index (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs); +moo_pfrc_t moo_pf_basic_last_index (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs); moo_pfrc_t moo_pf_basic_at (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs); moo_pfrc_t moo_pf_basic_at_put (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs); moo_pfrc_t moo_pf_basic_at_test_put (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs); diff --git a/moo/lib/pf-basic.c b/moo/lib/pf-basic.c index bb462ac..331c37f 100644 --- a/moo/lib/pf-basic.c +++ b/moo/lib/pf-basic.c @@ -292,6 +292,64 @@ moo_pfrc_t moo_pf_basic_size (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs) return MOO_PF_SUCCESS; } +moo_pfrc_t moo_pf_basic_last_index (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs) +{ + /* return the number of indexable fields */ + + moo_oop_t rcv, sz; + + MOO_ASSERT (moo, nargs == 0); + + rcv = MOO_STACK_GETRCV (moo, nargs); + + if (!MOO_OOP_IS_POINTER(rcv) || MOO_OBJ_GET_SIZE(rcv) == 0) + { + sz = MOO_SMOOI_TO_OOP(-1); + } + else + { + sz = MOO_SMOOI_TO_OOP(0); + } + + MOO_STACK_SETRET(moo, nargs, sz); + return MOO_PF_SUCCESS; +} + +moo_pfrc_t moo_pf_basic_first_index (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs) +{ + /* return the number of indexable fields */ + + moo_oop_t rcv, sz; + + MOO_ASSERT (moo, nargs == 0); + + rcv = MOO_STACK_GETRCV (moo, nargs); + + if (!MOO_OOP_IS_POINTER(rcv)) + { + /* a non-pointer object has the size of 0. + * such an object doesn't consume object memory space except an OOP word. + * use -1 to indicate that the last index doesn't exist */ + sz = MOO_SMOOI_TO_OOP(-1); + } + else + { + moo_oow_t rcvsize = MOO_OBJ_GET_SIZE(rcv); + if (rcvsize == 0) + { + sz = MOO_SMOOI_TO_OOP(-1); + } + else + { + sz = moo_oowtoint(moo, rcvsize - 1); + if (!sz) return MOO_PF_FAILURE; + } + } + + MOO_STACK_SETRET(moo, nargs, sz); + return MOO_PF_SUCCESS; +} + moo_pfrc_t moo_pf_basic_at (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs) { moo_oop_t rcv, pos, v;