summaryrefslogtreecommitdiff
path: root/src/bindings
diff options
context:
space:
mode:
authorLauro Moura <lauromoura@expertisesolutions.com.br>2016-12-07 18:00:36 -0300
committerFelipe Magno de Almeida <felipe@expertisesolutions.com.br>2017-02-22 18:56:30 -0300
commit26f62542250d026269522787a61f807e5e4a1d8a (patch)
tree74c1c3b94b565b858ea69fa9070976b3363a0cd0 /src/bindings
parentadd91ee80e0ba7814ea266a46d5b2a59ac15e6d1 (diff)
js: Raise exception to js instead of crashing
Diffstat (limited to 'src/bindings')
-rw-r--r--src/bindings/js/eina_js/eina_js_compatibility.hh2
-rw-r--r--src/bindings/js/eina_js/eina_js_get_value_from_c.hh63
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 37a9457..a568995 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<v8::Function> get_class_constructor(std::string const& class_n
890{ 890{
891 auto it = constructors_map_.find(class_name); 891 auto it = constructors_map_.find(class_name);
892 if (it == constructors_map_.end()) 892 if (it == constructors_map_.end())
893 throw std::runtime_error("Class not found"); 893 throw std::runtime_error("Class not found: " + class_name);
894 return it->second; 894 return it->second;
895} 895}
896 896
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 7e6499b..581c69c 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
142inline v8::Local<v8::Value> 142inline v8::Local<v8::Value>
143get_value_from_c(Eo* v, v8::Isolate* isolate, const char* class_name) 143get_value_from_c(Eo* v, v8::Isolate* isolate, const char* class_name)
144{ 144{
145 auto ctor = ::efl::eina::js::get_class_constructor(class_name); 145 try
146 auto obj = new_v8_external_instance(ctor, ::efl_ref(v), isolate); 146 {
147 efl::eina::js::make_weak(isolate, obj, [v]{ ::efl_unref(v); }); 147 auto ctor = ::efl::eina::js::get_class_constructor(class_name);
148 return obj; 148 auto obj = new_v8_external_instance(ctor, ::efl_ref(v), isolate);
149 efl::eina::js::make_weak(isolate, obj, [v]{ ::efl_unref(v); });
150 return obj;
151 }
152 catch (std::runtime_error const& error)
153 {
154 eina::js::compatibility_throw
155 (isolate, v8::Exception::TypeError
156 (eina::js::compatibility_new<v8::String>(isolate, error.what())));
157 return v8::Null(isolate);
158 }
149} 159}
150 160
151inline v8::Local<v8::Value> 161inline v8::Local<v8::Value>
152get_value_from_c(const Eo* v, v8::Isolate* isolate, const char* class_name) 162get_value_from_c(const Eo* v, v8::Isolate* isolate, const char* class_name)
153{ 163{
154 // TODO: implement const objects? 164 // TODO: implement const objects?
155 auto ctor = ::efl::eina::js::get_class_constructor(class_name); 165 try
156 auto obj = new_v8_external_instance(ctor, ::efl_ref(v), isolate); 166 {
157 efl::eina::js::make_weak(isolate, obj, [v]{ ::efl_unref(v); }); 167 auto ctor = ::efl::eina::js::get_class_constructor(class_name);
158 return obj; 168 auto obj = new_v8_external_instance(ctor, ::efl_ref(v), isolate);
169 efl::eina::js::make_weak(isolate, obj, [v]{ ::efl_unref(v); });
170 return obj;
171 }
172 catch (std::runtime_error const& error)
173 {
174 eina::js::compatibility_throw
175 (isolate, v8::Exception::TypeError
176 (eina::js::compatibility_new<v8::String>(isolate, error.what())));
177 return v8::Null(isolate);
178 }
159} 179}
160 180
161template <typename T> 181template <typename T>
162inline v8::Local<v8::Value> 182inline v8::Local<v8::Value>
163get_value_from_c(struct_ptr_tag<T*> v, v8::Isolate* isolate, const char* class_name) 183get_value_from_c(struct_ptr_tag<T*> v, v8::Isolate* isolate, const char* class_name)
164{ 184{
165 using nonconst_type = typename std::remove_const<T>::type; 185 try
166 bool own = false; // TODO: handle ownership 186 {
167 auto ptr = const_cast<nonconst_type*>(v.value); 187 using nonconst_type = typename std::remove_const<T>::type;
168 auto ctor = ::efl::eina::js::get_class_constructor(class_name); 188 bool own = false; // TODO: handle ownership
169 auto obj = new_v8_external_instance(ctor, ptr, isolate); 189 auto ptr = const_cast<nonconst_type*>(v.value);
170 if (own) 190 auto ctor = ::efl::eina::js::get_class_constructor(class_name);
171 efl::eina::js::make_weak(isolate, obj, [ptr]{ free(ptr); }); 191 auto obj = new_v8_external_instance(ctor, ptr, isolate);
172 return obj; 192 if (own)
193 efl::eina::js::make_weak(isolate, obj, [ptr]{ free(ptr); });
194 return obj;
195 }
196 catch (std::runtime_error const& error)
197 {
198 eina::js::compatibility_throw
199 (isolate, v8::Exception::TypeError
200 (eina::js::compatibility_new<v8::String>(isolate, error.what())));
201 return v8::Null(isolate);
202 }
203
173} 204}
174 205
175template <typename T> 206template <typename T>