fixed wrong computation of buffer capacity for moo->inttostr.xbuf and moo->inttostr.t

updated some sample code
fixed a few flaws in ffi.c
This commit is contained in:
hyunghwan.chung 2018-12-20 16:08:56 +00:00
parent fb7b226ea8
commit 9a4bbcaa4c
9 changed files with 65 additions and 54 deletions

View File

@ -43,12 +43,21 @@ class FFI(Object)
method call: name signature: sig arguments: args
{
| f |
f := self.funcs at: name.
(f isError) ifTrue: [
(* f := self.funcs at: name ifAbsent: [
f := self.ffi getsym(name).
(f isError) ifTrue: [^f].
if (f isError) { ^f }.
self.funcs at: name put: f.
].
f. ## need this as at:put: returns an association
]. *)
f := self.funcs at: name ifAbsent: [ nil ].
if (f isNil)
{
f := self.ffi getsym(name).
if (f isError) { ^f }.
self.funcs at: name put: f.
}.
^self.ffi call(f, sig, args)
}

View File

@ -248,19 +248,20 @@ class MyObject(TestObject)
^ [ 'a block returned by getABlock' dump. "^ self"]
}
method(#class) main
method(#class) test_ffi
{
"
| ffi |
ffi := FFI new: 'libc.so.6'.
## ffi call: #printf with: #((str '%d') (int 10) (long 20)).
ffi call: #printf withSig: 'i|sii' withArgs: #(S'hello world %d %d\n' 11123 9876543).
## ffi call: #puts withSig: 'i|s' withArgs: #('hello world').
ffi call: #printf signature: '|s|ici)i' arguments: #("hello world %d %c %d\n" 11123 $X 9876543).
##ffi call: #puts signature: 's)i' arguments: #('hello world').
ffi close.
"
}
method(#class) main
{
## ---------------------------------------------------------------
## getABlock has returned.
## aBlock's home context is getABlock. getABlock has returned
@ -272,7 +273,9 @@ class MyObject(TestObject)
aBlock value.
## ---------------------------------------------------------------
"
self test_ffi.
(* -----------------------------
PROCESS TESTING
| p |
'000000000000000000' dump.
@ -285,7 +288,8 @@ PROCESS TESTING
'222222222222222222' dump.
'333333333333333333' dump.
'444444444444444444' dump.
"
---------------------------- *)
(-2305843009213693952 - 1) dump.
@ -302,14 +306,15 @@ PROCESS TESTING
(2r111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 * 128971234897128931) dump.
"(-10000 rem: 3) dump.
(-10000 quo: 3) dump.
(-10000 \\ 3) dump.
(-10000 // 3) dump."
(* (-10000 rem: 3) dump.
(-10000 mod: 3) dump.
(-10000 div: 3) dump.
(-10000 mdiv: 3) dump. *)
(7 rem: -3) dump.
(7 quo: -3) dump.
(7 \\ -3) dump.
(7 // -3) dump.
(7 mod: -3) dump.
(7 div: -3) dump.
(7 mdiv: -3) dump.
##(777777777777777777777777777777777777777777777777777777777777777777777 rem: -8127348917239812731289371289731298) dump.
##(777777777777777777777777777777777777777777777777777777777777777777777 quo: -8127348917239812731289371289731298) dump.
@ -440,7 +445,7 @@ PROCESS TESTING
b dump.
] value.
'====================' dump.
([ :a :b | "a := 20." b := [ a + 20 ]. b value.] value: 99 value: 100) dump.
([ :a :b | (*a := 20.*) b := [ a + 20 ]. b value.] value: 99 value: 100) dump.
'====================' dump.
[ :a :b | a dump. b dump. a := 20. b := [ a + 20 ]. b value.] value dump. ## not sufficient arguments. it must fail
@ -458,7 +463,7 @@ PROCESS TESTING
}
"
(* ====================
[ a := 20. b := [ a + 20 ]. b value. ] value
^ ^ ^ ^
p1 p3 p4 p2
@ -517,4 +522,4 @@ method whileTrue: aBlock
[ b < 10 ] whileTrue: [ b dump. b := b + 1 ].
"
========== *)

View File

@ -4153,7 +4153,7 @@ moo_oop_t moo_inttostr (moo_t* moo, moo_oop_t num, int radix)
reqcapa = MOO_OOW_BITS + 1;
if (moo->inttostr.xbuf.capa < reqcapa)
{
xbuf = (moo_ooch_t*)moo_reallocmem(moo, moo->inttostr.xbuf.ptr, reqcapa);
xbuf = (moo_ooch_t*)moo_reallocmem(moo, moo->inttostr.xbuf.ptr, reqcapa * MOO_SIZEOF(*xbuf));
if (!xbuf) return MOO_NULL;
moo->inttostr.xbuf.capa = reqcapa;
moo->inttostr.xbuf.ptr = xbuf;
@ -4182,10 +4182,10 @@ moo_oop_t moo_inttostr (moo_t* moo, moo_oop_t num, int radix)
xlen = as * ((MOO_LIW_BITS + exp) / exp) + 1;
xpos = xlen;
reqcapa = MOO_SIZEOF(*xbuf) * xlen;
reqcapa = xlen;
if (moo->inttostr.xbuf.capa < reqcapa)
{
xbuf = (moo_ooch_t*)moo_reallocmem(moo, moo->inttostr.xbuf.ptr, reqcapa);
xbuf = (moo_ooch_t*)moo_reallocmem(moo, moo->inttostr.xbuf.ptr, reqcapa * MOO_SIZEOF(*xbuf));
if (!xbuf) return MOO_NULL;
moo->inttostr.xbuf.capa = reqcapa;
moo->inttostr.xbuf.ptr = xbuf;
@ -4232,10 +4232,10 @@ moo_oop_t moo_inttostr (moo_t* moo, moo_oop_t num, int radix)
/* TODO: find an optimial buffer size */
/* TODO: find an optimial buffer size */
reqcapa = MOO_SIZEOF(*xbuf) * (as * MOO_LIW_BITS + 1);
reqcapa = as * MOO_LIW_BITS + 1;
if (moo->inttostr.xbuf.capa < reqcapa)
{
xbuf = (moo_ooch_t*)moo_reallocmem(moo, moo->inttostr.xbuf.ptr, reqcapa);
xbuf = (moo_ooch_t*)moo_reallocmem(moo, moo->inttostr.xbuf.ptr, reqcapa * MOO_SIZEOF(*xbuf));
if (!xbuf) return MOO_NULL;
moo->inttostr.xbuf.capa = reqcapa;
moo->inttostr.xbuf.ptr = xbuf;
@ -4245,10 +4245,10 @@ moo_oop_t moo_inttostr (moo_t* moo, moo_oop_t num, int radix)
xbuf = moo->inttostr.xbuf.ptr;
}
reqcapa = MOO_SIZEOF(*t) * as * 3;
reqcapa = as * 3;
if (moo->inttostr.t.capa < reqcapa)
{
t = (moo_liw_t*)moo_reallocmem(moo, moo->inttostr.t.ptr, reqcapa);
t = (moo_liw_t*)moo_reallocmem(moo, moo->inttostr.t.ptr, reqcapa * MOO_SIZEOF(*t));
if (!t) return MOO_NULL;
moo->inttostr.t.capa = reqcapa;
moo->inttostr.t.ptr = t;
@ -4258,7 +4258,6 @@ moo_oop_t moo_inttostr (moo_t* moo, moo_oop_t num, int radix)
t = moo->inttostr.t.ptr;
}
#if (MOO_LIW_BITS == MOO_OOW_BITS)
b[0] = moo->bigint[radix].multiplier; /* block divisor */
bs = 1;

View File

@ -7407,7 +7407,7 @@ static int make_defined_class (moo_t* moo)
for (i = 0; i < initv_count; i++)
{
MOO_STORE_OOP (moo, &cc->self_oop->cvar[i], initv->slot[i]);
MOO_STORE_OOP (moo, &cc->self_oop->cvar[i], MOO_OBJ_GET_OOP_VAL(initv, i));
}
}

View File

@ -4852,7 +4852,7 @@ static int __execute (moo_t* moo)
if ((bcode >> 4) & 1)
{
/* push - bit 4 on */
LOG_INST1 (moo, "push_tempvar %zu", b1);
LOG_INST2 (moo, "push_tempvar %zu => %O", b1, ctx->stack[bx]);
MOO_STACK_PUSH (moo, ctx->stack[bx]);
}
else

View File

@ -207,9 +207,9 @@ int main (int argc, char* argv[])
{
moo_oow_t tab_size;
tab_size = 5000;
tab_size = 10;
moo_setoption (moo, MOO_SYMTAB_SIZE, &tab_size);
tab_size = 5000;
tab_size = 10;
moo_setoption (moo, MOO_SYSDIC_SIZE, &tab_size);
tab_size = 600;
moo_setoption (moo, MOO_PROCSTK_SIZE, &tab_size);

View File

@ -54,7 +54,7 @@
/*#define MOO_DEBUG_LEXER 1*/
#define MOO_DEBUG_COMPILER 1
#define MOO_DEBUG_VM_PROCESSOR 1
/*#define MOO_DEBUG_VM_EXEC*/
/*#define MOO_DEBUG_VM_EXEC 1*/
#define MOO_PROFILE_VM 1
#endif
@ -382,8 +382,6 @@ struct moo_loop_t
moo_loop_t* next;
};
typedef struct moo_oopbuf_t moo_oopbuf_t;
struct moo_oopbuf_t
{

View File

@ -67,15 +67,17 @@ static moo_oop_oop_t expand_bucket (moo_t* moo, moo_oop_oop_t oldbuc)
while (oldsz > 0)
{
symbol = (moo_oop_char_t)oldbuc->slot[--oldsz];
oldsz = oldsz - 1;
symbol = (moo_oop_char_t)MOO_OBJ_GET_OOP_VAL(oldbuc, oldsz);
if ((moo_oop_t)symbol != moo->_nil)
{
MOO_ASSERT (moo, MOO_CLASSOF(moo,symbol) == moo->_symbol);
/*MOO_ASSERT (moo, sym->size > 0);*/
index = moo_hashoochars(MOO_OBJ_GET_CHAR_SLOT(symbol), MOO_OBJ_GET_SIZE(symbol)) % newsz;
while (newbuc->slot[index] != moo->_nil) index = (index + 1) % newsz;
MOO_STORE_OOP (moo, &newbuc->slot[index], (moo_oop_t)symbol);
while (MOO_OBJ_GET_OOP_VAL(newbuc, index) != moo->_nil) index = (index + 1) % newsz;
MOO_STORE_OOP (moo, MOO_OBJ_GET_OOP_PTR(newbuc, index), (moo_oop_t)symbol);
}
}
@ -92,9 +94,8 @@ static moo_oop_t find_or_make_symbol (moo_t* moo, const moo_ooch_t* ptr, moo_oow
index = moo_hashoochars(ptr, len) % MOO_OBJ_GET_SIZE(moo->symtab->bucket);
/* find a matching symbol in the open-addressed symbol table */
while (moo->symtab->bucket->slot[index] != moo->_nil)
while ((moo_oop_t)(symbol = MOO_OBJ_GET_OOP_VAL(moo->symtab->bucket, index)) != moo->_nil)
{
symbol = (moo_oop_char_t)moo->symtab->bucket->slot[index];
MOO_ASSERT (moo, MOO_CLASSOF(moo,symbol) == moo->_symbol);
if (len == MOO_OBJ_GET_SIZE(symbol) &&
@ -147,7 +148,7 @@ static moo_oop_t find_or_make_symbol (moo_t* moo, const moo_ooch_t* ptr, moo_oow
/* recalculate the index for the expanded bucket */
index = moo_hashoochars(ptr, len) % MOO_OBJ_GET_SIZE(moo->symtab->bucket);
while (moo->symtab->bucket->slot[index] != moo->_nil)
while (MOO_OBJ_GET_OOP_VAL(moo->symtab->bucket, index) != moo->_nil)
index = (index + 1) % MOO_OBJ_GET_SIZE(moo->symtab->bucket);
}
@ -157,7 +158,7 @@ static moo_oop_t find_or_make_symbol (moo_t* moo, const moo_ooch_t* ptr, moo_oow
{
MOO_ASSERT (moo, tally < MOO_SMOOI_MAX);
moo->symtab->tally = MOO_SMOOI_TO_OOP(tally + 1);
MOO_STORE_OOP (moo, &moo->symtab->bucket->slot[index], (moo_oop_t)symbol);
MOO_STORE_OOP (moo, MOO_OBJ_GET_OOP_PTR(moo->symtab->bucket, index), (moo_oop_t)symbol);
}
return (moo_oop_t)symbol;

View File

@ -220,7 +220,6 @@ static moo_pfrc_t pf_call (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs)
moo_oop_t arg;
fmtc = MOO_OBJ_GET_CHAR_VAL(sig, i);
if (fmtc == ')')
{
i++;
@ -241,22 +240,22 @@ static moo_pfrc_t pf_call (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs)
/* more items in signature than the actual argument */
if (j >= MOO_OBJ_GET_SIZE(arr)) goto inval;
arg = arr->slot[j];
arg = MOO_OBJ_GET_OOP_VAL(arr, j);
switch (fmtc)
{
/* TODO: support more types... */
case 'c':
if (!MOO_OOP_IS_CHAR(arg)) goto inval;
dcArgChar (ffi->dc, MOO_OOP_TO_CHAR(arr->slot[j]));
dcArgChar (ffi->dc, MOO_OOP_TO_CHAR(arg));
j++;
break;
/* TODO: added unsigned types */
/* TODO: add unsigned types */
case 'i':
{
moo_ooi_t v;
if (moo_inttoooi(moo, arg, &v) == 0) goto inval;
dcArgInt (ffi->dc, i);
dcArgInt (ffi->dc, v);
j++;
break;
}
@ -300,7 +299,7 @@ static moo_pfrc_t pf_call (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs)
if (!MOO_OBJ_IS_CHAR_POINTER(arg)) goto inval;
#if defined(MOO_OOCH_IS_UCH)
ptr = moo_dupootobcharswithheadroom (moo, MOO_SIZEOF_VOID_P, MOO_OBJ_GET_CHAR_SLOT(arg), MOO_OBJ_GET_SIZE(arg), MOO_NULL);
ptr = moo_dupootobcharswithheadroom(moo, MOO_SIZEOF_VOID_P, MOO_OBJ_GET_CHAR_SLOT(arg), MOO_OBJ_GET_SIZE(arg), MOO_NULL);
if (!ptr) goto softfail; /* out of system memory or conversion error - soft failure */
link_ca (ffi, ptr);
#else
@ -349,7 +348,7 @@ static moo_pfrc_t pf_call (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs)
/* TODO: proper return value conversion */
case 'c':
{
char r = dcCallChar (ffi->dc, f);
char r = dcCallChar(ffi->dc, f);
MOO_STACK_SETRET (moo, nargs, MOO_CHAR_TO_OOP(r));
break;
}
@ -358,7 +357,7 @@ static moo_pfrc_t pf_call (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs)
{
moo_oop_t r;
r = moo_ooitoint (moo, dcCallInt(ffi->dc, f));
r = moo_ooitoint(moo, dcCallInt(ffi->dc, f));
if (!r) goto hardfail;
MOO_STACK_SETRET (moo, nargs, r);
break;
@ -368,7 +367,7 @@ static moo_pfrc_t pf_call (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs)
{
moo_oop_t r;
ret_as_long:
r = moo_ooitoint (moo, dcCallLong (ffi->dc, f));
r = moo_ooitoint(moo, dcCallLong(ffi->dc, f));
if (!r) goto hardfail;
MOO_STACK_SETRET (moo, nargs, r);
break;
@ -499,7 +498,7 @@ static moo_pfrc_t pf_getsym (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs)
goto softfail;
}
sym = moo->vmprim.dl_getsym (moo, ffi->handle, MOO_OBJ_GET_CHAR_SLOT(name));
sym = moo->vmprim.dl_getsym(moo, ffi->handle, MOO_OBJ_GET_CHAR_SLOT(name));
if (!sym) goto softfail;
MOO_DEBUG4 (moo, "<ffi.getsym> %.*js => %p in %p\n", MOO_OBJ_GET_SIZE(name), MOO_OBJ_GET_CHAR_SLOT(name), sym, ffi->handle);