From 850fb4971f020d8d833fe1b91a4b022bd253d000 Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Sun, 3 Nov 2019 09:15:24 +0000 Subject: [PATCH] added moo_count_bcstrl() and moo_count_ucstrl() that limits the maximum length. added some experimental emscripten/wasm related code --- moo/kernel/test-003.moo | 2 +- moo/lib/moo-std.h | 2 +- moo/lib/moo-utl.h | 10 ++++++++++ moo/lib/std.c | 34 ++++++++++++++++++++++++++++++++-- moo/lib/utl.c | 20 ++++++++++++++++++++ moo/wasm/moo.worker.js | 12 ++++++++---- 6 files changed, 72 insertions(+), 8 deletions(-) diff --git a/moo/kernel/test-003.moo b/moo/kernel/test-003.moo index 716e3a9..072b956 100644 --- a/moo/kernel/test-003.moo +++ b/moo/kernel/test-003.moo @@ -227,7 +227,7 @@ EXCEPTION_TEST: Exception signal: 'experiment with exception signalling'. // TODO: - String format("%s", " 나 는\\\"") dump. + System logNl: String format("%s", " 나 는\\\""). #"a b\nc" dump. } } diff --git a/moo/lib/moo-std.h b/moo/lib/moo-std.h index cb4ff03..2e834f4 100644 --- a/moo/lib/moo-std.h +++ b/moo/lib/moo-std.h @@ -128,7 +128,7 @@ MOO_EXPORT int moo_compilefileu ( #endif -MOO_EXPORT int moo_invokestdb ( +MOO_EXPORT int moo_invokebynameb ( moo_t* moo, const moo_bch_t* objname, const moo_bch_t* mthname diff --git a/moo/lib/moo-utl.h b/moo/lib/moo-utl.h index aeb36ff..3d3ea44 100644 --- a/moo/lib/moo-utl.h +++ b/moo/lib/moo-utl.h @@ -557,10 +557,20 @@ MOO_EXPORT moo_oow_t moo_count_ucstr ( 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 ( 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) # 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) diff --git a/moo/lib/std.c b/moo/lib/std.c index ff36907..050cd65 100644 --- a/moo/lib/std.c +++ b/moo/lib/std.c @@ -31,7 +31,7 @@ #include #include -#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 #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) { +#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) { 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; len -= wr; } +#endif return 0; } @@ -926,7 +948,9 @@ static void log_write (moo_t* moo, moo_bitmask_t mask, const moo_ooch_t* msg, mo else { logfd = xtn->log.fd; +#if !defined(EMSCRIPTEN) if (logfd <= -1) return; +#endif } /* 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; 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; @@ -4266,7 +4296,7 @@ int moo_compilefileu (moo_t* moo, const moo_uch_t* path) #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) int n = -1; diff --git a/moo/lib/utl.c b/moo/lib/utl.c index 9a988c6..d578a4e 100644 --- a/moo/lib/utl.c +++ b/moo/lib/utl.c @@ -315,6 +315,16 @@ moo_oow_t moo_count_ucstr (const moo_uch_t* 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) { const moo_bch_t* ptr = str; @@ -322,6 +332,16 @@ moo_oow_t moo_count_bcstr (const moo_bch_t* 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) { const moo_uch_t* end; diff --git a/moo/wasm/moo.worker.js b/moo/wasm/moo.worker.js index 2b246d5..3984982 100644 --- a/moo/wasm/moo.worker.js +++ b/moo/wasm/moo.worker.js @@ -13,11 +13,15 @@ const libmoo = { ignite: Module.cwrap('moo_ignite', 'number', ['number', 'number']), initdbgi: Module.cwrap('moo_initdbgi', 'number', ['number', 'number']), 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); //self.onmessage = function (evt) { @@ -44,7 +48,7 @@ self.addEventListener ('message', function (evt) { tmp = libmoo.initdbgi(moo, 102400); 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); tmp = libmoo.invoke(moo, "MyObject", "main");