diff --git a/moo/kernel/System.moo b/moo/kernel/System.moo index fb1022e..e53c8c8 100644 --- a/moo/kernel/System.moo +++ b/moo/kernel/System.moo @@ -13,6 +13,8 @@ class System(Apex) var(#class) asyncsg. var(#class) gcfin_sem. var(#class) gcfin_should_exit := false. + var(#class) gcfin_proc. + var(#class) ossig_proc. var(#class) shr. // signal handler registry pooldic Log @@ -62,7 +64,7 @@ class System(Apex) method(#class) startup(class_name, method_name) { - | class ret gcfin_proc ossig_proc | + | class ret | System gc. @@ -77,11 +79,11 @@ class System(Apex) // start the gc finalizer process and os signal handler process //[ self __gc_finalizer ] fork. //[ self __os_sig_handler ] fork. - gcfin_proc := [ self __gc_finalizer ] newProcess. - ossig_proc := [ :caller | self __os_sig_handler: caller ] newProcess(thisProcess). + self.gcfin_proc := [ self __gc_finalizer ] newProcess. + self.ossig_proc := [ :caller | self __os_sig_handler: caller ] newProcess(thisProcess). - gcfin_proc resume. - ossig_proc resume. + self.gcfin_proc resume. + self.ossig_proc resume. [ // TODO: change the method signature to variadic and pass extra arguments to perform??? @@ -145,6 +147,7 @@ class System(Apex) self.gcfin_sem signal. // in case the process is stuck in wait. self.gcfin_sem unsignal. System logNl: 'End of GC finalization process ' & (thisProcess id) asString. + self.gcfin_proc := nil. ]. } @@ -203,7 +206,8 @@ class System(Apex) 2 -> __os_sig_handler 3 .. -> other processes started by application. */ - proc := System _findProcessByIdGreaterThan: 2. +/* TODO: this loop is error-prone as it can't handle when the processs id wraps back and the id of gcfin_proc and ossig_proc gets bigger than normal child processes */ + proc := System _findProcessByIdGreaterThan: (self.ossig_proc id). while (proc notError) { pid := proc id. @@ -221,6 +225,7 @@ class System(Apex) self.gcfin_should_exit := true. self.gcfin_sem signal. // wake the gcfin process. + self.ossig_proc := nil. self _halting. // inform VM that it should get ready for halting. ]. } diff --git a/moo/lib/gc.c b/moo/lib/gc.c index 74283cb..071d4e3 100644 --- a/moo/lib/gc.c +++ b/moo/lib/gc.c @@ -405,7 +405,7 @@ static kernel_class_info_t kernel_classes[] = { 6, { 'S','y','s','t','e','m' }, 0, - 4, /* asyncsg, gcfin_sem, gcfin_should_exit, shr */ + 6, /* asyncsg, gcfin_sem, gcfin_should_exit, gcfin_proc, ossig_proc, shr */ 0, 0, MOO_OBJ_TYPE_OOP, diff --git a/moo/wasm/build.sh b/moo/wasm/build.sh index b64c359..8e2c4c5 100644 --- a/moo/wasm/build.sh +++ b/moo/wasm/build.sh @@ -6,6 +6,7 @@ blddir=${topdir}/bld/emcc emcc -Wall -O2 -g \ ${blddir}/lib/.libs/libmoo.a \ + ${blddir}/lib/.libs/libmoox.a \ ${topdir}/wasm/main.c \ -DMOO_HAVE_CFG_H \ -I${blddir}/lib \ @@ -16,5 +17,7 @@ emcc -Wall -O2 -g \ --embed-file ${topdir}/kernel/ \ --pre-js ${topdir}/wasm/moo.cb.js -cp -pf ${topdir}/wasm/moo.html . -cp -pf ${topdir}/wasm/moo.worker.js . +##cp -pf ${topdir}/wasm/moo.html . +##cp -pf ${topdir}/wasm/moo.worker.js . +ln -sf ${topdir}/wasm/moo.html moo.html +ln -sf ${topdir}/wasm/moo.worker.js moo.worker.js diff --git a/moo/wasm/main.c b/moo/wasm/main.c index b3e7b5c..4415b2d 100644 --- a/moo/wasm/main.c +++ b/moo/wasm/main.c @@ -110,3 +110,20 @@ EMSCRIPTEN_KEEPALIVE int open_moo (void) #endif } +EMSCRIPTEN_KEEPALIVE moo_bch_t* get_errmsg_from_moo (moo_t* moo) +{ +#if defined(MOO_OOCH_IS_UCH) +/* TODO: no static.... error check... */ + static moo_bch_t bm[256]; + moo_oow_t bl = MOO_COUNTOF(bm); + moo_convutobcstr (moo, moo_geterrmsg(moo), MOO_NULL, bm, &bl); + return bm; +#else + return moo_geterrmsg(moo); +#endif +} + +EMSCRIPTEN_KEEPALIVE void switch_process_in_moo (moo_t* moo) +{ + moo_switchprocess(moo); +} diff --git a/moo/wasm/moo.html b/moo/wasm/moo.html index 5d4a84c..b60556e 100644 --- a/moo/wasm/moo.html +++ b/moo/wasm/moo.html @@ -3,29 +3,51 @@
- + + + - + g_WebWorker.onmessage = function (evt) { + console.log(`Message from the Web Worker:\n\n ${evt.data}`); + }; + + function do_open() + { + g_WebWorker.postMessage({ "cmd": "open-moo", "data": "nothing as of now.." }); + } + + function do_run() + { + g_WebWorker.postMessage({ "cmd": "run-moo", "data": "nothing as of now.." }); + } + + function do_close() + { + g_WebWorker.postMessage({ "cmd": "close-moo", "data": "nothing as of now.." }); + } + diff --git a/moo/wasm/moo.worker.js b/moo/wasm/moo.worker.js index 7190ca1..43cf124 100644 --- a/moo/wasm/moo.worker.js +++ b/moo/wasm/moo.worker.js @@ -1,60 +1,108 @@ self.importScripts('libmoo.js'); -self.__ready = false; +self.libmoo = null; +self.moo = null; + +Module.noExitRuntime = false; +Module.onExit = function (status) { + console.log ('exiting....'); +}; Module.onRuntimeInitialized = function() { - self.__ready = true; - console.log ('runtime is ready now...'); -} + console.log ('runtime is ready now...'); -const libmoo = { - open: Module.cwrap('moo_openstd', 'number', ['number', 'number', 'number']), - close: Module.cwrap('moo_close', '', ['number']), - 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_invokebynameb', 'number', ['number', 'string', 'string']) + self.libmoo = { + open: Module.cwrap('moo_openstd', 'number', ['number', 'number', 'number']), + close: Module.cwrap('moo_close', '', ['number']), + 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_invokebynameb', 'number', ['number', 'string', 'string']), + geterrmsg: Module.cwrap('get_errmsg_from_moo', 'string', ['number']), + switchprocess: Module.cwrap('switch_process_in_moo', 'undefined', ['number']) + }; }; -/* -var log_write = Module.addFunction(function() { - -}); -*/ - //self.onmessage = function (evt) { self.addEventListener ('message', function (evt) { - var objData = evt.data; + var objData = evt.data; - var cmd = objData.cmd; - if (cmd === "test-moo") - { - //var x = Module.ccall('moo_openstd', [0, null, null]); - //Module.ccall('moo_close', x); + var cmd = objData.cmd; + if (cmd === "run-moo") + { + //var x = Module.ccall('moo_openstd', [0, null, null]); + //Module.ccall('moo_close', x); - if (self.__ready) - { - var moo, tmp; - var msg = ""; + if (self.moo !== null) + { + var tmp, msg = ""; + //var ticker; - moo = libmoo.open(0, null, null); + // while self.libmoo.invoke() is running, setInterval() is unable to trigger the timed event. + // it's an architectural issue. emscripten doesn't implement setitimer() yet. + // the moo program should 'yield' manually for process switching for now. + //ticker = setInterval(function() { self.libmoo.switchprocess (self.moo); console.log("ticking"); }, 100); + + tmp = self.libmoo.invoke(self.moo, "MyObject", "main"); + msg = msg.concat("invoke - " + tmp); + + //clearInterval (ticker); + + self.postMessage (msg); + } + else + { + self.postMessage ("not open"); + } + } + else if (cmd == "open-moo") + { + if (self.libmoo === null) + { + self.postMessage ("not ready"); + } + else if (self.moo === null) + { + var tmp, msg = ""; + + self.moo = self.libmoo.open(0, null, null); msg = msg.concat("open - " + moo); - tmp = libmoo.ignite(moo, 5000000); + tmp = self.libmoo.ignite(self.moo, 5000000); msg = msg.concat(" ignite - " + tmp); - tmp = libmoo.initdbgi(moo, 102400); + tmp = self.libmoo.initdbgi(self.moo, 102400); msg = msg.concat(" initdgbi - " + tmp); - tmp = libmoo.compilefile(moo, "kernel/t.moo"); - msg = msg.concat(" compilefile - " + tmp); + tmp = self.libmoo.compilefile(self.moo, "kernel/t.moo"); + if (tmp == -1) + { + msg = msg.concat(" compilefile - " + self.libmoo.geterrmsg(self.moo)); + } + else + { + msg = msg.concat(" compilefile - " + tmp); + } - tmp = libmoo.invoke(moo, "MyObject", "main"); - msg = msg.concat(" invoke - " + tmp); + self.postMessage (msg); + } + else + { + self.postMessage ("already open"); + } + } - self.postMessage (msg); - - libmoo.close (moo); - } - } + else if (cmd == "close-moo") + { + if (self.moo !== null) + { + self.libmoo.close (self.moo); + self.moo = null; + self.postMessage ("close ok"); + } + else + { + self.postMessage ("already closed"); + } + } });