added moo_count_bcstrl() and moo_count_ucstrl() that limits the maximum length.

added some experimental emscripten/wasm related code
This commit is contained in:
hyunghwan.chung 2019-11-03 09:15:24 +00:00
parent 09a1cb95ec
commit 850fb4971f
6 changed files with 72 additions and 8 deletions

View File

@ -227,7 +227,7 @@ EXCEPTION_TEST:
Exception signal: 'experiment with exception signalling'. Exception signal: 'experiment with exception signalling'.
// TODO: // TODO:
String format("%s", " 나 는\\\"") dump. System logNl: String format("%s", " 나 는\\\"").
#"a b\nc" dump. #"a b\nc" dump.
} }
} }

View File

@ -128,7 +128,7 @@ MOO_EXPORT int moo_compilefileu (
#endif #endif
MOO_EXPORT int moo_invokestdb ( MOO_EXPORT int moo_invokebynameb (
moo_t* moo, moo_t* moo,
const moo_bch_t* objname, const moo_bch_t* objname,
const moo_bch_t* mthname const moo_bch_t* mthname

View File

@ -557,10 +557,20 @@ MOO_EXPORT moo_oow_t moo_count_ucstr (
const moo_uch_t* str const moo_uch_t* str
); );
MOO_EXPORT moo_oow_t moo_count_ucstrl (
const moo_uch_t* str,
moo_oow_t maxlen
);
MOO_EXPORT moo_oow_t moo_count_bcstr ( MOO_EXPORT moo_oow_t moo_count_bcstr (
const moo_bch_t* str const moo_bch_t* str
); );
MOO_EXPORT moo_oow_t moo_count_bcstrl (
const moo_bch_t* str,
moo_oow_t maxlen
);
#if defined(MOO_OOCH_IS_UCH) #if defined(MOO_OOCH_IS_UCH)
# define moo_equal_oochars(str1,str2,len) moo_equal_uchars(str1,str2,len) # define moo_equal_oochars(str1,str2,len) moo_equal_uchars(str1,str2,len)
# define moo_comp_oochars(str1,len1,str2,len2) moo_comp_uchars(str1,len1,str2,len2) # define moo_comp_oochars(str1,len1,str2,len2) moo_comp_uchars(str1,len1,str2,len2)

View File

@ -31,7 +31,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#if !defined(__DOS__) && defined(HAVE_PTHREAD) && defined(HAVE_STRERROR_R) #if !defined(__DOS__) && !defined(EMSCRIPTEN) && defined(HAVE_PTHREAD) && defined(HAVE_STRERROR_R)
# define USE_THREAD # define USE_THREAD
#endif #endif
@ -822,6 +822,27 @@ static void free_heap (moo_t* moo, void* ptr)
static int write_all (int fd, const moo_bch_t* ptr, moo_oow_t len) static int write_all (int fd, const moo_bch_t* ptr, moo_oow_t len)
{ {
#if defined(EMSCRIPTEN)
while (len > 0)
{
moo_oow_t slen;
/* if the output data contains a null byte in the middle,
* UTF8ToString() cuts short before printing all data */
slen = moo_count_bcstrl(ptr, len);
EM_ASM_ ({
console.log ("%s", UTF8ToString($0, $1));
}, ptr, slen);
if (slen < len) slen++; /* skip the null byte */
len -= slen;
ptr += slen;
}
#else
while (len > 0) while (len > 0)
{ {
moo_ooi_t wr; moo_ooi_t wr;
@ -850,6 +871,7 @@ static int write_all (int fd, const moo_bch_t* ptr, moo_oow_t len)
ptr += wr; ptr += wr;
len -= wr; len -= wr;
} }
#endif
return 0; return 0;
} }
@ -926,7 +948,9 @@ static void log_write (moo_t* moo, moo_bitmask_t mask, const moo_ooch_t* msg, mo
else else
{ {
logfd = xtn->log.fd; logfd = xtn->log.fd;
#if !defined(EMSCRIPTEN)
if (logfd <= -1) return; if (logfd <= -1) return;
#endif
} }
/* TODO: beautify the log message. /* TODO: beautify the log message.
@ -4185,6 +4209,12 @@ moo_t* moo_openstd (moo_oow_t xtnsize, const moo_cfgstd_t* cfg, moo_errinf_t* er
evtcb.halting = halting_moo; evtcb.halting = halting_moo;
moo_regevtcb (moo, &evtcb); moo_regevtcb (moo, &evtcb);
#if defined(EMSCRIPTEN)
{
moo_bitmask_t m = ~(moo_bitmask_t)0;
moo_setoption (moo, MOO_OPTION_LOG_MASK, &m);
}
#endif
{ {
moo_bitmask_t bm = 0; moo_bitmask_t bm = 0;
@ -4266,7 +4296,7 @@ int moo_compilefileu (moo_t* moo, const moo_uch_t* path)
#endif #endif
int moo_invokestdb (moo_t* moo, const moo_bch_t* objname, const moo_bch_t* mthname) int moo_invokebynameb (moo_t* moo, const moo_bch_t* objname, const moo_bch_t* mthname)
{ {
#if defined(MOO_OOCH_IS_UCH) #if defined(MOO_OOCH_IS_UCH)
int n = -1; int n = -1;

View File

@ -315,6 +315,16 @@ moo_oow_t moo_count_ucstr (const moo_uch_t* str)
return ptr - str; return ptr - str;
} }
moo_oow_t moo_count_ucstrl (const moo_uch_t* str, moo_oow_t maxlen)
{
moo_oow_t i;
for (i = 0; i < maxlen; i++)
{
if (str[i] == '\0') break;
}
return i;
}
moo_oow_t moo_count_bcstr (const moo_bch_t* str) moo_oow_t moo_count_bcstr (const moo_bch_t* str)
{ {
const moo_bch_t* ptr = str; const moo_bch_t* ptr = str;
@ -322,6 +332,16 @@ moo_oow_t moo_count_bcstr (const moo_bch_t* str)
return ptr - str; return ptr - str;
} }
moo_oow_t moo_count_bcstrl (const moo_bch_t* str, moo_oow_t maxlen)
{
moo_oow_t i;
for (i = 0; i < maxlen; i++)
{
if (str[i] == '\0') break;
}
return i;
}
moo_uch_t* moo_find_uchar (const moo_uch_t* ptr, moo_oow_t len, moo_uch_t c) moo_uch_t* moo_find_uchar (const moo_uch_t* ptr, moo_oow_t len, moo_uch_t c)
{ {
const moo_uch_t* end; const moo_uch_t* end;

View File

@ -13,11 +13,15 @@ const libmoo = {
ignite: Module.cwrap('moo_ignite', 'number', ['number', 'number']), ignite: Module.cwrap('moo_ignite', 'number', ['number', 'number']),
initdbgi: Module.cwrap('moo_initdbgi', 'number', ['number', 'number']), initdbgi: Module.cwrap('moo_initdbgi', 'number', ['number', 'number']),
compilefile: Module.cwrap('moo_compilefileb', 'number', ['number', 'string']), compilefile: Module.cwrap('moo_compilefileb', 'number', ['number', 'string']),
invoke: Module.cwrap('moo_invokestdb', 'number', ['number', 'string', 'string']) invoke: Module.cwrap('moo_invokebynameb', 'number', ['number', 'string', 'string'])
}; };
/*
var log_write = Module.addFunction(function() {
});
*/
//console.log ("QQ %O\n", self); //console.log ("QQ %O\n", self);
//self.onmessage = function (evt) { //self.onmessage = function (evt) {
@ -44,7 +48,7 @@ self.addEventListener ('message', function (evt) {
tmp = libmoo.initdbgi(moo, 102400); tmp = libmoo.initdbgi(moo, 102400);
msg = msg.concat(" initdgbi - " + tmp); msg = msg.concat(" initdgbi - " + tmp);
tmp = libmoo.compilefile(moo, "kernel/test-001.moo"); tmp = libmoo.compilefile(moo, "kernel/test-003.moo");
msg = msg.concat(" compilefile - " + tmp); msg = msg.concat(" compilefile - " + tmp);
tmp = libmoo.invoke(moo, "MyObject", "main"); tmp = libmoo.invoke(moo, "MyObject", "main");