From 1b8cff6defc5bfaffe03a527aa6a966de19f8aa3 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 18 Aug 2013 20:11:16 +0100 Subject: [PATCH] Got basic hook working in ruby --- plugins/RubyTest.rb | 5 ++++ plugins/ruby-test.rb | 15 ----------- src/plugins/c_plugins.c | 2 +- src/plugins/plugins.c | 2 +- src/plugins/plugins.h | 5 ++-- src/plugins/python_plugins.c | 2 +- src/plugins/ruby_plugins.c | 50 ++++++++++++++---------------------- 7 files changed, 30 insertions(+), 51 deletions(-) create mode 100644 plugins/RubyTest.rb delete mode 100644 plugins/ruby-test.rb diff --git a/plugins/RubyTest.rb b/plugins/RubyTest.rb new file mode 100644 index 00000000..bd2dba97 --- /dev/null +++ b/plugins/RubyTest.rb @@ -0,0 +1,5 @@ +module RubyTest + def RubyTest.prof_init(version, status) + $stderr.puts "From Ruby: " + version + " " + status + end +end diff --git a/plugins/ruby-test.rb b/plugins/ruby-test.rb deleted file mode 100644 index 4f264e64..00000000 --- a/plugins/ruby-test.rb +++ /dev/null @@ -1,15 +0,0 @@ -module RubyTest - include prof - - prof_version = "" - prof_status = "" - - def prof_init(version, status) - prof_version = version - prof_status = status - end - - def prof_on_start() - prof.cons_show("RubyTest: " + prof_version + " " + prof_status) - end -end diff --git a/src/plugins/c_plugins.c b/src/plugins/c_plugins.c index b4bc4c53..ee42a951 100644 --- a/src/plugins/c_plugins.c +++ b/src/plugins/c_plugins.c @@ -33,7 +33,7 @@ c_plugin_create(const char * const filename) plugin = malloc(sizeof(ProfPlugin)); plugin->name = g_strdup(filename); - plugin->lang = C; + plugin->lang = LANG_C; plugin->module = handle; plugin->init_func = c_init_hook; plugin->on_start_func = c_on_start_hook; diff --git a/src/plugins/plugins.c b/src/plugins/plugins.c index 2c3ee5b4..99c5c322 100644 --- a/src/plugins/plugins.c +++ b/src/plugins/plugins.c @@ -133,7 +133,7 @@ plugins_shutdown(void) while (curr != NULL) { ProfPlugin *plugin = curr->data; - if (plugin->lang == C) + if (plugin->lang == LANG_C) c_close_library (plugin); curr = g_slist_next(curr); diff --git a/src/plugins/plugins.h b/src/plugins/plugins.h index e6ffdbc1..566a56cf 100644 --- a/src/plugins/plugins.h +++ b/src/plugins/plugins.h @@ -24,8 +24,9 @@ #define PLUGINS_H typedef enum { - PYTHON, - C + LANG_PYTHON, + LANG_RUBY, + LANG_C } lang_t; typedef struct prof_plugin_t { diff --git a/src/plugins/python_plugins.c b/src/plugins/python_plugins.c index be0ddf0c..abdc0904 100644 --- a/src/plugins/python_plugins.c +++ b/src/plugins/python_plugins.c @@ -54,7 +54,7 @@ python_plugin_create(const char * const filename) if (p_module != NULL) { ProfPlugin *plugin = malloc(sizeof(ProfPlugin)); plugin->name = module_name; - plugin->lang = PYTHON; + plugin->lang = LANG_PYTHON; plugin->module = p_module; plugin->init_func = python_init_hook; plugin->on_start_func = python_on_start_hook; diff --git a/src/plugins/ruby_plugins.c b/src/plugins/ruby_plugins.c index 721c2ed5..19857501 100644 --- a/src/plugins/ruby_plugins.c +++ b/src/plugins/ruby_plugins.c @@ -35,7 +35,8 @@ void ruby_env_init(void) { ruby_init(); - ruby_check_error(); + ruby_script("prof"); + ruby_init_loadpath(); ruby_api_init(); ruby_check_error(); // TODO set loadpath for ruby interpreter @@ -49,44 +50,31 @@ ruby_env_init(void) ProfPlugin * ruby_plugin_create(const char * const filename) { + GString *path = g_string_new("./plugins/"); + g_string_append(path, filename); + rb_require(path->str); gchar *module_name = g_strndup(filename, strlen(filename) - 3); - PyObject *p_module = PyImport_ImportModule(module_name); ruby_check_error(); - if (p_module != NULL) { - ProfPlugin *plugin = malloc(sizeof(ProfPlugin)); - plugin->name = module_name; - plugin->lang = PYTHON; - plugin->module = p_module; - plugin->init_func = ruby_init_hook; - plugin->on_start_func = ruby_on_start_hook; - plugin->on_connect_func = ruby_on_connect_hook; - plugin->on_message_received_func = ruby_on_message_received_hook; - g_free(module_name); - return plugin; - } else { - g_free(module_name); - return NULL; - } + + ProfPlugin *plugin = malloc(sizeof(ProfPlugin)); + plugin->name = module_name; + plugin->lang = LANG_RUBY; + plugin->module = NULL; + plugin->init_func = ruby_init_hook; + plugin->on_start_func = ruby_on_start_hook; + plugin->on_connect_func = ruby_on_connect_hook; + plugin->on_message_received_func = ruby_on_message_received_hook; + return plugin; } void ruby_init_hook(ProfPlugin *plugin, const char * const version, const char * const status) { -/* TODO - PyObject *p_args = Py_BuildValue("ss", version, status); - PyObject *p_function; + VALUE v_version = rb_str_new2(version); + VALUE v_status = rb_str_new2(status); - PyObject *p_module = plugin->module; - if (PyObject_HasAttrString(p_module, "prof_init")) { - p_function = PyObject_GetAttrString(p_module, "prof_init"); - ruby_check_error(); - if (p_function && PyCallable_Check(p_function)) { - PyObject_CallObject(p_function, p_args); - ruby_check_error(); - Py_XDECREF(p_function); - } - } -*/ + VALUE module = rb_const_get(rb_cObject, rb_intern(plugin->name)); + rb_funcall(module, rb_intern("prof_init"), 2, v_version, v_status); } void