From a827ca78264c22ad17650cd91d22e0cb2980ce41 Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Fri, 6 Sep 2019 03:33:34 +0000 Subject: [PATCH] added moo_oowtoptr() and moo_ptrtooow() --- moo/lib/comp.c | 23 ++++------------------- moo/lib/moo.h | 12 ++++++++++++ moo/lib/obj.c | 30 ++++++++++++++++++++++++++++++ moo/mod/ffi.c | 21 +-------------------- 4 files changed, 47 insertions(+), 39 deletions(-) diff --git a/moo/lib/comp.c b/moo/lib/comp.c index 5e83199..38c5ff1 100644 --- a/moo/lib/comp.c +++ b/moo/lib/comp.c @@ -840,7 +840,7 @@ static moo_oop_t string_to_error (moo_t* moo, moo_oocs_t* str, moo_ioloc_t* loc) return MOO_ERROR_TO_OOP(num); } -static moo_oop_t string_to_smptr (moo_t* moo, moo_oocs_t* str, moo_ioloc_t* loc) +static moo_oop_t string_to_ptr (moo_t* moo, moo_oocs_t* str, moo_ioloc_t* loc) { moo_oow_t num = 0; const moo_ooch_t* ptr, * end; @@ -874,22 +874,7 @@ static moo_oop_t string_to_smptr (moo_t* moo, moo_oocs_t* str, moo_ioloc_t* loc) ptr++; } -#if 0 - if (!MOO_IN_SMPTR_RANGE(num)) - { - moo_setsynerr (moo, MOO_SYNERR_SMPTRLITINVAL, loc, str); - return MOO_NULL; - } - - return MOO_SMPTR_TO_OOP(num); -#else - /* TODO: change the function name from string_to_smptr() to string_to_ptr() if this part of code is permanently accepted */ - if (MOO_IN_SMPTR_RANGE(num)) return MOO_SMPTR_TO_OOP(num); - ret = moo_instantiate(moo, moo->_large_pointer, MOO_NULL, 0); - if (!ret) return MOO_NULL; - MOO_OBJ_SET_WORD_VAL(ret, 0, num); - return ret; -#endif + return moo_oowtoptr(moo, num); } /* --------------------------------------------------------------------- @@ -5537,7 +5522,7 @@ static int compile_expression_primary (moo_t* moo, const moo_oocs_t* ident, cons { moo_oop_t tmp; - tmp = string_to_smptr(moo, TOKEN_NAME(moo), TOKEN_LOC(moo)); + tmp = string_to_ptr(moo, TOKEN_NAME(moo), TOKEN_LOC(moo)); if (!tmp) return -1; if (add_literal(moo, tmp, &index) <= -1 || @@ -9501,7 +9486,7 @@ static moo_oop_t token_to_literal (moo_t* moo, int rdonly) return string_to_error(moo, TOKEN_NAME(moo), TOKEN_LOC(moo)); case MOO_IOTOK_SMPTRLIT: - return string_to_smptr(moo, TOKEN_NAME(moo), TOKEN_LOC(moo)); + return string_to_ptr(moo, TOKEN_NAME(moo), TOKEN_LOC(moo)); case MOO_IOTOK_CHARLIT: MOO_ASSERT (moo, TOKEN_NAME_LEN(moo) == 1); diff --git a/moo/lib/moo.h b/moo/lib/moo.h index 33690f1..133c78d 100644 --- a/moo/lib/moo.h +++ b/moo/lib/moo.h @@ -2173,6 +2173,7 @@ MOO_EXPORT int moo_initdbgi ( MOO_EXPORT void moo_finidbgi ( moo_t* moo ); + /* ========================================================================= * COMMON OBJECT MANAGEMENT FUNCTIONS * ========================================================================= */ @@ -2254,6 +2255,17 @@ MOO_EXPORT int moo_inttointmax ( ); #endif +MOO_EXPORT moo_oop_t moo_oowtoptr ( + moo_t* moo, + moo_oow_t num +); + +MOO_EXPORT int moo_ptrtooow ( + moo_t* moo, + moo_oop_t ptr, + moo_oow_t* num +); + MOO_EXPORT moo_oop_t moo_findclass ( moo_t* moo, moo_oop_nsdic_t nsdic, diff --git a/moo/lib/obj.c b/moo/lib/obj.c index 9d455c7..4a3a25c 100644 --- a/moo/lib/obj.c +++ b/moo/lib/obj.c @@ -387,3 +387,33 @@ moo_oop_t moo_instantiatewithtrailer (moo_t* moo, moo_oop_class_t _class, moo_oo moo_popvolats (moo, tmp_count); return oop; } + +moo_oop_t moo_oowtoptr (moo_t* moo, moo_oow_t num) +{ + moo_oop_t ret; + + if (MOO_IN_SMPTR_RANGE(num)) return MOO_SMPTR_TO_OOP(num); + ret = moo_instantiate(moo, moo->_large_pointer, MOO_NULL, 0); + if (!ret) return MOO_NULL; + MOO_OBJ_SET_WORD_VAL(ret, 0, num); + return ret; +} + +int moo_ptrtooow (moo_t* moo, moo_oop_t ptr, moo_oow_t* num) +{ + if (MOO_OOP_IS_SMPTR(ptr)) + { + *num = (moo_oow_t)MOO_OOP_TO_SMPTR(ptr); + return 0; + } + + if (MOO_CLASSOF(moo, ptr) == moo->_large_pointer) + { + MOO_ASSERT (moo, MOO_OBJ_GET_SIZE(ptr) == 1); + *num = MOO_OBJ_GET_WORD_VAL(ptr, 0); + return 0; + } + + moo_seterrnum (moo, MOO_EINVAL); + return -1; +} diff --git a/moo/mod/ffi.c b/moo/mod/ffi.c index 7b0075b..904001c 100644 --- a/moo/mod/ffi.c +++ b/moo/mod/ffi.c @@ -542,8 +542,7 @@ static moo_pfrc_t pf_call (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs) sig = MOO_STACK_GETARG(moo, nargs, 1); args = MOO_STACK_GETARG(moo, nargs, 2); - if (MOO_OOP_IS_SMPTR(fun)) f = MOO_OOP_TO_SMPTR(fun); - else if (moo_inttooow(moo, fun, (moo_oow_t*)&f) <= -1) goto softfail; + if (moo_ptrtooow(moo, fun, (moo_oow_t*)&f) <= -1) goto softfail; /* the signature must not be empty. at least the return type must be * specified */ @@ -851,27 +850,9 @@ static moo_pfrc_t pf_getsym (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs) MOO_DEBUG4 (moo, " %.*js => %p in %p\n", MOO_OBJ_GET_SIZE(name), MOO_OBJ_GET_CHAR_SLOT(name), sym, ffi->handle); -#if 0 ret = moo_oowtoptr(moo, (moo_oow_t)sym); if (!ret) goto softfail; -#else - if (MOO_IN_SMPTR_RANGE(sym)) - { - ret = MOO_SMPTR_TO_OOP(sym); - } - else - { - ret = moo_oowtoint(moo, (moo_oow_t)sym); - if (!ret) goto softfail; - /* - MOO_DEBUG1 (moo, " unaligned symbol address - %p\n", sym); - moo_seterrnum (moo, MOO_EINVAL); - goto softfail; - */ - } -#endif - MOO_STACK_SETRET (moo, nargs, ret); return MOO_PF_SUCCESS;