From 26f62542250d026269522787a61f807e5e4a1d8a Mon Sep 17 00:00:00 2001 From: Lauro Moura Date: Wed, 7 Dec 2016 18:00:36 -0300 Subject: [PATCH] js: Raise exception to js instead of crashing --- .../js/eina_js/eina_js_compatibility.hh | 2 +- .../js/eina_js/eina_js_get_value_from_c.hh | 63 ++++++++++++++----- 2 files changed, 48 insertions(+), 17 deletions(-) diff --git a/src/bindings/js/eina_js/eina_js_compatibility.hh b/src/bindings/js/eina_js/eina_js_compatibility.hh index 37a9457bb9..a568995a6b 100644 --- a/src/bindings/js/eina_js/eina_js_compatibility.hh +++ b/src/bindings/js/eina_js/eina_js_compatibility.hh @@ -890,7 +890,7 @@ inline v8::Handle get_class_constructor(std::string const& class_n { auto it = constructors_map_.find(class_name); if (it == constructors_map_.end()) - throw std::runtime_error("Class not found"); + throw std::runtime_error("Class not found: " + class_name); return it->second; } diff --git a/src/bindings/js/eina_js/eina_js_get_value_from_c.hh b/src/bindings/js/eina_js/eina_js_get_value_from_c.hh index 7e6499b5b7..581c69c9e0 100644 --- a/src/bindings/js/eina_js/eina_js_get_value_from_c.hh +++ b/src/bindings/js/eina_js/eina_js_get_value_from_c.hh @@ -142,34 +142,65 @@ get_value_from_c(T object, v8::Isolate* isolate, const char* class_name inline v8::Local get_value_from_c(Eo* v, v8::Isolate* isolate, const char* class_name) { - auto ctor = ::efl::eina::js::get_class_constructor(class_name); - auto obj = new_v8_external_instance(ctor, ::efl_ref(v), isolate); - efl::eina::js::make_weak(isolate, obj, [v]{ ::efl_unref(v); }); - return obj; + try + { + auto ctor = ::efl::eina::js::get_class_constructor(class_name); + auto obj = new_v8_external_instance(ctor, ::efl_ref(v), isolate); + efl::eina::js::make_weak(isolate, obj, [v]{ ::efl_unref(v); }); + return obj; + } + catch (std::runtime_error const& error) + { + eina::js::compatibility_throw + (isolate, v8::Exception::TypeError + (eina::js::compatibility_new(isolate, error.what()))); + return v8::Null(isolate); + } } inline v8::Local get_value_from_c(const Eo* v, v8::Isolate* isolate, const char* class_name) { // TODO: implement const objects? - auto ctor = ::efl::eina::js::get_class_constructor(class_name); - auto obj = new_v8_external_instance(ctor, ::efl_ref(v), isolate); - efl::eina::js::make_weak(isolate, obj, [v]{ ::efl_unref(v); }); - return obj; + try + { + auto ctor = ::efl::eina::js::get_class_constructor(class_name); + auto obj = new_v8_external_instance(ctor, ::efl_ref(v), isolate); + efl::eina::js::make_weak(isolate, obj, [v]{ ::efl_unref(v); }); + return obj; + } + catch (std::runtime_error const& error) + { + eina::js::compatibility_throw + (isolate, v8::Exception::TypeError + (eina::js::compatibility_new(isolate, error.what()))); + return v8::Null(isolate); + } } template inline v8::Local get_value_from_c(struct_ptr_tag v, v8::Isolate* isolate, const char* class_name) { - using nonconst_type = typename std::remove_const::type; - bool own = false; // TODO: handle ownership - auto ptr = const_cast(v.value); - auto ctor = ::efl::eina::js::get_class_constructor(class_name); - auto obj = new_v8_external_instance(ctor, ptr, isolate); - if (own) - efl::eina::js::make_weak(isolate, obj, [ptr]{ free(ptr); }); - return obj; + try + { + using nonconst_type = typename std::remove_const::type; + bool own = false; // TODO: handle ownership + auto ptr = const_cast(v.value); + auto ctor = ::efl::eina::js::get_class_constructor(class_name); + auto obj = new_v8_external_instance(ctor, ptr, isolate); + if (own) + efl::eina::js::make_weak(isolate, obj, [ptr]{ free(ptr); }); + return obj; + } + catch (std::runtime_error const& error) + { + eina::js::compatibility_throw + (isolate, v8::Exception::TypeError + (eina::js::compatibility_new(isolate, error.what()))); + return v8::Null(isolate); + } + } template