From dbf066695b18a1101241c850c49f3851e9d0ffae Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Sun, 3 Nov 2019 16:02:22 +0000 Subject: [PATCH] experiment with emscripten and wasm --- moo/lib/std.c | 37 ++++++++++++++++++------------------- moo/wasm/build.sh | 2 +- moo/wasm/moo.cb.js | 5 +++-- moo/wasm/moo.html | 1 + moo/wasm/moo.worker.js | 4 +--- 5 files changed, 24 insertions(+), 25 deletions(-) diff --git a/moo/lib/std.c b/moo/lib/std.c index 050cd65..34140cf 100644 --- a/moo/lib/std.c +++ b/moo/lib/std.c @@ -820,28 +820,26 @@ static void free_heap (moo_t* moo, void* ptr) #endif } +#if defined(EMSCRIPTEN) +EM_JS(int, write_all, (int, const moo_bch_t* ptr, moo_oow_t len), { + // UTF8ToString() doesn't handle a null byte in the middle of an array. + // Use the heap memory and pass the right portion to UTF8Decoder. + //console.log ("%s", UTF8ToString(ptr, len)); + console.log ("%s", UTF8Decoder.decode(HEAPU8.subarray(ptr, ptr + len))); + return 0; +}); + +#else + 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; - - } - + EM_ASM_ ({ + // UTF8ToString() doesn't handle a null byte in the middle of an array. + // Use the heap memory and pass the right portion to UTF8Decoder. + //console.log ("%s", UTF8ToString($0, $1)); + console.log ("%s", UTF8Decoder.decode(HEAPU8.subarray($0, $0 + $1))); + }, ptr, len); #else while (len > 0) { @@ -875,6 +873,7 @@ static int write_all (int fd, const moo_bch_t* ptr, moo_oow_t len) return 0; } +#endif static int write_log (moo_t* moo, int fd, const moo_bch_t* ptr, moo_oow_t len) { diff --git a/moo/wasm/build.sh b/moo/wasm/build.sh index 7669057..b64c359 100644 --- a/moo/wasm/build.sh +++ b/moo/wasm/build.sh @@ -13,8 +13,8 @@ emcc -Wall -O2 -g \ -s WASM=1 -s LINKABLE=1 \ -s EXTRA_EXPORTED_RUNTIME_METHODS="['ccall','cwrap']" \ -o libmoo.js \ + --embed-file ${topdir}/kernel/ \ --pre-js ${topdir}/wasm/moo.cb.js - cp -pf ${topdir}/wasm/moo.html . cp -pf ${topdir}/wasm/moo.worker.js . diff --git a/moo/wasm/moo.cb.js b/moo/wasm/moo.cb.js index 99c3429..e5d01f1 100644 --- a/moo/wasm/moo.cb.js +++ b/moo/wasm/moo.cb.js @@ -9,7 +9,7 @@ var _jsGetTime = function(pointer) var _jsSleep = function(pointer) { -/* + /* var pos = pointer / 4; var msec = (HEAP32[POS] * 1000) + (HEAP32[pos + 1] / 1000000); @@ -18,5 +18,6 @@ var _jsSleep = function(pointer) }); await prom; -*/ + */ } + diff --git a/moo/wasm/moo.html b/moo/wasm/moo.html index 7e5322c..5d4a84c 100644 --- a/moo/wasm/moo.html +++ b/moo/wasm/moo.html @@ -12,6 +12,7 @@ alert ('No web assebly support in this browser'); } + g_WebWorker.onerror = function (evt) { console.log(`Error from Web Worker: ${evt.message}`); }; diff --git a/moo/wasm/moo.worker.js b/moo/wasm/moo.worker.js index 3984982..7190ca1 100644 --- a/moo/wasm/moo.worker.js +++ b/moo/wasm/moo.worker.js @@ -22,8 +22,6 @@ var log_write = Module.addFunction(function() { }); */ -//console.log ("QQ %O\n", self); - //self.onmessage = function (evt) { self.addEventListener ('message', function (evt) { var objData = evt.data; @@ -48,7 +46,7 @@ self.addEventListener ('message', function (evt) { tmp = libmoo.initdbgi(moo, 102400); msg = msg.concat(" initdgbi - " + tmp); - tmp = libmoo.compilefile(moo, "kernel/test-003.moo"); + tmp = libmoo.compilefile(moo, "kernel/t.moo"); msg = msg.concat(" compilefile - " + tmp); tmp = libmoo.invoke(moo, "MyObject", "main");