some wasm experiments

This commit is contained in:
hyunghwan.chung 2019-11-02 06:34:18 +00:00
parent ef75871a93
commit 09a1cb95ec
6 changed files with 174 additions and 22 deletions

View File

@ -476,7 +476,7 @@ int moo_inttoooi (moo_t* moo, moo_oop_t x, moo_ooi_t* i)
/* do nothing. required macros are defined in moo.h */ /* do nothing. required macros are defined in moo.h */
#elif (MOO_SIZEOF_UINTMAX_T == MOO_SIZEOF_OOW_T * 2) #elif (MOO_SIZEOF_UINTMAX_T == MOO_SIZEOF_OOW_T * 2) || (MOO_SIZEOF_UINTMAX_T == MOO_SIZEOF_OOW_T * 4)
static MOO_INLINE int bigint_to_uintmax_noseterr (moo_t* moo, moo_oop_t num, moo_uintmax_t* w) static MOO_INLINE int bigint_to_uintmax_noseterr (moo_t* moo, moo_oop_t num, moo_uintmax_t* w)
{ {
MOO_ASSERT (moo, MOO_OOP_IS_POINTER(num)); MOO_ASSERT (moo, MOO_OOP_IS_POINTER(num));
@ -492,9 +492,25 @@ static MOO_INLINE int bigint_to_uintmax_noseterr (moo_t* moo, moo_oop_t num, moo
goto done; goto done;
case 2: case 2:
*w = ((moo_uintmax_t)MOO_OBJ_GET_WORD_VAL(num, 0) << MOO_LIW_BITS) | MOO_OBJ_GET_WORD_VAL(num, 1); *w = ((moo_uintmax_t)MOO_OBJ_GET_WORD_VAL(num, 0) << MOO_LIW_BITS) |
((moo_uintmax_t)MOO_OBJ_GET_WORD_VAL(num, 1));
goto done; goto done;
#if (MOO_SIZEOF_UINTMAX_T >= MOO_SIZEOF_OOW_T * 4)
case 3:
*w = ((moo_uintmax_t)MOO_OBJ_GET_WORD_VAL(num, 0) << (MOO_LIW_BITS * 2)) |
((moo_uintmax_t)MOO_OBJ_GET_WORD_VAL(num, 1) << (MOO_LIW_BITS * 1)) |
((moo_uintmax_t)MOO_OBJ_GET_WORD_VAL(num, 2))
goto done;
case 4:
*w = ((moo_uintmax_t)MOO_OBJ_GET_WORD_VAL(num, 0) << (MOO_LIW_BITS * 3)) |
((moo_uintmax_t)MOO_OBJ_GET_WORD_VAL(num, 1) << (MOO_LIW_BITS * 2)) |
((moo_uintmax_t)MOO_OBJ_GET_WORD_VAL(num, 2) << (MOO_LIW_BITS * 1)) |
((moo_uintmax_t)MOO_OBJ_GET_WORD_VAL(num, 3))
goto done;
#endif
default: default:
return 0; /* not convertable */ return 0; /* not convertable */
} }
@ -504,16 +520,39 @@ static MOO_INLINE int bigint_to_uintmax_noseterr (moo_t* moo, moo_oop_t num, moo
switch (MOO_OBJ_GET_SIZE(num)) switch (MOO_OBJ_GET_SIZE(num))
{ {
case 2: case 2:
*w = ((moo_uintmax_t)MOO_OBJ_GET_HALFWORD_VAL(num, 0) << MOO_LIW_BITS) | MOO_OBJ_GET_HALFWORD_VAL(num, 1); *w = ((moo_uintmax_t)MOO_OBJ_GET_HALFWORD_VAL(num, 0) << MOO_LIW_BITS) |
((moo_uintmax_t)MOO_OBJ_GET_HALFWORD_VAL(num, 1));
goto done; goto done;
case 4: case 4:
*w = ((moo_uintmax_t)MOO_OBJ_GET_HALFWORD_VAL(num, 0) << MOO_LIW_BITS * 3) | *w = ((moo_uintmax_t)MOO_OBJ_GET_HALFWORD_VAL(num, 0) << MOO_LIW_BITS * 3) |
((moo_uintmax_t)MOO_OBJ_GET_HALFWORD_VAL(num, 1) << MOO_LIW_BITS * 2) | ((moo_uintmax_t)MOO_OBJ_GET_HALFWORD_VAL(num, 1) << MOO_LIW_BITS * 2) |
((moo_uintmax_t)MOO_OBJ_GET_HALFWORD_VAL(num, 2) << MOO_LIW_BITS * 1) | ((moo_uintmax_t)MOO_OBJ_GET_HALFWORD_VAL(num, 2) << MOO_LIW_BITS * 1) |
MOO_OBJ_GET_HALFWORD_VAL(num, 3); ((moo_uintmax_t)MOO_OBJ_GET_HALFWORD_VAL(num, 3));
goto done; goto done;
#if (MOO_SIZEOF_UINTMAX_T >= MOO_SIZEOF_OOW_T * 4)
case 6:
*w = ((moo_uintmax_t)MOO_OBJ_GET_HALFWORD_VAL(num, 0) << MOO_LIW_BITS * 5) |
((moo_uintmax_t)MOO_OBJ_GET_HALFWORD_VAL(num, 1) << MOO_LIW_BITS * 4) |
((moo_uintmax_t)MOO_OBJ_GET_HALFWORD_VAL(num, 2) << MOO_LIW_BITS * 3) |
((moo_uintmax_t)MOO_OBJ_GET_HALFWORD_VAL(num, 3) << MOO_LIW_BITS * 2) |
((moo_uintmax_t)MOO_OBJ_GET_HALFWORD_VAL(num, 4) << MOO_LIW_BITS * 1) |
((moo_uintmax_t)MOO_OBJ_GET_HALFWORD_VAL(num, 5));
goto done;
case 8:
*w = ((moo_uintmax_t)MOO_OBJ_GET_HALFWORD_VAL(num, 0) << MOO_LIW_BITS * 7) |
((moo_uintmax_t)MOO_OBJ_GET_HALFWORD_VAL(num, 1) << MOO_LIW_BITS * 6) |
((moo_uintmax_t)MOO_OBJ_GET_HALFWORD_VAL(num, 2) << MOO_LIW_BITS * 5) |
((moo_uintmax_t)MOO_OBJ_GET_HALFWORD_VAL(num, 3) << MOO_LIW_BITS * 4) |
((moo_uintmax_t)MOO_OBJ_GET_HALFWORD_VAL(num, 4) << MOO_LIW_BITS * 3) |
((moo_uintmax_t)MOO_OBJ_GET_HALFWORD_VAL(num, 5) << MOO_LIW_BITS * 2) |
((moo_uintmax_t)MOO_OBJ_GET_HALFWORD_VAL(num, 6) << MOO_LIW_BITS * 1) |
((moo_uintmax_t)MOO_OBJ_GET_HALFWORD_VAL(num, 7));
goto done;
#endif
default: default:
return 0; /* not convertable */ return 0; /* not convertable */
} }

View File

@ -115,8 +115,26 @@ MOO_EXPORT int moo_compilestd(
const moo_iostd_t* in, const moo_iostd_t* in,
moo_oow_t count moo_oow_t count
); );
MOO_EXPORT int moo_compilefileb (
moo_t* moo,
const moo_bch_t* path
);
MOO_EXPORT int moo_compilefileu (
moo_t* moo,
const moo_uch_t* path
);
#endif #endif
MOO_EXPORT int moo_invokestdb (
moo_t* moo,
const moo_bch_t* objname,
const moo_bch_t* mthname
);
MOO_EXPORT void moo_rcvtickstd ( MOO_EXPORT void moo_rcvtickstd (
moo_t* moo, moo_t* moo,
int v int v

View File

@ -4241,8 +4241,61 @@ int moo_compilestd (moo_t* moo, const moo_iostd_t* in, moo_oow_t count)
return 0; return 0;
} }
int moo_compilefileb (moo_t* moo, const moo_bch_t* path)
{
moo_iostd_t in;
MOO_MEMSET (&in, 0, MOO_SIZEOF(in));
in.type = MOO_IOSTD_FILEB;
in.u.fileb.path = path;
return moo_compilestd(moo, &in, 1);
}
int moo_compilefileu (moo_t* moo, const moo_uch_t* path)
{
moo_iostd_t in;
MOO_MEMSET (&in, 0, MOO_SIZEOF(in));
in.type = MOO_IOSTD_FILEU;
in.u.fileu.path = path;
return moo_compilestd(moo, &in, 1);
}
#endif #endif
int moo_invokestdb (moo_t* moo, const moo_bch_t* objname, const moo_bch_t* mthname)
{
#if defined(MOO_OOCH_IS_UCH)
int n = -1;
moo_oocs_t o, m;
o.ptr = moo_dupbtooocstr(moo, objname, &o.len);
m.ptr = moo_dupbtooocstr(moo, mthname, &m.len);
if (!o.ptr || !m.ptr) goto done;
n = moo_invoke(moo, &o, &m);
done:
if (m.ptr) moo_freemem (moo, m.ptr);
if (o.ptr) moo_freemem (moo, o.ptr);
return n;
#else
moo_oocs_t o, m;
o.ptr = objname;
o.len = moo_count_bcstr(objname);
m.ptr = objname;
m.len = moo_count_bcstr(objname);
return moo_invoke(moo, &o, &m);
#endif
}
void moo_rcvtickstd (moo_t* moo, int v) void moo_rcvtickstd (moo_t* moo, int v)
{ {
xtn_t* xtn = GET_XTN(moo); xtn_t* xtn = GET_XTN(moo);

View File

@ -6,16 +6,33 @@ Temporary notes on how to build libmoo to web assembly with emscripten.
#### How to Build #### How to Build
mkdir -p bld/emcc mkdir -p bld/emcc
cd blc/emcc cd blc/emcc
## run configure and make under emconfigure and emmake respectively
emconfigure ../../configure --prefix=${HOME}/xxx-emcc --disable-shared --enable-dynamic-module --disable-mod-x11 --disable-mod-con --disable-mod-sck CFLAGS="-Wall -O2 -g" emconfigure ../../configure --prefix=${HOME}/xxx-emcc --disable-shared --enable-dynamic-module --disable-mod-x11 --disable-mod-con --disable-mod-sck CFLAGS="-Wall -O2 -g"
emmake make emmake make
## convert the library to web assembly - the command produces libmoo.js and libmoo.wasm
##emcc lib/.libs/libmoo.a -s WASM=1 -s EXPORT_ALL=1 -s LINKABLE=1 -o libmoo.js ##emcc lib/.libs/libmoo.a -s WASM=1 -s EXPORT_ALL=1 -s LINKABLE=1 -o libmoo.js
#if you have defined MOO_EXPORT as EMSCRIPTEN_KEEPALIVE, the following will do #if you have defined MOO_EXPORT as EMSCRIPTEN_KEEPALIVE, the following will do
##emcc lib/.libs/libmoo.a -s WASM=1 -s -s LINKABLE=1 -o libmoo.js ##emcc lib/.libs/libmoo.a -s WASM=1 -s -s LINKABLE=1 -o libmoo.js
###emcc lib/.libs/libmoo.a lib/.libs/libmoox.a -s WASM=1 -s -s LINKABLE=1 -o libmoo.js ###emcc lib/.libs/libmoo.a lib/.libs/libmoox.a -s WASM=1 -s LINKABLE=1 -o libmoo.js
emcc lib/.libs/libmoo.a lib/.libs/libmoox.a -s WASM=1 -s -s LINKABLE=1 -s EXTRA_EXPORTED_RUNTIME_METHODS="['ccall','cwrap']" -o libmoo.js
## -s ALLOW_MEMORY_GROWTH=1
emcc lib/.libs/libmoo.a lib/.libs/libmoox.a -s WASM=1 -s LINKABLE=1 -s EXTRA_EXPORTED_RUNTIME_METHODS="['ccall','cwrap']" -o libmoo.js --embed-file ../../kernel/
### --embed-file, if given a directory, includes all files under the directory.
### the result libmoo.js will contain lines like below:
### Module['FS_createDataFile']('/kernel', 'Apex.moo', fileData35, true, true, false);
### var fileData36 = [];
### fileData36.push.apply(fileData36, [35, 105, ...
###
## when i executed the command above, some versions of wasm-ld complained like below
## if the library is compiled with -pthread.
## wasm-ld: error: 'atomics' feature is disallowed by atomic_2835fb94.c.o, so --shared-memory must not be used
## a quick work around was to remove -pthread before building with 'emmake make'
## if defined some javascript functions in moo.cb.js... ## if defined some javascript functions in moo.cb.js...
emcc -Wall -O2 -g lib/.libs/libmoo.a ws.c -DMOO_HAVE_CFG_H -Ilib -I../../lib -s WASM=1 -s -s LINKABLE=1 -s EXTRA_EXPORTED_RUNTIME_METHODS="['ccall','cwrap']" -o libmoo.js --pre-js moo.cb.js emcc -Wall -O2 -g lib/.libs/libmoo.a ws.c -DMOO_HAVE_CFG_H -Ilib -I../../lib -s WASM=1 -s LINKABLE=1 -s EXTRA_EXPORTED_RUNTIME_METHODS="['ccall','cwrap']" -o libmoo.js --pre-js moo.cb.js
#### moo.worker.js #### moo.worker.js

View File

@ -5,19 +5,23 @@
<body> <body>
<input type="button" value="Test" onclick="do_test();" /> <input type="button" value="Test" onclick="do_test();" />
<script type="text/javascript"> <script type="text/javascript">
var g_WebWorker = new Worker("moo.worker.js"); var g_WebWorker = new Worker("moo.worker.js");
if (typeof(WebAssembly) === 'undefined')
{
alert ('No web assebly support in this browser');
}
//if (typeof(WebAssembly) === 'undefined') g_WebWorker.onerror = function (evt) {
//{ console.log(`Error from Web Worker: ${evt.message}`);
// alert ('No web assebly support in this browser'); };
//}
g_WebWorker.onerror = function (evt) { console.log(`Error from Web Worker: ${evt.message}`); } g_WebWorker.onmessage = function (evt) {
g_WebWorker.onmessage = function (evt) { console.log(`Message from the Web Worker:\n\n ${evt.data}`); } console.log(`Message from the Web Worker:\n\n ${evt.data}`);
};
function do_test() { function do_test()
{
g_WebWorker.postMessage({ "cmd": "test-moo", "data": "nothing as of now.." }); g_WebWorker.postMessage({ "cmd": "test-moo", "data": "nothing as of now.." });
} }
</script> </script>

View File

@ -8,9 +8,14 @@ Module.onRuntimeInitialized = function()
} }
const libmoo = { const libmoo = {
//open: Module.cwrap('moo_openstd', 'number', ['number', 'number', 'number']), open: Module.cwrap('moo_openstd', 'number', ['number', 'number', 'number']),
//close: Module.cwrap('moo_close', '', ['number']), close: Module.cwrap('moo_close', '', ['number']),
open_moo: Module.cwrap('open_moo', '', ['']) 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'])
}; };
//console.log ("QQ %O\n", self); //console.log ("QQ %O\n", self);
@ -27,11 +32,27 @@ self.addEventListener ('message', function (evt) {
if (self.__ready) if (self.__ready)
{ {
//var moo = libmoo.open(0, null, null); var moo, tmp;
var moo = libmoo.open_moo(); var msg = "";
self.postMessage ('XXXXXXXXXXXXXXXx - ' + moo);
//libmoo.close (moo); moo = libmoo.open(0, null, null);
msg = msg.concat("open - " + moo);
tmp = libmoo.ignite(moo, 5000000);
msg = msg.concat(" ignite - " + tmp);
tmp = libmoo.initdbgi(moo, 102400);
msg = msg.concat(" initdgbi - " + tmp);
tmp = libmoo.compilefile(moo, "kernel/test-001.moo");
msg = msg.concat(" compilefile - " + tmp);
tmp = libmoo.invoke(moo, "MyObject", "main");
msg = msg.concat(" invoke - " + tmp);
self.postMessage (msg);
libmoo.close (moo);
} }
} }
}); });