ported the portability fix in bigint.c

This commit is contained in:
2025-08-05 22:35:17 +09:00
parent 9afb8e3898
commit 0da2d2abd3
37 changed files with 1992 additions and 1988 deletions

View File

@ -44,7 +44,7 @@ moo_t* moo_open (moo_mmgr_t* mmgr, moo_oow_t xtnsize, moo_cmgr_t* cmgr, const mo
}
else MOO_MEMSET (moo + 1, 0, xtnsize);
}
else if (errinfo)
else if (errinfo)
{
errinfo->num = MOO_ESYSMEM;
moo_copy_oocstr (errinfo->msg, MOO_COUNTOF(errinfo->msg), moo_errnum_to_errstr(MOO_ESYSMEM));
@ -132,7 +132,7 @@ int moo_init (moo_t* moo, moo_mmgr_t* mmgr, moo_cmgr_t* cmgr, const moo_vmprim_t
* routine still function despite some side-effects when
* reallocation fails */
/* +1 required for consistency with put_oocs and put_ooch in fmtout.c */
moo->log.ptr = moo_allocmem(moo, (moo->log.capa + 1) * MOO_SIZEOF(*moo->log.ptr));
moo->log.ptr = moo_allocmem(moo, (moo->log.capa + 1) * MOO_SIZEOF(*moo->log.ptr));
if (MOO_UNLIKELY(!moo->log.ptr)) goto oops;
#if defined(MOO_ENABLE_GC_MARK_SWEEP)
@ -161,13 +161,13 @@ int moo_init (moo_t* moo, moo_mmgr_t* mmgr, moo_cmgr_t* cmgr, const moo_vmprim_t
oops:
if (modtab_inited) moo_rbt_fini (&moo->modtab);
#if defined(MOO_ENABLE_GC_MARK_SWEEP)
if (moo->gci.stack.ptr)
if (moo->gci.stack.ptr)
{
moo_freemem (moo, moo->gci.stack.ptr);
moo->gci.stack.capa = 0;
}
#endif
if (moo->log.ptr)
if (moo->log.ptr)
{
moo_freemem (moo, moo->log.ptr);
moo->log.capa = 0;
@ -212,7 +212,7 @@ void moo_fini (moo_t* moo)
if (moo->log.len > 0)
{
/* flush pending log message that could be generated by the fini
/* flush pending log message that could be generated by the fini
* callbacks. however, the actual logging might not be produced at
* this point because one of the callbacks could arrange to stop
* logging */
@ -305,7 +305,7 @@ void moo_fini (moo_t* moo)
}
}
if (moo->log.ptr)
if (moo->log.ptr)
{
moo_freemem (moo, moo->log.ptr);
moo->log.capa = 0;
@ -319,7 +319,7 @@ void moo_fini (moo_t* moo)
moo->inttostr.xbuf.capa = 0;
moo->inttostr.xbuf.len = 0;
}
if (moo->inttostr.t.ptr)
{
moo_freemem (moo, moo->inttostr.t.ptr);
@ -531,7 +531,7 @@ static struct
const moo_bch_t* modname;
int (*modload) (moo_t* moo, moo_mod_t* mod);
}
static_modtab[] =
static_modtab[] =
{
#if defined(MOO_ENABLE_MOD_CON)
{ "con", moo_mod_con },
@ -563,17 +563,17 @@ moo_mod_data_t* moo_openmod (moo_t* moo, const moo_ooch_t* name, moo_oow_t namel
int n;
#endif
/* maximum module name length is MOO_MOD_NAME_LEN_MAX.
/* maximum module name length is MOO_MOD_NAME_LEN_MAX.
* MOD_PREFIX_LEN for MOD_PREFIX
* 1 for _ at the end when moo_mod_xxx_ is attempted.
* 1 for the terminating '\0'.
*/
moo_ooch_t buf[MOD_PREFIX_LEN + MOO_MOD_NAME_LEN_MAX + 1 + 1];
moo_ooch_t buf[MOD_PREFIX_LEN + MOO_MOD_NAME_LEN_MAX + 1 + 1];
/* copy instead of encoding conversion. MOD_PREFIX must not
* include a character that requires encoding conversion.
* note the terminating null isn't needed in buf here. */
moo_copy_bchars_to_oochars (buf, MOD_PREFIX, MOD_PREFIX_LEN);
moo_copy_bchars_to_oochars (buf, MOD_PREFIX, MOD_PREFIX_LEN);
if (namelen > MOO_COUNTOF(buf) - (MOD_PREFIX_LEN + 1 + 1))
{
@ -591,7 +591,7 @@ moo_mod_data_t* moo_openmod (moo_t* moo, const moo_ooch_t* name, moo_oow_t namel
/* TODO: binary search ... */
for (n = 0; n < MOO_COUNTOF(static_modtab); n++)
{
if (moo_comp_oochars_bcstr(name, namelen, static_modtab[n].modname) == 0)
if (moo_comp_oochars_bcstr(name, namelen, static_modtab[n].modname) == 0)
{
load = static_modtab[n].modload;
break;
@ -655,7 +655,7 @@ moo_mod_data_t* moo_openmod (moo_t* moo, const moo_ooch_t* name, moo_oow_t namel
md.handle = moo->vmprim.dl_open(moo, &buf[MOD_PREFIX_LEN], MOO_VMPRIM_DLOPEN_PFMOD);
}
if (md.handle == MOO_NULL)
if (md.handle == MOO_NULL)
{
MOO_DEBUG2 (moo, "Cannot open a module [%.*js]\n", namelen, name);
moo_seterrbfmt (moo, MOO_ENOENT, "unable to open a module [%.*js]", namelen, name);
@ -664,7 +664,7 @@ moo_mod_data_t* moo_openmod (moo_t* moo, const moo_ooch_t* name, moo_oow_t namel
/* attempt to get moo_mod_xxx where xxx is the module name*/
load = moo->vmprim.dl_getsym(moo, md.handle, buf);
if (!load)
if (!load)
{
moo_seterrbfmt (moo, moo_geterrnum(moo), "unable to get module symbol [%js] in [%.*js]", buf, namelen, name);
MOO_DEBUG3 (moo, "Cannot get a module symbol [%js] in [%.*js]\n", buf, namelen, name);
@ -689,7 +689,7 @@ moo_mod_data_t* moo_openmod (moo_t* moo, const moo_ooch_t* name, moo_oow_t namel
if (load(moo, &mdp->mod) <= -1)
{
const moo_ooch_t* oldmsg = moo_backuperrmsg (moo);
moo_seterrbfmt (moo, moo_geterrnum(moo), "module initializer [%js] returned failure in [%.*js] - %js", buf, namelen, name, oldmsg);
moo_seterrbfmt (moo, moo_geterrnum(moo), "module initializer [%js] returned failure in [%.*js] - %js", buf, namelen, name, oldmsg);
MOO_DEBUG3 (moo, "Module function [%js] returned failure in [%.*js]\n", buf, namelen, name);
moo_rbt_delete (&moo->modtab, name, namelen);
moo->vmprim.dl_close (moo, mdp->handle);
@ -710,7 +710,7 @@ void moo_closemod (moo_t* moo, moo_mod_data_t* mdp)
{
if (mdp->mod.unload) mdp->mod.unload (moo, &mdp->mod);
if (mdp->handle)
if (mdp->handle)
{
moo->vmprim.dl_close (moo, mdp->handle);
MOO_DEBUG2 (moo, "Closed a module [%js] - %p\n", mdp->mod.name, mdp->handle);
@ -735,7 +735,7 @@ int moo_importmod (moo_t* moo, moo_oop_class_t _class, const moo_ooch_t* name, m
int r = -1;
/* moo_openmod(), moo_closemod(), etc call a user-defined callback.
* i need to protect _class in case the user-defined callback allocates
* i need to protect _class in case the user-defined callback allocates
* a OOP memory chunk and GC occurs. */
moo_pushvolat (moo, (moo_oop_t*)&_class);
@ -772,7 +772,7 @@ int moo_importmod (moo_t* moo, moo_oop_class_t _class, const moo_ooch_t* name, m
done:
/* close the module opened above.
* [NOTE] if the import callback calls the moo_querymodpf(), the returned
* function pointers will get all invalidated here. so never do
* function pointers will get all invalidated here. so never do
* anything like that */
moo_closemod (moo, mdp);
@ -803,7 +803,7 @@ static void* query_mod (moo_t* moo, const moo_ooch_t* pid, moo_oow_t pidlen, moo
sep = moo_rfind_oochar(pid, pidlen, '.');
if (!sep)
{
/* i'm writing a conservative code here. the compiler should
/* i'm writing a conservative code here. the compiler should
* guarantee that a period is included in an primitive function identifer.
* what if the compiler is broken? imagine a buggy compiler rewritten
* in moo itself? */
@ -837,7 +837,7 @@ static void* query_mod (moo_t* moo, const moo_ooch_t* pid, moo_oow_t pidlen, moo
{
/* the primitive function is not found. but keep the module open even if it's opened above */
MOO_DEBUG4 (moo, "Cannot find a primitive %hs [%.*js] in a module [%js]\n", tags[pv], pidlen - mod_name_len - 1, sep + 1, mdp->mod.name);
moo_seterrbfmt (moo, MOO_ENOENT, "unable to find a primitive %hs [%.*js] in a module [%js]", tags[pv], pidlen - mod_name_len - 1, sep + 1, mdp->mod.name);
moo_seterrbfmt (moo, MOO_ENOENT, "unable to find a primitive %hs [%.*js] in a module [%js]", tags[pv], pidlen - mod_name_len - 1, sep + 1, mdp->mod.name);
return MOO_NULL;
}
@ -883,7 +883,7 @@ int moo_genpfmethod (moo_t* moo, moo_mod_t* mod, moo_oop_class_t _class, moo_met
for (i = 0; mthname[i]; i++)
{
if (mthname[i] == ':')
if (mthname[i] == ':')
{
if (variadic) goto oops_inval;
arg_count++;
@ -892,7 +892,7 @@ int moo_genpfmethod (moo_t* moo, moo_mod_t* mod, moo_oop_class_t _class, moo_met
/* TODO: check if name is a valid method name - more checks... */
/* TOOD: if the method name is a binary selector, it can still have an argument.. so the check below is invalid... */
if (arg_count > 0 && mthname[i - 1] != ':')
if (arg_count > 0 && mthname[i - 1] != ':')
{
oops_inval:
MOO_DEBUG2 (moo, "Cannot generate primitive function method [%js] in [%O] - invalid name\n", mthname, _class->name);
@ -917,7 +917,7 @@ int moo_genpfmethod (moo_t* moo, moo_mod_t* mod, moo_oop_class_t _class, moo_met
* pfid => mod->name + '.' + pfname */
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_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;
@ -931,7 +931,7 @@ int moo_genpfmethod (moo_t* moo, moo_mod_t* mod, moo_oop_class_t _class, moo_met
}
moo_pushvolat (moo, (moo_oop_t*)&pfidsym); tmp_count++;
mth = (moo_oop_method_t)moo_instantiatewithtrailer(moo, moo->_method, 1, MOO_NULL, 0);
mth = (moo_oop_method_t)moo_instantiatewithtrailer(moo, moo->_method, 1, MOO_NULL, 0);
if (MOO_UNLIKELY(!mth))
{
MOO_DEBUG2 (moo, "Cannot generate primitive function method [%js] in [%O] - method instantiation failure\n", mthname, _class->name);
@ -953,7 +953,7 @@ int moo_genpfmethod (moo_t* moo, moo_mod_t* mod, moo_oop_class_t _class, moo_met
/* TODO: emit BCODE_RETURN_NIL as a fallback or self primitiveFailed? or anything else?? */
if (!moo_putatdic (moo, _class->mthdic[type], (moo_oop_t)mnsym, (moo_oop_t)mth))
if (!moo_putatdic (moo, _class->mthdic[type], (moo_oop_t)mnsym, (moo_oop_t)mth))
{
MOO_DEBUG2 (moo, "Cannot generate primitive function method [%js] in [%O] - failed to add to method dictionary\n", mthname, _class->name);
goto oops;
@ -977,7 +977,7 @@ int moo_genpfmethods (moo_t* moo, moo_mod_t* mod, moo_oop_class_t _class, const
moo_pushvolat (moo, (moo_oop_t*)&_class);
for (i = 0; i < pfcount; i++)
{
if (moo_genpfmethod (moo, mod, _class, pfinfo[i].type, pfinfo[i].mthname, pfinfo[i].variadic, MOO_NULL) <= -1)
if (moo_genpfmethod (moo, mod, _class, pfinfo[i].type, pfinfo[i].mthname, pfinfo[i].variadic, MOO_NULL) <= -1)
{
/* TODO: delete pfmethod generated??? */
ret = -1;
@ -1069,12 +1069,12 @@ int moo_setclasstrsize (moo_t* moo, moo_oop_class_t _class, moo_oow_t size, moo_
trgc, MOO_OBJ_GET_SIZE(_class->name), MOO_OBJ_GET_CHAR_SLOT(_class->name));
return -1;
}
if (_class == moo->_method)
if (_class == moo->_method)
{
/* the bytes code emitted by the compiler go to the trailer part
* regardless of the trailer size. you're not allowed to change it */
moo_seterrbfmt (moo, MOO_EPERM, "not allowed to set trailer size to %zu on the %.*js class",
moo_seterrbfmt (moo, MOO_EPERM, "not allowed to set trailer size to %zu on the %.*js class",
size, MOO_OBJ_GET_SIZE(_class->name), MOO_OBJ_GET_CHAR_SLOT(_class->name));
return -1;
}
@ -1082,7 +1082,7 @@ int moo_setclasstrsize (moo_t* moo, moo_oop_class_t _class, moo_oow_t size, moo_
spec = MOO_OOP_TO_SMOOI(_class->spec);
if (MOO_CLASS_SPEC_IS_INDEXED(spec) && MOO_CLASS_SPEC_INDEXED_TYPE(spec) != MOO_OBJ_TYPE_OOP)
{
moo_seterrbfmt (moo, MOO_EPERM, "not allowed to set trailer size to %zu on the non-pointer class %.*js",
moo_seterrbfmt (moo, MOO_EPERM, "not allowed to set trailer size to %zu on the non-pointer class %.*js",
size, MOO_OBJ_GET_SIZE(_class->name), MOO_OBJ_GET_CHAR_SLOT(_class->name));
return -1;
}
@ -1090,7 +1090,7 @@ int moo_setclasstrsize (moo_t* moo, moo_oop_class_t _class, moo_oow_t size, moo_
if (_class->trsize != moo->_nil)
{
MOO_ASSERT (moo, _class->trgc != moo->_nil);
moo_seterrbfmt (moo, MOO_EPERM, "not allowed to double-set trailer size to %zu on the %.*js class",
moo_seterrbfmt (moo, MOO_EPERM, "not allowed to double-set trailer size to %zu on the %.*js class",
size, MOO_OBJ_GET_SIZE(_class->name), MOO_OBJ_GET_CHAR_SLOT(_class->name));
return -1;
}
@ -1109,7 +1109,7 @@ int moo_setclasstrsize (moo_t* moo, moo_oop_class_t _class, moo_oow_t size, moo_
_class->trsize = MOO_SMOOI_TO_OOP(size);
_class->trgc = MOO_SMPTR_TO_OOP(trgc); /* i don't replace NULL by nil for GC safety. */
MOO_DEBUG5 (moo, "Set trailer size to %zu on the %.*js class with gc callback of %p(%p)\n",
MOO_DEBUG5 (moo, "Set trailer size to %zu on the %.*js class with gc callback of %p(%p)\n",
size,
MOO_OBJ_GET_SIZE(_class->name),
MOO_OBJ_GET_CHAR_SLOT(_class->name),
@ -1133,7 +1133,7 @@ moo_oop_t moo_findclass (moo_t* moo, moo_oop_nsdic_t nsdic, const moo_ooch_t* na
n.len = moo_count_oocstr(name);
ass = moo_lookupdic_noseterr(moo, (moo_oop_dic_t)nsdic, &n);
if (!ass || MOO_CLASSOF(moo,ass->value) != moo->_class)
if (!ass || MOO_CLASSOF(moo,ass->value) != moo->_class)
{
moo_seterrnum (moo, MOO_ENOENT);
return MOO_NULL;
@ -1149,14 +1149,14 @@ int moo_iskindof (moo_t* moo, moo_oop_t obj, moo_oop_class_t _class)
MOO_ASSERT (moo, MOO_CLASSOF(moo,_class) == moo->_class);
c = MOO_CLASSOF(moo,obj); /* c := self class */
if (c == moo->_class)
if (c == moo->_class)
{
/* object is a class */
if (_class == moo->_class) return 1; /* obj isKindOf: Class */
}
else
{
if (c == _class) return 1;
if (c == _class) return 1;
}
c = (moo_oop_class_t)c->superclass;