added moo_copyoochartosbuf()

changed string_to_fpdec to use sbuf
This commit is contained in:
hyunghwan.chung 2019-02-20 09:08:10 +00:00
parent fe6c693b86
commit 313077f70f
5 changed files with 301 additions and 255 deletions

View File

@ -760,7 +760,7 @@ static moo_oop_t string_to_fpdec (moo_t* moo, moo_oocs_t* str, int prescaled)
moo_oow_t explen;
explen = len + xscale - scale;
/* TODO: reuse this buffer? */
#if 0
tmp = moo_allocmem(moo, explen * MOO_SIZEOF(*tmp));
if (!tmp)
{
@ -773,6 +773,16 @@ static moo_oop_t string_to_fpdec (moo_t* moo, moo_oocs_t* str, int prescaled)
moo_fill_oochars (&tmp[len], '0', explen - len);
v = moo_strtoint(moo, tmp, explen, base);
moo_freemem (moo, tmp);
#else
if (moo_copyoocharstosbuf(moo, &str->ptr[pos], len, MOO_SBUF_ID_FPDEC) <= -1 ||
moo_concatoochartosbuf(moo, '0', explen - len, MOO_SBUF_ID_FPDEC) <= -1)
{
const moo_ooch_t* oldmsg = moo_backuperrmsg(moo);
moo_seterrbfmt (moo, moo_geterrnum(moo), "unable to convert to fpdec %.*js - %js", str->len, str->ptr, oldmsg);
return MOO_NULL;
}
v = moo_strtoint(moo, moo->sbuf[MOO_SBUF_ID_FPDEC].ptr, moo->sbuf[MOO_SBUF_ID_FPDEC].len, base);
#endif
scale = xscale;
}
else if (scale > xscale)
@ -4785,7 +4795,7 @@ static int compile_block_expression (moo_t* moo)
/* store the accumulated number of temporaries for the current block.
* block depth is not raised as it's not entering a new block but
* updating the temporaries count for the current block. */
if (store_tmpr_count_for_block (moo, cc->mth.tmpr_count) <= -1) return -1;
if (store_tmpr_count_for_block(moo, cc->mth.tmpr_count) <= -1) return -1;
#if defined(MOO_USE_MAKE_BLOCK)
if (emit_double_param_instruction(moo, BCODE_MAKE_BLOCK, block_arg_count, cc->mth.tmpr_count/*block_tmpr_count*/) <= -1) return -1;
@ -4800,13 +4810,13 @@ static int compile_block_expression (moo_t* moo)
jump_inst_pos = cc->mth.code.len;
/* specifying MAX_CODE_JUMP causes emit_single_param_instruction() to
* produce the long jump instruction (BCODE_JUMP_FORWARD_X) */
if (emit_single_param_instruction (moo, BCODE_JUMP_FORWARD_0, MAX_CODE_JUMP) <= -1) return -1;
if (emit_single_param_instruction(moo, BCODE_JUMP_FORWARD_0, MAX_CODE_JUMP) <= -1) return -1;
/* compile statements inside a block */
if (TOKEN_TYPE(moo) == MOO_IOTOK_RBRACK)
{
/* the block is empty */
if (emit_byte_instruction (moo, BCODE_PUSH_NIL) <= -1) return -1;
if (emit_byte_instruction(moo, BCODE_PUSH_NIL) <= -1) return -1;
}
else
{

View File

@ -346,36 +346,6 @@ MOO_EXPORT moo_oow_t moo_count_bcstr (
# define moo_count_oocstr(str) moo_count_bcstr(str)
#endif
MOO_EXPORT int moo_copyoocstrtosbuf (
moo_t* moo,
const moo_ooch_t* str,
int id
);
MOO_EXPORT int moo_concatoocstrtosbuf (
moo_t* moo,
const moo_ooch_t* str,
int id
);
MOO_EXPORT int moo_copyoocharstosbuf (
moo_t* moo,
const moo_ooch_t* ptr,
moo_oow_t len,
int id
);
MOO_EXPORT int moo_concatoocharstosbuf (
moo_t* moo,
const moo_ooch_t* ptr,
moo_oow_t len,
int id
);
/* ------------------------------------------------------------------------- */
MOO_EXPORT int moo_ucwidth (

View File

@ -844,15 +844,15 @@ int moo_genpfmethod (moo_t* moo, moo_mod_t* mod, moo_oop_class_t _class, moo_met
/* compose a full primitive function identifier to VM's string buffer.
* pfid => mod->name + '.' + pfname */
if (moo_copyoocstrtosbuf(moo, mod->name, MOO_SBUF_TMP) <= -1 ||
moo_concatoocstrtosbuf(moo, dot, MOO_SBUF_TMP) <= -1 ||
moo_concatoocstrtosbuf(moo, pfname, MOO_SBUF_TMP) <= -1)
if (moo_copyoocstrtosbuf(moo, mod->name, MOO_SBUF_ID_TMP) <= -1 ||
moo_concatoocstrtosbuf(moo, dot, MOO_SBUF_ID_TMP) <= -1 ||
moo_concatoocstrtosbuf(moo, pfname, MOO_SBUF_ID_TMP) <= -1)
{
MOO_DEBUG2 (moo, "Cannot generate primitive function method [%js] in [%O] - VM memory shortage\n", mthname, _class->name);
return -1;
}
pfidsym = (moo_oop_char_t)moo_makesymbol(moo, moo->sbuf[MOO_SBUF_TMP].ptr, moo->sbuf[MOO_SBUF_TMP].len);
pfidsym = (moo_oop_char_t)moo_makesymbol(moo, moo->sbuf[MOO_SBUF_ID_TMP].ptr, moo->sbuf[MOO_SBUF_ID_TMP].len);
if (!pfidsym)
{
MOO_DEBUG2 (moo, "Cannot generate primitive function method [%js] in [%O] - symbol instantiation failure\n", mthname, _class->name);

View File

@ -1417,9 +1417,12 @@ typedef struct moo_compiler_t moo_compiler_t;
enum moo_sbuf_id_t
{
MOO_SBUF_ID_TMP = 0,
MOO_SBUF_ID_SYNERR = 1
MOO_SBUF_ID_SYNERR,
MOO_SBUF_ID_FPDEC,
/* more? */
MOO_SBUF_COUNT
};
typedef enum moo_sbuf_id_t moo_sbuf_id_t;
struct moo_errinf_t
{
@ -1641,7 +1644,7 @@ struct moo_t
} xbuf; /* buffer to support sprintf */
} sprintf;
moo_sbuf_t sbuf[64];
moo_sbuf_t sbuf[MOO_SBUF_COUNT];
struct
{
@ -2326,6 +2329,9 @@ MOO_EXPORT int moo_convutobcstr (
moo_oow_t* bcslen
);
/* =========================================================================
* STRING DUPLICATION
* ========================================================================= */
#if defined(MOO_OOCH_IS_UCH)
# define moo_dupootobcharswithheadroom(moo,hrb,oocs,oocslen,bcslen) moo_duputobcharswithheadroom(moo,hrb,oocs,oocslen,bcslen)
@ -2426,6 +2432,43 @@ MOO_EXPORT moo_bch_t* moo_dupbchars (
moo_oow_t bcslen
);
/* =========================================================================
* SBUF MANIPULATION
* ========================================================================= */
MOO_EXPORT int moo_copyoocstrtosbuf (
moo_t* moo,
const moo_ooch_t* str,
moo_sbuf_id_t id
);
MOO_EXPORT int moo_concatoocstrtosbuf (
moo_t* moo,
const moo_ooch_t* str,
moo_sbuf_id_t id
);
MOO_EXPORT int moo_copyoocharstosbuf (
moo_t* moo,
const moo_ooch_t* ptr,
moo_oow_t len,
moo_sbuf_id_t id
);
MOO_EXPORT int moo_concatoocharstosbuf (
moo_t* moo,
const moo_ooch_t* ptr,
moo_oow_t len,
moo_sbuf_id_t id
);
MOO_EXPORT int moo_concatoochartosbuf (
moo_t* moo,
moo_ooch_t ch,
moo_oow_t count,
moo_sbuf_id_t id
);
/* =========================================================================
* MOO VM LOGGING

View File

@ -26,6 +26,187 @@
#include "moo-prv.h"
/* ----------------------------------------------------------------------- */
#if defined(MOO_HAVE_UINT16_T)
moo_uint16_t moo_ntoh16 (moo_uint16_t x)
{
#if defined(MOO_ENDIAN_BIG)
return x;
#elif defined(MOO_ENDIAN_LITTLE)
moo_uint8_t* c = (moo_uint8_t*)&x;
return (moo_uint16_t)(
((moo_uint16_t)c[0] << 8) |
((moo_uint16_t)c[1] << 0));
#else
# error Unknown endian
#endif
}
moo_uint16_t moo_hton16 (moo_uint16_t x)
{
#if defined(MOO_ENDIAN_BIG)
return x;
#elif defined(MOO_ENDIAN_LITTLE)
moo_uint8_t* c = (moo_uint8_t*)&x;
return (moo_uint16_t)(
((moo_uint16_t)c[0] << 8) |
((moo_uint16_t)c[1] << 0));
#else
# error Unknown endian
#endif
}
#endif
/* --------------------------------------------------------------- */
#if defined(MOO_HAVE_UINT32_T)
moo_uint32_t moo_ntoh32 (moo_uint32_t x)
{
#if defined(MOO_ENDIAN_BIG)
return x;
#elif defined(MOO_ENDIAN_LITTLE)
moo_uint8_t* c = (moo_uint8_t*)&x;
return (moo_uint32_t)(
((moo_uint32_t)c[0] << 24) |
((moo_uint32_t)c[1] << 16) |
((moo_uint32_t)c[2] << 8) |
((moo_uint32_t)c[3] << 0));
#else
# error Unknown endian
#endif
}
moo_uint32_t moo_hton32 (moo_uint32_t x)
{
#if defined(MOO_ENDIAN_BIG)
return x;
#elif defined(MOO_ENDIAN_LITTLE)
moo_uint8_t* c = (moo_uint8_t*)&x;
return (moo_uint32_t)(
((moo_uint32_t)c[0] << 24) |
((moo_uint32_t)c[1] << 16) |
((moo_uint32_t)c[2] << 8) |
((moo_uint32_t)c[3] << 0));
#else
# error Unknown endian
#endif
}
#endif
#if defined(MOO_HAVE_UINT64_T)
moo_uint64_t moo_ntoh64 (moo_uint64_t x)
{
#if defined(MOO_ENDIAN_BIG)
return x;
#elif defined(MOO_ENDIAN_LITTLE)
moo_uint8_t* c = (moo_uint8_t*)&x;
return (moo_uint64_t)(
((moo_uint64_t)c[0] << 56) |
((moo_uint64_t)c[1] << 48) |
((moo_uint64_t)c[2] << 40) |
((moo_uint64_t)c[3] << 32) |
((moo_uint64_t)c[4] << 24) |
((moo_uint64_t)c[5] << 16) |
((moo_uint64_t)c[6] << 8) |
((moo_uint64_t)c[7] << 0));
#else
# error Unknown endian
#endif
}
moo_uint64_t moo_hton64 (moo_uint64_t x)
{
#if defined(MOO_ENDIAN_BIG)
return x;
#elif defined(MOO_ENDIAN_LITTLE)
moo_uint8_t* c = (moo_uint8_t*)&x;
return (moo_uint64_t)(
((moo_uint64_t)c[0] << 56) |
((moo_uint64_t)c[1] << 48) |
((moo_uint64_t)c[2] << 40) |
((moo_uint64_t)c[3] << 32) |
((moo_uint64_t)c[4] << 24) |
((moo_uint64_t)c[5] << 16) |
((moo_uint64_t)c[6] << 8) |
((moo_uint64_t)c[7] << 0));
#else
# error Unknown endian
#endif
}
#endif
/* --------------------------------------------------------------- */
#if defined(MOO_HAVE_UINT128_T)
moo_uint128_t moo_ntoh128 (moo_uint128_t x)
{
#if defined(MOO_ENDIAN_BIG)
return x;
#elif defined(MOO_ENDIAN_LITTLE)
moo_uint8_t* c = (moo_uint8_t*)&x;
return (moo_uint128_t)(
((moo_uint128_t)c[0] << 120) |
((moo_uint128_t)c[1] << 112) |
((moo_uint128_t)c[2] << 104) |
((moo_uint128_t)c[3] << 96) |
((moo_uint128_t)c[4] << 88) |
((moo_uint128_t)c[5] << 80) |
((moo_uint128_t)c[6] << 72) |
((moo_uint128_t)c[7] << 64) |
((moo_uint128_t)c[8] << 56) |
((moo_uint128_t)c[9] << 48) |
((moo_uint128_t)c[10] << 40) |
((moo_uint128_t)c[11] << 32) |
((moo_uint128_t)c[12] << 24) |
((moo_uint128_t)c[13] << 16) |
((moo_uint128_t)c[14] << 8) |
((moo_uint128_t)c[15] << 0));
#else
# error Unknown endian
#endif
}
moo_uint128_t moo_hton128 (moo_uint128_t x)
{
#if defined(MOO_ENDIAN_BIG)
return x;
#elif defined(MOO_ENDIAN_LITTLE)
moo_uint8_t* c = (moo_uint8_t*)&x;
return (moo_uint128_t)(
((moo_uint128_t)c[0] << 120) |
((moo_uint128_t)c[1] << 112) |
((moo_uint128_t)c[2] << 104) |
((moo_uint128_t)c[3] << 96) |
((moo_uint128_t)c[4] << 88) |
((moo_uint128_t)c[5] << 80) |
((moo_uint128_t)c[6] << 72) |
((moo_uint128_t)c[7] << 64) |
((moo_uint128_t)c[8] << 56) |
((moo_uint128_t)c[9] << 48) |
((moo_uint128_t)c[10] << 40) |
((moo_uint128_t)c[11] << 32) |
((moo_uint128_t)c[12] << 24) |
((moo_uint128_t)c[13] << 16) |
((moo_uint128_t)c[14] << 8) |
((moo_uint128_t)c[15] << 0));
#else
# error Unknown endian
#endif
}
#endif
/* ----------------------------------------------------------------------- */
/* some naming conventions
* bchars, uchars -> pointer and length
* bcstr, ucstr -> null-terminated string pointer
@ -363,53 +544,6 @@ moo_bch_t* moo_find_bchar_in_bcstr (const moo_bch_t* ptr, moo_bch_t c)
/* ----------------------------------------------------------------------- */
int moo_concatoocstrtosbuf (moo_t* moo, const moo_ooch_t* str, int id)
{
return moo_concatoocharstosbuf(moo, str, moo_count_oocstr(str), id);
}
int moo_concatoocharstosbuf (moo_t* moo, const moo_ooch_t* ptr, moo_oow_t len, int id)
{
moo_sbuf_t* p;
p = &moo->sbuf[id];
if (len > p->capa - p->len)
{
moo_oow_t newcapa;
moo_ooch_t* tmp;
newcapa = MOO_ALIGN(p->len + len, 512); /* TODO: adjust this capacity */
/* +1 to handle line ending injection more easily */
tmp = (moo_ooch_t*)moo_reallocmem(moo, p->ptr, (newcapa + 1) * MOO_SIZEOF(*tmp));
if (!tmp) return -1;
p->ptr = tmp;
p->capa = newcapa;
}
moo_copy_oochars (&p->ptr[p->len], ptr, len);
p->len += len;
p->ptr[p->len] = '\0';
return 0;
}
int moo_copyoocstrtosbuf (moo_t* moo, const moo_ooch_t* str, int id)
{
moo->sbuf[id].len = 0;;
return moo_concatoocstrtosbuf(moo, str, id);
}
int moo_copyoocharstosbuf (moo_t* moo, const moo_ooch_t* ptr, moo_oow_t len, int id)
{
moo->sbuf[id].len = 0;;
return moo_concatoocharstosbuf(moo, ptr, len, id);
}
/* ----------------------------------------------------------------------- */
MOO_INLINE int moo_conv_bchars_to_uchars_with_cmgr (
const moo_bch_t* bcs, moo_oow_t* bcslen,
moo_uch_t* ucs, moo_oow_t* ucslen, moo_cmgr_t* cmgr, int all)
@ -987,179 +1121,68 @@ moo_bch_t* moo_dupbchars (moo_t* moo, const moo_bch_t* bcs, moo_oow_t bcslen)
/* ----------------------------------------------------------------------- */
#if defined(MOO_HAVE_UINT16_T)
moo_uint16_t moo_ntoh16 (moo_uint16_t x)
static MOO_INLINE int secure_space_in_sbuf (moo_t* moo, moo_oow_t req, moo_sbuf_t* p)
{
#if defined(MOO_ENDIAN_BIG)
return x;
#elif defined(MOO_ENDIAN_LITTLE)
moo_uint8_t* c = (moo_uint8_t*)&x;
return (moo_uint16_t)(
((moo_uint16_t)c[0] << 8) |
((moo_uint16_t)c[1] << 0));
#else
# error Unknown endian
#endif
if (req > p->capa - p->len)
{
moo_oow_t newcapa;
moo_ooch_t* tmp;
newcapa = MOO_ALIGN_POW2(p->len + req, 512); /* TODO: adjust this capacity */
/* +1 to handle line ending injection more easily */
tmp = (moo_ooch_t*)moo_reallocmem(moo, p->ptr, (newcapa + 1) * MOO_SIZEOF(*tmp));
if (!tmp) return -1;
p->ptr = tmp;
p->capa = newcapa;
}
return 0;
}
moo_uint16_t moo_hton16 (moo_uint16_t x)
int moo_concatoocstrtosbuf (moo_t* moo, const moo_ooch_t* str, moo_sbuf_id_t id)
{
#if defined(MOO_ENDIAN_BIG)
return x;
#elif defined(MOO_ENDIAN_LITTLE)
moo_uint8_t* c = (moo_uint8_t*)&x;
return (moo_uint16_t)(
((moo_uint16_t)c[0] << 8) |
((moo_uint16_t)c[1] << 0));
#else
# error Unknown endian
#endif
return moo_concatoocharstosbuf(moo, str, moo_count_oocstr(str), id);
}
#endif
/* --------------------------------------------------------------- */
#if defined(MOO_HAVE_UINT32_T)
moo_uint32_t moo_ntoh32 (moo_uint32_t x)
int moo_concatoocharstosbuf (moo_t* moo, const moo_ooch_t* ptr, moo_oow_t len, moo_sbuf_id_t id)
{
#if defined(MOO_ENDIAN_BIG)
return x;
#elif defined(MOO_ENDIAN_LITTLE)
moo_uint8_t* c = (moo_uint8_t*)&x;
return (moo_uint32_t)(
((moo_uint32_t)c[0] << 24) |
((moo_uint32_t)c[1] << 16) |
((moo_uint32_t)c[2] << 8) |
((moo_uint32_t)c[3] << 0));
#else
# error Unknown endian
#endif
moo_sbuf_t* p;
p = &moo->sbuf[id];
if (secure_space_in_sbuf(moo, len, p) <= -1) return -1;
moo_copy_oochars (&p->ptr[p->len], ptr, len);
p->len += len;
p->ptr[p->len] = '\0';
return 0;
}
int moo_concatoochartosbuf (moo_t* moo, moo_ooch_t ch, moo_oow_t count, moo_sbuf_id_t id)
{
moo_sbuf_t* p;
p = &moo->sbuf[id];
if (secure_space_in_sbuf(moo, count, p) <= -1) return -1;
moo_fill_oochars (&p->ptr[p->len], ch, count);
p->len += count;
p->ptr[p->len] = '\0';
return 0;
}
moo_uint32_t moo_hton32 (moo_uint32_t x)
int moo_copyoocstrtosbuf (moo_t* moo, const moo_ooch_t* str, moo_sbuf_id_t id)
{
#if defined(MOO_ENDIAN_BIG)
return x;
#elif defined(MOO_ENDIAN_LITTLE)
moo_uint8_t* c = (moo_uint8_t*)&x;
return (moo_uint32_t)(
((moo_uint32_t)c[0] << 24) |
((moo_uint32_t)c[1] << 16) |
((moo_uint32_t)c[2] << 8) |
((moo_uint32_t)c[3] << 0));
#else
# error Unknown endian
#endif
}
#endif
#if defined(MOO_HAVE_UINT64_T)
moo_uint64_t moo_ntoh64 (moo_uint64_t x)
{
#if defined(MOO_ENDIAN_BIG)
return x;
#elif defined(MOO_ENDIAN_LITTLE)
moo_uint8_t* c = (moo_uint8_t*)&x;
return (moo_uint64_t)(
((moo_uint64_t)c[0] << 56) |
((moo_uint64_t)c[1] << 48) |
((moo_uint64_t)c[2] << 40) |
((moo_uint64_t)c[3] << 32) |
((moo_uint64_t)c[4] << 24) |
((moo_uint64_t)c[5] << 16) |
((moo_uint64_t)c[6] << 8) |
((moo_uint64_t)c[7] << 0));
#else
# error Unknown endian
#endif
moo->sbuf[id].len = 0;;
return moo_concatoocstrtosbuf(moo, str, id);
}
moo_uint64_t moo_hton64 (moo_uint64_t x)
int moo_copyoocharstosbuf (moo_t* moo, const moo_ooch_t* ptr, moo_oow_t len, moo_sbuf_id_t id)
{
#if defined(MOO_ENDIAN_BIG)
return x;
#elif defined(MOO_ENDIAN_LITTLE)
moo_uint8_t* c = (moo_uint8_t*)&x;
return (moo_uint64_t)(
((moo_uint64_t)c[0] << 56) |
((moo_uint64_t)c[1] << 48) |
((moo_uint64_t)c[2] << 40) |
((moo_uint64_t)c[3] << 32) |
((moo_uint64_t)c[4] << 24) |
((moo_uint64_t)c[5] << 16) |
((moo_uint64_t)c[6] << 8) |
((moo_uint64_t)c[7] << 0));
#else
# error Unknown endian
#endif
moo->sbuf[id].len = 0;;
return moo_concatoocharstosbuf(moo, ptr, len, id);
}
#endif
/* --------------------------------------------------------------- */
#if defined(MOO_HAVE_UINT128_T)
moo_uint128_t moo_ntoh128 (moo_uint128_t x)
{
#if defined(MOO_ENDIAN_BIG)
return x;
#elif defined(MOO_ENDIAN_LITTLE)
moo_uint8_t* c = (moo_uint8_t*)&x;
return (moo_uint128_t)(
((moo_uint128_t)c[0] << 120) |
((moo_uint128_t)c[1] << 112) |
((moo_uint128_t)c[2] << 104) |
((moo_uint128_t)c[3] << 96) |
((moo_uint128_t)c[4] << 88) |
((moo_uint128_t)c[5] << 80) |
((moo_uint128_t)c[6] << 72) |
((moo_uint128_t)c[7] << 64) |
((moo_uint128_t)c[8] << 56) |
((moo_uint128_t)c[9] << 48) |
((moo_uint128_t)c[10] << 40) |
((moo_uint128_t)c[11] << 32) |
((moo_uint128_t)c[12] << 24) |
((moo_uint128_t)c[13] << 16) |
((moo_uint128_t)c[14] << 8) |
((moo_uint128_t)c[15] << 0));
#else
# error Unknown endian
#endif
}
moo_uint128_t moo_hton128 (moo_uint128_t x)
{
#if defined(MOO_ENDIAN_BIG)
return x;
#elif defined(MOO_ENDIAN_LITTLE)
moo_uint8_t* c = (moo_uint8_t*)&x;
return (moo_uint128_t)(
((moo_uint128_t)c[0] << 120) |
((moo_uint128_t)c[1] << 112) |
((moo_uint128_t)c[2] << 104) |
((moo_uint128_t)c[3] << 96) |
((moo_uint128_t)c[4] << 88) |
((moo_uint128_t)c[5] << 80) |
((moo_uint128_t)c[6] << 72) |
((moo_uint128_t)c[7] << 64) |
((moo_uint128_t)c[8] << 56) |
((moo_uint128_t)c[9] << 48) |
((moo_uint128_t)c[10] << 40) |
((moo_uint128_t)c[11] << 32) |
((moo_uint128_t)c[12] << 24) |
((moo_uint128_t)c[13] << 16) |
((moo_uint128_t)c[14] << 8) |
((moo_uint128_t)c[15] << 0));
#else
# error Unknown endian
#endif
}
#endif