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
}
#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;
}
// 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)
{

View File

@ -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 .

View File

@ -20,3 +20,4 @@ var _jsSleep = function(pointer)
await prom;
*/
}

View File

@ -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}`);
};

View File

@ -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");