diff options
author | Sébastien Helleu <flashcode@flashtux.org> | 2015-03-07 09:22:47 +0100 |
---|---|---|
committer | Sébastien Helleu <flashcode@flashtux.org> | 2015-03-07 16:52:30 +0100 |
commit | dfb2c79cbb0475c74f13b7ca9d42188957675d4e (patch) | |
tree | 7ee124276b6e41642616065e21b479878f69bc20 /src | |
parent | 145191dbc5dfd1d679489196cd759f31067433c2 (diff) | |
download | weechat-dfb2c79cbb0475c74f13b7ca9d42188957675d4e.zip |
javascript: catch and display exceptions when running scripts/functions
Diffstat (limited to 'src')
-rw-r--r-- | src/plugins/javascript/weechat-js-v8.cpp | 35 | ||||
-rw-r--r-- | src/plugins/javascript/weechat-js.cpp | 66 |
2 files changed, 73 insertions, 28 deletions
diff --git a/src/plugins/javascript/weechat-js-v8.cpp b/src/plugins/javascript/weechat-js-v8.cpp index caeaafee8..6966af13e 100644 --- a/src/plugins/javascript/weechat-js-v8.cpp +++ b/src/plugins/javascript/weechat-js-v8.cpp @@ -32,6 +32,14 @@ extern "C" #include "weechat-js.h" #include "weechat-js-v8.h" +#define PRINT_EXCEPTION \ + Local<Value> exception = trycatch.Exception(); \ + String::Utf8Value str_exception(exception); \ + weechat_printf (NULL, \ + weechat_gettext ("%s%s: exception: %s"), \ + weechat_prefix ("error"), JS_PLUGIN_NAME, \ + *str_exception); + using namespace v8; @@ -84,11 +92,26 @@ WeechatJsV8::load(const char *source) bool WeechatJsV8::execScript() { + v8::TryCatch trycatch; + this->context = Context::New(NULL, this->global); Context::Scope context_scope(this->context); Handle<Script> script = Script::Compile(this->source); - script->Run(); + if (script.IsEmpty()) + { + PRINT_EXCEPTION; + return false; + } + else + { + Local<Value> value = script->Run(); + if (value.IsEmpty()) + { + PRINT_EXCEPTION; + return false; + } + } return true; } @@ -114,12 +137,20 @@ WeechatJsV8::functionExists(const char *function) Handle<Value> WeechatJsV8::execFunction(const char *function, int argc, Handle<Value> *argv) { + v8::TryCatch trycatch; + Context::Scope context_scope(this->context); Handle<Object> global = this->context->Global(); Handle<Value> value = global->Get(String::New(function)); Handle<Function> func = Handle<Function>::Cast(value); - return func->Call(global, argc, argv); + + Handle<Value> res = func->Call(global, argc, argv); + if (res.IsEmpty()) + { + PRINT_EXCEPTION; + } + return res; } /* diff --git a/src/plugins/javascript/weechat-js.cpp b/src/plugins/javascript/weechat-js.cpp index 30521b58e..ff3618d93 100644 --- a/src/plugins/javascript/weechat-js.cpp +++ b/src/plugins/javascript/weechat-js.cpp @@ -210,33 +210,37 @@ weechat_js_exec (struct t_plugin_script *script, argc, (argc > 0) ? argv2 : NULL); - if ((ret_type == WEECHAT_SCRIPT_EXEC_STRING) && (ret_js->IsString())) + if (!ret_js.IsEmpty()) { - String::Utf8Value temp_str(ret_js); - ret_value = *temp_str; - } - else if ((ret_type == WEECHAT_SCRIPT_EXEC_INT) && (ret_js->IsInt32())) - { - ret_int = (int *)malloc (sizeof (*ret_int)); - if (ret_int) - *ret_int = (int)(ret_js->IntegerValue()); - ret_value = ret_int; - } - else if ((ret_type == WEECHAT_SCRIPT_EXEC_HASHTABLE) - && (ret_js->IsObject())) - { - ret_value = (struct t_hashtable *)weechat_js_object_to_hashtable ( - ret_js->ToObject(), - WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE, - WEECHAT_HASHTABLE_STRING, - WEECHAT_HASHTABLE_STRING); - } - else - { - weechat_printf (NULL, - weechat_gettext ("%s%s: function \"%s\" must return " - "a valid value"), - weechat_prefix ("error"), JS_PLUGIN_NAME, function); + if ((ret_type == WEECHAT_SCRIPT_EXEC_STRING) && (ret_js->IsString())) + { + String::Utf8Value temp_str(ret_js); + ret_value = *temp_str; + } + else if ((ret_type == WEECHAT_SCRIPT_EXEC_INT) && (ret_js->IsInt32())) + { + ret_int = (int *)malloc (sizeof (*ret_int)); + if (ret_int) + *ret_int = (int)(ret_js->IntegerValue()); + ret_value = ret_int; + } + else if ((ret_type == WEECHAT_SCRIPT_EXEC_HASHTABLE) + && (ret_js->IsObject())) + { + ret_value = (struct t_hashtable *)weechat_js_object_to_hashtable ( + ret_js->ToObject(), + WEECHAT_SCRIPT_HASHTABLE_DEFAULT_SIZE, + WEECHAT_HASHTABLE_STRING, + WEECHAT_HASHTABLE_STRING); + } + else + { + weechat_printf (NULL, + weechat_gettext ("%s%s: function \"%s\" must " + "return a valid value"), + weechat_prefix ("error"), JS_PLUGIN_NAME, + function); + } } if (!ret_value) @@ -315,6 +319,7 @@ weechat_js_load (const char *filename) plugin_script_remove (weechat_js_plugin, &js_scripts, &last_js_script, js_current_script); + js_current_script = NULL; } return 0; @@ -329,6 +334,15 @@ weechat_js_load (const char *filename) "\"%s\""), weechat_prefix ("error"), JS_PLUGIN_NAME, filename); delete js_current_interpreter; + + /* if script was registered, remove it from list */ + if (js_current_script) + { + plugin_script_remove (weechat_js_plugin, + &js_scripts, &last_js_script, + js_current_script); + js_current_script = NULL; + } return 0; } |