11#include " AppRuntime.h"
22#include " WorkQueue.h"
3- #include < sstream>
4-
5- namespace {
6- std::string GetStringPropertyFromError (Napi::Error error, const char * propertyName)
7- {
8- Napi::Value value = error.Get (propertyName);
9- if (value.IsUndefined ())
10- {
11- return " " ;
12- }
13- return value.ToString ().Utf8Value ();
14- }
15-
16- int32_t GetNumberPropertyFromError (Napi::Error error, const char * propertyName)
17- {
18- Napi::Value value = error.Get (propertyName);
19- if (value.IsUndefined ())
20- {
21- return -1 ;
22- }
23- return value.ToNumber ().Int32Value ();
24- }
25- }
3+ #include < cassert>
264
275namespace Babylon
286{
29- AppRuntime::AppRuntime ()
30- : AppRuntime{DefaultUnhandledExceptionHandler }
7+ AppRuntime::AppRuntime () :
8+ AppRuntime{{} }
319 {
3210 }
3311
34- AppRuntime::AppRuntime (std::function< void ( const std::exception&)> unhandledExceptionHandler )
12+ AppRuntime::AppRuntime (Options options )
3513 : m_workQueue{std::make_unique<WorkQueue>([this ] { RunPlatformTier (); })}
36- , m_unhandledExceptionHandler{unhandledExceptionHandler }
14+ , m_options{ std::move (options) }
3715 {
3816 Dispatch ([this ](Napi::Env env) {
3917 JsRuntime::CreateForJavaScript (env, [this ](auto func) { Dispatch (std::move (func)); });
@@ -59,29 +37,6 @@ namespace Babylon
5937 m_workQueue->Resume ();
6038 }
6139
62- std::string AppRuntime::GetErrorInfos ()
63- {
64- std::ostringstream ss{};
65- try
66- {
67- throw ;
68- }
69- catch (const Napi::Error& error)
70- {
71- std::string msg = error.Message ();
72- std::string source = GetStringPropertyFromError (error, " source" );
73- std::string url = GetStringPropertyFromError (error, " url" );
74- int32_t line = GetNumberPropertyFromError (error, " line" );
75- int32_t column = GetNumberPropertyFromError (error, " column" );
76- int32_t length = GetNumberPropertyFromError (error, " length" );
77- std::string stack = GetStringPropertyFromError (error, " stack" );
78-
79- ss << " Error on line " << line << " and column " << column
80- << " : " << msg << " . Length: " << length << " . Source: " << source << " . URL: " << url << " . Stack:" << std::endl << stack << std::endl;
81- }
82- return ss.str ();
83- }
84-
8540 void AppRuntime::Dispatch (Dispatchable<void (Napi::Env)> func)
8641 {
8742 m_workQueue->Append ([this , func{std::move (func)}](Napi::Env env) mutable {
@@ -90,12 +45,13 @@ namespace Babylon
9045 {
9146 func (env);
9247 }
93- catch (const std::exception & error)
48+ catch (const Napi::Error & error)
9449 {
95- m_unhandledExceptionHandler (error);
50+ m_options. UnhandledExceptionHandler (error);
9651 }
9752 catch (...)
9853 {
54+ assert (false );
9955 std::abort ();
10056 }
10157 });
0 commit comments