Skip to content

Commit

Permalink
remove old way for ruby module definition as it breaks calling module…
Browse files Browse the repository at this point in the history
…s likes String and fix UI module collision
  • Loading branch information
jreidinger committed May 30, 2013
1 parent fbcdbc6 commit d504723
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 79 deletions.
23 changes: 9 additions & 14 deletions src/binary/YRuby.cc
Expand Up @@ -166,22 +166,17 @@ YCPValue YRuby::callInner (string module_name, string function,
YCPList argList, constTypePtr wanted_result_type)
{
//RUBY_INIT_STACK // bnc#708059
VALUE module = y2ruby_nested_const_get(module_name);
string full_name = string("YCP::")+module_name;
VALUE module = y2ruby_nested_const_get(full_name);
if (module == Qnil)
{
y2debug ("The Ruby module '%s' is not provided by its rb file. Try YCP prefix.", module_name.c_str());
string alternative_name = string("YCP::")+module_name;
module = y2ruby_nested_const_get(alternative_name);
if (module == Qnil)
{
y2error ("The Ruby module '%s' is not loaded.", alternative_name.c_str());
VALUE exception = rb_gv_get("$!"); /* get last exception */
VALUE reason = rb_funcall(exception, rb_intern("message"), 0 );
VALUE trace = rb_gv_get("$@"); /* get last exception trace */
VALUE backtrace = RARRAY_LEN(trace)>0 ? rb_ary_entry(trace, 0) : rb_str_new2("Unknown");
y2error("%s load failed:%s at %s", alternative_name.c_str(), StringValuePtr(reason),StringValuePtr(backtrace));
return YCPVoid();
}
y2error ("The Ruby module '%s' is not loaded.", full_name.c_str());
VALUE exception = rb_gv_get("$!"); /* get last exception */
VALUE reason = rb_funcall(exception, rb_intern("message"), 0 );
VALUE trace = rb_gv_get("$@"); /* get last exception trace */
VALUE backtrace = RARRAY_LEN(trace)>0 ? rb_ary_entry(trace, 0) : rb_str_new2("Unknown");
y2error("%s load failed:%s at %s", full_name.c_str(), StringValuePtr(reason), StringValuePtr(backtrace));
return YCPVoid();
}

int size = argList.size();
Expand Down
61 changes: 4 additions & 57 deletions src/binary/YRubyNamespace.cc
Expand Up @@ -166,15 +166,15 @@ void YRubyNamespace::constructSymbolTable(VALUE module)
{
int offset = 0; //track number of added method, so we can add extra one at the end
VALUE module_class = rb_obj_class(module);
//detect if module use new approach for exporting methods or old one
if (rb_respond_to(module_class, rb_intern("published_functions" )))
{
offset = addMethodsNewWay(module_class);
offset = addMethods(module_class);
offset = addVariables(module_class, offset);
}
else
{
offset = addMethodsOldWay(module);
y2error("Module '%s' doesn't export anything. DEPRECATED old way", m_name.c_str());
return;
}
addExceptionMethod(module,offset);
y2debug("%s", symbolsToString().c_str());
Expand Down Expand Up @@ -243,17 +243,10 @@ VALUE YRubyNamespace::getRubyModule()
{
ruby_module_name = string("YCP::") + m_name;
VALUE module = y2ruby_nested_const_get(ruby_module_name);
if (module == Qnil)
{
y2warning ("The Ruby module '%s' is not provided by its rb file. Trying it without YCP prefix.", ruby_module_name.c_str());
//old modules lives outside of YCP namespace
ruby_module_name = m_name;
module = y2ruby_nested_const_get(ruby_module_name);
}
return module;
}

int YRubyNamespace::addMethodsNewWay(VALUE module)
int YRubyNamespace::addMethods(VALUE module)
{
VALUE methods = rb_funcall(module, rb_intern("published_functions"),0);
methods = rb_funcall(methods,rb_intern("values"),0);
Expand Down Expand Up @@ -302,52 +295,6 @@ int YRubyNamespace::addVariables(VALUE module, int offset)
return offset+j;
}

int YRubyNamespace::addMethodsOldWay(VALUE module)
{
// we will perform operator- to determine the module methods
VALUE moduleklassmethods = rb_funcall( rb_cModule, rb_intern("methods"), 0);
VALUE mymodulemethods = rb_funcall( module, rb_intern("methods"), 0);
VALUE methods = rb_funcall( mymodulemethods, rb_intern("-"), 1, moduleklassmethods );

if (methods == Qnil)
{
y2internal ("Can't see methods in module '%s'", ruby_module_name.c_str());
return 0;
}

int i;
for(i = 0; i < RARRAY_LEN(methods); i++)
{
VALUE current = rb_funcall( methods, rb_intern("at"), 1, rb_fix_new(i) );
if (rb_type(current) == RUBY_T_SYMBOL) {
current = rb_funcall( current, rb_intern("to_s"), 0);
}
y2milestone("New method: '%s'", RSTRING_PTR(current));

// figure out arity.
Check_Type(module,T_MODULE);
VALUE methodobj = rb_funcall( module, rb_intern("method"), 1, current );
if ( methodobj == Qnil )
{
y2error ("Cannot access method object '%s'", RSTRING_PTR(current));
continue;
}
string signature = "any( ";
VALUE rbarity = rb_funcall( methodobj, rb_intern("arity"), 0);
int arity = NUM2INT(rbarity);
for ( int k=0; k < arity; ++k )
{
signature += "any";
if ( k < (arity - 1) )
signature += ",";
}
signature += ")";

addMethod(RSTRING_PTR(current), signature, i);
}
return i;
}

int YRubyNamespace::addExceptionMethod(VALUE module, int offset)
{
addMethod("last_exception", "string()", offset);
Expand Down
3 changes: 1 addition & 2 deletions src/binary/YRubyNamespace.h
Expand Up @@ -34,8 +34,7 @@ class YRubyNamespace : public Y2Namespace
string ruby_module_name;
VALUE getRubyModule(); //sets ruby_module name as sideeffect, so we know what is real name in ruby
void constructSymbolTable(VALUE module);
int addMethodsNewWay(VALUE module);
int addMethodsOldWay(VALUE module);
int addMethods(VALUE module);
int addVariables(VALUE module,int offset);
int addExceptionMethod(VALUE module, int offset);
void addMethod(const char *name, const string &signature, int offset);
Expand Down
2 changes: 1 addition & 1 deletion src/ruby/ycp.rb
Expand Up @@ -40,5 +40,5 @@
require "ycp/path"
require "ycp/scr"
require "ycp/term"
require "ycp/ui"
require "ycp/ui_shortcuts"
require "ycp/wfm"
4 changes: 2 additions & 2 deletions src/ruby/ycp/client.rb
@@ -1,13 +1,13 @@
require "ycp/ycp"
require "ycp/i18n"
require "ycp/exportable"
require "ycp/ui"
require "ycp/ui_shortcuts"

module YCP
class Client
include I18n
extend Exportable
include YCP
include UI
include UIShortcuts
end
end
4 changes: 2 additions & 2 deletions src/ruby/ycp/module.rb
@@ -1,13 +1,13 @@
require "ycp/ycp"
require "ycp/i18n"
require "ycp/exportable"
require "ycp/ui"
require "ycp/ui_shortcuts"

module YCP
class Module
include I18n
extend Exportable
include YCP
include UI
include UIShortcuts
end
end
2 changes: 1 addition & 1 deletion src/ruby/ycp/ui.rb → src/ruby/ycp/ui_shortcuts.rb
@@ -1,7 +1,7 @@
require "ycp/term"

module YCP
module UI
module UIShortcuts

# Define symbols for the UI
UI_TERMS = [ :BarGraph, :Bottom, :CheckBox, :ColoredLabel, :ComboBox, :Date,
Expand Down

0 comments on commit d504723

Please sign in to comment.