experiment with emscripten and wasm

This commit is contained in:
hyunghwan.chung 2019-11-03 16:02:22 +00:00
parent 850fb4971f
commit dbf066695b
5 changed files with 24 additions and 25 deletions

View File

@ -820,28 +820,26 @@ static void free_heap (moo_t* moo, void* ptr)
#endif #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) static int write_all (int fd, const moo_bch_t* ptr, moo_oow_t len)
{ {
#if defined(EMSCRIPTEN) #if defined(EMSCRIPTEN)
while (len > 0) EM_ASM_ ({
{ // UTF8ToString() doesn't handle a null byte in the middle of an array.
moo_oow_t slen; // Use the heap memory and pass the right portion to UTF8Decoder.
//console.log ("%s", UTF8ToString($0, $1));
/* if the output data contains a null byte in the middle, console.log ("%s", UTF8Decoder.decode(HEAPU8.subarray($0, $0 + $1)));
* UTF8ToString() cuts short before printing all data */ }, ptr, len);
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 #else
while (len > 0) while (len > 0)
{ {
@ -875,6 +873,7 @@ static int write_all (int fd, const moo_bch_t* ptr, moo_oow_t len)
return 0; return 0;
} }
#endif
static int write_log (moo_t* moo, int fd, const moo_bch_t* ptr, moo_oow_t len) static int write_log (moo_t* moo, int fd, const moo_bch_t* ptr, moo_oow_t len)
{ {

View File

@ -13,8 +13,8 @@ emcc -Wall -O2 -g \
-s WASM=1 -s LINKABLE=1 \ -s WASM=1 -s LINKABLE=1 \
-s EXTRA_EXPORTED_RUNTIME_METHODS="['ccall','cwrap']" \ -s EXTRA_EXPORTED_RUNTIME_METHODS="['ccall','cwrap']" \
-o libmoo.js \ -o libmoo.js \
--embed-file ${topdir}/kernel/ \
--pre-js ${topdir}/wasm/moo.cb.js --pre-js ${topdir}/wasm/moo.cb.js
cp -pf ${topdir}/wasm/moo.html . cp -pf ${topdir}/wasm/moo.html .
cp -pf ${topdir}/wasm/moo.worker.js . cp -pf ${topdir}/wasm/moo.worker.js .

View File

@ -9,7 +9,7 @@ var _jsGetTime = function(pointer)
var _jsSleep = function(pointer) var _jsSleep = function(pointer)
{ {
/* /*
var pos = pointer / 4; var pos = pointer / 4;
var msec = (HEAP32[POS] * 1000) + (HEAP32[pos + 1] / 1000000); var msec = (HEAP32[POS] * 1000) + (HEAP32[pos + 1] / 1000000);
@ -18,5 +18,6 @@ var _jsSleep = function(pointer)
}); });
await prom; await prom;
*/ */
} }

View File

@ -12,6 +12,7 @@
alert ('No web assebly support in this browser'); alert ('No web assebly support in this browser');
} }
g_WebWorker.onerror = function (evt) { g_WebWorker.onerror = function (evt) {
console.log(`Error from Web Worker: ${evt.message}`); console.log(`Error from Web Worker: ${evt.message}`);
}; };

View File

@ -22,8 +22,6 @@ var log_write = Module.addFunction(function() {
}); });
*/ */
//console.log ("QQ %O\n", self);
//self.onmessage = function (evt) { //self.onmessage = function (evt) {
self.addEventListener ('message', function (evt) { self.addEventListener ('message', function (evt) {
var objData = evt.data; var objData = evt.data;
@ -48,7 +46,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-003.moo"); tmp = libmoo.compilefile(moo, "kernel/t.moo");
msg = msg.concat(" compilefile - " + tmp); msg = msg.concat(" compilefile - " + tmp);
tmp = libmoo.invoke(moo, "MyObject", "main"); tmp = libmoo.invoke(moo, "MyObject", "main");