1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-26 04:46:37 +02:00

javascript: catch and display exceptions when running scripts/functions

This commit is contained in:
Sébastien Helleu
2015-03-07 09:22:47 +01:00
parent 145191dbc5
commit dfb2c79cbb
2 changed files with 73 additions and 28 deletions
+33 -2
View File
@@ -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;
}
/*
+40 -26
View File
@@ -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;
}