diff --git a/native/PluginProcessor.cpp b/native/PluginProcessor.cpp index 9a89000..f3fb4e1 100644 --- a/native/PluginProcessor.cpp +++ b/native/PluginProcessor.cpp @@ -265,31 +265,14 @@ void EffectsPluginProcessor::initJavaScriptEngine() }); jsContext.registerFunction("__log__", [this](choc::javascript::ArgumentList args) { - const auto* kDispatchScript = R"script( -(function() { - console.log(...JSON.parse(%)); - return true; -})(); -)script"; - - // Forward logs to the editor if it's available; then logs show up in one place. - // - // If not available, we fall back to std out. - if (auto* editor = static_cast(getActiveEditor())) { - auto v = choc::value::createEmptyArray(); - - for (size_t i = 0; i < args.numArgs; ++i) { - v.addArrayElement(*args[i]); - } + auto v = choc::value::createEmptyArray(); - auto expr = juce::String(kDispatchScript).replace("%", elem::js::serialize(choc::json::toString(v))).toStdString(); - editor->getWebViewPtr()->evaluateJavascript(expr); - } else { - for (size_t i = 0; i < args.numArgs; ++i) { - DBG(choc::json::toString(*args[i])); - } + for (size_t i = 0; i < args.numArgs; ++i) { + v.addArrayElement(*args[i]); } + logEmbeddedMessage(choc::json::toString(v)); + return choc::value::Value(); }); @@ -320,7 +303,12 @@ void EffectsPluginProcessor::initJavaScriptEngine() auto dspEntryFile = getAssetsDirectory().getChildFile("dsp.main.js"); auto dspEntryFileContents = dspEntryFile.loadFileAsString().toStdString(); #endif - jsContext.evaluate(dspEntryFileContents); + try { + jsContext.evaluate(dspEntryFileContents); + } catch (choc::javascript::Error const& e) { + // TODO: Something like this... need to fix the serialization + logEmbeddedMessage(std::string(e.what())); + } // Re-hydrate from current state const auto* kHydrateScript = R"script( @@ -328,7 +316,12 @@ void EffectsPluginProcessor::initJavaScriptEngine() if (typeof globalThis.__receiveHydrationData__ !== 'function') return false; - globalThis.__receiveHydrationData__(%); + try { + globalThis.__receiveHydrationData__(%); + } catch (e) { + console.error(e.name, e.message); + } + return true; })(); )script"; @@ -344,7 +337,12 @@ void EffectsPluginProcessor::dispatchStateChange() if (typeof globalThis.__receiveStateChange__ !== 'function') return false; - globalThis.__receiveStateChange__(%); + try { + globalThis.__receiveStateChange__(%); + } catch (e) { + console.error(e.name, e.message); + } + return true; })(); )script"; @@ -378,7 +376,12 @@ void EffectsPluginProcessor::dispatchError(std::string const& name, std::string let e = new Error(%); e.name = @; - globalThis.__receiveError__(e); + try { + globalThis.__receiveError__(e); + } catch (e) { + console.error(e.name, e.message); + } + return true; })(); )script"; @@ -397,6 +400,23 @@ void EffectsPluginProcessor::dispatchError(std::string const& name, std::string jsContext.evaluate(expr); } +//============================================================================== +void EffectsPluginProcessor::logEmbeddedMessage(std::string const& serializedMessage) +{ + const auto* kDispatchScript = R"script( +(function() { + console.log(...JSON.parse(%)); + return true; +})(); +)script"; + + // Forward logs to the editor if it's available; then all logs show up in one place. + if (auto* editor = static_cast(getActiveEditor())) { + auto expr = juce::String(kDispatchScript).replace("%", elem::js::serialize(serializedMessage)).toStdString(); + editor->getWebViewPtr()->evaluateJavascript(expr); + } +} + //============================================================================== void EffectsPluginProcessor::getStateInformation (juce::MemoryBlock& destData) { diff --git a/native/PluginProcessor.h b/native/PluginProcessor.h index d828257..0f9a0af 100644 --- a/native/PluginProcessor.h +++ b/native/PluginProcessor.h @@ -67,6 +67,9 @@ class EffectsPluginProcessor void dispatchError(std::string const& name, std::string const& message); private: + //============================================================================== + void logEmbeddedMessage(std::string const& serializedMessage); + //============================================================================== std::atomic shouldInitialize { false }; double lastKnownSampleRate = 0;