aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Richard Brown <rbrown@exherbo.org> 2006-11-12 22:31:00 +0000
committerAvatar Richard Brown <rbrown@exherbo.org> 2006-11-12 22:31:00 +0000
commita2f87fc332f4aee3ec13f7f8d0e413c1c57d00b1 (patch)
treea55c77456541bab86ac64a40f71a417bf799a93b
parentfca1d9a995915d620495e1c6953a44b34e700ece (diff)
downloadpaludis-a2f87fc332f4aee3ec13f7f8d0e413c1c57d00b1.tar.gz
paludis-a2f87fc332f4aee3ec13f7f8d0e413c1c57d00b1.tar.xz
Type check all Data_Get_Structs except for self.
-rw-r--r--ruby/environment.cc58
-rw-r--r--ruby/package_database_entry.cc16
-rw-r--r--ruby/paludis_ruby.cc10
-rw-r--r--ruby/paludis_ruby.hh2
-rw-r--r--ruby/paludis_ruby_TEST.rb18
5 files changed, 72 insertions, 32 deletions
diff --git a/ruby/environment.cc b/ruby/environment.cc
index e0ced2d..7d5a43a 100644
--- a/ruby/environment.cc
+++ b/ruby/environment.cc
@@ -63,13 +63,12 @@ namespace
try
{
- if (1 == argc || 2 == argc)
+ if (1 == argc)
+ return env_data->env_ptr->query_use(UseFlagName(StringValuePtr(argv[0])), 0) ? Qtrue : Qfalse;
+ else if (2 == argc)
{
- PackageDatabaseEntry * pde_ptr(0);
- if (2 == argc)
- Data_Get_Struct(argv[1], PackageDatabaseEntry, pde_ptr);
-
- return env_data->env_ptr->query_use(UseFlagName(StringValuePtr(argv[0])), pde_ptr) ? Qtrue : Qfalse;
+ PackageDatabaseEntry pde = value_to_package_database_entry(argv[1]);
+ return env_data->env_ptr->query_use(UseFlagName(StringValuePtr(argv[0])), &pde) ? Qtrue : Qfalse;
}
else
rb_raise(rb_eArgError, "Environment.query_use expects one or two arguments, but got %d", argc);
@@ -88,14 +87,12 @@ namespace
try
{
- if (1 == argc || 2 == argc)
+ if (1 == argc)
+ return env_data->env_ptr->accept_keyword(KeywordName(StringValuePtr(argv[0])), 0) ? Qtrue : Qfalse;
+ else if (2 == argc)
{
- PackageDatabaseEntry * pde_ptr(0);
- if (2 == argc)
- Data_Get_Struct(argv[1], PackageDatabaseEntry, pde_ptr);
-
- return env_data->env_ptr->accept_keyword(KeywordName(
- StringValuePtr(argv[0])), pde_ptr) ? Qtrue : Qfalse;
+ PackageDatabaseEntry pde = value_to_package_database_entry(argv[1]);
+ return env_data->env_ptr->accept_keyword(KeywordName(StringValuePtr(argv[0])), &pde) ? Qtrue : Qfalse;
}
else
rb_raise(rb_eArgError, "Environment.accept_keyword expects one or two arguments, but got %d", argc);
@@ -114,14 +111,12 @@ namespace
try
{
- if (1 == argc || 2 == argc)
+ if (1 == argc)
+ return env_data->env_ptr->accept_license(std::string(StringValuePtr(argv[0])), 0) ? Qtrue : Qfalse;
+ else if (2 == argc)
{
- PackageDatabaseEntry * pde_ptr(0);
- if (2 == argc)
- Data_Get_Struct(argv[1], PackageDatabaseEntry, pde_ptr);
-
- return env_data->env_ptr->accept_license(
- std::string(StringValuePtr(argv[0])), pde_ptr) ? Qtrue : Qfalse;
+ PackageDatabaseEntry pde = value_to_package_database_entry(argv[1]);
+ return env_data->env_ptr->accept_license(std::string(StringValuePtr(argv[0])), &pde) ? Qtrue : Qfalse;
}
else
rb_raise(rb_eArgError, "Environment.accept_license expects one or two arguments, but got %d", argc);
@@ -133,16 +128,15 @@ namespace
}
VALUE
- environment_mask_reasons(VALUE self, VALUE pde)
+ environment_mask_reasons(VALUE self, VALUE pde_value)
{
EnvironmentData * env_data;
Data_Get_Struct(self, EnvironmentData, env_data);
- PackageDatabaseEntry * pde_ptr(0);
- Data_Get_Struct(pde, PackageDatabaseEntry, pde_ptr);
+ PackageDatabaseEntry pde = value_to_package_database_entry(pde_value);
try
{
- MaskReasons r(env_data->env_ptr->mask_reasons(*pde_ptr));
+ MaskReasons r(env_data->env_ptr->mask_reasons(pde));
return mask_reasons_to_value(r);
}
catch (const std::exception & e)
@@ -291,6 +285,22 @@ namespace
}
}
+Environment *
+paludis::ruby::value_to_environment(VALUE v)
+{
+ if (rb_obj_is_kind_of(v, c_environment))
+ {
+ Environment * v_ptr;
+ Data_Get_Struct(v, Environment, v_ptr);
+ return v_ptr;
+ }
+ else
+ {
+ std::string message = "TypeError: can't convert " + std::string(rb_obj_classname(v)) + " into Environment";
+ rb_raise(rb_eTypeError, message.c_str());
+ }
+}
+
RegisterRubyClass::Register paludis_ruby_register_environment PALUDIS_ATTRIBUTE((used))
(&do_register_environment);
diff --git a/ruby/package_database_entry.cc b/ruby/package_database_entry.cc
index 8f0e805..82748e6 100644
--- a/ruby/package_database_entry.cc
+++ b/ruby/package_database_entry.cc
@@ -166,6 +166,22 @@ namespace
}
}
+PackageDatabaseEntry
+paludis::ruby::value_to_package_database_entry(VALUE v)
+{
+ if (rb_obj_is_kind_of(v, c_package_database_entry))
+ {
+ PackageDatabaseEntry * v_ptr;
+ Data_Get_Struct(v, PackageDatabaseEntry, v_ptr);
+ return *v_ptr;
+ }
+ else
+ {
+ std::string message = "TypeError: can't convert " + std::string(rb_obj_classname(v)) + " into PackageDatabaseEntry";
+ rb_raise(rb_eTypeError, message.c_str());
+ }
+}
+
VALUE
paludis::ruby::package_database_entry_to_value(const PackageDatabaseEntry & d)
{
diff --git a/ruby/paludis_ruby.cc b/ruby/paludis_ruby.cc
index 285cd8d..941d6da 100644
--- a/ruby/paludis_ruby.cc
+++ b/ruby/paludis_ruby.cc
@@ -60,12 +60,10 @@ namespace
{
try
{
- Environment * env;
- Data_Get_Struct(en, Environment, env);
- PackageDepAtom::ConstPointer atom(value_to_package_dep_atom(a));
- PackageDatabaseEntry * target;
- Data_Get_Struct(t, PackageDatabaseEntry, target);
- return match_package(env, atom, *target) ? Qtrue : Qfalse;
+ Environment * env = value_to_environment(en);
+ PackageDepAtom::ConstPointer atom = value_to_package_dep_atom(a);
+ PackageDatabaseEntry target = value_to_package_database_entry(t);
+ return match_package(env, atom, target) ? Qtrue : Qfalse;
}
catch (const std::exception & e)
{
diff --git a/ruby/paludis_ruby.hh b/ruby/paludis_ruby.hh
index da15c75..e64beb3 100644
--- a/ruby/paludis_ruby.hh
+++ b/ruby/paludis_ruby.hh
@@ -56,6 +56,8 @@ namespace paludis
VersionMetadata::ConstPointer value_to_version_metadata(VALUE);
PackageDepAtom::ConstPointer value_to_package_dep_atom(VALUE v);
QualifiedPackageName value_to_qualified_package_name(VALUE v);
+ PackageDatabaseEntry value_to_package_database_entry(VALUE v);
+ Environment* value_to_environment(VALUE v);
/* registration */
diff --git a/ruby/paludis_ruby_TEST.rb b/ruby/paludis_ruby_TEST.rb
index 93fb08f..9268a03 100644
--- a/ruby/paludis_ruby_TEST.rb
+++ b/ruby/paludis_ruby_TEST.rb
@@ -41,10 +41,24 @@ module Paludis
atom_good = PackageDepAtom.new('>=foo/bar-1')
atom_bad = PackageDepAtom.new('>=foo/bar-2')
pde = PackageDatabaseEntry.new('foo/bar','1','test')
-
+
assert Paludis::match_package(env, atom_good, pde)
assert !Paludis::match_package(env, atom_bad, pde)
-
+
+ end
+
+ def test_type_errors
+ env = DefaultEnvironment.instance
+ atom = PackageDepAtom.new('>=foo/bar-1')
+ pde = PackageDatabaseEntry.new('foo/bar','1','test')
+
+ assert_raise TypeError do
+ Paludis::match_package(atom,atom,pde)
+ end
+
+ assert_raise TypeError do
+ Paludis::match_package(env,atom,atom)
+ end
end
end