diff --git a/src/binary/Y2RubyReference.h b/src/binary/Y2RubyReference.h index bc3ddbe0..cae7afda 100644 --- a/src/binary/Y2RubyReference.h +++ b/src/binary/Y2RubyReference.h @@ -15,7 +15,12 @@ class ClientFunction : public Y2Function YCPList m_call; public: ClientFunction(VALUE m_object) : object(m_object) - {} + { + } + + ~ClientFunction() + { + } YCPValue evaluateCall (); @@ -69,14 +74,22 @@ class ClientFunction : public Y2Function class ClientNamespace : public Y2Namespace { private: - ClientFunction f; + VALUE object; public: - ClientNamespace(VALUE m_object) : f(m_object) - {} + ClientNamespace(VALUE m_object) : object(m_object) + { + // register object, so noone will destroy it under out hands + rb_gc_register_address(&object); + } + + ~ClientNamespace() + { + rb_gc_unregister_address(&object); + } Y2Function* createFunctionCall(const string name, constFunctionTypePtr type) { - return &f; + return new ClientFunction(object); } virtual const string filename() const diff --git a/src/binary/Y2RubyTypeConv.cc b/src/binary/Y2RubyTypeConv.cc index 7784d3f0..e06e2e38 100644 --- a/src/binary/Y2RubyTypeConv.cc +++ b/src/binary/Y2RubyTypeConv.cc @@ -130,7 +130,7 @@ static YCPValue rbreference_2_ycpreference( VALUE value ) { VALUE signature = rb_funcall(value,rb_intern("signature"),0); constTypePtr sym_tp = Type::fromSignature(RSTRING_PTR(signature)); -//FIXME memory leak +//FIXME memory leak , probably link it with reference, but then copy is broken :( const Y2Namespace *ns = new ClientNamespace(value); SymbolEntry *s_entry = new SymbolEntry(ns, 0, "ruby_reference", SymbolEntry::c_function, sym_tp); return YCPReference(s_entry); diff --git a/src/ruby/ycp/builtins.rb b/src/ruby/ycp/builtins.rb index 7c661996..6a9ab8ba 100644 --- a/src/ruby/ycp/builtins.rb +++ b/src/ruby/ycp/builtins.rb @@ -539,7 +539,7 @@ def self.sformat format, *args # Sleeps a number of milliseconds. def self.sleep milisecs # ruby sleep() accepts seconds (float) - sleep milisecs / 1000.0 + Kernel.sleep milisecs / 1000.0 end # time() YCP built-in @@ -672,8 +672,10 @@ def self.dpgettext end # Filters characters out of a ::String - def self.filterchars - raise "Builtin filterchars() is not implemented yet" + def self.filterchars string, chars + return nil if string.nil? || chars.nil? + + return string.gsub(/[^#{Regexp.escape chars}]/, "") end # Searches string for the first non matching chars diff --git a/src/ruby/ycp/convert.rb b/src/ruby/ycp/convert.rb index 9994987d..156c76c2 100644 --- a/src/ruby/ycp/convert.rb +++ b/src/ruby/ycp/convert.rb @@ -31,6 +31,10 @@ def self.convert(object, options) return nil unless object.is_a? Float YCP.y2warning "Conversion from integer to float lead to loose precision." return object.to_i + elsif to == "locale" && from == "string" + return object + elsif to == "string" && from == "locale" + return object else YCP.y2warning "Cannot convert #{object.class} from '#{from}' to '#{to}'" return nil diff --git a/src/ruby/ycp/ops.rb b/src/ruby/ycp/ops.rb index fa8aeedd..4cb6552e 100644 --- a/src/ruby/ycp/ops.rb +++ b/src/ruby/ycp/ops.rb @@ -18,27 +18,28 @@ module Ops 'list' => ::Array, 'map' => ::Hash, 'term' => YCP::Term, - 'path' => YCP::Path + 'path' => YCP::Path, + 'locale' => ::String } - def self.index (object, indexes, default) - res = object - indexes.each do |i| - case res - when ::Array, YCP::Term - if i.is_a? Fixnum - if (0..res.size-1).include? i - res = res[i] + def self.index (object, indexes, default) + res = object + indexes.each do |i| + case res + when ::Array, YCP::Term + if i.is_a? Fixnum + if (0..res.size-1).include? i + res = res[i] + else + YCP.y2warning "Index #{i} is out of array size" + return default + end else - YCP.y2warning "Index #{i} is out of array size" + YCP.y2warning "Passed #{i.inspect} as index key for array." return default end - else - YCP.y2warning "Passed #{i.inspect} as index key for array." - return default - end - when ::Hash - if res.has_key? i + when ::Hash + if res.has_key? i res = res[i] else return default diff --git a/src/ruby/ycp/reference.rb b/src/ruby/ycp/reference.rb index be6c7e01..1ea0a631 100644 --- a/src/ruby/ycp/reference.rb +++ b/src/ruby/ycp/reference.rb @@ -4,6 +4,8 @@ class Reference def initialize met, signature @remote_method = met + raise "invalid argument #{met.inspect}" unless met.respond_to? :call + # expand signature to containt full spec of lists and maps signature = signature.gsub(/map(\s*([^<\s]|$))/,"map\\1") signature = signature.gsub(/list(\s*([^<\s]|$))/,"list\\1")