From bcb2a742a7a87882cef69476be1e86627bad60a8 Mon Sep 17 00:00:00 2001 From: kgv Date: Tue, 19 Oct 2021 01:56:18 +0300 Subject: [PATCH] 0032631: Draw Harness - add DRAWEXE.evalAsync() to WebAssembly --- src/DRAWEXE/DRAWEXE.cxx | 48 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/DRAWEXE/DRAWEXE.cxx b/src/DRAWEXE/DRAWEXE.cxx index f76eefd835..401e47d09d 100644 --- a/src/DRAWEXE/DRAWEXE.cxx +++ b/src/DRAWEXE/DRAWEXE.cxx @@ -20,6 +20,8 @@ #include #include #include +#include +#include #include #ifdef OCCT_NO_PLUGINS @@ -39,6 +41,16 @@ Standard_IMPORT Standard_Boolean Draw_Interprete (const char* theCommand); #if defined(__EMSCRIPTEN__) #include #include +#include + +//! Signal async command completion to Module.evalAsyncCompleted callback. +EM_JS(void, occJSEvalAsyncCompleted, (int theResult), { + if (Module.evalAsyncCompleted != undefined) { + Module.evalAsyncCompleted (theResult); + } else { + console.error ("Module.evalAsyncCompleted() is undefined"); + } +}); //! Draw Harness interface for JavaScript. class DRAWEXE @@ -66,6 +78,41 @@ public: { return Draw::GetInterpretor().Complete (theCommand.c_str()); } + + //! Evaluate Tcl command asynchronously. + static void evalAsync (const std::string& theCommand) + { + #if defined(__EMSCRIPTEN_PTHREADS__) + std::string* aCmdPtr = new std::string (theCommand); + OSD_Thread aThread (&evalAsyncEntry); + aThread.Run (aCmdPtr); + #else + // fallback synchronous implementation + int aRes = eval (theCommand); + occJSEvalAsyncCompleted (aRes); + #endif + } + +#if defined(__EMSCRIPTEN_PTHREADS__) +private: + //! Thread entry for async command execution. + static Standard_Address evalAsyncEntry (Standard_Address theData) + { + OSD::SetSignal (false); + std::string* aCmdPtr = (std::string* )theData; + const std::string aCmd = *aCmdPtr; + delete aCmdPtr; + int aRes = eval (aCmd); + emscripten_async_run_in_main_runtime_thread (EM_FUNC_SIG_VI, evalAsyncCompletedEntry, aRes); + return 0; + } + + //! Notify Module.evalAsyncCompleted about async cmd completion. + static void evalAsyncCompletedEntry (int theResult) + { + occJSEvalAsyncCompleted (theResult); + } +#endif }; //! Print message to Module.printMessage callback. @@ -109,6 +156,7 @@ protected: EMSCRIPTEN_BINDINGS(DRAWEXE) { emscripten::function("eval", &DRAWEXE::eval); + emscripten::function("evalAsync", &DRAWEXE::evalAsync); emscripten::function("isComplete", &DRAWEXE::isComplete); } #endif