@@ -179,19 +179,32 @@ NativeJSRenderer::NativeJSRenderer(std::string waylandDisplay): mEngine(nullptr)
179179
180180NativeJSRenderer::~NativeJSRenderer ()
181181{
182- gPendingRequests .clear ();
183- if (mEngine )
184- {
185- delete mEngine ;
186- mEngine = nullptr ;
182+ mShutdownConsole = true ;
183+ if (mConsoleThread .joinable ()) {
184+ mConsoleThread .join ();
187185 }
186+ gPendingRequests .clear ();
187+
188188 if (mConsoleState && mConsoleState ->consoleContext ) {
189189 delete mConsoleState ->consoleContext ;
190+ mConsoleState ->consoleContext = nullptr ;
191+ }
192+ if (mEngine ){
193+ delete mEngine ;
194+ mEngine = nullptr ;
190195 }
196+
191197}
192198
193199void NativeJSRenderer::setEnvForConsoleMode (ModuleSettings& moduleSettings)
194200{
201+
202+ if (mConsoleThread .joinable ()) {
203+ mShutdownConsole = true ;
204+ mConsoleThread .join ();
205+ }
206+
207+ mShutdownConsole = false ;
195208 mConsoleState = std::make_unique<ConsoleState>();
196209 mConsoleState ->moduleSettings = moduleSettings;
197210
@@ -205,8 +218,7 @@ void NativeJSRenderer::setEnvForConsoleMode(ModuleSettings& moduleSettings)
205218 mConsoleState ->consoleContext = context;
206219
207220 NativeJSLogger::log (INFO, " Running developer console...\n " );
208- std::thread consoleThread (&JsRuntime::NativeJSRenderer::runDeveloperConsole, this , std::ref (mConsoleState ->moduleSettings ));
209- consoleThread.detach ();
221+ mConsoleThread = std::thread (&JsRuntime::NativeJSRenderer::runDeveloperConsole, this , std::ref (mConsoleState ->moduleSettings ));
210222
211223 mConsoleMode = true ;
212224}
@@ -542,12 +554,10 @@ void NativeJSRenderer::processDevConsoleRequests()
542554 mConsoleState ->inputMutex .unlock ();
543555}
544556
545- namespace {
546- bool consoleLoop = true ;
547- void handleDevConsoleSigInt (int /* sig*/ ){
548- consoleLoop = false ;
549- }
550- } // namespace
557+ bool NativeJSRenderer::consoleLoop = true ;
558+ void handleDevConsoleSigInt (int /* sig*/ ) {
559+ NativeJSRenderer::consoleLoop = false ;
560+ }
551561
552562void NativeJSRenderer::runDeveloperConsole (ModuleSettings moduleSettings)
553563{
@@ -557,7 +567,17 @@ void NativeJSRenderer::runDeveloperConsole(ModuleSettings moduleSettings)
557567 NativeJSLogger::log (INFO, " Type 'exit' or press CTRL+C and ENTER to quit.\n " );
558568
559569 signal (SIGINT, handleDevConsoleSigInt);
560- while (consoleLoop) {
570+
571+ #ifdef UNIT_TEST_BUILD
572+ input = " exit" ;
573+ NativeJSLogger::log (INFO, " [UNIT_TEST_BUILD] Auto-exiting developer console" );
574+ delete mConsoleState ->consoleContext ;
575+ mConsoleState ->consoleContext = nullptr ;
576+ signal (SIGINT, SIG_DFL);
577+ return ;
578+ #else
579+
580+ while (consoleLoop && !mShutdownConsole ) {
561581 // Don't display another input prompt until previous code is processed (prevents)
562582 {
563583 std::unique_lock<std::mutex> lk (mConsoleState ->isProcessing_cv_m );
@@ -566,11 +586,14 @@ void NativeJSRenderer::runDeveloperConsole(ModuleSettings moduleSettings)
566586 mConsoleState ->isProcessing = true ;
567587 }
568588
589+ if (mShutdownConsole ) break ;
569590 std::getline (std::cin, input);
591+ if (mShutdownConsole ) break ;
570592
571593 // Short-cirtuit: in case consoleLoop was altered by signal handler we shouldn't execute lines below
572594 if (!consoleLoop || input == " exit" ) {
573595 delete mConsoleState ->consoleContext ;
596+ mConsoleState ->consoleContext = nullptr ;
574597 break ;
575598 }
576599
@@ -580,6 +603,7 @@ void NativeJSRenderer::runDeveloperConsole(ModuleSettings moduleSettings)
580603 }
581604
582605 signal (SIGINT, SIG_DFL);
606+ #endif
583607}
584608
585609bool NativeJSRenderer::downloadFile (std::string& url, MemoryStruct& chunk)
0 commit comments