2017-01-09 09:54:49 +00:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
2019-11-19 09:40:26 +00:00
|
|
|
Copyright (c) 2014-2019 Chung, Hyung-Hwan. All rights reserved.
|
2017-01-09 09:54:49 +00:00
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
|
|
|
|
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "moo-prv.h"
|
|
|
|
|
2020-11-25 14:48:26 +00:00
|
|
|
moo_t* moo_open (moo_mmgr_t* mmgr, moo_oow_t xtnsize, moo_cmgr_t* cmgr, const moo_vmprim_t* vmprim, moo_gc_type_t gctype, moo_errinf_t* errinfo)
|
2017-01-09 09:54:49 +00:00
|
|
|
{
|
|
|
|
moo_t* moo;
|
|
|
|
|
|
|
|
/* if this assertion fails, correct the type definition in moo.h */
|
|
|
|
MOO_ASSERT (moo, MOO_SIZEOF(moo_oow_t) == MOO_SIZEOF(moo_oop_t));
|
|
|
|
|
2018-02-26 15:30:38 +00:00
|
|
|
moo = (moo_t*)MOO_MMGR_ALLOC(mmgr, MOO_SIZEOF(*moo) + xtnsize);
|
2017-01-09 09:54:49 +00:00
|
|
|
if (moo)
|
|
|
|
{
|
2020-11-25 14:48:26 +00:00
|
|
|
if (moo_init(moo, mmgr, cmgr, vmprim, gctype) <= -1)
|
2017-01-09 09:54:49 +00:00
|
|
|
{
|
2018-11-11 16:53:21 +00:00
|
|
|
if (errinfo) moo_geterrinf (moo, errinfo);
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_MMGR_FREE (mmgr, moo);
|
|
|
|
moo = MOO_NULL;
|
|
|
|
}
|
|
|
|
else MOO_MEMSET (moo + 1, 0, xtnsize);
|
|
|
|
}
|
2018-11-11 16:53:21 +00:00
|
|
|
else if (errinfo)
|
|
|
|
{
|
|
|
|
errinfo->num = MOO_ESYSMEM;
|
|
|
|
moo_copy_oocstr (errinfo->msg, MOO_COUNTOF(errinfo->msg), moo_errnum_to_errstr(MOO_ESYSMEM));
|
|
|
|
}
|
2017-01-09 09:54:49 +00:00
|
|
|
|
|
|
|
return moo;
|
|
|
|
}
|
|
|
|
|
|
|
|
void moo_close (moo_t* moo)
|
|
|
|
{
|
|
|
|
moo_fini (moo);
|
2019-06-21 07:21:58 +00:00
|
|
|
MOO_MMGR_FREE (moo_getmmgr(moo), moo);
|
2017-01-09 09:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void fill_bigint_tables (moo_t* moo)
|
|
|
|
{
|
|
|
|
int radix, safe_ndigits;
|
|
|
|
moo_oow_t ub, w;
|
|
|
|
moo_oow_t multiplier;
|
|
|
|
|
|
|
|
for (radix = 2; radix <= 36; radix++)
|
|
|
|
{
|
|
|
|
w = 0;
|
|
|
|
ub = (moo_oow_t)MOO_TYPE_MAX(moo_liw_t) / radix - (radix - 1);
|
|
|
|
multiplier = 1;
|
|
|
|
safe_ndigits = 0;
|
|
|
|
|
|
|
|
while (w <= ub)
|
|
|
|
{
|
|
|
|
w = w * radix + (radix - 1);
|
|
|
|
multiplier *= radix;
|
|
|
|
safe_ndigits++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* safe_ndigits contains the number of digits that never
|
2017-02-20 04:45:23 +00:00
|
|
|
* cause overflow when computed normally with a primitive type. */
|
2017-01-09 09:54:49 +00:00
|
|
|
moo->bigint[radix].safe_ndigits = safe_ndigits;
|
|
|
|
moo->bigint[radix].multiplier = multiplier;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-09 17:37:45 +00:00
|
|
|
static MOO_INLINE void* alloc_heap (moo_t* moo, moo_oow_t* size)
|
2018-03-01 14:48:14 +00:00
|
|
|
{
|
2021-02-09 17:37:45 +00:00
|
|
|
return moo_allocmem(moo, *size);
|
2018-03-01 14:48:14 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 07:21:58 +00:00
|
|
|
static MOO_INLINE void free_heap (moo_t* moo, void* ptr)
|
2018-03-01 14:48:14 +00:00
|
|
|
{
|
2019-06-21 07:21:58 +00:00
|
|
|
moo_freemem (moo, ptr);
|
2018-03-01 14:48:14 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 14:48:26 +00:00
|
|
|
int moo_init (moo_t* moo, moo_mmgr_t* mmgr, moo_cmgr_t* cmgr, const moo_vmprim_t* vmprim, moo_gc_type_t gctype)
|
2017-01-09 09:54:49 +00:00
|
|
|
{
|
|
|
|
int modtab_inited = 0;
|
|
|
|
|
2018-01-19 08:26:58 +00:00
|
|
|
if (!vmprim->syserrstrb && !vmprim->syserrstru)
|
|
|
|
{
|
|
|
|
moo_seterrnum (moo, MOO_EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_MEMSET (moo, 0, MOO_SIZEOF(*moo));
|
2019-06-21 07:21:58 +00:00
|
|
|
moo->_instsize = MOO_SIZEOF(*moo);
|
|
|
|
moo->_mmgr = mmgr;
|
|
|
|
moo->_cmgr = cmgr;
|
|
|
|
|
2020-11-25 14:48:26 +00:00
|
|
|
moo->gc_type = gctype;
|
2017-01-09 09:54:49 +00:00
|
|
|
moo->vmprim = *vmprim;
|
2018-03-01 14:48:14 +00:00
|
|
|
if (!moo->vmprim.alloc_heap) moo->vmprim.alloc_heap = alloc_heap;
|
|
|
|
if (!moo->vmprim.free_heap) moo->vmprim.free_heap = free_heap;
|
2017-01-09 09:54:49 +00:00
|
|
|
|
2021-02-11 13:59:03 +00:00
|
|
|
/*moo->option.log_mask = MOO_LOG_ALL_LEVELS | MOO_LOG_ALL_TYPES;*/
|
|
|
|
moo->option.log_mask = (moo_bitmask_t)0; /* log nothing by default */
|
2017-05-12 16:38:16 +00:00
|
|
|
moo->option.log_maxcapa = MOO_DFL_LOG_MAXCAPA;
|
2017-01-09 09:54:49 +00:00
|
|
|
moo->option.dfl_symtab_size = MOO_DFL_SYMTAB_SIZE;
|
|
|
|
moo->option.dfl_sysdic_size = MOO_DFL_SYSDIC_SIZE;
|
|
|
|
moo->option.dfl_procstk_size = MOO_DFL_PROCSTK_SIZE;
|
2018-02-21 09:35:59 +00:00
|
|
|
#if defined(MOO_BUILD_DEBUG)
|
2017-12-16 16:14:23 +00:00
|
|
|
moo->option.karatsuba_cutoff = MOO_KARATSUBA_CUTOFF;
|
2018-02-21 09:35:59 +00:00
|
|
|
#endif
|
2017-01-09 09:54:49 +00:00
|
|
|
|
2017-05-12 16:38:16 +00:00
|
|
|
moo->log.capa = MOO_ALIGN_POW2(1, MOO_LOG_CAPA_ALIGN); /* TODO: is this a good initial size? */
|
2017-05-12 04:15:09 +00:00
|
|
|
/* alloate the log buffer in advance though it may get reallocated
|
2019-01-25 07:49:01 +00:00
|
|
|
* in put_oocs and put_ooch in fmtout.c. this is to let the logging
|
2017-05-12 04:15:09 +00:00
|
|
|
* routine still function despite some side-effects when
|
|
|
|
* reallocation fails */
|
2019-01-25 07:49:01 +00:00
|
|
|
/* +1 required for consistency with put_oocs and put_ooch in fmtout.c */
|
2018-11-14 04:25:27 +00:00
|
|
|
moo->log.ptr = moo_allocmem(moo, (moo->log.capa + 1) * MOO_SIZEOF(*moo->log.ptr));
|
2020-10-26 16:43:09 +00:00
|
|
|
if (MOO_UNLIKELY(!moo->log.ptr)) goto oops;
|
2017-05-12 04:15:09 +00:00
|
|
|
|
2020-10-31 04:39:32 +00:00
|
|
|
#if defined(MOO_ENABLE_GC_MARK_SWEEP)
|
2020-10-26 16:43:09 +00:00
|
|
|
moo->gci.stack.capa = MOO_ALIGN_POW2(1, 1024); /* TODO: is this a good initial size? */
|
|
|
|
moo->gci.stack.ptr = moo_allocmem(moo, (moo->gci.stack.capa + 1) * MOO_SIZEOF(*moo->gci.stack.ptr));
|
|
|
|
if (MOO_UNLIKELY(!moo->gci.stack.ptr)) goto oops;
|
2020-10-31 04:39:32 +00:00
|
|
|
#endif
|
2020-10-26 16:43:09 +00:00
|
|
|
|
|
|
|
if (moo_rbt_init(&moo->modtab, moo, MOO_SIZEOF(moo_ooch_t), 1) <= -1) goto oops;
|
2017-01-09 09:54:49 +00:00
|
|
|
modtab_inited = 1;
|
2019-11-18 14:11:14 +00:00
|
|
|
moo_rbt_setstyle (&moo->modtab, moo_get_rbt_style(MOO_RBT_STYLE_INLINE_COPIERS));
|
2017-01-09 09:54:49 +00:00
|
|
|
|
|
|
|
fill_bigint_tables (moo);
|
|
|
|
|
2017-01-10 13:56:19 +00:00
|
|
|
moo->tagged_classes[MOO_OOP_TAG_SMOOI] = &moo->_small_integer;
|
2017-04-02 13:13:33 +00:00
|
|
|
moo->tagged_classes[MOO_OOP_TAG_SMPTR] = &moo->_small_pointer;
|
2017-01-09 09:54:49 +00:00
|
|
|
moo->tagged_classes[MOO_OOP_TAG_CHAR] = &moo->_character;
|
|
|
|
moo->tagged_classes[MOO_OOP_TAG_ERROR] = &moo->_error_class;
|
|
|
|
|
2017-07-30 15:15:47 +00:00
|
|
|
moo->proc_map_free_first = -1;
|
|
|
|
moo->proc_map_free_last = -1;
|
2017-12-24 17:36:20 +00:00
|
|
|
|
2018-11-03 16:11:55 +00:00
|
|
|
if (moo->vmprim.dl_startup) moo->vmprim.dl_startup (moo);
|
2017-01-09 09:54:49 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
oops:
|
|
|
|
if (modtab_inited) moo_rbt_fini (&moo->modtab);
|
2020-10-31 04:39:32 +00:00
|
|
|
#if defined(MOO_ENABLE_GC_MARK_SWEEP)
|
2020-10-26 16:43:09 +00:00
|
|
|
if (moo->gci.stack.ptr)
|
|
|
|
{
|
|
|
|
moo_freemem (moo, moo->gci.stack.ptr);
|
|
|
|
moo->gci.stack.capa = 0;
|
|
|
|
}
|
2020-10-31 04:39:32 +00:00
|
|
|
#endif
|
2020-10-26 16:43:09 +00:00
|
|
|
if (moo->log.ptr)
|
|
|
|
{
|
|
|
|
moo_freemem (moo, moo->log.ptr);
|
|
|
|
moo->log.capa = 0;
|
|
|
|
}
|
2017-01-09 09:54:49 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static moo_rbt_walk_t unload_module (moo_rbt_t* rbt, moo_rbt_pair_t* pair, void* ctx)
|
|
|
|
{
|
|
|
|
moo_t* moo = (moo_t*)ctx;
|
|
|
|
moo_mod_data_t* mdp;
|
|
|
|
|
|
|
|
mdp = MOO_RBT_VPTR(pair);
|
|
|
|
MOO_ASSERT (moo, mdp != MOO_NULL);
|
|
|
|
|
|
|
|
mdp->pair = MOO_NULL; /* to prevent moo_closemod() from calling moo_rbt_delete() */
|
|
|
|
moo_closemod (moo, mdp);
|
|
|
|
|
|
|
|
return MOO_RBT_WALK_FORWARD;
|
|
|
|
}
|
|
|
|
|
|
|
|
void moo_fini (moo_t* moo)
|
|
|
|
{
|
2018-11-13 06:54:30 +00:00
|
|
|
moo_evtcb_t* cb;
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_oow_t i;
|
|
|
|
|
2018-02-04 03:53:19 +00:00
|
|
|
moo_rbt_walk (&moo->modtab, unload_module, moo); /* unload all modules */
|
|
|
|
moo_rbt_fini (&moo->modtab);
|
|
|
|
|
2018-02-04 16:38:28 +00:00
|
|
|
if (moo->log.len > 0)
|
2018-02-04 03:53:19 +00:00
|
|
|
{
|
|
|
|
/* flush pending log messages just in case. */
|
2018-02-04 16:38:28 +00:00
|
|
|
MOO_ASSERT (moo, moo->log.ptr != MOO_NULL);
|
2018-02-22 04:44:34 +00:00
|
|
|
vmprim_log_write (moo, moo->log.last_mask, moo->log.ptr, moo->log.len);
|
2018-02-04 03:53:19 +00:00
|
|
|
}
|
|
|
|
|
2018-11-13 06:54:30 +00:00
|
|
|
for (cb = moo->evtcb_list; cb; cb = cb->next)
|
2018-02-04 03:53:19 +00:00
|
|
|
{
|
|
|
|
/* execute callbacks for finalization */
|
|
|
|
if (cb->fini) cb->fini (moo);
|
|
|
|
}
|
|
|
|
|
2018-02-04 16:38:28 +00:00
|
|
|
if (moo->log.len > 0)
|
2018-02-04 03:53:19 +00:00
|
|
|
{
|
|
|
|
/* 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 */
|
2018-02-04 16:38:28 +00:00
|
|
|
MOO_ASSERT (moo, moo->log.ptr != MOO_NULL);
|
2018-02-22 04:44:34 +00:00
|
|
|
vmprim_log_write (moo, moo->log.last_mask, moo->log.ptr, moo->log.len);
|
2018-02-04 03:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* deregister all callbacks */
|
2018-11-13 06:54:30 +00:00
|
|
|
while (moo->evtcb_list) moo_deregevtcb (moo, moo->evtcb_list);
|
2018-02-04 03:53:19 +00:00
|
|
|
|
|
|
|
/* free up internal data structures. this is done after all callbacks
|
|
|
|
* are completed */
|
2017-01-09 09:54:49 +00:00
|
|
|
if (moo->sem_list)
|
|
|
|
{
|
|
|
|
moo_freemem (moo, moo->sem_list);
|
|
|
|
moo->sem_list_capa = 0;
|
|
|
|
moo->sem_list_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (moo->sem_heap)
|
|
|
|
{
|
|
|
|
moo_freemem (moo, moo->sem_heap);
|
|
|
|
moo->sem_heap_capa = 0;
|
|
|
|
moo->sem_heap_count = 0;
|
|
|
|
}
|
|
|
|
|
2017-12-25 18:35:23 +00:00
|
|
|
if (moo->sem_io_tuple)
|
2017-02-12 18:59:03 +00:00
|
|
|
{
|
2017-12-25 18:35:23 +00:00
|
|
|
moo_freemem (moo, moo->sem_io_tuple);
|
2017-12-24 17:36:20 +00:00
|
|
|
moo->sem_io_tuple_capa = 0;
|
|
|
|
moo->sem_io_tuple_count = 0;
|
2017-02-12 18:59:03 +00:00
|
|
|
}
|
|
|
|
|
2017-12-26 15:55:06 +00:00
|
|
|
if (moo->sem_io_map)
|
|
|
|
{
|
|
|
|
moo_freemem (moo, moo->sem_io_map);
|
|
|
|
moo->sem_io_map_capa = 0;
|
|
|
|
}
|
|
|
|
|
2017-07-27 17:29:45 +00:00
|
|
|
if (moo->proc_map)
|
|
|
|
{
|
|
|
|
moo_freemem (moo, moo->proc_map);
|
|
|
|
moo->proc_map_capa = 0;
|
2019-11-04 14:53:33 +00:00
|
|
|
moo->proc_map_used = 0;
|
2017-07-30 15:15:47 +00:00
|
|
|
moo->proc_map_free_first = -1;
|
|
|
|
moo->proc_map_free_last = -1;
|
2017-07-27 17:29:45 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 14:48:26 +00:00
|
|
|
/* TOOD: persistency? storing objects to image file before destroying all objects and the heap? */
|
2017-01-09 09:54:49 +00:00
|
|
|
|
2020-10-23 16:55:11 +00:00
|
|
|
#if defined(MOO_ENABLE_GC_MARK_SWEEP)
|
2020-10-26 16:43:09 +00:00
|
|
|
if (moo->gci.b)
|
2020-10-23 16:55:11 +00:00
|
|
|
{
|
|
|
|
moo_gchdr_t* next;
|
|
|
|
do
|
|
|
|
{
|
2020-10-26 16:43:09 +00:00
|
|
|
next = moo->gci.b->next;
|
|
|
|
moo->gci.bsz -= MOO_SIZEOF(moo_obj_t) + moo_getobjpayloadbytes(moo, (moo_oop_t)(moo->gci.b + 1));
|
2020-11-25 14:48:26 +00:00
|
|
|
moo_freeheapmem (moo, moo->heap, moo->gci.b);
|
2020-10-26 16:43:09 +00:00
|
|
|
moo->gci.b = next;
|
2020-10-23 16:55:11 +00:00
|
|
|
}
|
2020-10-26 16:43:09 +00:00
|
|
|
while (moo->gci.b);
|
|
|
|
|
2020-11-27 05:13:02 +00:00
|
|
|
MOO_ASSERT (moo, moo->gci.bsz == 0);
|
2020-10-26 16:43:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (moo->gci.stack.ptr)
|
|
|
|
{
|
|
|
|
moo_freemem (moo, moo->gci.stack.ptr);
|
|
|
|
moo->gci.stack.ptr = 0;
|
|
|
|
moo->gci.stack.capa = 0;
|
|
|
|
moo->gci.stack.len = 0;
|
2020-10-23 16:55:11 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-11-25 14:48:26 +00:00
|
|
|
/* if the moo object is closed without moo_ignite(),
|
|
|
|
* the heap may not exist */
|
|
|
|
if (moo->heap) moo_killheap (moo, moo->heap);
|
|
|
|
|
2019-07-10 09:19:38 +00:00
|
|
|
moo_finidbgi (moo);
|
2019-07-07 15:24:27 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
for (i = 0; i < MOO_COUNTOF(moo->sbuf); i++)
|
|
|
|
{
|
|
|
|
if (moo->sbuf[i].ptr)
|
|
|
|
{
|
|
|
|
moo_freemem (moo, moo->sbuf[i].ptr);
|
|
|
|
moo->sbuf[i].ptr = MOO_NULL;
|
|
|
|
moo->sbuf[i].len = 0;
|
|
|
|
moo->sbuf[i].capa = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (moo->log.ptr)
|
|
|
|
{
|
|
|
|
moo_freemem (moo, moo->log.ptr);
|
|
|
|
moo->log.capa = 0;
|
|
|
|
moo->log.len = 0;
|
|
|
|
}
|
2018-02-17 13:32:30 +00:00
|
|
|
|
|
|
|
if (moo->inttostr.xbuf.ptr)
|
|
|
|
{
|
|
|
|
moo_freemem (moo, moo->inttostr.xbuf.ptr);
|
|
|
|
moo->inttostr.xbuf.ptr = MOO_NULL;
|
|
|
|
moo->inttostr.xbuf.capa = 0;
|
|
|
|
moo->inttostr.xbuf.len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (moo->inttostr.t.ptr)
|
|
|
|
{
|
|
|
|
moo_freemem (moo, moo->inttostr.t.ptr);
|
|
|
|
moo->inttostr.t.ptr = MOO_NULL;
|
|
|
|
moo->inttostr.t.capa = 0;
|
|
|
|
}
|
2018-11-03 16:11:55 +00:00
|
|
|
|
2018-12-21 16:25:25 +00:00
|
|
|
if (moo->sprintf.xbuf.ptr)
|
|
|
|
{
|
2019-02-20 09:08:10 +00:00
|
|
|
moo_freemem (moo, moo->sprintf.xbuf.ptr);
|
|
|
|
moo->sprintf.xbuf.ptr = MOO_NULL;
|
|
|
|
moo->sprintf.xbuf.capa = 0;
|
|
|
|
moo->sprintf.xbuf.len = 0;
|
2018-12-21 16:25:25 +00:00
|
|
|
}
|
|
|
|
|
2018-11-03 16:11:55 +00:00
|
|
|
if (moo->vmprim.dl_cleanup) moo->vmprim.dl_cleanup (moo);
|
2017-01-09 09:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int moo_setoption (moo_t* moo, moo_option_t id, const void* value)
|
|
|
|
{
|
|
|
|
switch (id)
|
|
|
|
{
|
2019-05-14 10:22:37 +00:00
|
|
|
case MOO_OPTION_TRAIT:
|
2018-04-26 04:44:01 +00:00
|
|
|
moo->option.trait = *(moo_bitmask_t*)value;
|
2018-02-21 09:35:59 +00:00
|
|
|
#if defined(MOO_BUILD_DEBUG)
|
2019-05-14 10:22:37 +00:00
|
|
|
moo->option.karatsuba_cutoff = ((moo->option.trait & MOO_TRAIT_DEBUG_BIGINT)? MOO_KARATSUBA_CUTOFF_DEBUG: MOO_KARATSUBA_CUTOFF);
|
2017-12-16 16:14:23 +00:00
|
|
|
#endif
|
2017-01-09 09:54:49 +00:00
|
|
|
return 0;
|
|
|
|
|
2019-05-14 10:22:37 +00:00
|
|
|
case MOO_OPTION_LOG_MASK:
|
2018-04-26 04:44:01 +00:00
|
|
|
moo->option.log_mask = *(moo_bitmask_t*)value;
|
2017-01-09 09:54:49 +00:00
|
|
|
return 0;
|
|
|
|
|
2019-05-14 10:22:37 +00:00
|
|
|
case MOO_OPTION_LOG_MAXCAPA:
|
2017-05-12 16:38:16 +00:00
|
|
|
moo->option.log_maxcapa = *(moo_oow_t*)value;
|
|
|
|
return 0;
|
|
|
|
|
2019-05-14 10:22:37 +00:00
|
|
|
case MOO_OPTION_SYMTAB_SIZE:
|
2017-01-09 09:54:49 +00:00
|
|
|
{
|
|
|
|
moo_oow_t w;
|
|
|
|
|
|
|
|
w = *(moo_oow_t*)value;
|
|
|
|
if (w <= 0 || w > MOO_SMOOI_MAX) goto einval;
|
|
|
|
|
|
|
|
moo->option.dfl_symtab_size = *(moo_oow_t*)value;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-14 10:22:37 +00:00
|
|
|
case MOO_OPTION_SYSDIC_SIZE:
|
2017-01-09 09:54:49 +00:00
|
|
|
{
|
|
|
|
moo_oow_t w;
|
|
|
|
|
|
|
|
w = *(moo_oow_t*)value;
|
|
|
|
if (w <= 0 || w > MOO_SMOOI_MAX) goto einval;
|
|
|
|
|
|
|
|
moo->option.dfl_sysdic_size = *(moo_oow_t*)value;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-14 10:22:37 +00:00
|
|
|
case MOO_OPTION_PROCSTK_SIZE:
|
2017-01-09 09:54:49 +00:00
|
|
|
{
|
|
|
|
moo_oow_t w;
|
|
|
|
|
|
|
|
w = *(moo_oow_t*)value;
|
|
|
|
if (w <= 0 || w > MOO_SMOOI_MAX) goto einval;
|
|
|
|
|
|
|
|
moo->option.dfl_procstk_size = *(moo_oow_t*)value;
|
|
|
|
return 0;
|
|
|
|
}
|
2018-04-09 16:31:11 +00:00
|
|
|
|
2019-05-14 10:22:37 +00:00
|
|
|
case MOO_OPTION_MOD_INCTX:
|
2018-04-09 16:31:11 +00:00
|
|
|
moo->option.mod_inctx = *(void**)value;
|
|
|
|
return 0;
|
2017-01-09 09:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
einval:
|
2017-05-11 14:59:20 +00:00
|
|
|
moo_seterrnum (moo, MOO_EINVAL);
|
2017-01-09 09:54:49 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int moo_getoption (moo_t* moo, moo_option_t id, void* value)
|
|
|
|
{
|
2019-05-14 10:22:37 +00:00
|
|
|
switch (id)
|
2017-01-09 09:54:49 +00:00
|
|
|
{
|
2019-05-14 10:22:37 +00:00
|
|
|
case MOO_OPTION_TRAIT:
|
2018-04-26 04:44:01 +00:00
|
|
|
*(moo_bitmask_t*)value = moo->option.trait;
|
2017-01-09 09:54:49 +00:00
|
|
|
return 0;
|
|
|
|
|
2019-05-14 10:22:37 +00:00
|
|
|
case MOO_OPTION_LOG_MASK:
|
2018-04-26 04:44:01 +00:00
|
|
|
*(moo_bitmask_t*)value = moo->option.log_mask;
|
2017-01-09 09:54:49 +00:00
|
|
|
return 0;
|
|
|
|
|
2019-05-14 10:22:37 +00:00
|
|
|
case MOO_OPTION_LOG_MAXCAPA:
|
2017-05-21 16:57:21 +00:00
|
|
|
*(moo_oow_t*)value = moo->option.log_maxcapa;
|
2017-05-12 16:38:16 +00:00
|
|
|
return 0;
|
|
|
|
|
2019-05-14 10:22:37 +00:00
|
|
|
case MOO_OPTION_SYMTAB_SIZE:
|
2017-01-09 09:54:49 +00:00
|
|
|
*(moo_oow_t*)value = moo->option.dfl_symtab_size;
|
|
|
|
return 0;
|
|
|
|
|
2019-05-14 10:22:37 +00:00
|
|
|
case MOO_OPTION_SYSDIC_SIZE:
|
2017-01-09 09:54:49 +00:00
|
|
|
*(moo_oow_t*)value = moo->option.dfl_sysdic_size;
|
|
|
|
return 0;
|
|
|
|
|
2019-05-14 10:22:37 +00:00
|
|
|
case MOO_OPTION_PROCSTK_SIZE:
|
2017-01-09 09:54:49 +00:00
|
|
|
*(moo_oow_t*)value = moo->option.dfl_procstk_size;
|
|
|
|
return 0;
|
2018-04-09 16:31:11 +00:00
|
|
|
|
2019-05-14 10:22:37 +00:00
|
|
|
case MOO_OPTION_MOD_INCTX:
|
2018-04-09 16:31:11 +00:00
|
|
|
*(void**)value = moo->option.mod_inctx;
|
|
|
|
return 0;
|
2017-01-09 09:54:49 +00:00
|
|
|
};
|
|
|
|
|
2017-05-11 14:59:20 +00:00
|
|
|
moo_seterrnum (moo, MOO_EINVAL);
|
2017-01-09 09:54:49 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-05-14 10:22:37 +00:00
|
|
|
moo_evtcb_t* moo_regevtcb (moo_t* moo, moo_evtcb_t* cb)
|
2017-01-09 09:54:49 +00:00
|
|
|
{
|
2018-11-13 06:54:30 +00:00
|
|
|
moo_evtcb_t* actual;
|
2017-01-09 09:54:49 +00:00
|
|
|
|
2018-11-13 06:54:30 +00:00
|
|
|
actual = (moo_evtcb_t*)moo_allocmem(moo, MOO_SIZEOF(*actual));
|
2017-01-09 09:54:49 +00:00
|
|
|
if (!actual) return MOO_NULL;
|
|
|
|
|
2019-05-14 10:22:37 +00:00
|
|
|
*actual = *cb;
|
2018-11-13 06:54:30 +00:00
|
|
|
actual->next = moo->evtcb_list;
|
2017-01-09 09:54:49 +00:00
|
|
|
actual->prev = MOO_NULL;
|
2018-11-13 06:54:30 +00:00
|
|
|
moo->evtcb_list = actual;
|
2017-01-09 09:54:49 +00:00
|
|
|
|
|
|
|
return actual;
|
|
|
|
}
|
|
|
|
|
2018-11-13 06:54:30 +00:00
|
|
|
void moo_deregevtcb (moo_t* moo, moo_evtcb_t* cb)
|
2017-01-09 09:54:49 +00:00
|
|
|
{
|
2018-11-13 06:54:30 +00:00
|
|
|
if (cb == moo->evtcb_list)
|
2017-01-09 09:54:49 +00:00
|
|
|
{
|
2018-11-13 06:54:30 +00:00
|
|
|
moo->evtcb_list = moo->evtcb_list->next;
|
|
|
|
if (moo->evtcb_list) moo->evtcb_list->prev = MOO_NULL;
|
2017-01-09 09:54:49 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (cb->next) cb->next->prev = cb->prev;
|
|
|
|
if (cb->prev) cb->prev->next = cb->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
moo_freemem (moo, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
void* moo_allocmem (moo_t* moo, moo_oow_t size)
|
|
|
|
{
|
|
|
|
void* ptr;
|
|
|
|
|
2019-06-21 07:21:58 +00:00
|
|
|
ptr = MOO_MMGR_ALLOC(moo_getmmgr(moo), size);
|
2017-05-11 14:59:20 +00:00
|
|
|
if (!ptr) moo_seterrnum (moo, MOO_ESYSMEM);
|
2017-01-09 09:54:49 +00:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* moo_callocmem (moo_t* moo, moo_oow_t size)
|
|
|
|
{
|
|
|
|
void* ptr;
|
|
|
|
|
2019-06-21 07:21:58 +00:00
|
|
|
ptr = MOO_MMGR_ALLOC(moo_getmmgr(moo), size);
|
2017-05-11 14:59:20 +00:00
|
|
|
if (!ptr) moo_seterrnum (moo, MOO_ESYSMEM);
|
2017-01-09 09:54:49 +00:00
|
|
|
else MOO_MEMSET (ptr, 0, size);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* moo_reallocmem (moo_t* moo, void* ptr, moo_oow_t size)
|
|
|
|
{
|
2019-06-21 07:21:58 +00:00
|
|
|
ptr = MOO_MMGR_REALLOC (moo_getmmgr(moo), ptr, size);
|
2017-05-11 14:59:20 +00:00
|
|
|
if (!ptr) moo_seterrnum (moo, MOO_ESYSMEM);
|
2017-01-09 09:54:49 +00:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void moo_freemem (moo_t* moo, void* ptr)
|
|
|
|
{
|
2019-06-21 07:21:58 +00:00
|
|
|
MOO_MMGR_FREE (moo_getmmgr(moo), ptr);
|
2017-01-09 09:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
#define MOD_PREFIX "moo_mod_"
|
2017-02-04 05:30:15 +00:00
|
|
|
#define MOD_PREFIX_LEN 8
|
2017-01-09 09:54:49 +00:00
|
|
|
|
|
|
|
#if defined(MOO_ENABLE_STATIC_MODULE)
|
|
|
|
|
2017-12-13 10:57:31 +00:00
|
|
|
#if defined(MOO_ENABLE_MOD_CON)
|
|
|
|
# include "../mod/_con.h"
|
|
|
|
#endif
|
2019-10-24 09:37:47 +00:00
|
|
|
|
2017-07-27 08:32:16 +00:00
|
|
|
#if defined(MOO_ENABLE_MOD_FFI)
|
|
|
|
# include "../mod/_ffi.h"
|
|
|
|
#endif
|
2019-10-24 09:37:47 +00:00
|
|
|
|
|
|
|
#include "../mod/_io.h"
|
|
|
|
|
2017-10-18 16:15:51 +00:00
|
|
|
#if defined(MOO_ENABLE_MOD_SCK)
|
|
|
|
# include "../mod/_sck.h"
|
|
|
|
#endif
|
2019-10-24 09:37:47 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
#include "../mod/_stdio.h"
|
2019-10-24 09:37:47 +00:00
|
|
|
|
2017-07-27 08:32:16 +00:00
|
|
|
#if defined(MOO_ENABLE_MOD_X11)
|
|
|
|
# include "../mod/_x11.h"
|
|
|
|
#endif
|
2017-01-09 09:54:49 +00:00
|
|
|
|
|
|
|
static struct
|
|
|
|
{
|
2019-05-04 17:53:16 +00:00
|
|
|
const moo_bch_t* modname;
|
2017-01-09 09:54:49 +00:00
|
|
|
int (*modload) (moo_t* moo, moo_mod_t* mod);
|
|
|
|
}
|
|
|
|
static_modtab[] =
|
|
|
|
{
|
2017-12-13 10:57:31 +00:00
|
|
|
#if defined(MOO_ENABLE_MOD_CON)
|
|
|
|
{ "con", moo_mod_con },
|
|
|
|
#endif
|
2017-04-28 04:03:52 +00:00
|
|
|
#if defined(MOO_ENABLE_MOD_FFI)
|
2017-02-13 13:25:42 +00:00
|
|
|
{ "ffi", moo_mod_ffi },
|
2017-10-18 16:15:51 +00:00
|
|
|
#endif
|
2019-10-24 09:37:47 +00:00
|
|
|
{ "io", moo_mod_io },
|
|
|
|
{ "io.file", moo_mod_io_file },
|
2017-10-18 16:15:51 +00:00
|
|
|
#if defined(MOO_ENABLE_MOD_SCK)
|
|
|
|
{ "sck", moo_mod_sck },
|
2017-12-30 19:07:31 +00:00
|
|
|
{ "sck.addr", moo_mod_sck_addr },
|
2017-04-28 04:03:52 +00:00
|
|
|
#endif
|
2017-02-13 13:25:42 +00:00
|
|
|
{ "stdio", moo_mod_stdio },
|
2017-04-28 04:03:52 +00:00
|
|
|
#if defined(MOO_ENABLE_MOD_X11)
|
2017-02-13 13:25:42 +00:00
|
|
|
{ "x11", moo_mod_x11 },
|
2017-06-27 13:47:58 +00:00
|
|
|
/*{ "x11.win", moo_mod_x11_win },*/
|
2017-04-28 04:03:52 +00:00
|
|
|
#endif
|
2017-01-09 09:54:49 +00:00
|
|
|
};
|
2019-10-24 09:37:47 +00:00
|
|
|
#endif /* MOO_ENABLE_STATIC_MODULE */
|
2017-01-09 09:54:49 +00:00
|
|
|
|
2017-03-19 14:18:37 +00:00
|
|
|
moo_mod_data_t* moo_openmod (moo_t* moo, const moo_ooch_t* name, moo_oow_t namelen, int hints)
|
2017-01-09 09:54:49 +00:00
|
|
|
{
|
|
|
|
moo_rbt_pair_t* pair;
|
|
|
|
moo_mod_data_t* mdp;
|
|
|
|
moo_mod_data_t md;
|
|
|
|
moo_mod_load_t load = MOO_NULL;
|
|
|
|
#if defined(MOO_ENABLE_STATIC_MODULE)
|
|
|
|
int n;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* 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];
|
|
|
|
|
|
|
|
/* 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. */
|
2018-04-07 15:54:09 +00:00
|
|
|
moo_copy_bchars_to_oochars (buf, MOD_PREFIX, MOD_PREFIX_LEN);
|
2017-01-09 09:54:49 +00:00
|
|
|
|
|
|
|
if (namelen > MOO_COUNTOF(buf) - (MOD_PREFIX_LEN + 1 + 1))
|
|
|
|
{
|
|
|
|
/* module name too long */
|
2017-05-11 14:59:20 +00:00
|
|
|
moo_seterrnum (moo, MOO_EINVAL); /* TODO: change the error number to something more specific */
|
2017-01-09 09:54:49 +00:00
|
|
|
return MOO_NULL;
|
|
|
|
}
|
|
|
|
|
2018-04-07 15:54:09 +00:00
|
|
|
moo_copy_oochars (&buf[MOD_PREFIX_LEN], name, namelen);
|
2017-01-09 09:54:49 +00:00
|
|
|
buf[MOD_PREFIX_LEN + namelen] = '\0';
|
|
|
|
|
|
|
|
#if defined(MOO_ENABLE_STATIC_MODULE)
|
|
|
|
/* attempt to find a statically linked module */
|
|
|
|
|
|
|
|
/* TODO: binary search ... */
|
|
|
|
for (n = 0; n < MOO_COUNTOF(static_modtab); n++)
|
|
|
|
{
|
2018-04-07 15:54:09 +00:00
|
|
|
if (moo_comp_oochars_bcstr(name, namelen, static_modtab[n].modname) == 0)
|
2017-01-09 09:54:49 +00:00
|
|
|
{
|
|
|
|
load = static_modtab[n].modload;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (load)
|
|
|
|
{
|
|
|
|
/* found the module in the staic module table */
|
|
|
|
|
|
|
|
MOO_MEMSET (&md, 0, MOO_SIZEOF(md));
|
2018-04-09 16:31:11 +00:00
|
|
|
md.mod.inctx = moo->option.mod_inctx;
|
2018-04-07 15:54:09 +00:00
|
|
|
moo_copy_oochars ((moo_ooch_t*)md.mod.name, name, namelen);
|
2017-01-09 09:54:49 +00:00
|
|
|
/* Note md.handle is MOO_NULL for a static module */
|
|
|
|
|
|
|
|
/* i copy-insert 'md' into the table before calling 'load'.
|
|
|
|
* to pass the same address to load(), query(), etc */
|
2018-02-12 16:50:32 +00:00
|
|
|
pair = moo_rbt_insert(&moo->modtab, (moo_ooch_t*)name, namelen, &md, MOO_SIZEOF(md));
|
2017-01-09 09:54:49 +00:00
|
|
|
if (pair == MOO_NULL)
|
|
|
|
{
|
2017-05-11 14:59:20 +00:00
|
|
|
moo_seterrnum (moo, MOO_ESYSMEM);
|
2017-01-09 09:54:49 +00:00
|
|
|
return MOO_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
mdp = (moo_mod_data_t*)MOO_RBT_VPTR(pair);
|
2017-03-19 14:18:37 +00:00
|
|
|
MOO_ASSERT (moo, MOO_SIZEOF(mdp->mod.hints) == MOO_SIZEOF(int));
|
2017-04-28 14:45:13 +00:00
|
|
|
mdp->mod.hints = hints;
|
2018-02-12 16:50:32 +00:00
|
|
|
if (load(moo, &mdp->mod) <= -1)
|
2017-01-09 09:54:49 +00:00
|
|
|
{
|
|
|
|
moo_rbt_delete (&moo->modtab, (moo_ooch_t*)name, namelen);
|
|
|
|
return MOO_NULL;
|
|
|
|
}
|
|
|
|
|
2017-03-19 14:18:37 +00:00
|
|
|
mdp->pair = pair;
|
|
|
|
|
|
|
|
MOO_DEBUG1 (moo, "Opened a static module [%js]\n", mdp->mod.name);
|
2017-01-09 09:54:49 +00:00
|
|
|
return mdp;
|
|
|
|
}
|
2017-03-19 14:18:37 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
#if !defined(MOO_ENABLE_DYNAMIC_MODULE)
|
|
|
|
MOO_DEBUG2 (moo, "Cannot find a static module [%.*js]\n", namelen, name);
|
2018-02-12 10:12:17 +00:00
|
|
|
moo_seterrbfmt (moo, MOO_ENOENT, "unable to find a static module [%.*js]", namelen, name);
|
2017-03-19 14:18:37 +00:00
|
|
|
return MOO_NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(MOO_ENABLE_DYNAMIC_MODULE)
|
|
|
|
MOO_DEBUG2 (moo, "Cannot open module [%.*js] - module loading disabled\n", namelen, name);
|
2018-02-12 10:12:17 +00:00
|
|
|
moo_seterrbfmt (moo, MOO_ENOIMPL, "unable to open module [%.*js] - module loading disabled", namelen, name);
|
2017-03-19 14:18:37 +00:00
|
|
|
return MOO_NULL;
|
2017-01-09 09:54:49 +00:00
|
|
|
#endif
|
|
|
|
|
2017-03-19 14:18:37 +00:00
|
|
|
/* attempt to find a dynamic external module */
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_MEMSET (&md, 0, MOO_SIZEOF(md));
|
2018-04-09 16:31:11 +00:00
|
|
|
md.mod.inctx = moo->option.mod_inctx;
|
2018-04-07 15:54:09 +00:00
|
|
|
moo_copy_oochars ((moo_ooch_t*)md.mod.name, name, namelen);
|
2017-01-09 09:54:49 +00:00
|
|
|
if (moo->vmprim.dl_open && moo->vmprim.dl_getsym && moo->vmprim.dl_close)
|
|
|
|
{
|
2018-04-12 02:49:22 +00:00
|
|
|
md.handle = moo->vmprim.dl_open(moo, &buf[MOD_PREFIX_LEN], MOO_VMPRIM_DLOPEN_PFMOD);
|
2017-01-09 09:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (md.handle == MOO_NULL)
|
|
|
|
{
|
|
|
|
MOO_DEBUG2 (moo, "Cannot open a module [%.*js]\n", namelen, name);
|
2018-02-12 10:12:17 +00:00
|
|
|
moo_seterrbfmt (moo, MOO_ENOENT, "unable to open a module [%.*js]", namelen, name);
|
2017-01-09 09:54:49 +00:00
|
|
|
return MOO_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* attempt to get moo_mod_xxx where xxx is the module name*/
|
2018-02-12 16:50:32 +00:00
|
|
|
load = moo->vmprim.dl_getsym(moo, md.handle, buf);
|
2017-01-09 09:54:49 +00:00
|
|
|
if (!load)
|
|
|
|
{
|
2018-09-30 16:14:53 +00:00
|
|
|
moo_seterrbfmt (moo, moo_geterrnum(moo), "unable to get module symbol [%js] in [%.*js]", buf, namelen, name);
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_DEBUG3 (moo, "Cannot get a module symbol [%js] in [%.*js]\n", buf, namelen, name);
|
|
|
|
moo->vmprim.dl_close (moo, md.handle);
|
|
|
|
return MOO_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* i copy-insert 'md' into the table before calling 'load'.
|
|
|
|
* to pass the same address to load(), query(), etc */
|
2018-02-12 16:50:32 +00:00
|
|
|
pair = moo_rbt_insert(&moo->modtab, (void*)name, namelen, &md, MOO_SIZEOF(md));
|
2017-01-09 09:54:49 +00:00
|
|
|
if (pair == MOO_NULL)
|
|
|
|
{
|
|
|
|
MOO_DEBUG2 (moo, "Cannot register a module [%.*js]\n", namelen, name);
|
2018-02-12 10:12:17 +00:00
|
|
|
moo_seterrbfmt (moo, MOO_ESYSMEM, "unable to register a module [%.*js] for memory shortage", namelen, name);
|
2017-01-09 09:54:49 +00:00
|
|
|
moo->vmprim.dl_close (moo, md.handle);
|
|
|
|
return MOO_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
mdp = (moo_mod_data_t*)MOO_RBT_VPTR(pair);
|
2017-03-19 14:18:37 +00:00
|
|
|
MOO_ASSERT (moo, MOO_SIZEOF(mdp->mod.hints) == MOO_SIZEOF(int));
|
2017-04-28 14:45:13 +00:00
|
|
|
mdp->mod.hints = hints;
|
2018-02-12 16:50:32 +00:00
|
|
|
if (load(moo, &mdp->mod) <= -1)
|
2017-01-09 09:54:49 +00:00
|
|
|
{
|
2018-02-12 10:19:33 +00:00
|
|
|
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);
|
2017-01-09 09:54:49 +00:00
|
|
|
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);
|
|
|
|
return MOO_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
mdp->pair = pair;
|
|
|
|
|
|
|
|
MOO_DEBUG2 (moo, "Opened a module [%js] - %p\n", mdp->mod.name, mdp->handle);
|
|
|
|
|
|
|
|
/* the module loader must ensure to set a proper query handler */
|
2019-10-25 08:44:05 +00:00
|
|
|
MOO_ASSERT (moo, mdp->mod.querypf != MOO_NULL);
|
2017-01-09 09:54:49 +00:00
|
|
|
|
|
|
|
return mdp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void moo_closemod (moo_t* moo, moo_mod_data_t* mdp)
|
|
|
|
{
|
|
|
|
if (mdp->mod.unload) mdp->mod.unload (moo, &mdp->mod);
|
|
|
|
|
|
|
|
if (mdp->handle)
|
|
|
|
{
|
|
|
|
moo->vmprim.dl_close (moo, mdp->handle);
|
|
|
|
MOO_DEBUG2 (moo, "Closed a module [%js] - %p\n", mdp->mod.name, mdp->handle);
|
|
|
|
mdp->handle = MOO_NULL;
|
|
|
|
}
|
2017-03-19 14:18:37 +00:00
|
|
|
else
|
|
|
|
{
|
2017-05-07 14:32:38 +00:00
|
|
|
MOO_DEBUG1 (moo, "Closed a static module [%js]\n", mdp->mod.name);
|
2017-03-19 14:18:37 +00:00
|
|
|
}
|
2017-01-09 09:54:49 +00:00
|
|
|
|
|
|
|
if (mdp->pair)
|
|
|
|
{
|
|
|
|
/*mdp->pair = MOO_NULL;*/ /* this reset isn't needed as the area will get freed by moo_rbt_delete()) */
|
2018-04-07 15:54:09 +00:00
|
|
|
moo_rbt_delete (&moo->modtab, mdp->mod.name, moo_count_oocstr(mdp->mod.name));
|
2017-01-09 09:54:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-15 11:57:24 +00:00
|
|
|
int moo_importmod (moo_t* moo, moo_oop_class_t _class, const moo_ooch_t* name, moo_oow_t len)
|
2017-01-09 09:54:49 +00:00
|
|
|
{
|
|
|
|
moo_rbt_pair_t* pair;
|
|
|
|
moo_mod_data_t* mdp;
|
2017-03-19 14:18:37 +00:00
|
|
|
int r = -1;
|
2017-01-09 09:54:49 +00:00
|
|
|
|
2017-02-14 08:29:30 +00:00
|
|
|
/* moo_openmod(), moo_closemod(), etc call a user-defined callback.
|
2017-01-09 09:54:49 +00:00
|
|
|
* i need to protect _class in case the user-defined callback allocates
|
2017-02-18 15:00:45 +00:00
|
|
|
* a OOP memory chunk and GC occurs. */
|
|
|
|
|
2018-12-28 08:29:27 +00:00
|
|
|
moo_pushvolat (moo, (moo_oop_t*)&_class);
|
2017-01-09 09:54:49 +00:00
|
|
|
|
2018-05-07 16:52:51 +00:00
|
|
|
pair = moo_rbt_search(&moo->modtab, name, len);
|
2017-01-09 09:54:49 +00:00
|
|
|
if (pair)
|
|
|
|
{
|
|
|
|
mdp = (moo_mod_data_t*)MOO_RBT_VPTR(pair);
|
|
|
|
MOO_ASSERT (moo, mdp != MOO_NULL);
|
2017-03-19 14:18:37 +00:00
|
|
|
|
|
|
|
MOO_DEBUG1 (moo, "Cannot import module [%js] - already active\n", mdp->mod.name);
|
2018-02-09 04:27:31 +00:00
|
|
|
moo_seterrbfmt (moo, MOO_EPERM, "unable to import module [%js] - already active", mdp->mod.name);
|
2017-03-19 14:18:37 +00:00
|
|
|
goto done2;
|
2017-01-09 09:54:49 +00:00
|
|
|
}
|
2017-03-19 14:18:37 +00:00
|
|
|
|
2018-05-07 16:52:51 +00:00
|
|
|
mdp = moo_openmod(moo, name, len, MOO_MOD_LOAD_FOR_IMPORT);
|
2017-03-19 14:18:37 +00:00
|
|
|
if (!mdp) goto done2;
|
2017-01-09 09:54:49 +00:00
|
|
|
|
|
|
|
if (!mdp->mod.import)
|
|
|
|
{
|
|
|
|
MOO_DEBUG1 (moo, "Cannot import module [%js] - importing not supported by the module\n", mdp->mod.name);
|
2018-02-09 04:27:31 +00:00
|
|
|
moo_seterrbfmt (moo, MOO_ENOIMPL, "unable to import module [%js] - not supported by the module", mdp->mod.name);
|
2017-01-09 09:54:49 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2018-05-07 16:52:51 +00:00
|
|
|
if (mdp->mod.import(moo, &mdp->mod, _class) <= -1)
|
2017-01-09 09:54:49 +00:00
|
|
|
{
|
|
|
|
MOO_DEBUG1 (moo, "Cannot import module [%js] - module's import() returned failure\n", mdp->mod.name);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2017-03-19 14:18:37 +00:00
|
|
|
r = 0; /* everything successful */
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
done:
|
2017-03-19 14:18:37 +00:00
|
|
|
/* close the module opened above.
|
2019-10-25 08:44:05 +00:00
|
|
|
* [NOTE] if the import callback calls the moo_querymodpf(), the returned
|
2017-03-19 14:18:37 +00:00
|
|
|
* function pointers will get all invalidated here. so never do
|
|
|
|
* anything like that */
|
|
|
|
moo_closemod (moo, mdp);
|
2017-01-09 09:54:49 +00:00
|
|
|
|
|
|
|
done2:
|
2018-12-28 08:29:27 +00:00
|
|
|
moo_popvolat (moo);
|
2017-01-09 09:54:49 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2019-10-25 08:44:05 +00:00
|
|
|
static void* query_mod (moo_t* moo, const moo_ooch_t* pid, moo_oow_t pidlen, moo_mod_t** mod, int pv)
|
2017-01-09 09:54:49 +00:00
|
|
|
{
|
2019-10-25 08:44:05 +00:00
|
|
|
/* primitive function/value identifier
|
2019-10-25 03:22:40 +00:00
|
|
|
* modname.funcname
|
|
|
|
* modname.modname2.funcname
|
2017-01-09 09:54:49 +00:00
|
|
|
*/
|
|
|
|
moo_rbt_pair_t* pair;
|
|
|
|
moo_mod_data_t* mdp;
|
|
|
|
const moo_ooch_t* sep;
|
|
|
|
|
|
|
|
moo_oow_t mod_name_len;
|
2019-10-25 08:44:05 +00:00
|
|
|
void* base;
|
2017-01-09 09:54:49 +00:00
|
|
|
|
2019-10-25 08:44:05 +00:00
|
|
|
static const moo_bch_t* tags[] =
|
|
|
|
{
|
|
|
|
"function",
|
|
|
|
"value"
|
|
|
|
};
|
|
|
|
|
|
|
|
sep = moo_rfind_oochar(pid, pidlen, '.');
|
2017-01-09 09:54:49 +00:00
|
|
|
if (!sep)
|
|
|
|
{
|
|
|
|
/* i'm writing a conservative code here. the compiler should
|
2017-02-20 04:45:23 +00:00
|
|
|
* guarantee that a period is included in an primitive function identifer.
|
2017-01-09 09:54:49 +00:00
|
|
|
* what if the compiler is broken? imagine a buggy compiler rewritten
|
|
|
|
* in moo itself? */
|
2019-10-25 08:44:05 +00:00
|
|
|
MOO_DEBUG3 (moo, "Internal error - no period in a primitive %hs identifier [%.*js] - buggy compiler?\n", tags[pv], pidlen, pid);
|
|
|
|
moo_seterrbfmt (moo, MOO_EINTERN, "no period in a primitive %hs identifier [%.*js]", tags[pv], pidlen, pid);
|
2017-01-09 09:54:49 +00:00
|
|
|
return MOO_NULL;
|
|
|
|
}
|
|
|
|
|
2019-10-25 08:44:05 +00:00
|
|
|
mod_name_len = sep - pid;
|
2017-01-09 09:54:49 +00:00
|
|
|
|
2017-03-19 14:18:37 +00:00
|
|
|
/* the first segment through the segment before the last compose a
|
|
|
|
* module id. the last segment is the primitive function name.
|
2017-02-13 13:25:42 +00:00
|
|
|
* for instance, in con.window.open, con.window is a module id and
|
|
|
|
* open is the primitive function name. */
|
2019-10-25 08:44:05 +00:00
|
|
|
pair = moo_rbt_search(&moo->modtab, pid, mod_name_len);
|
2017-01-09 09:54:49 +00:00
|
|
|
if (pair)
|
|
|
|
{
|
|
|
|
mdp = (moo_mod_data_t*)MOO_RBT_VPTR(pair);
|
|
|
|
MOO_ASSERT (moo, mdp != MOO_NULL);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-13 13:25:42 +00:00
|
|
|
/* open a module using the part before the last period */
|
2019-10-25 08:44:05 +00:00
|
|
|
mdp = moo_openmod(moo, pid, mod_name_len, 0);
|
2017-01-09 09:54:49 +00:00
|
|
|
if (!mdp) return MOO_NULL;
|
|
|
|
}
|
|
|
|
|
2019-11-01 09:15:53 +00:00
|
|
|
base = pv? (void*)mdp->mod.querypv(moo, &mdp->mod, sep + 1, pidlen - mod_name_len - 1):
|
|
|
|
(void*)mdp->mod.querypf(moo, &mdp->mod, sep + 1, pidlen - mod_name_len - 1);
|
2019-10-25 08:44:05 +00:00
|
|
|
if (!base)
|
2017-01-09 09:54:49 +00:00
|
|
|
{
|
2017-02-13 13:25:42 +00:00
|
|
|
/* the primitive function is not found. but keep the module open even if it's opened above */
|
2019-10-25 08:44:05 +00:00
|
|
|
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);
|
2017-01-09 09:54:49 +00:00
|
|
|
return MOO_NULL;
|
|
|
|
}
|
|
|
|
|
2018-05-07 16:52:51 +00:00
|
|
|
if (mod) *mod = &mdp->mod;
|
|
|
|
|
2019-10-25 08:44:05 +00:00
|
|
|
MOO_DEBUG5 (moo, "Found a primitive %hs [%.*js] in a module [%js] - %p\n",
|
|
|
|
tags[pv], pidlen - mod_name_len - 1, sep + 1, mdp->mod.name, base);
|
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
|
|
|
moo_pfbase_t* moo_querymodpf (moo_t* moo, const moo_ooch_t* pfid, moo_oow_t pfidlen, moo_mod_t** mod)
|
|
|
|
{
|
|
|
|
return (moo_pfbase_t*)query_mod(moo, pfid, pfidlen, mod, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
moo_pvbase_t* moo_querymodpv (moo_t* moo, const moo_ooch_t* pvid, moo_oow_t pvidlen, moo_mod_t** mod)
|
|
|
|
{
|
|
|
|
return (moo_pvbase_t*)query_mod(moo, pvid, pvidlen, mod, 1);
|
2017-01-09 09:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
|
2017-03-30 14:59:55 +00:00
|
|
|
#if 0
|
2017-01-09 09:54:49 +00:00
|
|
|
/* add a new primitive method */
|
2017-02-15 11:57:24 +00:00
|
|
|
int moo_genpfmethod (moo_t* moo, moo_mod_t* mod, moo_oop_class_t _class, moo_method_type_t type, const moo_ooch_t* mthname, int variadic, const moo_ooch_t* pfname)
|
2017-01-09 09:54:49 +00:00
|
|
|
{
|
2019-08-07 05:33:35 +00:00
|
|
|
/* [NOTE] this function is a subset of add_compiled_method() in comp.c */
|
2017-01-09 09:54:49 +00:00
|
|
|
|
|
|
|
moo_oop_char_t mnsym, pfidsym;
|
|
|
|
moo_oop_method_t mth;
|
|
|
|
moo_oow_t tmp_count = 0, i;
|
|
|
|
moo_ooi_t arg_count = 0;
|
|
|
|
moo_oocs_t cs;
|
|
|
|
moo_ooi_t preamble_flags = 0;
|
|
|
|
static moo_ooch_t dot[] = { '.', '\0' };
|
|
|
|
|
|
|
|
MOO_ASSERT (moo, MOO_CLASSOF(moo, _class) == moo->_class);
|
|
|
|
|
|
|
|
if (!pfname) pfname = mthname;
|
|
|
|
|
2018-12-28 08:29:27 +00:00
|
|
|
moo_pushvolat (moo, (moo_oop_t*)&_class); tmp_count++;
|
2017-02-15 11:57:24 +00:00
|
|
|
MOO_ASSERT (moo, MOO_CLASSOF(moo, (moo_oop_t)_class->mthdic[type]) == moo->_method_dictionary);
|
2017-01-09 09:54:49 +00:00
|
|
|
|
|
|
|
for (i = 0; mthname[i]; i++)
|
|
|
|
{
|
|
|
|
if (mthname[i] == ':')
|
|
|
|
{
|
|
|
|
if (variadic) goto oops_inval;
|
|
|
|
arg_count++;
|
|
|
|
}
|
|
|
|
}
|
2017-02-20 04:45:23 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
/* 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] != ':')
|
|
|
|
{
|
|
|
|
oops_inval:
|
2017-02-15 11:57:24 +00:00
|
|
|
MOO_DEBUG2 (moo, "Cannot generate primitive function method [%js] in [%O] - invalid name\n", mthname, _class->name);
|
2017-05-11 14:59:20 +00:00
|
|
|
moo_seterrnum (moo, MOO_EINVAL);
|
2017-01-09 09:54:49 +00:00
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
|
|
|
|
cs.ptr = (moo_ooch_t*)mthname;
|
|
|
|
cs.len = i;
|
2019-10-18 06:42:16 +00:00
|
|
|
if (moo_lookupdic_noseterr(moo, _class->mthdic[type], &cs) != MOO_NULL)
|
2017-01-09 09:54:49 +00:00
|
|
|
{
|
2017-02-15 11:57:24 +00:00
|
|
|
MOO_DEBUG2 (moo, "Cannot generate primitive function method [%js] in [%O] - duplicate\n", mthname, _class->name);
|
2017-05-11 14:59:20 +00:00
|
|
|
moo_seterrnum (moo, MOO_EEXIST);
|
2017-01-09 09:54:49 +00:00
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
|
2019-09-05 08:45:04 +00:00
|
|
|
mnsym = (moo_oop_char_t)moo_makesymbol(moo, mthname, i);
|
2020-10-15 14:50:08 +00:00
|
|
|
if (MOO_UNLIKELY(!mnsym)) goto oops;
|
2018-12-28 08:29:27 +00:00
|
|
|
moo_pushvolat (moo, (moo_oop_t*)&mnsym); tmp_count++;
|
2017-01-09 09:54:49 +00:00
|
|
|
|
|
|
|
/* compose a full primitive function identifier to VM's string buffer.
|
|
|
|
* pfid => mod->name + '.' + pfname */
|
2019-02-20 09:08:10 +00:00
|
|
|
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)
|
2017-01-09 09:54:49 +00:00
|
|
|
{
|
2017-02-15 11:57:24 +00:00
|
|
|
MOO_DEBUG2 (moo, "Cannot generate primitive function method [%js] in [%O] - VM memory shortage\n", mthname, _class->name);
|
2017-01-09 09:54:49 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-02-20 09:08:10 +00:00
|
|
|
pfidsym = (moo_oop_char_t)moo_makesymbol(moo, moo->sbuf[MOO_SBUF_ID_TMP].ptr, moo->sbuf[MOO_SBUF_ID_TMP].len);
|
2020-10-15 14:50:08 +00:00
|
|
|
if (MOO_UNLIKELY(!pfidsym))
|
2017-01-09 09:54:49 +00:00
|
|
|
{
|
2017-02-15 11:57:24 +00:00
|
|
|
MOO_DEBUG2 (moo, "Cannot generate primitive function method [%js] in [%O] - symbol instantiation failure\n", mthname, _class->name);
|
2017-01-09 09:54:49 +00:00
|
|
|
goto oops;
|
|
|
|
}
|
2018-12-28 08:29:27 +00:00
|
|
|
moo_pushvolat (moo, (moo_oop_t*)&pfidsym); tmp_count++;
|
2017-01-09 09:54:49 +00:00
|
|
|
|
2020-10-15 14:50:08 +00:00
|
|
|
mth = (moo_oop_method_t)moo_instantiatewithtrailer(moo, moo->_method, 1, MOO_NULL, 0);
|
|
|
|
if (MOO_UNLIKELY(!mth))
|
2017-01-09 09:54:49 +00:00
|
|
|
{
|
2017-02-15 11:57:24 +00:00
|
|
|
MOO_DEBUG2 (moo, "Cannot generate primitive function method [%js] in [%O] - method instantiation failure\n", mthname, _class->name);
|
2017-01-09 09:54:49 +00:00
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* store the primitive function name symbol to the literal frame */
|
2018-12-19 16:57:16 +00:00
|
|
|
mth->literal_frame[0] = (moo_oop_t)pfidsym;
|
2017-01-09 09:54:49 +00:00
|
|
|
|
|
|
|
/* premable should contain the index to the literal frame which is always 0 */
|
2019-10-12 04:21:23 +00:00
|
|
|
MOO_STORE_OOP (moo, (moo_oop_t*)&mth->owner, (moo_oop_t)_class);
|
|
|
|
MOO_STORE_OOP (moo, (moo_oop_t*)&mth->name, (moo_oop_t)mnsym);
|
2017-01-09 09:54:49 +00:00
|
|
|
if (variadic) preamble_flags |= MOO_METHOD_PREAMBLE_FLAG_VARIADIC;
|
|
|
|
mth->preamble = MOO_SMOOI_TO_OOP(MOO_METHOD_MAKE_PREAMBLE(MOO_METHOD_PREAMBLE_NAMED_PRIMITIVE, 0, preamble_flags));
|
2018-05-07 16:52:51 +00:00
|
|
|
mth->preamble_data[0] = MOO_SMPTR_TO_OOP(0);
|
|
|
|
mth->preamble_data[1] = MOO_SMPTR_TO_OOP(0);
|
2017-01-09 09:54:49 +00:00
|
|
|
mth->tmpr_count = MOO_SMOOI_TO_OOP(arg_count);
|
|
|
|
mth->tmpr_nargs = MOO_SMOOI_TO_OOP(arg_count);
|
|
|
|
|
2017-02-13 13:25:42 +00:00
|
|
|
/* TODO: emit BCODE_RETURN_NIL as a fallback or self primitiveFailed? or anything else?? */
|
2017-01-09 09:54:49 +00:00
|
|
|
|
2017-02-15 11:57:24 +00:00
|
|
|
if (!moo_putatdic (moo, _class->mthdic[type], (moo_oop_t)mnsym, (moo_oop_t)mth))
|
2017-01-09 09:54:49 +00:00
|
|
|
{
|
2017-02-15 11:57:24 +00:00
|
|
|
MOO_DEBUG2 (moo, "Cannot generate primitive function method [%js] in [%O] - failed to add to method dictionary\n", mthname, _class->name);
|
2017-01-09 09:54:49 +00:00
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
|
2017-02-15 11:57:24 +00:00
|
|
|
MOO_DEBUG2 (moo, "Generated primitive function method [%js] in [%O]\n", mthname, _class->name);
|
2017-01-09 09:54:49 +00:00
|
|
|
|
2018-12-28 08:29:27 +00:00
|
|
|
moo_popvolats (moo, tmp_count); tmp_count = 0;
|
2017-01-09 09:54:49 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
oops:
|
2018-12-28 08:29:27 +00:00
|
|
|
moo_popvolats (moo, tmp_count);
|
2017-01-09 09:54:49 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2017-02-14 08:29:30 +00:00
|
|
|
|
2017-02-15 11:57:24 +00:00
|
|
|
int moo_genpfmethods (moo_t* moo, moo_mod_t* mod, moo_oop_class_t _class, const moo_pfinfo_t* pfinfo, moo_oow_t pfcount)
|
2017-02-14 10:25:26 +00:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
moo_oow_t i;
|
|
|
|
|
2018-12-28 08:29:27 +00:00
|
|
|
moo_pushvolat (moo, (moo_oop_t*)&_class);
|
2017-02-14 10:25:26 +00:00
|
|
|
for (i = 0; i < pfcount; i++)
|
|
|
|
{
|
|
|
|
if (moo_genpfmethod (moo, mod, _class, pfinfo[i].type, pfinfo[i].mthname, pfinfo[i].variadic, MOO_NULL) <= -1)
|
|
|
|
{
|
|
|
|
/* TODO: delete pfmethod generated??? */
|
|
|
|
ret = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-28 08:29:27 +00:00
|
|
|
moo_popvolat (moo);
|
2017-02-14 10:25:26 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2017-03-30 14:59:55 +00:00
|
|
|
#endif
|
2017-02-14 10:25:26 +00:00
|
|
|
|
2017-03-31 14:21:22 +00:00
|
|
|
moo_pfbase_t* moo_findpfbase (moo_t* moo, moo_pfinfo_t* pfinfo, moo_oow_t pfcount, const moo_ooch_t* name, moo_oow_t namelen)
|
2017-02-14 10:25:26 +00:00
|
|
|
{
|
2017-05-04 05:22:45 +00:00
|
|
|
int n;
|
|
|
|
|
|
|
|
/* binary search */
|
2017-06-16 09:45:22 +00:00
|
|
|
#if 0
|
|
|
|
/* [NOTE] this algorithm is NOT underflow safe with moo_oow_t types */
|
|
|
|
int left, right, mid;
|
2017-02-14 10:25:26 +00:00
|
|
|
|
2017-06-16 09:45:22 +00:00
|
|
|
for (left = 0, right = pfcount - 1; left <= right; )
|
2017-02-14 10:25:26 +00:00
|
|
|
{
|
2017-05-04 05:22:45 +00:00
|
|
|
/*mid = (left + right) / 2;*/
|
|
|
|
mid = left + ((right - left) / 2);
|
2017-02-14 10:25:26 +00:00
|
|
|
|
2019-10-25 03:22:40 +00:00
|
|
|
n = moo_comp_oochars_bcstr(name, namelen, pfinfo[mid].name);
|
2017-06-16 09:45:22 +00:00
|
|
|
if (n < 0) right = mid - 1; /* this substraction can make right negative. so i can't use moo_oow_t for the variable */
|
2017-02-14 10:25:26 +00:00
|
|
|
else if (n > 0) left = mid + 1;
|
2017-03-31 14:21:22 +00:00
|
|
|
else return &pfinfo[mid].base;
|
2017-02-14 10:25:26 +00:00
|
|
|
}
|
2017-06-16 09:45:22 +00:00
|
|
|
#else
|
|
|
|
/* [NOTE] this algorithm is underflow safe with moo_oow_t types */
|
|
|
|
moo_oow_t base, mid, lim;
|
|
|
|
|
|
|
|
for (base = 0, lim = pfcount; lim > 0; lim >>= 1)
|
|
|
|
{
|
|
|
|
mid = base + (lim >> 1);
|
2019-10-25 03:22:40 +00:00
|
|
|
n = moo_comp_oochars_bcstr(name, namelen, pfinfo[mid].name);
|
2017-06-16 09:45:22 +00:00
|
|
|
if (n == 0) return &pfinfo[mid].base;
|
|
|
|
if (n > 0) { base = mid + 1; lim--; }
|
|
|
|
}
|
|
|
|
#endif
|
2017-02-14 10:25:26 +00:00
|
|
|
|
2017-05-11 14:59:20 +00:00
|
|
|
moo_seterrnum (moo, MOO_ENOENT);
|
2017-02-14 10:25:26 +00:00
|
|
|
return MOO_NULL;
|
|
|
|
}
|
|
|
|
|
2019-10-25 08:44:05 +00:00
|
|
|
moo_pvbase_t* moo_findpvbase (moo_t* moo, moo_pvinfo_t* pvinfo, moo_oow_t pvcount, const moo_ooch_t* name, moo_oow_t namelen)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
/* binary search */
|
|
|
|
moo_oow_t base, mid, lim;
|
|
|
|
|
|
|
|
for (base = 0, lim = pvcount; lim > 0; lim >>= 1)
|
|
|
|
{
|
|
|
|
mid = base + (lim >> 1);
|
|
|
|
n = moo_comp_oochars_bcstr(name, namelen, pvinfo[mid].name);
|
|
|
|
if (n == 0) return &pvinfo[mid].base;
|
|
|
|
if (n > 0) { base = mid + 1; lim--; }
|
|
|
|
}
|
|
|
|
|
|
|
|
moo_seterrnum (moo, MOO_ENOENT);
|
|
|
|
return MOO_NULL;
|
|
|
|
}
|
|
|
|
|
2017-02-14 10:25:26 +00:00
|
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
|
2017-06-16 09:45:22 +00:00
|
|
|
int moo_setclasstrsize (moo_t* moo, moo_oop_class_t _class, moo_oow_t size, moo_trgc_t trgc)
|
2017-02-14 08:29:30 +00:00
|
|
|
{
|
|
|
|
moo_oop_class_t sc;
|
|
|
|
moo_oow_t spec;
|
|
|
|
|
|
|
|
MOO_ASSERT (moo, MOO_CLASSOF(moo, _class) == moo->_class);
|
2019-09-16 14:16:55 +00:00
|
|
|
/*MOO_ASSERT (moo, size <= MOO_SMOOI_MAX);
|
|
|
|
MOO_ASSERT (moo, MOO_IN_SMPTR_RANGE(trgc));*/
|
|
|
|
|
|
|
|
if (size > MOO_SMOOI_MAX)
|
|
|
|
{
|
|
|
|
moo_seterrbfmt (moo, MOO_EINVAL, "trailer size %zu too large for the %.*js class",
|
|
|
|
size, MOO_OBJ_GET_SIZE(_class->name), MOO_OBJ_GET_CHAR_SLOT(_class->name));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!MOO_IN_SMPTR_RANGE(trgc))
|
|
|
|
{
|
|
|
|
moo_seterrbfmt (moo, MOO_EINVAL, "trailer gc pointer %p not SMPTR compatible for the %.*js class",
|
|
|
|
trgc, MOO_OBJ_GET_SIZE(_class->name), MOO_OBJ_GET_CHAR_SLOT(_class->name));
|
|
|
|
return -1;
|
|
|
|
}
|
2017-06-16 09:45:22 +00:00
|
|
|
|
2017-02-15 11:57:24 +00:00
|
|
|
if (_class == moo->_method)
|
2017-02-14 08:29:30 +00:00
|
|
|
{
|
|
|
|
/* the bytes code emitted by the compiler go to the trailer part
|
|
|
|
* regardless of the trailer size. you're not allowed to change it */
|
2018-01-07 08:00:23 +00:00
|
|
|
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;
|
2017-02-14 08:29:30 +00:00
|
|
|
}
|
|
|
|
|
2017-02-15 11:57:24 +00:00
|
|
|
spec = MOO_OOP_TO_SMOOI(_class->spec);
|
2017-02-14 08:29:30 +00:00
|
|
|
if (MOO_CLASS_SPEC_IS_INDEXED(spec) && MOO_CLASS_SPEC_INDEXED_TYPE(spec) != MOO_OBJ_TYPE_OOP)
|
|
|
|
{
|
2018-01-07 08:00:23 +00:00
|
|
|
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;
|
2017-02-14 08:29:30 +00:00
|
|
|
}
|
|
|
|
|
2017-02-15 11:57:24 +00:00
|
|
|
if (_class->trsize != moo->_nil)
|
2017-02-14 08:29:30 +00:00
|
|
|
{
|
2017-06-16 09:45:22 +00:00
|
|
|
MOO_ASSERT (moo, _class->trgc != moo->_nil);
|
2018-01-07 08:00:23 +00:00
|
|
|
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;
|
2017-02-14 08:29:30 +00:00
|
|
|
}
|
2017-06-16 09:45:22 +00:00
|
|
|
MOO_ASSERT (moo, _class->trgc == moo->_nil);
|
2017-02-14 08:29:30 +00:00
|
|
|
|
2017-02-15 11:57:24 +00:00
|
|
|
sc = (moo_oop_class_t)_class->superclass;
|
2017-02-14 08:29:30 +00:00
|
|
|
if (MOO_OOP_IS_SMOOI(sc->trsize) && size < MOO_OOP_TO_SMOOI(sc->trsize))
|
|
|
|
{
|
2018-01-07 08:00:23 +00:00
|
|
|
moo_seterrbfmt (moo, MOO_EPERM, "not allowed to set the trailer size of %.*js to be smaller(%zu) than that(%zu) of the superclass %.*js",
|
|
|
|
size, MOO_OBJ_GET_SIZE(_class->name), MOO_OBJ_GET_CHAR_SLOT(_class->name),
|
|
|
|
MOO_OOP_TO_SMOOI(sc->trsize), MOO_OBJ_GET_SIZE(sc->name), MOO_OBJ_GET_CHAR_SLOT(sc->name));
|
|
|
|
return -1;
|
2017-02-14 08:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* you can only set the trailer size once when it's not set yet */
|
2017-02-15 11:57:24 +00:00
|
|
|
_class->trsize = MOO_SMOOI_TO_OOP(size);
|
2017-06-16 09:45:22 +00:00
|
|
|
_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",
|
2017-02-14 08:29:30 +00:00
|
|
|
size,
|
2017-02-15 11:57:24 +00:00
|
|
|
MOO_OBJ_GET_SIZE(_class->name),
|
2017-06-16 09:45:22 +00:00
|
|
|
MOO_OBJ_GET_CHAR_SLOT(_class->name),
|
|
|
|
MOO_SMPTR_TO_OOP(trgc), trgc);
|
2017-02-14 08:29:30 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* moo_getobjtrailer (moo_t* moo, moo_oop_t obj, moo_oow_t* size)
|
|
|
|
{
|
|
|
|
if (!MOO_OBJ_IS_OOP_POINTER(obj) || !MOO_OBJ_GET_FLAGS_TRAILER(obj)) return MOO_NULL;
|
|
|
|
if (size) *size = MOO_OBJ_GET_TRAILER_SIZE(obj);
|
|
|
|
return MOO_OBJ_GET_TRAILER_BYTE(obj);
|
|
|
|
}
|
2017-03-19 14:18:37 +00:00
|
|
|
|
2017-05-20 02:27:48 +00:00
|
|
|
moo_oop_t moo_findclass (moo_t* moo, moo_oop_nsdic_t nsdic, const moo_ooch_t* name)
|
2017-03-19 14:18:37 +00:00
|
|
|
{
|
|
|
|
moo_oop_association_t ass;
|
|
|
|
moo_oocs_t n;
|
|
|
|
|
|
|
|
n.ptr = (moo_ooch_t*)name;
|
2018-04-07 15:54:09 +00:00
|
|
|
n.len = moo_count_oocstr(name);
|
2017-03-19 14:18:37 +00:00
|
|
|
|
2019-10-18 06:42:16 +00:00
|
|
|
ass = moo_lookupdic_noseterr(moo, (moo_oop_dic_t)nsdic, &n);
|
2017-03-19 14:18:37 +00:00
|
|
|
if (!ass || MOO_CLASSOF(moo,ass->value) != moo->_class)
|
|
|
|
{
|
2017-05-11 14:59:20 +00:00
|
|
|
moo_seterrnum (moo, MOO_ENOENT);
|
2017-03-19 14:18:37 +00:00
|
|
|
return MOO_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ass->value;
|
|
|
|
}
|
2017-09-29 15:03:54 +00:00
|
|
|
|
|
|
|
int moo_iskindof (moo_t* moo, moo_oop_t obj, moo_oop_class_t _class)
|
|
|
|
{
|
|
|
|
moo_oop_class_t c;
|
|
|
|
|
2017-09-29 15:12:01 +00:00
|
|
|
MOO_ASSERT (moo, MOO_CLASSOF(moo,_class) == moo->_class);
|
|
|
|
|
2017-09-29 15:03:54 +00:00
|
|
|
c = MOO_CLASSOF(moo,obj); /* c := self class */
|
|
|
|
if (c == moo->_class)
|
|
|
|
{
|
|
|
|
/* object is a class */
|
2017-09-30 04:49:54 +00:00
|
|
|
if (_class == moo->_class) return 1; /* obj isKindOf: Class */
|
2017-09-29 15:03:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (c == _class) return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
c = (moo_oop_class_t)c->superclass;
|
|
|
|
while ((moo_oop_t)c != moo->_nil)
|
|
|
|
{
|
|
|
|
if (c == _class) return 1;
|
|
|
|
c = (moo_oop_class_t)c->superclass;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2019-07-05 08:12:42 +00:00
|
|
|
|
|
|
|
int moo_ischildclassof (moo_t* moo, moo_oop_class_t c, moo_oop_class_t k)
|
|
|
|
{
|
|
|
|
c = (moo_oop_class_t)c->superclass;
|
|
|
|
while ((moo_oop_t)c != moo->_nil)
|
|
|
|
{
|
|
|
|
if (c == k) return 1;
|
|
|
|
c = (moo_oop_class_t)c->superclass;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|