diff options
Diffstat (limited to 'spec/ruby/optional')
99 files changed, 4589 insertions, 6874 deletions
diff --git a/spec/ruby/optional/capi/README b/spec/ruby/optional/capi/README index 069ca3c106..efbfb09dcb 100644 --- a/spec/ruby/optional/capi/README +++ b/spec/ruby/optional/capi/README @@ -8,6 +8,9 @@ specs: optional/capi/array_spec.rb 2. Put the C file containing the C functions for array_spec.rb in optional/capi/ext/array_spec.c -3. Name the C extension class 'CApiArraySpecs'. -4. Name the C functions 'array_spec_rb_ary_new'. -5. Attach the C function to the class using the name 'rb_ary_new' +3. Add a '#define HAVE_RB_ARY_NEW 1' to rubyspec.h +4. Name the C extension class 'CApiArraySpecs'. +5. Name the C functions 'array_spec_rb_ary_new'. +6. Wrap the code in the optional/capi/ext/array_spec.c in + '#ifdef HAVE_RB_ARY_NEW' +6. Attach the C function to the class using the name 'rb_ary_new' diff --git a/spec/ruby/optional/capi/array_spec.rb b/spec/ruby/optional/capi/array_spec.rb index 9c35017e21..2fd898ad94 100644 --- a/spec/ruby/optional/capi/array_spec.rb +++ b/spec/ruby/optional/capi/array_spec.rb @@ -1,4 +1,4 @@ -require_relative 'spec_helper' +require File.expand_path('../spec_helper', __FILE__) load_extension("array") @@ -8,7 +8,7 @@ describe :rb_ary_new2, shared: true do end it "raises an ArgumentError when the given argument is negative" do - -> { @s.send(@method, -1) }.should raise_error(ArgumentError) + lambda { @s.send(@method, -1) }.should raise_error(ArgumentError) end end @@ -83,8 +83,8 @@ describe "C-API Array function" do @s.rb_ary_cat([1, 2], 3, 4).should == [1, 2, 3, 4] end - it "raises a FrozenError if the array is frozen" do - -> { @s.rb_ary_cat([].freeze, 1) }.should raise_error(FrozenError) + it "raises a RuntimeError if the array is frozen" do + lambda { @s.rb_ary_cat([].freeze, 1) }.should raise_error(RuntimeError) end end @@ -130,8 +130,8 @@ describe "C-API Array function" do @s.rb_ary_rotate([1, 2, 3, 4], -3).should == [2, 3, 4, 1] end - it "raises a FrozenError if the array is frozen" do - -> { @s.rb_ary_rotate([].freeze, 1) }.should raise_error(FrozenError) + it "raises a RuntimeError if the array is frozen" do + lambda { @s.rb_ary_rotate([].freeze, 1) }.should raise_error(RuntimeError) end end @@ -190,22 +190,6 @@ describe "C-API Array function" do end end - describe "rb_ary_sort" do - it "returns a new sorted array" do - a = [2, 1, 3] - @s.rb_ary_sort(a).should == [1, 2, 3] - a.should == [2, 1, 3] - end - end - - describe "rb_ary_sort_bang" do - it "sorts the given array" do - a = [2, 1, 3] - @s.rb_ary_sort_bang(a).should == [1, 2, 3] - a.should == [1, 2, 3] - end - end - describe "rb_ary_store" do it "overwrites the element at the given position" do a = [1, 2, 3] @@ -221,7 +205,7 @@ describe "C-API Array function" do it "raises an IndexError if the negative index is greater than the length" do a = [1, 2, 3] - -> { @s.rb_ary_store(a, -10, 5) }.should raise_error(IndexError) + lambda { @s.rb_ary_store(a, -10, 5) }.should raise_error(IndexError) end it "enlarges the array as needed" do @@ -230,9 +214,9 @@ describe "C-API Array function" do a.should == [nil, nil, 7] end - it "raises a FrozenError if the array is frozen" do + it "raises a RuntimeError if the array is frozen" do a = [1, 2, 3].freeze - -> { @s.rb_ary_store(a, 1, 5) }.should raise_error(FrozenError) + lambda { @s.rb_ary_store(a, 1, 5) }.should raise_error(RuntimeError) end end @@ -265,14 +249,6 @@ describe "C-API Array function" do @s.RARRAY_PTR_assign(a, :set) a.should == [:set, :set, :set] end - - it "allows memcpying between arrays" do - a = [1, 2, 3] - b = [0, 0, 0] - @s.RARRAY_PTR_memcpy(a, b) - b.should == [1, 2, 3] - a.should == [1, 2, 3] # check a was not modified - end end describe "RARRAY_LEN" do @@ -288,16 +264,6 @@ describe "C-API Array function" do end end - describe "RARRAY_ASET" do - # This macro does NOT do any bounds checking! - it "writes an element in the array" do - ary = [1, 2, 3] - @s.RARRAY_ASET(ary, 0, 0) - @s.RARRAY_ASET(ary, 2, 42) - ary.should == [0, 2, 42] - end - end - describe "rb_assoc_new" do it "returns an array containing the two elements" do @s.rb_assoc_new(1, 2).should == [1, 2] @@ -377,40 +343,6 @@ describe "C-API Array function" do end end - describe "rb_block_call" do - it "calls an callback function as a block passed to an method" do - s = [1,2,3,4] - s2 = @s.rb_block_call(s) - - s2.should == s - - # Make sure they're different objects - s2.equal?(s).should be_false - end - - it "calls a function with the other function available as a block" do - h = {a: 1, b: 2} - - @s.rb_block_call_each_pair(h).sort.should == [1,2] - end - - it "calls a function which can yield into the original block" do - s2 = [] - - o = Object.new - def o.each - yield 1 - yield 2 - yield 3 - yield 4 - end - - @s.rb_block_call_then_yield(o) { |x| s2 << x } - - s2.should == [1,2,3,4] - end - end - describe "rb_ary_delete" do it "removes an element from an array and returns it" do ary = [1, 2, 3, 4] diff --git a/spec/ruby/optional/capi/basic_object_spec.rb b/spec/ruby/optional/capi/basic_object_spec.rb deleted file mode 100644 index 2922a421da..0000000000 --- a/spec/ruby/optional/capi/basic_object_spec.rb +++ /dev/null @@ -1,24 +0,0 @@ -require_relative 'spec_helper' - -load_extension("basic_object") - -describe "C-API basic object" do - before :each do - @s = CApiBasicObjectSpecs.new - end - - describe "RBASIC_CLASS" do - it "returns the class of an object" do - c = Class.new - o = c.new - @s.RBASIC_CLASS(o).should == c - end - - it "returns the singleton class" do - o = Object.new - @s.RBASIC_CLASS(o).should == Object - singleton_class = o.singleton_class - @s.RBASIC_CLASS(o).should == singleton_class - end - end -end diff --git a/spec/ruby/optional/capi/bignum_spec.rb b/spec/ruby/optional/capi/bignum_spec.rb index 179f053eec..a5d5995dc0 100644 --- a/spec/ruby/optional/capi/bignum_spec.rb +++ b/spec/ruby/optional/capi/bignum_spec.rb @@ -1,4 +1,4 @@ -require_relative 'spec_helper' +require File.expand_path('../spec_helper', __FILE__) load_extension("bignum") @@ -7,23 +7,21 @@ def ensure_bignum(n) n end -full_range_longs = (fixnum_max == max_long) -max_ulong = begin - require 'rbconfig/sizeof' - RbConfig::LIMITS['ULONG_MAX'] -rescue LoadError - nil -end -# If the system doesn't offer ULONG_MAX, assume 2's complement and derive it -# from LONG_MAX. -max_ulong ||= 2 * (max_long + 1) - 1 +full_range_longs = (fixnum_max == 2**(0.size * 8 - 1) - 1) describe "CApiBignumSpecs" do before :each do @s = CApiBignumSpecs.new - @max_long = max_long - @min_long = min_long - @max_ulong = ensure_bignum(max_ulong) + + if full_range_longs + @max_long = 2**(0.size * 8 - 1) - 1 + @min_long = -@max_long - 1 + @max_ulong = ensure_bignum(2**(0.size * 8) - 1) + else + @max_long = ensure_bignum(2**(0.size * 8 - 1) - 1) + @min_long = ensure_bignum(-@max_long - 1) + @max_ulong = ensure_bignum(2**(0.size * 8) - 1) + end end describe "rb_big2long" do @@ -35,8 +33,8 @@ describe "CApiBignumSpecs" do end it "raises RangeError if passed Bignum overflow long" do - -> { @s.rb_big2long(ensure_bignum(@max_long + 1)) }.should raise_error(RangeError) - -> { @s.rb_big2long(ensure_bignum(@min_long - 1)) }.should raise_error(RangeError) + lambda { @s.rb_big2long(ensure_bignum(@max_long + 1)) }.should raise_error(RangeError) + lambda { @s.rb_big2long(ensure_bignum(@min_long - 1)) }.should raise_error(RangeError) end end @@ -49,8 +47,8 @@ describe "CApiBignumSpecs" do end it "raises RangeError if passed Bignum overflow long" do - -> { @s.rb_big2ll(ensure_bignum(@max_long << 40)) }.should raise_error(RangeError) - -> { @s.rb_big2ll(ensure_bignum(@min_long << 40)) }.should raise_error(RangeError) + lambda { @s.rb_big2ll(ensure_bignum(@max_long << 40)) }.should raise_error(RangeError) + lambda { @s.rb_big2ll(ensure_bignum(@min_long << 40)) }.should raise_error(RangeError) end end @@ -67,8 +65,8 @@ describe "CApiBignumSpecs" do end it "raises RangeError if passed Bignum overflow long" do - -> { @s.rb_big2ulong(ensure_bignum(@max_ulong + 1)) }.should raise_error(RangeError) - -> { @s.rb_big2ulong(ensure_bignum(@min_long - 1)) }.should raise_error(RangeError) + lambda { @s.rb_big2ulong(ensure_bignum(@max_ulong + 1)) }.should raise_error(RangeError) + lambda { @s.rb_big2ulong(ensure_bignum(@min_long - 1)) }.should raise_error(RangeError) end end @@ -99,16 +97,6 @@ describe "CApiBignumSpecs" do end end - describe "RBIGNUM_SIGN" do - it "returns 1 for a positive Bignum" do - @s.RBIGNUM_SIGN(bignum_value(1)).should == 1 - end - - it "returns 0 for a negative Bignum" do - @s.RBIGNUM_SIGN(-bignum_value(1)).should == 0 - end - end - describe "rb_big_cmp" do it "compares a Bignum with a Bignum" do @s.rb_big_cmp(bignum_value, bignum_value(1)).should == -1 @@ -125,7 +113,7 @@ describe "CApiBignumSpecs" do val.should == @max_ulong end - platform_is c_long_size: 64 do + platform_is wordsize: 64 do it "packs max_ulong into 2 ulongs to allow sign bit" do val = @s.rb_big_pack_length(@max_ulong) val.should == 2 @@ -189,14 +177,14 @@ describe "CApiBignumSpecs" do it "returns a Fixnum for a Fixnum input value" do val = @s.rb_dbl2big(2) - val.kind_of?(Integer).should == true + val.kind_of?(Fixnum).should == true val.should == 2 end it "returns a Fixnum for a Float input value" do val = @s.rb_dbl2big(2.5) - val.kind_of?(Integer).should == true + val.kind_of?(Fixnum).should == true val.should == 2 end @@ -204,7 +192,7 @@ describe "CApiBignumSpecs" do input = 219238102380912830988.5 # chosen by fair dice roll val = @s.rb_dbl2big(input) - val.kind_of?(Integer).should == true + val.kind_of?(Bignum).should == true # This value is based on the output of a simple C extension that uses # rb_dbl2big() to convert the above input value to a Bignum. @@ -214,13 +202,13 @@ describe "CApiBignumSpecs" do it "raises FloatDomainError for Infinity values" do inf = 1.0 / 0 - -> { @s.rb_dbl2big(inf) }.should raise_error(FloatDomainError) + lambda { @s.rb_dbl2big(inf) }.should raise_error(FloatDomainError) end it "raises FloatDomainError for NaN values" do nan = 0.0 / 0 - -> { @s.rb_dbl2big(nan) }.should raise_error(FloatDomainError) + lambda { @s.rb_dbl2big(nan) }.should raise_error(FloatDomainError) end end end diff --git a/spec/ruby/optional/capi/binding_spec.rb b/spec/ruby/optional/capi/binding_spec.rb deleted file mode 100644 index 2165705457..0000000000 --- a/spec/ruby/optional/capi/binding_spec.rb +++ /dev/null @@ -1,28 +0,0 @@ -require_relative 'spec_helper' - -load_extension("binding") - -describe "CApiBindingSpecs" do - before :each do - @b = CApiBindingSpecs.new - end - - describe "Kernel#binding" do - ruby_version_is '3.2' do - it "raises when called from C" do - foo = 14 - -> { @b.get_binding }.should raise_error(RuntimeError) - end - end - - ruby_version_is ''...'3.2' do - it "gives the top-most Ruby binding when called from C" do - foo = 14 - b = @b.get_binding - b.local_variable_get(:foo).should == 14 - b.local_variable_set :foo, 12 - foo.should == 12 - end - end - end -end diff --git a/spec/ruby/optional/capi/boolean_spec.rb b/spec/ruby/optional/capi/boolean_spec.rb index 351419cbec..49303662e7 100644 --- a/spec/ruby/optional/capi/boolean_spec.rb +++ b/spec/ruby/optional/capi/boolean_spec.rb @@ -1,4 +1,4 @@ -require_relative 'spec_helper' +require File.expand_path('../spec_helper', __FILE__) load_extension("boolean") diff --git a/spec/ruby/optional/capi/class_spec.rb b/spec/ruby/optional/capi/class_spec.rb index a231245ebe..a25e80af60 100644 --- a/spec/ruby/optional/capi/class_spec.rb +++ b/spec/ruby/optional/capi/class_spec.rb @@ -1,6 +1,5 @@ -require_relative 'spec_helper' -require_relative 'fixtures/class' -require_relative '../../core/module/fixtures/classes' +require File.expand_path('../spec_helper', __FILE__) +require File.expand_path('../fixtures/class', __FILE__) load_extension("class") compile_extension("class_under_autoload") @@ -12,7 +11,6 @@ autoload :ClassIdUnderAutoload, "#{object_path}/class_id_under_autoload_spec" describe :rb_path_to_class, shared: true do it "returns a class or module from a scoped String" do @s.send(@method, "CApiClassSpecs::A::B").should equal(CApiClassSpecs::A::B) - @s.send(@method, "CApiClassSpecs::A::M").should equal(CApiClassSpecs::A::M) end it "resolves autoload constants" do @@ -20,21 +18,19 @@ describe :rb_path_to_class, shared: true do end it "raises an ArgumentError if a constant in the path does not exist" do - -> { @s.send(@method, "CApiClassSpecs::NotDefined::B") }.should raise_error(ArgumentError) + lambda { @s.send(@method, "CApiClassSpecs::NotDefined::B") }.should raise_error(ArgumentError) end it "raises an ArgumentError if the final constant does not exist" do - -> { @s.send(@method, "CApiClassSpecs::NotDefined") }.should raise_error(ArgumentError) + lambda { @s.send(@method, "CApiClassSpecs::NotDefined") }.should raise_error(ArgumentError) end it "raises a TypeError if the constant is not a class or module" do - -> { - @s.send(@method, "CApiClassSpecs::A::C") - }.should raise_error(TypeError, 'CApiClassSpecs::A::C does not refer to class/module') + lambda { @s.send(@method, "CApiClassSpecs::A::C") }.should raise_error(TypeError) end it "raises an ArgumentError even if a constant in the path exists on toplevel" do - -> { @s.send(@method, "CApiClassSpecs::Object") }.should raise_error(ArgumentError) + lambda { @s.send(@method, "CApiClassSpecs::Object") }.should raise_error(ArgumentError) end end @@ -43,105 +39,24 @@ describe "C-API Class function" do @s = CApiClassSpecs.new end - describe "rb_class_instance_methods" do - it "returns the public and protected methods of self and its ancestors" do - methods = @s.rb_class_instance_methods(ModuleSpecs::Basic) - methods.should include(:protected_module, :public_module) - - methods = @s.rb_class_instance_methods(ModuleSpecs::Basic, true) - methods.should include(:protected_module, :public_module) - end - - it "when passed false as a parameter, returns the instance methods of the class" do - methods = @s.rb_class_instance_methods(ModuleSpecs::Child, false) - methods.should include(:protected_child, :public_child) - end - end - - describe "rb_class_public_instance_methods" do - it "returns a list of public methods in module and its ancestors" do - methods = @s.rb_class_public_instance_methods(ModuleSpecs::CountsChild) - methods.should include(:public_3) - methods.should include(:public_2) - methods.should include(:public_1) - - methods = @s.rb_class_public_instance_methods(ModuleSpecs::CountsChild, true) - methods.should include(:public_3) - methods.should include(:public_2) - methods.should include(:public_1) - end - - it "when passed false as a parameter, should return only methods defined in that module" do - @s.rb_class_public_instance_methods(ModuleSpecs::CountsChild, false).should == [:public_1] - end - end - - describe "rb_class_protected_instance_methods" do - it "returns a list of protected methods in module and its ancestors" do - methods = @s.rb_class_protected_instance_methods(ModuleSpecs::CountsChild) - methods.should include(:protected_3) - methods.should include(:protected_2) - methods.should include(:protected_1) - - methods = @s.rb_class_protected_instance_methods(ModuleSpecs::CountsChild, true) - methods.should include(:protected_3) - methods.should include(:protected_2) - methods.should include(:protected_1) - end - - it "when passed false as a parameter, should return only methods defined in that module" do - @s.rb_class_public_instance_methods(ModuleSpecs::CountsChild, false).should == [:public_1] - end - end - - describe "rb_class_private_instance_methods" do - it "returns a list of private methods in module and its ancestors" do - @s.rb_class_private_instance_methods(ModuleSpecs::CountsChild).should == ModuleSpecs::CountsChild.private_instance_methods - @s.rb_class_private_instance_methods(ModuleSpecs::CountsChild, true).should == ModuleSpecs::CountsChild.private_instance_methods - end - - it "when passed false as a parameter, should return only methods defined in that module" do - methods = @s.rb_class_private_instance_methods(ModuleSpecs::CountsChild, false) - methods.should == [:private_1] - end - end - describe "rb_class_new_instance" do it "allocates and initializes a new object" do - o = @s.rb_class_new_instance([], CApiClassSpecs::Alloc) + o = @s.rb_class_new_instance(0, nil, CApiClassSpecs::Alloc) o.class.should == CApiClassSpecs::Alloc o.initialized.should be_true end it "passes arguments to the #initialize method" do - o = @s.rb_class_new_instance([:one, :two], CApiClassSpecs::Alloc) + o = @s.rb_class_new_instance(2, [:one, :two], CApiClassSpecs::Alloc) o.arguments.should == [:one, :two] end end - describe "rb_class_new_instance_kw" do - it "passes arguments and keywords to the #initialize method" do - obj = @s.rb_class_new_instance_kw([{pos: 1}, {kw: 2}], CApiClassSpecs::KeywordAlloc) - obj.args.should == [{pos: 1}] - obj.kwargs.should == {kw: 2} - - obj = @s.rb_class_new_instance_kw([{}], CApiClassSpecs::KeywordAlloc) - obj.args.should == [] - obj.kwargs.should == {} - end - - it "raises TypeError if the last argument is not a Hash" do - -> { - @s.rb_class_new_instance_kw([42], CApiClassSpecs::KeywordAlloc) - }.should raise_error(TypeError, 'no implicit conversion of Integer into Hash') - end - end - describe "rb_include_module" do it "includes a module into a class" do c = Class.new o = c.new - -> { o.included? }.should raise_error(NameError) + lambda { o.included? }.should raise_error(NameError) @s.rb_include_module(c, CApiClassSpecs::M) o.included?.should be_true end @@ -155,12 +70,12 @@ describe "C-API Class function" do it "defines an attr_reader when passed true, false" do @s.rb_define_attr(CApiClassSpecs::Attr, :foo, true, false) @a.foo.should == 1 - -> { @a.foo = 5 }.should raise_error(NameError) + lambda { @a.foo = 5 }.should raise_error(NameError) end it "defines an attr_writer when passed false, true" do @s.rb_define_attr(CApiClassSpecs::Attr, :bar, false, true) - -> { @a.bar }.should raise_error(NameError) + lambda { @a.bar }.should raise_error(NameError) @a.bar = 5 @a.instance_variable_get(:@bar).should == 5 end @@ -180,12 +95,6 @@ describe "C-API Class function" do obj.call_super_method.should == :super_method end - it "calls the method in the superclass with the correct self" do - @s.define_call_super_method CApiClassSpecs::SubSelf, "call_super_method" - obj = CApiClassSpecs::SubSelf.new - obj.call_super_method.should equal obj - end - it "calls the method in the superclass through two native levels" do @s.define_call_super_method CApiClassSpecs::Sub, "call_super_method" @s.define_call_super_method CApiClassSpecs::SubSub, "call_super_method" @@ -202,10 +111,6 @@ describe "C-API Class function" do it "returns a string for an anonymous class" do @s.rb_class2name(Class.new).should be_kind_of(String) end - - it "returns a string beginning with # for an anonymous class" do - @s.rb_class2name(Struct.new(:x, :y).new(1, 2).class).should.start_with?('#') - end end describe "rb_class_path" do @@ -266,7 +171,7 @@ describe "C-API Class function" do end it "raises a NameError if the class variable is not defined" do - -> { + lambda { @s.rb_cv_get(CApiClassSpecs::CVars, "@@no_cvar") }.should raise_error(NameError, /class variable @@no_cvar/) end @@ -305,30 +210,23 @@ describe "C-API Class function" do end it "raises a TypeError when given a non class object to superclass" do - -> { + lambda { @s.rb_define_class("ClassSpecDefineClass3", Module.new) }.should raise_error(TypeError) end it "raises a TypeError when given a mismatched class to superclass" do - -> { + lambda { @s.rb_define_class("ClassSpecDefineClass", Object) }.should raise_error(TypeError) end - it "raises a ArgumentError when given NULL as superclass" do - -> { - @s.rb_define_class("ClassSpecDefineClass4", nil) - }.should raise_error(ArgumentError) - end - - it "allows arbitrary names, including constant names not valid in Ruby" do - cls = @s.rb_define_class("_INVALID_CLASS", CApiClassSpecs::Super) - cls.name.should == "_INVALID_CLASS" - - -> { - Object.const_get(cls.name) - }.should raise_error(NameError, /wrong constant name/) + ruby_version_is "2.4" do + it "raises a ArgumentError when given NULL as superclass" do + lambda { + @s.rb_define_class("ClassSpecDefineClass4", nil) + }.should raise_error(ArgumentError) + end end end @@ -353,35 +251,40 @@ describe "C-API Class function" do end it "raises a TypeError when given a non class object to superclass" do - -> { @s.rb_define_class_under(CApiClassSpecs, + lambda { @s.rb_define_class_under(CApiClassSpecs, "ClassUnder5", Module.new) }.should raise_error(TypeError) end - it "raises a TypeError when given a mismatched class to superclass" do - CApiClassSpecs::ClassUnder6 = Class.new(CApiClassSpecs::Super) - -> { @s.rb_define_class_under(CApiClassSpecs, - "ClassUnder6", - Class.new) - }.should raise_error(TypeError) + ruby_version_is "2.3" do + it "raises a TypeError when given a mismatched class to superclass" do + CApiClassSpecs::ClassUnder6 = Class.new(CApiClassSpecs::Super) + lambda { @s.rb_define_class_under(CApiClassSpecs, + "ClassUnder6", + Class.new) + }.should raise_error(TypeError) + end end - it "defines a class for an existing Autoload" do - ClassUnderAutoload.name.should == "ClassUnderAutoload" + ruby_version_is ""..."2.3" do + it "raises a NameError when given a mismatched class to superclass" do + CApiClassSpecs::ClassUnder6 = Class.new(CApiClassSpecs::Super) + lambda { @s.rb_define_class_under(CApiClassSpecs, + "ClassUnder6", + Class.new) + }.should raise_error(NameError) + end end - it "raises a TypeError if class is defined and its superclass mismatches the given one" do - -> { @s.rb_define_class_under(CApiClassSpecs, "Sub", Object) }.should raise_error(TypeError) + it "defines a class for an existing Autoload" do + ClassUnderAutoload.name.should == "ClassUnderAutoload" end - it "allows arbitrary names, including constant names not valid in Ruby" do - cls = @s.rb_define_class_under(CApiClassSpecs, "_INVALID_CLASS", CApiClassSpecs::Super) - cls.name.should == "CApiClassSpecs::_INVALID_CLASS" - - -> { - CApiClassSpecs.const_get(cls.name) - }.should raise_error(NameError, /wrong constant name/) + ruby_version_is "2.3" do + it "raises a TypeError if class is defined and its superclass mismatches the given one" do + lambda { @s.rb_define_class_under(CApiClassSpecs, "Sub", Object) }.should raise_error(TypeError) + end end end @@ -407,17 +310,10 @@ describe "C-API Class function" do ClassIdUnderAutoload.name.should == "ClassIdUnderAutoload" end - it "raises a TypeError if class is defined and its superclass mismatches the given one" do - -> { @s.rb_define_class_id_under(CApiClassSpecs, :Sub, Object) }.should raise_error(TypeError) - end - - it "allows arbitrary names, including constant names not valid in Ruby" do - cls = @s.rb_define_class_id_under(CApiClassSpecs, :_INVALID_CLASS2, CApiClassSpecs::Super) - cls.name.should == "CApiClassSpecs::_INVALID_CLASS2" - - -> { - CApiClassSpecs.const_get(cls.name) - }.should raise_error(NameError, /wrong constant name/) + ruby_version_is "2.3" do + it "raises a TypeError if class is defined and its superclass mismatches the given one" do + lambda { @s.rb_define_class_id_under(CApiClassSpecs, :Sub, Object) }.should raise_error(TypeError) + end end end @@ -437,25 +333,25 @@ describe "C-API Class function" do end it "raises a NameError if the class variable is not defined" do - -> { + lambda { @s.rb_cvar_get(CApiClassSpecs::CVars, "@@no_cvar") }.should raise_error(NameError, /class variable @@no_cvar/) end end describe "rb_class_new" do - it "returns a new subclass of the superclass" do + it "returns an new subclass of the superclass" do subclass = @s.rb_class_new(CApiClassSpecs::NewClass) CApiClassSpecs::NewClass.should be_ancestor_of(subclass) end it "raises a TypeError if passed Class as the superclass" do - -> { @s.rb_class_new(Class) }.should raise_error(TypeError) + lambda { @s.rb_class_new(Class) }.should raise_error(TypeError) end it "raises a TypeError if passed a singleton class as the superclass" do metaclass = Object.new.singleton_class - -> { @s.rb_class_new(metaclass) }.should raise_error(TypeError) + lambda { @s.rb_class_new(metaclass) }.should raise_error(TypeError) end end @@ -487,16 +383,4 @@ describe "C-API Class function" do @s.rb_class_real(0).should == 0 end end - - describe "rb_class_get_superclass" do - it "returns parent class for a provided class" do - a = Class.new - @s.rb_class_get_superclass(Class.new(a)).should == a - end - - it "returns false when there is no parent class" do - @s.rb_class_get_superclass(BasicObject).should == false - @s.rb_class_get_superclass(Module.new).should == false - end - end end diff --git a/spec/ruby/optional/capi/complex_spec.rb b/spec/ruby/optional/capi/complex_spec.rb index 3d8142d172..ad84e47ced 100644 --- a/spec/ruby/optional/capi/complex_spec.rb +++ b/spec/ruby/optional/capi/complex_spec.rb @@ -1,4 +1,4 @@ -require_relative 'spec_helper' +require File.expand_path('../spec_helper', __FILE__) load_extension("complex") diff --git a/spec/ruby/optional/capi/constants_spec.rb b/spec/ruby/optional/capi/constants_spec.rb index 172d10a788..096332971c 100644 --- a/spec/ruby/optional/capi/constants_spec.rb +++ b/spec/ruby/optional/capi/constants_spec.rb @@ -1,4 +1,4 @@ -require_relative 'spec_helper' +require File.expand_path('../spec_helper', __FILE__) load_extension("constants") @@ -11,42 +11,30 @@ describe "C-API constant" do @s.rb_cArray.should == Array end - specify "rb_cBasicObject references the BasicObject class" do - @s.rb_cBasicObject.should == BasicObject - end - - specify "rb_cBinding references the Binding class" do - @s.rb_cBinding.should == Binding + ruby_version_is ""..."2.4" do + specify "rb_cBignum references the Bignum class" do + @s.rb_cBignum.should == Bignum + end end specify "rb_cClass references the Class class" do @s.rb_cClass.should == Class end - specify "rb_cComplex references the Complex class" do - @s.rb_cComplex.should == Complex - end - specify "rb_mComparable references the Comparable module" do @s.rb_mComparable.should == Comparable end - specify "rb_cDir references the Dir class" do - @s.rb_cDir.should == Dir - end - - specify "rb_cEncoding references the Encoding class" do - @s.rb_cEncoding.should == Encoding + ruby_version_is ""..."2.5" do + specify "rb_cData references the Data class" do + @s.rb_cData.should == Data + end end specify "rb_mEnumerable references the Enumerable module" do @s.rb_mEnumerable.should == Enumerable end - specify "rb_cEnumerator references the Enumerator class" do - @s.rb_cEnumerator.should == Enumerator - end - specify "rb_cFalseClass references the FalseClass class" do @s.rb_cFalseClass.should == FalseClass end @@ -55,18 +43,16 @@ describe "C-API constant" do @s.rb_cFile.should == File end - specify "rb_mFileTest references the FileTest module" do - @s.rb_mFileTest.should == FileTest + ruby_version_is ""..."2.4" do + specify "rb_cFixnum references the Fixnum class" do + @s.rb_cFixnum.should == Fixnum + end end specify "rb_cFloat references the Float class" do @s.rb_cFloat.should == Float end - specify "rb_mGC references the GC module" do - @s.rb_mGC.should == GC - end - specify "rb_cHash references the Hash class" do @s.rb_cHash.should == Hash end @@ -83,18 +69,10 @@ describe "C-API constant" do @s.rb_mKernel.should == Kernel end - specify "rb_mMath references the Math module" do - @s.rb_mMath.should == Math - end - specify "rb_cMatch references the MatchData class" do @s.rb_cMatch.should == MatchData end - specify "rb_cMethod references the Method class" do - @s.rb_cMethod.should == Method - end - specify "rb_cModule references the Module class" do @s.rb_cModule.should == Module end @@ -111,34 +89,14 @@ describe "C-API constant" do @s.rb_cObject.should == Object end - specify "rb_cProc references the Proc class" do - @s.rb_cProc.should == Proc - end - - specify "rb_mProcess references the Process module" do - @s.rb_mProcess.should == Process - end - - specify "rb_cRandom references the Random class" do - @s.rb_cRandom.should == Random - end - specify "rb_cRange references the Range class" do @s.rb_cRange.should == Range end - specify "rb_cRational references the Rational class" do - @s.rb_cRational.should == Rational - end - specify "rb_cRegexp references the Regexp class" do @s.rb_cRegexp.should == Regexp end - specify "rb_cStat references the File::Stat class" do - @s.rb_cStat.should == File::Stat - end - specify "rb_cString references the String class" do @s.rb_cString.should == String end @@ -163,9 +121,18 @@ describe "C-API constant" do @s.rb_cTrueClass.should == TrueClass end - specify "rb_cUnboundMethod references the UnboundMethod class" do - @s.rb_cUnboundMethod.should == UnboundMethod + specify "rb_cProc references the Proc class" do + @s.rb_cProc.should == Proc + end + + specify "rb_cMethod references the Method class" do + @s.rb_cMethod.should == Method end + + specify "rb_cDir references the Dir class" do + @s.rb_cDir.should == Dir + end + end describe "C-API exception constant" do @@ -177,14 +144,6 @@ describe "C-API exception constant" do @s.rb_eArgError.should == ArgumentError end - specify "rb_eEncodingError references the EncodingError class" do - @s.rb_eEncodingError.should == EncodingError - end - - specify "rb_eEncCompatError references the Encoding::CompatibilityError" do - @s.rb_eEncCompatError.should == Encoding::CompatibilityError - end - specify "rb_eEOFError references the EOFError class" do @s.rb_eEOFError.should == EOFError end @@ -197,20 +156,10 @@ describe "C-API exception constant" do @s.rb_eException.should == Exception end - specify "rb_eFatal references the fatal class" do - fatal = @s.rb_eFatal - fatal.should be_kind_of(Class) - fatal.should < Exception - end - specify "rb_eFloatDomainError references the FloatDomainError class" do @s.rb_eFloatDomainError.should == FloatDomainError end - specify "rb_eFrozenError references the FrozenError class" do - @s.rb_eFrozenError.should == FrozenError - end - specify "rb_eIndexError references the IndexError class" do @s.rb_eIndexError.should == IndexError end @@ -223,10 +172,6 @@ describe "C-API exception constant" do @s.rb_eIOError.should == IOError end - specify "rb_eKeyError references the KeyError class" do - @s.rb_eKeyError.should == KeyError - end - specify "rb_eLoadError references the LoadError class" do @s.rb_eLoadError.should == LoadError end @@ -239,6 +184,10 @@ describe "C-API exception constant" do @s.rb_eMathDomainError.should == Math::DomainError end + specify "rb_eEncCompatError references the Encoding::CompatibilityError" do + @s.rb_eEncCompatError.should == Encoding::CompatibilityError + end + specify "rb_eNameError references the NameError class" do @s.rb_eNameError.should == NameError end @@ -283,10 +232,6 @@ describe "C-API exception constant" do @s.rb_eStandardError.should == StandardError end - specify "rb_eStopIteration references the StopIteration class" do - @s.rb_eStopIteration.should == StopIteration - end - specify "rb_eSyntaxError references the SyntaxError class" do @s.rb_eSyntaxError.should == SyntaxError end diff --git a/spec/ruby/optional/capi/data_spec.rb b/spec/ruby/optional/capi/data_spec.rb index d000eba6e3..ae1f57b091 100644 --- a/spec/ruby/optional/capi/data_spec.rb +++ b/spec/ruby/optional/capi/data_spec.rb @@ -1,53 +1,46 @@ -require_relative 'spec_helper' -ruby_version_is ""..."3.4" do - load_extension("data") - - describe "CApiAllocSpecs (a class with an alloc func defined)" do - it "calls the alloc func" do - @s = CApiAllocSpecs.new - @s.wrapped_data.should == 42 # not defined in initialize - end +require File.expand_path('../spec_helper', __FILE__) + +load_extension("data") + +describe "CApiAllocSpecs (a class with an alloc func defined)" do + it "calls the alloc func" do + @s = CApiAllocSpecs.new + @s.wrapped_data.should == 42 # not defined in initialize end +end - describe "CApiWrappedStruct" do - before :each do - @s = CApiWrappedStructSpecs.new - end +describe "CApiWrappedStruct" do + before :each do + @s = CApiWrappedStructSpecs.new + end - it "wraps with Data_Wrap_Struct and Data_Get_Struct returns data" do - a = @s.wrap_struct(1024) - @s.get_struct(a).should == 1024 - end + it "wraps with Data_Wrap_Struct and Data_Get_Struct returns data" do + a = @s.wrap_struct(1024) + @s.get_struct(a).should == 1024 + end + + it "allows for using NULL as the klass for Data_Wrap_Struct" do + a = @s.wrap_struct_null(1024) + @s.get_struct(a).should == 1024 + end - describe "RDATA()" do - it "returns the struct data" do - a = @s.wrap_struct(1024) - @s.get_struct_rdata(a).should == 1024 - end - - it "allows changing the wrapped struct" do - a = @s.wrap_struct(1024) - @s.change_struct(a, 100) - @s.get_struct(a).should == 100 - end - - it "raises a TypeError if the object does not wrap a struct" do - -> { @s.get_struct(Object.new) }.should raise_error(TypeError) - end + describe "RDATA()" do + it "returns the struct data" do + a = @s.wrap_struct(1024) + @s.get_struct_rdata(a).should == 1024 end - describe "rb_check_type" do - it "does not raise an exception when checking data objects" do - a = @s.wrap_struct(1024) - @s.rb_check_type(a, a).should == true - end + it "allows changing the wrapped struct" do + a = @s.wrap_struct(1024) + @s.change_struct(a, 100) + @s.get_struct(a).should == 100 end + end - describe "DATA_PTR" do - it "returns the struct data" do - a = @s.wrap_struct(1024) - @s.get_struct_data_ptr(a).should == 1024 - end + describe "DATA_PTR" do + it "returns the struct data" do + a = @s.wrap_struct(1024) + @s.get_struct_data_ptr(a).should == 1024 end end end diff --git a/spec/ruby/optional/capi/debug_spec.rb b/spec/ruby/optional/capi/debug_spec.rb deleted file mode 100644 index 14ba25609c..0000000000 --- a/spec/ruby/optional/capi/debug_spec.rb +++ /dev/null @@ -1,74 +0,0 @@ -require_relative 'spec_helper' - -load_extension('debug') - -describe "C-API Debug function" do - before :each do - @o = CApiDebugSpecs.new - end - - describe "rb_debug_inspector_open" do - it "creates a debug context and calls the given callback" do - @o.rb_debug_inspector_open(42).should be_kind_of(Array) - @o.debug_spec_callback_data.should == 42 - end - end - - describe "rb_debug_inspector_frame_self_get" do - it "returns self" do - @o.rb_debug_inspector_frame_self_get(0).should == @o - @o.rb_debug_inspector_frame_self_get(1).should == self - end - end - - describe "rb_debug_inspector_frame_class_get" do - it "returns the frame class" do - @o.rb_debug_inspector_frame_class_get(0).should == CApiDebugSpecs - end - end - - describe "rb_debug_inspector_frame_binding_get" do - it "returns the current binding" do - a = "test" - b = @o.rb_debug_inspector_frame_binding_get(1) - b.should be_an_instance_of(Binding) - b.local_variable_get(:a).should == "test" - end - - it "matches the locations in rb_debug_inspector_backtrace_locations" do - frames = @o.rb_debug_inspector_open(42) - frames.each do |_s, klass, binding, iseq, backtrace_location| - if binding - # YJIT modifies Array#each backtraces but leaves its source_location as is - unless defined?(RubyVM::YJIT) && klass == Array && iseq.label == "each" - binding.source_location.should == [backtrace_location.path, backtrace_location.lineno] - end - method_name = binding.eval('__method__') - if method_name - method_name.should == backtrace_location.base_label.to_sym - end - end - end - end - end - - describe "rb_debug_inspector_frame_iseq_get" do - it "returns an InstructionSequence" do - if defined?(RubyVM::InstructionSequence) - @o.rb_debug_inspector_frame_iseq_get(1).should be_an_instance_of(RubyVM::InstructionSequence) - else - @o.rb_debug_inspector_frame_iseq_get(1).should == nil - end - end - end - - describe "rb_debug_inspector_backtrace_locations" do - it "returns an array of Thread::Backtrace::Location" do - bts = @o.rb_debug_inspector_backtrace_locations - bts.should_not.empty? - bts.each { |bt| bt.should be_kind_of(Thread::Backtrace::Location) } - location = "#{__FILE__}:#{__LINE__ - 3}" - bts[1].to_s.should include(location) - end - end -end diff --git a/spec/ruby/optional/capi/encoding_spec.rb b/spec/ruby/optional/capi/encoding_spec.rb index 1529e012b0..d6b0bacd70 100644 --- a/spec/ruby/optional/capi/encoding_spec.rb +++ b/spec/ruby/optional/capi/encoding_spec.rb @@ -1,9 +1,8 @@ # -*- encoding: utf-8 -*- -# frozen_string_literal: false -require_relative 'spec_helper' -require_relative 'fixtures/encoding' +require File.expand_path('../spec_helper', __FILE__) +require File.expand_path('../fixtures/encoding', __FILE__) -extension_path = load_extension('encoding') +load_extension('encoding') describe :rb_enc_get_index, shared: true do it "returns the index of the encoding of a String" do @@ -13,6 +12,24 @@ describe :rb_enc_get_index, shared: true do it "returns the index of the encoding of a Regexp" do @s.send(@method, /regexp/).should >= 0 end + + it "returns the index of the encoding of an Object" do + obj = mock("rb_enc_get_index string") + @s.rb_enc_set_index(obj, 1) + @s.send(@method, obj).should == 1 + end + + it "returns the index of the dummy encoding of an Object" do + obj = mock("rb_enc_get_index string") + index = Encoding.list.index(Encoding::UTF_16) + @s.rb_enc_set_index(obj, index) + @s.send(@method, obj).should == index + end + + it "returns 0 for an object without an encoding" do + obj = mock("rb_enc_get_index string") + @s.send(@method, obj).should == 0 + end end describe :rb_enc_set_index, shared: true do @@ -22,7 +39,7 @@ describe :rb_enc_set_index, shared: true do # This is used because indexes should be considered implementation # dependent. So a pair is returned: - # [rb_enc_find_index() -> name, rb_enc_get(obj) -> name] + # [rb_enc_find_index()->name, rb_enc_get(obj)->name] result.first.should == result.last end @@ -32,77 +49,22 @@ describe :rb_enc_set_index, shared: true do result.first.should == result.last end - it "raises an ArgumentError for a non-encoding capable object" do - obj = Object.new - -> { - result = @s.send(@method, obj, 1) - }.should raise_error(ArgumentError, "cannot set encoding on non-encoding capable object") + it "associates an encoding with an object" do + obj = mock("rb_enc_set_index string") + result = @s.send(@method, obj, 1) + result.first.should == result.last end end describe "C-API Encoding function" do - @n = 0 - before :each do @s = CApiEncodingSpecs.new end - describe "rb_enc_alias" do + describe "rb_encdb_alias" do it "creates an alias for an existing Encoding" do - name = "ZOMGWTFBBQ#{@n += 1}" - @s.rb_enc_alias(name, "UTF-8").should >= 0 - Encoding.find(name).name.should == "UTF-8" - end - end - - describe "rb_enc_codelen" do - it "returns the correct length for the given codepoint" do - @s.rb_enc_codelen(0x24, Encoding::UTF_8).should == 1 - @s.rb_enc_codelen(0xA2, Encoding::UTF_8).should == 2 - @s.rb_enc_codelen(0x20AC, Encoding::UTF_8).should == 3 - @s.rb_enc_codelen(0x24B62, Encoding::UTF_8).should == 4 - end - end - - describe "rb_enc_strlen" do - before :each do - @str = 'こにちわ' # Each codepoint in this string is 3 bytes in UTF-8 - end - - it "returns the correct string length for the encoding" do - @s.rb_enc_strlen(@str, @str.bytesize, Encoding::UTF_8).should == 4 - @s.rb_enc_strlen(@str, @str.bytesize, Encoding::BINARY).should == 12 - end - - it "returns the string length based on a fixed-width encoding's character length, even if the encoding is incompatible" do - @s.rb_enc_strlen(@str, @str.bytesize, Encoding::UTF_16BE).should == 6 - @s.rb_enc_strlen(@str, @str.bytesize, Encoding::UTF_16LE).should == 6 - @s.rb_enc_strlen(@str, @str.bytesize, Encoding::UTF_32BE).should == 3 - @s.rb_enc_strlen(@str, @str.bytesize, Encoding::UTF_32LE).should == 3 - end - - it "does not consider strings to be NUL-terminated" do - s = "abc\0def" - @s.rb_enc_strlen(s, s.bytesize, Encoding::US_ASCII).should == 7 - @s.rb_enc_strlen(s, s.bytesize, Encoding::UTF_8).should == 7 - end - - describe "handles broken strings" do - it "combines valid character and invalid character counts in UTF-8" do - # The result is 3 because `rb_enc_strlen` counts the first valid character and then adds - # the byte count for the invalid character that follows for 1 + 2. - @s.rb_enc_strlen(@str, 5, Encoding::UTF_8).should == 3 - end - - it "combines valid character and invalid character counts in UTF-16" do - @s.rb_enc_strlen(@str, 5, Encoding::UTF_16BE).should == 3 - end - - it "rounds up for fixed-width encodings" do - @s.rb_enc_strlen(@str, 7, Encoding::UTF_32BE).should == 2 - @s.rb_enc_strlen(@str, 7, Encoding::UTF_32LE).should == 2 - @s.rb_enc_strlen(@str, 5, Encoding::BINARY).should == 5 - end + @s.rb_encdb_alias("ZOMGWTFBBQ", "UTF-8").should >= 0 + Encoding.find("ZOMGWTFBBQ").name.should == "UTF-8" end end @@ -130,72 +92,12 @@ describe "C-API Encoding function" do end end - describe "rb_enc_isalnum" do - it "returns non-zero for alpha-numeric characters" do - @s.rb_enc_isalnum("a".ord, Encoding::US_ASCII).should == true - @s.rb_enc_isalnum("2".ord, Encoding::US_ASCII).should == true - @s.rb_enc_isalnum("a".ord, Encoding::UTF_8).should == true - @s.rb_enc_isalnum("2".ord, Encoding::UTF_8).should == true - @s.rb_enc_isalnum("é".encode(Encoding::ISO_8859_1).ord, Encoding::ISO_8859_1).should == true - end - - it "returns zero for non alpha-numeric characters" do - @s.rb_enc_isalnum("-".ord, Encoding::US_ASCII).should == false - @s.rb_enc_isalnum(" ".ord, Encoding::US_ASCII).should == false - @s.rb_enc_isalnum("-".ord, Encoding::UTF_8).should == false - @s.rb_enc_isalnum(" ".ord, Encoding::UTF_8).should == false - end - end - - describe "rb_enc_isspace" do - it "returns non-zero for space characters" do - @s.rb_enc_isspace(" ".ord, Encoding::US_ASCII).should == true - @s.rb_enc_isspace(" ".ord, Encoding::UTF_8).should == true - end - - it "returns zero for non space characters" do - @s.rb_enc_isspace("-".ord, Encoding::US_ASCII).should == false - @s.rb_enc_isspace("A".ord, Encoding::US_ASCII).should == false - @s.rb_enc_isspace("3".ord, Encoding::US_ASCII).should == false - @s.rb_enc_isspace("-".ord, Encoding::UTF_8).should == false - @s.rb_enc_isspace("A".ord, Encoding::UTF_8).should == false - @s.rb_enc_isspace("3".ord, Encoding::UTF_8).should == false - end - end - describe "rb_enc_from_index" do it "returns an Encoding" do @s.rb_enc_from_index(0).should be_an_instance_of(String) end end - describe "rb_enc_mbc_to_codepoint" do - it "returns the correct codepoint for the given character and size" do - @s.rb_enc_mbc_to_codepoint("é").should == 0xE9 - end - - it "returns 0 if p == e" do - @s.rb_enc_mbc_to_codepoint("").should == 0 - end - - it "returns the raw byte if incomplete character in UTF-8" do - @s.rb_enc_mbc_to_codepoint("\xC3").should == 0xC3 - @s.rb_enc_mbc_to_codepoint("\x80").should == 0x80 - end - end - - describe "rb_enc_mbcput" do - it "writes the correct bytes to the buffer" do - @s.rb_enc_mbcput(0x24, Encoding::UTF_8).should == "$" - @s.rb_enc_mbcput(0xA2, Encoding::UTF_8).should == "¢" - @s.rb_enc_mbcput(0x20AC, Encoding::UTF_8).should == "€" - @s.rb_enc_mbcput(0x24B62, Encoding::UTF_8).should == "𤭢" - - @s.rb_enc_mbcput(0x24, Encoding::UTF_16BE).bytes.should == [0, 0x24] - @s.rb_enc_mbcput(0x24B62, Encoding::UTF_16LE).bytes.should == [82, 216, 98, 223] - end - end - describe "rb_usascii_encoding" do it "returns the encoding for Encoding::US_ASCII" do @s.rb_usascii_encoding.should == "US-ASCII" @@ -203,7 +105,7 @@ describe "C-API Encoding function" do end describe "rb_ascii8bit_encoding" do - it "returns the encoding for Encoding::BINARY" do + it "returns the encoding for Encoding::ASCII_8BIT" do @s.rb_ascii8bit_encoding.should == "ASCII-8BIT" end end @@ -233,39 +135,16 @@ describe "C-API Encoding function" do end describe "rb_enc_get" do - it "returns the encoding associated with an object" do - str = "abc".encode Encoding::BINARY + it "returns the encoding ossociated with an object" do + str = "abc".encode Encoding::ASCII_8BIT @s.rb_enc_get(str).should == "ASCII-8BIT" end end - describe "rb_enc_precise_mbclen" do - it "returns the correct length for single byte characters" do - @s.rb_enc_precise_mbclen("hello", 7).should == 1 - @s.rb_enc_precise_mbclen("hello", 5).should == 1 - @s.rb_enc_precise_mbclen("hello", 1).should == 1 - @s.rb_enc_precise_mbclen("hello", 0).should == -2 - @s.rb_enc_precise_mbclen("hello", -1).should == -2 - @s.rb_enc_precise_mbclen("hello", -5).should == -2 - end - - it "returns the correct length for multi-byte characters" do - @s.rb_enc_precise_mbclen("ésumé", 2).should == 2 - @s.rb_enc_precise_mbclen("ésumé", 3).should == 2 - @s.rb_enc_precise_mbclen("ésumé", 0).should == -2 - @s.rb_enc_precise_mbclen("ésumé", 1).should == -2 - @s.rb_enc_precise_mbclen("あ", 20).should == 3 - @s.rb_enc_precise_mbclen("あ", 3).should == 3 - @s.rb_enc_precise_mbclen("あ", 2).should == -2 - @s.rb_enc_precise_mbclen("あ", 0).should == -2 - @s.rb_enc_precise_mbclen("あ", -2).should == -2 - end - end - describe "rb_obj_encoding" do - it "returns the encoding associated with an object" do - str = "abc".encode Encoding::BINARY - @s.rb_obj_encoding(str).should == Encoding::BINARY + it "returns the encoding ossociated with an object" do + str = "abc".encode Encoding::ASCII_8BIT + @s.rb_obj_encoding(str).should == Encoding::ASCII_8BIT end end @@ -273,20 +152,15 @@ describe "C-API Encoding function" do it_behaves_like :rb_enc_get_index, :rb_enc_get_index it "returns the index of the encoding of a Symbol" do - @s.rb_enc_get_index(:symbol).should >= 0 + @s.send(@method, :symbol).should >= 0 end it "returns -1 as the index of nil" do - @s.rb_enc_get_index(nil).should == -1 + @s.send(@method, nil).should == -1 end it "returns -1 as the index for immediates" do - @s.rb_enc_get_index(1).should == -1 - end - - it "returns -1 for an object without an encoding" do - obj = Object.new - @s.rb_enc_get_index(obj).should == -1 + @s.send(@method, 1).should == -1 end end @@ -302,36 +176,16 @@ describe "C-API Encoding function" do end end - describe "rb_enc_str_new_cstr" do - it "creates a new ruby string from a c string literal" do - result = @s.rb_enc_str_new_cstr_constant(Encoding::US_ASCII) - result.should == "test string literal" - result.encoding.should == Encoding::US_ASCII - end - - it "creates a new ruby string from a c string variable" do - result = @s.rb_enc_str_new_cstr("test string", Encoding::US_ASCII) - result.should == "test string" - result.encoding.should == Encoding::US_ASCII - end - - it "when null encoding is given with a c string literal, it creates a new ruby string with ASCII_8BIT encoding" do - result = @s.rb_enc_str_new_cstr_constant(nil) - result.should == "test string literal" - result.encoding.should == Encoding::ASCII_8BIT - end - end - describe "rb_enc_str_coderange" do - describe "when the encoding is BINARY" do + describe "when the encoding is ASCII-8BIT" do it "returns ENC_CODERANGE_7BIT if there are no high bits set" do - result = @s.rb_enc_str_coderange("abc".force_encoding("binary")) + result = @s.rb_enc_str_coderange("abc".force_encoding("ascii-8bit")) result.should == :coderange_7bit end it "returns ENC_CODERANGE_VALID if there are high bits set" do xEE = [0xEE].pack('C').force_encoding('utf-8') - result = @s.rb_enc_str_coderange(xEE.force_encoding("binary")) + result = @s.rb_enc_str_coderange(xEE.force_encoding("ascii-8bit")) result.should == :coderange_valid end end @@ -366,17 +220,6 @@ describe "C-API Encoding function" do end end - describe "MBCLEN_CHARFOUND_P" do - it "returns non-zero for valid character" do - @s.MBCLEN_CHARFOUND_P("a".ord).should == 1 - end - - it "returns zero for invalid characters" do - @s.MBCLEN_CHARFOUND_P(0).should == 0 - @s.MBCLEN_CHARFOUND_P(-1).should == 0 - end - end - describe "ENCODING_GET" do it_behaves_like :rb_enc_get_index, :ENCODING_GET end @@ -417,14 +260,6 @@ describe "C-API Encoding function" do @s.rb_to_encoding(obj).should == "UTF-8" end - - describe "when the rb_encoding struct is stored in native memory" do - it "can still read the name of the encoding" do - address = @s.rb_to_encoding_native_store(Encoding::UTF_8) - address.should be_kind_of(Integer) - @s.rb_to_encoding_native_name(address).should == "UTF-8" - end - end end describe "rb_to_encoding_index" do @@ -451,7 +286,7 @@ describe "C-API Encoding function" do describe "rb_enc_compatible" do it "returns 0 if the encodings of the Strings are not compatible" do - a = [0xff].pack('C').force_encoding "binary" + a = [0xff].pack('C').force_encoding "ascii-8bit" b = "\u3042".encode("utf-8") @s.rb_enc_compatible(a, b).should == 0 end @@ -476,11 +311,11 @@ describe "C-API Encoding function" do end it "raises a RuntimeError if the second argument is a Symbol" do - -> { @s.rb_enc_copy(:symbol, @obj) }.should raise_error(RuntimeError) + lambda { @s.rb_enc_copy(:symbol, @obj) }.should raise_error(RuntimeError) end it "sets the encoding of a Regexp to that of the second argument" do - @s.rb_enc_copy(/regexp/.dup, @obj).encoding.should == Encoding::US_ASCII + @s.rb_enc_copy(/regexp/, @obj).encoding.should == Encoding::US_ASCII end end @@ -516,63 +351,63 @@ describe "C-API Encoding function" do end it "returns the encoding for Encoding.default_external" do - Encoding.default_external = "ASCII-8BIT" + Encoding.default_external = "BINARY" @s.rb_default_external_encoding.should == "ASCII-8BIT" end end describe "rb_enc_associate" do it "sets the encoding of a String to the encoding" do - @s.rb_enc_associate("string", "BINARY").encoding.should == Encoding::BINARY + @s.rb_enc_associate("string", "ASCII-8BIT").encoding.should == Encoding::ASCII_8BIT end it "raises a RuntimeError if the argument is Symbol" do - -> { @s.rb_enc_associate(:symbol, "US-ASCII") }.should raise_error(RuntimeError) + lambda { @s.rb_enc_associate(:symbol, "US-ASCII") }.should raise_error(RuntimeError) end it "sets the encoding of a Regexp to the encoding" do - @s.rb_enc_associate(/regexp/.dup, "BINARY").encoding.should == Encoding::BINARY + @s.rb_enc_associate(/regexp/, "ASCII-8BIT").encoding.should == Encoding::ASCII_8BIT end it "sets the encoding of a String to a default when the encoding is NULL" do - @s.rb_enc_associate("string", nil).encoding.should == Encoding::BINARY + @s.rb_enc_associate("string", nil).encoding.should == Encoding::ASCII_8BIT end end describe "rb_enc_associate_index" do it "sets the encoding of a String to the encoding" do - index = @s.rb_enc_find_index("BINARY") + index = @s.rb_enc_find_index("ASCII-8BIT") enc = @s.rb_enc_associate_index("string", index).encoding - enc.should == Encoding::BINARY + enc.should == Encoding::ASCII_8BIT end it "sets the encoding of a Regexp to the encoding" do index = @s.rb_enc_find_index("UTF-8") - enc = @s.rb_enc_associate_index(/regexp/.dup, index).encoding + enc = @s.rb_enc_associate_index(/regexp/, index).encoding enc.should == Encoding::UTF_8 end it "sets the encoding of a Symbol to the encoding" do index = @s.rb_enc_find_index("UTF-8") - -> { @s.rb_enc_associate_index(:symbol, index) }.should raise_error(RuntimeError) + lambda { @s.rb_enc_associate_index(:symbol, index) }.should raise_error(RuntimeError) end end describe "rb_ascii8bit_encindex" do it "returns an index for the ASCII-8BIT encoding" do - @s.rb_ascii8bit_encindex().should == 0 + @s.rb_ascii8bit_encindex().should >= 0 end end describe "rb_utf8_encindex" do it "returns an index for the UTF-8 encoding" do - @s.rb_utf8_encindex().should == 1 + @s.rb_utf8_encindex().should >= 0 end end describe "rb_usascii_encindex" do it "returns an index for the US-ASCII encoding" do - @s.rb_usascii_encindex().should == 2 + @s.rb_usascii_encindex().should >= 0 end end @@ -608,13 +443,13 @@ describe "C-API Encoding function" do describe "rb_enc_codepoint_len" do it "raises ArgumentError if an empty string is given" do - -> do + lambda do @s.rb_enc_codepoint_len("") end.should raise_error(ArgumentError) end it "raises ArgumentError if an invalid byte sequence is given" do - -> do + lambda do @s.rb_enc_codepoint_len([0xa0, 0xa1].pack('CC').force_encoding('utf-8')) # Invalid sequence identifier end.should raise_error(ArgumentError) end @@ -647,102 +482,4 @@ describe "C-API Encoding function" do length.should == 4 end end - - describe "rb_enc_str_asciionly_p" do - it "returns true for an ASCII string" do - @s.rb_enc_str_asciionly_p("hello").should be_true - end - - it "returns false for a non-ASCII string" do - @s.rb_enc_str_asciionly_p("hüllo").should be_false - end - end - - describe "rb_enc_raise" do - it "forces exception message encoding to the specified one" do - utf_8_incompatible_string = "\x81".b - - -> { - @s.rb_enc_raise(Encoding::UTF_8, RuntimeError, utf_8_incompatible_string) - }.should raise_error { |e| - e.message.encoding.should == Encoding::UTF_8 - e.message.valid_encoding?.should == false - e.message.bytes.should == utf_8_incompatible_string.bytes - } - end - end - - describe "rb_uv_to_utf8" do - it 'converts a Unicode codepoint to a UTF-8 C string' do - str = ' ' * 6 - { - 1 => "\x01", - 0x80 => "\xC2\x80", - 0x800 => "\xE0\xA0\x80", - 0x10000 => "\xF0\x90\x80\x80", - 0x200000 => "\xF8\x88\x80\x80\x80", - 0x4000000 => "\xFC\x84\x80\x80\x80\x80", - }.each do |num, result| - len = @s.rb_uv_to_utf8(str, num) - str.byteslice(0, len).should == result - end - end - end - - describe "rb_enc_left_char_head" do - it 'returns the head position of a character' do - @s.rb_enc_left_char_head("é", 1).should == 0 - @s.rb_enc_left_char_head("éééé", 7).should == 6 - - @s.rb_enc_left_char_head("a", 0).should == 0 - - # unclear if this is intended to work - @s.rb_enc_left_char_head("a", 1).should == 1 - - # Works because for single-byte encodings rb_enc_left_char_head() just returns the pointer - @s.rb_enc_left_char_head("a".force_encoding(Encoding::US_ASCII), 88).should == 88 - @s.rb_enc_left_char_head("a".b, 88).should == 88 - end - end - - describe "ONIGENC_MBC_CASE_FOLD" do - it "returns the correct case fold for the given string" do - @s.ONIGENC_MBC_CASE_FOLD("lower").should == ["l", 1] - @s.ONIGENC_MBC_CASE_FOLD("Upper").should == ["u", 1] - @s.ONIGENC_MBC_CASE_FOLD("ABC"[1..-1]).should == ["b", 1] - end - - it "works with other encodings" do - @s.ONIGENC_MBC_CASE_FOLD("lower".force_encoding("binary")).should == ["l", 1] - @s.ONIGENC_MBC_CASE_FOLD("Upper".force_encoding("binary")).should == ["u", 1] - @s.ONIGENC_MBC_CASE_FOLD("É").should == ["é", 2] - - str, length = @s.ONIGENC_MBC_CASE_FOLD('$'.encode(Encoding::UTF_16BE)) - length.should == 2 - str.bytes.should == [0, 0x24] - end - end - - describe "rb_define_dummy_encoding" do - it "defines the dummy encoding" do - @s.rb_define_dummy_encoding("FOO") - enc = Encoding.find("FOO") - enc.should.dummy? - end - - it "returns the index of the dummy encoding" do - index = @s.rb_define_dummy_encoding("BAR") - index.should == Encoding.list.size - 1 - end - - ruby_version_is "3.2" do - it "raises EncodingError if too many encodings" do - code = <<-RUBY - require #{extension_path.dump} - 1_000.times {|i| CApiEncodingSpecs.new.rb_define_dummy_encoding("R_\#{i}") } - RUBY - ruby_exe(code, args: "2>&1", exit_status: 1).should.include?('too many encoding (> 256) (EncodingError)') - end - end - end end diff --git a/spec/ruby/optional/capi/enumerator_spec.rb b/spec/ruby/optional/capi/enumerator_spec.rb index 9ed68c9063..44634f73a1 100644 --- a/spec/ruby/optional/capi/enumerator_spec.rb +++ b/spec/ruby/optional/capi/enumerator_spec.rb @@ -1,4 +1,4 @@ -require_relative 'spec_helper' +require File.expand_path('../spec_helper', __FILE__) load_extension("enumerator") @@ -36,31 +36,4 @@ describe "C-API Enumerator function" do enumerator.each {} end end - - describe "rb_enumeratorize_with_size" do - - it "enumerates the given object" do - enumerator = @s.rb_enumeratorize_with_size(@enumerable, :each) - enumerated = [] - enumerator.each { |i| enumerated << i } - enumerated.should == @enumerable - end - - it "uses the given method for enumeration" do - enumerator = @s.rb_enumeratorize_with_size(@enumerable, :awesome_each) - @enumerable.should_receive(:awesome_each) - enumerator.each {} - end - - it "passes the given arguments to the enumeration method" do - enumerator = @s.rb_enumeratorize_with_size(@enumerable, :each, :arg1, :arg2) - @enumerable.should_receive(:each).with(:arg1, :arg2) - enumerator.each {} - end - - it "uses the size function to report the size" do - enumerator = @s.rb_enumeratorize_with_size(@enumerable, :each, :arg1, :arg2) - enumerator.size.should == 7 - end - end end diff --git a/spec/ruby/optional/capi/exception_spec.rb b/spec/ruby/optional/capi/exception_spec.rb index 5bb60608b2..4800e6ffc0 100644 --- a/spec/ruby/optional/capi/exception_spec.rb +++ b/spec/ruby/optional/capi/exception_spec.rb @@ -1,4 +1,4 @@ -require_relative 'spec_helper' +require File.expand_path('../spec_helper', __FILE__) load_extension("exception") @@ -28,57 +28,13 @@ describe "C-API Exception function" do describe "rb_exc_raise" do it "raises passed exception" do runtime_error = RuntimeError.new '42' - -> { @s.rb_exc_raise(runtime_error) }.should raise_error(RuntimeError, '42') + lambda { @s.rb_exc_raise(runtime_error) }.should raise_error(RuntimeError, '42') end it "raises an exception with an empty backtrace" do runtime_error = RuntimeError.new '42' runtime_error.set_backtrace [] - -> { @s.rb_exc_raise(runtime_error) }.should raise_error(RuntimeError, '42') - end - - it "sets $! to the raised exception when not rescuing from an another exception" do - runtime_error = RuntimeError.new '42' - runtime_error.set_backtrace [] - begin - @s.rb_exc_raise(runtime_error) - rescue - $!.should == runtime_error - end - end - - it "sets $! to the raised exception when $! when rescuing from an another exception" do - runtime_error = RuntimeError.new '42' - runtime_error.set_backtrace [] - begin - begin - raise StandardError - rescue - @s.rb_exc_raise(runtime_error) - end - rescue - $!.should == runtime_error - end - end - end - - describe "rb_errinfo" do - it "is cleared when entering a C method" do - begin - raise StandardError - rescue - $!.class.should == StandardError - @s.rb_errinfo().should == nil - end - end - - it "does not clear $! in the calling method" do - begin - raise StandardError - rescue - @s.rb_errinfo() - $!.class.should == StandardError - end + lambda { @s.rb_exc_raise(runtime_error) }.should raise_error(RuntimeError, '42') end end @@ -96,86 +52,7 @@ describe "C-API Exception function" do end it "raises a TypeError if the object is not nil or an Exception instance" do - -> { @s.rb_set_errinfo("error") }.should raise_error(TypeError) - end - end - - describe "rb_syserr_new" do - it "returns system error with default message when passed message is NULL" do - exception = @s.rb_syserr_new(Errno::ENOENT::Errno, nil) - exception.class.should == Errno::ENOENT - exception.message.should include("No such file or directory") - exception.should.is_a?(SystemCallError) - end - - it "returns system error with custom message" do - exception = @s.rb_syserr_new(Errno::ENOENT::Errno, "custom message") - - exception.message.should include("custom message") - exception.class.should == Errno::ENOENT - exception.should.is_a?(SystemCallError) - end - end - - describe "rb_syserr_new_str" do - it "returns system error with default message when passed message is nil" do - exception = @s.rb_syserr_new_str(Errno::ENOENT::Errno, nil) - - exception.message.should include("No such file or directory") - exception.class.should == Errno::ENOENT - exception.should.is_a?(SystemCallError) - end - - it "returns system error with custom message" do - exception = @s.rb_syserr_new_str(Errno::ENOENT::Errno, "custom message") - exception.message.should include("custom message") - exception.class.should == Errno::ENOENT - exception.should.is_a?(SystemCallError) - end - end - - describe "rb_make_exception" do - it "returns a RuntimeError when given a String argument" do - e = @s.rb_make_exception(["Message"]) - e.class.should == RuntimeError - e.message.should == "Message" - end - - it "returns the exception when given an Exception argument" do - exc = Exception.new - e = @s.rb_make_exception([exc]) - e.should == exc - end - - it "returns the exception with the given class and message" do - e = @s.rb_make_exception([Exception, "Message"]) - e.class.should == Exception - e.message.should == "Message" - end - - it "returns the exception with the given class, message, and backtrace" do - e = @s.rb_make_exception([Exception, "Message", ["backtrace 1"]]) - e.class.should == Exception - e.message.should == "Message" - e.backtrace.should == ["backtrace 1"] - end - - it "raises a TypeError for incorrect types" do - -> { @s.rb_make_exception([nil]) }.should raise_error(TypeError) - -> { @s.rb_make_exception([Object.new]) }.should raise_error(TypeError) - obj = Object.new - def obj.exception - "not exception type" - end - -> { @s.rb_make_exception([obj]) }.should raise_error(TypeError) - end - - it "raises an ArgumentError for too many arguments" do - -> { @s.rb_make_exception([Exception, "Message", ["backtrace 1"], "extra"]) }.should raise_error(ArgumentError) - end - - it "returns nil for empty arguments" do - @s.rb_make_exception([]).should == nil + lambda { @s.rb_set_errinfo("error") }.should raise_error(TypeError) end end end diff --git a/spec/ruby/optional/capi/ext/array_spec.c b/spec/ruby/optional/capi/ext/array_spec.c index 2347798bb4..8bc144195c 100644 --- a/spec/ruby/optional/capi/ext/array_spec.c +++ b/spec/ruby/optional/capi/ext/array_spec.c @@ -7,10 +7,13 @@ extern "C" { #endif +#ifdef HAVE_RB_ARRAY static VALUE array_spec_rb_Array(VALUE self, VALUE object) { return rb_Array(object); } +#endif +#if defined(HAVE_RARRAY_LEN) && defined(HAVE_RARRAY_PTR) static VALUE array_spec_RARRAY_PTR_iterate(VALUE self, VALUE array) { int i; VALUE* ptr; @@ -32,93 +35,109 @@ static VALUE array_spec_RARRAY_PTR_assign(VALUE self, VALUE array, VALUE value) } return Qnil; } +#endif - -static VALUE array_spec_RARRAY_PTR_memcpy(VALUE self, VALUE array1, VALUE array2) { - VALUE *ptr1, *ptr2; - long size; - size = RARRAY_LEN(array1); - ptr1 = RARRAY_PTR(array1); - ptr2 = RARRAY_PTR(array2); - if (ptr1 != NULL && ptr2 != NULL) { - memcpy(ptr2, ptr1, size * sizeof(VALUE)); - } - return Qnil; -} - +#ifdef HAVE_RARRAY_LEN static VALUE array_spec_RARRAY_LEN(VALUE self, VALUE array) { return INT2FIX(RARRAY_LEN(array)); } +#endif +#ifdef HAVE_RARRAY_AREF static VALUE array_spec_RARRAY_AREF(VALUE self, VALUE array, VALUE index) { return RARRAY_AREF(array, FIX2INT(index)); } +#endif -static VALUE array_spec_RARRAY_ASET(VALUE self, VALUE array, VALUE index, VALUE value) { - RARRAY_ASET(array, FIX2INT(index), value); - return value; -} - +#ifdef HAVE_RB_ARY_AREF static VALUE array_spec_rb_ary_aref(int argc, VALUE *argv, VALUE self) { VALUE ary, args; rb_scan_args(argc, argv, "1*", &ary, &args); return rb_ary_aref((int)RARRAY_LEN(args), RARRAY_PTR(args), ary); } +#endif +#ifdef HAVE_RB_ARY_CLEAR static VALUE array_spec_rb_ary_clear(VALUE self, VALUE array) { return rb_ary_clear(array); } +#endif +#ifdef HAVE_RB_ARY_DELETE static VALUE array_spec_rb_ary_delete(VALUE self, VALUE array, VALUE item) { return rb_ary_delete(array, item); } +#endif +#ifdef HAVE_RB_ARY_DELETE_AT static VALUE array_spec_rb_ary_delete_at(VALUE self, VALUE array, VALUE index) { return rb_ary_delete_at(array, NUM2LONG(index)); } +#endif +#ifdef HAVE_RB_ARY_DUP static VALUE array_spec_rb_ary_dup(VALUE self, VALUE array) { return rb_ary_dup(array); } +#endif +#ifdef HAVE_RB_ARY_ENTRY static VALUE array_spec_rb_ary_entry(VALUE self, VALUE array, VALUE offset) { return rb_ary_entry(array, FIX2INT(offset)); } +#endif +#ifdef HAVE_RB_ARY_INCLUDES static VALUE array_spec_rb_ary_includes(VALUE self, VALUE ary, VALUE item) { return rb_ary_includes(ary, item); } +#endif +#ifdef HAVE_RB_ARY_JOIN static VALUE array_spec_rb_ary_join(VALUE self, VALUE array1, VALUE array2) { return rb_ary_join(array1, array2); } +#endif +#ifdef HAVE_RB_ARY_TO_S static VALUE array_spec_rb_ary_to_s(VALUE self, VALUE array) { return rb_ary_to_s(array); } +#endif +#ifdef HAVE_RB_ARY_NEW static VALUE array_spec_rb_ary_new(VALUE self) { VALUE ret; ret = rb_ary_new(); return ret; } +#endif +#ifdef HAVE_RB_ARY_NEW2 static VALUE array_spec_rb_ary_new2(VALUE self, VALUE length) { return rb_ary_new2(NUM2LONG(length)); } +#endif +#ifdef HAVE_RB_ARY_NEW_CAPA static VALUE array_spec_rb_ary_new_capa(VALUE self, VALUE length) { return rb_ary_new_capa(NUM2LONG(length)); } +#endif +#ifdef HAVE_RB_ARY_NEW3 static VALUE array_spec_rb_ary_new3(VALUE self, VALUE first, VALUE second, VALUE third) { return rb_ary_new3(3, first, second, third); } +#endif +#ifdef HAVE_RB_ARY_NEW_FROM_ARGS static VALUE array_spec_rb_ary_new_from_args(VALUE self, VALUE first, VALUE second, VALUE third) { return rb_ary_new_from_args(3, first, second, third); } +#endif +#ifdef HAVE_RB_ARY_NEW4 static VALUE array_spec_rb_ary_new4(VALUE self, VALUE first, VALUE second, VALUE third) { VALUE values[3]; values[0] = first; @@ -126,7 +145,9 @@ static VALUE array_spec_rb_ary_new4(VALUE self, VALUE first, VALUE second, VALUE values[2] = third; return rb_ary_new4(3, values); } +#endif +#ifdef HAVE_RB_ARY_NEW_FROM_VALUES static VALUE array_spec_rb_ary_new_from_values(VALUE self, VALUE first, VALUE second, VALUE third) { VALUE values[3]; values[0] = first; @@ -134,65 +155,81 @@ static VALUE array_spec_rb_ary_new_from_values(VALUE self, VALUE first, VALUE se values[2] = third; return rb_ary_new_from_values(3, values); } +#endif +#ifdef HAVE_RB_ARY_POP static VALUE array_spec_rb_ary_pop(VALUE self, VALUE array) { return rb_ary_pop(array); } +#endif +#ifdef HAVE_RB_ARY_PUSH static VALUE array_spec_rb_ary_push(VALUE self, VALUE array, VALUE item) { rb_ary_push(array, item); return array; } +#endif +#ifdef HAVE_RB_ARY_CAT static VALUE array_spec_rb_ary_cat(int argc, VALUE *argv, VALUE self) { VALUE ary, args; rb_scan_args(argc, argv, "1*", &ary, &args); return rb_ary_cat(ary, RARRAY_PTR(args), RARRAY_LEN(args)); } +#endif +#ifdef HAVE_RB_ARY_REVERSE static VALUE array_spec_rb_ary_reverse(VALUE self, VALUE array) { return rb_ary_reverse(array); } +#endif +#ifdef HAVE_RB_ARY_ROTATE static VALUE array_spec_rb_ary_rotate(VALUE self, VALUE array, VALUE count) { return rb_ary_rotate(array, NUM2LONG(count)); } +#endif +#ifdef HAVE_RB_ARY_SHIFT static VALUE array_spec_rb_ary_shift(VALUE self, VALUE array) { return rb_ary_shift(array); } +#endif -static VALUE array_spec_rb_ary_sort(VALUE self, VALUE array) { - return rb_ary_sort(array); -} - -static VALUE array_spec_rb_ary_sort_bang(VALUE self, VALUE array) { - return rb_ary_sort_bang(array); -} - +#ifdef HAVE_RB_ARY_STORE static VALUE array_spec_rb_ary_store(VALUE self, VALUE array, VALUE offset, VALUE value) { rb_ary_store(array, FIX2INT(offset), value); return Qnil; } +#endif +#ifdef HAVE_RB_ARY_CONCAT static VALUE array_spec_rb_ary_concat(VALUE self, VALUE array1, VALUE array2) { return rb_ary_concat(array1, array2); } +#endif +#ifdef HAVE_RB_ARY_PLUS static VALUE array_spec_rb_ary_plus(VALUE self, VALUE array1, VALUE array2) { return rb_ary_plus(array1, array2); } +#endif +#ifdef HAVE_RB_ARY_UNSHIFT static VALUE array_spec_rb_ary_unshift(VALUE self, VALUE array, VALUE val) { return rb_ary_unshift(array, val); } +#endif +#ifdef HAVE_RB_ASSOC_NEW static VALUE array_spec_rb_assoc_new(VALUE self, VALUE first, VALUE second) { return rb_assoc_new(first, second); } +#endif -static VALUE copy_ary(RB_BLOCK_CALL_FUNC_ARGLIST(el, new_ary)) { +#if defined(HAVE_RB_ITERATE) && defined(HAVE_RB_EACH) +static VALUE copy_ary(VALUE el, VALUE new_ary) { return rb_ary_push(new_ary, el); } @@ -204,15 +241,7 @@ static VALUE array_spec_rb_iterate(VALUE self, VALUE ary) { return new_ary; } -static VALUE array_spec_rb_block_call(VALUE self, VALUE ary) { - VALUE new_ary = rb_ary_new(); - - rb_block_call(ary, rb_intern("each"), 0, 0, copy_ary, new_ary); - - return new_ary; -} - -static VALUE sub_pair(RB_BLOCK_CALL_FUNC_ARGLIST(el, holder)) { +static VALUE sub_pair(VALUE el, VALUE holder) { return rb_ary_push(holder, rb_ary_entry(el, 1)); } @@ -228,15 +257,7 @@ static VALUE array_spec_rb_iterate_each_pair(VALUE self, VALUE obj) { return new_ary; } -static VALUE array_spec_rb_block_call_each_pair(VALUE self, VALUE obj) { - VALUE new_ary = rb_ary_new(); - - rb_block_call(obj, rb_intern("each_pair"), 0, 0, sub_pair, new_ary); - - return new_ary; -} - -static VALUE iter_yield(RB_BLOCK_CALL_FUNC_ARGLIST(el, ary)) { +static VALUE iter_yield(VALUE el, VALUE ary) { rb_yield(el); return Qnil; } @@ -245,79 +266,185 @@ static VALUE array_spec_rb_iterate_then_yield(VALUE self, VALUE obj) { rb_iterate(rb_each, obj, iter_yield, obj); return Qnil; } +#endif -static VALUE array_spec_rb_block_call_then_yield(VALUE self, VALUE obj) { - rb_block_call(obj, rb_intern("each"), 0, 0, iter_yield, obj); - return Qnil; -} - +#if defined(HAVE_RB_MEM_CLEAR) static VALUE array_spec_rb_mem_clear(VALUE self, VALUE obj) { VALUE ary[1]; ary[0] = obj; rb_mem_clear(ary, 1); return ary[0]; } +#endif +#ifdef HAVE_RB_ARY_FREEZE static VALUE array_spec_rb_ary_freeze(VALUE self, VALUE ary) { return rb_ary_freeze(ary); } +#endif +#ifdef HAVE_RB_ARY_TO_ARY static VALUE array_spec_rb_ary_to_ary(VALUE self, VALUE ary) { return rb_ary_to_ary(ary); } +#endif +#ifdef HAVE_RB_ARY_SUBSEQ static VALUE array_spec_rb_ary_subseq(VALUE self, VALUE ary, VALUE begin, VALUE len) { return rb_ary_subseq(ary, FIX2LONG(begin), FIX2LONG(len)); } +#endif void Init_array_spec(void) { - VALUE cls = rb_define_class("CApiArraySpecs", rb_cObject); + VALUE cls; + cls = rb_define_class("CApiArraySpecs", rb_cObject); + +#ifdef HAVE_RB_ARRAY rb_define_method(cls, "rb_Array", array_spec_rb_Array, 1); +#endif + +#ifdef HAVE_RARRAY_LEN rb_define_method(cls, "RARRAY_LEN", array_spec_RARRAY_LEN, 1); +#endif + +#if defined(HAVE_RARRAY_LEN) && defined(HAVE_RARRAY_PTR) rb_define_method(cls, "RARRAY_PTR_iterate", array_spec_RARRAY_PTR_iterate, 1); rb_define_method(cls, "RARRAY_PTR_assign", array_spec_RARRAY_PTR_assign, 2); - rb_define_method(cls, "RARRAY_PTR_memcpy", array_spec_RARRAY_PTR_memcpy, 2); +#endif + +#ifdef HAVE_RARRAY_AREF rb_define_method(cls, "RARRAY_AREF", array_spec_RARRAY_AREF, 2); - rb_define_method(cls, "RARRAY_ASET", array_spec_RARRAY_ASET, 3); +#endif + +#ifdef HAVE_RB_ARY_AREF rb_define_method(cls, "rb_ary_aref", array_spec_rb_ary_aref, -1); +#endif + +#ifdef HAVE_RB_ARY_CLEAR rb_define_method(cls, "rb_ary_clear", array_spec_rb_ary_clear, 1); +#endif + +#ifdef HAVE_RB_ARY_DELETE rb_define_method(cls, "rb_ary_delete", array_spec_rb_ary_delete, 2); +#endif + +#ifdef HAVE_RB_ARY_DELETE_AT rb_define_method(cls, "rb_ary_delete_at", array_spec_rb_ary_delete_at, 2); +#endif + +#ifdef HAVE_RB_ARY_DUP rb_define_method(cls, "rb_ary_dup", array_spec_rb_ary_dup, 1); +#endif + +#ifdef HAVE_RB_ARY_ENTRY rb_define_method(cls, "rb_ary_entry", array_spec_rb_ary_entry, 2); +#endif + +#ifdef HAVE_RB_ARY_INCLUDES rb_define_method(cls, "rb_ary_includes", array_spec_rb_ary_includes, 2); +#endif + +#ifdef HAVE_RB_ARY_JOIN rb_define_method(cls, "rb_ary_join", array_spec_rb_ary_join, 2); +#endif + +#ifdef HAVE_RB_ARY_TO_S rb_define_method(cls, "rb_ary_to_s", array_spec_rb_ary_to_s, 1); +#endif + +#ifdef HAVE_RB_ARY_NEW rb_define_method(cls, "rb_ary_new", array_spec_rb_ary_new, 0); +#endif + +#ifdef HAVE_RB_ARY_NEW2 rb_define_method(cls, "rb_ary_new2", array_spec_rb_ary_new2, 1); +#endif + +#ifdef HAVE_RB_ARY_NEW_CAPA rb_define_method(cls, "rb_ary_new_capa", array_spec_rb_ary_new_capa, 1); +#endif + +#ifdef HAVE_RB_ARY_NEW3 rb_define_method(cls, "rb_ary_new3", array_spec_rb_ary_new3, 3); +#endif + +#ifdef HAVE_RB_ARY_NEW_FROM_ARGS rb_define_method(cls, "rb_ary_new_from_args", array_spec_rb_ary_new_from_args, 3); +#endif + +#ifdef HAVE_RB_ARY_NEW4 rb_define_method(cls, "rb_ary_new4", array_spec_rb_ary_new4, 3); +#endif + +#ifdef HAVE_RB_ARY_NEW_FROM_VALUES rb_define_method(cls, "rb_ary_new_from_values", array_spec_rb_ary_new_from_values, 3); +#endif + +#ifdef HAVE_RB_ARY_POP rb_define_method(cls, "rb_ary_pop", array_spec_rb_ary_pop, 1); +#endif + +#ifdef HAVE_RB_ARY_PUSH rb_define_method(cls, "rb_ary_push", array_spec_rb_ary_push, 2); +#endif + +#ifdef HAVE_RB_ARY_CAT rb_define_method(cls, "rb_ary_cat", array_spec_rb_ary_cat, -1); +#endif + +#ifdef HAVE_RB_ARY_REVERSE rb_define_method(cls, "rb_ary_reverse", array_spec_rb_ary_reverse, 1); +#endif + +#ifdef HAVE_RB_ARY_ROTATE rb_define_method(cls, "rb_ary_rotate", array_spec_rb_ary_rotate, 2); +#endif + +#ifdef HAVE_RB_ARY_SHIFT rb_define_method(cls, "rb_ary_shift", array_spec_rb_ary_shift, 1); - rb_define_method(cls, "rb_ary_sort", array_spec_rb_ary_sort, 1); - rb_define_method(cls, "rb_ary_sort_bang", array_spec_rb_ary_sort_bang, 1); +#endif + +#ifdef HAVE_RB_ARY_STORE rb_define_method(cls, "rb_ary_store", array_spec_rb_ary_store, 3); +#endif + +#ifdef HAVE_RB_ARY_CONCAT rb_define_method(cls, "rb_ary_concat", array_spec_rb_ary_concat, 2); +#endif + +#ifdef HAVE_RB_ARY_PLUS rb_define_method(cls, "rb_ary_plus", array_spec_rb_ary_plus, 2); +#endif + +#ifdef HAVE_RB_ARY_UNSHIFT rb_define_method(cls, "rb_ary_unshift", array_spec_rb_ary_unshift, 2); +#endif + +#ifdef HAVE_RB_ASSOC_NEW rb_define_method(cls, "rb_assoc_new", array_spec_rb_assoc_new, 2); +#endif + +#if defined(HAVE_RB_ITERATE) && defined(HAVE_RB_EACH) rb_define_method(cls, "rb_iterate", array_spec_rb_iterate, 1); rb_define_method(cls, "rb_iterate_each_pair", array_spec_rb_iterate_each_pair, 1); rb_define_method(cls, "rb_iterate_then_yield", array_spec_rb_iterate_then_yield, 1); - rb_define_method(cls, "rb_block_call", array_spec_rb_block_call, 1); - rb_define_method(cls, "rb_block_call_each_pair", array_spec_rb_block_call_each_pair, 1); - rb_define_method(cls, "rb_block_call_then_yield", array_spec_rb_block_call_then_yield, 1); +#endif + +#if defined(HAVE_RB_MEM_CLEAR) rb_define_method(cls, "rb_mem_clear", array_spec_rb_mem_clear, 1); +#endif + +#ifdef HAVE_RB_ARY_FREEZE rb_define_method(cls, "rb_ary_freeze", array_spec_rb_ary_freeze, 1); +#endif + +#ifdef HAVE_RB_ARY_TO_ARY rb_define_method(cls, "rb_ary_to_ary", array_spec_rb_ary_to_ary, 1); +#endif + +#ifdef HAVE_RB_ARY_SUBSEQ rb_define_method(cls, "rb_ary_subseq", array_spec_rb_ary_subseq, 3); +#endif } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/basic_object_spec.c b/spec/ruby/optional/capi/ext/basic_object_spec.c deleted file mode 100644 index 1618670ceb..0000000000 --- a/spec/ruby/optional/capi/ext/basic_object_spec.c +++ /dev/null @@ -1,19 +0,0 @@ -#include "ruby.h" -#include "rubyspec.h" - -#ifdef __cplusplus -extern "C" { -#endif - -static VALUE basic_object_spec_RBASIC_CLASS(VALUE self, VALUE obj) { - return RBASIC_CLASS(obj); -} - -void Init_basic_object_spec(void) { - VALUE cls = rb_define_class("CApiBasicObjectSpecs", rb_cObject); - rb_define_method(cls, "RBASIC_CLASS", basic_object_spec_RBASIC_CLASS, 1); -} - -#ifdef __cplusplus -} -#endif diff --git a/spec/ruby/optional/capi/ext/bignum_spec.c b/spec/ruby/optional/capi/ext/bignum_spec.c index a950d8b16f..ab3b36eadc 100644 --- a/spec/ruby/optional/capi/ext/bignum_spec.c +++ b/spec/ruby/optional/capi/ext/bignum_spec.c @@ -5,40 +5,51 @@ extern "C" { #endif +#ifdef HAVE_RB_BIG2DBL static VALUE bignum_spec_rb_big2dbl(VALUE self, VALUE num) { return rb_float_new(rb_big2dbl(num)); } +#endif +#ifdef HAVE_RB_DBL2BIG static VALUE bignum_spec_rb_dbl2big(VALUE self, VALUE num) { double dnum = NUM2DBL(num); return rb_dbl2big(dnum); } +#endif +#ifdef HAVE_RB_BIG2LL static VALUE bignum_spec_rb_big2ll(VALUE self, VALUE num) { return rb_ll2inum(rb_big2ll(num)); } +#endif +#ifdef HAVE_RB_BIG2LONG static VALUE bignum_spec_rb_big2long(VALUE self, VALUE num) { return LONG2NUM(rb_big2long(num)); } +#endif +#ifdef HAVE_RB_BIG2STR static VALUE bignum_spec_rb_big2str(VALUE self, VALUE num, VALUE base) { return rb_big2str(num, FIX2INT(base)); } +#endif +#ifdef HAVE_RB_BIG2ULONG static VALUE bignum_spec_rb_big2ulong(VALUE self, VALUE num) { return ULONG2NUM(rb_big2ulong(num)); } +#endif -static VALUE bignum_spec_RBIGNUM_SIGN(VALUE self, VALUE val) { - return INT2FIX(RBIGNUM_SIGN(val)); -} - +#ifdef HAVE_RB_BIG_CMP static VALUE bignum_spec_rb_big_cmp(VALUE self, VALUE x, VALUE y) { return rb_big_cmp(x, y); } +#endif +#ifdef HAVE_RB_BIG_PACK static VALUE bignum_spec_rb_big_pack(VALUE self, VALUE val) { unsigned long buff; @@ -46,7 +57,9 @@ static VALUE bignum_spec_rb_big_pack(VALUE self, VALUE val) { return ULONG2NUM(buff); } +#endif +#if HAVE_ABSINT_SIZE static VALUE bignum_spec_rb_big_pack_length(VALUE self, VALUE val) { long long_len; int leading_bits = 0; @@ -59,13 +72,15 @@ static VALUE bignum_spec_rb_big_pack_length(VALUE self, VALUE val) { long_len = len / divisor + ((len % divisor == 0) ? 0 : 1); return LONG2NUM(long_len); } +#endif +#ifdef HAVE_RB_BIG_PACK static VALUE bignum_spec_rb_big_pack_array(VALUE self, VALUE val, VALUE len) { int i; long long_len = NUM2LONG(len); VALUE ary = rb_ary_new_capa(long_len); - unsigned long *buf = (unsigned long*) malloc(long_len * SIZEOF_LONG); + unsigned long *buf = malloc(long_len * SIZEOF_LONG); /* The array should be filled with recognisable junk so we can check it is all cleared properly. */ @@ -85,22 +100,50 @@ static VALUE bignum_spec_rb_big_pack_array(VALUE self, VALUE val, VALUE len) { free(buf); return ary; } +#endif void Init_bignum_spec(void) { - VALUE cls = rb_define_class("CApiBignumSpecs", rb_cObject); + VALUE cls; + cls = rb_define_class("CApiBignumSpecs", rb_cObject); + +#ifdef HAVE_RB_BIG2DBL rb_define_method(cls, "rb_big2dbl", bignum_spec_rb_big2dbl, 1); +#endif + +#ifdef HAVE_RB_DBL2BIG rb_define_method(cls, "rb_dbl2big", bignum_spec_rb_dbl2big, 1); +#endif + +#ifdef HAVE_RB_BIG2LL rb_define_method(cls, "rb_big2ll", bignum_spec_rb_big2ll, 1); +#endif + +#ifdef HAVE_RB_BIG2LONG rb_define_method(cls, "rb_big2long", bignum_spec_rb_big2long, 1); +#endif + +#ifdef HAVE_RB_BIG2STR rb_define_method(cls, "rb_big2str", bignum_spec_rb_big2str, 2); +#endif + +#ifdef HAVE_RB_BIG2ULONG rb_define_method(cls, "rb_big2ulong", bignum_spec_rb_big2ulong, 1); - rb_define_method(cls, "RBIGNUM_SIGN", bignum_spec_RBIGNUM_SIGN, 1); +#endif + +#ifdef HAVE_RB_BIG_CMP rb_define_method(cls, "rb_big_cmp", bignum_spec_rb_big_cmp, 2); +#endif + +#ifdef HAVE_RB_BIG_PACK rb_define_method(cls, "rb_big_pack", bignum_spec_rb_big_pack, 1); rb_define_method(cls, "rb_big_pack_array", bignum_spec_rb_big_pack_array, 2); +#endif + +#ifdef HAVE_ABSINT_SIZE rb_define_method(cls, "rb_big_pack_length", bignum_spec_rb_big_pack_length, 1); +#endif } #ifdef __cplusplus -} +extern "C" { #endif diff --git a/spec/ruby/optional/capi/ext/binding_spec.c b/spec/ruby/optional/capi/ext/binding_spec.c deleted file mode 100644 index b2e3c88b6d..0000000000 --- a/spec/ruby/optional/capi/ext/binding_spec.c +++ /dev/null @@ -1,19 +0,0 @@ -#include "ruby.h" -#include "rubyspec.h" - -#ifdef __cplusplus -extern "C" { -#endif - -static VALUE binding_spec_get_binding(VALUE self) { - return rb_funcall(self, rb_intern("binding"), 0); -} - -void Init_binding_spec(void) { - VALUE cls = rb_define_class("CApiBindingSpecs", rb_cObject); - rb_define_method(cls, "get_binding", binding_spec_get_binding, 0); -} - -#ifdef __cplusplus -} -#endif diff --git a/spec/ruby/optional/capi/ext/boolean_spec.c b/spec/ruby/optional/capi/ext/boolean_spec.c index 081cffa103..94d79c0fc0 100644 --- a/spec/ruby/optional/capi/ext/boolean_spec.c +++ b/spec/ruby/optional/capi/ext/boolean_spec.c @@ -22,12 +22,13 @@ static VALUE boolean_spec_q_false(VALUE self) { } void Init_boolean_spec(void) { - VALUE cls = rb_define_class("CApiBooleanSpecs", rb_cObject); + VALUE cls; + cls = rb_define_class("CApiBooleanSpecs", rb_cObject); rb_define_method(cls, "is_true", boolean_spec_is_true, 1); rb_define_method(cls, "q_true", boolean_spec_q_true, 0); rb_define_method(cls, "q_false", boolean_spec_q_false, 0); } #ifdef __cplusplus -} +extern "C" { #endif diff --git a/spec/ruby/optional/capi/ext/class_id_under_autoload_spec.c b/spec/ruby/optional/capi/ext/class_id_under_autoload_spec.c index cc5550f041..64393a9397 100644 --- a/spec/ruby/optional/capi/ext/class_id_under_autoload_spec.c +++ b/spec/ruby/optional/capi/ext/class_id_under_autoload_spec.c @@ -1,13 +1,5 @@ #include "ruby.h" -#ifdef __cplusplus -extern "C" { -#endif - void Init_class_id_under_autoload_spec(void) { rb_define_class_id_under(rb_cObject, rb_intern("ClassIdUnderAutoload"), rb_cObject); } - -#ifdef __cplusplus -} -#endif diff --git a/spec/ruby/optional/capi/ext/class_spec.c b/spec/ruby/optional/capi/ext/class_spec.c index c13f02ecf2..e3860df1da 100644 --- a/spec/ruby/optional/capi/ext/class_spec.c +++ b/spec/ruby/optional/capi/ext/class_spec.c @@ -8,6 +8,7 @@ extern "C" { #endif +#ifdef HAVE_RB_CALL_SUPER static VALUE class_spec_call_super_method(VALUE self) { return rb_call_super(0, 0); } @@ -16,101 +17,110 @@ static VALUE class_spec_define_call_super_method(VALUE self, VALUE obj, VALUE st rb_define_method(obj, RSTRING_PTR(str_name), class_spec_call_super_method, 0); return Qnil; } +#endif +#ifdef HAVE_RB_CLASS_PATH static VALUE class_spec_rb_class_path(VALUE self, VALUE klass) { return rb_class_path(klass); } +#endif +#ifdef HAVE_RB_CLASS_NAME static VALUE class_spec_rb_class_name(VALUE self, VALUE klass) { return rb_class_name(klass); } +#endif +#ifdef HAVE_RB_CLASS2NAME static VALUE class_spec_rb_class2name(VALUE self, VALUE klass) { return rb_str_new2( rb_class2name(klass) ); } +#endif +#ifdef HAVE_RB_PATH2CLASS static VALUE class_spec_rb_path2class(VALUE self, VALUE path) { return rb_path2class(RSTRING_PTR(path)); } +#endif +#ifdef HAVE_RB_PATH_TO_CLASS static VALUE class_spec_rb_path_to_class(VALUE self, VALUE path) { return rb_path_to_class(path); } +#endif -static VALUE class_spec_rb_class_instance_methods(int argc, VALUE* argv, VALUE self) { - VALUE mod = argv[0]; - return rb_class_instance_methods(--argc, ++argv, mod); -} - -static VALUE class_spec_rb_class_public_instance_methods(int argc, VALUE* argv, VALUE self) { - VALUE mod = argv[0]; - return rb_class_public_instance_methods(--argc, ++argv, mod); -} - -static VALUE class_spec_rb_class_protected_instance_methods(int argc, VALUE* argv, VALUE self) { - VALUE mod = argv[0]; - return rb_class_protected_instance_methods(--argc, ++argv, mod); -} - -static VALUE class_spec_rb_class_private_instance_methods(int argc, VALUE* argv, VALUE self) { - VALUE mod = argv[0]; - return rb_class_private_instance_methods(--argc, ++argv, mod); -} - +#ifdef HAVE_RB_CLASS_NEW static VALUE class_spec_rb_class_new(VALUE self, VALUE super) { return rb_class_new(super); } +#endif -static VALUE class_spec_rb_class_new_instance(VALUE self, VALUE args, VALUE klass) { - return rb_class_new_instance(RARRAY_LENINT(args), RARRAY_PTR(args), klass); -} +#ifdef HAVE_RB_CLASS_NEW_INSTANCE +static VALUE class_spec_rb_class_new_instance(VALUE self, + VALUE nargs, VALUE args, + VALUE klass) { + int c_nargs = FIX2INT(nargs); + VALUE *c_args = alloca(sizeof(VALUE) * c_nargs); + int i; + + for (i = 0; i < c_nargs; i++) + c_args[i] = rb_ary_entry(args, i); -#ifdef RUBY_VERSION_IS_3_0 -static VALUE class_spec_rb_class_new_instance_kw(VALUE self, VALUE args, VALUE klass) { - return rb_class_new_instance_kw(RARRAY_LENINT(args), RARRAY_PTR(args), klass, RB_PASS_KEYWORDS); + return rb_class_new_instance(c_nargs, c_args, klass); } #endif +#ifdef HAVE_RB_CLASS_REAL static VALUE class_spec_rb_class_real(VALUE self, VALUE object) { - if (rb_type_p(object, T_FIXNUM)) { + if(rb_type_p(object, T_FIXNUM)) { return INT2FIX(rb_class_real(FIX2INT(object))); } else { return rb_class_real(CLASS_OF(object)); } } +#endif -static VALUE class_spec_rb_class_get_superclass(VALUE self, VALUE klass) { - return rb_class_get_superclass(klass); -} - +#ifdef HAVE_RB_CLASS_SUPERCLASS static VALUE class_spec_rb_class_superclass(VALUE self, VALUE klass) { return rb_class_superclass(klass); } +#endif +#ifdef HAVE_RB_CVAR_DEFINED static VALUE class_spec_cvar_defined(VALUE self, VALUE klass, VALUE id) { ID as_id = rb_intern(StringValuePtr(id)); return rb_cvar_defined(klass, as_id); } +#endif +#ifdef HAVE_RB_CVAR_GET static VALUE class_spec_cvar_get(VALUE self, VALUE klass, VALUE name) { return rb_cvar_get(klass, rb_intern(StringValuePtr(name))); } +#endif +#ifdef HAVE_RB_CVAR_SET static VALUE class_spec_cvar_set(VALUE self, VALUE klass, VALUE name, VALUE val) { rb_cvar_set(klass, rb_intern(StringValuePtr(name)), val); return Qnil; } +#endif +#ifdef HAVE_RB_CV_GET static VALUE class_spec_cv_get(VALUE self, VALUE klass, VALUE name) { return rb_cv_get(klass, StringValuePtr(name)); } +#endif +#ifdef HAVE_RB_CV_SET static VALUE class_spec_cv_set(VALUE self, VALUE klass, VALUE name, VALUE val) { rb_cv_set(klass, StringValuePtr(name), val); return Qnil; } +#endif +#ifdef HAVE_RB_DEFINE_ATTR VALUE class_spec_define_attr(VALUE self, VALUE klass, VALUE sym, VALUE read, VALUE write) { int int_read, int_write; int_read = read == Qtrue ? 1 : 0; @@ -118,65 +128,132 @@ VALUE class_spec_define_attr(VALUE self, VALUE klass, VALUE sym, VALUE read, VAL rb_define_attr(klass, rb_id2name(SYM2ID(sym)), int_read, int_write); return Qnil; } +#endif +#ifdef HAVE_RB_DEFINE_CLASS static VALUE class_spec_rb_define_class(VALUE self, VALUE name, VALUE super) { - if (NIL_P(super)) super = 0; + if(NIL_P(super)) super = 0; return rb_define_class(RSTRING_PTR(name), super); } +#endif +#ifdef HAVE_RB_DEFINE_CLASS_UNDER static VALUE class_spec_rb_define_class_under(VALUE self, VALUE outer, VALUE name, VALUE super) { - if (NIL_P(super)) super = 0; + if(NIL_P(super)) super = 0; return rb_define_class_under(outer, RSTRING_PTR(name), super); } +#endif +#ifdef HAVE_RB_DEFINE_CLASS_ID_UNDER static VALUE class_spec_rb_define_class_id_under(VALUE self, VALUE outer, VALUE name, VALUE super) { - if (NIL_P(super)) super = 0; + if(NIL_P(super)) super = 0; return rb_define_class_id_under(outer, SYM2ID(name), super); } +#endif +#ifdef HAVE_RB_DEFINE_CLASS_VARIABLE static VALUE class_spec_define_class_variable(VALUE self, VALUE klass, VALUE name, VALUE val) { rb_define_class_variable(klass, StringValuePtr(name), val); return Qnil; } +#endif +#ifdef HAVE_RB_INCLUDE_MODULE static VALUE class_spec_include_module(VALUE self, VALUE klass, VALUE module) { rb_include_module(klass, module); return klass; } +#endif void Init_class_spec(void) { - VALUE cls = rb_define_class("CApiClassSpecs", rb_cObject); + VALUE cls; + cls = rb_define_class("CApiClassSpecs", rb_cObject); + +#ifdef HAVE_RB_CALL_SUPER rb_define_method(cls, "define_call_super_method", class_spec_define_call_super_method, 2); +#endif + +#ifdef HAVE_RB_CLASS_PATH rb_define_method(cls, "rb_class_path", class_spec_rb_class_path, 1); +#endif + +#ifdef HAVE_RB_CLASS_NAME rb_define_method(cls, "rb_class_name", class_spec_rb_class_name, 1); +#endif + +#ifdef HAVE_RB_CLASS2NAME rb_define_method(cls, "rb_class2name", class_spec_rb_class2name, 1); +#endif + +#ifdef HAVE_RB_PATH2CLASS rb_define_method(cls, "rb_path2class", class_spec_rb_path2class, 1); +#endif + +#ifdef HAVE_RB_PATH_TO_CLASS rb_define_method(cls, "rb_path_to_class", class_spec_rb_path_to_class, 1); - rb_define_method(cls, "rb_class_instance_methods", class_spec_rb_class_instance_methods, -1); - rb_define_method(cls, "rb_class_public_instance_methods", class_spec_rb_class_public_instance_methods, -1); - rb_define_method(cls, "rb_class_protected_instance_methods", class_spec_rb_class_protected_instance_methods, -1); - rb_define_method(cls, "rb_class_private_instance_methods", class_spec_rb_class_private_instance_methods, -1); +#endif + +#ifdef HAVE_RB_CLASS_NEW rb_define_method(cls, "rb_class_new", class_spec_rb_class_new, 1); - rb_define_method(cls, "rb_class_new_instance", class_spec_rb_class_new_instance, 2); -#ifdef RUBY_VERSION_IS_3_0 - rb_define_method(cls, "rb_class_new_instance_kw", class_spec_rb_class_new_instance_kw, 2); #endif + +#ifdef HAVE_RB_CLASS_NEW_INSTANCE + rb_define_method(cls, "rb_class_new_instance", class_spec_rb_class_new_instance, 3); +#endif + +#ifdef HAVE_RB_CLASS_REAL rb_define_method(cls, "rb_class_real", class_spec_rb_class_real, 1); - rb_define_method(cls, "rb_class_get_superclass", class_spec_rb_class_get_superclass, 1); +#endif + +#ifdef HAVE_RB_CLASS_SUPERCLASS rb_define_method(cls, "rb_class_superclass", class_spec_rb_class_superclass, 1); +#endif + +#ifdef HAVE_RB_CVAR_DEFINED rb_define_method(cls, "rb_cvar_defined", class_spec_cvar_defined, 2); +#endif + +#ifdef HAVE_RB_CVAR_GET rb_define_method(cls, "rb_cvar_get", class_spec_cvar_get, 2); +#endif + +#ifdef HAVE_RB_CVAR_SET rb_define_method(cls, "rb_cvar_set", class_spec_cvar_set, 3); +#endif + +#ifdef HAVE_RB_CV_GET rb_define_method(cls, "rb_cv_get", class_spec_cv_get, 2); +#endif + +#ifdef HAVE_RB_CV_SET rb_define_method(cls, "rb_cv_set", class_spec_cv_set, 3); +#endif + +#ifdef HAVE_RB_DEFINE_ATTR rb_define_method(cls, "rb_define_attr", class_spec_define_attr, 4); +#endif + +#ifdef HAVE_RB_DEFINE_CLASS rb_define_method(cls, "rb_define_class", class_spec_rb_define_class, 2); +#endif + +#ifdef HAVE_RB_DEFINE_CLASS_UNDER rb_define_method(cls, "rb_define_class_under", class_spec_rb_define_class_under, 3); +#endif + +#ifdef HAVE_RB_DEFINE_CLASS_ID_UNDER rb_define_method(cls, "rb_define_class_id_under", class_spec_rb_define_class_id_under, 3); +#endif + +#ifdef HAVE_RB_DEFINE_CLASS_VARIABLE rb_define_method(cls, "rb_define_class_variable", class_spec_define_class_variable, 3); +#endif + +#ifdef HAVE_RB_INCLUDE_MODULE rb_define_method(cls, "rb_include_module", class_spec_include_module, 2); +#endif } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/class_under_autoload_spec.c b/spec/ruby/optional/capi/ext/class_under_autoload_spec.c index e0b1f249c0..120dec7327 100644 --- a/spec/ruby/optional/capi/ext/class_under_autoload_spec.c +++ b/spec/ruby/optional/capi/ext/class_under_autoload_spec.c @@ -1,13 +1,5 @@ #include "ruby.h" -#ifdef __cplusplus -extern "C" { -#endif - void Init_class_under_autoload_spec(void) { rb_define_class_under(rb_cObject, "ClassUnderAutoload", rb_cObject); } - -#ifdef __cplusplus -} -#endif diff --git a/spec/ruby/optional/capi/ext/complex_spec.c b/spec/ruby/optional/capi/ext/complex_spec.c index dfccd7a037..476bbce31c 100644 --- a/spec/ruby/optional/capi/ext/complex_spec.c +++ b/spec/ruby/optional/capi/ext/complex_spec.c @@ -5,38 +5,69 @@ extern "C" { #endif +#ifdef HAVE_RB_COMPLEX static VALUE complex_spec_rb_Complex(VALUE self, VALUE num, VALUE den) { return rb_Complex(num, den); } +#endif +#ifdef HAVE_RB_COMPLEX1 static VALUE complex_spec_rb_Complex1(VALUE self, VALUE num) { return rb_Complex1(num); } +#endif +#ifdef HAVE_RB_COMPLEX2 static VALUE complex_spec_rb_Complex2(VALUE self, VALUE num, VALUE den) { return rb_Complex2(num, den); } +#endif +#ifdef HAVE_RB_COMPLEX_NEW static VALUE complex_spec_rb_complex_new(VALUE self, VALUE num, VALUE den) { return rb_complex_new(num, den); } +#endif +#ifdef HAVE_RB_COMPLEX_NEW1 static VALUE complex_spec_rb_complex_new1(VALUE self, VALUE num) { return rb_complex_new1(num); } +#endif +#ifdef HAVE_RB_COMPLEX_NEW2 static VALUE complex_spec_rb_complex_new2(VALUE self, VALUE num, VALUE den) { return rb_complex_new2(num, den); } +#endif void Init_complex_spec(void) { - VALUE cls = rb_define_class("CApiComplexSpecs", rb_cObject); + VALUE cls; + cls = rb_define_class("CApiComplexSpecs", rb_cObject); + +#ifdef HAVE_RB_COMPLEX rb_define_method(cls, "rb_Complex", complex_spec_rb_Complex, 2); +#endif + +#ifdef HAVE_RB_COMPLEX1 rb_define_method(cls, "rb_Complex1", complex_spec_rb_Complex1, 1); +#endif + +#ifdef HAVE_RB_COMPLEX2 rb_define_method(cls, "rb_Complex2", complex_spec_rb_Complex2, 2); +#endif + +#ifdef HAVE_RB_COMPLEX_NEW rb_define_method(cls, "rb_complex_new", complex_spec_rb_complex_new, 2); +#endif + +#ifdef HAVE_RB_COMPLEX_NEW1 rb_define_method(cls, "rb_complex_new1", complex_spec_rb_complex_new1, 1); +#endif + +#ifdef HAVE_RB_COMPLEX_NEW2 rb_define_method(cls, "rb_complex_new2", complex_spec_rb_complex_new2, 2); +#endif } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/constants_spec.c b/spec/ruby/optional/capi/ext/constants_spec.c index 9aee8db37f..7751b12224 100644 --- a/spec/ruby/optional/capi/ext/constants_spec.c +++ b/spec/ruby/optional/capi/ext/constants_spec.c @@ -5,172 +5,640 @@ extern "C" { #endif -#define defconstfunc(name) \ -static VALUE constants_spec_##name(VALUE self) { return name; } - -defconstfunc(rb_cArray) -defconstfunc(rb_cBasicObject) -defconstfunc(rb_cBinding) -defconstfunc(rb_cClass) -defconstfunc(rb_cComplex) -defconstfunc(rb_mComparable) -#ifndef RUBY_VERSION_IS_3_0 -defconstfunc(rb_cData) -#endif -defconstfunc(rb_cDir) -defconstfunc(rb_cEncoding) -defconstfunc(rb_mEnumerable) -defconstfunc(rb_cEnumerator) -defconstfunc(rb_cFalseClass) -defconstfunc(rb_cFile) -defconstfunc(rb_mFileTest) -defconstfunc(rb_cFloat) -defconstfunc(rb_mGC) -defconstfunc(rb_cHash) -defconstfunc(rb_cInteger) -defconstfunc(rb_cIO) -defconstfunc(rb_mKernel) -defconstfunc(rb_mMath) -defconstfunc(rb_cMatch) -defconstfunc(rb_cMethod) -defconstfunc(rb_cModule) -defconstfunc(rb_cNilClass) -defconstfunc(rb_cNumeric) -defconstfunc(rb_cObject) -defconstfunc(rb_cProc) -defconstfunc(rb_mProcess) -defconstfunc(rb_cRandom) -defconstfunc(rb_cRange) -defconstfunc(rb_cRational) -defconstfunc(rb_cRegexp) -defconstfunc(rb_cStat) -defconstfunc(rb_cString) -defconstfunc(rb_cStruct) -defconstfunc(rb_cSymbol) -defconstfunc(rb_cTime) -defconstfunc(rb_cThread) -defconstfunc(rb_cTrueClass) -defconstfunc(rb_cUnboundMethod) -defconstfunc(rb_eArgError) -defconstfunc(rb_eEncodingError) -defconstfunc(rb_eEncCompatError) -defconstfunc(rb_eEOFError) -defconstfunc(rb_mErrno) -defconstfunc(rb_eException) -defconstfunc(rb_eFatal) -defconstfunc(rb_eFloatDomainError) -defconstfunc(rb_eFrozenError) -defconstfunc(rb_eIndexError) -defconstfunc(rb_eInterrupt) -defconstfunc(rb_eIOError) -defconstfunc(rb_eKeyError) -defconstfunc(rb_eLoadError) -defconstfunc(rb_eLocalJumpError) -defconstfunc(rb_eMathDomainError) -defconstfunc(rb_eNameError) -defconstfunc(rb_eNoMemError) -defconstfunc(rb_eNoMethodError) -defconstfunc(rb_eNotImpError) -defconstfunc(rb_eRangeError) -defconstfunc(rb_eRegexpError) -defconstfunc(rb_eRuntimeError) -defconstfunc(rb_eScriptError) -defconstfunc(rb_eSecurityError) -defconstfunc(rb_eSignal) -defconstfunc(rb_eStandardError) -defconstfunc(rb_eStopIteration) -defconstfunc(rb_eSyntaxError) -defconstfunc(rb_eSystemCallError) -defconstfunc(rb_eSystemExit) -defconstfunc(rb_eSysStackError) -defconstfunc(rb_eTypeError) -defconstfunc(rb_eThreadError) -defconstfunc(rb_mWaitReadable) -defconstfunc(rb_mWaitWritable) -defconstfunc(rb_eZeroDivError) +#ifdef HAVE_RB_CARRAY +static VALUE constants_spec_rb_cArray(VALUE self) { + return rb_cArray; +} +#endif + +#ifdef HAVE_RB_CBIGNUM +static VALUE constants_spec_rb_cBignum(VALUE self) { + return rb_cBignum; +} +#endif + +#ifdef HAVE_RB_CCLASS +static VALUE constants_spec_rb_cClass(VALUE self) { + return rb_cClass; +} +#endif + +#ifdef HAVE_RB_CDATA +static VALUE constants_spec_rb_cData(VALUE self) { + return rb_cData; +} +#endif + +#ifdef HAVE_RB_CFALSECLASS +static VALUE constants_spec_rb_cFalseClass(VALUE self) { + return rb_cFalseClass; +} +#endif + +#ifdef HAVE_RB_CFILE +static VALUE constants_spec_rb_cFile(VALUE self) { + return rb_cFile; +} +#endif + +#ifdef HAVE_RB_CFIXNUM +static VALUE constants_spec_rb_cFixnum(VALUE self) { + return rb_cFixnum; +} +#endif + +#ifdef HAVE_RB_CFLOAT +static VALUE constants_spec_rb_cFloat(VALUE self) { + return rb_cFloat; +} +#endif + +#ifdef HAVE_RB_CHASH +static VALUE constants_spec_rb_cHash(VALUE self) { + return rb_cHash; +} +#endif + +#ifdef HAVE_RB_CINTEGER +static VALUE constants_spec_rb_cInteger(VALUE self) { + return rb_cInteger; +} +#endif + +#ifdef HAVE_RB_CIO +static VALUE constants_spec_rb_cIO(VALUE self) { + return rb_cIO; +} +#endif + +#ifdef HAVE_RB_CMODULE +static VALUE constants_spec_rb_cModule(VALUE self) { + return rb_cModule; +} +#endif + +#ifdef HAVE_RB_CMATCH +static VALUE constants_spec_rb_cMatch(VALUE self) { + return rb_cMatch; +} +#endif + +#ifdef HAVE_RB_CNILCLASS +static VALUE constants_spec_rb_cNilClass(VALUE self) { + return rb_cNilClass; +} +#endif + +#ifdef HAVE_RB_CNUMERIC +static VALUE constants_spec_rb_cNumeric(VALUE self) { + return rb_cNumeric; +} +#endif + +#ifdef HAVE_RB_COBJECT +static VALUE constants_spec_rb_cObject(VALUE self) { + return rb_cObject; +} +#endif + +#ifdef HAVE_RB_CRANGE +static VALUE constants_spec_rb_cRange(VALUE self) { + return rb_cRange; +} +#endif + +#ifdef HAVE_RB_CREGEXP +static VALUE constants_spec_rb_cRegexp(VALUE self) { + return rb_cRegexp; +} +#endif + +#ifdef HAVE_RB_CSTRING +static VALUE constants_spec_rb_cString(VALUE self) { + return rb_cString; +} +#endif + +#ifdef HAVE_RB_CSTRUCT +static VALUE constants_spec_rb_cStruct(VALUE self) { + return rb_cStruct; +} +#endif + +#ifdef HAVE_RB_CSYMBOL +static VALUE constants_spec_rb_cSymbol(VALUE self) { + return rb_cSymbol; +} +#endif + +#ifdef HAVE_RB_CTIME +static VALUE constants_spec_rb_cTime(VALUE self) { + return rb_cTime; +} +#endif + +#ifdef HAVE_RB_CTHREAD +static VALUE constants_spec_rb_cThread(VALUE self) { + return rb_cThread; +} +#endif + +#ifdef HAVE_RB_CTRUECLASS +static VALUE constants_spec_rb_cTrueClass(VALUE self) { + return rb_cTrueClass; +} +#endif + +#ifdef HAVE_RB_CPROC +static VALUE constants_spec_rb_cProc(VALUE self) { + return rb_cProc; +} +#endif + +#ifdef HAVE_RB_CMETHOD +static VALUE constants_spec_rb_cMethod(VALUE self) { + return rb_cMethod; +} +#endif + +#ifdef HAVE_RB_CENUMERATOR +static VALUE constants_spec_rb_cEnumerator(VALUE self) { + return rb_cEnumerator; +} +#endif + +#ifdef HAVE_RB_MCOMPARABLE +static VALUE constants_spec_rb_mComparable(VALUE self) { + return rb_mComparable; +} +#endif + +#ifdef HAVE_RB_MENUMERABLE +static VALUE constants_spec_rb_mEnumerable(VALUE self) { + return rb_mEnumerable; +} +#endif + +#ifdef HAVE_RB_MKERNEL +static VALUE constants_spec_rb_mKernel(VALUE self) { + return rb_mKernel; +} +#endif + +#ifdef HAVE_RB_EARGERROR +static VALUE constants_spec_rb_eArgError(VALUE self) { + return rb_eArgError; +} +#endif + +#ifdef HAVE_RB_EEOFERROR +static VALUE constants_spec_rb_eEOFError(VALUE self) { + return rb_eEOFError; +} +#endif + +#ifdef HAVE_RB_MERRNO +static VALUE constants_spec_rb_mErrno(VALUE self) { + return rb_mErrno; +} +#endif + +#ifdef HAVE_RB_EEXCEPTION +static VALUE constants_spec_rb_eException(VALUE self) { + return rb_eException; +} +#endif + +#ifdef HAVE_RB_EFLOATDOMAINERROR +static VALUE constants_spec_rb_eFloatDomainError(VALUE self) { + return rb_eFloatDomainError; +} +#endif + +#ifdef HAVE_RB_EINDEXERROR +static VALUE constants_spec_rb_eIndexError(VALUE self) { + return rb_eIndexError; +} +#endif + +#ifdef HAVE_RB_EINTERRUPT +static VALUE constants_spec_rb_eInterrupt(VALUE self) { + return rb_eInterrupt; +} +#endif + +#ifdef HAVE_RB_EIOERROR +static VALUE constants_spec_rb_eIOError(VALUE self) { + return rb_eIOError; +} +#endif + +#ifdef HAVE_RB_ELOADERROR +static VALUE constants_spec_rb_eLoadError(VALUE self) { + return rb_eLoadError; +} +#endif + +#ifdef HAVE_RB_ELOCALJUMPERROR +static VALUE constants_spec_rb_eLocalJumpError(VALUE self) { + return rb_eLocalJumpError; +} +#endif + +#ifdef HAVE_RB_ENAMEERROR +static VALUE constants_spec_rb_eNameError(VALUE self) { + return rb_eNameError; +} +#endif + +#ifdef HAVE_RB_ENOMEMERROR +static VALUE constants_spec_rb_eNoMemError(VALUE self) { + return rb_eNoMemError; +} +#endif + +#ifdef HAVE_RB_ENOMETHODERROR +static VALUE constants_spec_rb_eNoMethodError(VALUE self) { + return rb_eNoMethodError; +} +#endif + +#ifdef HAVE_RB_ENOTIMPERROR +static VALUE constants_spec_rb_eNotImpError(VALUE self) { + return rb_eNotImpError; +} +#endif + +#ifdef HAVE_RB_ERANGEERROR +static VALUE constants_spec_rb_eRangeError(VALUE self) { + return rb_eRangeError; +} +#endif + +#ifdef HAVE_RB_EREGEXPERROR +static VALUE constants_spec_rb_eRegexpError(VALUE self) { + return rb_eRegexpError; +} +#endif + +#ifdef HAVE_RB_ERUNTIMEERROR +static VALUE constants_spec_rb_eRuntimeError(VALUE self) { + return rb_eRuntimeError; +} +#endif + +#ifdef HAVE_RB_ESCRIPTERROR +static VALUE constants_spec_rb_eScriptError(VALUE self) { + return rb_eScriptError; +} +#endif + +#ifdef HAVE_RB_ESECURITYERROR +static VALUE constants_spec_rb_eSecurityError(VALUE self) { + return rb_eSecurityError; +} +#endif + +#ifdef HAVE_RB_ESIGNAL +static VALUE constants_spec_rb_eSignal(VALUE self) { + return rb_eSignal; +} +#endif + +#ifdef HAVE_RB_ESTANDARDERROR +static VALUE constants_spec_rb_eStandardError(VALUE self) { + return rb_eStandardError; +} +#endif + +#ifdef HAVE_RB_ESYNTAXERROR +static VALUE constants_spec_rb_eSyntaxError(VALUE self) { + return rb_eSyntaxError; +} +#endif + +#ifdef HAVE_RB_ESYSTEMCALLERROR +static VALUE constants_spec_rb_eSystemCallError(VALUE self) { + return rb_eSystemCallError; +} +#endif + +#ifdef HAVE_RB_ESYSTEMEXIT +static VALUE constants_spec_rb_eSystemExit(VALUE self) { + return rb_eSystemExit; +} +#endif + +#ifdef HAVE_RB_ESYSSTACKERROR +static VALUE constants_spec_rb_eSysStackError(VALUE self) { + return rb_eSysStackError; +} +#endif + +#ifdef HAVE_RB_ETYPEERROR +static VALUE constants_spec_rb_eTypeError(VALUE self) { + return rb_eTypeError; +} +#endif + +#ifdef HAVE_RB_ETHREADERROR +static VALUE constants_spec_rb_eThreadError(VALUE self) { + return rb_eThreadError; +} +#endif + +#ifdef HAVE_RB_EZERODIVERROR +static VALUE constants_spec_rb_eZeroDivError(VALUE self) { + return rb_eZeroDivError; +} +#endif + +#ifdef HAVE_RB_EMATHDOMAINERROR +static VALUE constants_spec_rb_eMathDomainError(VALUE self) { + return rb_eMathDomainError; +} +#endif + +#ifdef HAVE_RB_EENCCOMPATERROR +static VALUE constants_spec_rb_eEncCompatError(VALUE self) { + return rb_eEncCompatError; +} +#endif + +#ifdef HAVE_RB_MWAITREADABLE +static VALUE constants_spec_rb_mWaitReadable(VALUE self) { + return rb_mWaitReadable; +} +#endif + +#ifdef HAVE_RB_MWAITWRITABLE +static VALUE constants_spec_rb_mWaitWritable(VALUE self) { + return rb_mWaitWritable; +} +#endif + +#ifdef HAVE_RB_CDIR +static VALUE constants_spec_rb_cDir(VALUE self) { + return rb_cDir; +} +#endif void Init_constants_spec(void) { - VALUE cls = rb_define_class("CApiConstantsSpecs", rb_cObject); + VALUE cls; + cls = rb_define_class("CApiConstantsSpecs", rb_cObject); + +#ifdef HAVE_RB_CARRAY rb_define_method(cls, "rb_cArray", constants_spec_rb_cArray, 0); - rb_define_method(cls, "rb_cBasicObject", constants_spec_rb_cBasicObject, 0); - rb_define_method(cls, "rb_cBinding", constants_spec_rb_cBinding, 0); +#endif + +#ifdef HAVE_RB_CBIGNUM + rb_define_method(cls, "rb_cBignum", constants_spec_rb_cBignum, 0); +#endif + +#ifdef HAVE_RB_CCLASS rb_define_method(cls, "rb_cClass", constants_spec_rb_cClass, 0); - rb_define_method(cls, "rb_cComplex", constants_spec_rb_cComplex, 0); - rb_define_method(cls, "rb_mComparable", constants_spec_rb_mComparable, 0); - #ifndef RUBY_VERSION_IS_3_0 +#endif + +#ifdef HAVE_RB_CDATA rb_define_method(cls, "rb_cData", constants_spec_rb_cData, 0); - #endif - rb_define_method(cls, "rb_cDir", constants_spec_rb_cDir, 0); - rb_define_method(cls, "rb_cEncoding", constants_spec_rb_cEncoding, 0); - rb_define_method(cls, "rb_mEnumerable", constants_spec_rb_mEnumerable, 0); - rb_define_method(cls, "rb_cEnumerator", constants_spec_rb_cEnumerator, 0); +#endif + +#ifdef HAVE_RB_CFALSECLASS rb_define_method(cls, "rb_cFalseClass", constants_spec_rb_cFalseClass, 0); +#endif + +#ifdef HAVE_RB_CFILE rb_define_method(cls, "rb_cFile", constants_spec_rb_cFile, 0); - rb_define_method(cls, "rb_mFileTest", constants_spec_rb_mFileTest, 0); +#endif + +#ifdef HAVE_RB_CFIXNUM + rb_define_method(cls, "rb_cFixnum", constants_spec_rb_cFixnum, 0); +#endif + +#ifdef HAVE_RB_CFLOAT rb_define_method(cls, "rb_cFloat", constants_spec_rb_cFloat, 0); - rb_define_method(cls, "rb_mGC", constants_spec_rb_mGC, 0); +#endif + +#ifdef HAVE_RB_CHASH rb_define_method(cls, "rb_cHash", constants_spec_rb_cHash, 0); +#endif + +#ifdef HAVE_RB_CINTEGER rb_define_method(cls, "rb_cInteger", constants_spec_rb_cInteger, 0); +#endif + +#ifdef HAVE_RB_CIO rb_define_method(cls, "rb_cIO", constants_spec_rb_cIO, 0); - rb_define_method(cls, "rb_mKernel", constants_spec_rb_mKernel, 0); - rb_define_method(cls, "rb_mMath", constants_spec_rb_mMath, 0); +#endif + +#ifdef HAVE_RB_CMATCH rb_define_method(cls, "rb_cMatch", constants_spec_rb_cMatch, 0); - rb_define_method(cls, "rb_cMethod", constants_spec_rb_cMethod, 0); +#endif + +#ifdef HAVE_RB_CMODULE rb_define_method(cls, "rb_cModule", constants_spec_rb_cModule, 0); +#endif + +#ifdef HAVE_RB_CNILCLASS rb_define_method(cls, "rb_cNilClass", constants_spec_rb_cNilClass, 0); +#endif + +#ifdef HAVE_RB_CNUMERIC rb_define_method(cls, "rb_cNumeric", constants_spec_rb_cNumeric, 0); +#endif + +#ifdef HAVE_RB_COBJECT rb_define_method(cls, "rb_cObject", constants_spec_rb_cObject, 0); - rb_define_method(cls, "rb_cProc", constants_spec_rb_cProc, 0); - rb_define_method(cls, "rb_mProcess", constants_spec_rb_mProcess, 0); - rb_define_method(cls, "rb_cRandom", constants_spec_rb_cRandom, 0); +#endif + +#ifdef HAVE_RB_CRANGE rb_define_method(cls, "rb_cRange", constants_spec_rb_cRange, 0); - rb_define_method(cls, "rb_cRational", constants_spec_rb_cRational, 0); +#endif + +#ifdef HAVE_RB_CREGEXP rb_define_method(cls, "rb_cRegexp", constants_spec_rb_cRegexp, 0); - rb_define_method(cls, "rb_cStat", constants_spec_rb_cStat, 0); +#endif + +#ifdef HAVE_RB_CSTRING rb_define_method(cls, "rb_cString", constants_spec_rb_cString, 0); +#endif + +#ifdef HAVE_RB_CSTRUCT rb_define_method(cls, "rb_cStruct", constants_spec_rb_cStruct, 0); +#endif + +#ifdef HAVE_RB_CSYMBOL rb_define_method(cls, "rb_cSymbol", constants_spec_rb_cSymbol, 0); +#endif + +#ifdef HAVE_RB_CTIME rb_define_method(cls, "rb_cTime", constants_spec_rb_cTime, 0); +#endif + +#ifdef HAVE_RB_CTHREAD rb_define_method(cls, "rb_cThread", constants_spec_rb_cThread, 0); +#endif + +#ifdef HAVE_RB_CTRUECLASS rb_define_method(cls, "rb_cTrueClass", constants_spec_rb_cTrueClass, 0); - rb_define_method(cls, "rb_cUnboundMethod", constants_spec_rb_cUnboundMethod, 0); +#endif + +#ifdef HAVE_RB_CPROC + rb_define_method(cls, "rb_cProc", constants_spec_rb_cProc, 0); +#endif + +#ifdef HAVE_RB_CMETHOD + rb_define_method(cls, "rb_cMethod", constants_spec_rb_cMethod, 0); +#endif + +#ifdef HAVE_RB_CENUMERATOR + rb_define_method(cls, "rb_cEnumerator", constants_spec_rb_cEnumerator, 0); +#endif + +#ifdef HAVE_RB_MCOMPARABLE + rb_define_method(cls, "rb_mComparable", constants_spec_rb_mComparable, 0); +#endif + +#ifdef HAVE_RB_MENUMERABLE + rb_define_method(cls, "rb_mEnumerable", constants_spec_rb_mEnumerable, 0); +#endif + +#ifdef HAVE_RB_MKERNEL + rb_define_method(cls, "rb_mKernel", constants_spec_rb_mKernel, 0); +#endif + +#ifdef HAVE_RB_EARGERROR rb_define_method(cls, "rb_eArgError", constants_spec_rb_eArgError, 0); - rb_define_method(cls, "rb_eEncodingError", constants_spec_rb_eEncodingError, 0); - rb_define_method(cls, "rb_eEncCompatError", constants_spec_rb_eEncCompatError, 0); +#endif + +#ifdef HAVE_RB_EEOFERROR rb_define_method(cls, "rb_eEOFError", constants_spec_rb_eEOFError, 0); +#endif + +#ifdef HAVE_RB_MERRNO rb_define_method(cls, "rb_mErrno", constants_spec_rb_mErrno, 0); +#endif + +#ifdef HAVE_RB_EEXCEPTION rb_define_method(cls, "rb_eException", constants_spec_rb_eException, 0); - rb_define_method(cls, "rb_eFatal", constants_spec_rb_eFatal, 0); +#endif + +#ifdef HAVE_RB_EFLOATDOMAINERROR rb_define_method(cls, "rb_eFloatDomainError", constants_spec_rb_eFloatDomainError, 0); - rb_define_method(cls, "rb_eFrozenError", constants_spec_rb_eFrozenError, 0); +#endif + +#ifdef HAVE_RB_EINDEXERROR rb_define_method(cls, "rb_eIndexError", constants_spec_rb_eIndexError, 0); +#endif + +#ifdef HAVE_RB_EINTERRUPT rb_define_method(cls, "rb_eInterrupt", constants_spec_rb_eInterrupt, 0); +#endif + +#ifdef HAVE_RB_EIOERROR rb_define_method(cls, "rb_eIOError", constants_spec_rb_eIOError, 0); - rb_define_method(cls, "rb_eKeyError", constants_spec_rb_eKeyError, 0); +#endif + +#ifdef HAVE_RB_ELOADERROR rb_define_method(cls, "rb_eLoadError", constants_spec_rb_eLoadError, 0); +#endif + +#ifdef HAVE_RB_ELOCALJUMPERROR rb_define_method(cls, "rb_eLocalJumpError", constants_spec_rb_eLocalJumpError, 0); - rb_define_method(cls, "rb_eMathDomainError", constants_spec_rb_eMathDomainError, 0); +#endif + +#ifdef HAVE_RB_ENAMEERROR rb_define_method(cls, "rb_eNameError", constants_spec_rb_eNameError, 0); +#endif + +#ifdef HAVE_RB_ENOMEMERROR rb_define_method(cls, "rb_eNoMemError", constants_spec_rb_eNoMemError, 0); +#endif + +#ifdef HAVE_RB_ENOMETHODERROR rb_define_method(cls, "rb_eNoMethodError", constants_spec_rb_eNoMethodError, 0); +#endif + +#ifdef HAVE_RB_ENOTIMPERROR rb_define_method(cls, "rb_eNotImpError", constants_spec_rb_eNotImpError, 0); +#endif + +#ifdef HAVE_RB_ERANGEERROR rb_define_method(cls, "rb_eRangeError", constants_spec_rb_eRangeError, 0); +#endif + +#ifdef HAVE_RB_EREGEXPERROR rb_define_method(cls, "rb_eRegexpError", constants_spec_rb_eRegexpError, 0); +#endif + +#ifdef HAVE_RB_ERUNTIMEERROR rb_define_method(cls, "rb_eRuntimeError", constants_spec_rb_eRuntimeError, 0); +#endif + +#ifdef HAVE_RB_ESCRIPTERROR rb_define_method(cls, "rb_eScriptError", constants_spec_rb_eScriptError, 0); +#endif + +#ifdef HAVE_RB_ESECURITYERROR rb_define_method(cls, "rb_eSecurityError", constants_spec_rb_eSecurityError, 0); +#endif + +#ifdef HAVE_RB_ESIGNAL rb_define_method(cls, "rb_eSignal", constants_spec_rb_eSignal, 0); +#endif + +#ifdef HAVE_RB_ESTANDARDERROR rb_define_method(cls, "rb_eStandardError", constants_spec_rb_eStandardError, 0); - rb_define_method(cls, "rb_eStopIteration", constants_spec_rb_eStopIteration, 0); +#endif + +#ifdef HAVE_RB_ESYNTAXERROR rb_define_method(cls, "rb_eSyntaxError", constants_spec_rb_eSyntaxError, 0); +#endif + +#ifdef HAVE_RB_ESYSTEMCALLERROR rb_define_method(cls, "rb_eSystemCallError", constants_spec_rb_eSystemCallError, 0); +#endif + +#ifdef HAVE_RB_ESYSTEMEXIT rb_define_method(cls, "rb_eSystemExit", constants_spec_rb_eSystemExit, 0); +#endif + +#ifdef HAVE_RB_ESYSSTACKERROR rb_define_method(cls, "rb_eSysStackError", constants_spec_rb_eSysStackError, 0); +#endif + +#ifdef HAVE_RB_ETYPEERROR rb_define_method(cls, "rb_eTypeError", constants_spec_rb_eTypeError, 0); +#endif + +#ifdef HAVE_RB_ETHREADERROR rb_define_method(cls, "rb_eThreadError", constants_spec_rb_eThreadError, 0); +#endif + +#ifdef HAVE_RB_EZERODIVERROR + rb_define_method(cls, "rb_eZeroDivError", constants_spec_rb_eZeroDivError, 0); +#endif + +#ifdef HAVE_RB_EMATHDOMAINERROR + rb_define_method(cls, "rb_eMathDomainError", constants_spec_rb_eMathDomainError, 0); +#endif + +#ifdef HAVE_RB_EENCCOMPATERROR + rb_define_method(cls, "rb_eEncCompatError", constants_spec_rb_eEncCompatError, 0); +#endif + +#ifdef HAVE_RB_MWAITREADABLE rb_define_method(cls, "rb_mWaitReadable", constants_spec_rb_mWaitReadable, 0); +#endif + +#ifdef HAVE_RB_MWAITWRITABLE rb_define_method(cls, "rb_mWaitWritable", constants_spec_rb_mWaitWritable, 0); - rb_define_method(cls, "rb_eZeroDivError", constants_spec_rb_eZeroDivError, 0); +#endif + +#ifdef HAVE_RB_CDIR + rb_define_method(cls, "rb_cDir", constants_spec_rb_cDir, 0); +#endif + } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/data_spec.c b/spec/ruby/optional/capi/ext/data_spec.c index efefe37c3a..ed79497897 100644 --- a/spec/ruby/optional/capi/ext/data_spec.c +++ b/spec/ruby/optional/capi/ext/data_spec.c @@ -3,11 +3,11 @@ #include <string.h> -#ifndef RUBY_VERSION_IS_3_4 #ifdef __cplusplus extern "C" { #endif +#if defined(HAVE_RDATA) && defined(HAVE_DATA_WRAP_STRUCT) struct sample_wrapped_struct { int foo; }; @@ -20,7 +20,7 @@ void sample_wrapped_struct_mark(void* st) { } VALUE sdaf_alloc_func(VALUE klass) { - struct sample_wrapped_struct* bar = (struct sample_wrapped_struct*) malloc(sizeof(struct sample_wrapped_struct)); + struct sample_wrapped_struct* bar = (struct sample_wrapped_struct *)malloc(sizeof(struct sample_wrapped_struct)); bar->foo = 42; return Data_Wrap_Struct(klass, &sample_wrapped_struct_mark, &sample_wrapped_struct_free, bar); } @@ -33,11 +33,17 @@ VALUE sdaf_get_struct(VALUE self) { } VALUE sws_wrap_struct(VALUE self, VALUE val) { - struct sample_wrapped_struct* bar = (struct sample_wrapped_struct*) malloc(sizeof(struct sample_wrapped_struct)); + struct sample_wrapped_struct* bar = (struct sample_wrapped_struct *)malloc(sizeof(struct sample_wrapped_struct)); bar->foo = FIX2INT(val); return Data_Wrap_Struct(rb_cObject, &sample_wrapped_struct_mark, &sample_wrapped_struct_free, bar); } +VALUE sws_wrap_struct_null(VALUE self, VALUE val) { + struct sample_wrapped_struct* bar = (struct sample_wrapped_struct *)malloc(sizeof(struct sample_wrapped_struct)); + bar->foo = FIX2INT(val); + return Data_Wrap_Struct(0, &sample_wrapped_struct_mark, &sample_wrapped_struct_free, bar); +} + VALUE sws_get_struct(VALUE self, VALUE obj) { struct sample_wrapped_struct* bar; Data_Get_Struct(obj, struct sample_wrapped_struct, bar); @@ -59,32 +65,30 @@ VALUE sws_get_struct_data_ptr(VALUE self, VALUE obj) { VALUE sws_change_struct(VALUE self, VALUE obj, VALUE new_val) { struct sample_wrapped_struct *old_struct, *new_struct; - new_struct = (struct sample_wrapped_struct*) malloc(sizeof(struct sample_wrapped_struct)); + new_struct = (struct sample_wrapped_struct *)malloc(sizeof(struct sample_wrapped_struct)); new_struct->foo = FIX2INT(new_val); - old_struct = (struct sample_wrapped_struct*) RDATA(obj)->data; + old_struct = RDATA(obj)->data; free(old_struct); RDATA(obj)->data = new_struct; return Qnil; } - -VALUE sws_rb_check_type(VALUE self, VALUE obj, VALUE other) { - rb_check_type(obj, TYPE(other)); - return Qtrue; -} #endif void Init_data_spec(void) { -#ifndef RUBY_VERSION_IS_3_4 - VALUE cls = rb_define_class("CApiAllocSpecs", rb_cObject); + VALUE cls; + cls = rb_define_class("CApiAllocSpecs", rb_cObject); + +#if defined(HAVE_RDATA) && defined(HAVE_DATA_WRAP_STRUCT) rb_define_alloc_func(cls, sdaf_alloc_func); rb_define_method(cls, "wrapped_data", sdaf_get_struct, 0); + cls = rb_define_class("CApiWrappedStructSpecs", rb_cObject); rb_define_method(cls, "wrap_struct", sws_wrap_struct, 1); + rb_define_method(cls, "wrap_struct_null", sws_wrap_struct_null, 1); rb_define_method(cls, "get_struct", sws_get_struct, 1); rb_define_method(cls, "get_struct_rdata", sws_get_struct_rdata, 1); rb_define_method(cls, "get_struct_data_ptr", sws_get_struct_data_ptr, 1); rb_define_method(cls, "change_struct", sws_change_struct, 2); - rb_define_method(cls, "rb_check_type", sws_rb_check_type, 2); #endif } diff --git a/spec/ruby/optional/capi/ext/debug_spec.c b/spec/ruby/optional/capi/ext/debug_spec.c deleted file mode 100644 index 9131eda78b..0000000000 --- a/spec/ruby/optional/capi/ext/debug_spec.c +++ /dev/null @@ -1,93 +0,0 @@ -#include "ruby.h" -#include "rubyspec.h" -#include "ruby/debug.h" - -#ifdef __cplusplus -extern "C" { -#endif - -static VALUE callback_data = Qfalse; - -static VALUE rb_debug_inspector_open_callback(const rb_debug_inspector_t *dc, void *ptr) { - if (!dc) { - rb_raise(rb_eRuntimeError, "rb_debug_inspector_t should not be NULL"); - } - - VALUE locations = rb_debug_inspector_backtrace_locations(dc); - int len = RARRAY_LENINT(locations); - VALUE results = rb_ary_new2(len); - for (int i = 0; i < len; i++) { - VALUE ary = rb_ary_new2(5); // [self, klass, binding, iseq, backtrace_location] - rb_ary_store(ary, 0, rb_debug_inspector_frame_self_get(dc, i)); - rb_ary_store(ary, 1, rb_debug_inspector_frame_class_get(dc, i)); - rb_ary_store(ary, 2, rb_debug_inspector_frame_binding_get(dc, i)); - rb_ary_store(ary, 3, rb_debug_inspector_frame_iseq_get(dc, i)); - rb_ary_store(ary, 4, rb_ary_entry(locations, i)); - rb_ary_push(results, ary); - } - callback_data = (VALUE)ptr; - return results; -} - -static VALUE rb_debug_inspector_frame_self_get_callback(const rb_debug_inspector_t *dc, void *ptr) { - return rb_debug_inspector_frame_self_get(dc, NUM2LONG((VALUE) ptr)); -} - -static VALUE rb_debug_inspector_frame_class_get_callback(const rb_debug_inspector_t *dc, void *ptr) { - return rb_debug_inspector_frame_class_get(dc, NUM2LONG((VALUE) ptr)); -} - -static VALUE rb_debug_inspector_frame_binding_get_callback(const rb_debug_inspector_t *dc, void *ptr) { - return rb_debug_inspector_frame_binding_get(dc, NUM2LONG((VALUE) ptr)); -} - -static VALUE rb_debug_inspector_frame_iseq_get_callback(const rb_debug_inspector_t *dc, void *ptr) { - return rb_debug_inspector_frame_iseq_get(dc, NUM2LONG((VALUE) ptr)); -} - -static VALUE debug_spec_callback_data(VALUE self) { - return callback_data; -} - -VALUE debug_spec_rb_debug_inspector_open(VALUE self, VALUE index) { - return rb_debug_inspector_open(rb_debug_inspector_open_callback, (void *)index); -} - -VALUE debug_spec_rb_debug_inspector_frame_self_get(VALUE self, VALUE index) { - return rb_debug_inspector_open(rb_debug_inspector_frame_self_get_callback, (void *)index); -} - -VALUE debug_spec_rb_debug_inspector_frame_class_get(VALUE self, VALUE index) { - return rb_debug_inspector_open(rb_debug_inspector_frame_class_get_callback, (void *)index); -} - -VALUE debug_spec_rb_debug_inspector_frame_binding_get(VALUE self, VALUE index) { - return rb_debug_inspector_open(rb_debug_inspector_frame_binding_get_callback, (void *)index); -} - -VALUE debug_spec_rb_debug_inspector_frame_iseq_get(VALUE self, VALUE index) { - return rb_debug_inspector_open(rb_debug_inspector_frame_iseq_get_callback, (void *)index); -} - -static VALUE rb_debug_inspector_backtrace_locations_func(const rb_debug_inspector_t *dc, void *ptr) { - return rb_debug_inspector_backtrace_locations(dc); -} - -VALUE debug_spec_rb_debug_inspector_backtrace_locations(VALUE self) { - return rb_debug_inspector_open(rb_debug_inspector_backtrace_locations_func, (void *)self); -} - -void Init_debug_spec(void) { - VALUE cls = rb_define_class("CApiDebugSpecs", rb_cObject); - rb_define_method(cls, "rb_debug_inspector_open", debug_spec_rb_debug_inspector_open, 1); - rb_define_method(cls, "rb_debug_inspector_frame_self_get", debug_spec_rb_debug_inspector_frame_self_get, 1); - rb_define_method(cls, "rb_debug_inspector_frame_class_get", debug_spec_rb_debug_inspector_frame_class_get, 1); - rb_define_method(cls, "rb_debug_inspector_frame_binding_get", debug_spec_rb_debug_inspector_frame_binding_get, 1); - rb_define_method(cls, "rb_debug_inspector_frame_iseq_get", debug_spec_rb_debug_inspector_frame_iseq_get, 1); - rb_define_method(cls, "rb_debug_inspector_backtrace_locations", debug_spec_rb_debug_inspector_backtrace_locations, 0); - rb_define_method(cls, "debug_spec_callback_data", debug_spec_callback_data, 0); -} - -#ifdef __cplusplus -} -#endif diff --git a/spec/ruby/optional/capi/ext/encoding_spec.c b/spec/ruby/optional/capi/ext/encoding_spec.c index aa8662cfbd..2e555ebc77 100644 --- a/spec/ruby/optional/capi/ext/encoding_spec.c +++ b/spec/ruby/optional/capi/ext/encoding_spec.c @@ -7,158 +7,175 @@ extern "C" { #endif -static VALUE encoding_spec_MBCLEN_CHARFOUND_P(VALUE self, VALUE obj) { - return INT2FIX(MBCLEN_CHARFOUND_P(FIX2INT(obj))); -} - +#ifdef HAVE_ENC_CODERANGE_ASCIIONLY static VALUE encoding_spec_ENC_CODERANGE_ASCIIONLY(VALUE self, VALUE obj) { - if (ENC_CODERANGE_ASCIIONLY(obj)) { + if(ENC_CODERANGE_ASCIIONLY(obj)) { return Qtrue; } else { return Qfalse; } } +#endif +#ifdef HAVE_RB_USASCII_ENCODING static VALUE encoding_spec_rb_usascii_encoding(VALUE self) { return rb_str_new2(rb_usascii_encoding()->name); } +#endif +#ifdef HAVE_RB_USASCII_ENCINDEX static VALUE encoding_spec_rb_usascii_encindex(VALUE self) { return INT2NUM(rb_usascii_encindex()); } +#endif +#ifdef HAVE_RB_ASCII8BIT_ENCODING static VALUE encoding_spec_rb_ascii8bit_encoding(VALUE self) { return rb_str_new2(rb_ascii8bit_encoding()->name); } +#endif +#ifdef HAVE_RB_ASCII8BIT_ENCINDEX static VALUE encoding_spec_rb_ascii8bit_encindex(VALUE self) { return INT2NUM(rb_ascii8bit_encindex()); } +#endif +#ifdef HAVE_RB_UTF8_ENCODING static VALUE encoding_spec_rb_utf8_encoding(VALUE self) { return rb_str_new2(rb_utf8_encoding()->name); } +#endif +#ifdef HAVE_RB_UTF8_ENCINDEX static VALUE encoding_spec_rb_utf8_encindex(VALUE self) { return INT2NUM(rb_utf8_encindex()); } +#endif +#ifdef HAVE_RB_LOCALE_ENCODING static VALUE encoding_spec_rb_locale_encoding(VALUE self) { return rb_str_new2(rb_locale_encoding()->name); } +#endif +#ifdef HAVE_RB_LOCALE_ENCINDEX static VALUE encoding_spec_rb_locale_encindex(VALUE self) { return INT2NUM(rb_locale_encindex()); } +#endif +#ifdef HAVE_RB_FILESYSTEM_ENCODING static VALUE encoding_spec_rb_filesystem_encoding(VALUE self) { return rb_str_new2(rb_filesystem_encoding()->name); } +#endif +#ifdef HAVE_RB_FILESYSTEM_ENCINDEX static VALUE encoding_spec_rb_filesystem_encindex(VALUE self) { return INT2NUM(rb_filesystem_encindex()); } +#endif +#ifdef HAVE_RB_DEFAULT_INTERNAL_ENCODING static VALUE encoding_spec_rb_default_internal_encoding(VALUE self) { rb_encoding* enc = rb_default_internal_encoding(); - if (enc == 0) return Qnil; + if(enc == 0) return Qnil; return rb_str_new2(enc->name); } +#endif +#ifdef HAVE_RB_DEFAULT_EXTERNAL_ENCODING static VALUE encoding_spec_rb_default_external_encoding(VALUE self) { rb_encoding* enc = rb_default_external_encoding(); - if (enc == 0) return Qnil; + if(enc == 0) return Qnil; return rb_str_new2(enc->name); } +#endif -static VALUE encoding_spec_rb_enc_alias(VALUE self, VALUE alias, VALUE orig) { - return INT2NUM(rb_enc_alias(RSTRING_PTR(alias), RSTRING_PTR(orig))); +#ifdef HAVE_RB_ENCDB_ALIAS +/* Not exposed by MRI C-API encoding.h but used in the pg gem. */ +extern int rb_encdb_alias(const char* alias, const char* orig); + +static VALUE encoding_spec_rb_encdb_alias(VALUE self, VALUE alias, VALUE orig) { + return INT2NUM(rb_encdb_alias(RSTRING_PTR(alias), RSTRING_PTR(orig))); } +#endif +#if defined(HAVE_RB_ENC_ASSOCIATE) && defined(HAVE_RB_ENC_FIND) static VALUE encoding_spec_rb_enc_associate(VALUE self, VALUE obj, VALUE enc) { return rb_enc_associate(obj, NIL_P(enc) ? NULL : rb_enc_find(RSTRING_PTR(enc))); } +#endif +#if defined(HAVE_RB_ENC_ASSOCIATE_INDEX) && defined(HAVE_RB_ENC_FIND_INDEX) static VALUE encoding_spec_rb_enc_associate_index(VALUE self, VALUE obj, VALUE index) { return rb_enc_associate_index(obj, FIX2INT(index)); } +#endif +#ifdef HAVE_RB_ENC_COMPATIBLE static VALUE encoding_spec_rb_enc_compatible(VALUE self, VALUE a, VALUE b) { rb_encoding* enc = rb_enc_compatible(a, b); - if (!enc) return INT2FIX(0); + if(!enc) return INT2FIX(0); return rb_enc_from_encoding(enc); } +#endif +#ifdef HAVE_RB_ENC_COPY static VALUE encoding_spec_rb_enc_copy(VALUE self, VALUE dest, VALUE src) { rb_enc_copy(dest, src); return dest; } +#endif +#ifdef HAVE_RB_ENC_FIND static VALUE encoding_spec_rb_enc_find(VALUE self, VALUE name) { return rb_str_new2(rb_enc_find(RSTRING_PTR(name))->name); } +#endif +#ifdef HAVE_RB_ENC_FIND_INDEX static VALUE encoding_spec_rb_enc_find_index(VALUE self, VALUE name) { return INT2NUM(rb_enc_find_index(RSTRING_PTR(name))); } +#endif -static VALUE encoding_spec_rb_enc_isalnum(VALUE self, VALUE chr, VALUE encoding) { - rb_encoding *e = rb_to_encoding(encoding); - return rb_enc_isalnum(FIX2INT(chr), e) ? Qtrue : Qfalse; -} - -static VALUE encoding_spec_rb_enc_isspace(VALUE self, VALUE chr, VALUE encoding) { - rb_encoding *e = rb_to_encoding(encoding); - return rb_enc_isspace(FIX2INT(chr), e) ? Qtrue : Qfalse; -} - +#ifdef HAVE_RB_ENC_FROM_INDEX static VALUE encoding_spec_rb_enc_from_index(VALUE self, VALUE index) { return rb_str_new2(rb_enc_from_index(NUM2INT(index))->name); } +#endif -static VALUE encoding_spec_rb_enc_mbc_to_codepoint(VALUE self, VALUE str) { - char *p = RSTRING_PTR(str); - char *e = RSTRING_END(str); - return INT2FIX(rb_enc_mbc_to_codepoint(p, e, rb_enc_get(str))); -} - -static VALUE encoding_spec_rb_enc_mbcput(VALUE self, VALUE code, VALUE encoding) { - unsigned int c = FIX2UINT(code); - rb_encoding *enc = rb_to_encoding(encoding); - char buf[ONIGENC_CODE_TO_MBC_MAXLEN]; - memset(buf, '\1', sizeof(buf)); - int len = rb_enc_mbcput(c, buf, enc); - if (buf[len] != '\1') { - rb_raise(rb_eRuntimeError, "should not change bytes after len"); - } - return rb_enc_str_new(buf, len, enc); -} - +#ifdef HAVE_RB_ENC_FROM_ENCODING static VALUE encoding_spec_rb_enc_from_encoding(VALUE self, VALUE name) { return rb_enc_from_encoding(rb_enc_find(RSTRING_PTR(name))); } +#endif +#ifdef HAVE_RB_ENC_GET static VALUE encoding_spec_rb_enc_get(VALUE self, VALUE obj) { return rb_str_new2(rb_enc_get(obj)->name); } +#endif -static VALUE encoding_spec_rb_enc_precise_mbclen(VALUE self, VALUE str, VALUE offset) { - int o = FIX2INT(offset); - char *p = RSTRING_PTR(str); - char *e = p + o; - return INT2FIX(rb_enc_precise_mbclen(p, e, rb_enc_get(str))); -} - +#ifdef HAVE_RB_OBJ_ENCODING static VALUE encoding_spec_rb_obj_encoding(VALUE self, VALUE obj) { return rb_obj_encoding(obj); } +#endif +#ifdef HAVE_RB_ENC_GET_INDEX static VALUE encoding_spec_rb_enc_get_index(VALUE self, VALUE obj) { return INT2NUM(rb_enc_get_index(obj)); } +#endif +#if defined(HAVE_RB_ENC_SET_INDEX) \ + && defined(HAVE_RB_ENC_FIND_INDEX) \ + && defined(HAVE_RB_ENC_FIND_INDEX) static VALUE encoding_spec_rb_enc_set_index(VALUE self, VALUE obj, VALUE index) { int i = NUM2INT(index); @@ -168,7 +185,9 @@ static VALUE encoding_spec_rb_enc_set_index(VALUE self, VALUE obj, VALUE index) return rb_ary_new3(2, rb_str_new2(rb_enc_name(enc)), rb_str_new2(rb_enc_name(rb_enc_get(obj)))); } +#endif +#ifdef HAVE_RB_ENC_STR_CODERANGE static VALUE encoding_spec_rb_enc_str_coderange(VALUE self, VALUE str) { int coderange = rb_enc_str_coderange(str); @@ -185,30 +204,21 @@ static VALUE encoding_spec_rb_enc_str_coderange(VALUE self, VALUE str) { return ID2SYM(rb_intern("coderange_unrecognized")); } } +#endif -static VALUE encoding_spec_rb_enc_str_new_cstr(VALUE self, VALUE str, VALUE enc) { - rb_encoding *e = rb_to_encoding(enc); - return rb_enc_str_new_cstr(StringValueCStr(str), e); -} - -static VALUE encoding_spec_rb_enc_str_new_cstr_constant(VALUE self, VALUE enc) { - if (NIL_P(enc)) { - rb_encoding *e = NULL; - return rb_enc_str_new_static("test string literal", strlen("test string literal"), e); - } else { - rb_encoding *e = rb_to_encoding(enc); - return rb_enc_str_new_cstr("test string literal", e); - } -} - +#ifdef HAVE_RB_ENC_STR_NEW static VALUE encoding_spec_rb_enc_str_new(VALUE self, VALUE str, VALUE len, VALUE enc) { return rb_enc_str_new(RSTRING_PTR(str), FIX2INT(len), rb_to_encoding(enc)); } +#endif +#ifdef HAVE_ENCODING_GET static VALUE encoding_spec_ENCODING_GET(VALUE self, VALUE obj) { return INT2NUM(ENCODING_GET(obj)); } +#endif +#ifdef HAVE_ENCODING_SET static VALUE encoding_spec_ENCODING_SET(VALUE self, VALUE obj, VALUE index) { int i = NUM2INT(index); @@ -218,41 +228,36 @@ static VALUE encoding_spec_ENCODING_SET(VALUE self, VALUE obj, VALUE index) { return rb_ary_new3(2, rb_str_new2(rb_enc_name(enc)), rb_str_new2(rb_enc_name(rb_enc_get(obj)))); } +#endif +#if defined(HAVE_RB_ENC_TO_INDEX) && defined(HAVE_RB_ENC_FIND) static VALUE encoding_spec_rb_enc_to_index(VALUE self, VALUE name) { return INT2NUM(rb_enc_to_index(NIL_P(name) ? NULL : rb_enc_find(RSTRING_PTR(name)))); } +#endif +#ifdef HAVE_RB_TO_ENCODING static VALUE encoding_spec_rb_to_encoding(VALUE self, VALUE obj) { return rb_str_new2(rb_to_encoding(obj)->name); } +#endif -static rb_encoding** native_rb_encoding_pointer; - -static VALUE encoding_spec_rb_to_encoding_native_store(VALUE self, VALUE obj) { - rb_encoding* enc = rb_to_encoding(obj); - VALUE address = SIZET2NUM((size_t) native_rb_encoding_pointer); - *native_rb_encoding_pointer = enc; - return address; -} - -static VALUE encoding_spec_rb_to_encoding_native_name(VALUE self, VALUE address) { - rb_encoding** ptr = (rb_encoding**) NUM2SIZET(address); - rb_encoding* enc = *ptr; - return rb_str_new2(enc->name); -} - +#ifdef HAVE_RB_TO_ENCODING_INDEX static VALUE encoding_spec_rb_to_encoding_index(VALUE self, VALUE obj) { return INT2NUM(rb_to_encoding_index(obj)); } +#endif +#ifdef HAVE_RB_ENC_NTH static VALUE encoding_spec_rb_enc_nth(VALUE self, VALUE str, VALUE index) { char* start = RSTRING_PTR(str); char* end = start + RSTRING_LEN(str); char* ptr = rb_enc_nth(start, end, FIX2LONG(index), rb_enc_get(str)); return LONG2NUM(ptr - start); } +#endif +#ifdef HAVE_RB_ENC_CODEPOINT_LEN static VALUE encoding_spec_rb_enc_codepoint_len(VALUE self, VALUE str) { char* start = RSTRING_PTR(str); char* end = start + RSTRING_LEN(str); @@ -262,128 +267,156 @@ static VALUE encoding_spec_rb_enc_codepoint_len(VALUE self, VALUE str) { return rb_ary_new3(2, LONG2NUM(codepoint), LONG2NUM(len)); } - -static VALUE encoding_spec_rb_enc_str_asciionly_p(VALUE self, VALUE str) { - if (rb_enc_str_asciionly_p(str)) { - return Qtrue; - } else { - return Qfalse; - } -} - -static VALUE encoding_spec_rb_enc_raise(VALUE self, VALUE encoding, VALUE exception_class, VALUE format) { - rb_encoding *e = rb_to_encoding(encoding); - const char *f = RSTRING_PTR(format); - - rb_enc_raise(e, exception_class, "%s", f); -} - -static VALUE encoding_spec_rb_uv_to_utf8(VALUE self, VALUE buf, VALUE num) { - int len = rb_uv_to_utf8(RSTRING_PTR(buf), NUM2INT(num)); - RB_ENC_CODERANGE_CLEAR(buf); - return INT2NUM(len); -} - -static VALUE encoding_spec_ONIGENC_MBC_CASE_FOLD(VALUE self, VALUE str) { - char *beg = RSTRING_PTR(str); - char *beg_initial = beg; - char *end = beg + 2; - OnigUChar fold[ONIGENC_GET_CASE_FOLD_CODES_MAX_NUM]; - memset(fold, '\1', sizeof(fold)); - rb_encoding *enc = rb_enc_get(str); - int r = ONIGENC_MBC_CASE_FOLD(enc, ONIGENC_CASE_FOLD, &beg, (const OnigUChar *)end, fold); - if (r > 0 && fold[r] != '\1') { - rb_raise(rb_eRuntimeError, "should not change bytes after len"); - } - VALUE str_result = r <= 0 ? Qnil : rb_enc_str_new((char *)fold, r, enc); - long bytes_used = beg - beg_initial; - return rb_ary_new3(2, str_result, INT2FIX(bytes_used)); -} - -static VALUE encoding_spec_rb_enc_codelen(VALUE self, VALUE code, VALUE encoding) { - unsigned int c = FIX2UINT(code); - rb_encoding *enc = rb_to_encoding(encoding); - return INT2FIX(rb_enc_codelen(c, enc)); -} - -static VALUE encoding_spec_rb_enc_strlen(VALUE self, VALUE str, VALUE length, VALUE encoding) { - int l = FIX2INT(length); - char *p = RSTRING_PTR(str); - char *e = p + l; - - return LONG2FIX(rb_enc_strlen(p, e, rb_to_encoding(encoding))); -} - -static VALUE encoding_spec_rb_enc_left_char_head(VALUE self, VALUE str, VALUE offset) { - char *ptr = RSTRING_PTR(str); - char *result = rb_enc_left_char_head(ptr, ptr + NUM2INT(offset), RSTRING_END(str), rb_enc_get(str)); - return LONG2NUM(result - ptr); -} - -static VALUE encoding_spec_rb_define_dummy_encoding(VALUE self, VALUE name) { - return INT2NUM(rb_define_dummy_encoding(RSTRING_PTR(name))); -} +#endif void Init_encoding_spec(void) { VALUE cls; - native_rb_encoding_pointer = (rb_encoding**) malloc(sizeof(rb_encoding*)); - cls = rb_define_class("CApiEncodingSpecs", rb_cObject); + +#ifdef HAVE_ENC_CODERANGE_ASCIIONLY rb_define_method(cls, "ENC_CODERANGE_ASCIIONLY", encoding_spec_ENC_CODERANGE_ASCIIONLY, 1); +#endif +#ifdef HAVE_RB_USASCII_ENCODING rb_define_method(cls, "rb_usascii_encoding", encoding_spec_rb_usascii_encoding, 0); +#endif + +#ifdef HAVE_RB_USASCII_ENCINDEX rb_define_method(cls, "rb_usascii_encindex", encoding_spec_rb_usascii_encindex, 0); +#endif + +#ifdef HAVE_RB_ASCII8BIT_ENCODING rb_define_method(cls, "rb_ascii8bit_encoding", encoding_spec_rb_ascii8bit_encoding, 0); +#endif + +#ifdef HAVE_RB_ASCII8BIT_ENCINDEX rb_define_method(cls, "rb_ascii8bit_encindex", encoding_spec_rb_ascii8bit_encindex, 0); +#endif + +#ifdef HAVE_RB_UTF8_ENCODING rb_define_method(cls, "rb_utf8_encoding", encoding_spec_rb_utf8_encoding, 0); +#endif + +#ifdef HAVE_RB_UTF8_ENCINDEX rb_define_method(cls, "rb_utf8_encindex", encoding_spec_rb_utf8_encindex, 0); +#endif + +#ifdef HAVE_RB_LOCALE_ENCODING rb_define_method(cls, "rb_locale_encoding", encoding_spec_rb_locale_encoding, 0); +#endif + +#ifdef HAVE_RB_LOCALE_ENCINDEX rb_define_method(cls, "rb_locale_encindex", encoding_spec_rb_locale_encindex, 0); +#endif + +#ifdef HAVE_RB_FILESYSTEM_ENCODING rb_define_method(cls, "rb_filesystem_encoding", encoding_spec_rb_filesystem_encoding, 0); +#endif + +#ifdef HAVE_RB_FILESYSTEM_ENCINDEX rb_define_method(cls, "rb_filesystem_encindex", encoding_spec_rb_filesystem_encindex, 0); - rb_define_method(cls, "rb_default_internal_encoding", encoding_spec_rb_default_internal_encoding, 0); - rb_define_method(cls, "rb_default_external_encoding", encoding_spec_rb_default_external_encoding, 0); - rb_define_method(cls, "rb_enc_alias", encoding_spec_rb_enc_alias, 2); - rb_define_method(cls, "MBCLEN_CHARFOUND_P", encoding_spec_MBCLEN_CHARFOUND_P, 1); +#endif + +#ifdef HAVE_RB_DEFAULT_INTERNAL_ENCODING + rb_define_method(cls, "rb_default_internal_encoding", + encoding_spec_rb_default_internal_encoding, 0); +#endif + +#ifdef HAVE_RB_DEFAULT_EXTERNAL_ENCODING + rb_define_method(cls, "rb_default_external_encoding", + encoding_spec_rb_default_external_encoding, 0); +#endif + +#ifdef HAVE_RB_ENCDB_ALIAS + rb_define_method(cls, "rb_encdb_alias", encoding_spec_rb_encdb_alias, 2); +#endif + +#ifdef HAVE_RB_ENC_ASSOCIATE rb_define_method(cls, "rb_enc_associate", encoding_spec_rb_enc_associate, 2); +#endif + +#ifdef HAVE_RB_ENC_ASSOCIATE_INDEX rb_define_method(cls, "rb_enc_associate_index", encoding_spec_rb_enc_associate_index, 2); +#endif + +#ifdef HAVE_RB_ENC_COMPATIBLE rb_define_method(cls, "rb_enc_compatible", encoding_spec_rb_enc_compatible, 2); +#endif + +#ifdef HAVE_RB_ENC_COPY rb_define_method(cls, "rb_enc_copy", encoding_spec_rb_enc_copy, 2); - rb_define_method(cls, "rb_enc_codelen", encoding_spec_rb_enc_codelen, 2); - rb_define_method(cls, "rb_enc_strlen", encoding_spec_rb_enc_strlen, 3); +#endif + +#ifdef HAVE_RB_ENC_FIND rb_define_method(cls, "rb_enc_find", encoding_spec_rb_enc_find, 1); +#endif + +#ifdef HAVE_RB_ENC_FIND_INDEX rb_define_method(cls, "rb_enc_find_index", encoding_spec_rb_enc_find_index, 1); - rb_define_method(cls, "rb_enc_isalnum", encoding_spec_rb_enc_isalnum, 2); - rb_define_method(cls, "rb_enc_isspace", encoding_spec_rb_enc_isspace, 2); +#endif + +#ifdef HAVE_RB_ENC_FROM_INDEX rb_define_method(cls, "rb_enc_from_index", encoding_spec_rb_enc_from_index, 1); - rb_define_method(cls, "rb_enc_mbc_to_codepoint", encoding_spec_rb_enc_mbc_to_codepoint, 1); - rb_define_method(cls, "rb_enc_mbcput", encoding_spec_rb_enc_mbcput, 2); +#endif + +#ifdef HAVE_RB_ENC_FROM_ENCODING rb_define_method(cls, "rb_enc_from_encoding", encoding_spec_rb_enc_from_encoding, 1); +#endif + +#ifdef HAVE_RB_ENC_GET rb_define_method(cls, "rb_enc_get", encoding_spec_rb_enc_get, 1); - rb_define_method(cls, "rb_enc_precise_mbclen", encoding_spec_rb_enc_precise_mbclen, 2); +#endif + +#ifdef HAVE_RB_OBJ_ENCODING rb_define_method(cls, "rb_obj_encoding", encoding_spec_rb_obj_encoding, 1); +#endif + +#ifdef HAVE_RB_ENC_GET_INDEX rb_define_method(cls, "rb_enc_get_index", encoding_spec_rb_enc_get_index, 1); +#endif + +#if defined(HAVE_RB_ENC_SET_INDEX) \ + && defined(HAVE_RB_ENC_FIND_INDEX) \ + && defined(HAVE_RB_ENC_FIND_INDEX) rb_define_method(cls, "rb_enc_set_index", encoding_spec_rb_enc_set_index, 2); +#endif + +#ifdef HAVE_RB_ENC_STR_CODERANGE rb_define_method(cls, "rb_enc_str_coderange", encoding_spec_rb_enc_str_coderange, 1); - rb_define_method(cls, "rb_enc_str_new_cstr", encoding_spec_rb_enc_str_new_cstr, 2); - rb_define_method(cls, "rb_enc_str_new_cstr_constant", encoding_spec_rb_enc_str_new_cstr_constant, 1); +#endif + +#ifdef HAVE_RB_ENC_STR_NEW rb_define_method(cls, "rb_enc_str_new", encoding_spec_rb_enc_str_new, 3); +#endif + +#ifdef HAVE_ENCODING_GET rb_define_method(cls, "ENCODING_GET", encoding_spec_ENCODING_GET, 1); +#endif + +#ifdef HAVE_ENCODING_SET rb_define_method(cls, "ENCODING_SET", encoding_spec_ENCODING_SET, 2); +#endif + +#if defined(HAVE_RB_ENC_TO_INDEX) && defined(HAVE_RB_ENC_FIND) rb_define_method(cls, "rb_enc_to_index", encoding_spec_rb_enc_to_index, 1); +#endif + +#ifdef HAVE_RB_TO_ENCODING rb_define_method(cls, "rb_to_encoding", encoding_spec_rb_to_encoding, 1); - rb_define_method(cls, "rb_to_encoding_native_store", encoding_spec_rb_to_encoding_native_store, 1); - rb_define_method(cls, "rb_to_encoding_native_name", encoding_spec_rb_to_encoding_native_name, 1); +#endif + +#ifdef HAVE_RB_TO_ENCODING_INDEX rb_define_method(cls, "rb_to_encoding_index", encoding_spec_rb_to_encoding_index, 1); +#endif + +#ifdef HAVE_RB_ENC_NTH rb_define_method(cls, "rb_enc_nth", encoding_spec_rb_enc_nth, 2); +#endif + +#ifdef HAVE_RB_ENC_CODEPOINT_LEN rb_define_method(cls, "rb_enc_codepoint_len", encoding_spec_rb_enc_codepoint_len, 1); - rb_define_method(cls, "rb_enc_str_asciionly_p", encoding_spec_rb_enc_str_asciionly_p, 1); - rb_define_method(cls, "rb_enc_raise", encoding_spec_rb_enc_raise, 3); - rb_define_method(cls, "rb_uv_to_utf8", encoding_spec_rb_uv_to_utf8, 2); - rb_define_method(cls, "ONIGENC_MBC_CASE_FOLD", encoding_spec_ONIGENC_MBC_CASE_FOLD, 1); - rb_define_method(cls, "rb_enc_left_char_head", encoding_spec_rb_enc_left_char_head, 2); - rb_define_method(cls, "rb_define_dummy_encoding", encoding_spec_rb_define_dummy_encoding, 1); +#endif } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/enumerator_spec.c b/spec/ruby/optional/capi/ext/enumerator_spec.c index 917621c003..6b08feab52 100644 --- a/spec/ruby/optional/capi/ext/enumerator_spec.c +++ b/spec/ruby/optional/capi/ext/enumerator_spec.c @@ -5,26 +5,21 @@ extern "C" { #endif +#ifdef HAVE_RB_ENUMERATORIZE VALUE enumerator_spec_rb_enumeratorize(int argc, VALUE *argv, VALUE self) { VALUE obj, meth, args; rb_scan_args(argc, argv, "2*", &obj, &meth, &args); return rb_enumeratorize(obj, meth, (int)RARRAY_LEN(args), RARRAY_PTR(args)); } - -VALUE enumerator_spec_size_fn(VALUE obj, VALUE args, VALUE anEnum) { - return INT2NUM(7); -} - -VALUE enumerator_spec_rb_enumeratorize_with_size(int argc, VALUE *argv, VALUE self) { - VALUE obj, meth, args; - rb_scan_args(argc, argv, "2*", &obj, &meth, &args); - return rb_enumeratorize_with_size(obj, meth, (int)RARRAY_LEN(args), RARRAY_PTR(args), enumerator_spec_size_fn); -} +#endif void Init_enumerator_spec(void) { - VALUE cls = rb_define_class("CApiEnumeratorSpecs", rb_cObject); + VALUE cls; + cls = rb_define_class("CApiEnumeratorSpecs", rb_cObject); + +#ifdef HAVE_RB_ENUMERATORIZE rb_define_method(cls, "rb_enumeratorize", enumerator_spec_rb_enumeratorize, -1); - rb_define_method(cls, "rb_enumeratorize_with_size", enumerator_spec_rb_enumeratorize_with_size, -1); +#endif } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/exception_spec.c b/spec/ruby/optional/capi/ext/exception_spec.c index 0e8347ab0d..b37f74f03e 100644 --- a/spec/ruby/optional/capi/ext/exception_spec.c +++ b/spec/ruby/optional/capi/ext/exception_spec.c @@ -8,67 +8,63 @@ extern "C" { #endif -VALUE exception_spec_rb_errinfo(VALUE self) { - return rb_errinfo(); -} - +#ifdef HAVE_RB_EXC_NEW VALUE exception_spec_rb_exc_new(VALUE self, VALUE str) { char *cstr = StringValuePtr(str); return rb_exc_new(rb_eException, cstr, strlen(cstr)); } +#endif +#ifdef HAVE_RB_EXC_NEW2 VALUE exception_spec_rb_exc_new2(VALUE self, VALUE str) { char *cstr = StringValuePtr(str); return rb_exc_new2(rb_eException, cstr); } +#endif +#ifdef HAVE_RB_EXC_NEW3 VALUE exception_spec_rb_exc_new3(VALUE self, VALUE str) { return rb_exc_new3(rb_eException, str); } +#endif +#ifdef HAVE_RB_EXC_RAISE VALUE exception_spec_rb_exc_raise(VALUE self, VALUE exc) { - if (self != Qundef) rb_exc_raise(exc); + if (self != Qundef) rb_exc_raise(exc); return Qnil; } +#endif +#ifdef HAVE_RB_SET_ERRINFO VALUE exception_spec_rb_set_errinfo(VALUE self, VALUE exc) { rb_set_errinfo(exc); return Qnil; } - -VALUE exception_spec_rb_syserr_new(VALUE self, VALUE num, VALUE msg) { - int n = NUM2INT(num); - char *cstr = NULL; - - if (msg != Qnil) { - cstr = StringValuePtr(msg); - } - - return rb_syserr_new(n, cstr); -} - -VALUE exception_spec_rb_syserr_new_str(VALUE self, VALUE num, VALUE msg) { - int n = NUM2INT(num); - return rb_syserr_new_str(n, msg); -} - -VALUE exception_spec_rb_make_exception(VALUE self, VALUE ary) { - int argc = RARRAY_LENINT(ary); - VALUE *argv = RARRAY_PTR(ary); - return rb_make_exception(argc, argv); -} +#endif void Init_exception_spec(void) { - VALUE cls = rb_define_class("CApiExceptionSpecs", rb_cObject); - rb_define_method(cls, "rb_errinfo", exception_spec_rb_errinfo, 0); + VALUE cls; + cls = rb_define_class("CApiExceptionSpecs", rb_cObject); + +#ifdef HAVE_RB_EXC_NEW rb_define_method(cls, "rb_exc_new", exception_spec_rb_exc_new, 1); +#endif + +#ifdef HAVE_RB_EXC_NEW2 rb_define_method(cls, "rb_exc_new2", exception_spec_rb_exc_new2, 1); +#endif + +#ifdef HAVE_RB_EXC_NEW3 rb_define_method(cls, "rb_exc_new3", exception_spec_rb_exc_new3, 1); +#endif + +#ifdef HAVE_RB_EXC_RAISE rb_define_method(cls, "rb_exc_raise", exception_spec_rb_exc_raise, 1); +#endif + +#ifdef HAVE_RB_SET_ERRINFO rb_define_method(cls, "rb_set_errinfo", exception_spec_rb_set_errinfo, 1); - rb_define_method(cls, "rb_syserr_new", exception_spec_rb_syserr_new, 2); - rb_define_method(cls, "rb_syserr_new_str", exception_spec_rb_syserr_new_str, 2); - rb_define_method(cls, "rb_make_exception", exception_spec_rb_make_exception, 1); +#endif } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/fiber_spec.c b/spec/ruby/optional/capi/ext/fiber_spec.c deleted file mode 100644 index f06a54494e..0000000000 --- a/spec/ruby/optional/capi/ext/fiber_spec.c +++ /dev/null @@ -1,69 +0,0 @@ -#include "ruby.h" -#include "rubyspec.h" - -#ifdef __cplusplus -extern "C" { -#endif - -VALUE fiber_spec_rb_fiber_current(VALUE self) { - return rb_fiber_current(); -} - -VALUE fiber_spec_rb_fiber_alive_p(VALUE self, VALUE fiber) { - return rb_fiber_alive_p(fiber); -} - -VALUE fiber_spec_rb_fiber_resume(VALUE self, VALUE fiber, VALUE ary) { - long argc = RARRAY_LEN(ary); - VALUE *argv = (VALUE*) alloca(sizeof(VALUE) * argc); - int i; - - for (i = 0; i < argc; i++) { - argv[i] = rb_ary_entry(ary, i); - } - - return rb_fiber_resume(fiber, (int)argc, argv); -} - -VALUE fiber_spec_rb_fiber_yield(VALUE self, VALUE ary) { - long argc = RARRAY_LEN(ary); - VALUE *argv = (VALUE*) alloca(sizeof(VALUE) * argc); - int i; - - for (i = 0; i < argc; i++) { - argv[i] = rb_ary_entry(ary, i); - } - return rb_fiber_yield((int)argc, argv); -} - -VALUE fiber_spec_rb_fiber_new_function(RB_BLOCK_CALL_FUNC_ARGLIST(args, dummy)) { - return rb_funcall(args, rb_intern("inspect"), 0); -} - -VALUE fiber_spec_rb_fiber_new(VALUE self) { - return rb_fiber_new(fiber_spec_rb_fiber_new_function, Qnil); -} - -#ifdef RUBY_VERSION_IS_3_1 -VALUE fiber_spec_rb_fiber_raise(int argc, VALUE *argv, VALUE self) { - VALUE fiber = argv[0]; - return rb_fiber_raise(fiber, argc-1, argv+1); -} -#endif - -void Init_fiber_spec(void) { - VALUE cls = rb_define_class("CApiFiberSpecs", rb_cObject); - rb_define_method(cls, "rb_fiber_current", fiber_spec_rb_fiber_current, 0); - rb_define_method(cls, "rb_fiber_alive_p", fiber_spec_rb_fiber_alive_p, 1); - rb_define_method(cls, "rb_fiber_resume", fiber_spec_rb_fiber_resume, 2); - rb_define_method(cls, "rb_fiber_yield", fiber_spec_rb_fiber_yield, 1); - rb_define_method(cls, "rb_fiber_new", fiber_spec_rb_fiber_new, 0); - -#ifdef RUBY_VERSION_IS_3_1 - rb_define_method(cls, "rb_fiber_raise", fiber_spec_rb_fiber_raise, -1); -#endif -} - -#ifdef __cplusplus -} -#endif diff --git a/spec/ruby/optional/capi/ext/file_spec.c b/spec/ruby/optional/capi/ext/file_spec.c index cd4a653765..98f8db1595 100644 --- a/spec/ruby/optional/capi/ext/file_spec.c +++ b/spec/ruby/optional/capi/ext/file_spec.c @@ -5,23 +5,38 @@ extern "C" { #endif +#ifdef HAVE_RB_FILE_OPEN VALUE file_spec_rb_file_open(VALUE self, VALUE name, VALUE mode) { return rb_file_open(RSTRING_PTR(name), RSTRING_PTR(mode)); } +#endif +#ifdef HAVE_RB_FILE_OPEN_STR VALUE file_spec_rb_file_open_str(VALUE self, VALUE name, VALUE mode) { return rb_file_open_str(name, RSTRING_PTR(mode)); } +#endif +#ifdef HAVE_FILEPATHVALUE VALUE file_spec_FilePathValue(VALUE self, VALUE obj) { return FilePathValue(obj); } +#endif void Init_file_spec(void) { VALUE cls = rb_define_class("CApiFileSpecs", rb_cObject); + +#ifdef HAVE_RB_FILE_OPEN rb_define_method(cls, "rb_file_open", file_spec_rb_file_open, 2); +#endif + +#ifdef HAVE_RB_FILE_OPEN_STR rb_define_method(cls, "rb_file_open_str", file_spec_rb_file_open_str, 2); +#endif + +#ifdef HAVE_FILEPATHVALUE rb_define_method(cls, "FilePathValue", file_spec_FilePathValue, 1); +#endif } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/fixnum_spec.c b/spec/ruby/optional/capi/ext/fixnum_spec.c index 7048ce3f13..c3a207b387 100644 --- a/spec/ruby/optional/capi/ext/fixnum_spec.c +++ b/spec/ruby/optional/capi/ext/fixnum_spec.c @@ -15,10 +15,36 @@ static VALUE fixnum_spec_FIX2UINT(VALUE self, VALUE value) { return UINT2NUM(i); } +#ifdef HAVE_RB_FIX2UINT +static VALUE fixnum_spec_rb_fix2uint(VALUE self, VALUE value) { + unsigned long i = rb_fix2uint(value); + return ULONG2NUM(i); +} +#endif + +#ifdef HAVE_RB_FIX2INT +static VALUE fixnum_spec_rb_fix2int(VALUE self, VALUE value) { + long i = rb_fix2int(value); + return LONG2NUM(i); +} +#endif + void Init_fixnum_spec(void) { - VALUE cls = rb_define_class("CApiFixnumSpecs", rb_cObject); + VALUE cls; + cls = rb_define_class("CApiFixnumSpecs", rb_cObject); + rb_define_method(cls, "FIX2INT", fixnum_spec_FIX2INT, 1); rb_define_method(cls, "FIX2UINT", fixnum_spec_FIX2UINT, 1); + +#ifdef HAVE_RB_FIX2UINT + rb_define_method(cls, "rb_fix2uint", fixnum_spec_rb_fix2uint, 1); +#endif + +#ifdef HAVE_RB_FIX2INT + rb_define_method(cls, "rb_fix2int", fixnum_spec_rb_fix2int, 1); +#endif + + (void)cls; } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/float_spec.c b/spec/ruby/optional/capi/ext/float_spec.c index 3db05cef8c..15c74e62c9 100644 --- a/spec/ruby/optional/capi/ext/float_spec.c +++ b/spec/ruby/optional/capi/ext/float_spec.c @@ -7,6 +7,7 @@ extern "C" { #endif +#ifdef HAVE_RB_FLOAT_NEW static VALUE float_spec_new_zero(VALUE self) { double flt = 0; return rb_float_new(flt); @@ -16,30 +17,36 @@ static VALUE float_spec_new_point_five(VALUE self) { double flt = 0.555; return rb_float_new(flt); } +#endif +#ifdef HAVE_RB_RFLOAT static VALUE float_spec_rb_Float(VALUE self, VALUE float_str) { return rb_Float(float_str); } +#endif +#ifdef HAVE_RFLOAT_VALUE static VALUE float_spec_RFLOAT_VALUE(VALUE self, VALUE float_h) { return rb_float_new(RFLOAT_VALUE(float_h)); } - -static VALUE float_spec_RB_FLOAT_TYPE_P(VALUE self, VALUE val) { - if (RB_FLOAT_TYPE_P(val)) { - return Qtrue; - } else { - return Qfalse; - } -} +#endif void Init_float_spec(void) { - VALUE cls = rb_define_class("CApiFloatSpecs", rb_cObject); + VALUE cls; + cls = rb_define_class("CApiFloatSpecs", rb_cObject); + +#ifdef HAVE_RB_FLOAT_NEW rb_define_method(cls, "new_zero", float_spec_new_zero, 0); rb_define_method(cls, "new_point_five", float_spec_new_point_five, 0); +#endif + +#ifdef HAVE_RB_RFLOAT rb_define_method(cls, "rb_Float", float_spec_rb_Float, 1); +#endif + +#ifdef HAVE_RFLOAT_VALUE rb_define_method(cls, "RFLOAT_VALUE", float_spec_RFLOAT_VALUE, 1); - rb_define_method(cls, "RB_FLOAT_TYPE_P", float_spec_RB_FLOAT_TYPE_P, 1); +#endif } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/gc_spec.c b/spec/ruby/optional/capi/ext/gc_spec.c index 2637ad27ac..05341bb01d 100644 --- a/spec/ruby/optional/capi/ext/gc_spec.c +++ b/spec/ruby/optional/capi/ext/gc_spec.c @@ -5,18 +5,9 @@ extern "C" { #endif +#ifdef HAVE_RB_GC_REGISTER_ADDRESS VALUE registered_tagged_value; VALUE registered_reference_value; -VALUE registered_before_rb_gc_register_address; -VALUE registered_before_rb_global_variable_string; -VALUE registered_before_rb_global_variable_bignum; -VALUE registered_before_rb_global_variable_float; -VALUE registered_after_rb_global_variable_string; -VALUE registered_after_rb_global_variable_bignum; -VALUE registered_after_rb_global_variable_float; -VALUE rb_gc_register_address_outside_init; - -VALUE rb_gc_register_mark_object_not_referenced_float; static VALUE registered_tagged_address(VALUE self) { return registered_tagged_value; @@ -25,123 +16,55 @@ static VALUE registered_tagged_address(VALUE self) { static VALUE registered_reference_address(VALUE self) { return registered_reference_value; } +#endif -static VALUE get_registered_before_rb_gc_register_address(VALUE self) { - return registered_before_rb_gc_register_address; -} - -static VALUE get_registered_before_rb_global_variable_string(VALUE self) { - return registered_before_rb_global_variable_string; -} - -static VALUE get_registered_before_rb_global_variable_bignum(VALUE self) { - return registered_before_rb_global_variable_bignum; -} - -static VALUE get_registered_before_rb_global_variable_float(VALUE self) { - return registered_before_rb_global_variable_float; -} - -static VALUE get_registered_after_rb_global_variable_string(VALUE self) { - return registered_after_rb_global_variable_string; -} - -static VALUE get_registered_after_rb_global_variable_bignum(VALUE self) { - return registered_after_rb_global_variable_bignum; -} - -static VALUE get_registered_after_rb_global_variable_float(VALUE self) { - return registered_after_rb_global_variable_float; -} - -static VALUE gc_spec_rb_gc_register_address(VALUE self) { - rb_gc_register_address(&rb_gc_register_address_outside_init); - rb_gc_register_address_outside_init = rb_str_new_cstr("rb_gc_register_address() outside Init_"); - return rb_gc_register_address_outside_init; -} - -static VALUE gc_spec_rb_gc_unregister_address(VALUE self) { - rb_gc_unregister_address(&rb_gc_register_address_outside_init); - return Qnil; -} - -static VALUE gc_spec_rb_gc_enable(VALUE self) { +#ifdef HAVE_RB_GC_ENABLE +static VALUE gc_spec_rb_gc_enable() { return rb_gc_enable(); } +#endif -static VALUE gc_spec_rb_gc_disable(VALUE self) { +#ifdef HAVE_RB_GC_DISABLE +static VALUE gc_spec_rb_gc_disable() { return rb_gc_disable(); } +#endif -static VALUE gc_spec_rb_gc(VALUE self) { +#ifdef HAVE_RB_GC +static VALUE gc_spec_rb_gc() { rb_gc(); return Qnil; } +#endif -static VALUE gc_spec_rb_gc_latest_gc_info(VALUE self, VALUE hash_or_key) { - return rb_gc_latest_gc_info(hash_or_key); -} - -static VALUE gc_spec_rb_gc_adjust_memory_usage(VALUE self, VALUE diff) { - rb_gc_adjust_memory_usage(NUM2SSIZET(diff)); - return Qnil; -} - -static VALUE gc_spec_rb_gc_register_mark_object(VALUE self, VALUE obj) { - rb_gc_register_mark_object(obj); - return Qnil; -} - -static VALUE gc_spec_rb_gc_register_mark_object_not_referenced_float(VALUE self) { - return rb_gc_register_mark_object_not_referenced_float; -} void Init_gc_spec(void) { - VALUE cls = rb_define_class("CApiGCSpecs", rb_cObject); - - rb_gc_register_address(®istered_tagged_value); - rb_gc_register_address(®istered_reference_value); - rb_gc_register_address(®istered_before_rb_gc_register_address); - rb_global_variable(®istered_before_rb_global_variable_string); - rb_global_variable(®istered_before_rb_global_variable_bignum); - rb_global_variable(®istered_before_rb_global_variable_float); + VALUE cls; + cls = rb_define_class("CApiGCSpecs", rb_cObject); +#ifdef HAVE_RB_GC_REGISTER_ADDRESS registered_tagged_value = INT2NUM(10); registered_reference_value = rb_str_new2("Globally registered data"); - registered_before_rb_gc_register_address = rb_str_new_cstr("registered before rb_gc_register_address()"); - registered_before_rb_global_variable_string = rb_str_new_cstr("registered before rb_global_variable()"); - registered_before_rb_global_variable_bignum = LL2NUM(INT64_MAX); - registered_before_rb_global_variable_float = DBL2NUM(3.14); - - registered_after_rb_global_variable_string = rb_str_new_cstr("registered after rb_global_variable()"); - rb_global_variable(®istered_after_rb_global_variable_string); - registered_after_rb_global_variable_bignum = LL2NUM(INT64_MAX); - rb_global_variable(®istered_after_rb_global_variable_bignum); - registered_after_rb_global_variable_float = DBL2NUM(6.28); - rb_global_variable(®istered_after_rb_global_variable_float); - - rb_gc_register_mark_object_not_referenced_float = DBL2NUM(1.61); - rb_gc_register_mark_object(rb_gc_register_mark_object_not_referenced_float); + rb_gc_register_address(®istered_tagged_value); + rb_gc_register_address(®istered_reference_value); rb_define_method(cls, "registered_tagged_address", registered_tagged_address, 0); rb_define_method(cls, "registered_reference_address", registered_reference_address, 0); - rb_define_method(cls, "registered_before_rb_gc_register_address", get_registered_before_rb_gc_register_address, 0); - rb_define_method(cls, "registered_before_rb_global_variable_string", get_registered_before_rb_global_variable_string, 0); - rb_define_method(cls, "registered_before_rb_global_variable_bignum", get_registered_before_rb_global_variable_bignum, 0); - rb_define_method(cls, "registered_before_rb_global_variable_float", get_registered_before_rb_global_variable_float, 0); - rb_define_method(cls, "registered_after_rb_global_variable_string", get_registered_after_rb_global_variable_string, 0); - rb_define_method(cls, "registered_after_rb_global_variable_bignum", get_registered_after_rb_global_variable_bignum, 0); - rb_define_method(cls, "registered_after_rb_global_variable_float", get_registered_after_rb_global_variable_float, 0); - rb_define_method(cls, "rb_gc_register_address", gc_spec_rb_gc_register_address, 0); - rb_define_method(cls, "rb_gc_unregister_address", gc_spec_rb_gc_unregister_address, 0); +#endif + +#ifdef HAVE_RB_GC_ENABLE rb_define_method(cls, "rb_gc_enable", gc_spec_rb_gc_enable, 0); +#endif + +#ifdef HAVE_RB_GC_DISABLE rb_define_method(cls, "rb_gc_disable", gc_spec_rb_gc_disable, 0); +#endif + +#ifdef HAVE_RB_GC rb_define_method(cls, "rb_gc", gc_spec_rb_gc, 0); - rb_define_method(cls, "rb_gc_adjust_memory_usage", gc_spec_rb_gc_adjust_memory_usage, 1); - rb_define_method(cls, "rb_gc_register_mark_object", gc_spec_rb_gc_register_mark_object, 1); - rb_define_method(cls, "rb_gc_register_mark_object_not_referenced_float", gc_spec_rb_gc_register_mark_object_not_referenced_float, 0); - rb_define_method(cls, "rb_gc_latest_gc_info", gc_spec_rb_gc_latest_gc_info, 1); +#endif + } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/globals_spec.c b/spec/ruby/optional/capi/ext/globals_spec.c index 20dea1a05a..0c28a7f8ab 100644 --- a/spec/ruby/optional/capi/ext/globals_spec.c +++ b/spec/ruby/optional/capi/ext/globals_spec.c @@ -5,31 +5,20 @@ extern "C" { #endif +#ifdef HAVE_RB_DEFINE_HOOKED_VARIABLE VALUE g_hooked_var; -VALUE var_2x_getter(ID id, VALUE *data) { - return *data; -} - void var_2x_setter(VALUE val, ID id, VALUE *var) { - *var = INT2NUM(NUM2INT(val) * 2); + *var = INT2NUM(NUM2INT(val) * 2); } static VALUE sb_define_hooked_variable(VALUE self, VALUE var_name) { - rb_define_hooked_variable(StringValuePtr(var_name), &g_hooked_var, var_2x_getter, var_2x_setter); - return Qnil; -} - -static VALUE sb_define_hooked_variable_default_accessors(VALUE self, VALUE var_name) { - rb_define_hooked_variable(StringValuePtr(var_name), &g_hooked_var, (rb_gvar_getter_t*) NULL, (rb_gvar_setter_t*) NULL); + rb_define_hooked_variable(StringValuePtr(var_name), &g_hooked_var, 0, var_2x_setter); return Qnil; } +#endif -static VALUE sb_define_hooked_variable_null_var(VALUE self, VALUE var_name) { - rb_define_hooked_variable(StringValuePtr(var_name), NULL, (rb_gvar_getter_t*) NULL, (rb_gvar_setter_t*) NULL); - return Qnil; -} - +#ifdef HAVE_RB_DEFINE_READONLY_VARIABLE VALUE g_ro_var; static VALUE sb_define_readonly_variable(VALUE self, VALUE var_name, VALUE val) { @@ -37,7 +26,9 @@ static VALUE sb_define_readonly_variable(VALUE self, VALUE var_name, VALUE val) rb_define_readonly_variable(StringValuePtr(var_name), &g_ro_var); return Qnil; } +#endif +#ifdef HAVE_RB_DEFINE_VARIABLE VALUE g_var; static VALUE sb_get_global_value(VALUE self) { @@ -49,111 +40,158 @@ static VALUE sb_define_variable(VALUE self, VALUE var_name, VALUE val) { rb_define_variable(StringValuePtr(var_name), &g_var); return Qnil; } +#endif -long virtual_var_storage; - -VALUE incrementing_getter(ID id, VALUE *data) { - return LONG2FIX(virtual_var_storage++); -} - -void incrementing_setter(VALUE val, ID id, VALUE *data) { - virtual_var_storage = FIX2LONG(val); -} - -static VALUE sb_define_virtual_variable_default_accessors(VALUE self, VALUE name) { - rb_define_virtual_variable(StringValuePtr(name), (rb_gvar_getter_t*) NULL, (rb_gvar_setter_t*) NULL); - return Qnil; -} - -static VALUE sb_define_virtual_variable_incrementing_accessors(VALUE self, VALUE name) { - rb_define_virtual_variable(StringValuePtr(name), incrementing_getter, incrementing_setter); - return Qnil; -} - +#ifdef HAVE_RB_F_GLOBAL_VARIABLES static VALUE sb_f_global_variables(VALUE self) { return rb_f_global_variables(); } +#endif +#ifdef HAVE_RB_GV_GET static VALUE sb_gv_get(VALUE self, VALUE var) { return rb_gv_get(StringValuePtr(var)); } +#endif +#ifdef HAVE_RB_GV_SET static VALUE sb_gv_set(VALUE self, VALUE var, VALUE val) { return rb_gv_set(StringValuePtr(var), val); } +#endif +#ifdef HAVE_RB_STDIN static VALUE global_spec_rb_stdin(VALUE self) { return rb_stdin; } +#endif +#ifdef HAVE_RB_STDOUT static VALUE global_spec_rb_stdout(VALUE self) { return rb_stdout; } +#endif +#ifdef HAVE_RB_STDERR static VALUE global_spec_rb_stderr(VALUE self) { return rb_stderr; } +#endif +#ifdef HAVE_RB_DEFOUT static VALUE global_spec_rb_defout(VALUE self) { return rb_defout; } +#endif -static VALUE global_spec_rb_fs(VALUE self) { - return rb_fs; -} - +#ifdef HAVE_RB_RS static VALUE global_spec_rb_rs(VALUE self) { return rb_rs; } +#endif +#ifdef HAVE_RB_DEFAULT_RS static VALUE global_spec_rb_default_rs(VALUE self) { return rb_default_rs; } +#endif +#ifdef HAVE_RB_OUTPUT_RS static VALUE global_spec_rb_output_rs(VALUE self) { return rb_output_rs; } +#endif +#ifdef HAVE_RB_OUTPUT_FS static VALUE global_spec_rb_output_fs(VALUE self) { return rb_output_fs; } +#endif +#ifdef HAVE_RB_LASTLINE_SET static VALUE global_spec_rb_lastline_set(VALUE self, VALUE line) { rb_lastline_set(line); return Qnil; } +#endif +#ifdef HAVE_RB_LASTLINE_GET static VALUE global_spec_rb_lastline_get(VALUE self) { return rb_lastline_get(); } +#endif void Init_globals_spec(void) { - VALUE cls = rb_define_class("CApiGlobalSpecs", rb_cObject); + VALUE cls; + cls = rb_define_class("CApiGlobalSpecs", rb_cObject); + +#ifdef HAVE_RB_DEFINE_HOOKED_VARIABLE g_hooked_var = Qnil; rb_define_method(cls, "rb_define_hooked_variable_2x", sb_define_hooked_variable, 1); - rb_define_method(cls, "rb_define_hooked_variable_default_accessors", sb_define_hooked_variable_default_accessors, 1); - rb_define_method(cls, "rb_define_hooked_variable_null_var", sb_define_hooked_variable_null_var, 1); +#endif + +#ifdef HAVE_RB_DEFINE_READONLY_VARIABLE g_ro_var = Qnil; rb_define_method(cls, "rb_define_readonly_variable", sb_define_readonly_variable, 2); +#endif + +#ifdef HAVE_RB_DEFINE_VARIABLE g_var = Qnil; rb_define_method(cls, "rb_define_variable", sb_define_variable, 2); - rb_define_method(cls, "rb_define_virtual_variable_default_accessors", sb_define_virtual_variable_default_accessors, 1); - rb_define_method(cls, "rb_define_virtual_variable_incrementing_accessors", sb_define_virtual_variable_incrementing_accessors, 1); rb_define_method(cls, "sb_get_global_value", sb_get_global_value, 0); +#endif + +#ifdef HAVE_RB_F_GLOBAL_VARIABLES rb_define_method(cls, "rb_f_global_variables", sb_f_global_variables, 0); +#endif + +#ifdef HAVE_RB_GV_GET rb_define_method(cls, "sb_gv_get", sb_gv_get, 1); +#endif + +#ifdef HAVE_RB_GV_SET rb_define_method(cls, "sb_gv_set", sb_gv_set, 2); +#endif + +#ifdef HAVE_RB_STDIN rb_define_method(cls, "rb_stdin", global_spec_rb_stdin, 0); +#endif + +#ifdef HAVE_RB_STDOUT rb_define_method(cls, "rb_stdout", global_spec_rb_stdout, 0); +#endif + +#ifdef HAVE_RB_STDERR rb_define_method(cls, "rb_stderr", global_spec_rb_stderr, 0); +#endif + +#ifdef HAVE_RB_DEFOUT rb_define_method(cls, "rb_defout", global_spec_rb_defout, 0); - rb_define_method(cls, "rb_fs", global_spec_rb_fs, 0); +#endif + +#ifdef HAVE_RB_RS rb_define_method(cls, "rb_rs", global_spec_rb_rs, 0); +#endif + +#ifdef HAVE_RB_DEFAULT_RS rb_define_method(cls, "rb_default_rs", global_spec_rb_default_rs, 0); +#endif + +#ifdef HAVE_RB_OUTPUT_RS rb_define_method(cls, "rb_output_rs", global_spec_rb_output_rs, 0); +#endif + +#ifdef HAVE_RB_OUTPUT_FS rb_define_method(cls, "rb_output_fs", global_spec_rb_output_fs, 0); +#endif + +#ifdef HAVE_RB_LASTLINE_SET rb_define_method(cls, "rb_lastline_set", global_spec_rb_lastline_set, 1); +#endif + +#ifdef HAVE_RB_LASTLINE_GET rb_define_method(cls, "rb_lastline_get", global_spec_rb_lastline_get, 0); +#endif } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/hash_spec.c b/spec/ruby/optional/capi/ext/hash_spec.c index 0e5b3d1c0a..73e7ef5c13 100644 --- a/spec/ruby/optional/capi/ext/hash_spec.c +++ b/spec/ruby/optional/capi/ext/hash_spec.c @@ -5,26 +5,37 @@ extern "C" { #endif +#ifdef HAVE_RB_HASH VALUE hash_spec_rb_hash(VALUE self, VALUE hash) { return rb_hash(hash); } +#endif +#ifdef HAVE_RB_HASH2 VALUE hash_spec_rb_Hash(VALUE self, VALUE val) { return rb_Hash(val); } +#endif +#ifdef HAVE_RB_HASH_DUP VALUE hash_spec_rb_hash_dup(VALUE self, VALUE hash) { return rb_hash_dup(hash); } +#endif +#ifdef HAVE_RB_HASH_FETCH VALUE hash_spec_rb_hash_fetch(VALUE self, VALUE hash, VALUE key) { return rb_hash_fetch(hash, key); } +#endif +#ifdef HAVE_RB_HASH_FREEZE VALUE hash_spec_rb_hash_freeze(VALUE self, VALUE hash) { return rb_hash_freeze(hash); } +#endif +#ifdef HAVE_RB_HASH_AREF VALUE hash_spec_rb_hash_aref(VALUE self, VALUE hash, VALUE key) { return rb_hash_aref(hash, key); } @@ -33,23 +44,33 @@ VALUE hash_spec_rb_hash_aref_nil(VALUE self, VALUE hash, VALUE key) { VALUE ret = rb_hash_aref(hash, key); return NIL_P(ret) ? Qtrue : Qfalse; } +#endif +#ifdef HAVE_RB_HASH_ASET VALUE hash_spec_rb_hash_aset(VALUE self, VALUE hash, VALUE key, VALUE val) { return rb_hash_aset(hash, key, val); } +#endif +#ifdef HAVE_RB_HASH_CLEAR VALUE hash_spec_rb_hash_clear(VALUE self, VALUE hash) { return rb_hash_clear(hash); } +#endif +#ifdef HAVE_RB_HASH_DELETE VALUE hash_spec_rb_hash_delete(VALUE self, VALUE hash, VALUE key) { return rb_hash_delete(hash, key); } +#endif +#ifdef HAVE_RB_HASH_DELETE_IF VALUE hash_spec_rb_hash_delete_if(VALUE self, VALUE hash) { return rb_hash_delete_if(hash); } +#endif +#ifdef HAVE_RB_HASH_FOREACH static int foreach_i(VALUE key, VALUE val, VALUE other) { rb_hash_aset(other, key, val); return 0; /* ST_CONTINUE; */ @@ -82,7 +103,9 @@ VALUE hash_spec_rb_hash_foreach_delete(VALUE self, VALUE hsh) { rb_hash_foreach(hsh, foreach_delete_i, other); return other; } +#endif +#ifdef HAVE_RB_HASH_LOOKUP VALUE hash_spec_rb_hash_lookup(VALUE self, VALUE hash, VALUE key) { return rb_hash_lookup(hash, key); } @@ -91,92 +114,103 @@ VALUE hash_spec_rb_hash_lookup_nil(VALUE self, VALUE hash, VALUE key) { VALUE ret = rb_hash_lookup(hash, key); return ret == Qnil ? Qtrue : Qfalse; } +#endif +#ifdef HAVE_RB_HASH_LOOKUP2 VALUE hash_spec_rb_hash_lookup2(VALUE self, VALUE hash, VALUE key, VALUE def) { return rb_hash_lookup2(hash, key, def); } +#endif -VALUE hash_spec_rb_hash_lookup2_default_undef(VALUE self, VALUE hash, VALUE key) { - VALUE ret = rb_hash_lookup2(hash, key, Qundef); - return ret == Qundef ? Qtrue : Qfalse; -} - +#ifdef HAVE_RB_HASH_NEW VALUE hash_spec_rb_hash_new(VALUE self) { return rb_hash_new(); } - -#ifdef RUBY_VERSION_IS_3_2 -VALUE hash_spec_rb_hash_new_capa(VALUE self, VALUE capacity) { - return rb_hash_new_capa(NUM2LONG(capacity)); -} #endif -VALUE rb_ident_hash_new(void); /* internal.h, used in ripper */ - -VALUE hash_spec_rb_ident_hash_new(VALUE self) { - return rb_ident_hash_new(); -} - +#ifdef HAVE_RB_HASH_SIZE VALUE hash_spec_rb_hash_size(VALUE self, VALUE hash) { return rb_hash_size(hash); } +#endif +#ifdef HAVE_RB_HASH_SET_IFNONE VALUE hash_spec_rb_hash_set_ifnone(VALUE self, VALUE hash, VALUE def) { return rb_hash_set_ifnone(hash, def); } - -VALUE hash_spec_compute_a_hash_code(VALUE self, VALUE seed) { - int int_seed = FIX2INT(seed); - st_index_t h = rb_hash_start(int_seed); - h = rb_hash_uint32(h, 540u); - h = rb_hash_uint32(h, 340u); - h = rb_hash_end(h); - return ULONG2NUM(h); -} - -VALUE hash_spec_rb_hash_bulk_insert(VALUE self, VALUE array_len, VALUE array, VALUE hash) { - VALUE* ptr; - - if (array == Qnil) { - ptr = NULL; - } else { - ptr = RARRAY_PTR(array); - } - - long len = FIX2LONG(array_len); - rb_hash_bulk_insert(len, ptr, hash); - return Qnil; -} +#endif void Init_hash_spec(void) { - VALUE cls = rb_define_class("CApiHashSpecs", rb_cObject); + VALUE cls; + cls = rb_define_class("CApiHashSpecs", rb_cObject); + +#ifdef HAVE_RB_HASH rb_define_method(cls, "rb_hash", hash_spec_rb_hash, 1); +#endif + +#ifdef HAVE_RB_HASH2 rb_define_method(cls, "rb_Hash", hash_spec_rb_Hash, 1); +#endif + +#ifdef HAVE_RB_HASH_DUP rb_define_method(cls, "rb_hash_dup", hash_spec_rb_hash_dup, 1); +#endif + +#ifdef HAVE_RB_HASH_FREEZE rb_define_method(cls, "rb_hash_freeze", hash_spec_rb_hash_freeze, 1); +#endif + +#ifdef HAVE_RB_HASH_AREF rb_define_method(cls, "rb_hash_aref", hash_spec_rb_hash_aref, 2); rb_define_method(cls, "rb_hash_aref_nil", hash_spec_rb_hash_aref_nil, 2); +#endif + +#ifdef HAVE_RB_HASH_ASET rb_define_method(cls, "rb_hash_aset", hash_spec_rb_hash_aset, 3); +#endif + +#ifdef HAVE_RB_HASH_CLEAR rb_define_method(cls, "rb_hash_clear", hash_spec_rb_hash_clear, 1); +#endif + +#ifdef HAVE_RB_HASH_DELETE rb_define_method(cls, "rb_hash_delete", hash_spec_rb_hash_delete, 2); +#endif + +#ifdef HAVE_RB_HASH_DELETE_IF rb_define_method(cls, "rb_hash_delete_if", hash_spec_rb_hash_delete_if, 1); +#endif + +#ifdef HAVE_RB_HASH_FETCH rb_define_method(cls, "rb_hash_fetch", hash_spec_rb_hash_fetch, 2); +#endif + +#ifdef HAVE_RB_HASH_FOREACH rb_define_method(cls, "rb_hash_foreach", hash_spec_rb_hash_foreach, 1); rb_define_method(cls, "rb_hash_foreach_stop", hash_spec_rb_hash_foreach_stop, 1); rb_define_method(cls, "rb_hash_foreach_delete", hash_spec_rb_hash_foreach_delete, 1); +#endif + +#ifdef HAVE_RB_HASH_LOOKUP rb_define_method(cls, "rb_hash_lookup_nil", hash_spec_rb_hash_lookup_nil, 2); rb_define_method(cls, "rb_hash_lookup", hash_spec_rb_hash_lookup, 2); +#endif + +#ifdef HAVE_RB_HASH_LOOKUP2 rb_define_method(cls, "rb_hash_lookup2", hash_spec_rb_hash_lookup2, 3); - rb_define_method(cls, "rb_hash_lookup2_default_undef", hash_spec_rb_hash_lookup2_default_undef, 2); +#endif + +#ifdef HAVE_RB_HASH_NEW rb_define_method(cls, "rb_hash_new", hash_spec_rb_hash_new, 0); -#ifdef RUBY_VERSION_IS_3_2 - rb_define_method(cls, "rb_hash_new_capa", hash_spec_rb_hash_new_capa, 1); #endif - rb_define_method(cls, "rb_ident_hash_new", hash_spec_rb_ident_hash_new, 0); + +#ifdef HAVE_RB_HASH_SIZE rb_define_method(cls, "rb_hash_size", hash_spec_rb_hash_size, 1); +#endif + +#ifdef HAVE_RB_HASH_SET_IFNONE rb_define_method(cls, "rb_hash_set_ifnone", hash_spec_rb_hash_set_ifnone, 2); - rb_define_method(cls, "compute_a_hash_code", hash_spec_compute_a_hash_code, 1); - rb_define_method(cls, "rb_hash_bulk_insert", hash_spec_rb_hash_bulk_insert, 3); +#endif } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/integer_spec.c b/spec/ruby/optional/capi/ext/integer_spec.c index 792fc0652a..821d8373c9 100644 --- a/spec/ruby/optional/capi/ext/integer_spec.c +++ b/spec/ruby/optional/capi/ext/integer_spec.c @@ -5,21 +5,21 @@ extern "C" { #endif +#ifdef HAVE_RB_INTEGER_PACK static VALUE integer_spec_rb_integer_pack(VALUE self, VALUE value, - VALUE words, VALUE numwords, VALUE wordsize, VALUE nails, VALUE flags) { + VALUE words, VALUE numwords, VALUE wordsize, VALUE nails, VALUE flags) +{ int result = rb_integer_pack(value, (void*)RSTRING_PTR(words), FIX2INT(numwords), FIX2INT(wordsize), FIX2INT(nails), FIX2INT(flags)); return INT2FIX(result); } - -RUBY_EXTERN VALUE rb_int_positive_pow(long x, unsigned long y); /* internal.h, used in ripper */ - -static VALUE integer_spec_rb_int_positive_pow(VALUE self, VALUE a, VALUE b) { - return rb_int_positive_pow(FIX2INT(a), FIX2INT(b)); -} +#endif void Init_integer_spec(void) { - VALUE cls = rb_define_class("CApiIntegerSpecs", rb_cObject); +#ifdef HAVE_RB_INTEGER_PACK + VALUE cls; + cls = rb_define_class("CApiIntegerSpecs", rb_cObject); + rb_define_const(cls, "MSWORD", INT2NUM(INTEGER_PACK_MSWORD_FIRST)); rb_define_const(cls, "LSWORD", INT2NUM(INTEGER_PACK_LSWORD_FIRST)); rb_define_const(cls, "MSBYTE", INT2NUM(INTEGER_PACK_MSBYTE_FIRST)); @@ -32,9 +32,9 @@ void Init_integer_spec(void) { rb_define_const(cls, "NEGATIVE", INT2NUM(INTEGER_PACK_NEGATIVE)); rb_define_method(cls, "rb_integer_pack", integer_spec_rb_integer_pack, 6); - rb_define_method(cls, "rb_int_positive_pow", integer_spec_rb_int_positive_pow, 2); +#endif } #ifdef __cplusplus -} +extern "C" { #endif diff --git a/spec/ruby/optional/capi/ext/io_spec.c b/spec/ruby/optional/capi/ext/io_spec.c index d1cc861e21..40069bd54b 100644 --- a/spec/ruby/optional/capi/ext/io_spec.c +++ b/spec/ruby/optional/capi/ext/io_spec.c @@ -21,33 +21,33 @@ static int set_non_blocking(int fd) { int flags = 1; return ioctl(fd, FIOBIO, &flags); #else -#define SET_NON_BLOCKING_FAILS_ALWAYS 1 errno = ENOSYS; return -1; #endif } +#ifdef HAVE_GET_OPEN_FILE static int io_spec_get_fd(VALUE io) { -#ifdef RUBY_VERSION_IS_3_1 - return rb_io_descriptor(io); -#else rb_io_t* fp; GetOpenFile(io, fp); return fp->fd; -#endif } VALUE io_spec_GetOpenFile_fd(VALUE self, VALUE io) { return INT2NUM(io_spec_get_fd(io)); } +#endif +#ifdef HAVE_RB_IO_ADDSTR VALUE io_spec_rb_io_addstr(VALUE self, VALUE io, VALUE str) { return rb_io_addstr(io, str); } +#endif +#ifdef HAVE_RB_IO_PRINTF VALUE io_spec_rb_io_printf(VALUE self, VALUE io, VALUE ary) { long argc = RARRAY_LEN(ary); - VALUE *argv = (VALUE*) alloca(sizeof(VALUE) * argc); + VALUE *argv = alloca(sizeof(VALUE) * argc); int i; for (i = 0; i < argc; i++) { @@ -56,10 +56,12 @@ VALUE io_spec_rb_io_printf(VALUE self, VALUE io, VALUE ary) { return rb_io_printf((int)argc, argv, io); } +#endif +#ifdef HAVE_RB_IO_PRINT VALUE io_spec_rb_io_print(VALUE self, VALUE io, VALUE ary) { long argc = RARRAY_LEN(ary); - VALUE *argv = (VALUE*) alloca(sizeof(VALUE) * argc); + VALUE *argv = alloca(sizeof(VALUE) * argc); int i; for (i = 0; i < argc; i++) { @@ -68,10 +70,12 @@ VALUE io_spec_rb_io_print(VALUE self, VALUE io, VALUE ary) { return rb_io_print((int)argc, argv, io); } +#endif +#ifdef HAVE_RB_IO_PUTS VALUE io_spec_rb_io_puts(VALUE self, VALUE io, VALUE ary) { long argc = RARRAY_LEN(ary); - VALUE *argv = (VALUE*) alloca(sizeof(VALUE) * argc); + VALUE *argv = alloca(sizeof(VALUE) * argc); int i; for (i = 0; i < argc; i++) { @@ -80,61 +84,68 @@ VALUE io_spec_rb_io_puts(VALUE self, VALUE io, VALUE ary) { return rb_io_puts((int)argc, argv, io); } +#endif +#ifdef HAVE_RB_IO_WRITE VALUE io_spec_rb_io_write(VALUE self, VALUE io, VALUE str) { return rb_io_write(io, str); } +#endif +#ifdef HAVE_RB_IO_CHECK_IO VALUE io_spec_rb_io_check_io(VALUE self, VALUE io) { return rb_io_check_io(io); } +#endif +#ifdef HAVE_RB_IO_CHECK_READABLE VALUE io_spec_rb_io_check_readable(VALUE self, VALUE io) { rb_io_t* fp; GetOpenFile(io, fp); rb_io_check_readable(fp); return Qnil; } +#endif +#ifdef HAVE_RB_IO_CHECK_WRITABLE VALUE io_spec_rb_io_check_writable(VALUE self, VALUE io) { rb_io_t* fp; GetOpenFile(io, fp); rb_io_check_writable(fp); return Qnil; } +#endif +#ifdef HAVE_RB_IO_CHECK_CLOSED VALUE io_spec_rb_io_check_closed(VALUE self, VALUE io) { rb_io_t* fp; GetOpenFile(io, fp); rb_io_check_closed(fp); return Qnil; } +#endif +#ifdef HAVE_RB_IO_TAINT_CHECK VALUE io_spec_rb_io_taint_check(VALUE self, VALUE io) { /*rb_io_t* fp; GetOpenFile(io, fp);*/ rb_io_taint_check(io); return io; } +#endif +#ifdef HAVE_RB_IO_WAIT_READABLE #define RB_IO_WAIT_READABLE_BUF 13 -#ifdef SET_NON_BLOCKING_FAILS_ALWAYS -NORETURN(VALUE io_spec_rb_io_wait_readable(VALUE self, VALUE io, VALUE read_p)); -#endif - VALUE io_spec_rb_io_wait_readable(VALUE self, VALUE io, VALUE read_p) { int fd = io_spec_get_fd(io); -#ifndef SET_NON_BLOCKING_FAILS_ALWAYS char buf[RB_IO_WAIT_READABLE_BUF]; int ret, saved_errno; -#endif if (set_non_blocking(fd) == -1) rb_sys_fail("set_non_blocking failed"); -#ifndef SET_NON_BLOCKING_FAILS_ALWAYS - if (RTEST(read_p)) { + if(RTEST(read_p)) { if (read(fd, buf, RB_IO_WAIT_READABLE_BUF) != -1) { return Qnil; } @@ -143,13 +154,9 @@ VALUE io_spec_rb_io_wait_readable(VALUE self, VALUE io, VALUE read_p) { errno = saved_errno; } -#ifdef RUBY_VERSION_IS_3_1 - ret = rb_io_maybe_wait_readable(errno, io, Qnil); -#else ret = rb_io_wait_readable(fd); -#endif - if (RTEST(read_p)) { + if(RTEST(read_p)) { ssize_t r = read(fd, buf, RB_IO_WAIT_READABLE_BUF); if (r != RB_IO_WAIT_READABLE_BUF) { perror("read"); @@ -160,284 +167,134 @@ VALUE io_spec_rb_io_wait_readable(VALUE self, VALUE io, VALUE read_p) { } return ret ? Qtrue : Qfalse; -#else - UNREACHABLE_RETURN(Qnil); -#endif } +#endif +#ifdef HAVE_RB_IO_WAIT_WRITABLE VALUE io_spec_rb_io_wait_writable(VALUE self, VALUE io) { -#ifdef RUBY_VERSION_IS_3_1 - int ret = rb_io_maybe_wait_writable(errno, io, Qnil); -#else int ret = rb_io_wait_writable(io_spec_get_fd(io)); -#endif return ret ? Qtrue : Qfalse; } - -#ifdef RUBY_VERSION_IS_3_1 -VALUE io_spec_rb_io_maybe_wait_writable(VALUE self, VALUE error, VALUE io, VALUE timeout) { - int ret = rb_io_maybe_wait_writable(NUM2INT(error), io, timeout); - return INT2NUM(ret); -} -#endif - -#ifdef RUBY_VERSION_IS_3_1 -#ifdef SET_NON_BLOCKING_FAILS_ALWAYS -NORETURN(VALUE io_spec_rb_io_maybe_wait_readable(VALUE self, VALUE error, VALUE io, VALUE timeout, VALUE read_p)); -#endif - -VALUE io_spec_rb_io_maybe_wait_readable(VALUE self, VALUE error, VALUE io, VALUE timeout, VALUE read_p) { - int fd = io_spec_get_fd(io); -#ifndef SET_NON_BLOCKING_FAILS_ALWAYS - char buf[RB_IO_WAIT_READABLE_BUF]; - int ret, saved_errno; -#endif - - if (set_non_blocking(fd) == -1) - rb_sys_fail("set_non_blocking failed"); - -#ifndef SET_NON_BLOCKING_FAILS_ALWAYS - if (RTEST(read_p)) { - if (read(fd, buf, RB_IO_WAIT_READABLE_BUF) != -1) { - return Qnil; - } - saved_errno = errno; - rb_ivar_set(self, rb_intern("@write_data"), Qtrue); - errno = saved_errno; - } - - // main part - ret = rb_io_maybe_wait_readable(NUM2INT(error), io, timeout); - - if (RTEST(read_p)) { - ssize_t r = read(fd, buf, RB_IO_WAIT_READABLE_BUF); - if (r != RB_IO_WAIT_READABLE_BUF) { - perror("read"); - return SSIZET2NUM(r); - } - rb_ivar_set(self, rb_intern("@read_data"), - rb_str_new(buf, RB_IO_WAIT_READABLE_BUF)); - } - - return INT2NUM(ret); -#else - UNREACHABLE_RETURN(Qnil); -#endif -} -#endif - -#ifdef RUBY_VERSION_IS_3_1 -VALUE io_spec_rb_io_maybe_wait(VALUE self, VALUE error, VALUE io, VALUE events, VALUE timeout) { - return rb_io_maybe_wait(NUM2INT(error), io, events, timeout); -} #endif +#ifdef HAVE_RB_THREAD_WAIT_FD VALUE io_spec_rb_thread_wait_fd(VALUE self, VALUE io) { rb_thread_wait_fd(io_spec_get_fd(io)); return Qnil; } - -VALUE io_spec_rb_wait_for_single_fd(VALUE self, VALUE io, VALUE events, VALUE secs, VALUE usecs) { -#ifdef RUBY_VERSION_IS_3_0 - VALUE timeout = Qnil; - if (!NIL_P(secs)) { - timeout = rb_float_new((double)FIX2INT(secs) + (0.000001f * FIX2INT(usecs))); - } - VALUE result = rb_io_wait(io, events, timeout); - if (result == Qfalse) return INT2FIX(0); - else return result; -#else - struct timeval tv; - if (!NIL_P(secs)) { - tv.tv_sec = FIX2INT(secs); - tv.tv_usec = FIX2INT(usecs); - } - int fd = io_spec_get_fd(io); - return INT2FIX(rb_wait_for_single_fd(fd, FIX2INT(events), NIL_P(secs) ? NULL : &tv)); #endif -} +#ifdef HAVE_RB_THREAD_FD_WRITABLE VALUE io_spec_rb_thread_fd_writable(VALUE self, VALUE io) { rb_thread_fd_writable(io_spec_get_fd(io)); return Qnil; } +#endif -VALUE io_spec_rb_thread_fd_select_read(VALUE self, VALUE io) { - int fd = io_spec_get_fd(io); - - rb_fdset_t fds; - rb_fd_init(&fds); - rb_fd_set(fd, &fds); - - int r = rb_thread_fd_select(fd + 1, &fds, NULL, NULL, NULL); - rb_fd_term(&fds); - return INT2FIX(r); -} - -VALUE io_spec_rb_thread_fd_select_write(VALUE self, VALUE io) { - int fd = io_spec_get_fd(io); - - rb_fdset_t fds; - rb_fd_init(&fds); - rb_fd_set(fd, &fds); - - int r = rb_thread_fd_select(fd + 1, NULL, &fds, NULL, NULL); - rb_fd_term(&fds); - return INT2FIX(r); -} - -VALUE io_spec_rb_thread_fd_select_timeout(VALUE self, VALUE io) { - int fd = io_spec_get_fd(io); - - struct timeval timeout; - timeout.tv_sec = 10; - timeout.tv_usec = 20; - - rb_fdset_t fds; - rb_fd_init(&fds); - rb_fd_set(fd, &fds); - - int r = rb_thread_fd_select(fd + 1, NULL, &fds, NULL, &timeout); - rb_fd_term(&fds); - return INT2FIX(r); -} - +#ifdef HAVE_RB_IO_BINMODE VALUE io_spec_rb_io_binmode(VALUE self, VALUE io) { return rb_io_binmode(io); } +#endif +#ifdef HAVE_RB_FD_FIX_CLOEXEC VALUE io_spec_rb_fd_fix_cloexec(VALUE self, VALUE io) { rb_fd_fix_cloexec(io_spec_get_fd(io)); return Qnil; } +#endif +#ifdef HAVE_RB_CLOEXEC_OPEN VALUE io_spec_rb_cloexec_open(VALUE self, VALUE path, VALUE flags, VALUE mode) { const char *pathname = StringValuePtr(path); int fd = rb_cloexec_open(pathname, FIX2INT(flags), FIX2INT(mode)); return rb_funcall(rb_cIO, rb_intern("for_fd"), 1, INT2FIX(fd)); } +#endif +#ifdef HAVE_RB_IO_CLOSE VALUE io_spec_rb_io_close(VALUE self, VALUE io) { return rb_io_close(io); } - -VALUE io_spec_rb_io_set_nonblock(VALUE self, VALUE io) { - rb_io_t* fp; -#ifdef F_GETFL - int flags; #endif - GetOpenFile(io, fp); - rb_io_set_nonblock(fp); -#ifdef F_GETFL - flags = fcntl(io_spec_get_fd(io), F_GETFL, 0); - return flags & O_NONBLOCK ? Qtrue : Qfalse; -#else - return Qfalse; -#endif -} -/* - * this is needed to ensure rb_io_wait_*able functions behave - * predictably because errno may be set to unexpected values - * otherwise. - */ -static VALUE io_spec_errno_set(VALUE self, VALUE val) { - int e = NUM2INT(val); - errno = e; - return val; -} +void Init_io_spec(void) { + VALUE cls = rb_define_class("CApiIOSpecs", rb_cObject); -VALUE io_spec_mode_sync_flag(VALUE self, VALUE io) { - int mode; -#ifdef RUBY_VERSION_IS_3_3 - mode = rb_io_mode(io); -#else - rb_io_t *fp; - GetOpenFile(io, fp); - mode = fp->mode; +#ifdef HAVE_GET_OPEN_FILE + rb_define_method(cls, "GetOpenFile_fd", io_spec_GetOpenFile_fd, 1); #endif - if (mode & FMODE_SYNC) { - return Qtrue; - } else { - return Qfalse; - } -} - -#if defined(RUBY_VERSION_IS_3_3) || defined(TRUFFLERUBY) -static VALUE io_spec_rb_io_mode(VALUE self, VALUE io) { - return INT2FIX(rb_io_mode(io)); -} - -static VALUE io_spec_rb_io_path(VALUE self, VALUE io) { - return rb_io_path(io); -} - -static VALUE io_spec_rb_io_closed_p(VALUE self, VALUE io) { - return rb_io_closed_p(io); -} - -static VALUE io_spec_rb_io_open_descriptor(VALUE self, VALUE klass, VALUE descriptor, VALUE mode, VALUE path, VALUE timeout, VALUE internal_encoding, VALUE external_encoding, VALUE ecflags, VALUE ecopts) { - struct rb_io_encoding *io_encoding; - - io_encoding = (struct rb_io_encoding *) malloc(sizeof(struct rb_io_encoding)); - - io_encoding->enc = rb_to_encoding(internal_encoding); - io_encoding->enc2 = rb_to_encoding(external_encoding); - io_encoding->ecflags = FIX2INT(ecflags); - io_encoding->ecopts = ecopts; - - return rb_io_open_descriptor(klass, FIX2INT(descriptor), FIX2INT(mode), path, timeout, io_encoding); -} -static VALUE io_spec_rb_io_open_descriptor_without_encoding(VALUE self, VALUE klass, VALUE descriptor, VALUE mode, VALUE path, VALUE timeout) { - return rb_io_open_descriptor(klass, FIX2INT(descriptor), FIX2INT(mode), path, timeout, NULL); -} +#ifdef HAVE_RB_IO_ADDSTR + rb_define_method(cls, "rb_io_addstr", io_spec_rb_io_addstr, 2); #endif -void Init_io_spec(void) { - VALUE cls = rb_define_class("CApiIOSpecs", rb_cObject); - rb_define_method(cls, "GetOpenFile_fd", io_spec_GetOpenFile_fd, 1); - rb_define_method(cls, "rb_io_addstr", io_spec_rb_io_addstr, 2); +#ifdef HAVE_RB_IO_PRINTF rb_define_method(cls, "rb_io_printf", io_spec_rb_io_printf, 2); +#endif + +#ifdef HAVE_RB_IO_PRINT rb_define_method(cls, "rb_io_print", io_spec_rb_io_print, 2); +#endif + +#ifdef HAVE_RB_IO_PUTS rb_define_method(cls, "rb_io_puts", io_spec_rb_io_puts, 2); +#endif + +#ifdef HAVE_RB_IO_WRITE rb_define_method(cls, "rb_io_write", io_spec_rb_io_write, 2); +#endif + +#ifdef HAVE_RB_IO_CLOSE rb_define_method(cls, "rb_io_close", io_spec_rb_io_close, 1); +#endif + +#ifdef HAVE_RB_IO_CHECK_IO rb_define_method(cls, "rb_io_check_io", io_spec_rb_io_check_io, 1); +#endif + +#ifdef HAVE_RB_IO_CHECK_READABLE rb_define_method(cls, "rb_io_check_readable", io_spec_rb_io_check_readable, 1); +#endif + +#ifdef HAVE_RB_IO_CHECK_WRITABLE rb_define_method(cls, "rb_io_check_writable", io_spec_rb_io_check_writable, 1); +#endif + +#ifdef HAVE_RB_IO_CHECK_CLOSED rb_define_method(cls, "rb_io_check_closed", io_spec_rb_io_check_closed, 1); - rb_define_method(cls, "rb_io_set_nonblock", io_spec_rb_io_set_nonblock, 1); +#endif + +#ifdef HAVE_RB_IO_TAINT_CHECK rb_define_method(cls, "rb_io_taint_check", io_spec_rb_io_taint_check, 1); +#endif + +#ifdef HAVE_RB_IO_WAIT_READABLE rb_define_method(cls, "rb_io_wait_readable", io_spec_rb_io_wait_readable, 2); +#endif + +#ifdef HAVE_RB_IO_WAIT_WRITABLE rb_define_method(cls, "rb_io_wait_writable", io_spec_rb_io_wait_writable, 1); -#ifdef RUBY_VERSION_IS_3_1 - rb_define_method(cls, "rb_io_maybe_wait_writable", io_spec_rb_io_maybe_wait_writable, 3); - rb_define_method(cls, "rb_io_maybe_wait_readable", io_spec_rb_io_maybe_wait_readable, 4); - rb_define_method(cls, "rb_io_maybe_wait", io_spec_rb_io_maybe_wait, 4); #endif + +#ifdef HAVE_RB_THREAD_WAIT_FD rb_define_method(cls, "rb_thread_wait_fd", io_spec_rb_thread_wait_fd, 1); +#endif + +#ifdef HAVE_RB_THREAD_FD_WRITABLE rb_define_method(cls, "rb_thread_fd_writable", io_spec_rb_thread_fd_writable, 1); - rb_define_method(cls, "rb_thread_fd_select_read", io_spec_rb_thread_fd_select_read, 1); - rb_define_method(cls, "rb_thread_fd_select_write", io_spec_rb_thread_fd_select_write, 1); - rb_define_method(cls, "rb_thread_fd_select_timeout", io_spec_rb_thread_fd_select_timeout, 1); - rb_define_method(cls, "rb_wait_for_single_fd", io_spec_rb_wait_for_single_fd, 4); +#endif + +#ifdef HAVE_RB_IO_BINMODE rb_define_method(cls, "rb_io_binmode", io_spec_rb_io_binmode, 1); +#endif + +#ifdef HAVE_RB_FD_FIX_CLOEXEC rb_define_method(cls, "rb_fd_fix_cloexec", io_spec_rb_fd_fix_cloexec, 1); +#endif + +#ifdef HAVE_RB_CLOEXEC_OPEN rb_define_method(cls, "rb_cloexec_open", io_spec_rb_cloexec_open, 3); - rb_define_method(cls, "errno=", io_spec_errno_set, 1); - rb_define_method(cls, "rb_io_mode_sync_flag", io_spec_mode_sync_flag, 1); -#if defined(RUBY_VERSION_IS_3_3) || defined(TRUFFLERUBY) - rb_define_method(cls, "rb_io_mode", io_spec_rb_io_mode, 1); - rb_define_method(cls, "rb_io_path", io_spec_rb_io_path, 1); - rb_define_method(cls, "rb_io_closed_p", io_spec_rb_io_closed_p, 1); - rb_define_method(cls, "rb_io_open_descriptor", io_spec_rb_io_open_descriptor, 9); - rb_define_method(cls, "rb_io_open_descriptor_without_encoding", io_spec_rb_io_open_descriptor_without_encoding, 5); - rb_define_const(cls, "FMODE_READABLE", INT2FIX(FMODE_READABLE)); - rb_define_const(cls, "FMODE_WRITABLE", INT2FIX(FMODE_WRITABLE)); - rb_define_const(cls, "FMODE_BINMODE", INT2FIX(FMODE_BINMODE)); - rb_define_const(cls, "FMODE_TEXTMODE", INT2FIX(FMODE_TEXTMODE)); - rb_define_const(cls, "ECONV_UNIVERSAL_NEWLINE_DECORATOR", INT2FIX(ECONV_UNIVERSAL_NEWLINE_DECORATOR)); #endif } diff --git a/spec/ruby/optional/capi/ext/kernel_spec.c b/spec/ruby/optional/capi/ext/kernel_spec.c index abd8d20ff4..226354e18a 100644 --- a/spec/ruby/optional/capi/ext/kernel_spec.c +++ b/spec/ruby/optional/capi/ext/kernel_spec.c @@ -7,30 +7,34 @@ extern "C" { #endif -static VALUE kernel_spec_call_proc(VALUE arg_array) { +VALUE kernel_spec_call_proc(VALUE arg_array) { VALUE arg = rb_ary_pop(arg_array); VALUE proc = rb_ary_pop(arg_array); return rb_funcall(proc, rb_intern("call"), 1, arg); } +#ifdef HAVE_RB_BLOCK_GIVEN_P static VALUE kernel_spec_rb_block_given_p(VALUE self) { return rb_block_given_p() ? Qtrue : Qfalse; } +#endif +#ifdef HAVE_RB_NEED_BLOCK VALUE kernel_spec_rb_need_block(VALUE self) { rb_need_block(); return Qnil; } +#endif +#ifdef HAVE_RB_BLOCK_PROC VALUE kernel_spec_rb_block_proc(VALUE self) { return rb_block_proc(); } +#endif -VALUE kernel_spec_rb_block_lambda(VALUE self) { - return rb_block_lambda(); -} +#ifdef HAVE_RB_BLOCK_CALL -VALUE block_call_inject(RB_BLOCK_CALL_FUNC_ARGLIST(yield_value, data2)) { +VALUE block_call_inject(VALUE yield_value, VALUE data2) { /* yield_value yields the first block argument */ VALUE elem = yield_value; VALUE elem_incr = INT2FIX(FIX2INT(elem) + 1); @@ -41,7 +45,7 @@ VALUE kernel_spec_rb_block_call(VALUE self, VALUE ary) { return rb_block_call(ary, rb_intern("map"), 0, NULL, block_call_inject, Qnil); } -VALUE block_call_inject_multi_arg(RB_BLOCK_CALL_FUNC_ARGLIST(yield_value, data2)) { +VALUE block_call_inject_multi_arg(VALUE yield_value, VALUE data2, int argc, VALUE argv[]) { /* yield_value yields the first block argument */ VALUE sum = yield_value; VALUE elem = argv[1]; @@ -55,32 +59,13 @@ VALUE kernel_spec_rb_block_call_multi_arg(VALUE self, VALUE ary) { return rb_block_call(ary, rb_intern("inject"), 1, method_args, block_call_inject_multi_arg, Qnil); } -static VALUE return_extra_data(RB_BLOCK_CALL_FUNC_ARGLIST(yield_value, extra_data)) { - return extra_data; -} - -VALUE rb_block_call_extra_data(VALUE self, VALUE object) { - return rb_block_call(object, rb_intern("instance_exec"), 0, NULL, return_extra_data, object); -} - VALUE kernel_spec_rb_block_call_no_func(VALUE self, VALUE ary) { - return rb_block_call(ary, rb_intern("map"), 0, NULL, (rb_block_call_func_t)NULL, Qnil); -} - -VALUE kernel_spec_rb_frame_this_func(VALUE self) { - return ID2SYM(rb_frame_this_func()); + return rb_block_call(ary, rb_intern("map"), 0, NULL, NULL, Qnil); } -VALUE kernel_spec_rb_category_warn_deprecated(VALUE self) { - rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, "foo"); - return Qnil; -} - -VALUE kernel_spec_rb_category_warn_deprecated_with_integer_extra_value(VALUE self, VALUE value) { - rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, "foo %d", FIX2INT(value)); - return Qnil; -} +#endif +#ifdef HAVE_RB_ENSURE VALUE kernel_spec_rb_ensure(VALUE self, VALUE main_proc, VALUE arg, VALUE ensure_proc, VALUE arg2) { VALUE main_array, ensure_array; @@ -96,31 +81,35 @@ VALUE kernel_spec_rb_ensure(VALUE self, VALUE main_proc, VALUE arg, return rb_ensure(kernel_spec_call_proc, main_array, kernel_spec_call_proc, ensure_array); } +#endif -VALUE kernel_spec_call_proc_with_catch(RB_BLOCK_CALL_FUNC_ARGLIST(arg, data)) { +#ifdef HAVE_RB_CATCH +VALUE kernel_spec_call_proc_with_catch(VALUE arg, VALUE data) { return rb_funcall(data, rb_intern("call"), 0); } VALUE kernel_spec_rb_catch(VALUE self, VALUE sym, VALUE main_proc) { return rb_catch(StringValuePtr(sym), kernel_spec_call_proc_with_catch, main_proc); } +#endif -VALUE kernel_spec_call_proc_with_catch_obj(RB_BLOCK_CALL_FUNC_ARGLIST(arg, data)) { +#ifdef HAVE_RB_CATCH_OBJ +VALUE kernel_spec_call_proc_with_catch_obj(VALUE arg, VALUE data) { return rb_funcall(data, rb_intern("call"), 0); } VALUE kernel_spec_rb_catch_obj(VALUE self, VALUE obj, VALUE main_proc) { - return rb_catch_obj(obj, kernel_spec_call_proc_with_catch_obj, main_proc); + return rb_catch_obj(obj, kernel_spec_call_proc_with_catch, main_proc); } +#endif +#ifdef HAVE_RB_EVAL_STRING VALUE kernel_spec_rb_eval_string(VALUE self, VALUE str) { return rb_eval_string(RSTRING_PTR(str)); } +#endif -VALUE kernel_spec_rb_eval_cmd_kw(VALUE self, VALUE cmd, VALUE args, VALUE kw_splat) { - return rb_eval_cmd_kw(cmd, args, NUM2INT(kw_splat)); -} - +#ifdef HAVE_RB_RAISE VALUE kernel_spec_rb_raise(VALUE self, VALUE hash) { rb_hash_aset(hash, ID2SYM(rb_intern("stage")), ID2SYM(rb_intern("before"))); if (self != Qundef) @@ -128,27 +117,24 @@ VALUE kernel_spec_rb_raise(VALUE self, VALUE hash) { rb_hash_aset(hash, ID2SYM(rb_intern("stage")), ID2SYM(rb_intern("after"))); return Qnil; } +#endif +#ifdef HAVE_RB_THROW VALUE kernel_spec_rb_throw(VALUE self, VALUE result) { if (self != Qundef) rb_throw("foo", result); return ID2SYM(rb_intern("rb_throw_failed")); } +#endif +#ifdef HAVE_RB_THROW_OBJ VALUE kernel_spec_rb_throw_obj(VALUE self, VALUE obj, VALUE result) { if (self != Qundef) rb_throw_obj(obj, result); return ID2SYM(rb_intern("rb_throw_failed")); } +#endif -VALUE kernel_spec_rb_errinfo(VALUE self) { - return rb_errinfo(); -} - -VALUE kernel_spec_rb_set_errinfo(VALUE self, VALUE exc) { - rb_set_errinfo(exc); - return Qnil; -} - -static VALUE kernel_spec_call_proc_with_raised_exc(VALUE arg_array, VALUE raised_exc) { +#ifdef HAVE_RB_RESCUE +VALUE kernel_spec_call_proc_with_raised_exc(VALUE arg_array, VALUE raised_exc) { VALUE argv[2]; int argc; @@ -160,7 +146,7 @@ static VALUE kernel_spec_call_proc_with_raised_exc(VALUE arg_array, VALUE raised argc = 2; - return rb_funcallv(proc, rb_intern("call"), argc, argv); + return rb_funcall2(proc, rb_intern("call"), argc, argv); } VALUE kernel_spec_rb_rescue(VALUE self, VALUE main_proc, VALUE arg, @@ -171,10 +157,6 @@ VALUE kernel_spec_rb_rescue(VALUE self, VALUE main_proc, VALUE arg, rb_ary_push(main_array, main_proc); rb_ary_push(main_array, arg); - if (raise_proc == Qnil) { - return rb_rescue(kernel_spec_call_proc, main_array, NULL, arg2); - } - raise_array = rb_ary_new(); rb_ary_push(raise_array, raise_proc); rb_ary_push(raise_array, arg2); @@ -182,7 +164,9 @@ VALUE kernel_spec_rb_rescue(VALUE self, VALUE main_proc, VALUE arg, return rb_rescue(kernel_spec_call_proc, main_array, kernel_spec_call_proc_with_raised_exc, raise_array); } +#endif +#ifdef HAVE_RB_RESCUE2 VALUE kernel_spec_rb_rescue2(int argc, VALUE *args, VALUE self) { VALUE main_array, raise_array; @@ -195,74 +179,53 @@ VALUE kernel_spec_rb_rescue2(int argc, VALUE *args, VALUE self) { rb_ary_push(raise_array, args[3]); return rb_rescue2(kernel_spec_call_proc, main_array, - kernel_spec_call_proc_with_raised_exc, raise_array, args[4], args[5], (VALUE)0); + kernel_spec_call_proc, raise_array, args[4], args[5], (VALUE)0); } +#endif +#ifdef HAVE_RB_PROTECT static VALUE kernel_spec_rb_protect_yield(VALUE self, VALUE obj, VALUE ary) { int status = 0; VALUE res = rb_protect(rb_yield, obj, &status); rb_ary_store(ary, 0, INT2NUM(23)); - rb_ary_store(ary, 1, res); - if (status) { - rb_jump_tag(status); - } - return res; -} - -static VALUE kernel_spec_rb_protect_ignore_status(VALUE self, VALUE obj, VALUE ary) { - int status = 0; - VALUE res = rb_protect(rb_yield, obj, &status); - rb_ary_store(ary, 0, INT2NUM(23)); - rb_ary_store(ary, 1, res); - return rb_errinfo(); -} - -static VALUE kernel_spec_rb_protect_null_status(VALUE self, VALUE obj) { - return rb_protect(rb_yield, obj, NULL); -} - -static VALUE kernel_spec_rb_eval_string_protect(VALUE self, VALUE str, VALUE ary) { - int status = 0; - VALUE res = rb_eval_string_protect(RSTRING_PTR(str), &status); - rb_ary_store(ary, 0, INT2NUM(23)); - rb_ary_store(ary, 1, res); if (status) { rb_jump_tag(status); } return res; } +#endif +#ifdef HAVE_RB_SYS_FAIL VALUE kernel_spec_rb_sys_fail(VALUE self, VALUE msg) { errno = 1; - if (msg == Qnil) { - rb_sys_fail(NULL); + if(msg == Qnil) { + rb_sys_fail(0); } else if (self != Qundef) { rb_sys_fail(StringValuePtr(msg)); } return Qnil; } +#endif +#ifdef HAVE_RB_SYSERR_FAIL VALUE kernel_spec_rb_syserr_fail(VALUE self, VALUE err, VALUE msg) { - if (msg == Qnil) { + if(msg == Qnil) { rb_syserr_fail(NUM2INT(err), NULL); } else if (self != Qundef) { rb_syserr_fail(NUM2INT(err), StringValuePtr(msg)); } return Qnil; } +#endif -VALUE kernel_spec_rb_syserr_fail_str(VALUE self, VALUE err, VALUE msg) { - if (self != Qundef) { - rb_syserr_fail_str(NUM2INT(err), msg); - } - return Qnil; -} - +#ifdef HAVE_RB_WARN VALUE kernel_spec_rb_warn(VALUE self, VALUE msg) { rb_warn("%s", StringValuePtr(msg)); return Qnil; } +#endif +#ifdef HAVE_RB_YIELD static VALUE kernel_spec_rb_yield(VALUE self, VALUE obj) { return rb_yield(obj); } @@ -294,28 +257,25 @@ static VALUE kernel_indirected(int (*compar)(const void *, const void *)) { static VALUE kernel_spec_rb_yield_indirected(VALUE self, VALUE obj) { return kernel_indirected(kernel_cb); } +#endif +#ifdef HAVE_RB_YIELD_SPLAT static VALUE kernel_spec_rb_yield_splat(VALUE self, VALUE ary) { return rb_yield_splat(ary); } +#endif +#ifdef HAVE_RB_YIELD_VALUES static VALUE kernel_spec_rb_yield_values(VALUE self, VALUE obj1, VALUE obj2) { return rb_yield_values(2, obj1, obj2); } +#endif -static VALUE kernel_spec_rb_yield_values2(VALUE self, VALUE ary) { - long len = RARRAY_LEN(ary); - VALUE *args = (VALUE*)alloca(sizeof(VALUE) * len); - for (int i = 0; i < len; i++) { - args[i] = rb_ary_entry(ary, i); - } - return rb_yield_values2((int)len, args); -} - +#ifdef HAVE_RB_EXEC_RECURSIVE static VALUE do_rec(VALUE obj, VALUE arg, int is_rec) { - if (is_rec) { + if(is_rec) { return obj; - } else if (arg == Qtrue) { + } else if(arg == Qtrue) { return rb_exec_recursive(do_rec, obj, Qnil); } else { return Qnil; @@ -325,126 +285,164 @@ static VALUE do_rec(VALUE obj, VALUE arg, int is_rec) { static VALUE kernel_spec_rb_exec_recursive(VALUE self, VALUE obj) { return rb_exec_recursive(do_rec, obj, Qtrue); } +#endif +#ifdef HAVE_RB_SET_END_PROC static void write_io(VALUE io) { - rb_funcall(io, rb_intern("write"), 1, rb_str_new2("in write_io")); + rb_funcall(io, rb_intern("write"), 1, rb_str_new2("e")); } static VALUE kernel_spec_rb_set_end_proc(VALUE self, VALUE io) { rb_set_end_proc(write_io, io); return Qnil; } +#endif +#ifdef HAVE_RB_F_SPRINTF static VALUE kernel_spec_rb_f_sprintf(VALUE self, VALUE ary) { return rb_f_sprintf((int)RARRAY_LEN(ary), RARRAY_PTR(ary)); } +#endif -static VALUE kernel_spec_rb_str_format(VALUE self, VALUE count, VALUE ary, VALUE format) { - return rb_str_format(FIX2INT(count), RARRAY_PTR(ary), format); -} - +#ifdef HAVE_RB_MAKE_BACKTRACE static VALUE kernel_spec_rb_make_backtrace(VALUE self) { return rb_make_backtrace(); } - -static VALUE kernel_spec_rb_funcallv(VALUE self, VALUE obj, VALUE method, VALUE args) { - return rb_funcallv(obj, SYM2ID(method), RARRAY_LENINT(args), RARRAY_PTR(args)); -} - -#ifdef RUBY_VERSION_IS_3_0 -static VALUE kernel_spec_rb_funcallv_kw(VALUE self, VALUE obj, VALUE method, VALUE args) { - return rb_funcallv_kw(obj, SYM2ID(method), RARRAY_LENINT(args), RARRAY_PTR(args), RB_PASS_KEYWORDS); -} - -static VALUE kernel_spec_rb_keyword_given_p(int argc, VALUE *args, VALUE self) { - return rb_keyword_given_p() ? Qtrue : Qfalse; -} #endif -static VALUE kernel_spec_rb_funcallv_public(VALUE self, VALUE obj, VALUE method) { - return rb_funcallv_public(obj, SYM2ID(method), 0, NULL); -} - -static VALUE kernel_spec_rb_funcall_with_block(VALUE self, VALUE obj, VALUE method, VALUE args, VALUE block) { - return rb_funcall_with_block(obj, SYM2ID(method), RARRAY_LENINT(args), RARRAY_PTR(args), block); -} - -static VALUE kernel_spec_rb_funcall_with_block_kw(VALUE self, VALUE obj, VALUE method, VALUE args, VALUE block) { - return rb_funcall_with_block_kw(obj, SYM2ID(method), RARRAY_LENINT(args), RARRAY_PTR(args), block, RB_PASS_KEYWORDS); +#ifdef HAVE_RB_OBJ_METHOD +static VALUE kernel_spec_rb_obj_method(VALUE self, VALUE obj, VALUE method) { + return rb_obj_method(obj, method); } +#endif -static VALUE kernel_spec_rb_funcall_many_args(VALUE self, VALUE obj, VALUE method) { - return rb_funcall(obj, SYM2ID(method), 15, - INT2FIX(15), INT2FIX(14), INT2FIX(13), INT2FIX(12), INT2FIX(11), - INT2FIX(10), INT2FIX(9), INT2FIX(8), INT2FIX(7), INT2FIX(6), - INT2FIX(5), INT2FIX(4), INT2FIX(3), INT2FIX(2), INT2FIX(1)); +#ifdef HAVE_RB_FUNCALL3 +static VALUE kernel_spec_rb_funcall3(VALUE self, VALUE obj, VALUE method) { + return rb_funcall3(obj, SYM2ID(method), 0, NULL); } +#endif -static VALUE kernel_spec_rb_check_funcall(VALUE self, VALUE receiver, VALUE method, VALUE args) { - VALUE ret = rb_check_funcall(receiver, SYM2ID(method), RARRAY_LENINT(args), RARRAY_PTR(args)); - if (ret == Qundef) { - return ID2SYM(rb_intern("Qundef")); - } else { - return ret; - } +#ifdef HAVE_RB_FUNCALL_WITH_BLOCK +static VALUE kernel_spec_rb_funcall_with_block(VALUE self, VALUE obj, VALUE method, VALUE block) { + return rb_funcall_with_block(obj, SYM2ID(method), 0, NULL, block); } +#endif void Init_kernel_spec(void) { - VALUE cls = rb_define_class("CApiKernelSpecs", rb_cObject); + VALUE cls; + cls = rb_define_class("CApiKernelSpecs", rb_cObject); + +#ifdef HAVE_RB_BLOCK_GIVEN_P rb_define_method(cls, "rb_block_given_p", kernel_spec_rb_block_given_p, 0); +#endif + +#ifdef HAVE_RB_NEED_BLOCK rb_define_method(cls, "rb_need_block", kernel_spec_rb_need_block, 0); +#endif + +#ifdef HAVE_RB_BLOCK_CALL rb_define_method(cls, "rb_block_call", kernel_spec_rb_block_call, 1); rb_define_method(cls, "rb_block_call_multi_arg", kernel_spec_rb_block_call_multi_arg, 1); rb_define_method(cls, "rb_block_call_no_func", kernel_spec_rb_block_call_no_func, 1); - rb_define_method(cls, "rb_block_call_extra_data", rb_block_call_extra_data, 1); +#endif + +#ifdef HAVE_RB_BLOCK_PROC rb_define_method(cls, "rb_block_proc", kernel_spec_rb_block_proc, 0); - rb_define_method(cls, "rb_block_lambda", kernel_spec_rb_block_lambda, 0); - rb_define_method(cls, "rb_frame_this_func_test", kernel_spec_rb_frame_this_func, 0); - rb_define_method(cls, "rb_frame_this_func_test_again", kernel_spec_rb_frame_this_func, 0); - rb_define_method(cls, "rb_category_warn_deprecated", kernel_spec_rb_category_warn_deprecated, 0); - rb_define_method(cls, "rb_category_warn_deprecated_with_integer_extra_value", kernel_spec_rb_category_warn_deprecated_with_integer_extra_value, 1); +#endif + +#ifdef HAVE_RB_ENSURE rb_define_method(cls, "rb_ensure", kernel_spec_rb_ensure, 4); +#endif + +#ifdef HAVE_RB_EVAL_STRING rb_define_method(cls, "rb_eval_string", kernel_spec_rb_eval_string, 1); - rb_define_method(cls, "rb_eval_cmd_kw", kernel_spec_rb_eval_cmd_kw, 3); +#endif + +#ifdef HAVE_RB_RAISE rb_define_method(cls, "rb_raise", kernel_spec_rb_raise, 1); +#endif + +#ifdef HAVE_RB_THROW rb_define_method(cls, "rb_throw", kernel_spec_rb_throw, 1); +#endif + +#ifdef HAVE_RB_THROW_OBJ rb_define_method(cls, "rb_throw_obj", kernel_spec_rb_throw_obj, 2); - rb_define_method(cls, "rb_errinfo", kernel_spec_rb_errinfo, 0); - rb_define_method(cls, "rb_set_errinfo", kernel_spec_rb_set_errinfo, 1); - rb_define_method(cls, "rb_rescue", kernel_spec_rb_rescue, 4); +#endif + +#ifdef HAVE_RB_RESCUE rb_define_method(cls, "rb_rescue", kernel_spec_rb_rescue, 4); +#endif + +#ifdef HAVE_RB_RESCUE2 rb_define_method(cls, "rb_rescue2", kernel_spec_rb_rescue2, -1); +#endif + +#ifdef HAVE_RB_PROTECT rb_define_method(cls, "rb_protect_yield", kernel_spec_rb_protect_yield, 2); - rb_define_method(cls, "rb_protect_ignore_status", kernel_spec_rb_protect_ignore_status, 2); - rb_define_method(cls, "rb_protect_null_status", kernel_spec_rb_protect_null_status, 1); - rb_define_method(cls, "rb_eval_string_protect", kernel_spec_rb_eval_string_protect, 2); +#endif + +#ifdef HAVE_RB_CATCH rb_define_method(cls, "rb_catch", kernel_spec_rb_catch, 2); +#endif + +#ifdef HAVE_RB_CATCH_OBJ rb_define_method(cls, "rb_catch_obj", kernel_spec_rb_catch_obj, 2); +#endif + +#ifdef HAVE_RB_SYS_FAIL rb_define_method(cls, "rb_sys_fail", kernel_spec_rb_sys_fail, 1); +#endif + +#ifdef HAVE_RB_SYSERR_FAIL rb_define_method(cls, "rb_syserr_fail", kernel_spec_rb_syserr_fail, 2); - rb_define_method(cls, "rb_syserr_fail_str", kernel_spec_rb_syserr_fail_str, 2); +#endif + +#ifdef HAVE_RB_WARN rb_define_method(cls, "rb_warn", kernel_spec_rb_warn, 1); +#endif + +#ifdef HAVE_RB_YIELD rb_define_method(cls, "rb_yield", kernel_spec_rb_yield, 1); rb_define_method(cls, "rb_yield_indirected", kernel_spec_rb_yield_indirected, 1); rb_define_method(cls, "rb_yield_define_each", kernel_spec_rb_yield_define_each, 1); +#endif + +#ifdef HAVE_RB_YIELD_VALUES rb_define_method(cls, "rb_yield_values", kernel_spec_rb_yield_values, 2); - rb_define_method(cls, "rb_yield_values2", kernel_spec_rb_yield_values2, 1); +#endif + +#ifdef HAVE_RB_YIELD_SPLAT rb_define_method(cls, "rb_yield_splat", kernel_spec_rb_yield_splat, 1); +#endif + +#ifdef HAVE_RB_EXEC_RECURSIVE rb_define_method(cls, "rb_exec_recursive", kernel_spec_rb_exec_recursive, 1); +#endif + +#ifdef HAVE_RB_SET_END_PROC rb_define_method(cls, "rb_set_end_proc", kernel_spec_rb_set_end_proc, 1); +#endif + +#ifdef HAVE_RB_F_SPRINTF rb_define_method(cls, "rb_f_sprintf", kernel_spec_rb_f_sprintf, 1); - rb_define_method(cls, "rb_str_format", kernel_spec_rb_str_format, 3); +#endif + +#ifdef HAVE_RB_MAKE_BACKTRACE rb_define_method(cls, "rb_make_backtrace", kernel_spec_rb_make_backtrace, 0); - rb_define_method(cls, "rb_funcallv", kernel_spec_rb_funcallv, 3); -#ifdef RUBY_VERSION_IS_3_0 - rb_define_method(cls, "rb_funcallv_kw", kernel_spec_rb_funcallv_kw, 3); - rb_define_method(cls, "rb_keyword_given_p", kernel_spec_rb_keyword_given_p, -1); -#endif - rb_define_method(cls, "rb_funcallv_public", kernel_spec_rb_funcallv_public, 2); - rb_define_method(cls, "rb_funcall_many_args", kernel_spec_rb_funcall_many_args, 2); - rb_define_method(cls, "rb_funcall_with_block", kernel_spec_rb_funcall_with_block, 4); - rb_define_method(cls, "rb_funcall_with_block_kw", kernel_spec_rb_funcall_with_block_kw, 4); - rb_define_method(cls, "rb_check_funcall", kernel_spec_rb_check_funcall, 3); +#endif + +#ifdef HAVE_RB_OBJ_METHOD + rb_define_method(cls, "rb_obj_method", kernel_spec_rb_obj_method, 2); +#endif + +#ifdef HAVE_RB_FUNCALL3 + rb_define_method(cls, "rb_funcall3", kernel_spec_rb_funcall3, 2); +#endif + +#ifdef HAVE_RB_FUNCALL_WITH_BLOCK + rb_define_method(cls, "rb_funcall_with_block", kernel_spec_rb_funcall_with_block, 3); +#endif } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/language_spec.c b/spec/ruby/optional/capi/ext/language_spec.c deleted file mode 100644 index 749c188956..0000000000 --- a/spec/ruby/optional/capi/ext/language_spec.c +++ /dev/null @@ -1,42 +0,0 @@ -#include "ruby.h" -#include "rubyspec.h" - -#ifdef __cplusplus -extern "C" { -#endif - -static VALUE language_spec_switch(VALUE self, VALUE value) { - if (value == ID2SYM(rb_intern("undef"))) { - value = Qundef; - } - - switch (value) { - case Qtrue: - return ID2SYM(rb_intern("true")); - case Qfalse: - return ID2SYM(rb_intern("false")); - case Qnil: - return ID2SYM(rb_intern("nil")); - case Qundef: - return ID2SYM(rb_intern("undef")); - default: - return ID2SYM(rb_intern("default")); - } -} - -/* Defining a local variable rb_mProcess which already exists as a global variable - * For instance eventmachine does this in Init_rubyeventmachine() */ -static VALUE language_spec_global_local_var(VALUE self) { - VALUE rb_mProcess = rb_const_get(rb_cObject, rb_intern("Process")); - return rb_mProcess; -} - -void Init_language_spec(void) { - VALUE cls = rb_define_class("CApiLanguageSpecs", rb_cObject); - rb_define_method(cls, "switch", language_spec_switch, 1); - rb_define_method(cls, "global_local_var", language_spec_global_local_var, 0); -} - -#ifdef __cplusplus -} -#endif diff --git a/spec/ruby/optional/capi/ext/marshal_spec.c b/spec/ruby/optional/capi/ext/marshal_spec.c index ea8e3d5a07..dbb6e71abf 100644 --- a/spec/ruby/optional/capi/ext/marshal_spec.c +++ b/spec/ruby/optional/capi/ext/marshal_spec.c @@ -5,18 +5,30 @@ extern "C" { #endif +#ifdef HAVE_RB_MARSHAL_DUMP VALUE marshal_spec_rb_marshal_dump(VALUE self, VALUE obj, VALUE port) { return rb_marshal_dump(obj, port); } +#endif +#ifdef HAVE_RB_MARSHAL_LOAD VALUE marshal_spec_rb_marshal_load(VALUE self, VALUE data) { return rb_marshal_load(data); } +#endif void Init_marshal_spec(void) { - VALUE cls = rb_define_class("CApiMarshalSpecs", rb_cObject); + VALUE cls; + cls = rb_define_class("CApiMarshalSpecs", rb_cObject); + +#ifdef HAVE_RB_MARSHAL_DUMP rb_define_method(cls, "rb_marshal_dump", marshal_spec_rb_marshal_dump, 2); +#endif + +#ifdef HAVE_RB_MARSHAL_LOAD rb_define_method(cls, "rb_marshal_load", marshal_spec_rb_marshal_load, 1); +#endif + } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/module_spec.c b/spec/ruby/optional/capi/ext/module_spec.c index 12bcf99983..e408404a09 100644 --- a/spec/ruby/optional/capi/ext/module_spec.c +++ b/spec/ruby/optional/capi/ext/module_spec.c @@ -9,166 +9,252 @@ static VALUE module_specs_test_method(VALUE self) { return ID2SYM(rb_intern("test_method")); } +#ifdef HAVE_RB_CONST_DEFINED static VALUE module_specs_const_defined(VALUE self, VALUE klass, VALUE id) { return rb_const_defined(klass, SYM2ID(id)) ? Qtrue : Qfalse; } +#endif +#ifdef HAVE_RB_CONST_DEFINED_AT static VALUE module_specs_const_defined_at(VALUE self, VALUE klass, VALUE id) { return rb_const_defined_at(klass, SYM2ID(id)) ? Qtrue : Qfalse; } +#endif +#ifdef HAVE_RB_CONST_GET static VALUE module_specs_const_get(VALUE self, VALUE klass, VALUE val) { return rb_const_get(klass, SYM2ID(val)); } +#endif +#ifdef HAVE_RB_CONST_GET_AT static VALUE module_specs_const_get_at(VALUE self, VALUE klass, VALUE val) { return rb_const_get_at(klass, SYM2ID(val)); } +#endif +#ifdef HAVE_RB_CONST_GET_FROM static VALUE module_specs_const_get_from(VALUE self, VALUE klass, VALUE val) { return rb_const_get_from(klass, SYM2ID(val)); } +#endif +#ifdef HAVE_RB_CONST_SET static VALUE module_specs_const_set(VALUE self, VALUE klass, VALUE name, VALUE val) { rb_const_set(klass, SYM2ID(name), val); return Qnil; } +#endif +#ifdef HAVE_RB_DEFINE_ALIAS static VALUE module_specs_rb_define_alias(VALUE self, VALUE obj, VALUE new_name, VALUE old_name) { rb_define_alias(obj, RSTRING_PTR(new_name), RSTRING_PTR(old_name)); return Qnil; } +#endif +#ifdef HAVE_RB_ALIAS static VALUE module_specs_rb_alias(VALUE self, VALUE obj, VALUE new_name, VALUE old_name) { rb_alias(obj, SYM2ID(new_name), SYM2ID(old_name)); return Qnil; } +#endif +#ifdef HAVE_RB_DEFINE_MODULE static VALUE module_specs_rb_define_module(VALUE self, VALUE name) { return rb_define_module(RSTRING_PTR(name)); } +#endif +#ifdef HAVE_RB_DEFINE_MODULE_UNDER static VALUE module_specs_rb_define_module_under(VALUE self, VALUE outer, VALUE name) { return rb_define_module_under(outer, RSTRING_PTR(name)); } +#endif +#ifdef HAVE_RB_DEFINE_CONST static VALUE module_specs_define_const(VALUE self, VALUE klass, VALUE str_name, VALUE val) { rb_define_const(klass, RSTRING_PTR(str_name), val); return Qnil; } +#endif +#ifdef HAVE_RB_DEFINE_GLOBAL_CONST static VALUE module_specs_define_global_const(VALUE self, VALUE str_name, VALUE obj) { rb_define_global_const(RSTRING_PTR(str_name), obj); return Qnil; } +#endif +#ifdef HAVE_RB_DEFINE_GLOBAL_FUNCTION static VALUE module_specs_rb_define_global_function(VALUE self, VALUE str_name) { rb_define_global_function(RSTRING_PTR(str_name), module_specs_test_method, 0); return Qnil; } +#endif +#ifdef HAVE_RB_DEFINE_METHOD static VALUE module_specs_rb_define_method(VALUE self, VALUE cls, VALUE str_name) { rb_define_method(cls, RSTRING_PTR(str_name), module_specs_test_method, 0); return Qnil; } +#endif -static VALUE module_specs_method_var_args_1(int argc, VALUE *argv, VALUE self) { - VALUE ary = rb_ary_new(); - int i; - for (i = 0; i < argc; i++) { - rb_ary_push(ary, argv[i]); - } - return ary; -} - -static VALUE module_specs_method_var_args_2(VALUE self, VALUE argv) { - return argv; -} - -static VALUE module_specs_rb_define_method_1required(VALUE self, VALUE arg1) { - return arg1; -} - -static VALUE module_specs_rb_define_method_2required(VALUE self, VALUE arg1, VALUE arg2) { - return arg2; -} - +#ifdef HAVE_RB_DEFINE_MODULE_FUNCTION static VALUE module_specs_rb_define_module_function(VALUE self, VALUE cls, VALUE str_name) { rb_define_module_function(cls, RSTRING_PTR(str_name), module_specs_test_method, 0); return Qnil; } +#endif +#ifdef HAVE_RB_DEFINE_PRIVATE_METHOD static VALUE module_specs_rb_define_private_method(VALUE self, VALUE cls, VALUE str_name) { rb_define_private_method(cls, RSTRING_PTR(str_name), module_specs_test_method, 0); return Qnil; } +#endif +#ifdef HAVE_RB_DEFINE_PROTECTED_METHOD static VALUE module_specs_rb_define_protected_method(VALUE self, VALUE cls, VALUE str_name) { rb_define_protected_method(cls, RSTRING_PTR(str_name), module_specs_test_method, 0); return Qnil; } +#endif +#ifdef HAVE_RB_DEFINE_SINGLETON_METHOD static VALUE module_specs_rb_define_singleton_method(VALUE self, VALUE cls, VALUE str_name) { rb_define_singleton_method(cls, RSTRING_PTR(str_name), module_specs_test_method, 0); return Qnil; } +#endif +#ifdef HAVE_RB_UNDEF_METHOD static VALUE module_specs_rb_undef_method(VALUE self, VALUE cls, VALUE str_name) { rb_undef_method(cls, RSTRING_PTR(str_name)); return Qnil; } +#endif +#ifdef HAVE_RB_UNDEF static VALUE module_specs_rb_undef(VALUE self, VALUE cls, VALUE symbol_name) { rb_undef(cls, SYM2ID(symbol_name)); return Qnil; } +#endif +#ifdef HAVE_RB_CLASS2NAME static VALUE module_specs_rbclass2name(VALUE self, VALUE klass) { return rb_str_new2(rb_class2name(klass)); } +#endif +#ifdef HAVE_RB_MOD_ANCESTORS static VALUE module_specs_rb_mod_ancestors(VALUE self, VALUE klass) { return rb_mod_ancestors(klass); } +#endif void Init_module_spec(void) { - VALUE cls = rb_define_class("CApiModuleSpecs", rb_cObject); + VALUE cls; + + cls = rb_define_class("CApiModuleSpecs", rb_cObject); + +#ifdef HAVE_RB_CONST_DEFINED rb_define_method(cls, "rb_const_defined", module_specs_const_defined, 2); +#endif + +#ifdef HAVE_RB_CONST_DEFINED_AT rb_define_method(cls, "rb_const_defined_at", module_specs_const_defined_at, 2); +#endif + +#ifdef HAVE_RB_CONST_GET rb_define_method(cls, "rb_const_get", module_specs_const_get, 2); +#endif + +#ifdef HAVE_RB_CONST_GET_AT rb_define_method(cls, "rb_const_get_at", module_specs_const_get_at, 2); +#endif + +#ifdef HAVE_RB_CONST_GET_FROM rb_define_method(cls, "rb_const_get_from", module_specs_const_get_from, 2); +#endif + +#ifdef HAVE_RB_CONST_SET rb_define_method(cls, "rb_const_set", module_specs_const_set, 3); +#endif + +#ifdef HAVE_RB_DEFINE_ALIAS rb_define_method(cls, "rb_define_alias", module_specs_rb_define_alias, 3); +#endif + +#ifdef HAVE_RB_ALIAS rb_define_method(cls, "rb_alias", module_specs_rb_alias, 3); +#endif + +#ifdef HAVE_RB_DEFINE_MODULE rb_define_method(cls, "rb_define_module", module_specs_rb_define_module, 1); +#endif + +#ifdef HAVE_RB_DEFINE_MODULE_UNDER rb_define_method(cls, "rb_define_module_under", module_specs_rb_define_module_under, 2); +#endif + +#ifdef HAVE_RB_DEFINE_CONST rb_define_method(cls, "rb_define_const", module_specs_define_const, 3); +#endif + +#ifdef HAVE_RB_DEFINE_GLOBAL_CONST rb_define_method(cls, "rb_define_global_const", module_specs_define_global_const, 2); - rb_define_method(cls, "rb_define_global_function", module_specs_rb_define_global_function, 1); +#endif + +#ifdef HAVE_RB_DEFINE_GLOBAL_FUNCTION + rb_define_method(cls, "rb_define_global_function", + module_specs_rb_define_global_function, 1); +#endif +#ifdef HAVE_RB_DEFINE_METHOD rb_define_method(cls, "rb_define_method", module_specs_rb_define_method, 2); - rb_define_method(cls, "rb_define_method_varargs_1", module_specs_method_var_args_1, -1); - rb_define_method(cls, "rb_define_method_varargs_2", module_specs_method_var_args_2, -2); - rb_define_method(cls, "rb_define_method_1required", module_specs_rb_define_method_1required, 1); - rb_define_method(cls, "rb_define_method_2required", module_specs_rb_define_method_2required, 2); +#endif - rb_define_method(cls, "rb_define_module_function", module_specs_rb_define_module_function, 2); +#ifdef HAVE_RB_DEFINE_MODULE_FUNCTION + rb_define_method(cls, "rb_define_module_function", + module_specs_rb_define_module_function, 2); +#endif - rb_define_method(cls, "rb_define_private_method", module_specs_rb_define_private_method, 2); +#ifdef HAVE_RB_DEFINE_PRIVATE_METHOD + rb_define_method(cls, "rb_define_private_method", + module_specs_rb_define_private_method, 2); +#endif - rb_define_method(cls, "rb_define_protected_method", module_specs_rb_define_protected_method, 2); +#ifdef HAVE_RB_DEFINE_PROTECTED_METHOD + rb_define_method(cls, "rb_define_protected_method", + module_specs_rb_define_protected_method, 2); +#endif - rb_define_method(cls, "rb_define_singleton_method", module_specs_rb_define_singleton_method, 2); +#ifdef HAVE_RB_DEFINE_SINGLETON_METHOD + rb_define_method(cls, "rb_define_singleton_method", + module_specs_rb_define_singleton_method, 2); +#endif +#ifdef HAVE_RB_UNDEF_METHOD rb_define_method(cls, "rb_undef_method", module_specs_rb_undef_method, 2); +#endif + +#ifdef HAVE_RB_UNDEF rb_define_method(cls, "rb_undef", module_specs_rb_undef, 2); +#endif + +#ifdef HAVE_RB_CLASS2NAME rb_define_method(cls, "rb_class2name", module_specs_rbclass2name, 1); +#endif + +#ifdef HAVE_RB_MOD_ANCESTORS rb_define_method(cls, "rb_mod_ancestors", module_specs_rb_mod_ancestors, 1); +#endif } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/module_under_autoload_spec.c b/spec/ruby/optional/capi/ext/module_under_autoload_spec.c index b19466e555..c8f19a287b 100644 --- a/spec/ruby/optional/capi/ext/module_under_autoload_spec.c +++ b/spec/ruby/optional/capi/ext/module_under_autoload_spec.c @@ -1,15 +1,7 @@ #include "ruby.h" -#ifdef __cplusplus -extern "C" { -#endif - void Init_module_under_autoload_spec(void) { VALUE specs = rb_const_get(rb_cObject, rb_intern("CApiModuleSpecs")); rb_define_module_under(specs, "ModuleUnderAutoload"); rb_define_module_under(specs, "RubyUnderAutoload"); } - -#ifdef __cplusplus -} -#endif diff --git a/spec/ruby/optional/capi/ext/mutex_spec.c b/spec/ruby/optional/capi/ext/mutex_spec.c index d2c8f98e89..d5ce06e124 100644 --- a/spec/ruby/optional/capi/ext/mutex_spec.c +++ b/spec/ruby/optional/capi/ext/mutex_spec.c @@ -5,69 +5,84 @@ extern "C" { #endif +#ifdef HAVE_RB_MUTEX_NEW VALUE mutex_spec_rb_mutex_new(VALUE self) { return rb_mutex_new(); } +#endif +#ifdef HAVE_RB_MUTEX_LOCKED_P VALUE mutex_spec_rb_mutex_locked_p(VALUE self, VALUE mutex) { return rb_mutex_locked_p(mutex); } +#endif +#ifdef HAVE_RB_MUTEX_TRYLOCK VALUE mutex_spec_rb_mutex_trylock(VALUE self, VALUE mutex) { return rb_mutex_trylock(mutex); } +#endif +#ifdef HAVE_RB_MUTEX_LOCK VALUE mutex_spec_rb_mutex_lock(VALUE self, VALUE mutex) { return rb_mutex_lock(mutex); } +#endif +#ifdef HAVE_RB_MUTEX_UNLOCK VALUE mutex_spec_rb_mutex_unlock(VALUE self, VALUE mutex) { return rb_mutex_unlock(mutex); } +#endif +#ifdef HAVE_RB_MUTEX_SLEEP VALUE mutex_spec_rb_mutex_sleep(VALUE self, VALUE mutex, VALUE timeout) { return rb_mutex_sleep(mutex, timeout); } +#endif + +#ifdef HAVE_RB_MUTEX_SYNCHRONIZE VALUE mutex_spec_rb_mutex_callback(VALUE arg) { return rb_funcall(arg, rb_intern("call"), 0); } -VALUE mutex_spec_rb_mutex_naughty_callback(VALUE arg) { - int *result = (int *) arg; - return (VALUE) result; -} - -VALUE mutex_spec_rb_mutex_callback_basic(VALUE arg) { - return arg; -} - VALUE mutex_spec_rb_mutex_synchronize(VALUE self, VALUE mutex, VALUE value) { return rb_mutex_synchronize(mutex, mutex_spec_rb_mutex_callback, value); } - -VALUE mutex_spec_rb_mutex_synchronize_with_naughty_callback(VALUE self, VALUE mutex) { - // a naughty callback accepts or returns not a Ruby object but arbitrary value - int arg = 42; - VALUE result = rb_mutex_synchronize(mutex, mutex_spec_rb_mutex_naughty_callback, (VALUE) &arg); - return INT2NUM(*((int *) result)); -} - -VALUE mutex_spec_rb_mutex_synchronize_with_native_callback(VALUE self, VALUE mutex, VALUE value) { - return rb_mutex_synchronize(mutex, mutex_spec_rb_mutex_callback_basic, value); -} +#endif void Init_mutex_spec(void) { - VALUE cls = rb_define_class("CApiMutexSpecs", rb_cObject); + VALUE cls; + cls = rb_define_class("CApiMutexSpecs", rb_cObject); + +#ifdef HAVE_RB_MUTEX_NEW rb_define_method(cls, "rb_mutex_new", mutex_spec_rb_mutex_new, 0); +#endif + +#ifdef HAVE_RB_MUTEX_LOCKED_P rb_define_method(cls, "rb_mutex_locked_p", mutex_spec_rb_mutex_locked_p, 1); +#endif + +#ifdef HAVE_RB_MUTEX_TRYLOCK rb_define_method(cls, "rb_mutex_trylock", mutex_spec_rb_mutex_trylock, 1); +#endif + +#ifdef HAVE_RB_MUTEX_LOCK rb_define_method(cls, "rb_mutex_lock", mutex_spec_rb_mutex_lock, 1); +#endif + +#ifdef HAVE_RB_MUTEX_UNLOCK rb_define_method(cls, "rb_mutex_unlock", mutex_spec_rb_mutex_unlock, 1); +#endif + +#ifdef HAVE_RB_MUTEX_SLEEP rb_define_method(cls, "rb_mutex_sleep", mutex_spec_rb_mutex_sleep, 2); +#endif + +#ifdef HAVE_RB_MUTEX_SYNCHRONIZE rb_define_method(cls, "rb_mutex_synchronize", mutex_spec_rb_mutex_synchronize, 2); - rb_define_method(cls, "rb_mutex_synchronize_with_naughty_callback", mutex_spec_rb_mutex_synchronize_with_naughty_callback, 1); - rb_define_method(cls, "rb_mutex_synchronize_with_native_callback", mutex_spec_rb_mutex_synchronize_with_native_callback, 2); +#endif } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/numeric_spec.c b/spec/ruby/optional/capi/ext/numeric_spec.c index d3639580a8..2f0f504549 100644 --- a/spec/ruby/optional/capi/ext/numeric_spec.c +++ b/spec/ruby/optional/capi/ext/numeric_spec.c @@ -5,124 +5,170 @@ extern "C" { #endif -static VALUE numeric_spec_size_of_VALUE(VALUE self) { - return INT2FIX(sizeof(VALUE)); -} - -static VALUE numeric_spec_size_of_long_long(VALUE self) { - return INT2FIX(sizeof(LONG_LONG)); -} - +#ifdef HAVE_NUM2CHR static VALUE numeric_spec_NUM2CHR(VALUE self, VALUE value) { return INT2FIX(NUM2CHR(value)); } +#endif +#ifdef HAVE_RB_INT2INUM static VALUE numeric_spec_rb_int2inum_14(VALUE self) { return rb_int2inum(14); } +#endif -static VALUE numeric_spec_rb_uint2inum_14(VALUE self) { - return rb_uint2inum(14); -} - -static VALUE numeric_spec_rb_uint2inum_n14(VALUE self) { - return rb_uint2inum(-14); -} - +#ifdef HAVE_RB_INTEGER static VALUE numeric_spec_rb_Integer(VALUE self, VALUE str) { return rb_Integer(str); } +#endif +#ifdef HAVE_RB_LL2INUM static VALUE numeric_spec_rb_ll2inum_14(VALUE self) { return rb_ll2inum(14); } +#endif -static VALUE numeric_spec_rb_ull2inum_14(VALUE self) { - return rb_ull2inum(14); -} - -static VALUE numeric_spec_rb_ull2inum_n14(VALUE self) { - return rb_ull2inum(-14); -} - -static VALUE numeric_spec_NUM2DBL(VALUE self, VALUE num) { - return rb_float_new(NUM2DBL(num)); -} - -static VALUE numeric_spec_NUM2INT(VALUE self, VALUE num) { - return LONG2NUM(NUM2INT(num)); +#ifdef HAVE_RB_NUM2DBL +static VALUE numeric_spec_rb_num2dbl(VALUE self, VALUE num) { + return rb_float_new(rb_num2dbl(num)); } +#endif -static VALUE numeric_spec_INT2NUM(VALUE self, VALUE num) { - return INT2NUM(NUM2INT(num)); +#ifdef HAVE_RB_NUM2INT +static VALUE numeric_spec_rb_num2int(VALUE self, VALUE num) { + return LONG2NUM(rb_num2int(num)); } +#endif -static VALUE numeric_spec_NUM2LONG(VALUE self, VALUE num) { - return LONG2NUM(NUM2LONG(num)); +#ifdef HAVE_RB_INT2NUM +static VALUE numeric_spec_rb_int2num(VALUE self, VALUE num) { + return INT2NUM(rb_num2long(num)); } +#endif -static VALUE numeric_spec_NUM2SHORT(VALUE self, VALUE num) { - return LONG2NUM(NUM2SHORT(num)); +#ifdef HAVE_RB_NUM2LONG +static VALUE numeric_spec_rb_num2long(VALUE self, VALUE num) { + return LONG2NUM(rb_num2long(num)); } +#endif -static VALUE numeric_spec_NUM2UINT(VALUE self, VALUE num) { - return ULONG2NUM(NUM2UINT(num)); +#ifdef HAVE_RB_NUM2UINT +static VALUE numeric_spec_rb_num2uint(VALUE self, VALUE num) { + return ULONG2NUM(rb_num2uint(num)); } +#endif -static VALUE numeric_spec_NUM2ULONG(VALUE self, VALUE num) { - return ULONG2NUM(NUM2ULONG(num)); +#ifdef HAVE_RB_NUM2ULONG +static VALUE numeric_spec_rb_num2ulong(VALUE self, VALUE num) { + return ULONG2NUM(rb_num2ulong(num)); } +#endif +#ifdef HAVE_RB_NUM_ZERODIV static VALUE numeric_spec_rb_num_zerodiv(VALUE self) { rb_num_zerodiv(); return Qnil; } +#endif +#ifdef HAVE_RB_CMPINT static VALUE numeric_spec_rb_cmpint(VALUE self, VALUE val, VALUE b) { return INT2FIX(rb_cmpint(val, val, b)); } +#endif +#ifdef HAVE_RB_NUM_COERCE_BIN static VALUE numeric_spec_rb_num_coerce_bin(VALUE self, VALUE x, VALUE y, VALUE op) { return rb_num_coerce_bin(x, y, SYM2ID(op)); } +#endif +#ifdef HAVE_RB_NUM_COERCE_CMP static VALUE numeric_spec_rb_num_coerce_cmp(VALUE self, VALUE x, VALUE y, VALUE op) { return rb_num_coerce_cmp(x, y, SYM2ID(op)); } +#endif +#ifdef HAVE_RB_NUM_COERCE_RELOP static VALUE numeric_spec_rb_num_coerce_relop(VALUE self, VALUE x, VALUE y, VALUE op) { return rb_num_coerce_relop(x, y, SYM2ID(op)); } +#endif +#ifdef HAVE_RB_ABSINT_SINGLEBIT_P static VALUE numeric_spec_rb_absint_singlebit_p(VALUE self, VALUE num) { return INT2FIX(rb_absint_singlebit_p(num)); } +#endif void Init_numeric_spec(void) { - VALUE cls = rb_define_class("CApiNumericSpecs", rb_cObject); - rb_define_method(cls, "size_of_VALUE", numeric_spec_size_of_VALUE, 0); - rb_define_method(cls, "size_of_long_long", numeric_spec_size_of_long_long, 0); + VALUE cls; + cls = rb_define_class("CApiNumericSpecs", rb_cObject); + +#ifdef HAVE_NUM2CHR rb_define_method(cls, "NUM2CHR", numeric_spec_NUM2CHR, 1); +#endif + +#ifdef HAVE_RB_INT2INUM rb_define_method(cls, "rb_int2inum_14", numeric_spec_rb_int2inum_14, 0); - rb_define_method(cls, "rb_uint2inum_14", numeric_spec_rb_uint2inum_14, 0); - rb_define_method(cls, "rb_uint2inum_n14", numeric_spec_rb_uint2inum_n14, 0); +#endif + +#ifdef HAVE_RB_INTEGER rb_define_method(cls, "rb_Integer", numeric_spec_rb_Integer, 1); +#endif + +#ifdef HAVE_RB_LL2INUM rb_define_method(cls, "rb_ll2inum_14", numeric_spec_rb_ll2inum_14, 0); - rb_define_method(cls, "rb_ull2inum_14", numeric_spec_rb_ull2inum_14, 0); - rb_define_method(cls, "rb_ull2inum_n14", numeric_spec_rb_ull2inum_n14, 0); - rb_define_method(cls, "NUM2DBL", numeric_spec_NUM2DBL, 1); - rb_define_method(cls, "NUM2INT", numeric_spec_NUM2INT, 1); - rb_define_method(cls, "NUM2LONG", numeric_spec_NUM2LONG, 1); - rb_define_method(cls, "NUM2SHORT", numeric_spec_NUM2SHORT, 1); - rb_define_method(cls, "INT2NUM", numeric_spec_INT2NUM, 1); - rb_define_method(cls, "NUM2UINT", numeric_spec_NUM2UINT, 1); - rb_define_method(cls, "NUM2ULONG", numeric_spec_NUM2ULONG, 1); +#endif + +#ifdef HAVE_RB_NUM2DBL + rb_define_method(cls, "rb_num2dbl", numeric_spec_rb_num2dbl, 1); +#endif + +#ifdef HAVE_RB_NUM2INT + rb_define_method(cls, "rb_num2int", numeric_spec_rb_num2int, 1); +#endif + +#ifdef HAVE_RB_NUM2LONG + rb_define_method(cls, "rb_num2long", numeric_spec_rb_num2long, 1); +#endif + +#ifdef HAVE_RB_INT2NUM + rb_define_method(cls, "rb_int2num", numeric_spec_rb_int2num, 1); +#endif + +#ifdef HAVE_RB_NUM2UINT + rb_define_method(cls, "rb_num2uint", numeric_spec_rb_num2uint, 1); +#endif + +#ifdef HAVE_RB_NUM2ULONG + rb_define_method(cls, "rb_num2ulong", numeric_spec_rb_num2ulong, 1); +#endif + +#ifdef HAVE_RB_NUM_ZERODIV rb_define_method(cls, "rb_num_zerodiv", numeric_spec_rb_num_zerodiv, 0); +#endif + +#ifdef HAVE_RB_CMPINT rb_define_method(cls, "rb_cmpint", numeric_spec_rb_cmpint, 2); +#endif + +#ifdef HAVE_RB_NUM_COERCE_BIN rb_define_method(cls, "rb_num_coerce_bin", numeric_spec_rb_num_coerce_bin, 3); +#endif + +#ifdef HAVE_RB_NUM_COERCE_CMP rb_define_method(cls, "rb_num_coerce_cmp", numeric_spec_rb_num_coerce_cmp, 3); +#endif + +#ifdef HAVE_RB_NUM_COERCE_RELOP rb_define_method(cls, "rb_num_coerce_relop", numeric_spec_rb_num_coerce_relop, 3); +#endif + +#ifdef HAVE_RB_ABSINT_SINGLEBIT_P rb_define_method(cls, "rb_absint_singlebit_p", numeric_spec_rb_absint_singlebit_p, 1); +#endif } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/object_spec.c b/spec/ruby/optional/capi/ext/object_spec.c index aa60662e1e..45a28169ef 100644 --- a/spec/ruby/optional/capi/ext/object_spec.c +++ b/spec/ruby/optional/capi/ext/object_spec.c @@ -5,6 +5,7 @@ extern "C" { #endif +#ifdef HAVE_FL_ABLE static VALUE object_spec_FL_ABLE(VALUE self, VALUE obj) { if (FL_ABLE(obj)) { return Qtrue; @@ -12,15 +13,14 @@ static VALUE object_spec_FL_ABLE(VALUE self, VALUE obj) { return Qfalse; } } +#endif +#ifdef HAVE_FL_TEST static int object_spec_FL_TEST_flag(VALUE flag_string) { char *flag_cstr = StringValueCStr(flag_string); -#ifndef RUBY_VERSION_IS_3_1 if (strcmp(flag_cstr, "FL_TAINT") == 0) { return FL_TAINT; - } -#endif - if (strcmp(flag_cstr, "FL_FREEZE") == 0) { + } else if (strcmp(flag_cstr, "FL_FREEZE") == 0) { return FL_FREEZE; } return 0; @@ -29,85 +29,119 @@ static int object_spec_FL_TEST_flag(VALUE flag_string) { static VALUE object_spec_FL_TEST(VALUE self, VALUE obj, VALUE flag) { return INT2FIX(FL_TEST(obj, object_spec_FL_TEST_flag(flag))); } +#endif -#ifndef RUBY_VERSION_IS_3_1 +#ifdef HAVE_OBJ_TAINT static VALUE object_spec_OBJ_TAINT(VALUE self, VALUE obj) { OBJ_TAINT(obj); return Qnil; } +#endif +#ifdef HAVE_OBJ_TAINTED static VALUE object_spec_OBJ_TAINTED(VALUE self, VALUE obj) { return OBJ_TAINTED(obj) ? Qtrue : Qfalse; } +#endif +#ifdef HAVE_OBJ_INFECT static VALUE object_spec_OBJ_INFECT(VALUE self, VALUE host, VALUE source) { OBJ_INFECT(host, source); return Qnil; } #endif +#ifdef HAVE_RB_ANY_TO_S static VALUE object_spec_rb_any_to_s(VALUE self, VALUE obj) { return rb_any_to_s(obj); } +#endif +#ifdef HAVE_RB_ATTR_GET static VALUE so_attr_get(VALUE self, VALUE obj, VALUE attr) { return rb_attr_get(obj, SYM2ID(attr)); } +#endif +#ifdef HAVE_RB_OBJ_INSTANCE_VARIABLES static VALUE object_spec_rb_obj_instance_variables(VALUE self, VALUE obj) { return rb_obj_instance_variables(obj); } +#endif +#ifdef HAVE_RB_CHECK_ARRAY_TYPE static VALUE so_check_array_type(VALUE self, VALUE ary) { return rb_check_array_type(ary); } +#endif +#ifdef HAVE_RB_CHECK_CONVERT_TYPE static VALUE so_check_convert_type(VALUE self, VALUE obj, VALUE klass, VALUE method) { return rb_check_convert_type(obj, T_ARRAY, RSTRING_PTR(klass), RSTRING_PTR(method)); } +#endif +#ifdef HAVE_RB_CHECK_TO_INTEGER static VALUE so_check_to_integer(VALUE self, VALUE obj, VALUE method) { return rb_check_to_integer(obj, RSTRING_PTR(method)); } +#endif +#ifdef HAVE_RB_CHECK_FROZEN static VALUE object_spec_rb_check_frozen(VALUE self, VALUE obj) { rb_check_frozen(obj); return Qnil; } +#endif +#ifdef HAVE_RB_CHECK_STRING_TYPE static VALUE so_check_string_type(VALUE self, VALUE str) { return rb_check_string_type(str); } +#endif +#ifdef HAVE_RB_CLASS_OF static VALUE so_rbclassof(VALUE self, VALUE obj) { return rb_class_of(obj); } +#endif +#ifdef HAVE_RB_CONVERT_TYPE static VALUE so_convert_type(VALUE self, VALUE obj, VALUE klass, VALUE method) { return rb_convert_type(obj, T_ARRAY, RSTRING_PTR(klass), RSTRING_PTR(method)); } +#endif +#ifdef HAVE_RB_EXTEND_OBJECT static VALUE object_spec_rb_extend_object(VALUE self, VALUE obj, VALUE mod) { rb_extend_object(obj, mod); return obj; } +#endif +#ifdef HAVE_RB_INSPECT static VALUE so_inspect(VALUE self, VALUE obj) { return rb_inspect(obj); } +#endif +#ifdef HAVE_RB_OBJ_ALLOC static VALUE so_rb_obj_alloc(VALUE self, VALUE klass) { return rb_obj_alloc(klass); } +#endif +#ifdef HAVE_RB_OBJ_DUP static VALUE so_rb_obj_dup(VALUE self, VALUE klass) { return rb_obj_dup(klass); } +#endif +#ifdef HAVE_RB_OBJ_CALL_INIT static VALUE so_rb_obj_call_init(VALUE self, VALUE object, VALUE nargs, VALUE args) { int c_nargs = FIX2INT(nargs); - VALUE *c_args = (VALUE*) alloca(sizeof(VALUE) * c_nargs); + VALUE *c_args = alloca(sizeof(VALUE) * c_nargs); int i; for (i = 0; i < c_nargs; i++) @@ -117,380 +151,494 @@ static VALUE so_rb_obj_call_init(VALUE self, VALUE object, return Qnil; } +#endif -static VALUE so_rb_obj_class(VALUE self, VALUE obj) { - return rb_obj_class(obj); -} - +#ifdef HAVE_RB_OBJ_CLASSNAME static VALUE so_rbobjclassname(VALUE self, VALUE obj) { return rb_str_new2(rb_obj_classname(obj)); } +#endif + +#ifdef HAVE_RB_OBJ_FREEZE static VALUE object_spec_rb_obj_freeze(VALUE self, VALUE obj) { return rb_obj_freeze(obj); } +#endif +#ifdef HAVE_RB_OBJ_FROZEN_P static VALUE object_spec_rb_obj_frozen_p(VALUE self, VALUE obj) { return rb_obj_frozen_p(obj); } +#endif +#ifdef HAVE_RB_OBJ_ID static VALUE object_spec_rb_obj_id(VALUE self, VALUE obj) { return rb_obj_id(obj); } +#endif +#ifdef HAVE_RB_OBJ_IS_INSTANCE_OF static VALUE so_instance_of(VALUE self, VALUE obj, VALUE klass) { return rb_obj_is_instance_of(obj, klass); } +#endif +#ifdef HAVE_RB_OBJ_IS_KIND_OF static VALUE so_kind_of(VALUE self, VALUE obj, VALUE klass) { return rb_obj_is_kind_of(obj, klass); } +#endif +#ifdef HAVE_RB_OBJ_METHOD_ARITY static VALUE object_specs_rb_obj_method_arity(VALUE self, VALUE obj, VALUE mid) { return INT2FIX(rb_obj_method_arity(obj, SYM2ID(mid))); } +#endif -static VALUE object_specs_rb_obj_method(VALUE self, VALUE obj, VALUE method) { - return rb_obj_method(obj, method); -} - -#ifndef RUBY_VERSION_IS_3_2 +#ifdef HAVE_RB_OBJ_TAINT static VALUE object_spec_rb_obj_taint(VALUE self, VALUE obj) { return rb_obj_taint(obj); } #endif +#ifdef HAVE_RB_REQUIRE static VALUE so_require(VALUE self) { rb_require("fixtures/foo"); return Qnil; } +#endif +#ifdef HAVE_RB_RESPOND_TO static VALUE so_respond_to(VALUE self, VALUE obj, VALUE sym) { return rb_respond_to(obj, SYM2ID(sym)) ? Qtrue : Qfalse; } +#endif +#ifdef HAVE_RB_OBJ_RESPOND_TO static VALUE so_obj_respond_to(VALUE self, VALUE obj, VALUE sym, VALUE priv) { return rb_obj_respond_to(obj, SYM2ID(sym), priv == Qtrue ? 1 : 0) ? Qtrue : Qfalse; } +#endif +#ifdef HAVE_RB_METHOD_BOUNDP static VALUE object_spec_rb_method_boundp(VALUE self, VALUE obj, VALUE method, VALUE exclude_private) { ID id = SYM2ID(method); return rb_method_boundp(obj, id, exclude_private == Qtrue ? 1 : 0) ? Qtrue : Qfalse; } +#endif +#ifdef HAVE_RB_SPECIAL_CONST_P static VALUE object_spec_rb_special_const_p(VALUE self, VALUE value) { - return rb_special_const_p(value); + if (rb_special_const_p(value)) { + return Qtrue; + } else { + return Qfalse; + } } +#endif +#ifdef HAVE_RB_TO_ID static VALUE so_to_id(VALUE self, VALUE obj) { return ID2SYM(rb_to_id(obj)); } +#endif +#ifdef HAVE_RTEST static VALUE object_spec_RTEST(VALUE self, VALUE value) { return RTEST(value) ? Qtrue : Qfalse; } +#endif -static VALUE so_check_type(VALUE self, VALUE obj, VALUE other) { - rb_check_type(obj, TYPE(other)); - return Qtrue; -} - +#ifdef HAVE_TYPE static VALUE so_is_type_nil(VALUE self, VALUE obj) { - if (TYPE(obj) == T_NIL) { + if(TYPE(obj) == T_NIL) { return Qtrue; } return Qfalse; } static VALUE so_is_type_object(VALUE self, VALUE obj) { - if (TYPE(obj) == T_OBJECT) { + if(TYPE(obj) == T_OBJECT) { return Qtrue; } return Qfalse; } static VALUE so_is_type_array(VALUE self, VALUE obj) { - if (TYPE(obj) == T_ARRAY) { + if(TYPE(obj) == T_ARRAY) { return Qtrue; } return Qfalse; } static VALUE so_is_type_module(VALUE self, VALUE obj) { - if (TYPE(obj) == T_MODULE) { + if(TYPE(obj) == T_MODULE) { return Qtrue; } return Qfalse; } static VALUE so_is_type_class(VALUE self, VALUE obj) { - if (TYPE(obj) == T_CLASS) { + if(TYPE(obj) == T_CLASS) { return Qtrue; } return Qfalse; } static VALUE so_is_type_data(VALUE self, VALUE obj) { - if (TYPE(obj) == T_DATA) { + if(TYPE(obj) == T_DATA) { return Qtrue; } return Qfalse; } +#endif +#ifdef HAVE_RB_TYPE_P static VALUE so_is_rb_type_p_nil(VALUE self, VALUE obj) { - if (rb_type_p(obj, T_NIL)) { + if(rb_type_p(obj, T_NIL)) { return Qtrue; } return Qfalse; } static VALUE so_is_rb_type_p_object(VALUE self, VALUE obj) { - if (rb_type_p(obj, T_OBJECT)) { + if(rb_type_p(obj, T_OBJECT)) { return Qtrue; } return Qfalse; } static VALUE so_is_rb_type_p_array(VALUE self, VALUE obj) { - if (rb_type_p(obj, T_ARRAY)) { + if(rb_type_p(obj, T_ARRAY)) { return Qtrue; } return Qfalse; } static VALUE so_is_rb_type_p_module(VALUE self, VALUE obj) { - if (rb_type_p(obj, T_MODULE)) { + if(rb_type_p(obj, T_MODULE)) { return Qtrue; } return Qfalse; } static VALUE so_is_rb_type_p_class(VALUE self, VALUE obj) { - if (rb_type_p(obj, T_CLASS)) { + if(rb_type_p(obj, T_CLASS)) { return Qtrue; } return Qfalse; } static VALUE so_is_rb_type_p_data(VALUE self, VALUE obj) { - if (rb_type_p(obj, T_DATA)) { - return Qtrue; - } - return Qfalse; -} - -static VALUE so_is_rb_type_p_file(VALUE self, VALUE obj) { - if (rb_type_p(obj, T_FILE)) { + if(rb_type_p(obj, T_DATA)) { return Qtrue; } return Qfalse; } +#endif +#ifdef HAVE_BUILTIN_TYPE static VALUE so_is_builtin_type_object(VALUE self, VALUE obj) { - if (BUILTIN_TYPE(obj) == T_OBJECT) { + if(BUILTIN_TYPE(obj) == T_OBJECT) { return Qtrue; } return Qfalse; } static VALUE so_is_builtin_type_array(VALUE self, VALUE obj) { - if (BUILTIN_TYPE(obj) == T_ARRAY) { + if(BUILTIN_TYPE(obj) == T_ARRAY) { return Qtrue; } return Qfalse; } static VALUE so_is_builtin_type_module(VALUE self, VALUE obj) { - if (BUILTIN_TYPE(obj) == T_MODULE) { + if(BUILTIN_TYPE(obj) == T_MODULE) { return Qtrue; } return Qfalse; } static VALUE so_is_builtin_type_class(VALUE self, VALUE obj) { - if (BUILTIN_TYPE(obj) == T_CLASS) { + if(BUILTIN_TYPE(obj) == T_CLASS) { return Qtrue; } return Qfalse; } static VALUE so_is_builtin_type_data(VALUE self, VALUE obj) { - if (BUILTIN_TYPE(obj) == T_DATA) { + if(BUILTIN_TYPE(obj) == T_DATA) { return Qtrue; } return Qfalse; } +#endif +#ifdef HAVE_RB_TO_INT static VALUE object_spec_rb_to_int(VALUE self, VALUE obj) { return rb_to_int(obj); } +#endif +#ifdef HAVE_RB_OBJ_INSTANCE_EVAL static VALUE object_spec_rb_obj_instance_eval(VALUE self, VALUE obj) { return rb_obj_instance_eval(0, NULL, obj); } +#endif +#ifdef HAVE_RB_IV_GET static VALUE object_spec_rb_iv_get(VALUE self, VALUE obj, VALUE name) { return rb_iv_get(obj, RSTRING_PTR(name)); } +#endif +#ifdef HAVE_RB_IV_SET static VALUE object_spec_rb_iv_set(VALUE self, VALUE obj, VALUE name, VALUE value) { return rb_iv_set(obj, RSTRING_PTR(name), value); } +#endif -static VALUE object_spec_rb_ivar_count(VALUE self, VALUE obj) { - return ULONG2NUM(rb_ivar_count(obj)); -} - +#ifdef HAVE_RB_IVAR_GET static VALUE object_spec_rb_ivar_get(VALUE self, VALUE obj, VALUE sym_name) { return rb_ivar_get(obj, SYM2ID(sym_name)); } +#endif +#ifdef HAVE_RB_IVAR_SET static VALUE object_spec_rb_ivar_set(VALUE self, VALUE obj, VALUE sym_name, VALUE value) { return rb_ivar_set(obj, SYM2ID(sym_name), value); } +#endif +#ifdef HAVE_RB_IVAR_DEFINED static VALUE object_spec_rb_ivar_defined(VALUE self, VALUE obj, VALUE sym_name) { return rb_ivar_defined(obj, SYM2ID(sym_name)); } +#endif -static VALUE object_spec_rb_copy_generic_ivar(VALUE self, VALUE clone, VALUE obj) { - rb_copy_generic_ivar(clone, obj); - return self; -} - -static VALUE object_spec_rb_free_generic_ivar(VALUE self, VALUE obj) { - rb_free_generic_ivar(obj); - return self; -} - +#ifdef HAVE_RB_EQUAL static VALUE object_spec_rb_equal(VALUE self, VALUE a, VALUE b) { return rb_equal(a, b); } +#endif +#ifdef HAVE_RB_CLASS_INHERITED_P static VALUE object_spec_rb_class_inherited_p(VALUE self, VALUE mod, VALUE arg) { return rb_class_inherited_p(mod, arg); } +#endif -static int foreach_f(ID key, VALUE val, VALUE ary) { - rb_ary_push(ary, ID2SYM(key)); - rb_ary_push(ary, val); - return ST_CONTINUE; -} - -static VALUE object_spec_rb_ivar_foreach(VALUE self, VALUE obj) { - VALUE ary = rb_ary_new(); - rb_ivar_foreach(obj, foreach_f, ary); - return ary; -} - -static VALUE speced_allocator(VALUE klass) { - VALUE super = rb_class_get_superclass(klass); - VALUE instance = rb_get_alloc_func(super)(klass); - rb_iv_set(instance, "@from_custom_allocator", Qtrue); - return instance; -} - -static VALUE object_spec_rb_define_alloc_func(VALUE self, VALUE klass) { - rb_define_alloc_func(klass, speced_allocator); - return Qnil; -} - -static VALUE object_spec_rb_undef_alloc_func(VALUE self, VALUE klass) { - rb_undef_alloc_func(klass); - return Qnil; -} - -static VALUE object_spec_speced_allocator_p(VALUE self, VALUE klass) { - rb_alloc_func_t allocator = rb_get_alloc_func(klass); - return (allocator == speced_allocator) ? Qtrue : Qfalse; -} - -static VALUE object_spec_custom_alloc_func_p(VALUE self, VALUE klass) { - rb_alloc_func_t allocator = rb_get_alloc_func(klass); - return allocator ? Qtrue : Qfalse; -} void Init_object_spec(void) { - VALUE cls = rb_define_class("CApiObjectSpecs", rb_cObject); + VALUE cls; + cls = rb_define_class("CApiObjectSpecs", rb_cObject); + +#ifdef HAVE_FL_ABLE rb_define_method(cls, "FL_ABLE", object_spec_FL_ABLE, 1); +#endif + +#ifdef HAVE_FL_TEST rb_define_method(cls, "FL_TEST", object_spec_FL_TEST, 2); -#ifndef RUBY_VERSION_IS_3_1 +#endif + +#ifdef HAVE_OBJ_TAINT rb_define_method(cls, "OBJ_TAINT", object_spec_OBJ_TAINT, 1); +#endif + +#ifdef HAVE_OBJ_TAINTED rb_define_method(cls, "OBJ_TAINTED", object_spec_OBJ_TAINTED, 1); +#endif + +#ifdef HAVE_OBJ_INFECT rb_define_method(cls, "OBJ_INFECT", object_spec_OBJ_INFECT, 2); #endif + +#ifdef HAVE_RB_ANY_TO_S rb_define_method(cls, "rb_any_to_s", object_spec_rb_any_to_s, 1); +#endif + +#ifdef HAVE_RB_ATTR_GET rb_define_method(cls, "rb_attr_get", so_attr_get, 2); +#endif + +#ifdef HAVE_RB_OBJ_INSTANCE_VARIABLES rb_define_method(cls, "rb_obj_instance_variables", object_spec_rb_obj_instance_variables, 1); +#endif + +#ifdef HAVE_RB_CHECK_ARRAY_TYPE rb_define_method(cls, "rb_check_array_type", so_check_array_type, 1); +#endif + +#ifdef HAVE_RB_CHECK_CONVERT_TYPE rb_define_method(cls, "rb_check_convert_type", so_check_convert_type, 3); +#endif + +#ifdef HAVE_RB_CHECK_TO_INTEGER rb_define_method(cls, "rb_check_to_integer", so_check_to_integer, 2); +#endif + +#ifdef HAVE_RB_CHECK_FROZEN rb_define_method(cls, "rb_check_frozen", object_spec_rb_check_frozen, 1); +#endif + +#ifdef HAVE_RB_CHECK_STRING_TYPE rb_define_method(cls, "rb_check_string_type", so_check_string_type, 1); +#endif + +#ifdef HAVE_RB_CLASS_OF rb_define_method(cls, "rb_class_of", so_rbclassof, 1); +#endif + +#ifdef HAVE_RB_CONVERT_TYPE rb_define_method(cls, "rb_convert_type", so_convert_type, 3); +#endif + +#ifdef HAVE_RB_EXTEND_OBJECT rb_define_method(cls, "rb_extend_object", object_spec_rb_extend_object, 2); +#endif + +#ifdef HAVE_RB_INSPECT rb_define_method(cls, "rb_inspect", so_inspect, 1); +#endif + +#ifdef HAVE_RB_OBJ_ALLOC rb_define_method(cls, "rb_obj_alloc", so_rb_obj_alloc, 1); +#endif + +#ifdef HAVE_RB_OBJ_ALLOC rb_define_method(cls, "rb_obj_dup", so_rb_obj_dup, 1); +#endif + +#ifdef HAVE_RB_OBJ_CALL_INIT rb_define_method(cls, "rb_obj_call_init", so_rb_obj_call_init, 3); - rb_define_method(cls, "rb_obj_class", so_rb_obj_class, 1); +#endif + +#ifdef HAVE_RB_OBJ_CLASSNAME rb_define_method(cls, "rb_obj_classname", so_rbobjclassname, 1); +#endif + +#ifdef HAVE_RB_OBJ_FREEZE rb_define_method(cls, "rb_obj_freeze", object_spec_rb_obj_freeze, 1); +#endif + +#ifdef HAVE_RB_OBJ_FROZEN_P rb_define_method(cls, "rb_obj_frozen_p", object_spec_rb_obj_frozen_p, 1); +#endif + +#ifdef HAVE_RB_OBJ_ID rb_define_method(cls, "rb_obj_id", object_spec_rb_obj_id, 1); +#endif + +#ifdef HAVE_RB_OBJ_IS_INSTANCE_OF rb_define_method(cls, "rb_obj_is_instance_of", so_instance_of, 2); +#endif + +#ifdef HAVE_RB_OBJ_IS_KIND_OF rb_define_method(cls, "rb_obj_is_kind_of", so_kind_of, 2); +#endif + +#ifdef HAVE_RB_OBJ_METHOD_ARITY rb_define_method(cls, "rb_obj_method_arity", object_specs_rb_obj_method_arity, 2); - rb_define_method(cls, "rb_obj_method", object_specs_rb_obj_method, 2); -#ifndef RUBY_VERSION_IS_3_2 +#endif + +#ifdef HAVE_RB_OBJ_TAINT rb_define_method(cls, "rb_obj_taint", object_spec_rb_obj_taint, 1); #endif + +#ifdef HAVE_RB_REQUIRE rb_define_method(cls, "rb_require", so_require, 0); +#endif + +#ifdef HAVE_RB_RESPOND_TO rb_define_method(cls, "rb_respond_to", so_respond_to, 2); +#endif + +#ifdef HAVE_RB_METHOD_BOUNDP rb_define_method(cls, "rb_method_boundp", object_spec_rb_method_boundp, 3); +#endif + +#ifdef HAVE_RB_OBJ_RESPOND_TO rb_define_method(cls, "rb_obj_respond_to", so_obj_respond_to, 3); +#endif + +#ifdef HAVE_RB_SPECIAL_CONST_P rb_define_method(cls, "rb_special_const_p", object_spec_rb_special_const_p, 1); +#endif +#ifdef HAVE_RB_STR_NEW2 +#endif + +#ifdef HAVE_RB_TO_ID rb_define_method(cls, "rb_to_id", so_to_id, 1); +#endif + +#ifdef HAVE_RTEST rb_define_method(cls, "RTEST", object_spec_RTEST, 1); - rb_define_method(cls, "rb_check_type", so_check_type, 2); +#endif + +#ifdef HAVE_TYPE rb_define_method(cls, "rb_is_type_nil", so_is_type_nil, 1); rb_define_method(cls, "rb_is_type_object", so_is_type_object, 1); rb_define_method(cls, "rb_is_type_array", so_is_type_array, 1); rb_define_method(cls, "rb_is_type_module", so_is_type_module, 1); rb_define_method(cls, "rb_is_type_class", so_is_type_class, 1); rb_define_method(cls, "rb_is_type_data", so_is_type_data, 1); +#endif + +#ifdef HAVE_RB_TYPE_P rb_define_method(cls, "rb_is_rb_type_p_nil", so_is_rb_type_p_nil, 1); rb_define_method(cls, "rb_is_rb_type_p_object", so_is_rb_type_p_object, 1); rb_define_method(cls, "rb_is_rb_type_p_array", so_is_rb_type_p_array, 1); rb_define_method(cls, "rb_is_rb_type_p_module", so_is_rb_type_p_module, 1); rb_define_method(cls, "rb_is_rb_type_p_class", so_is_rb_type_p_class, 1); rb_define_method(cls, "rb_is_rb_type_p_data", so_is_rb_type_p_data, 1); - rb_define_method(cls, "rb_is_rb_type_p_file", so_is_rb_type_p_file, 1); +#endif + +#ifdef HAVE_BUILTIN_TYPE rb_define_method(cls, "rb_is_builtin_type_object", so_is_builtin_type_object, 1); rb_define_method(cls, "rb_is_builtin_type_array", so_is_builtin_type_array, 1); rb_define_method(cls, "rb_is_builtin_type_module", so_is_builtin_type_module, 1); rb_define_method(cls, "rb_is_builtin_type_class", so_is_builtin_type_class, 1); rb_define_method(cls, "rb_is_builtin_type_data", so_is_builtin_type_data, 1); +#endif + +#ifdef HAVE_RB_TO_INT rb_define_method(cls, "rb_to_int", object_spec_rb_to_int, 1); +#endif + +#ifdef HAVE_RB_EQUAL rb_define_method(cls, "rb_equal", object_spec_rb_equal, 2); +#endif + +#ifdef HAVE_RB_CLASS_INHERITED_P rb_define_method(cls, "rb_class_inherited_p", object_spec_rb_class_inherited_p, 2); +#endif + +#ifdef HAVE_RB_OBJ_INSTANCE_EVAL rb_define_method(cls, "rb_obj_instance_eval", object_spec_rb_obj_instance_eval, 1); +#endif + +#ifdef HAVE_RB_IV_GET rb_define_method(cls, "rb_iv_get", object_spec_rb_iv_get, 2); +#endif + +#ifdef HAVE_RB_IV_SET rb_define_method(cls, "rb_iv_set", object_spec_rb_iv_set, 3); - rb_define_method(cls, "rb_ivar_count", object_spec_rb_ivar_count, 1); +#endif + +#ifdef HAVE_RB_IVAR_GET rb_define_method(cls, "rb_ivar_get", object_spec_rb_ivar_get, 2); +#endif + +#ifdef HAVE_RB_IVAR_SET rb_define_method(cls, "rb_ivar_set", object_spec_rb_ivar_set, 3); +#endif + +#ifdef HAVE_RB_IVAR_DEFINED rb_define_method(cls, "rb_ivar_defined", object_spec_rb_ivar_defined, 2); - rb_define_method(cls, "rb_copy_generic_ivar", object_spec_rb_copy_generic_ivar, 2); - rb_define_method(cls, "rb_free_generic_ivar", object_spec_rb_free_generic_ivar, 1); - rb_define_method(cls, "rb_define_alloc_func", object_spec_rb_define_alloc_func, 1); - rb_define_method(cls, "rb_undef_alloc_func", object_spec_rb_undef_alloc_func, 1); - rb_define_method(cls, "speced_allocator?", object_spec_speced_allocator_p, 1); - rb_define_method(cls, "custom_alloc_func?", object_spec_custom_alloc_func_p, 1); - rb_define_method(cls, "not_implemented_method", rb_f_notimplement, -1); - rb_define_method(cls, "rb_ivar_foreach", object_spec_rb_ivar_foreach, 1); +#endif + } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/proc_spec.c b/spec/ruby/optional/capi/ext/proc_spec.c index b7cd5d6262..f9c0f6b1b9 100644 --- a/spec/ruby/optional/capi/ext/proc_spec.c +++ b/spec/ruby/optional/capi/ext/proc_spec.c @@ -7,90 +7,27 @@ extern "C" { #endif -VALUE proc_spec_rb_proc_new_function(RB_BLOCK_CALL_FUNC_ARGLIST(args, dummy)) { +#ifdef HAVE_RB_PROC_NEW +VALUE proc_spec_rb_proc_new_function(VALUE args) { return rb_funcall(args, rb_intern("inspect"), 0); } -VALUE proc_spec_rb_proc_new_function_arg(VALUE arg, VALUE callback_arg, int argc, const VALUE *argv, VALUE blockarg) { - return arg; -} - -VALUE proc_spec_rb_proc_new_function_argc(VALUE arg, VALUE callback_arg, int argc, const VALUE *argv, VALUE blockarg) { - return INT2FIX(argc); -} - -VALUE proc_spec_rb_proc_new_function_argv_n(VALUE arg, VALUE callback_arg, int argc, const VALUE *argv, VALUE blockarg) { - int n = FIX2INT(arg); - if (n < argc) { - return argv[n]; - } else { - rb_exc_raise(rb_exc_new2(rb_eArgError, "Arg index out of bounds.")); - } -} - -VALUE proc_spec_rb_proc_new_function_callback_arg(VALUE arg, VALUE callback_arg, int argc, const VALUE *argv, VALUE blockarg) { - return callback_arg; -} - -VALUE proc_spec_rb_proc_new_function_blockarg(VALUE arg, VALUE callback_arg, int argc, const VALUE *argv, VALUE blockarg) { - return blockarg; -} - -VALUE proc_spec_rb_proc_new_function_block_given_p(VALUE arg, VALUE callback_arg, int argc, const VALUE *argv, VALUE blockarg) { - return rb_block_given_p() ? Qtrue : Qfalse; -} - VALUE proc_spec_rb_proc_new(VALUE self) { return rb_proc_new(proc_spec_rb_proc_new_function, Qnil); } +#endif -VALUE proc_spec_rb_proc_new_arg(VALUE self) { - return rb_proc_new(proc_spec_rb_proc_new_function_arg, Qnil); -} - -VALUE proc_spec_rb_proc_new_argc(VALUE self) { - return rb_proc_new(proc_spec_rb_proc_new_function_argc, Qnil); -} - -VALUE proc_spec_rb_proc_new_argv_n(VALUE self) { - return rb_proc_new(proc_spec_rb_proc_new_function_argv_n, Qnil); -} - -VALUE proc_spec_rb_proc_new_callback_arg(VALUE self, VALUE arg) { - return rb_proc_new(proc_spec_rb_proc_new_function_callback_arg, arg); -} - -VALUE proc_spec_rb_proc_new_blockarg(VALUE self) { - return rb_proc_new(proc_spec_rb_proc_new_function_blockarg, Qnil); -} - -VALUE proc_spec_rb_proc_new_block_given_p(VALUE self) { - return rb_proc_new(proc_spec_rb_proc_new_function_block_given_p, Qnil); -} - +#ifdef HAVE_RB_PROC_ARITY VALUE proc_spec_rb_proc_arity(VALUE self, VALUE prc) { return INT2FIX(rb_proc_arity(prc)); } +#endif +#ifdef HAVE_RB_PROC_CALL VALUE proc_spec_rb_proc_call(VALUE self, VALUE prc, VALUE args) { return rb_proc_call(prc, args); } - -VALUE proc_spec_rb_proc_call_kw(VALUE self, VALUE prc, VALUE args) { - return rb_proc_call_kw(prc, args, RB_PASS_KEYWORDS); -} - -VALUE proc_spec_rb_proc_call_with_block(VALUE self, VALUE prc, VALUE args, VALUE block) { - return rb_proc_call_with_block(prc, RARRAY_LENINT(args), RARRAY_PTR(args), block); -} - -static VALUE proc_spec_rb_proc_call_with_block_kw(VALUE self, VALUE prc, VALUE args, VALUE block) { - return rb_proc_call_with_block_kw(prc, RARRAY_LENINT(args), RARRAY_PTR(args), block, RB_PASS_KEYWORDS); -} - -VALUE proc_spec_rb_obj_is_proc(VALUE self, VALUE prc) { - return rb_obj_is_proc(prc); -} +#endif /* This helper is not strictly necessary but reflects the code in wxRuby that * originally exposed issues with this Proc.new behavior. @@ -125,21 +62,22 @@ VALUE proc_spec_rb_Proc_new(VALUE self, VALUE scenario) { } void Init_proc_spec(void) { - VALUE cls = rb_define_class("CApiProcSpecs", rb_cObject); + VALUE cls; + cls = rb_define_class("CApiProcSpecs", rb_cObject); + +#ifdef HAVE_RB_PROC_NEW rb_define_method(cls, "rb_proc_new", proc_spec_rb_proc_new, 0); - rb_define_method(cls, "rb_proc_new_arg", proc_spec_rb_proc_new_arg, 0); - rb_define_method(cls, "rb_proc_new_argc", proc_spec_rb_proc_new_argc, 0); - rb_define_method(cls, "rb_proc_new_argv_n", proc_spec_rb_proc_new_argv_n, 0); - rb_define_method(cls, "rb_proc_new_callback_arg", proc_spec_rb_proc_new_callback_arg, 1); - rb_define_method(cls, "rb_proc_new_blockarg", proc_spec_rb_proc_new_blockarg, 0); - rb_define_method(cls, "rb_proc_new_block_given_p", proc_spec_rb_proc_new_block_given_p, 0); +#endif + +#ifdef HAVE_RB_PROC_ARITY rb_define_method(cls, "rb_proc_arity", proc_spec_rb_proc_arity, 1); +#endif + +#ifdef HAVE_RB_PROC_CALL rb_define_method(cls, "rb_proc_call", proc_spec_rb_proc_call, 2); - rb_define_method(cls, "rb_proc_call_kw", proc_spec_rb_proc_call_kw, 2); - rb_define_method(cls, "rb_proc_call_with_block", proc_spec_rb_proc_call_with_block, 3); - rb_define_method(cls, "rb_proc_call_with_block_kw", proc_spec_rb_proc_call_with_block_kw, 3); +#endif + rb_define_method(cls, "rb_Proc_new", proc_spec_rb_Proc_new, 1); - rb_define_method(cls, "rb_obj_is_proc", proc_spec_rb_obj_is_proc, 1); } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/range_spec.c b/spec/ruby/optional/capi/ext/range_spec.c index b0cf1a8662..6dc2d579fd 100644 --- a/spec/ruby/optional/capi/ext/range_spec.c +++ b/spec/ruby/optional/capi/ext/range_spec.c @@ -5,14 +5,17 @@ extern "C" { #endif +#ifdef HAVE_RB_RANGE_NEW VALUE range_spec_rb_range_new(int argc, VALUE* argv, VALUE self) { int exclude_end = 0; - if (argc == 3) { + if(argc == 3) { exclude_end = RTEST(argv[2]); } return rb_range_new(argv[0], argv[1], exclude_end); } +#endif +#ifdef HAVE_RB_RANGE_VALUES VALUE range_spec_rb_range_values(VALUE self, VALUE range) { VALUE beg; VALUE end; @@ -24,7 +27,9 @@ VALUE range_spec_rb_range_values(VALUE self, VALUE range) { rb_ary_store(ary, 2, excl ? Qtrue : Qfalse); return ary; } +#endif +#ifdef HAVE_RB_RANGE_BEG_LEN VALUE range_spec_rb_range_beg_len(VALUE self, VALUE range, VALUE begpv, VALUE lenpv, VALUE lenv, VALUE errv) { long begp = FIX2LONG(begpv); long lenp = FIX2LONG(lenpv); @@ -37,12 +42,23 @@ VALUE range_spec_rb_range_beg_len(VALUE self, VALUE range, VALUE begpv, VALUE le rb_ary_store(ary, 2, res); return ary; } +#endif void Init_range_spec(void) { - VALUE cls = rb_define_class("CApiRangeSpecs", rb_cObject); + VALUE cls; + cls = rb_define_class("CApiRangeSpecs", rb_cObject); + +#ifdef HAVE_RB_RANGE_NEW rb_define_method(cls, "rb_range_new", range_spec_rb_range_new, -1); +#endif + +#ifdef HAVE_RB_RANGE_VALUES rb_define_method(cls, "rb_range_values", range_spec_rb_range_values, 1); +#endif + +#ifdef HAVE_RB_RANGE_BEG_LEN rb_define_method(cls, "rb_range_beg_len", range_spec_rb_range_beg_len, 5); +#endif } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/rational_spec.c b/spec/ruby/optional/capi/ext/rational_spec.c index 6273af68c5..9f349261a0 100644 --- a/spec/ruby/optional/capi/ext/rational_spec.c +++ b/spec/ruby/optional/capi/ext/rational_spec.c @@ -5,48 +5,89 @@ extern "C" { #endif +#ifdef HAVE_RB_RATIONAL static VALUE rational_spec_rb_Rational(VALUE self, VALUE num, VALUE den) { return rb_Rational(num, den); } +#endif +#ifdef HAVE_RB_RATIONAL1 static VALUE rational_spec_rb_Rational1(VALUE self, VALUE num) { return rb_Rational1(num); } +#endif +#ifdef HAVE_RB_RATIONAL2 static VALUE rational_spec_rb_Rational2(VALUE self, VALUE num, VALUE den) { return rb_Rational2(num, den); } +#endif +#ifdef HAVE_RB_RATIONAL_NEW static VALUE rational_spec_rb_rational_new(VALUE self, VALUE num, VALUE den) { return rb_rational_new(num, den); } +#endif +#ifdef HAVE_RB_RATIONAL_NEW1 static VALUE rational_spec_rb_rational_new1(VALUE self, VALUE num) { return rb_rational_new1(num); } +#endif +#ifdef HAVE_RB_RATIONAL_NEW2 static VALUE rational_spec_rb_rational_new2(VALUE self, VALUE num, VALUE den) { return rb_rational_new2(num, den); } +#endif +#ifdef HAVE_RB_RATIONAL_NUM static VALUE rational_spec_rb_rational_num(VALUE self, VALUE rational) { return rb_rational_num(rational); } +#endif +#ifdef HAVE_RB_RATIONAL_DEN static VALUE rational_spec_rb_rational_den(VALUE self, VALUE rational) { return rb_rational_den(rational); } +#endif void Init_rational_spec(void) { - VALUE cls = rb_define_class("CApiRationalSpecs", rb_cObject); + VALUE cls; + cls = rb_define_class("CApiRationalSpecs", rb_cObject); + +#ifdef HAVE_RB_RATIONAL rb_define_method(cls, "rb_Rational", rational_spec_rb_Rational, 2); +#endif + +#ifdef HAVE_RB_RATIONAL1 rb_define_method(cls, "rb_Rational1", rational_spec_rb_Rational1, 1); +#endif + +#ifdef HAVE_RB_RATIONAL2 rb_define_method(cls, "rb_Rational2", rational_spec_rb_Rational2, 2); +#endif + +#ifdef HAVE_RB_RATIONAL_NEW rb_define_method(cls, "rb_rational_new", rational_spec_rb_rational_new, 2); +#endif + +#ifdef HAVE_RB_RATIONAL_NEW1 rb_define_method(cls, "rb_rational_new1", rational_spec_rb_rational_new1, 1); +#endif + +#ifdef HAVE_RB_RATIONAL_NEW2 rb_define_method(cls, "rb_rational_new2", rational_spec_rb_rational_new2, 2); +#endif + +#ifdef HAVE_RB_RATIONAL_NUM rb_define_method(cls, "rb_rational_num", rational_spec_rb_rational_num, 1); +#endif + +#ifdef HAVE_RB_RATIONAL_DEN rb_define_method(cls, "rb_rational_den", rational_spec_rb_rational_den, 1); +#endif } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/rbasic_spec.c b/spec/ruby/optional/capi/ext/rbasic_spec.c deleted file mode 100644 index 26be2fed6d..0000000000 --- a/spec/ruby/optional/capi/ext/rbasic_spec.c +++ /dev/null @@ -1,114 +0,0 @@ -#include "ruby.h" -#include "rubyspec.h" - -#ifdef __cplusplus -extern "C" { -#endif - -#ifndef RBASIC_FLAGS -#define RBASIC_FLAGS(obj) (RBASIC(obj)->flags) -#endif - -#ifndef RBASIC_SET_FLAGS -#define RBASIC_SET_FLAGS(obj, flags_to_set) (RBASIC(obj)->flags = flags_to_set) -#endif - -#ifndef FL_SHAREABLE -static const VALUE VISIBLE_BITS = FL_TAINT | FL_FREEZE; -static const VALUE DATA_VISIBLE_BITS = FL_TAINT | FL_FREEZE | ~(FL_USER0 - 1); -#else -static const VALUE VISIBLE_BITS = FL_FREEZE; -static const VALUE DATA_VISIBLE_BITS = FL_FREEZE | ~(FL_USER0 - 1); -#endif - -#if SIZEOF_VALUE == SIZEOF_LONG -#define VALUE2NUM(v) ULONG2NUM(v) -#define NUM2VALUE(n) NUM2ULONG(n) -#elif SIZEOF_VALUE == SIZEOF_LONG_LONG -#define VALUE2NUM(v) ULL2NUM(v) -#define NUM2VALUE(n) NUM2ULL(n) -#else -#error "unsupported" -#endif - - -#ifndef RUBY_VERSION_IS_3_1 -VALUE rbasic_spec_taint_flag(VALUE self) { - return VALUE2NUM(RUBY_FL_TAINT); -} -#endif - -VALUE rbasic_spec_freeze_flag(VALUE self) { - return VALUE2NUM(RUBY_FL_FREEZE); -} - -static VALUE spec_get_flags(VALUE obj, VALUE visible_bits) { - VALUE flags = RB_FL_TEST(obj, visible_bits); - return VALUE2NUM(flags); -} - -static VALUE spec_set_flags(VALUE obj, VALUE flags, VALUE visible_bits) { - flags &= visible_bits; - - // Could also be done like: - // RB_FL_UNSET(obj, visible_bits); - // RB_FL_SET(obj, flags); - // But that seems rather indirect - RBASIC_SET_FLAGS(obj, (RBASIC_FLAGS(obj) & ~visible_bits) | flags); - - return VALUE2NUM(flags); -} - -static VALUE rbasic_spec_get_flags(VALUE self, VALUE obj) { - return spec_get_flags(obj, VISIBLE_BITS); -} - -static VALUE rbasic_spec_set_flags(VALUE self, VALUE obj, VALUE flags) { - return spec_set_flags(obj, NUM2VALUE(flags), VISIBLE_BITS); -} - -static VALUE rbasic_spec_copy_flags(VALUE self, VALUE to, VALUE from) { - return spec_set_flags(to, RBASIC_FLAGS(from), VISIBLE_BITS); -} - -static VALUE rbasic_spec_get_klass(VALUE self, VALUE obj) { - return RBASIC_CLASS(obj); -} - -static VALUE rbasic_rdata_spec_get_flags(VALUE self, VALUE structure) { - return spec_get_flags(structure, DATA_VISIBLE_BITS); -} - -static VALUE rbasic_rdata_spec_set_flags(VALUE self, VALUE structure, VALUE flags) { - return spec_set_flags(structure, NUM2VALUE(flags), DATA_VISIBLE_BITS); -} - -static VALUE rbasic_rdata_spec_copy_flags(VALUE self, VALUE to, VALUE from) { - return spec_set_flags(to, RBASIC_FLAGS(from), DATA_VISIBLE_BITS); -} - -static VALUE rbasic_rdata_spec_get_klass(VALUE self, VALUE structure) { - return RBASIC_CLASS(structure); -} - -void Init_rbasic_spec(void) { - VALUE cls = rb_define_class("CApiRBasicSpecs", rb_cObject); -#ifndef RUBY_VERSION_IS_3_1 - rb_define_method(cls, "taint_flag", rbasic_spec_taint_flag, 0); -#endif - rb_define_method(cls, "freeze_flag", rbasic_spec_freeze_flag, 0); - rb_define_method(cls, "get_flags", rbasic_spec_get_flags, 1); - rb_define_method(cls, "set_flags", rbasic_spec_set_flags, 2); - rb_define_method(cls, "copy_flags", rbasic_spec_copy_flags, 2); - rb_define_method(cls, "get_klass", rbasic_spec_get_klass, 1); - - cls = rb_define_class("CApiRBasicRDataSpecs", rb_cObject); - rb_define_method(cls, "get_flags", rbasic_rdata_spec_get_flags, 1); - rb_define_method(cls, "set_flags", rbasic_rdata_spec_set_flags, 2); - rb_define_method(cls, "copy_flags", rbasic_rdata_spec_copy_flags, 2); - rb_define_method(cls, "get_klass", rbasic_rdata_spec_get_klass, 1); -} - -#ifdef __cplusplus -} -#endif diff --git a/spec/ruby/optional/capi/ext/regexp_spec.c b/spec/ruby/optional/capi/ext/regexp_spec.c index 9de7982b50..1058293444 100644 --- a/spec/ruby/optional/capi/ext/regexp_spec.c +++ b/spec/ruby/optional/capi/ext/regexp_spec.c @@ -9,64 +9,74 @@ extern "C" { #endif -VALUE regexp_spec_re(VALUE self, VALUE str, VALUE options) { - char *cstr = StringValueCStr(str); - int opts = FIX2INT(options); - return rb_reg_new(cstr, strlen(cstr), opts); +#ifdef HAVE_RB_REG_NEW +VALUE regexp_spec_re(VALUE self) { + return rb_reg_new("a", 1, 0); } +#endif +#ifdef HAVE_RB_REG_NTH_MATCH VALUE regexp_spec_reg_1st_match(VALUE self, VALUE md) { return rb_reg_nth_match(1, md); } +#endif +#ifdef HAVE_RB_REG_OPTIONS VALUE regexp_spec_rb_reg_options(VALUE self, VALUE regexp) { return INT2FIX(rb_reg_options(regexp)); } +#endif +#ifdef HAVE_RB_REG_REGCOMP VALUE regexp_spec_rb_reg_regcomp(VALUE self, VALUE str) { return rb_reg_regcomp(str); } +#endif +#ifdef HAVE_RB_REG_MATCH VALUE regexp_spec_reg_match(VALUE self, VALUE re, VALUE str) { return rb_reg_match(re, str); } +#endif +#ifdef HAVE_RB_BACKREF_GET VALUE regexp_spec_backref_get(VALUE self) { return rb_backref_get(); } - -static VALUE regexp_spec_backref_set(VALUE self, VALUE backref) { - rb_backref_set(backref); - return Qnil; -} - -VALUE regexp_spec_reg_match_backref_get(VALUE self, VALUE re, VALUE str) { - rb_reg_match(re, str); - return rb_backref_get(); -} +#endif VALUE regexp_spec_match(VALUE self, VALUE regexp, VALUE str) { return rb_funcall(regexp, rb_intern("match"), 1, str); } -VALUE regexp_spec_memcicmp(VALUE self, VALUE str1, VALUE str2) { - long l1 = RSTRING_LEN(str1); - long l2 = RSTRING_LEN(str2); - return INT2FIX(rb_memcicmp(RSTRING_PTR(str1), RSTRING_PTR(str2), l1 < l2 ? l1 : l2)); -} - void Init_regexp_spec(void) { VALUE cls = rb_define_class("CApiRegexpSpecs", rb_cObject); + rb_define_method(cls, "match", regexp_spec_match, 2); - rb_define_method(cls, "a_re", regexp_spec_re, 2); + +#ifdef HAVE_RB_REG_NEW + rb_define_method(cls, "a_re", regexp_spec_re, 0); +#endif + +#ifdef HAVE_RB_REG_NTH_MATCH rb_define_method(cls, "a_re_1st_match", regexp_spec_reg_1st_match, 1); +#endif + +#ifdef HAVE_RB_REG_MATCH rb_define_method(cls, "rb_reg_match", regexp_spec_reg_match, 2); +#endif + +#ifdef HAVE_RB_BACKREF_GET rb_define_method(cls, "rb_backref_get", regexp_spec_backref_get, 0); - rb_define_method(cls, "rb_backref_set", regexp_spec_backref_set, 1); - rb_define_method(cls, "rb_reg_match_backref_get", regexp_spec_reg_match_backref_get, 2); +#endif + +#ifdef HAVE_RB_REG_OPTIONS rb_define_method(cls, "rb_reg_options", regexp_spec_rb_reg_options, 1); +#endif + +#ifdef HAVE_RB_REG_REGCOMP rb_define_method(cls, "rb_reg_regcomp", regexp_spec_rb_reg_regcomp, 1); - rb_define_method(cls, "rb_memcicmp", regexp_spec_memcicmp, 2); +#endif } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/rubyspec.h b/spec/ruby/optional/capi/ext/rubyspec.h index 1df274ead1..341cff0428 100644 --- a/spec/ruby/optional/capi/ext/rubyspec.h +++ b/spec/ruby/optional/capi/ext/rubyspec.h @@ -1,8 +1,9 @@ #ifndef RUBYSPEC_H #define RUBYSPEC_H -/* Define convenience macros similar to the mspec - * guards to assist with version incompatibilities. */ +/* Define convenience macros similar to the mspec guards to assist + * with version incompatibilities. + */ #include <ruby.h> #ifdef HAVE_RUBY_VERSION_H @@ -11,29 +12,6 @@ # include <version.h> #endif -/* copied from ext/-test-/cxxanyargs/cxxanyargs.cpp */ -#if 0 /* Ignore deprecation warnings */ - -#elif defined(_MSC_VER) -#pragma warning(disable : 4996) - -#elif defined(__INTEL_COMPILER) -#pragma warning(disable : 1786) - -#elif defined(__clang__) -#pragma clang diagnostic ignored "-Wdeprecated-declarations" - -#elif defined(__GNUC__) -#pragma GCC diagnostic ignored "-Wdeprecated-declarations" - -#elif defined(__SUNPRO_CC) -#pragma error_messages (off,symdeprecated) - -#else -// :FIXME: improve here for your compiler. - -#endif - #ifndef RUBY_VERSION_MAJOR #define RUBY_VERSION_MAJOR RUBY_API_VERSION_MAJOR #define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR @@ -44,26 +22,592 @@ ((RUBY_VERSION_MAJOR < (major)) || \ (RUBY_VERSION_MAJOR == (major) && RUBY_VERSION_MINOR < (minor)) || \ (RUBY_VERSION_MAJOR == (major) && RUBY_VERSION_MINOR == (minor) && RUBY_VERSION_TEENY < (teeny))) -#define RUBY_VERSION_SINCE(major,minor,teeny) (!RUBY_VERSION_BEFORE(major, minor, teeny)) -#if RUBY_VERSION_SINCE(3, 4, 0) -#define RUBY_VERSION_IS_3_4 +#if RUBY_VERSION_MAJOR > 2 || (RUBY_VERSION_MAJOR == 2 && RUBY_VERSION_MINOR >= 4) +#define RUBY_VERSION_IS_2_4 #endif -#if RUBY_VERSION_SINCE(3, 3, 0) -#define RUBY_VERSION_IS_3_3 +#if RUBY_VERSION_MAJOR > 2 || (RUBY_VERSION_MAJOR == 2 && RUBY_VERSION_MINOR >= 3) +#define RUBY_VERSION_IS_2_3 #endif -#if RUBY_VERSION_SINCE(3, 2, 0) -#define RUBY_VERSION_IS_3_2 +/* Define all function flags */ + +/* Array */ +#define HAVE_RB_ARRAY 1 +#define HAVE_RARRAY_AREF 1 +#define HAVE_RARRAY_LEN 1 +#define HAVE_RARRAY_PTR 1 +#define HAVE_RB_ARY_AREF 1 +#define HAVE_RB_ARY_CLEAR 1 +#define HAVE_RB_ARY_DELETE 1 +#define HAVE_RB_ARY_DELETE_AT 1 +#define HAVE_RB_ARY_DUP 1 +#define HAVE_RB_ARY_ENTRY 1 +#define HAVE_RB_ARY_FREEZE 1 +#define HAVE_RB_ARY_INCLUDES 1 +#define HAVE_RB_ARY_JOIN 1 +#define HAVE_RB_ARY_NEW 1 +#define HAVE_RB_ARY_NEW2 1 +#define HAVE_RB_ARY_NEW_CAPA 1 +#define HAVE_RB_ARY_NEW3 1 +#define HAVE_RB_ARY_NEW_FROM_ARGS 1 +#define HAVE_RB_ARY_NEW4 1 +#define HAVE_RB_ARY_NEW_FROM_VALUES 1 +#define HAVE_RB_ARY_POP 1 +#define HAVE_RB_ARY_PUSH 1 +#define HAVE_RB_ARY_CAT 1 +#define HAVE_RB_ARY_REVERSE 1 +#define HAVE_RB_ARY_ROTATE 1 +#define HAVE_RB_ARY_SHIFT 1 +#define HAVE_RB_ARY_STORE 1 +#define HAVE_RB_ARY_CONCAT 1 +#define HAVE_RB_ARY_PLUS 1 +#define HAVE_RB_ARY_TO_ARY 1 +#define HAVE_RB_ARY_SUBSEQ 1 +#define HAVE_RB_ARY_TO_S 1 +#define HAVE_RB_ARY_UNSHIFT 1 +#define HAVE_RB_ASSOC_NEW 1 + +#define HAVE_RB_EACH 1 +#define HAVE_RB_ITERATE 1 +#define HAVE_RB_MEM_CLEAR 1 + +/* Bignum */ +#define HAVE_ABSINT_SIZE 1 +#define HAVE_RB_BIG2DBL 1 +#define HAVE_RB_DBL2BIG 1 +#define HAVE_RB_BIG2LL 1 +#define HAVE_RB_BIG2LONG 1 +#define HAVE_RB_BIG2STR 1 +#define HAVE_RB_BIG2ULONG 1 +#define HAVE_RB_BIG_CMP 1 +#define HAVE_RB_BIG_PACK 1 + +/* Class */ +#define HAVE_RB_CALL_SUPER 1 +#define HAVE_RB_CLASS2NAME 1 +#define HAVE_RB_CLASS_NAME 1 +#define HAVE_RB_CLASS_NEW 1 +#define HAVE_RB_CLASS_NEW_INSTANCE 1 +#define HAVE_RB_CLASS_PATH 1 +#define HAVE_RB_CLASS_REAL 1 +#define HAVE_RB_CVAR_DEFINED 1 +#define HAVE_RB_CVAR_GET 1 +#define HAVE_RB_CVAR_SET 1 +#define HAVE_RB_CV_GET 1 +#define HAVE_RB_CV_SET 1 +#define HAVE_RB_DEFINE_ATTR 1 +#define HAVE_RB_DEFINE_CLASS_VARIABLE 1 +#define HAVE_RB_INCLUDE_MODULE 1 +#define HAVE_RB_PATH2CLASS 1 +#define HAVE_RB_PATH_TO_CLASS 1 +#define HAVE_RB_CLASS_SUPERCLASS 1 + +/* Complex */ +#define HAVE_RB_COMPLEX 1 +#define HAVE_RB_COMPLEX1 1 +#define HAVE_RB_COMPLEX2 1 +#define HAVE_RB_COMPLEX_NEW 1 +#define HAVE_RB_COMPLEX_NEW1 1 +#define HAVE_RB_COMPLEX_NEW2 1 + +/* Constants */ +#define HAVE_RB_CARRAY 1 +#ifndef RUBY_INTEGER_UNIFICATION +#define HAVE_RB_CBIGNUM 1 #endif +#define HAVE_RB_CCLASS 1 +#define HAVE_RB_CDATA 1 +#define HAVE_RB_CFALSECLASS 1 +#define HAVE_RB_CFILE 1 +#ifndef RUBY_INTEGER_UNIFICATION +#define HAVE_RB_CFIXNUM 1 +#endif +#define HAVE_RB_CFLOAT 1 +#define HAVE_RB_CHASH 1 +#define HAVE_RB_CINTEGER 1 +#define HAVE_RB_CIO 1 +#define HAVE_RB_CMATCH 1 +#define HAVE_RB_CMODULE 1 +#define HAVE_RB_CNILCLASS 1 +#define HAVE_RB_CNUMERIC 1 +#define HAVE_RB_COBJECT 1 +#define HAVE_RB_CPROC 1 +#define HAVE_RB_CMETHOD 1 +#define HAVE_RB_CRANGE 1 +#define HAVE_RB_CREGEXP 1 +#define HAVE_RB_CSTRING 1 +#define HAVE_RB_CSTRUCT 1 +#define HAVE_RB_CSYMBOL 1 +#define HAVE_RB_CTIME 1 +#define HAVE_RB_CTHREAD 1 +#define HAVE_RB_CTRUECLASS 1 +#define HAVE_RB_CNUMERATOR 1 +#define HAVE_RB_EARGERROR 1 +#define HAVE_RB_EEOFERROR 1 +#define HAVE_RB_EEXCEPTION 1 +#define HAVE_RB_EFLOATDOMAINERROR 1 +#define HAVE_RB_EINDEXERROR 1 +#define HAVE_RB_EINTERRUPT 1 +#define HAVE_RB_EIOERROR 1 +#define HAVE_RB_ELOADERROR 1 +#define HAVE_RB_ELOCALJUMPERROR 1 +#define HAVE_RB_EMATHDOMAINERROR 1 +#define HAVE_RB_ENAMEERROR 1 +#define HAVE_RB_ENOMEMERROR 1 +#define HAVE_RB_ENOMETHODERROR 1 +#define HAVE_RB_ENOTIMPERROR 1 +#define HAVE_RB_ERANGEERROR 1 +#define HAVE_RB_EREGEXPERROR 1 +#define HAVE_RB_ERUNTIMEERROR 1 +#define HAVE_RB_ESCRIPTERROR 1 +#define HAVE_RB_ESECURITYERROR 1 +#define HAVE_RB_ESIGNAL 1 +#define HAVE_RB_ESTANDARDERROR 1 +#define HAVE_RB_ESYNTAXERROR 1 +#define HAVE_RB_ESYSSTACKERROR 1 +#define HAVE_RB_ESYSTEMCALLERROR 1 +#define HAVE_RB_ESYSTEMEXIT 1 +#define HAVE_RB_ETHREADERROR 1 +#define HAVE_RB_ETYPEERROR 1 +#define HAVE_RB_EZERODIVERROR 1 +#define HAVE_RB_MCOMPARABLE 1 +#define HAVE_RB_MENUMERABLE 1 +#define HAVE_RB_MERRNO 1 +#define HAVE_RB_MKERNEL 1 +#define HAVE_RB_CDIR 1 + +/* Data */ +#define HAVE_DATA_WRAP_STRUCT 1 +#define HAVE_RDATA 1 + +#define HAVE_TYPEDDATA_WRAP_STRUCT 1 +#define HAVE_RTYPEDDATA + +/* Encoding */ +#define HAVE_ENCODING_GET 1 +#define HAVE_ENCODING_SET 1 +#define HAVE_ENC_CODERANGE_ASCIIONLY 1 + +#define HAVE_RB_ASCII8BIT_ENCODING 1 +#define HAVE_RB_ASCII8BIT_ENCINDEX 1 +#define HAVE_RB_USASCII_ENCODING 1 +#define HAVE_RB_USASCII_ENCINDEX 1 +#define HAVE_RB_UTF8_ENCODING 1 +#define HAVE_RB_UTF8_ENCINDEX 1 +#define HAVE_RB_LOCALE_ENCODING 1 +#define HAVE_RB_LOCALE_ENCINDEX 1 +#define HAVE_RB_FILESYSTEM_ENCODING 1 +#define HAVE_RB_FILESYSTEM_ENCINDEX 1 + +#define HAVE_RB_DEFAULT_INTERNAL_ENCODING 1 +#define HAVE_RB_DEFAULT_EXTERNAL_ENCODING 1 + +#define HAVE_RB_ENCDB_ALIAS 1 +#define HAVE_RB_ENC_ASSOCIATE 1 +#define HAVE_RB_ENC_ASSOCIATE_INDEX 1 +#define HAVE_RB_ENC_CODEPOINT_LEN 1 +#define HAVE_RB_ENC_COMPATIBLE 1 +#define HAVE_RB_ENC_COPY 1 +#define HAVE_RB_ENC_FIND 1 +#define HAVE_RB_ENC_FIND_INDEX 1 +#define HAVE_RB_ENC_FROM_ENCODING 1 +#define HAVE_RB_ENC_FROM_INDEX 1 +#define HAVE_RB_ENC_GET 1 +#define HAVE_RB_ENC_GET_INDEX 1 +#define HAVE_RB_ENC_SET_INDEX 1 +#define HAVE_RB_ENC_STR_CODERANGE 1 +#define HAVE_RB_ENC_STR_NEW 1 +#define HAVE_RB_ENC_TO_INDEX 1 +#define HAVE_RB_OBJ_ENCODING 1 + +#define HAVE_RB_STR_ENCODE 1 +#define HAVE_RB_STR_NEW_CSTR 1 +#define HAVE_RB_USASCII_STR_NEW 1 +#define HAVE_RB_USASCII_STR_NEW_CSTR 1 +#define HAVE_RB_EXTERNAL_STR_NEW 1 +#define HAVE_RB_EXTERNAL_STR_NEW_CSTR 1 +#define HAVE_RB_EXTERNAL_STR_NEW_WITH_ENC 1 + +#define HAVE_RB_TO_ENCODING 1 +#define HAVE_RB_TO_ENCODING_INDEX 1 +#define HAVE_RB_ENC_NTH 1 + +#define HAVE_RB_EENCCOMPATERROR 1 + +#define HAVE_RB_MWAITREADABLE 1 +#define HAVE_RB_MWAITWRITABLE 1 + +#define HAVE_RSTRING_LENINT 1 +#define HAVE_TIMET2NUM 1 + +#define HAVE_RB_LONG2INT 1 +#define HAVE_RB_INTERN3 1 + +#define HAVE_RB_ITER_BREAK 1 +#define HAVE_RB_SOURCEFILE 1 +#define HAVE_RB_SOURCELINE 1 +#define HAVE_RB_METHOD_BOUNDP 1 + +/* Enumerable */ +#define HAVE_RB_ENUMERATORIZE 1 -#if RUBY_VERSION_SINCE(3, 1, 0) -#define RUBY_VERSION_IS_3_1 +/* Exception */ +#define HAVE_RB_EXC_NEW 1 +#define HAVE_RB_EXC_NEW2 1 +#define HAVE_RB_EXC_NEW3 1 +#define HAVE_RB_EXC_RAISE 1 +#define HAVE_RB_SET_ERRINFO 1 + +/* File */ +#define HAVE_RB_FILE_OPEN 1 +#define HAVE_RB_FILE_OPEN_STR 1 +#define HAVE_FILEPATHVALUE 1 + +/* Float */ +#define HAVE_RB_FLOAT_NEW 1 +#define HAVE_RB_RFLOAT 1 +#define HAVE_RFLOAT_VALUE 1 + +/* Globals */ +#define HAVE_RB_DEFAULT_RS 1 +#define HAVE_RB_DEFINE_HOOKED_VARIABLE 1 +#define HAVE_RB_DEFINE_READONLY_VARIABLE 1 +#define HAVE_RB_DEFINE_VARIABLE 1 +#define HAVE_RB_F_GLOBAL_VARIABLES 1 +#define HAVE_RB_GV_GET 1 +#define HAVE_RB_GV_SET 1 +#define HAVE_RB_RS 1 +#define HAVE_RB_OUTPUT_RS 1 +#define HAVE_RB_OUTPUT_FS 1 +#define HAVE_RB_STDERR 1 +#define HAVE_RB_STDIN 1 +#define HAVE_RB_STDOUT 1 +#define HAVE_RB_DEFOUT 1 + +#define HAVE_RB_LASTLINE_SET 1 +#define HAVE_RB_LASTLINE_GET 1 + +/* Hash */ +#define HAVE_RB_HASH 1 +#define HAVE_RB_HASH2 1 +#define HAVE_RB_HASH_DUP 1 +#define HAVE_RB_HASH_FREEZE 1 +#define HAVE_RB_HASH_AREF 1 +#define HAVE_RB_HASH_ASET 1 +#define HAVE_RB_HASH_CLEAR 1 +#define HAVE_RB_HASH_DELETE 1 +#define HAVE_RB_HASH_DELETE_IF 1 +#define HAVE_RB_HASH_FETCH 1 +#define HAVE_RB_HASH_FOREACH 1 +#define HAVE_RB_HASH_LOOKUP 1 +#define HAVE_RB_HASH_LOOKUP2 1 +#define HAVE_RB_HASH_NEW 1 +#define HAVE_RB_HASH_SET_IFNONE 1 +#define HAVE_RB_HASH_SIZE 1 + +/* Integer */ +#define HAVE_RB_INTEGER_PACK 1 + +/* IO */ +#define HAVE_GET_OPEN_FILE 1 +#define HAVE_RB_IO_ADDSTR 1 +#define HAVE_RB_IO_CHECK_IO 1 +#define HAVE_RB_IO_CHECK_CLOSED 1 +#define HAVE_RB_IO_TAINT_CHECK 1 +#define HAVE_RB_IO_CHECK_READABLE 1 +#define HAVE_RB_IO_CHECK_WRITABLE 1 +#define HAVE_RB_IO_CLOSE 1 +#define HAVE_RB_IO_PRINT 1 +#define HAVE_RB_IO_PRINTF 1 +#define HAVE_RB_IO_PUTS 1 +#define HAVE_RB_IO_WAIT_READABLE 1 +#define HAVE_RB_IO_WAIT_WRITABLE 1 +#define HAVE_RB_IO_WRITE 1 +#define HAVE_RB_IO_BINMODE 1 + +#define HAVE_RB_THREAD_FD_WRITABLE 1 +#define HAVE_RB_THREAD_WAIT_FD 1 + +#define HAVE_RB_MUTEX_NEW 1 +#define HAVE_RB_MUTEX_LOCKED_P 1 +#define HAVE_RB_MUTEX_TRYLOCK 1 +#define HAVE_RB_MUTEX_LOCK 1 +#define HAVE_RB_MUTEX_UNLOCK 1 +#define HAVE_RB_MUTEX_SLEEP 1 +#define HAVE_RB_MUTEX_SYNCHRONIZE 1 + +#define HAVE_RB_FD_FIX_CLOEXEC 1 +#define HAVE_RB_CLOEXEC_OPEN 1 + +/* Kernel */ +#define HAVE_RB_BLOCK_GIVEN_P 1 +#define HAVE_RB_BLOCK_PROC 1 +#define HAVE_RB_BLOCK_CALL 1 +#define HAVE_RB_ENSURE 1 +#define HAVE_RB_EVAL_STRING 1 +#define HAVE_RB_EXEC_RECURSIVE 1 +#define HAVE_RB_F_SPRINTF 1 +#define HAVE_RB_NEED_BLOCK 1 +#define HAVE_RB_RAISE 1 +#define HAVE_RB_RESCUE 1 +#define HAVE_RB_RESCUE2 1 +#define HAVE_RB_SET_END_PROC 1 +#define HAVE_RB_SYS_FAIL 1 +#define HAVE_RB_SYSERR_FAIL 1 +#define HAVE_RB_MAKE_BACKTRACE 1 +#define HAVE_RB_THROW 1 +#define HAVE_RB_CATCH 1 +#define HAVE_RB_THROW_OBJ 1 +#define HAVE_RB_CATCH_OBJ 1 +#define HAVE_RB_WARN 1 +#define HAVE_RB_YIELD 1 +#define HAVE_RB_YIELD_SPLAT 1 +#define HAVE_RB_YIELD_VALUES 1 +#define HAVE_RB_FUNCALL3 1 +#define HAVE_RB_FUNCALL_WITH_BLOCK 1 +#define HAVE_RB_PROTECT 1 + +/* GC */ +#define HAVE_RB_GC_REGISTER_ADDRESS 1 +#define HAVE_RB_GC_ENABLE 1 +#define HAVE_RB_GC_DISABLE 1 +#define HAVE_RB_GC 1 + +/* Marshal */ +#define HAVE_RB_MARSHAL_DUMP 1 +#define HAVE_RB_MARSHAL_LOAD 1 + +/* Module */ +#define HAVE_RB_ALIAS 1 +#define HAVE_RB_CONST_DEFINED 1 +#define HAVE_RB_CONST_DEFINED_AT 1 +#define HAVE_RB_CONST_GET 1 +#define HAVE_RB_CONST_GET_AT 1 +#define HAVE_RB_CONST_GET_FROM 1 +#define HAVE_RB_CONST_SET 1 +#define HAVE_RB_DEFINE_ALIAS 1 +#define HAVE_RB_DEFINE_CLASS 1 +#define HAVE_RB_DEFINE_CLASS_UNDER 1 +#define HAVE_RB_DEFINE_CLASS_ID_UNDER 1 +#define HAVE_RB_DEFINE_CONST 1 +#define HAVE_RB_DEFINE_GLOBAL_CONST 1 +#define HAVE_RB_DEFINE_GLOBAL_FUNCTION 1 +#define HAVE_RB_DEFINE_METHOD 1 +#define HAVE_RB_DEFINE_MODULE_FUNCTION 1 +#define HAVE_RB_DEFINE_MODULE 1 +#define HAVE_RB_DEFINE_MODULE_UNDER 1 +#define HAVE_RB_DEFINE_PRIVATE_METHOD 1 +#define HAVE_RB_DEFINE_PROTECTED_METHOD 1 +#define HAVE_RB_DEFINE_SINGLETON_METHOD 1 +#define HAVE_RB_MOD_ANCESTORS 1 +#define HAVE_RB_UNDEF 1 +#define HAVE_RB_UNDEF_METHOD 1 + +/* Numeric */ +#define HAVE_NUM2CHR 1 +#define HAVE_RB_CMPINT 1 +#define HAVE_RB_INT2INUM 1 +#define HAVE_RB_INTEGER 1 +#define HAVE_RB_LL2INUM 1 +#define HAVE_RB_NUM2DBL 1 +#if SIZEOF_INT < SIZEOF_LONG +#define HAVE_RB_NUM2INT 1 +#define HAVE_RB_NUM2UINT 1 #endif +#define HAVE_RB_NUM2LONG 1 +#define HAVE_RB_INT2NUM 1 +#define HAVE_RB_NUM2ULONG 1 +#define HAVE_RB_NUM_COERCE_BIN 1 +#define HAVE_RB_NUM_COERCE_CMP 1 +#define HAVE_RB_NUM_COERCE_RELOP 1 +#define HAVE_RB_ABSINT_SINGLEBIT_P 1 +#define HAVE_RB_NUM_ZERODIV 1 -#if RUBY_VERSION_SINCE(3, 0, 0) -#define RUBY_VERSION_IS_3_0 +/* Fixnum */ +#if SIZEOF_INT < SIZEOF_LONG +#define HAVE_RB_FIX2UINT 1 +#define HAVE_RB_FIX2INT 1 #endif +/* Object */ +#define HAVE_FL_ABLE 1 +#define HAVE_FL_TEST 1 +#define HAVE_OBJ_TAINT 1 +#define HAVE_OBJ_TAINTED 1 +#define HAVE_OBJ_INFECT 1 +#define HAVE_RB_ANY_TO_S 1 +#define HAVE_RB_ATTR_GET 1 +#define HAVE_RB_OBJ_INSTANCE_VARIABLES 1 +#define HAVE_RB_CHECK_ARRAY_TYPE 1 +#define HAVE_RB_CHECK_CONVERT_TYPE 1 +#define HAVE_RB_CHECK_TO_INTEGER 1 +#define HAVE_RB_CHECK_FROZEN 1 +#define HAVE_RB_CHECK_STRING_TYPE 1 +#define HAVE_RB_CLASS_OF 1 +#define HAVE_RB_CONVERT_TYPE 1 +#define HAVE_RB_EQUAL 1 +#define HAVE_RB_CLASS_INHERITED_P 1 +#define HAVE_RB_EXTEND_OBJECT 1 +#define HAVE_RB_INSPECT 1 +#define HAVE_RB_IVAR_DEFINED 1 +#define HAVE_RB_IVAR_GET 1 +#define HAVE_RB_IVAR_SET 1 +#define HAVE_RB_IV_GET 1 +#define HAVE_RB_IV_SET 1 +#define HAVE_RB_OBJ_ALLOC 1 +#define HAVE_RB_OBJ_CALL_INIT 1 +#define HAVE_RB_OBJ_CLASSNAME 1 +#define HAVE_RB_OBJ_DUP 1 +#define HAVE_RB_OBJ_FREEZE 1 +#define HAVE_RB_OBJ_FROZEN_P 1 +#define HAVE_RB_OBJ_ID 1 +#define HAVE_RB_OBJ_INSTANCE_EVAL 1 +#define HAVE_RB_OBJ_IS_INSTANCE_OF 1 +#define HAVE_RB_OBJ_IS_KIND_OF 1 +#define HAVE_RB_OBJ_TAINT 1 +#define HAVE_RB_OBJ_METHOD 1 +#define HAVE_RB_OBJ_METHOD_ARITY 1 +#define HAVE_RB_REQUIRE 1 +#define HAVE_RB_RESPOND_TO 1 +#define HAVE_RB_OBJ_RESPOND_TO 1 +#define HAVE_RB_SPECIAL_CONST_P 1 +#define HAVE_RB_TO_ID 1 +#define HAVE_RB_TO_INT 1 +#define HAVE_RTEST 1 +#define HAVE_TYPE 1 +#define HAVE_RB_TYPE_P 1 +#define HAVE_BUILTIN_TYPE 1 + +/* Proc */ +#define HAVE_RB_PROC_NEW 1 +#define HAVE_RB_PROC_ARITY 1 +#define HAVE_RB_PROC_CALL 1 + +/* Range */ +#define HAVE_RB_RANGE_NEW 1 +#define HAVE_RB_RANGE_VALUES 1 +#define HAVE_RB_RANGE_BEG_LEN 1 + +/* Rational */ +#define HAVE_RB_RATIONAL 1 +#define HAVE_RB_RATIONAL1 1 +#define HAVE_RB_RATIONAL2 1 +#define HAVE_RB_RATIONAL_NEW 1 +#define HAVE_RB_RATIONAL_NEW1 1 +#define HAVE_RB_RATIONAL_NEW2 1 +#define HAVE_RB_RATIONAL_NUM 1 +#define HAVE_RB_RATIONAL_DEN 1 + +/* Regexp */ +#define HAVE_RB_BACKREF_GET 1 +#define HAVE_RB_REG_MATCH 1 +#define HAVE_RB_REG_NEW 1 +#define HAVE_RB_REG_NTH_MATCH 1 +#define HAVE_RB_REG_OPTIONS 1 +#define HAVE_RB_REG_REGCOMP 1 + +/* st */ +#define HAVE_RB_ST 1 + +/* String */ +#define HAVE_RB_CSTR2INUM 1 +#define HAVE_RB_CSTR_TO_INUM 1 +#define HAVE_RB_STR2INUM 1 +#define HAVE_RB_STR_APPEND 1 +#define HAVE_RB_STR_BUF_CAT 1 +#define HAVE_RB_STR_BUF_NEW 1 +#define HAVE_RB_STR_BUF_NEW2 1 +#define HAVE_RB_STR_CAT 1 +#define HAVE_RB_STR_CAT2 1 +#define HAVE_RB_STR_CMP 1 +#define HAVE_RB_STR_DUP 1 +#define HAVE_RB_STR_FLUSH 1 +#define HAVE_RB_STR_FREEZE 1 +#define HAVE_RB_STR_HASH 1 +#define HAVE_RB_STR_UPDATE 1 +#define HAVE_RB_STR_INSPECT 1 +#define HAVE_RB_STR_INTERN 1 +#define HAVE_RB_STR_NEW 1 +#define HAVE_RB_STR_NEW2 1 +#define HAVE_RB_STR_NEW3 1 +#define HAVE_RB_STR_NEW4 1 +#define HAVE_RB_STR_NEW5 1 +#define HAVE_RB_TAINTED_STR_NEW 1 +#define HAVE_RB_TAINTED_STR_NEW2 1 +#define HAVE_RB_STR_PLUS 1 +#define HAVE_RB_STR_TIMES 1 +#define HAVE_RB_STR_RESIZE 1 +#define HAVE_RB_STR_SET_LEN 1 +#define HAVE_RB_STR_SPLIT 1 +#define HAVE_RB_STR_SUBSTR 1 +#define HAVE_RB_STR_TO_STR 1 +#define HAVE_RSTRING_LEN 1 +#define HAVE_RSTRING_PTR 1 +#define HAVE_STRINGVALUE 1 + +#define HAVE_RB_STR_FREE 1 +#define HAVE_RB_SPRINTF 1 +#define HAVE_RB_LOCALE_STR_NEW 1 +#define HAVE_RB_LOCALE_STR_NEW_CSTR 1 +#define HAVE_RB_STR_CONV_ENC 1 +#define HAVE_RB_STR_CONV_ENC_OPTS 1 +#define HAVE_RB_STR_EXPORT 1 +#define HAVE_RB_STR_EXPORT_LOCALE 1 +#define HAVE_RB_STR_LENGTH 1 +#define HAVE_RB_STR_EQUAL 1 +#define HAVE_RB_STR_SUBSEQ 1 +#define HAVE_RB_VSPRINTF 1 +#define HAVE_RB_STRING 1 +#define HAVE_SAFE_STRING_VALUE 1 + +/* Struct */ +#define HAVE_RB_STRUCT_AREF 1 +#define HAVE_RB_STRUCT_ASET 1 +#define HAVE_RB_STRUCT_DEFINE 1 +#define HAVE_RB_STRUCT_DEFINE_UNDER 1 +#define HAVE_RB_STRUCT_NEW 1 +#define HAVE_RB_STRUCT_GETMEMBER 1 +#define HAVE_RB_STRUCT_S_MEMBERS 1 +#define HAVE_RB_STRUCT_MEMBERS 1 +#ifdef RUBY_VERSION_IS_2_4 +#define HAVE_RB_STRUCT_SIZE 1 +#endif + +/* Symbol */ +#define HAVE_RB_ID2NAME 1 +#define HAVE_RB_ID2STR 1 +#define HAVE_RB_INTERN_STR 1 +#define HAVE_RB_INTERN 1 +#define HAVE_RB_IS_CLASS_ID 1 +#define HAVE_RB_IS_CONST_ID 1 +#define HAVE_RB_IS_INSTANCE_ID 1 +#define HAVE_RB_SYM2STR 1 + +/* Thread */ +#define HAVE_RB_THREAD_ALONE 1 +#define HAVE_RB_THREAD_CALL_WITHOUT_GVL 1 +#define HAVE_RB_THREAD_CURRENT 1 +#define HAVE_RB_THREAD_LOCAL_AREF 1 +#define HAVE_RB_THREAD_LOCAL_ASET 1 +#define HAVE_RB_THREAD_WAIT_FOR 1 +#define HAVE_RB_THREAD_WAKEUP 1 +#define HAVE_RB_THREAD_CREATE 1 + +/* Time */ +#define HAVE_RB_TIME_NEW 1 +#define HAVE_RB_TIME_NANO_NEW 1 +#define HAVE_RB_TIME_NUM_NEW 1 +#define HAVE_RB_TIME_INTERVAL 1 +#define HAVE_RB_TIME_TIMEVAL 1 +#define HAVE_RB_TIME_TIMESPEC 1 +#ifdef RUBY_VERSION_IS_2_3 +#define HAVE_RB_TIMESPEC_NOW 1 +#define HAVE_RB_TIME_TIMESPEC_NEW 1 +#endif + +/* Util */ +#define HAVE_RB_SCAN_ARGS 1 + #endif diff --git a/spec/ruby/optional/capi/ext/st_spec.c b/spec/ruby/optional/capi/ext/st_spec.c index 0fb5b5dc2d..4e59698d77 100644 --- a/spec/ruby/optional/capi/ext/st_spec.c +++ b/spec/ruby/optional/capi/ext/st_spec.c @@ -4,12 +4,15 @@ #include <string.h> #include <stdarg.h> +#ifdef HAVE_RB_ST #include <ruby/st.h> +#endif #ifdef __cplusplus extern "C" { #endif +#ifdef HAVE_RB_ST #if SIZEOF_LONG == SIZEOF_VOIDP # define ST2NUM(x) ULONG2NUM(x) @@ -69,13 +72,20 @@ VALUE st_spec_st_lookup(VALUE self) { #endif } +#endif + void Init_st_spec(void) { - VALUE cls = rb_define_class("CApiStSpecs", rb_cObject); + VALUE cls; + cls = rb_define_class("CApiStSpecs", rb_cObject); + +#ifdef HAVE_RB_ST rb_define_method(cls, "st_init_numtable", st_spec_st_init_numtable, 0); rb_define_method(cls, "st_init_numtable_with_size", st_spec_st_init_numtable_with_size, 0); rb_define_method(cls, "st_insert", st_spec_st_insert, 0); rb_define_method(cls, "st_foreach", st_spec_st_foreach, 0); rb_define_method(cls, "st_lookup", st_spec_st_lookup, 0); +#endif + } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/string_spec.c b/spec/ruby/optional/capi/ext/string_spec.c index a140c86347..929d69f9e5 100644 --- a/spec/ruby/optional/capi/ext/string_spec.c +++ b/spec/ruby/optional/capi/ext/string_spec.c @@ -1,44 +1,45 @@ #include "ruby.h" #include "rubyspec.h" -#include <fcntl.h> #include <string.h> #include <stdarg.h> -#include <errno.h> +#ifdef HAVE_RUBY_ENCODING_H #include "ruby/encoding.h" +#endif #ifdef __cplusplus extern "C" { #endif -/* Make sure the RSTRING_PTR and the bytes are in native memory. - * On TruffleRuby RSTRING_PTR and the bytes remain in managed memory - * until they must be written to native memory. - * In some specs we want to test using the native memory. */ -#ifndef NATIVE_RSTRING_PTR -#define NATIVE_RSTRING_PTR(str) RSTRING_PTR(str) -#endif - +#ifdef HAVE_RB_CSTR2INUM VALUE string_spec_rb_cstr2inum(VALUE self, VALUE str, VALUE inum) { int num = FIX2INT(inum); return rb_cstr2inum(RSTRING_PTR(str), num); } +#endif +#ifdef HAVE_RB_CSTR_TO_INUM static VALUE string_spec_rb_cstr_to_inum(VALUE self, VALUE str, VALUE inum, VALUE badcheck) { int num = FIX2INT(inum); return rb_cstr_to_inum(RSTRING_PTR(str), num, RTEST(badcheck)); } +#endif +#ifdef HAVE_RB_STR2INUM VALUE string_spec_rb_str2inum(VALUE self, VALUE str, VALUE inum) { int num = FIX2INT(inum); return rb_str2inum(str, num); } +#endif +#ifdef HAVE_RB_STR_APPEND VALUE string_spec_rb_str_append(VALUE self, VALUE str, VALUE str2) { return rb_str_append(str, str2); } +#endif +#ifdef HAVE_RB_STR_SET_LEN VALUE string_spec_rb_str_set_len(VALUE self, VALUE str, VALUE len) { rb_str_set_len(str, NUM2LONG(len)); @@ -50,84 +51,62 @@ VALUE string_spec_rb_str_set_len_RSTRING_LEN(VALUE self, VALUE str, VALUE len) { return INT2FIX(RSTRING_LEN(str)); } +#endif +#ifdef HAVE_RB_STR_BUF_NEW VALUE string_spec_rb_str_buf_new(VALUE self, VALUE len, VALUE str) { VALUE buf; buf = rb_str_buf_new(NUM2LONG(len)); - if (RTEST(str)) { + if(RTEST(str)) { snprintf(RSTRING_PTR(buf), NUM2LONG(len), "%s", RSTRING_PTR(str)); } return buf; } +#endif -VALUE string_spec_rb_str_capacity(VALUE self, VALUE str) { - return SIZET2NUM(rb_str_capacity(str)); -} - +#ifdef HAVE_RB_STR_BUF_NEW2 VALUE string_spec_rb_str_buf_new2(VALUE self) { return rb_str_buf_new2("hello\0invisible"); } +#endif -VALUE string_spec_rb_str_tmp_new(VALUE self, VALUE len) { - VALUE str = rb_str_tmp_new(NUM2LONG(len)); - rb_obj_reveal(str, rb_cString); - return str; -} - -VALUE string_spec_rb_str_tmp_new_klass(VALUE self, VALUE len) { - return RBASIC_CLASS(rb_str_tmp_new(NUM2LONG(len))); -} - -VALUE string_spec_rb_str_buf_append(VALUE self, VALUE str, VALUE two) { - return rb_str_buf_append(str, two); -} - +#ifdef HAVE_RB_STR_BUF_CAT VALUE string_spec_rb_str_buf_cat(VALUE self, VALUE str) { const char *question_mark = "?"; rb_str_buf_cat(str, question_mark, strlen(question_mark)); return str; } +#endif -VALUE string_spec_rb_enc_str_buf_cat(VALUE self, VALUE str, VALUE other, VALUE encoding) { - char *cstr = StringValueCStr(other); - rb_encoding* enc = rb_to_encoding(encoding); - return rb_enc_str_buf_cat(str, cstr, strlen(cstr), enc); -} - +#ifdef HAVE_RB_STR_CAT VALUE string_spec_rb_str_cat(VALUE self, VALUE str) { return rb_str_cat(str, "?", 1); } +#endif +#ifdef HAVE_RB_STR_CAT2 VALUE string_spec_rb_str_cat2(VALUE self, VALUE str) { return rb_str_cat2(str, "?"); } +#endif -VALUE string_spec_rb_str_cat_cstr(VALUE self, VALUE str, VALUE other) { - return rb_str_cat_cstr(str, StringValueCStr(other)); -} - -VALUE string_spec_rb_str_cat_cstr_constant(VALUE self, VALUE str) { - return rb_str_cat_cstr(str, "?"); -} - +#ifdef HAVE_RB_STR_CMP VALUE string_spec_rb_str_cmp(VALUE self, VALUE str1, VALUE str2) { return INT2NUM(rb_str_cmp(str1, str2)); } +#endif -VALUE string_spec_rb_str_strlen(VALUE self, VALUE str) { - return LONG2NUM(rb_str_strlen(str)); -} - +#ifdef HAVE_RB_STR_CONV_ENC VALUE string_spec_rb_str_conv_enc(VALUE self, VALUE str, VALUE from, VALUE to) { rb_encoding* from_enc; rb_encoding* to_enc; from_enc = rb_to_encoding(from); - if (NIL_P(to)) { + if(NIL_P(to)) { to_enc = 0; } else { to_enc = rb_to_encoding(to); @@ -135,15 +114,18 @@ VALUE string_spec_rb_str_conv_enc(VALUE self, VALUE str, VALUE from, VALUE to) { return rb_str_conv_enc(str, from_enc, to_enc); } +#endif +#ifdef HAVE_RB_STR_CONV_ENC_OPTS VALUE string_spec_rb_str_conv_enc_opts(VALUE self, VALUE str, VALUE from, VALUE to, - VALUE ecflags, VALUE ecopts) { + VALUE ecflags, VALUE ecopts) +{ rb_encoding* from_enc; rb_encoding* to_enc; from_enc = rb_to_encoding(from); - if (NIL_P(to)) { + if(NIL_P(to)) { to_enc = 0; } else { to_enc = rb_to_encoding(to); @@ -151,130 +133,159 @@ VALUE string_spec_rb_str_conv_enc_opts(VALUE self, VALUE str, VALUE from, VALUE return rb_str_conv_enc_opts(str, from_enc, to_enc, FIX2INT(ecflags), ecopts); } +#endif -VALUE string_spec_rb_str_drop_bytes(VALUE self, VALUE str, VALUE len) { - return rb_str_drop_bytes(str, NUM2LONG(len)); -} - +#ifdef HAVE_RB_STR_EXPORT VALUE string_spec_rb_str_export(VALUE self, VALUE str) { return rb_str_export(str); } +#endif +#ifdef HAVE_RB_STR_EXPORT_LOCALE VALUE string_spec_rb_str_export_locale(VALUE self, VALUE str) { return rb_str_export_locale(str); } +#endif +#ifdef HAVE_RB_STR_DUP VALUE string_spec_rb_str_dup(VALUE self, VALUE str) { return rb_str_dup(str); } +#endif +#ifdef HAVE_RB_STR_FREEZE VALUE string_spec_rb_str_freeze(VALUE self, VALUE str) { return rb_str_freeze(str); } +#endif +#ifdef HAVE_RB_STR_INSPECT VALUE string_spec_rb_str_inspect(VALUE self, VALUE str) { return rb_str_inspect(str); } +#endif +#ifdef HAVE_RB_STR_INTERN VALUE string_spec_rb_str_intern(VALUE self, VALUE str) { return rb_str_intern(str); } +#endif +#ifdef HAVE_RB_STR_LENGTH VALUE string_spec_rb_str_length(VALUE self, VALUE str) { return rb_str_length(str); } +#endif +#ifdef HAVE_RB_STR_NEW VALUE string_spec_rb_str_new(VALUE self, VALUE str, VALUE len) { return rb_str_new(RSTRING_PTR(str), FIX2INT(len)); } -VALUE string_spec_rb_str_new_native(VALUE self, VALUE str, VALUE len) { - return rb_str_new(NATIVE_RSTRING_PTR(str), FIX2INT(len)); -} - VALUE string_spec_rb_str_new_offset(VALUE self, VALUE str, VALUE offset, VALUE len) { return rb_str_new(RSTRING_PTR(str) + FIX2INT(offset), FIX2INT(len)); } +#endif +#ifdef HAVE_RB_STR_NEW2 VALUE string_spec_rb_str_new2(VALUE self, VALUE str) { - if (NIL_P(str)) { + if(NIL_P(str)) { return rb_str_new2(""); } else { return rb_str_new2(RSTRING_PTR(str)); } } +#endif +#ifdef HAVE_RB_STR_ENCODE VALUE string_spec_rb_str_encode(VALUE self, VALUE str, VALUE enc, VALUE flags, VALUE opts) { return rb_str_encode(str, enc, FIX2INT(flags), opts); } +#endif -VALUE string_spec_rb_str_export_to_enc(VALUE self, VALUE str, VALUE enc) { - return rb_str_export_to_enc(str, rb_to_encoding(enc)); -} - +#ifdef HAVE_RB_STR_NEW_CSTR VALUE string_spec_rb_str_new_cstr(VALUE self, VALUE str) { - if (NIL_P(str)) { + if(NIL_P(str)) { return rb_str_new_cstr(""); } else { return rb_str_new_cstr(RSTRING_PTR(str)); } } +#endif +#ifdef HAVE_RB_EXTERNAL_STR_NEW VALUE string_spec_rb_external_str_new(VALUE self, VALUE str) { return rb_external_str_new(RSTRING_PTR(str), RSTRING_LEN(str)); } +#endif +#ifdef HAVE_RB_EXTERNAL_STR_NEW_CSTR VALUE string_spec_rb_external_str_new_cstr(VALUE self, VALUE str) { return rb_external_str_new_cstr(RSTRING_PTR(str)); } +#endif +#ifdef HAVE_RB_EXTERNAL_STR_NEW_WITH_ENC VALUE string_spec_rb_external_str_new_with_enc(VALUE self, VALUE str, VALUE len, VALUE encoding) { return rb_external_str_new_with_enc(RSTRING_PTR(str), FIX2LONG(len), rb_to_encoding(encoding)); } +#endif +#ifdef HAVE_RB_LOCALE_STR_NEW VALUE string_spec_rb_locale_str_new(VALUE self, VALUE str, VALUE len) { return rb_locale_str_new(RSTRING_PTR(str), FIX2INT(len)); } +#endif +#ifdef HAVE_RB_LOCALE_STR_NEW_CSTR VALUE string_spec_rb_locale_str_new_cstr(VALUE self, VALUE str) { return rb_locale_str_new_cstr(RSTRING_PTR(str)); } +#endif +#ifdef HAVE_RB_STR_NEW3 VALUE string_spec_rb_str_new3(VALUE self, VALUE str) { return rb_str_new3(str); } +#endif +#ifdef HAVE_RB_STR_NEW4 VALUE string_spec_rb_str_new4(VALUE self, VALUE str) { return rb_str_new4(str); } +#endif +#ifdef HAVE_RB_STR_NEW5 VALUE string_spec_rb_str_new5(VALUE self, VALUE str, VALUE ptr, VALUE len) { return rb_str_new5(str, RSTRING_PTR(ptr), FIX2INT(len)); } +#endif -#ifndef RUBY_VERSION_IS_3_2 +#ifdef HAVE_RB_TAINTED_STR_NEW VALUE string_spec_rb_tainted_str_new(VALUE self, VALUE str, VALUE len) { return rb_tainted_str_new(RSTRING_PTR(str), FIX2INT(len)); } +#endif +#ifdef HAVE_RB_TAINTED_STR_NEW2 VALUE string_spec_rb_tainted_str_new2(VALUE self, VALUE str) { return rb_tainted_str_new2(RSTRING_PTR(str)); } #endif +#ifdef HAVE_RB_STR_PLUS VALUE string_spec_rb_str_plus(VALUE self, VALUE str1, VALUE str2) { return rb_str_plus(str1, str2); } +#endif +#ifdef HAVE_RB_STR_TIMES VALUE string_spec_rb_str_times(VALUE self, VALUE str, VALUE times) { return rb_str_times(str, times); } +#endif -VALUE string_spec_rb_str_modify_expand(VALUE self, VALUE str, VALUE size) { - rb_str_modify_expand(str, FIX2LONG(size)); - return str; -} - +#ifdef HAVE_RB_STR_RESIZE VALUE string_spec_rb_str_resize(VALUE self, VALUE str, VALUE size) { return rb_str_resize(str, FIX2INT(size)); } @@ -283,41 +294,45 @@ VALUE string_spec_rb_str_resize_RSTRING_LEN(VALUE self, VALUE str, VALUE size) { VALUE modified = rb_str_resize(str, FIX2INT(size)); return INT2FIX(RSTRING_LEN(modified)); } +#endif -VALUE string_spec_rb_str_resize_copy(VALUE self, VALUE str) { - rb_str_modify_expand(str, 5); - char *buffer = RSTRING_PTR(str); - buffer[1] = 'e'; - buffer[2] = 's'; - buffer[3] = 't'; - rb_str_resize(str, 4); - return str; -} - +#ifdef HAVE_RB_STR_SPLIT VALUE string_spec_rb_str_split(VALUE self, VALUE str) { return rb_str_split(str, ","); } +#endif +#ifdef HAVE_RB_STR_SUBSEQ VALUE string_spec_rb_str_subseq(VALUE self, VALUE str, VALUE beg, VALUE len) { return rb_str_subseq(str, FIX2INT(beg), FIX2INT(len)); } +#endif +#ifdef HAVE_RB_STR_SUBSTR VALUE string_spec_rb_str_substr(VALUE self, VALUE str, VALUE beg, VALUE len) { return rb_str_substr(str, FIX2INT(beg), FIX2INT(len)); } +#endif +#ifdef HAVE_RB_STR_TO_STR VALUE string_spec_rb_str_to_str(VALUE self, VALUE arg) { return rb_str_to_str(arg); } +#endif +#ifdef HAVE_RSTRING_LEN VALUE string_spec_RSTRING_LEN(VALUE self, VALUE str) { return INT2FIX(RSTRING_LEN(str)); } +#endif +#ifdef HAVE_RSTRING_LENINT VALUE string_spec_RSTRING_LENINT(VALUE self, VALUE str) { return INT2FIX(RSTRING_LENINT(str)); } +#endif +#ifdef HAVE_RSTRING_PTR VALUE string_spec_RSTRING_PTR_iterate(VALUE self, VALUE str) { int i; char* ptr; @@ -329,25 +344,6 @@ VALUE string_spec_RSTRING_PTR_iterate(VALUE self, VALUE str) { return Qnil; } -VALUE string_spec_RSTRING_PTR_iterate_uint32(VALUE self, VALUE str) { - uint32_t* ptr; - long i, l = RSTRING_LEN(str) / sizeof(uint32_t); - - ptr = (uint32_t *)RSTRING_PTR(str); - for(i = 0; i < l; i++) { - rb_yield(UINT2NUM(ptr[i])); - } - return Qnil; -} - -VALUE string_spec_RSTRING_PTR_short_memcpy(VALUE self, VALUE str) { - /* Short memcpy operations may be optimised by the compiler to a single write. */ - if (RSTRING_LEN(str) >= 8) { - memcpy(RSTRING_PTR(str), "Infinity", 8); - } - return str; -} - VALUE string_spec_RSTRING_PTR_assign(VALUE self, VALUE str, VALUE chr) { int i; char c; @@ -362,133 +358,65 @@ VALUE string_spec_RSTRING_PTR_assign(VALUE self, VALUE str, VALUE chr) { return Qnil; } -VALUE string_spec_RSTRING_PTR_set(VALUE self, VALUE str, VALUE i, VALUE chr) { - RSTRING_PTR(str)[FIX2INT(i)] = (char) FIX2INT(chr); - return str; -} - VALUE string_spec_RSTRING_PTR_after_funcall(VALUE self, VALUE str, VALUE cb) { /* Silence gcc 4.3.2 warning about computed value not used */ - if (RSTRING_PTR(str)) { /* force it out */ + if(RSTRING_PTR(str)) { /* force it out */ rb_funcall(cb, rb_intern("call"), 1, str); } return rb_str_new2(RSTRING_PTR(str)); } +#endif -VALUE string_spec_RSTRING_PTR_after_yield(VALUE self, VALUE str) { - char* ptr = NATIVE_RSTRING_PTR(str); - long len = RSTRING_LEN(str); - VALUE from_rstring_ptr; - - ptr[0] = '1'; - rb_yield(str); - ptr[2] = '2'; - - from_rstring_ptr = rb_str_new(ptr, len); - return from_rstring_ptr; -} - -VALUE string_spec_RSTRING_PTR_read(VALUE self, VALUE str, VALUE path) { - char *cpath = StringValueCStr(path); - int fd = open(cpath, O_RDONLY); - VALUE capacities = rb_ary_new(); - if (fd < 0) { - rb_syserr_fail(errno, "open"); - } - - rb_str_modify_expand(str, 30); - rb_ary_push(capacities, SIZET2NUM(rb_str_capacity(str))); - char *buffer = RSTRING_PTR(str); - if (read(fd, buffer, 30) < 0) { - rb_syserr_fail(errno, "read"); - } - - rb_str_modify_expand(str, 53); - rb_ary_push(capacities, SIZET2NUM(rb_str_capacity(str))); - char *buffer2 = RSTRING_PTR(str); - if (read(fd, buffer2 + 30, 53 - 30) < 0) { - rb_syserr_fail(errno, "read"); - } - - rb_str_set_len(str, 53); - close(fd); - return capacities; -} - -VALUE string_spec_RSTRING_PTR_null_terminate(VALUE self, VALUE str, VALUE min_length) { - char* ptr = RSTRING_PTR(str); - char* end = ptr + RSTRING_LEN(str); - return rb_str_new(end, FIX2LONG(min_length)); -} - +#ifdef HAVE_STRINGVALUE VALUE string_spec_StringValue(VALUE self, VALUE str) { return StringValue(str); } +#endif +#ifdef HAVE_SAFE_STRING_VALUE static VALUE string_spec_SafeStringValue(VALUE self, VALUE str) { SafeStringValue(str); return str; } +#endif +#ifdef HAVE_RB_STR_HASH static VALUE string_spec_rb_str_hash(VALUE self, VALUE str) { st_index_t val = rb_str_hash(str); - return ST2FIX(val); +#if SIZEOF_LONG == SIZEOF_VOIDP || SIZEOF_LONG_LONG == SIZEOF_VOIDP + return LONG2FIX((long)val); +#else +# error unsupported platform +#endif } +#endif +#ifdef HAVE_RB_STR_UPDATE static VALUE string_spec_rb_str_update(VALUE self, VALUE str, VALUE beg, VALUE end, VALUE replacement) { rb_str_update(str, FIX2LONG(beg), FIX2LONG(end), replacement); return str; } +#endif +#ifdef HAVE_RB_STR_FREE static VALUE string_spec_rb_str_free(VALUE self, VALUE str) { rb_str_free(str); return Qnil; } +#endif +#ifdef HAVE_RB_SPRINTF static VALUE string_spec_rb_sprintf1(VALUE self, VALUE str, VALUE repl) { return rb_sprintf(RSTRING_PTR(str), RSTRING_PTR(repl)); } static VALUE string_spec_rb_sprintf2(VALUE self, VALUE str, VALUE repl1, VALUE repl2) { return rb_sprintf(RSTRING_PTR(str), RSTRING_PTR(repl1), RSTRING_PTR(repl2)); } +#endif -static VALUE string_spec_rb_sprintf3(VALUE self, VALUE str) { - return rb_sprintf("Result: %" PRIsVALUE ".", str); -} - -static VALUE string_spec_rb_sprintf4(VALUE self, VALUE str) { - return rb_sprintf("Result: %+" PRIsVALUE ".", str); -} - -static VALUE string_spec_rb_sprintf5(VALUE self, VALUE width, VALUE precision, VALUE str) { - return rb_sprintf("Result: %*.*s.", FIX2INT(width), FIX2INT(precision), RSTRING_PTR(str)); -} - -static VALUE string_spec_rb_sprintf6(VALUE self, VALUE width, VALUE precision, VALUE str) { - return rb_sprintf("Result: %*.*" PRIsVALUE ".", FIX2INT(width), FIX2INT(precision), str); -} - -static VALUE string_spec_rb_sprintf7(VALUE self, VALUE str, VALUE obj) { - VALUE results = rb_ary_new(); - rb_ary_push(results, rb_sprintf(RSTRING_PTR(str), obj)); - char cstr[256]; - int len = snprintf(cstr, 256, RSTRING_PTR(str), obj); - rb_ary_push(results, rb_str_new(cstr, len)); - return results; -} - -static VALUE string_spec_rb_sprintf8(VALUE self, VALUE str, VALUE num) { - VALUE results = rb_ary_new(); - rb_ary_push(results, rb_sprintf(RSTRING_PTR(str), FIX2LONG(num))); - char cstr[256]; - int len = snprintf(cstr, 256, RSTRING_PTR(str), FIX2LONG(num)); - rb_ary_push(results, rb_str_new(cstr, len)); - return results; -} - -PRINTF_ARGS(static VALUE string_spec_rb_vsprintf_worker(char* fmt, ...), 1, 2); +#ifdef HAVE_RB_VSPRINTF static VALUE string_spec_rb_vsprintf_worker(char* fmt, ...) { va_list varargs; VALUE str; @@ -504,196 +432,265 @@ static VALUE string_spec_rb_vsprintf(VALUE self, VALUE fmt, VALUE str, VALUE i, return string_spec_rb_vsprintf_worker(RSTRING_PTR(fmt), RSTRING_PTR(str), FIX2INT(i), RFLOAT_VALUE(f)); } +#endif +#ifdef HAVE_RB_STR_EQUAL VALUE string_spec_rb_str_equal(VALUE self, VALUE str1, VALUE str2) { return rb_str_equal(str1, str2); } +#endif +#ifdef HAVE_RB_USASCII_STR_NEW static VALUE string_spec_rb_usascii_str_new(VALUE self, VALUE str, VALUE len) { return rb_usascii_str_new(RSTRING_PTR(str), NUM2INT(len)); } +#endif -static VALUE string_spec_rb_usascii_str_new_lit(VALUE self) { - return rb_usascii_str_new_lit("nokogiri"); -} - -static VALUE string_spec_rb_usascii_str_new_lit_non_ascii(VALUE self) { - return rb_usascii_str_new_lit("r\xc3\xa9sum\xc3\xa9"); -} - +#ifdef HAVE_RB_USASCII_STR_NEW_CSTR static VALUE string_spec_rb_usascii_str_new_cstr(VALUE self, VALUE str) { return rb_usascii_str_new_cstr(RSTRING_PTR(str)); } +#endif +#ifdef HAVE_RB_STRING static VALUE string_spec_rb_String(VALUE self, VALUE val) { return rb_String(val); } - -static VALUE string_spec_rb_string_value_cstr(VALUE self, VALUE str) { - char *c_str = rb_string_value_cstr(&str); - return c_str ? Qtrue : Qfalse; -} - -static VALUE string_spec_rb_str_modify(VALUE self, VALUE str) { - rb_str_modify(str); - return str; -} - -static VALUE string_spec_rb_utf8_str_new_static(VALUE self) { - return rb_utf8_str_new_static("nokogiri", 8); -} - -static VALUE string_spec_rb_utf8_str_new(VALUE self) { - return rb_utf8_str_new("nokogiri", 8); -} - -static VALUE string_spec_rb_utf8_str_new_cstr(VALUE self) { - return rb_utf8_str_new_cstr("nokogiri"); -} - -PRINTF_ARGS(static VALUE call_rb_str_vcatf(VALUE mesg, const char *fmt, ...), 2, 3); -static VALUE call_rb_str_vcatf(VALUE mesg, const char *fmt, ...) { - va_list ap; - va_start(ap, fmt); - VALUE result = rb_str_vcatf(mesg, fmt, ap); - va_end(ap); - return result; -} - -static VALUE string_spec_rb_str_vcatf(VALUE self, VALUE mesg) { - return call_rb_str_vcatf(mesg, "fmt %d %d number", 42, 7); -} - -static VALUE string_spec_rb_str_catf(VALUE self, VALUE mesg) { - return rb_str_catf(mesg, "fmt %d %d number", 41, 6); -} - -static VALUE string_spec_rb_str_locktmp(VALUE self, VALUE str) { - return rb_str_locktmp(str); -} - -static VALUE string_spec_rb_str_unlocktmp(VALUE self, VALUE str) { - return rb_str_unlocktmp(str); -} - -static VALUE string_spec_rb_enc_interned_str_cstr(VALUE self, VALUE str, VALUE enc) { - rb_encoding *e = NIL_P(enc) ? 0 : rb_to_encoding(enc); - return rb_enc_interned_str_cstr(RSTRING_PTR(str), e); -} - -static VALUE string_spec_rb_enc_interned_str(VALUE self, VALUE str, VALUE len, VALUE enc) { - rb_encoding *e = NIL_P(enc) ? 0 : rb_to_encoding(enc); - return rb_enc_interned_str(RSTRING_PTR(str), FIX2LONG(len), e); -} - -static VALUE string_spec_rb_str_to_interned_str(VALUE self, VALUE str) { - return rb_str_to_interned_str(str); -} +#endif void Init_string_spec(void) { - VALUE cls = rb_define_class("CApiStringSpecs", rb_cObject); + VALUE cls; + cls = rb_define_class("CApiStringSpecs", rb_cObject); + +#ifdef HAVE_RB_CSTR2INUM rb_define_method(cls, "rb_cstr2inum", string_spec_rb_cstr2inum, 2); +#endif + +#ifdef HAVE_RB_CSTR_TO_INUM rb_define_method(cls, "rb_cstr_to_inum", string_spec_rb_cstr_to_inum, 3); +#endif + +#ifdef HAVE_RB_STR2INUM rb_define_method(cls, "rb_str2inum", string_spec_rb_str2inum, 2); +#endif + +#ifdef HAVE_RB_STR_APPEND rb_define_method(cls, "rb_str_append", string_spec_rb_str_append, 2); +#endif + +#ifdef HAVE_RB_STR_BUF_NEW rb_define_method(cls, "rb_str_buf_new", string_spec_rb_str_buf_new, 2); - rb_define_method(cls, "rb_str_capacity", string_spec_rb_str_capacity, 1); +#endif + +#ifdef HAVE_RB_STR_BUF_NEW2 rb_define_method(cls, "rb_str_buf_new2", string_spec_rb_str_buf_new2, 0); - rb_define_method(cls, "rb_str_tmp_new", string_spec_rb_str_tmp_new, 1); - rb_define_method(cls, "rb_str_tmp_new_klass", string_spec_rb_str_tmp_new_klass, 1); - rb_define_method(cls, "rb_str_buf_append", string_spec_rb_str_buf_append, 2); +#endif + +#ifdef HAVE_RB_STR_BUF_CAT rb_define_method(cls, "rb_str_buf_cat", string_spec_rb_str_buf_cat, 1); - rb_define_method(cls, "rb_enc_str_buf_cat", string_spec_rb_enc_str_buf_cat, 3); +#endif + +#ifdef HAVE_RB_STR_CAT rb_define_method(cls, "rb_str_cat", string_spec_rb_str_cat, 1); +#endif + +#ifdef HAVE_RB_STR_CAT2 rb_define_method(cls, "rb_str_cat2", string_spec_rb_str_cat2, 1); - rb_define_method(cls, "rb_str_cat_cstr", string_spec_rb_str_cat_cstr, 2); - rb_define_method(cls, "rb_str_cat_cstr_constant", string_spec_rb_str_cat_cstr_constant, 1); +#endif + +#ifdef HAVE_RB_STR_CMP rb_define_method(cls, "rb_str_cmp", string_spec_rb_str_cmp, 2); - rb_define_method(cls, "rb_str_strlen", string_spec_rb_str_strlen, 1); +#endif + +#ifdef HAVE_RB_STR_CONV_ENC rb_define_method(cls, "rb_str_conv_enc", string_spec_rb_str_conv_enc, 3); +#endif + +#ifdef HAVE_RB_STR_CONV_ENC_OPTS rb_define_method(cls, "rb_str_conv_enc_opts", string_spec_rb_str_conv_enc_opts, 5); - rb_define_method(cls, "rb_str_drop_bytes", string_spec_rb_str_drop_bytes, 2); +#endif + +#ifdef HAVE_RB_STR_EXPORT rb_define_method(cls, "rb_str_export", string_spec_rb_str_export, 1); +#endif + +#ifdef HAVE_RB_STR_EXPORT_LOCALE rb_define_method(cls, "rb_str_export_locale", string_spec_rb_str_export_locale, 1); +#endif + +#ifdef HAVE_RB_STR_DUP rb_define_method(cls, "rb_str_dup", string_spec_rb_str_dup, 1); +#endif + +#ifdef HAVE_RB_STR_FREEZE rb_define_method(cls, "rb_str_freeze", string_spec_rb_str_freeze, 1); +#endif + +#ifdef HAVE_RB_STR_INSPECT rb_define_method(cls, "rb_str_inspect", string_spec_rb_str_inspect, 1); +#endif + +#ifdef HAVE_RB_STR_INTERN rb_define_method(cls, "rb_str_intern", string_spec_rb_str_intern, 1); +#endif + +#ifdef HAVE_RB_STR_LENGTH rb_define_method(cls, "rb_str_length", string_spec_rb_str_length, 1); +#endif + +#ifdef HAVE_RB_STR_NEW rb_define_method(cls, "rb_str_new", string_spec_rb_str_new, 2); - rb_define_method(cls, "rb_str_new_native", string_spec_rb_str_new_native, 2); rb_define_method(cls, "rb_str_new_offset", string_spec_rb_str_new_offset, 3); +#endif + +#ifdef HAVE_RB_STR_NEW2 rb_define_method(cls, "rb_str_new2", string_spec_rb_str_new2, 1); +#endif + +#ifdef HAVE_RB_STR_ENCODE rb_define_method(cls, "rb_str_encode", string_spec_rb_str_encode, 4); - rb_define_method(cls, "rb_str_export_to_enc", string_spec_rb_str_export_to_enc, 2); +#endif + +#ifdef HAVE_RB_STR_NEW_CSTR rb_define_method(cls, "rb_str_new_cstr", string_spec_rb_str_new_cstr, 1); +#endif + +#ifdef HAVE_RB_EXTERNAL_STR_NEW rb_define_method(cls, "rb_external_str_new", string_spec_rb_external_str_new, 1); - rb_define_method(cls, "rb_external_str_new_cstr", string_spec_rb_external_str_new_cstr, 1); +#endif + +#ifdef HAVE_RB_EXTERNAL_STR_NEW_CSTR + rb_define_method(cls, "rb_external_str_new_cstr", + string_spec_rb_external_str_new_cstr, 1); +#endif + +#ifdef HAVE_RB_EXTERNAL_STR_NEW_WITH_ENC rb_define_method(cls, "rb_external_str_new_with_enc", string_spec_rb_external_str_new_with_enc, 3); +#endif + +#ifdef HAVE_RB_LOCALE_STR_NEW rb_define_method(cls, "rb_locale_str_new", string_spec_rb_locale_str_new, 2); +#endif + +#ifdef HAVE_RB_LOCALE_STR_NEW_CSTR rb_define_method(cls, "rb_locale_str_new_cstr", string_spec_rb_locale_str_new_cstr, 1); +#endif + +#ifdef HAVE_RB_STR_NEW3 rb_define_method(cls, "rb_str_new3", string_spec_rb_str_new3, 1); +#endif + +#ifdef HAVE_RB_STR_NEW4 rb_define_method(cls, "rb_str_new4", string_spec_rb_str_new4, 1); +#endif + +#ifdef HAVE_RB_STR_NEW5 rb_define_method(cls, "rb_str_new5", string_spec_rb_str_new5, 3); -#ifndef RUBY_VERSION_IS_3_2 +#endif + +#ifdef HAVE_RB_TAINTED_STR_NEW rb_define_method(cls, "rb_tainted_str_new", string_spec_rb_tainted_str_new, 2); +#endif + +#ifdef HAVE_RB_TAINTED_STR_NEW2 rb_define_method(cls, "rb_tainted_str_new2", string_spec_rb_tainted_str_new2, 1); #endif + +#ifdef HAVE_RB_STR_PLUS rb_define_method(cls, "rb_str_plus", string_spec_rb_str_plus, 2); +#endif + +#ifdef HAVE_RB_STR_TIMES rb_define_method(cls, "rb_str_times", string_spec_rb_str_times, 2); - rb_define_method(cls, "rb_str_modify_expand", string_spec_rb_str_modify_expand, 2); +#endif + +#ifdef HAVE_RB_STR_RESIZE rb_define_method(cls, "rb_str_resize", string_spec_rb_str_resize, 2); - rb_define_method(cls, "rb_str_resize_RSTRING_LEN", string_spec_rb_str_resize_RSTRING_LEN, 2); - rb_define_method(cls, "rb_str_resize_copy", string_spec_rb_str_resize_copy, 1); + rb_define_method(cls, "rb_str_resize_RSTRING_LEN", + string_spec_rb_str_resize_RSTRING_LEN, 2); +#endif + +#ifdef HAVE_RB_STR_SET_LEN rb_define_method(cls, "rb_str_set_len", string_spec_rb_str_set_len, 2); - rb_define_method(cls, "rb_str_set_len_RSTRING_LEN", string_spec_rb_str_set_len_RSTRING_LEN, 2); + rb_define_method(cls, "rb_str_set_len_RSTRING_LEN", + string_spec_rb_str_set_len_RSTRING_LEN, 2); +#endif + +#ifdef HAVE_RB_STR_SPLIT rb_define_method(cls, "rb_str_split", string_spec_rb_str_split, 1); +#endif + +#ifdef HAVE_RB_STR_SUBSEQ rb_define_method(cls, "rb_str_subseq", string_spec_rb_str_subseq, 3); +#endif + +#ifdef HAVE_RB_STR_SUBSTR rb_define_method(cls, "rb_str_substr", string_spec_rb_str_substr, 3); +#endif + +#ifdef HAVE_RB_STR_TO_STR rb_define_method(cls, "rb_str_to_str", string_spec_rb_str_to_str, 1); +#endif + +#ifdef HAVE_RSTRING_LEN rb_define_method(cls, "RSTRING_LEN", string_spec_RSTRING_LEN, 1); +#endif + +#ifdef HAVE_RSTRING_LENINT rb_define_method(cls, "RSTRING_LENINT", string_spec_RSTRING_LENINT, 1); +#endif + +#ifdef HAVE_RSTRING_PTR rb_define_method(cls, "RSTRING_PTR_iterate", string_spec_RSTRING_PTR_iterate, 1); - rb_define_method(cls, "RSTRING_PTR_iterate_uint32", string_spec_RSTRING_PTR_iterate_uint32, 1); - rb_define_method(cls, "RSTRING_PTR_short_memcpy", string_spec_RSTRING_PTR_short_memcpy, 1); rb_define_method(cls, "RSTRING_PTR_assign", string_spec_RSTRING_PTR_assign, 2); - rb_define_method(cls, "RSTRING_PTR_set", string_spec_RSTRING_PTR_set, 3); - rb_define_method(cls, "RSTRING_PTR_after_funcall", string_spec_RSTRING_PTR_after_funcall, 2); - rb_define_method(cls, "RSTRING_PTR_after_yield", string_spec_RSTRING_PTR_after_yield, 1); - rb_define_method(cls, "RSTRING_PTR_read", string_spec_RSTRING_PTR_read, 2); - rb_define_method(cls, "RSTRING_PTR_null_terminate", string_spec_RSTRING_PTR_null_terminate, 2); + rb_define_method(cls, "RSTRING_PTR_after_funcall", + string_spec_RSTRING_PTR_after_funcall, 2); +#endif + +#ifdef HAVE_STRINGVALUE rb_define_method(cls, "StringValue", string_spec_StringValue, 1); +#endif + +#ifdef HAVE_SAFE_STRING_VALUE rb_define_method(cls, "SafeStringValue", string_spec_SafeStringValue, 1); +#endif + +#ifdef HAVE_RB_STR_HASH rb_define_method(cls, "rb_str_hash", string_spec_rb_str_hash, 1); +#endif + +#ifdef HAVE_RB_STR_UPDATE rb_define_method(cls, "rb_str_update", string_spec_rb_str_update, 4); +#endif + +#ifdef HAVE_RB_STR_FREE rb_define_method(cls, "rb_str_free", string_spec_rb_str_free, 1); +#endif + +#ifdef HAVE_RB_SPRINTF rb_define_method(cls, "rb_sprintf1", string_spec_rb_sprintf1, 2); rb_define_method(cls, "rb_sprintf2", string_spec_rb_sprintf2, 3); - rb_define_method(cls, "rb_sprintf3", string_spec_rb_sprintf3, 1); - rb_define_method(cls, "rb_sprintf4", string_spec_rb_sprintf4, 1); - rb_define_method(cls, "rb_sprintf5", string_spec_rb_sprintf5, 3); - rb_define_method(cls, "rb_sprintf6", string_spec_rb_sprintf6, 3); - rb_define_method(cls, "rb_sprintf7", string_spec_rb_sprintf7, 2); - rb_define_method(cls, "rb_sprintf8", string_spec_rb_sprintf8, 2); +#endif + +#ifdef HAVE_RB_VSPRINTF rb_define_method(cls, "rb_vsprintf", string_spec_rb_vsprintf, 4); +#endif + +#ifdef HAVE_RB_STR_EQUAL rb_define_method(cls, "rb_str_equal", string_spec_rb_str_equal, 2); +#endif + +#ifdef HAVE_RB_USASCII_STR_NEW rb_define_method(cls, "rb_usascii_str_new", string_spec_rb_usascii_str_new, 2); - rb_define_method(cls, "rb_usascii_str_new_lit", string_spec_rb_usascii_str_new_lit, 0); - rb_define_method(cls, "rb_usascii_str_new_lit_non_ascii", string_spec_rb_usascii_str_new_lit_non_ascii, 0); +#endif + +#ifdef HAVE_RB_USASCII_STR_NEW_CSTR rb_define_method(cls, "rb_usascii_str_new_cstr", string_spec_rb_usascii_str_new_cstr, 1); +#endif + +#ifdef HAVE_RB_STRING rb_define_method(cls, "rb_String", string_spec_rb_String, 1); - rb_define_method(cls, "rb_string_value_cstr", string_spec_rb_string_value_cstr, 1); - rb_define_method(cls, "rb_str_modify", string_spec_rb_str_modify, 1); - rb_define_method(cls, "rb_utf8_str_new_static", string_spec_rb_utf8_str_new_static, 0); - rb_define_method(cls, "rb_utf8_str_new", string_spec_rb_utf8_str_new, 0); - rb_define_method(cls, "rb_utf8_str_new_cstr", string_spec_rb_utf8_str_new_cstr, 0); - rb_define_method(cls, "rb_str_vcatf", string_spec_rb_str_vcatf, 1); - rb_define_method(cls, "rb_str_catf", string_spec_rb_str_catf, 1); - rb_define_method(cls, "rb_str_locktmp", string_spec_rb_str_locktmp, 1); - rb_define_method(cls, "rb_str_unlocktmp", string_spec_rb_str_unlocktmp, 1); - rb_define_method(cls, "rb_enc_interned_str_cstr", string_spec_rb_enc_interned_str_cstr, 2); - rb_define_method(cls, "rb_enc_interned_str", string_spec_rb_enc_interned_str, 3); - rb_define_method(cls, "rb_str_to_interned_str", string_spec_rb_str_to_interned_str, 1); +#endif } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/struct_spec.c b/spec/ruby/optional/capi/ext/struct_spec.c index 9c45bd5672..8f373d9f48 100644 --- a/spec/ruby/optional/capi/ext/struct_spec.c +++ b/spec/ruby/optional/capi/ext/struct_spec.c @@ -7,26 +7,39 @@ extern "C" { #endif +#ifdef HAVE_RB_STRUCT_AREF static VALUE struct_spec_rb_struct_aref(VALUE self, VALUE st, VALUE key) { return rb_struct_aref(st, key); } +#endif +#ifdef HAVE_RB_STRUCT_GETMEMBER static VALUE struct_spec_rb_struct_getmember(VALUE self, VALUE st, VALUE key) { return rb_struct_getmember(st, SYM2ID(key)); } +#endif -static VALUE struct_spec_rb_struct_s_members(VALUE self, VALUE klass) { +#ifdef HAVE_RB_STRUCT_S_MEMBERS +static VALUE struct_spec_rb_struct_s_members(VALUE self, VALUE klass) +{ return rb_ary_dup(rb_struct_s_members(klass)); } +#endif -static VALUE struct_spec_rb_struct_members(VALUE self, VALUE st) { +#ifdef HAVE_RB_STRUCT_MEMBERS +static VALUE struct_spec_rb_struct_members(VALUE self, VALUE st) +{ return rb_ary_dup(rb_struct_members(st)); } +#endif +#ifdef HAVE_RB_STRUCT_ASET static VALUE struct_spec_rb_struct_aset(VALUE self, VALUE st, VALUE key, VALUE value) { return rb_struct_aset(st, key, value); } +#endif +#ifdef HAVE_RB_STRUCT_DEFINE /* Only allow setting three attributes, should be sufficient for testing. */ static VALUE struct_spec_struct_define(VALUE self, VALUE name, VALUE attr1, VALUE attr2, VALUE attr3) { @@ -40,7 +53,9 @@ static VALUE struct_spec_struct_define(VALUE self, VALUE name, return rb_struct_define(nm, a1, a2, a3, NULL); } +#endif +#ifdef HAVE_RB_STRUCT_DEFINE_UNDER /* Only allow setting three attributes, should be sufficient for testing. */ static VALUE struct_spec_struct_define_under(VALUE self, VALUE outer, VALUE name, VALUE attr1, VALUE attr2, VALUE attr3) { @@ -52,27 +67,63 @@ static VALUE struct_spec_struct_define_under(VALUE self, VALUE outer, return rb_struct_define_under(outer, nm, a1, a2, a3, NULL); } +#endif +#ifdef HAVE_RB_STRUCT_NEW static VALUE struct_spec_rb_struct_new(VALUE self, VALUE klass, - VALUE a, VALUE b, VALUE c) { + VALUE a, VALUE b, VALUE c) +{ + return rb_struct_new(klass, a, b, c); } +#endif -static VALUE struct_spec_rb_struct_size(VALUE self, VALUE st) { +#ifdef HAVE_RB_STRUCT_SIZE +static VALUE struct_spec_rb_struct_size(VALUE self, VALUE st) +{ return rb_struct_size(st); } +#endif void Init_struct_spec(void) { - VALUE cls = rb_define_class("CApiStructSpecs", rb_cObject); + VALUE cls; + cls = rb_define_class("CApiStructSpecs", rb_cObject); + +#ifdef HAVE_RB_STRUCT_AREF rb_define_method(cls, "rb_struct_aref", struct_spec_rb_struct_aref, 2); +#endif + +#ifdef HAVE_RB_STRUCT_GETMEMBER rb_define_method(cls, "rb_struct_getmember", struct_spec_rb_struct_getmember, 2); +#endif + +#ifdef HAVE_RB_STRUCT_S_MEMBERS rb_define_method(cls, "rb_struct_s_members", struct_spec_rb_struct_s_members, 1); +#endif + +#ifdef HAVE_RB_STRUCT_MEMBERS rb_define_method(cls, "rb_struct_members", struct_spec_rb_struct_members, 1); +#endif + +#ifdef HAVE_RB_STRUCT_ASET rb_define_method(cls, "rb_struct_aset", struct_spec_rb_struct_aset, 3); +#endif + +#ifdef HAVE_RB_STRUCT_DEFINE rb_define_method(cls, "rb_struct_define", struct_spec_struct_define, 4); +#endif + +#ifdef HAVE_RB_STRUCT_DEFINE_UNDER rb_define_method(cls, "rb_struct_define_under", struct_spec_struct_define_under, 5); +#endif + +#ifdef HAVE_RB_STRUCT_NEW rb_define_method(cls, "rb_struct_new", struct_spec_rb_struct_new, 4); +#endif + +#ifdef HAVE_RB_STRUCT_SIZE rb_define_method(cls, "rb_struct_size", struct_spec_rb_struct_size, 1); +#endif } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/symbol_spec.c b/spec/ruby/optional/capi/ext/symbol_spec.c index ba88635faa..7ffa7cf9b1 100644 --- a/spec/ruby/optional/capi/ext/symbol_spec.c +++ b/spec/ruby/optional/capi/ext/symbol_spec.c @@ -1,16 +1,15 @@ #include "ruby.h" #include "rubyspec.h" +#ifdef HAVE_RUBY_ENCODING_H #include "ruby/encoding.h" +#endif #ifdef __cplusplus extern "C" { #endif -VALUE symbol_spec_SYMBOL_P(VALUE self, VALUE obj) { - return SYMBOL_P(obj) ? Qtrue : Qfalse; -} - +#ifdef HAVE_RB_INTERN VALUE symbol_spec_rb_intern(VALUE self, VALUE string) { return ID2SYM(rb_intern(RSTRING_PTR(string))); } @@ -32,7 +31,9 @@ VALUE symbol_spec_rb_intern2_c_compare(VALUE self, VALUE string, VALUE len, VALU ID symbol = rb_intern2(RSTRING_PTR(string), FIX2LONG(len)); return (SYM2ID(sym) == symbol) ? Qtrue : Qfalse; } +#endif +#ifdef HAVE_RB_INTERN3 VALUE symbol_spec_rb_intern3(VALUE self, VALUE string, VALUE len, VALUE enc) { return ID2SYM(rb_intern3(RSTRING_PTR(string), FIX2LONG(len), rb_enc_get(enc))); } @@ -41,74 +42,95 @@ VALUE symbol_spec_rb_intern3_c_compare(VALUE self, VALUE string, VALUE len, VALU ID symbol = rb_intern3(RSTRING_PTR(string), FIX2LONG(len), rb_enc_get(enc)); return (SYM2ID(sym) == symbol) ? Qtrue : Qfalse; } +#endif +#ifdef HAVE_RB_ID2NAME VALUE symbol_spec_rb_id2name(VALUE self, VALUE symbol) { const char* c_str = rb_id2name(SYM2ID(symbol)); return rb_str_new(c_str, strlen(c_str)); } +#endif -VALUE symbol_spec_rb_id2name_id_zero(VALUE self) { - const char* c_str = rb_id2name((ID) 0); - return c_str ? rb_str_new(c_str, strlen(c_str)) : Qnil; -} - +#ifdef HAVE_RB_ID2STR VALUE symbol_spec_rb_id2str(VALUE self, VALUE symbol) { return rb_id2str(SYM2ID(symbol)); } +#endif -VALUE symbol_spec_rb_id2str_id_zero(VALUE self) { - return rb_id2str((ID) 0); -} - +#ifdef HAVE_RB_INTERN_STR VALUE symbol_spec_rb_intern_str(VALUE self, VALUE str) { return ID2SYM(rb_intern_str(str)); } +#endif -VALUE symbol_spec_rb_check_symbol_cstr(VALUE self, VALUE str) { - return rb_check_symbol_cstr(RSTRING_PTR(str), RSTRING_LEN(str), rb_enc_get(str)); -} - +#ifdef HAVE_RB_IS_CLASS_ID VALUE symbol_spec_rb_is_class_id(VALUE self, VALUE sym) { return rb_is_class_id(SYM2ID(sym)) ? Qtrue : Qfalse; } +#endif +#ifdef HAVE_RB_IS_CONST_ID VALUE symbol_spec_rb_is_const_id(VALUE self, VALUE sym) { return rb_is_const_id(SYM2ID(sym)) ? Qtrue : Qfalse; } +#endif +#ifdef HAVE_RB_IS_INSTANCE_ID VALUE symbol_spec_rb_is_instance_id(VALUE self, VALUE sym) { return rb_is_instance_id(SYM2ID(sym)) ? Qtrue : Qfalse; } +#endif +#ifdef HAVE_RB_SYM2STR VALUE symbol_spec_rb_sym2str(VALUE self, VALUE sym) { return rb_sym2str(sym); } - -VALUE symbol_spec_rb_to_symbol(VALUE self, VALUE val) { - return rb_to_symbol(val); -} +#endif void Init_symbol_spec(void) { - VALUE cls = rb_define_class("CApiSymbolSpecs", rb_cObject); - rb_define_method(cls, "SYMBOL_P", symbol_spec_SYMBOL_P, 1); + VALUE cls; + cls = rb_define_class("CApiSymbolSpecs", rb_cObject); + +#ifdef HAVE_RB_INTERN rb_define_method(cls, "rb_intern", symbol_spec_rb_intern, 1); rb_define_method(cls, "rb_intern2", symbol_spec_rb_intern2, 2); rb_define_method(cls, "rb_intern_const", symbol_spec_rb_intern_const, 1); rb_define_method(cls, "rb_intern_c_compare", symbol_spec_rb_intern_c_compare, 2); rb_define_method(cls, "rb_intern2_c_compare", symbol_spec_rb_intern2_c_compare, 3); +#endif + +#ifdef HAVE_RB_INTERN3 rb_define_method(cls, "rb_intern3", symbol_spec_rb_intern3, 3); rb_define_method(cls, "rb_intern3_c_compare", symbol_spec_rb_intern3_c_compare, 4); +#endif + +#ifdef HAVE_RB_ID2NAME rb_define_method(cls, "rb_id2name", symbol_spec_rb_id2name, 1); - rb_define_method(cls, "rb_id2name_id_zero", symbol_spec_rb_id2name_id_zero, 0); +#endif + +#ifdef HAVE_RB_ID2STR rb_define_method(cls, "rb_id2str", symbol_spec_rb_id2str, 1); - rb_define_method(cls, "rb_id2str_id_zero", symbol_spec_rb_id2str_id_zero, 0); +#endif + +#ifdef HAVE_RB_INTERN_STR rb_define_method(cls, "rb_intern_str", symbol_spec_rb_intern_str, 1); - rb_define_method(cls, "rb_check_symbol_cstr", symbol_spec_rb_check_symbol_cstr, 1); +#endif + +#ifdef HAVE_RB_IS_CLASS_ID rb_define_method(cls, "rb_is_class_id", symbol_spec_rb_is_class_id, 1); +#endif + +#ifdef HAVE_RB_IS_CONST_ID rb_define_method(cls, "rb_is_const_id", symbol_spec_rb_is_const_id, 1); +#endif + +#ifdef HAVE_RB_IS_INSTANCE_ID rb_define_method(cls, "rb_is_instance_id", symbol_spec_rb_is_instance_id, 1); +#endif + +#ifdef HAVE_RB_SYM2STR rb_define_method(cls, "rb_sym2str", symbol_spec_rb_sym2str, 1); - rb_define_method(cls, "rb_to_symbol", symbol_spec_rb_to_symbol, 1); +#endif } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/thread_spec.c b/spec/ruby/optional/capi/ext/thread_spec.c index 3511c2fbcf..3bfe76b925 100644 --- a/spec/ruby/optional/capi/ext/thread_spec.c +++ b/spec/ruby/optional/capi/ext/thread_spec.c @@ -4,32 +4,30 @@ #include <math.h> #include <errno.h> -#ifdef HAVE_UNISTD_H +#if HAVE_UNISTD_H #include <unistd.h> #endif #if defined(_WIN32) -#include "ruby/win32.h" -#define read rb_w32_read -#define write rb_w32_write -#define pipe rb_w32_pipe -#endif - -#ifndef _WIN32 -#include <pthread.h> +#define pipe(p) rb_w32_pipe(p) #endif #ifdef __cplusplus extern "C" { #endif -static VALUE thread_spec_rb_thread_alone(VALUE self) { +#ifdef HAVE_RB_THREAD_ALONE +static VALUE thread_spec_rb_thread_alone() { return rb_thread_alone() ? Qtrue : Qfalse; } +#endif +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" + +#ifdef HAVE_RB_THREAD_CALL_WITHOUT_GVL /* This is unblocked by unblock_func(). */ static void* blocking_gvl_func(void* data) { int rfd = *(int *)data; - char dummy = ' '; + char dummy; ssize_t r; do { @@ -38,7 +36,7 @@ static void* blocking_gvl_func(void* data) { close(rfd); - return (void*)((r == 1 && dummy == 'A') ? Qtrue : Qfalse); + return (void*)((r == 1) ? Qtrue : Qfalse); } static void unblock_gvl_func(void *data) { @@ -59,7 +57,7 @@ static VALUE thread_spec_rb_thread_call_without_gvl(VALUE self) { void* ret; if (pipe(fds) == -1) { - rb_raise(rb_eRuntimeError, "could not create pipe"); + return Qfalse; } ret = rb_thread_call_without_gvl(blocking_gvl_func, &fds[0], unblock_gvl_func, &fds[1]); @@ -67,7 +65,7 @@ static VALUE thread_spec_rb_thread_call_without_gvl(VALUE self) { } /* This is unblocked by a signal. */ -static void* blocking_gvl_func_for_ubf_io(void *data) { +static void* blocking_gvl_func_for_udf_io(void *data) { int rfd = (int)(size_t)data; char dummy; @@ -84,32 +82,42 @@ static VALUE thread_spec_rb_thread_call_without_gvl_with_ubf_io(VALUE self) { void* ret; if (pipe(fds) == -1) { - rb_raise(rb_eRuntimeError, "could not create pipe"); + return Qfalse; } - ret = rb_thread_call_without_gvl(blocking_gvl_func_for_ubf_io, + ret = rb_thread_call_without_gvl(blocking_gvl_func_for_udf_io, (void*)(size_t)fds[0], RUBY_UBF_IO, 0); close(fds[0]); close(fds[1]); return (VALUE)ret; } +#endif -static VALUE thread_spec_rb_thread_current(VALUE self) { +#ifdef HAVE_RB_THREAD_CURRENT +static VALUE thread_spec_rb_thread_current() { return rb_thread_current(); } +#endif +#ifdef HAVE_RB_THREAD_LOCAL_AREF static VALUE thread_spec_rb_thread_local_aref(VALUE self, VALUE thr, VALUE sym) { return rb_thread_local_aref(thr, SYM2ID(sym)); } +#endif +#ifdef HAVE_RB_THREAD_LOCAL_ASET static VALUE thread_spec_rb_thread_local_aset(VALUE self, VALUE thr, VALUE sym, VALUE value) { return rb_thread_local_aset(thr, SYM2ID(sym), value); } +#endif +#ifdef HAVE_RB_THREAD_WAKEUP static VALUE thread_spec_rb_thread_wakeup(VALUE self, VALUE thr) { return rb_thread_wakeup(thr); } +#endif +#ifdef HAVE_RB_THREAD_WAIT_FOR static VALUE thread_spec_rb_thread_wait_for(VALUE self, VALUE s, VALUE ms) { struct timeval tv; tv.tv_sec = NUM2INT(s); @@ -117,10 +125,11 @@ static VALUE thread_spec_rb_thread_wait_for(VALUE self, VALUE s, VALUE ms) { rb_thread_wait_for(tv); return Qnil; } +#endif +#ifdef HAVE_RB_THREAD_CREATE -VALUE thread_spec_call_proc(void *arg_ptr) { - VALUE arg_array = (VALUE)arg_ptr; +VALUE thread_spec_call_proc(VALUE arg_array) { VALUE arg = rb_ary_pop(arg_array); VALUE proc = rb_ary_pop(arg_array); return rb_funcall(proc, rb_intern("call"), 1, arg); @@ -133,53 +142,45 @@ static VALUE thread_spec_rb_thread_create(VALUE self, VALUE proc, VALUE arg) { return rb_thread_create(thread_spec_call_proc, (void*)args); } - -static VALUE thread_spec_ruby_native_thread_p(VALUE self) { - if (ruby_native_thread_p()) { - return Qtrue; - } else { - return Qfalse; - } -} - -#ifndef _WIN32 -static VALUE false_result = Qfalse; -static VALUE true_result = Qtrue; - -static void *new_thread_check(void *args) { - if (ruby_native_thread_p()) { - return &true_result; - } else { - return &false_result; - } -} #endif -static VALUE thread_spec_ruby_native_thread_p_new_thread(VALUE self) { -#ifndef _WIN32 - pthread_t t; - void *result = &true_result; - pthread_create(&t, NULL, new_thread_check, NULL); - pthread_join(t, &result); - return *(VALUE *)result; -#else - return Qfalse; -#endif -} void Init_thread_spec(void) { - VALUE cls = rb_define_class("CApiThreadSpecs", rb_cObject); + VALUE cls; + cls = rb_define_class("CApiThreadSpecs", rb_cObject); + +#ifdef HAVE_RB_THREAD_ALONE rb_define_method(cls, "rb_thread_alone", thread_spec_rb_thread_alone, 0); +#endif + +#ifdef HAVE_RB_THREAD_CALL_WITHOUT_GVL rb_define_method(cls, "rb_thread_call_without_gvl", thread_spec_rb_thread_call_without_gvl, 0); rb_define_method(cls, "rb_thread_call_without_gvl_with_ubf_io", thread_spec_rb_thread_call_without_gvl_with_ubf_io, 0); +#endif + +#ifdef HAVE_RB_THREAD_CURRENT rb_define_method(cls, "rb_thread_current", thread_spec_rb_thread_current, 0); +#endif + +#ifdef HAVE_RB_THREAD_LOCAL_AREF rb_define_method(cls, "rb_thread_local_aref", thread_spec_rb_thread_local_aref, 2); +#endif + +#ifdef HAVE_RB_THREAD_LOCAL_ASET rb_define_method(cls, "rb_thread_local_aset", thread_spec_rb_thread_local_aset, 3); +#endif + +#ifdef HAVE_RB_THREAD_WAKEUP rb_define_method(cls, "rb_thread_wakeup", thread_spec_rb_thread_wakeup, 1); +#endif + +#ifdef HAVE_RB_THREAD_WAIT_FOR rb_define_method(cls, "rb_thread_wait_for", thread_spec_rb_thread_wait_for, 2); +#endif + +#ifdef HAVE_RB_THREAD_CREATE rb_define_method(cls, "rb_thread_create", thread_spec_rb_thread_create, 2); - rb_define_method(cls, "ruby_native_thread_p", thread_spec_ruby_native_thread_p, 0); - rb_define_method(cls, "ruby_native_thread_p_new_thread", thread_spec_ruby_native_thread_p_new_thread, 0); +#endif } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/time_spec.c b/spec/ruby/optional/capi/ext/time_spec.c index fec70dea9d..6b51120f1b 100644 --- a/spec/ruby/optional/capi/ext/time_spec.c +++ b/spec/ruby/optional/capi/ext/time_spec.c @@ -7,18 +7,25 @@ extern "C" { #endif +#ifdef HAVE_RB_TIME_NEW static VALUE time_spec_rb_time_new(VALUE self, VALUE sec, VALUE usec) { return rb_time_new(NUM2TIMET(sec), NUM2LONG(usec)); } +#endif +#ifdef HAVE_RB_TIME_NANO_NEW static VALUE time_spec_rb_time_nano_new(VALUE self, VALUE sec, VALUE nsec) { return rb_time_nano_new(NUM2TIMET(sec), NUM2LONG(nsec)); } +#endif +#ifdef HAVE_RB_TIME_NUM_NEW static VALUE time_spec_rb_time_num_new(VALUE self, VALUE ts, VALUE offset) { return rb_time_num_new(ts, offset); } +#endif +#ifdef HAVE_RB_TIME_INTERVAL static VALUE time_spec_rb_time_interval(VALUE self, VALUE ts) { struct timeval interval = rb_time_interval(ts); VALUE ary = rb_ary_new(); @@ -26,7 +33,9 @@ static VALUE time_spec_rb_time_interval(VALUE self, VALUE ts) { rb_ary_push(ary, TIMET2NUM(interval.tv_usec)); return ary; } +#endif +#ifdef HAVE_RB_TIME_TIMEVAL static VALUE time_spec_rb_time_timeval(VALUE self, VALUE ts) { struct timeval tv = rb_time_timeval(ts); VALUE ary = rb_ary_new(); @@ -34,7 +43,9 @@ static VALUE time_spec_rb_time_timeval(VALUE self, VALUE ts) { rb_ary_push(ary, TIMET2NUM(tv.tv_usec)); return ary; } +#endif +#ifdef HAVE_RB_TIME_TIMESPEC static VALUE time_spec_rb_time_timespec(VALUE self, VALUE time) { struct timespec ts = rb_time_timespec(time); VALUE ary = rb_ary_new(); @@ -42,7 +53,9 @@ static VALUE time_spec_rb_time_timespec(VALUE self, VALUE time) { rb_ary_push(ary, TIMET2NUM(ts.tv_nsec)); return ary; } +#endif +#ifdef HAVE_RB_TIME_TIMESPEC_NEW static VALUE time_spec_rb_time_timespec_new(VALUE self, VALUE sec, VALUE nsec, VALUE offset) { struct timespec ts; ts.tv_sec = NUM2TIMET(sec); @@ -50,30 +63,63 @@ static VALUE time_spec_rb_time_timespec_new(VALUE self, VALUE sec, VALUE nsec, V return rb_time_timespec_new(&ts, NUM2INT(offset)); } +#endif +#ifdef HAVE_RB_TIMESPEC_NOW static VALUE time_spec_rb_time_from_timspec_now(VALUE self, VALUE offset) { struct timespec ts; rb_timespec_now(&ts); return rb_time_timespec_new(&ts, NUM2INT(offset)); } +#endif +#ifdef HAVE_TIMET2NUM static VALUE time_spec_TIMET2NUM(VALUE self) { time_t t = 10; return TIMET2NUM(t); } +#endif void Init_time_spec(void) { - VALUE cls = rb_define_class("CApiTimeSpecs", rb_cObject); + VALUE cls; + cls = rb_define_class("CApiTimeSpecs", rb_cObject); + +#ifdef HAVE_RB_TIME_NEW rb_define_method(cls, "rb_time_new", time_spec_rb_time_new, 2); +#endif + +#ifdef HAVE_TIMET2NUM rb_define_method(cls, "TIMET2NUM", time_spec_TIMET2NUM, 0); +#endif + +#ifdef HAVE_RB_TIME_NANO_NEW rb_define_method(cls, "rb_time_nano_new", time_spec_rb_time_nano_new, 2); +#endif + +#ifdef HAVE_RB_TIME_NUM_NEW rb_define_method(cls, "rb_time_num_new", time_spec_rb_time_num_new, 2); +#endif + +#ifdef HAVE_RB_TIME_INTERVAL rb_define_method(cls, "rb_time_interval", time_spec_rb_time_interval, 1); +#endif + +#ifdef HAVE_RB_TIME_TIMEVAL rb_define_method(cls, "rb_time_timeval", time_spec_rb_time_timeval, 1); +#endif + +#ifdef HAVE_RB_TIME_TIMESPEC rb_define_method(cls, "rb_time_timespec", time_spec_rb_time_timespec, 1); +#endif + +#ifdef HAVE_RB_TIME_TIMESPEC_NEW rb_define_method(cls, "rb_time_timespec_new", time_spec_rb_time_timespec_new, 3); +#endif + +#ifdef HAVE_RB_TIMESPEC_NOW rb_define_method(cls, "rb_time_from_timespec", time_spec_rb_time_from_timspec_now, 1); +#endif } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/tracepoint_spec.c b/spec/ruby/optional/capi/ext/tracepoint_spec.c deleted file mode 100644 index 6666c8f85c..0000000000 --- a/spec/ruby/optional/capi/ext/tracepoint_spec.c +++ /dev/null @@ -1,49 +0,0 @@ -#include "ruby.h" -#include "rubyspec.h" - -#include <ruby/debug.h> - -#ifdef __cplusplus -extern "C" { -#endif - -static VALUE callback_called = Qnil; - -static void callback(VALUE tpval, void *data) { - callback_called = (VALUE) data; -} - -static VALUE tracepoint_spec_rb_tracepoint_new(VALUE self, VALUE data) { - return rb_tracepoint_new(Qnil, RUBY_EVENT_LINE, callback, (void*) data); -} - -static VALUE tracepoint_spec_callback_called(VALUE self) { - return callback_called; -} - -static VALUE tracepoint_spec_rb_tracepoint_disable(VALUE self, VALUE trace) { - rb_tracepoint_disable(trace); - return rb_tracepoint_enabled_p(trace); -} - -static VALUE tracepoint_spec_rb_tracepoint_enable(VALUE self, VALUE trace) { - rb_tracepoint_enable(trace); - return rb_tracepoint_enabled_p(trace); -} - -static VALUE tracepoint_spec_rb_tracepoint_enabled_p(VALUE self, VALUE trace) { - return rb_tracepoint_enabled_p(trace); -} - -void Init_tracepoint_spec(void) { - VALUE cls = rb_define_class("CApiTracePointSpecs", rb_cObject); - rb_define_method(cls, "rb_tracepoint_new", tracepoint_spec_rb_tracepoint_new, 1); - rb_define_method(cls, "rb_tracepoint_disable", tracepoint_spec_rb_tracepoint_disable, 1); - rb_define_method(cls, "rb_tracepoint_enable", tracepoint_spec_rb_tracepoint_enable, 1); - rb_define_method(cls, "rb_tracepoint_enabled_p", tracepoint_spec_rb_tracepoint_enabled_p, 1); - rb_define_method(cls, "callback_called?", tracepoint_spec_callback_called, 0); -} - -#ifdef __cplusplus -} -#endif diff --git a/spec/ruby/optional/capi/ext/typed_data_spec.c b/spec/ruby/optional/capi/ext/typed_data_spec.c index 221f1c8ac4..8c9a0708b4 100644 --- a/spec/ruby/optional/capi/ext/typed_data_spec.c +++ b/spec/ruby/optional/capi/ext/typed_data_spec.c @@ -7,8 +7,9 @@ extern "C" { #endif +#if defined(HAVE_RTYPEDDATA) && defined(HAVE_TYPEDDATA_WRAP_STRUCT) struct sample_typed_wrapped_struct_parent { - int foo; + int foo; }; void sample_typed_wrapped_struct_parent_free(void* st) { @@ -32,7 +33,7 @@ static const rb_data_type_t sample_typed_wrapped_struct_parent_data_type = { }; struct sample_typed_wrapped_struct { - int foo; + int foo; }; void sample_typed_wrapped_struct_free(void* st) { @@ -43,11 +44,7 @@ void sample_typed_wrapped_struct_mark(void* st) { } size_t sample_typed_wrapped_struct_memsize(const void* st) { - if (st == NULL) { - return 0; - } else { - return ((struct sample_typed_wrapped_struct *)st)->foo; - } + return sizeof(struct sample_typed_wrapped_struct); } static const rb_data_type_t sample_typed_wrapped_struct_data_type = { @@ -61,7 +58,7 @@ static const rb_data_type_t sample_typed_wrapped_struct_data_type = { }; struct sample_typed_wrapped_struct_other { - int foo; + int foo; }; void sample_typed_wrapped_struct_other_free(void* st) { @@ -86,53 +83,49 @@ static const rb_data_type_t sample_typed_wrapped_struct_other_data_type = { VALUE sdaf_alloc_typed_func(VALUE klass) { - struct sample_typed_wrapped_struct* bar; - bar = (struct sample_typed_wrapped_struct *) malloc(sizeof(struct sample_typed_wrapped_struct)); - bar->foo = 42; - return TypedData_Wrap_Struct(klass, &sample_typed_wrapped_struct_data_type, bar); + struct sample_typed_wrapped_struct* bar = (struct sample_typed_wrapped_struct *)malloc(sizeof(struct sample_typed_wrapped_struct)); + bar->foo = 42; + return TypedData_Wrap_Struct(klass, &sample_typed_wrapped_struct_data_type, bar); } VALUE sdaf_typed_get_struct(VALUE self) { - struct sample_typed_wrapped_struct* bar; - TypedData_Get_Struct(self, struct sample_typed_wrapped_struct, &sample_typed_wrapped_struct_data_type, bar); + struct sample_typed_wrapped_struct* bar; + TypedData_Get_Struct(self, struct sample_typed_wrapped_struct, &sample_typed_wrapped_struct_data_type, bar); - return INT2FIX((*bar).foo); + return INT2FIX((*bar).foo); } VALUE sws_typed_wrap_struct(VALUE self, VALUE val) { - struct sample_typed_wrapped_struct* bar; - bar = (struct sample_typed_wrapped_struct *) malloc(sizeof(struct sample_typed_wrapped_struct)); - bar->foo = FIX2INT(val); - return TypedData_Wrap_Struct(rb_cObject, &sample_typed_wrapped_struct_data_type, bar); + struct sample_typed_wrapped_struct* bar = (struct sample_typed_wrapped_struct *)malloc(sizeof(struct sample_typed_wrapped_struct)); + bar->foo = FIX2INT(val); + return TypedData_Wrap_Struct(rb_cObject, &sample_typed_wrapped_struct_data_type, bar); } -#undef RUBY_UNTYPED_DATA_WARNING -#define RUBY_UNTYPED_DATA_WARNING 0 -VALUE sws_untyped_wrap_struct(VALUE self, VALUE val) { - int* data = (int*) malloc(sizeof(int)); - *data = FIX2INT(val); - return Data_Wrap_Struct(rb_cObject, NULL, free, data); +VALUE sws_typed_wrap_struct_null(VALUE self, VALUE val) { + struct sample_typed_wrapped_struct* bar = (struct sample_typed_wrapped_struct *)malloc(sizeof(struct sample_typed_wrapped_struct)); + bar->foo = FIX2INT(val); + return TypedData_Wrap_Struct(0, &sample_typed_wrapped_struct_data_type, bar); } VALUE sws_typed_get_struct(VALUE self, VALUE obj) { - struct sample_typed_wrapped_struct* bar; - TypedData_Get_Struct(obj, struct sample_typed_wrapped_struct, &sample_typed_wrapped_struct_data_type, bar); + struct sample_typed_wrapped_struct* bar; + TypedData_Get_Struct(obj, struct sample_typed_wrapped_struct, &sample_typed_wrapped_struct_data_type, bar); - return INT2FIX((*bar).foo); + return INT2FIX((*bar).foo); } VALUE sws_typed_get_struct_different_type(VALUE self, VALUE obj) { - struct sample_typed_wrapped_struct_other* bar; - TypedData_Get_Struct(obj, struct sample_typed_wrapped_struct_other, &sample_typed_wrapped_struct_other_data_type, bar); + struct sample_typed_wrapped_struct_other* bar; + TypedData_Get_Struct(obj, struct sample_typed_wrapped_struct_other, &sample_typed_wrapped_struct_other_data_type, bar); - return INT2FIX((*bar).foo); + return INT2FIX((*bar).foo); } VALUE sws_typed_get_struct_parent_type(VALUE self, VALUE obj) { - struct sample_typed_wrapped_struct_parent* bar; - TypedData_Get_Struct(obj, struct sample_typed_wrapped_struct_parent, &sample_typed_wrapped_struct_parent_data_type, bar); + struct sample_typed_wrapped_struct_parent* bar; + TypedData_Get_Struct(obj, struct sample_typed_wrapped_struct_parent, &sample_typed_wrapped_struct_parent_data_type, bar); - return INT2FIX((*bar).foo); + return INT2FIX((*bar).foo); } VALUE sws_typed_get_struct_rdata(VALUE self, VALUE obj) { @@ -148,53 +141,34 @@ VALUE sws_typed_get_struct_data_ptr(VALUE self, VALUE obj) { } VALUE sws_typed_change_struct(VALUE self, VALUE obj, VALUE new_val) { - struct sample_typed_wrapped_struct *new_struct; - new_struct = (struct sample_typed_wrapped_struct *) malloc(sizeof(struct sample_typed_wrapped_struct)); + struct sample_typed_wrapped_struct *old_struct, *new_struct; + new_struct = (struct sample_typed_wrapped_struct *)malloc(sizeof(struct sample_typed_wrapped_struct)); new_struct->foo = FIX2INT(new_val); - free(RTYPEDDATA(obj)->data); + old_struct = RTYPEDDATA(obj)->data; + free(old_struct); RTYPEDDATA(obj)->data = new_struct; return Qnil; } - -VALUE sws_typed_rb_check_type(VALUE self, VALUE obj, VALUE other) { - rb_check_type(obj, TYPE(other)); - return Qtrue; -} - -VALUE sws_typed_rb_check_typeddata_same_type(VALUE self, VALUE obj) { - return rb_check_typeddata(obj, &sample_typed_wrapped_struct_data_type) == DATA_PTR(obj) ? Qtrue : Qfalse; -} - -VALUE sws_typed_rb_check_typeddata_same_type_parent(VALUE self, VALUE obj) { - return rb_check_typeddata(obj, &sample_typed_wrapped_struct_parent_data_type) == DATA_PTR(obj) ? Qtrue : Qfalse; -} - -VALUE sws_typed_rb_check_typeddata_different_type(VALUE self, VALUE obj) { - return rb_check_typeddata(obj, &sample_typed_wrapped_struct_other_data_type) == DATA_PTR(obj) ? Qtrue : Qfalse; -} - -VALUE sws_typed_RTYPEDDATA_P(VALUE self, VALUE obj) { - return RTYPEDDATA_P(obj) ? Qtrue : Qfalse; -} +#endif void Init_typed_data_spec(void) { - VALUE cls = rb_define_class("CApiAllocTypedSpecs", rb_cObject); + VALUE cls; + cls = rb_define_class("CApiAllocTypedSpecs", rb_cObject); + +#if defined(HAVE_RTYPEDDATA) && defined(HAVE_TYPEDDATA_WRAP_STRUCT) rb_define_alloc_func(cls, sdaf_alloc_typed_func); rb_define_method(cls, "typed_wrapped_data", sdaf_typed_get_struct, 0); + cls = rb_define_class("CApiWrappedTypedStructSpecs", rb_cObject); rb_define_method(cls, "typed_wrap_struct", sws_typed_wrap_struct, 1); - rb_define_method(cls, "untyped_wrap_struct", sws_untyped_wrap_struct, 1); + rb_define_method(cls, "typed_wrap_struct_null", sws_typed_wrap_struct_null, 1); rb_define_method(cls, "typed_get_struct", sws_typed_get_struct, 1); rb_define_method(cls, "typed_get_struct_other", sws_typed_get_struct_different_type, 1); rb_define_method(cls, "typed_get_struct_parent", sws_typed_get_struct_parent_type, 1); rb_define_method(cls, "typed_get_struct_rdata", sws_typed_get_struct_rdata, 1); rb_define_method(cls, "typed_get_struct_data_ptr", sws_typed_get_struct_data_ptr, 1); rb_define_method(cls, "typed_change_struct", sws_typed_change_struct, 2); - rb_define_method(cls, "rb_check_type", sws_typed_rb_check_type, 2); - rb_define_method(cls, "rb_check_typeddata_same_type", sws_typed_rb_check_typeddata_same_type, 1); - rb_define_method(cls, "rb_check_typeddata_same_type_parent", sws_typed_rb_check_typeddata_same_type_parent, 1); - rb_define_method(cls, "rb_check_typeddata_different_type", sws_typed_rb_check_typeddata_different_type, 1); - rb_define_method(cls, "RTYPEDDATA_P", sws_typed_RTYPEDDATA_P, 1); +#endif } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/ext/util_spec.c b/spec/ruby/optional/capi/ext/util_spec.c index b5bde420d2..50795b51af 100644 --- a/spec/ruby/optional/capi/ext/util_spec.c +++ b/spec/ruby/optional/capi/ext/util_spec.c @@ -1,51 +1,35 @@ #include "ruby.h" -#include "ruby/util.h" #include "rubyspec.h" #ifdef __cplusplus extern "C" { #endif +#ifdef HAVE_RB_SCAN_ARGS VALUE util_spec_rb_scan_args(VALUE self, VALUE argv, VALUE fmt, VALUE expected, VALUE acc) { - int result, argc; - VALUE a1, a2, a3, a4, a5, a6; - - argc = (int) RARRAY_LEN(argv); - VALUE* args = RARRAY_PTR(argv); - /* the line above can be replaced with this for Ruby implementations which do not support RARRAY_PTR() yet - VALUE args[6]; - for(int i = 0; i < argc; i++) { - args[i] = rb_ary_entry(argv, i); - } */ - - a1 = a2 = a3 = a4 = a5 = a6 = INT2FIX(-1); - -#ifdef RB_SCAN_ARGS_KEYWORDS - if (*RSTRING_PTR(fmt) == 'k') { - result = rb_scan_args_kw(RB_SCAN_ARGS_KEYWORDS, argc, args, RSTRING_PTR(fmt)+1, &a1, &a2, &a3, &a4, &a5, &a6); - } else { -#endif - result = rb_scan_args(argc, args, RSTRING_PTR(fmt), &a1, &a2, &a3, &a4, &a5, &a6); -#ifdef RB_SCAN_ARGS_KEYWORDS + int i, result, argc = (int)RARRAY_LEN(argv); + VALUE args[6], failed, a1, a2, a3, a4, a5, a6; + + failed = rb_intern("failed"); + a1 = a2 = a3 = a4 = a5 = a6 = failed; + + for(i = 0; i < argc; i++) { + args[i] = rb_ary_entry(argv, i); } -#endif + + result = rb_scan_args(argc, args, RSTRING_PTR(fmt), &a1, &a2, &a3, &a4, &a5, &a6); switch(NUM2INT(expected)) { case 6: rb_ary_unshift(acc, a6); - /* FALLTHROUGH */ case 5: rb_ary_unshift(acc, a5); - /* FALLTHROUGH */ case 4: rb_ary_unshift(acc, a4); - /* FALLTHROUGH */ case 3: rb_ary_unshift(acc, a3); - /* FALLTHROUGH */ case 2: rb_ary_unshift(acc, a2); - /* FALLTHROUGH */ case 1: rb_ary_unshift(acc, a1); break; @@ -55,65 +39,55 @@ VALUE util_spec_rb_scan_args(VALUE self, VALUE argv, VALUE fmt, VALUE expected, return INT2NUM(result); } +#endif -static VALUE util_spec_rb_get_kwargs(VALUE self, VALUE keyword_hash, VALUE keys, VALUE required, VALUE optional) { - int req = FIX2INT(required); - int opt = FIX2INT(optional); - int len = RARRAY_LENINT(keys); - - int values_len = req + (opt < 0 ? -1 - opt : opt); - - ID *ids = (ID *)alloca(sizeof(VALUE) * len); - VALUE *results = (VALUE *)alloca(sizeof(VALUE) * values_len); - - for (int i = 0; i < len; i++) { - ids[i] = SYM2ID(rb_ary_entry(keys, i)); - } - - int extracted = rb_get_kwargs(keyword_hash, ids, req, opt, results); - - return rb_ary_new_from_values(extracted, results); -} - +#ifdef HAVE_RB_LONG2INT static VALUE util_spec_rb_long2int(VALUE self, VALUE n) { return INT2NUM(rb_long2int(NUM2LONG(n))); } +#endif +#ifdef HAVE_RB_ITER_BREAK static VALUE util_spec_rb_iter_break(VALUE self) { rb_iter_break(); return Qnil; } +#endif +#ifdef HAVE_RB_SOURCEFILE static VALUE util_spec_rb_sourcefile(VALUE self) { return rb_str_new2(rb_sourcefile()); } +#endif +#ifdef HAVE_RB_SOURCELINE static VALUE util_spec_rb_sourceline(VALUE self) { return INT2NUM(rb_sourceline()); } - -static VALUE util_spec_strtod(VALUE self, VALUE string) { - char *endptr = NULL; - double value = strtod(RSTRING_PTR(string), &endptr); - return rb_ary_new_from_args(2, rb_float_new(value), endptr ? rb_str_new2(endptr) : Qnil); -} - -static VALUE util_spec_ruby_strtod(VALUE self, VALUE string) { - char *endptr = NULL; - double value = ruby_strtod(RSTRING_PTR(string), &endptr); - return rb_ary_new_from_args(2, rb_float_new(value), endptr ? rb_str_new2(endptr) : Qnil); -} +#endif void Init_util_spec(void) { VALUE cls = rb_define_class("CApiUtilSpecs", rb_cObject); + +#ifdef HAVE_RB_SCAN_ARGS rb_define_method(cls, "rb_scan_args", util_spec_rb_scan_args, 4); - rb_define_method(cls, "rb_get_kwargs", util_spec_rb_get_kwargs, 4); +#endif + +#ifdef HAVE_RB_LONG2INT rb_define_method(cls, "rb_long2int", util_spec_rb_long2int, 1); +#endif + +#ifdef HAVE_RB_ITER_BREAK rb_define_method(cls, "rb_iter_break", util_spec_rb_iter_break, 0); +#endif + +#ifdef HAVE_RB_SOURCEFILE rb_define_method(cls, "rb_sourcefile", util_spec_rb_sourcefile, 0); +#endif + +#ifdef HAVE_RB_SOURCELINE rb_define_method(cls, "rb_sourceline", util_spec_rb_sourceline, 0); - rb_define_method(cls, "strtod", util_spec_strtod, 1); - rb_define_method(cls, "ruby_strtod", util_spec_ruby_strtod, 1); +#endif } #ifdef __cplusplus diff --git a/spec/ruby/optional/capi/fiber_spec.rb b/spec/ruby/optional/capi/fiber_spec.rb deleted file mode 100644 index 357033f860..0000000000 --- a/spec/ruby/optional/capi/fiber_spec.rb +++ /dev/null @@ -1,89 +0,0 @@ -require_relative 'spec_helper' -require 'fiber' - -load_extension('fiber') - -describe "C-API Fiber function" do - before :each do - @s = CApiFiberSpecs.new - end - - describe "rb_fiber_current" do - it "returns the current fiber" do - result = @s.rb_fiber_current() - result.should be_an_instance_of(Fiber) - result.should == Fiber.current - end - end - - describe "rb_fiber_alive_p" do - it "returns the fibers alive status" do - fiber = Fiber.new { Fiber.yield } - fiber.resume - @s.rb_fiber_alive_p(fiber).should be_true - fiber.resume - @s.rb_fiber_alive_p(fiber).should be_false - end - end - - describe "rb_fiber_resume" do - it "resumes the fiber" do - fiber = Fiber.new { |arg| Fiber.yield arg } - @s.rb_fiber_resume(fiber, [1]).should == 1 - @s.rb_fiber_resume(fiber, [2]).should == 2 - end - end - - describe "rb_fiber_yield" do - it "yields the fiber" do - fiber = Fiber.new { @s.rb_fiber_yield([1]) } - fiber.resume.should == 1 - end - end - - describe "rb_fiber_new" do - it "returns a new fiber" do - fiber = @s.rb_fiber_new - fiber.should be_an_instance_of(Fiber) - fiber.resume(42).should == "42" - end - end - - ruby_version_is '3.1' do - describe "rb_fiber_raise" do - it "raises an exception on the resumed fiber" do - fiber = Fiber.new do - begin - Fiber.yield - rescue => error - error - end - end - - fiber.resume - - result = @s.rb_fiber_raise(fiber, "Boom!") - result.should be_an_instance_of(RuntimeError) - result.message.should == "Boom!" - end - - it "raises an exception on the transferred fiber" do - main = Fiber.current - - fiber = Fiber.new do - begin - main.transfer - rescue => error - error - end - end - - fiber.transfer - - result = @s.rb_fiber_raise(fiber, "Boom!") - result.should be_an_instance_of(RuntimeError) - result.message.should == "Boom!" - end - end - end -end diff --git a/spec/ruby/optional/capi/file_spec.rb b/spec/ruby/optional/capi/file_spec.rb index 12449b4e34..2459dba979 100644 --- a/spec/ruby/optional/capi/file_spec.rb +++ b/spec/ruby/optional/capi/file_spec.rb @@ -1,11 +1,11 @@ -require_relative 'spec_helper' +require File.expand_path('../spec_helper', __FILE__) load_extension('file') describe :rb_file_open, shared: true do it "raises an ArgumentError if passed an empty mode string" do touch @name - -> { @s.rb_file_open(@name, "") }.should raise_error(ArgumentError) + lambda { @s.rb_file_open(@name, "") }.should raise_error(ArgumentError) end it "opens a file in read-only mode with 'r'" do @@ -69,7 +69,7 @@ describe "C-API File function" do end it "does not call #to_str on a String" do - obj = +"path" + obj = "path" obj.should_not_receive(:to_str) @s.FilePathValue(obj).should eql(obj) end diff --git a/spec/ruby/optional/capi/fixnum_spec.rb b/spec/ruby/optional/capi/fixnum_spec.rb index e691aa3893..d9e9d1946d 100644 --- a/spec/ruby/optional/capi/fixnum_spec.rb +++ b/spec/ruby/optional/capi/fixnum_spec.rb @@ -1,4 +1,4 @@ -require_relative 'spec_helper' +require File.expand_path('../spec_helper', __FILE__) load_extension("fixnum") @@ -8,16 +8,14 @@ describe "CApiFixnumSpecs" do end describe "FIX2INT" do - max_int = (1 << 31) - 1 - min_int = -(1 << 31) - it "converts a Fixnum to a native int" do @s.FIX2INT(42).should == 42 @s.FIX2INT(-14).should == -14 - @s.FIX2INT(-1).should == -1 - @s.FIX2INT(1).should == 1 end + max_int = (1 << 31) - 1 + min_int = -(1 << 31) + guard -> { fixnum_min <= min_int and max_int <= fixnum_max } do it "converts a Fixnum representing the minimum and maximum native int" do @s.FIX2INT(max_int).should == max_int @@ -25,76 +23,101 @@ describe "CApiFixnumSpecs" do end end - platform_is c_long_size: 64 do # sizeof(long) > sizeof(int) + end + + describe "FIX2UINT" do + it "converts a Fixnum to a native int" do + @s.FIX2UINT(42).should == 42 + @s.FIX2UINT(0).should == 0 + end + + max_uint = (1 << 32) - 1 + + guard -> { max_uint <= fixnum_max } do + it "converts a Fixnum representing the maximum native uint" do + @s.FIX2UINT(max_uint).should == max_uint + end + end + + end + + platform_is wordsize: 64 do + describe "rb_fix2uint" do it "raises a TypeError if passed nil" do - -> { @s.FIX2INT(nil) }.should raise_error(TypeError) + lambda { @s.rb_fix2uint(nil) }.should raise_error(TypeError) end - it "converts a Float" do - @s.FIX2INT(25.4567).should == 25 + it "converts a Fixnum" do + @s.rb_fix2uint(0).should == 0 + @s.rb_fix2uint(1).should == 1 end - it "converts a negative Bignum into an signed number" do - @s.FIX2INT(-2147442171).should == -2147442171 + it "converts the maximum uint value" do + @s.rb_fix2uint(0xffff_ffff).should == 0xffff_ffff end - it "raises a RangeError if the value does not fit a native int" do - -> { @s.FIX2INT(0x7fff_ffff+1) }.should raise_error(RangeError) - -> { @s.FIX2INT(-(1 << 31) - 1) }.should raise_error(RangeError) + it "converts a Float" do + @s.rb_fix2uint(25.4567).should == 25 end - it "raises a RangeError if the value is more than 32bits" do - -> { @s.FIX2INT(0xffff_ffff+1) }.should raise_error(RangeError) + it "raises a RangeError if the value does not fit a native uint" do + # Interestingly, on MRI rb_fix2uint(-1) is allowed + lambda { @s.rb_fix2uint(0xffff_ffff+1) }.should raise_error(RangeError) + lambda { @s.rb_fix2uint(-(1 << 31) - 1) }.should raise_error(RangeError) end - it "raises a RangeError if the value is more than 64bits" do - -> { @s.FIX2INT(0xffff_ffff_ffff_ffff+1) }.should raise_error(RangeError) + it "raises a RangeError if the value is more than 32bits" do + lambda { @s.rb_fix2uint(0xffff_ffff+1) }.should raise_error(RangeError) end - it "calls #to_int to coerce the value" do - obj = mock("number") - obj.should_receive(:to_int).and_return(2) - @s.FIX2INT(obj).should == 2 + it "raises a RangeError if the value is more than 64bits" do + lambda { @s.rb_fix2uint(0xffff_ffff_ffff_ffff+1) }.should raise_error(RangeError) end end - end - describe "FIX2UINT" do - max_uint = (1 << 32) - 1 + describe "rb_fix2int" do + it "raises a TypeError if passed nil" do + lambda { @s.rb_fix2int(nil) }.should raise_error(TypeError) + end - it "converts a Fixnum" do - @s.FIX2UINT(0).should == 0 - @s.FIX2UINT(1).should == 1 - @s.FIX2UINT(42).should == 42 - end + it "converts a Fixnum" do + @s.rb_fix2int(-1).should == -1 + @s.rb_fix2int(1).should == 1 + end - guard -> { max_uint <= fixnum_max } do - it "converts a Fixnum representing the maximum native uint" do - @s.FIX2UINT(max_uint).should == max_uint + it "converts the minimum int value" do + @s.rb_fix2int(-(1 << 31)).should == -(1 << 31) end - end - platform_is c_long_size: 64 do # sizeof(long) > sizeof(int) - it "raises a TypeError if passed nil" do - -> { @s.FIX2UINT(nil) }.should raise_error(TypeError) + it "converts the maximum int value" do + @s.rb_fix2int(0x7fff_ffff).should == 0x7fff_ffff end it "converts a Float" do - @s.FIX2UINT(25.4567).should == 25 + @s.rb_fix2int(25.4567).should == 25 end - it "raises a RangeError if the value does not fit a native uint" do - # Interestingly, on MRI FIX2UINT(-1) is allowed - -> { @s.FIX2UINT(0xffff_ffff+1) }.should raise_error(RangeError) - -> { @s.FIX2UINT(-(1 << 31) - 1) }.should raise_error(RangeError) + it "converts a negative Bignum into an signed number" do + @s.rb_fix2int(-2147442171).should == -2147442171 + end + + it "raises a RangeError if the value does not fit a native int" do + lambda { @s.rb_fix2int(0x7fff_ffff+1) }.should raise_error(RangeError) + lambda { @s.rb_fix2int(-(1 << 31) - 1) }.should raise_error(RangeError) end it "raises a RangeError if the value is more than 32bits" do - -> { @s.FIX2UINT(0xffff_ffff+1) }.should raise_error(RangeError) + lambda { @s.rb_fix2int(0xffff_ffff+1) }.should raise_error(RangeError) end it "raises a RangeError if the value is more than 64bits" do - -> { @s.FIX2UINT(0xffff_ffff_ffff_ffff+1) }.should raise_error(RangeError) + lambda { @s.rb_fix2int(0xffff_ffff_ffff_ffff+1) }.should raise_error(RangeError) + end + + it "calls #to_int to coerce the value" do + obj = mock("number") + obj.should_receive(:to_int).and_return(2) + @s.rb_fix2int(obj).should == 2 end end end diff --git a/spec/ruby/optional/capi/fixtures/class.rb b/spec/ruby/optional/capi/fixtures/class.rb index b463e3b4c3..de824b3ab0 100644 --- a/spec/ruby/optional/capi/fixtures/class.rb +++ b/spec/ruby/optional/capi/fixtures/class.rb @@ -15,16 +15,6 @@ class CApiClassSpecs end end - class KeywordAlloc - attr_reader :initialized, :args, :kwargs - - def initialize(*args, **kwargs) - @initialized = true - @args = args - @kwargs = kwargs - end - end - class Attr def initialize @foo, @bar, @baz = 1, 2, 3 @@ -78,27 +68,15 @@ class CApiClassSpecs class SubSub < Sub def call_super_method - :subsubclass_method - end - end - - class SuperSelf - def call_super_method - self + :subclass_method end end - class SubSelf < SuperSelf - end - class A C = 1 autoload :D, File.expand_path('../path_to_class.rb', __FILE__) class B end - - module M - end end end diff --git a/spec/ruby/optional/capi/fixtures/kernel.rb b/spec/ruby/optional/capi/fixtures/kernel.rb deleted file mode 100644 index d3fc7c57e8..0000000000 --- a/spec/ruby/optional/capi/fixtures/kernel.rb +++ /dev/null @@ -1,19 +0,0 @@ -class CApiKernelSpecs - class ClassWithPublicMethod - def public_method(*, **) - :public - end - end - - class ClassWithPrivateMethod - private def private_method(*, **) - :private - end - end - - class ClassWithProtectedMethod - protected def protected_method(*, **) - :protected - end - end -end diff --git a/spec/ruby/optional/capi/fixtures/module.rb b/spec/ruby/optional/capi/fixtures/module.rb index aac8bfbfb3..ba90eb7181 100644 --- a/spec/ruby/optional/capi/fixtures/module.rb +++ b/spec/ruby/optional/capi/fixtures/module.rb @@ -13,10 +13,6 @@ class CApiModuleSpecs autoload :D, File.expand_path('../const_get.rb', __FILE__) X = 1 - Q = 1 - R = 2 - S = 3 - T = 5 end class B < A diff --git a/spec/ruby/optional/capi/fixtures/object.rb b/spec/ruby/optional/capi/fixtures/object.rb deleted file mode 100644 index a59f2309d8..0000000000 --- a/spec/ruby/optional/capi/fixtures/object.rb +++ /dev/null @@ -1,29 +0,0 @@ -class CApiObjectSpecs - class IVars - def initialize - @a = 3 - @b = 7 - @c = 4 - end - - def self.set_class_variables - @@foo = :a - @@bar = :b - @@baz = :c - end - end - - module MVars - @@mvar = :foo - @@mvar2 = :bar - - @ivar = :baz - end - - module CVars - @@cvar = :foo - @@cvar2 = :bar - - @ivar = :baz - end -end diff --git a/spec/ruby/optional/capi/fixtures/read.txt b/spec/ruby/optional/capi/fixtures/read.txt deleted file mode 100644 index f7065a35d0..0000000000 --- a/spec/ruby/optional/capi/fixtures/read.txt +++ /dev/null @@ -1 +0,0 @@ -fixture file contents to test read() with RSTRING_PTR diff --git a/spec/ruby/optional/capi/float_spec.rb b/spec/ruby/optional/capi/float_spec.rb index 4b98902b59..8d709b2b82 100644 --- a/spec/ruby/optional/capi/float_spec.rb +++ b/spec/ruby/optional/capi/float_spec.rb @@ -1,4 +1,4 @@ -require_relative 'spec_helper' +require File.expand_path('../spec_helper', __FILE__) load_extension("float") @@ -27,17 +27,4 @@ describe "CApiFloatSpecs" do f.should eql(101.99) end end - - describe "RB_FLOAT_TYPE_P" do - it "returns true for floats" do - @f.RB_FLOAT_TYPE_P(2.0).should == true - end - - it "returns false for non-floats" do - @f.RB_FLOAT_TYPE_P(nil).should == false - @f.RB_FLOAT_TYPE_P(10).should == false - @f.RB_FLOAT_TYPE_P("string").should == false - @f.RB_FLOAT_TYPE_P(Object.new).should == false - end - end end diff --git a/spec/ruby/optional/capi/gc_spec.rb b/spec/ruby/optional/capi/gc_spec.rb index d9661328ab..ee9e11d11c 100644 --- a/spec/ruby/optional/capi/gc_spec.rb +++ b/spec/ruby/optional/capi/gc_spec.rb @@ -1,4 +1,4 @@ -require_relative 'spec_helper' +require File.expand_path('../spec_helper', __FILE__) load_extension("gc") @@ -7,60 +7,15 @@ describe "CApiGCSpecs" do @f = CApiGCSpecs.new end - describe "rb_gc_register_address" do - it "correctly gets the value from a registered address" do - @f.registered_tagged_address.should == 10 - @f.registered_tagged_address.should equal(@f.registered_tagged_address) - @f.registered_reference_address.should == "Globally registered data" - @f.registered_reference_address.should equal(@f.registered_reference_address) - end - - it "keeps the value alive even if the value is assigned after rb_gc_register_address() is called" do - GC.start - @f.registered_before_rb_gc_register_address.should == "registered before rb_gc_register_address()" - end - - it "can be called outside Init_" do - @f.rb_gc_register_address.should == "rb_gc_register_address() outside Init_" - @f.rb_gc_unregister_address - end - end - - describe "rb_global_variable" do - before :all do - GC.start - end - - describe "keeps the value alive even if the value is assigned after rb_global_variable() is called" do - it "for a string" do - @f.registered_before_rb_global_variable_string.should == "registered before rb_global_variable()" - end - - it "for a bignum" do - @f.registered_before_rb_global_variable_bignum.should == 2**63 - 1 - end - - it "for a Float" do - @f.registered_before_rb_global_variable_float.should == 3.14 - end - end - - describe "keeps the value alive when the value is assigned before rb_global_variable() is called" do - it "for a string" do - @f.registered_after_rb_global_variable_string.should == "registered after rb_global_variable()" - end - - it "for a bignum" do - @f.registered_after_rb_global_variable_bignum.should == 2**63 - 1 - end - - it "for a Float" do - @f.registered_after_rb_global_variable_float.should == 6.28 - end - end + it "correctly gets the value from a registered address" do + @f.registered_tagged_address.should == 10 + @f.registered_tagged_address.object_id.should == @f.registered_tagged_address.object_id + @f.registered_reference_address.should == "Globally registered data" + @f.registered_reference_address.object_id.should == @f.registered_reference_address.object_id end describe "rb_gc_enable" do + after do GC.enable end @@ -87,50 +42,13 @@ describe "CApiGCSpecs" do end describe "rb_gc" do + it "increases gc count" do gc_count = GC.count @f.rb_gc GC.count.should > gc_count end - end - - describe "rb_gc_adjust_memory_usage" do - # Just check that it does not throw, as it seems hard to observe any effect - it "adjusts the amount of registered external memory" do - -> { - @f.rb_gc_adjust_memory_usage(8) - @f.rb_gc_adjust_memory_usage(-8) - }.should_not raise_error - end - end - - describe "rb_gc_register_mark_object" do - it "can be called with an object" do - @f.rb_gc_register_mark_object(Object.new).should be_nil - end - it "keeps the value alive even if the value is not referenced by any Ruby object" do - @f.rb_gc_register_mark_object_not_referenced_float.should == 1.61 - end end - describe "rb_gc_latest_gc_info" do - it "raises a TypeError when hash or symbol not given" do - -> { @f.rb_gc_latest_gc_info("foo") }.should raise_error(TypeError) - end - - it "raises an ArgumentError when unknown symbol given" do - -> { @f.rb_gc_latest_gc_info(:unknown) }.should raise_error(ArgumentError) - end - - it "returns the populated hash when a hash is given" do - h = {} - @f.rb_gc_latest_gc_info(h).should == h - h.size.should_not == 0 - end - - it "returns a value when symbol is given" do - @f.rb_gc_latest_gc_info(:state).should be_kind_of(Symbol) - end - end end diff --git a/spec/ruby/optional/capi/globals_spec.rb b/spec/ruby/optional/capi/globals_spec.rb index 48677620bc..c6e2ed912b 100644 --- a/spec/ruby/optional/capi/globals_spec.rb +++ b/spec/ruby/optional/capi/globals_spec.rb @@ -1,4 +1,4 @@ -require_relative 'spec_helper' +require File.expand_path('../spec_helper', __FILE__) require "stringio" load_extension("globals") @@ -9,7 +9,7 @@ describe "CApiGlobalSpecs" do end it "correctly gets global values" do - suppress_warning { @f.sb_gv_get("$BLAH") }.should == nil + @f.sb_gv_get("$BLAH").should == nil @f.sb_gv_get("$\\").should == nil @f.sb_gv_get("\\").should == nil # rb_gv_get should change \ to $\ end @@ -21,7 +21,7 @@ describe "CApiGlobalSpecs" do end it "correctly sets global values" do - suppress_warning { @f.sb_gv_get("$BLAH") }.should == nil + @f.sb_gv_get("$BLAH").should == nil @f.sb_gv_set("$BLAH", 10) begin @f.sb_gv_get("$BLAH").should == 10 @@ -42,13 +42,9 @@ describe "CApiGlobalSpecs" do end it "rb_define_readonly_variable should define a new readonly global variable" do - # Check the gvar doesn't exist and ensure rb_gv_get doesn't implicitly declare the gvar, - # otherwise the rb_define_readonly_variable call will conflict. - suppress_warning { @f.sb_gv_get("ro_gvar") } .should == nil - @f.rb_define_readonly_variable("ro_gvar", 15) $ro_gvar.should == 15 - -> { $ro_gvar = 10 }.should raise_error(NameError) + lambda { $ro_gvar = 10 }.should raise_error(NameError) end it "rb_define_hooked_variable should define a C hooked global variable" do @@ -57,78 +53,13 @@ describe "CApiGlobalSpecs" do $hooked_gvar.should == 4 end - it "rb_define_hooked_variable should use default accessors if NULL ones are supplied" do - @f.rb_define_hooked_variable_default_accessors("$hooked_gvar_default_accessors") - $hooked_gvar_default_accessors = 10 - $hooked_gvar_default_accessors.should == 10 - end - - it "rb_define_hooked_variable with default accessors should return nil for NULL variables" do - @f.rb_define_hooked_variable_null_var("$hooked_gvar_null_value") - $hooked_gvar_null_value.should == nil - end - - describe "rb_define_virtual_variable" do - describe "with default accessors" do - before :all do - @f.rb_define_virtual_variable_default_accessors("$virtual_variable_default_accessors") - end - - it "is read-only" do - -> { $virtual_variable_default_accessors = 10 }.should raise_error(NameError, /read-only/) - end - - it "returns false with the default getter" do - $virtual_variable_default_accessors.should == false - $virtual_variable_default_accessors.should == false - end - end - - describe "with supplied accessors" do - before :all do - @f.rb_define_virtual_variable_incrementing_accessors("$virtual_variable_incrementing_accessors") - end - - it "returns a dynamically changing value" do - $virtual_variable_incrementing_accessors = 20 - $virtual_variable_incrementing_accessors.should == 20 - $virtual_variable_incrementing_accessors.should == 21 - $virtual_variable_incrementing_accessors.should == 22 - - $virtual_variable_incrementing_accessors = 100 - $virtual_variable_incrementing_accessors.should == 100 - $virtual_variable_incrementing_accessors.should == 101 - $virtual_variable_incrementing_accessors.should == 102 - end - end - end - - describe "rb_fs" do - before :each do - @field_separator = $; - end - - after :each do - suppress_warning { $; = @field_separator } - end - - it "returns nil by default" do - @f.rb_fs.should == nil - end - - it "returns the value of $;" do - suppress_warning { $; = "foo" } - @f.rb_fs.should == "foo" - end - end - describe "rb_rs" do before :each do @dollar_slash = $/ end after :each do - suppress_warning { $/ = @dollar_slash } + $/ = @dollar_slash end it "returns \\n by default" do @@ -136,7 +67,7 @@ describe "CApiGlobalSpecs" do end it "returns the value of $/" do - suppress_warning { $/ = "foo" } + $/ = "foo" @f.rb_rs.should == "foo" end end @@ -190,7 +121,7 @@ describe "CApiGlobalSpecs" do $stdout = STDOUT end - it "is an alias of rb_stdout" do + it "returns $stdout" do $stdout = @stream @f.rb_defout.should equal($stdout) end @@ -209,7 +140,7 @@ describe "CApiGlobalSpecs" do end after :each do - suppress_warning {$\ = @dollar_backslash} + $\ = @dollar_backslash end it "returns nil by default" do @@ -217,7 +148,7 @@ describe "CApiGlobalSpecs" do end it "returns the value of $\\" do - suppress_warning {$\ = "foo"} + $\ = "foo" @f.rb_output_rs.should == "foo" end end @@ -228,7 +159,7 @@ describe "CApiGlobalSpecs" do end after :each do - suppress_warning {$, = @dollar_comma} + $, = @dollar_comma end it "returns nil by default" do @@ -236,7 +167,7 @@ describe "CApiGlobalSpecs" do end it "returns the value of $\\" do - suppress_warning {$, = "foo"} + $, = "foo" @f.rb_output_fs.should == "foo" end end diff --git a/spec/ruby/optional/capi/hash_spec.rb b/spec/ruby/optional/capi/hash_spec.rb index 08e03fcb26..1c406f0394 100644 --- a/spec/ruby/optional/capi/hash_spec.rb +++ b/spec/ruby/optional/capi/hash_spec.rb @@ -1,5 +1,4 @@ -require_relative 'spec_helper' -require_relative '../../shared/hash/key_error' +require File.expand_path('../spec_helper', __FILE__) load_extension("hash") @@ -21,7 +20,7 @@ describe "C-API Hash function" do # The actual conversion is an implementation detail. # We only care that ultimately we get a Fixnum instance. - @s.rb_hash(obj).should.between?(fixnum_min, fixnum_max) + @s.rb_hash(obj).should be_an_instance_of(Fixnum) end it "calls #to_int to converts a value returned by #hash to a Fixnum" do @@ -36,7 +35,7 @@ describe "C-API Hash function" do obj = mock("rb_hash no to_int") obj.should_receive(:hash).and_return(nil) - -> { @s.rb_hash(obj) }.should raise_error(TypeError) + lambda { @s.rb_hash(obj) }.should raise_error(TypeError) end end @@ -50,30 +49,6 @@ describe "C-API Hash function" do end end - ruby_version_is '3.2' do - describe "rb_hash_new_capa" do - it "returns a new hash" do - @s.rb_hash_new_capa(3).should == {} - end - - it "creates a hash with no default proc" do - @s.rb_hash_new_capa(3) {}.default_proc.should be_nil - end - - it "raises RuntimeError when negative index is provided" do - -> { @s.rb_hash_new_capa(-1) }.should raise_error(RuntimeError, "st_table too big") - end - end - end - - describe "rb_ident_hash_new" do - it "returns a new compare by identity hash" do - result = @s.rb_ident_hash_new - result.should == {} - result.compare_by_identity?.should == true - end - end - describe "rb_hash_dup" do it "returns a copy of the hash" do hsh = {} @@ -155,17 +130,11 @@ describe "C-API Hash function" do it "raises a KeyError if the key is not found and default is set" do @hsh.default = :d - -> { @s.rb_hash_fetch(@hsh, :c) }.should raise_error(KeyError) + lambda { @s.rb_hash_fetch(@hsh, :c) }.should raise_error(KeyError) end it "raises a KeyError if the key is not found and no default is set" do - -> { @s.rb_hash_fetch(@hsh, :c) }.should raise_error(KeyError) - end - - context "when key is not found" do - it_behaves_like :key_error, -> obj, key { - @s.rb_hash_fetch(obj, key) - }, { a: 1 } + lambda { @s.rb_hash_fetch(@hsh, :c) }.should raise_error(KeyError) end end @@ -194,61 +163,6 @@ describe "C-API Hash function" do end end - describe "rb_hash_bulk_insert" do - it 'inserts key-value pairs into the hash' do - arr = [:a, 1, :b, 2, :c, 3] - hash = {} - - @s.rb_hash_bulk_insert(arr.length, arr, hash) - - hash.should == {a: 1, b: 2, c: 3} - end - - it 'overwrites existing keys' do - arr = [:a, 4, :b, 5, :c, 6] - hash = {a: 1, b: 2} - - @s.rb_hash_bulk_insert(arr.length, arr, hash) - - hash.should == {a: 4, b: 5, c: 6} - end - - it 'uses the last key in the array if it appears multiple times' do - arr = [:a, 1, :b, 2, :a, 3] - hash = {} - - @s.rb_hash_bulk_insert(arr.length, arr, hash) - - hash.should == {a: 3, b: 2} - end - - it 'allows the array to be NULL if the length is zero' do - hash = {} - - @s.rb_hash_bulk_insert(0, nil, hash) - - hash.should == {} - end - - it 'does not include any keys after the given length' do - arr = [:a, 1, :b, 2, :c, 3, :d, 4] - hash = {} - - @s.rb_hash_bulk_insert(arr.length - 2, arr, hash) - - hash.should == {a: 1, b: 2, c: 3} - end - - it 'does not modify the hash if the length is zero' do - arr = [] - hash = {a: 1, b: 2} - - @s.rb_hash_bulk_insert(arr.length, arr, hash) - - hash.should == {a: 1, b: 2} - end - end - describe "rb_hash_size" do it "returns the size of the hash" do hsh = {fast: 'car', good: 'music'} @@ -290,11 +204,6 @@ describe "C-API Hash function" do @s.rb_hash_lookup2(hash, :chunky, 10).should == 10 end - - it "returns undefined if that is the default value specified" do - hsh = Hash.new(0) - @s.rb_hash_lookup2_default_undef(hsh, :chunky).should be_true - end end end @@ -324,22 +233,13 @@ describe "C-API Hash function" do end it "raises a TypeError if the argument does not respond to #to_hash" do - -> { @s.rb_Hash(42) }.should raise_error(TypeError) + lambda { @s.rb_Hash(42) }.should raise_error(TypeError) end it "raises a TypeError if #to_hash does not return a hash" do h = BasicObject.new def h.to_hash; 42; end - -> { @s.rb_Hash(h) }.should raise_error(TypeError) - end - end - - describe "hash code functions" do - it "computes a deterministic number" do - hash_code = @s.compute_a_hash_code(53) - hash_code.should be_an_instance_of(Integer) - hash_code.should == @s.compute_a_hash_code(53) - @s.compute_a_hash_code(90).should == @s.compute_a_hash_code(90) + lambda { @s.rb_Hash(h) }.should raise_error(TypeError) end end end diff --git a/spec/ruby/optional/capi/integer_spec.rb b/spec/ruby/optional/capi/integer_spec.rb index 089872381c..9a660cdb5c 100644 --- a/spec/ruby/optional/capi/integer_spec.rb +++ b/spec/ruby/optional/capi/integer_spec.rb @@ -1,5 +1,5 @@ # -*- encoding: binary -*- -require_relative 'spec_helper' +require File.expand_path('../spec_helper', __FILE__) load_extension("integer") @@ -140,23 +140,6 @@ describe "CApiIntegerSpecs" do result.should == -1 @words.should == "\x11\x32\x54\x76\x98\xBA\xDC\xFE" end - - it "converts numbers near the fixnum limit successfully" do - result = @s.rb_integer_pack(0x7123_4567_89ab_cdef, @words, 1, 8, 0, - CApiIntegerSpecs::NATIVE|CApiIntegerSpecs::PACK_2COMP) - result.should == 1 - @words.should == "\xEF\xCD\xAB\x89\x67\x45\x23\x71" - - result = @s.rb_integer_pack(2**62-1, @words, 1, 8, 0, - CApiIntegerSpecs::NATIVE|CApiIntegerSpecs::PACK_2COMP) - result.should == 1 - @words.should == "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x3F" - - result = @s.rb_integer_pack(2**63-1, @words, 1, 8, 0, - CApiIntegerSpecs::NATIVE|CApiIntegerSpecs::PACK_2COMP) - result.should == 1 - @words.should == "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F" - end end end end @@ -289,19 +272,4 @@ describe "CApiIntegerSpecs" do end end end - - describe "rb_int_positive_pow" do - it "raises an integer to given power" do - @s.rb_int_positive_pow(2, 3).should == 8 - end - - it "raises a negative integer to given power" do - @s.rb_int_positive_pow(-2, 3).should == -8 - @s.rb_int_positive_pow(-2, 4).should == 16 - end - - it "overflows for large inputs" do - @s.rb_int_positive_pow(8, 23).should == 590295810358705651712 - end - end end diff --git a/spec/ruby/optional/capi/io_spec.rb b/spec/ruby/optional/capi/io_spec.rb index dbfaf556f6..92bc2da54e 100644 --- a/spec/ruby/optional/capi/io_spec.rb +++ b/spec/ruby/optional/capi/io_spec.rb @@ -1,5 +1,4 @@ -require_relative 'spec_helper' -require_relative '../../fixtures/io' +require File.expand_path('../spec_helper', __FILE__) load_extension('io') @@ -10,7 +9,7 @@ describe "C-API IO function" do @name = tmp("c_api_rb_io_specs") touch @name - @io = new_io @name, "w:utf-8" + @io = new_io @name, fmode("w:utf-8") @io.sync = true end @@ -114,7 +113,7 @@ describe "C-API IO function" do @name = tmp("c_api_io_specs") touch @name - @io = new_io @name, "r:utf-8" + @io = new_io @name, fmode("r:utf-8") end after :each do @@ -143,22 +142,12 @@ describe "C-API IO function" do describe "rb_io_check_closed" do it "does not raise an exception if the IO is not closed" do # The MRI function is void, so we use should_not raise_error - -> { @o.rb_io_check_closed(@io) }.should_not raise_error + lambda { @o.rb_io_check_closed(@io) }.should_not raise_error end it "raises an error if the IO is closed" do @io.close - -> { @o.rb_io_check_closed(@io) }.should raise_error(IOError) - end - end - - describe "rb_io_set_nonblock" do - platform_is_not :windows do - it "returns true when nonblock flag is set" do - require 'io/nonblock' - @o.rb_io_set_nonblock(@io) - @io.nonblock?.should be_true - end + lambda { @o.rb_io_check_closed(@io) }.should raise_error(IOError) end end @@ -166,28 +155,23 @@ describe "C-API IO function" do # object is frozen, *not* if it's tainted. describe "rb_io_taint_check" do it "does not raise an exception if the IO is not frozen" do - -> { @o.rb_io_taint_check(@io) }.should_not raise_error + lambda { @o.rb_io_taint_check(@io) }.should_not raise_error end it "raises an exception if the IO is frozen" do @io.freeze - -> { @o.rb_io_taint_check(@io) }.should raise_error(RuntimeError) + lambda { @o.rb_io_taint_check(@io) }.should raise_error(RuntimeError) end end - describe "rb_io_descriptor or GetOpenFile" do + describe "GetOpenFile" do it "allows access to the system fileno" do @o.GetOpenFile_fd($stdin).should == 0 @o.GetOpenFile_fd($stdout).should == 1 @o.GetOpenFile_fd($stderr).should == 2 @o.GetOpenFile_fd(@io).should == @io.fileno end - - it "raises IOError if the IO is closed" do - @io.close - -> { @o.GetOpenFile_fd(@io) }.should raise_error(IOError, "closed stream") - end end describe "rb_io_binmode" do @@ -209,7 +193,7 @@ describe "C-API IO function" do @name = tmp("c_api_io_specs") touch @name - @rw_io = new_io @name, "w+" + @rw_io = new_io @name, fmode("w+") end after :each do @@ -222,80 +206,42 @@ describe "C-API IO function" do describe "rb_io_check_readable" do it "does not raise an exception if the IO is opened for reading" do # The MRI function is void, so we use should_not raise_error - -> { @o.rb_io_check_readable(@r_io) }.should_not raise_error + lambda { @o.rb_io_check_readable(@r_io) }.should_not raise_error end it "does not raise an exception if the IO is opened for read and write" do - -> { @o.rb_io_check_readable(@rw_io) }.should_not raise_error + lambda { @o.rb_io_check_readable(@rw_io) }.should_not raise_error end it "raises an IOError if the IO is not opened for reading" do - -> { @o.rb_io_check_readable(@w_io) }.should raise_error(IOError) + lambda { @o.rb_io_check_readable(@w_io) }.should raise_error(IOError) end end describe "rb_io_check_writable" do - it "does not raise an exception if the IO is opened for writing" do + it "does not raise an exeption if the IO is opened for writing" do # The MRI function is void, so we use should_not raise_error - -> { @o.rb_io_check_writable(@w_io) }.should_not raise_error + lambda { @o.rb_io_check_writable(@w_io) }.should_not raise_error end it "does not raise an exception if the IO is opened for read and write" do - -> { @o.rb_io_check_writable(@rw_io) }.should_not raise_error + lambda { @o.rb_io_check_writable(@rw_io) }.should_not raise_error end it "raises an IOError if the IO is not opened for reading" do - -> { @o.rb_io_check_writable(@r_io) }.should raise_error(IOError) + lambda { @o.rb_io_check_writable(@r_io) }.should raise_error(IOError) end end describe "rb_io_wait_writable" do it "returns false if there is no error condition" do - @o.errno = 0 @o.rb_io_wait_writable(@w_io).should be_false end it "raises an IOError if the IO is closed" do @w_io.close - -> { @o.rb_io_wait_writable(@w_io) }.should raise_error(IOError) - end - end - - ruby_version_is "3.1" do - describe "rb_io_maybe_wait_writable" do - it "returns mask for events if operation was interrupted" do - @o.rb_io_maybe_wait_writable(Errno::EINTR::Errno, @w_io, nil).should == IO::WRITABLE - end - - it "returns 0 if there is no error condition" do - @o.rb_io_maybe_wait_writable(0, @w_io, nil).should == 0 - end - - it "raises an IOError if the IO is closed" do - @w_io.close - -> { @o.rb_io_maybe_wait_writable(0, @w_io, nil) }.should raise_error(IOError, "closed stream") - end - - it "raises an IOError if the IO is not initialized" do - -> { @o.rb_io_maybe_wait_writable(0, IO.allocate, nil) }.should raise_error(IOError, "uninitialized stream") - end - - it "can be interrupted" do - IOSpec.exhaust_write_buffer(@w_io) - start = Process.clock_gettime(Process::CLOCK_MONOTONIC) - - t = Thread.new do - @o.rb_io_maybe_wait_writable(0, @w_io, 10) - end - - Thread.pass until t.stop? - t.kill - t.join - - finish = Process.clock_gettime(Process::CLOCK_MONOTONIC) - (finish - start).should < 9 - end + lambda { @o.rb_io_wait_writable(@w_io) }.should raise_error(IOError) end end @@ -305,31 +251,15 @@ describe "C-API IO function" do end end - describe "rb_thread_fd_select" do - it "waits until an fd is ready for reading" do - @w_io.write "rb_thread_fd_select" - @o.rb_thread_fd_select_read(@r_io).should == 1 - end - - it "waits until an fd is ready for writing" do - @o.rb_thread_fd_select_write(@w_io).should == 1 - end - - it "waits until an fd is ready for writing with timeout" do - @o.rb_thread_fd_select_timeout(@w_io).should == 1 - end - end - platform_is_not :windows do describe "rb_io_wait_readable" do it "returns false if there is no error condition" do - @o.errno = 0 @o.rb_io_wait_readable(@r_io, false).should be_false end it "raises and IOError if passed a closed stream" do @r_io.close - -> { + lambda { @o.rb_io_wait_readable(@r_io, false) }.should raise_error(IOError) end @@ -341,62 +271,12 @@ describe "C-API IO function" do @w_io.write "rb_io_wait_readable" end - @o.errno = Errno::EAGAIN.new.errno @o.rb_io_wait_readable(@r_io, true).should be_true @o.instance_variable_get(:@read_data).should == "rb_io_wait_re" thr.join end end - - ruby_version_is "3.1" do - describe "rb_io_maybe_wait_readable" do - it "returns mask for events if operation was interrupted" do - @o.rb_io_maybe_wait_readable(Errno::EINTR::Errno, @r_io, nil, false).should == IO::READABLE - end - - it "returns 0 if there is no error condition" do - @o.rb_io_maybe_wait_readable(0, @r_io, nil, false).should == 0 - end - - it "blocks until the io is readable and returns events that actually occurred" do - @o.instance_variable_set :@write_data, false - thr = Thread.new do - Thread.pass until @o.instance_variable_get(:@write_data) - @w_io.write "rb_io_wait_readable" - end - - @o.rb_io_maybe_wait_readable(Errno::EAGAIN::Errno, @r_io, IO::READABLE, true).should == IO::READABLE - @o.instance_variable_get(:@read_data).should == "rb_io_wait_re" - - thr.join - end - - it "can be interrupted" do - start = Process.clock_gettime(Process::CLOCK_MONOTONIC) - - t = Thread.new do - @o.rb_io_maybe_wait_readable(0, @r_io, 10, false) - end - - Thread.pass until t.stop? - t.kill - t.join - - finish = Process.clock_gettime(Process::CLOCK_MONOTONIC) - (finish - start).should < 9 - end - - it "raises an IOError if the IO is closed" do - @r_io.close - -> { @o.rb_io_maybe_wait_readable(0, @r_io, nil, false) }.should raise_error(IOError, "closed stream") - end - - it "raises an IOError if the IO is not initialized" do - -> { @o.rb_io_maybe_wait_readable(0, IO.allocate, nil, false) }.should raise_error(IOError, "uninitialized stream") - end - end - end end describe "rb_thread_wait_fd" do @@ -416,271 +296,17 @@ describe "C-API IO function" do end end - describe "rb_wait_for_single_fd" do - it "waits til an fd is ready for reading" do - start = false - thr = Thread.new do - start = true - sleep 0.05 - @w_io.write "rb_io_wait_readable" - end - - Thread.pass until start - - @o.rb_wait_for_single_fd(@r_io, 1, nil, nil).should == 1 - - thr.join - end - - it "polls whether an fd is ready for reading if timeout is 0" do - @o.rb_wait_for_single_fd(@r_io, 1, 0, 0).should == 0 - end - end - - ruby_version_is "3.1" do - describe "rb_io_maybe_wait" do - it "waits til an fd is ready for reading" do - start = false - thr = Thread.new do - start = true - sleep 0.05 - @w_io.write "rb_io_maybe_wait" - end - - Thread.pass until start - - @o.rb_io_maybe_wait(Errno::EAGAIN::Errno, @r_io, IO::READABLE, nil).should == IO::READABLE - - thr.join - end - - it "returns mask for events if operation was interrupted" do - @o.rb_io_maybe_wait(Errno::EINTR::Errno, @w_io, IO::WRITABLE, nil).should == IO::WRITABLE - end - - it "raises an IOError if the IO is closed" do - @w_io.close - -> { @o.rb_io_maybe_wait(0, @w_io, IO::WRITABLE, nil) }.should raise_error(IOError, "closed stream") - end - - it "raises an IOError if the IO is not initialized" do - -> { @o.rb_io_maybe_wait(0, IO.allocate, IO::WRITABLE, nil) }.should raise_error(IOError, "uninitialized stream") - end - - it "can be interrupted when waiting for READABLE event" do - start = Process.clock_gettime(Process::CLOCK_MONOTONIC) - - t = Thread.new do - @o.rb_io_maybe_wait(0, @r_io, IO::READABLE, 10) - end - - Thread.pass until t.stop? - t.kill - t.join - - finish = Process.clock_gettime(Process::CLOCK_MONOTONIC) - (finish - start).should < 9 - end - - it "can be interrupted when waiting for WRITABLE event" do - IOSpec.exhaust_write_buffer(@w_io) - start = Process.clock_gettime(Process::CLOCK_MONOTONIC) - - t = Thread.new do - @o.rb_io_maybe_wait(0, @w_io, IO::WRITABLE, 10) - end - - Thread.pass until t.stop? - t.kill - t.join - - finish = Process.clock_gettime(Process::CLOCK_MONOTONIC) - (finish - start).should < 9 - end - end - end - - ruby_version_is "3.3" do - describe "rb_io_mode" do - it "returns the mode" do - (@o.rb_io_mode(@r_io) & 0b11).should == 0b01 - (@o.rb_io_mode(@w_io) & 0b11).should == 0b10 - (@o.rb_io_mode(@rw_io) & 0b11).should == 0b11 - end - end - - describe "rb_io_path" do - it "returns the IO#path" do - @o.rb_io_path(@r_io).should == @r_io.path - @o.rb_io_path(@rw_io).should == @rw_io.path - @o.rb_io_path(@rw_io).should == @name - end - end - - describe "rb_io_closed_p" do - it "returns false when io is not closed" do - @o.rb_io_closed_p(@r_io).should == false - @r_io.closed?.should == false - end - - it "returns true when io is closed" do - @r_io.close - - @o.rb_io_closed_p(@r_io).should == true - @r_io.closed?.should == true - end - end - - quarantine! do # "Errno::EBADF: Bad file descriptor" at closing @r_io, @rw_io etc in the after :each hook - describe "rb_io_open_descriptor" do - it "creates a new IO instance" do - io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {}) - io.should.is_a?(IO) - end - - it "return an instance of the specified class" do - io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {}) - io.class.should == File - - io = @o.rb_io_open_descriptor(IO, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {}) - io.class.should == IO - end - - it "sets the specified file descriptor" do - io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {}) - io.fileno.should == @r_io.fileno - end - - it "sets the specified path" do - io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {}) - io.path.should == "a.txt" - end - - it "sets the specified mode" do - io = @o.rb_io_open_descriptor(File, @r_io.fileno, CApiIOSpecs::FMODE_BINMODE, "a.txt", 60, "US-ASCII", "UTF-8", 0, {}) - io.should.binmode? - - io = @o.rb_io_open_descriptor(File, @r_io.fileno, CApiIOSpecs::FMODE_TEXTMODE, "a.txt", 60, "US-ASCII", "UTF-8", 0, {}) - io.should_not.binmode? - end - - it "sets the specified timeout" do - io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {}) - io.timeout.should == 60 - end - - it "sets the specified internal encoding" do - io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {}) - io.internal_encoding.should == Encoding::US_ASCII - end - - it "sets the specified external encoding" do - io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {}) - io.external_encoding.should == Encoding::UTF_8 - end - - it "does not apply the specified encoding flags" do - name = tmp("rb_io_open_descriptor_specs") - File.write(name, "123\r\n456\n89") - file = File.open(name, "r") - - io = @o.rb_io_open_descriptor(File, file.fileno, CApiIOSpecs::FMODE_READABLE, "a.txt", 60, "US-ASCII", "UTF-8", CApiIOSpecs::ECONV_UNIVERSAL_NEWLINE_DECORATOR, {}) - io.read_nonblock(20).should == "123\r\n456\n89" - ensure - file.close - rm_r name - end - - it "ignores the IO open options" do - io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {external_encoding: "windows-1251"}) - io.external_encoding.should == Encoding::UTF_8 - - io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {internal_encoding: "windows-1251"}) - io.internal_encoding.should == Encoding::US_ASCII - - io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {encoding: "windows-1251:binary"}) - io.external_encoding.should == Encoding::UTF_8 - io.internal_encoding.should == Encoding::US_ASCII - - io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {textmode: false}) - io.should_not.binmode? - - io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {binmode: true}) - io.should_not.binmode? - - io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {autoclose: false}) - io.should.autoclose? - - io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, "a.txt", 60, "US-ASCII", "UTF-8", 0, {path: "a.txt"}) - io.path.should == "a.txt" - end - - it "ignores the IO encoding options" do - io = @o.rb_io_open_descriptor(File, @w_io.fileno, CApiIOSpecs::FMODE_WRITABLE, "a.txt", 60, "US-ASCII", "UTF-8", 0, {crlf_newline: true}) - - io.write("123\r\n456\n89") - io.flush - - @r_io.read_nonblock(20).should == "123\r\n456\n89" - end - - it "allows wrong mode" do - io = @o.rb_io_open_descriptor(File, @w_io.fileno, CApiIOSpecs::FMODE_READABLE, "a.txt", 60, "US-ASCII", "UTF-8", 0, {}) - io.should.is_a?(File) - - platform_is_not :windows do - -> { io.read_nonblock(1) }.should raise_error(Errno::EBADF) - end - - platform_is :windows do - -> { io.read_nonblock(1) }.should raise_error(IO::EWOULDBLOCKWaitReadable) - end - end - - it "tolerates NULL as rb_io_encoding *encoding parameter" do - io = @o.rb_io_open_descriptor_without_encoding(File, @r_io.fileno, 0, "a.txt", 60) - io.should.is_a?(File) - end - - it "deduplicates path String" do - path = "a.txt".dup - io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, path, 60, "US-ASCII", "UTF-8", 0, {}) - io.path.should_not equal(path) - - path = "a.txt".freeze - io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, path, 60, "US-ASCII", "UTF-8", 0, {}) - io.path.should_not equal(path) - end - - it "calls #to_str to convert a path to a String" do - path = Object.new - def path.to_str; "a.txt"; end - - io = @o.rb_io_open_descriptor(File, @r_io.fileno, 0, path, 60, "US-ASCII", "UTF-8", 0, {}) - - io.path.should == "a.txt" - end - end - end - end - - ruby_version_is "3.4" do - describe "rb_io_maybe_wait" do - it "returns nil if there is no error condition" do - @o.rb_io_maybe_wait(0, @w_io, IO::WRITABLE, nil).should == nil - end - end - end end describe "rb_fd_fix_cloexec" do + before :each do @o = CApiIOSpecs.new @name = tmp("c_api_rb_io_specs") touch @name - @io = new_io @name, "w:utf-8" + @io = new_io @name, fmode("w:utf-8") @io.close_on_exec = false @io.sync = true end @@ -716,29 +342,3 @@ describe "rb_cloexec_open" do @io.close_on_exec?.should be_true end end - -describe "rb_io_t modes flags" do - before :each do - @o = CApiIOSpecs.new - @name = tmp("c_api_rb_io_specs") - touch @name - end - - after :each do - rm_r @name - end - - it "has the sync flag set if the IO object is synced in Ruby" do - File.open(@name) { |io| - io.sync = true - @o.rb_io_mode_sync_flag(io).should == true - } - end - - it "has the sync flag unset if the IO object is not synced in Ruby" do - File.open(@name) { |io| - io.sync = false - @o.rb_io_mode_sync_flag(io).should == false - } - end -end diff --git a/spec/ruby/optional/capi/kernel_spec.rb b/spec/ruby/optional/capi/kernel_spec.rb index c90e50f1db..73928b3154 100644 --- a/spec/ruby/optional/capi/kernel_spec.rb +++ b/spec/ruby/optional/capi/kernel_spec.rb @@ -1,24 +1,15 @@ -require_relative 'spec_helper' -require_relative 'fixtures/kernel' +require File.expand_path('../spec_helper', __FILE__) -kernel_path = load_extension("kernel") - -class CApiKernelSpecs::Exc < StandardError -end -exception_class = CApiKernelSpecs::Exc +load_extension("kernel") describe "C-API Kernel function" do before :each do @s = CApiKernelSpecs.new end - after :each do - @s.rb_errinfo.should == nil - end - describe "rb_block_given_p" do it "returns false if no block is passed" do - @s.should_not.rb_block_given_p + @s.rb_block_given_p.should == false end it "returns true if a block is passed" do @@ -28,7 +19,7 @@ describe "C-API Kernel function" do describe "rb_need_block" do it "raises a LocalJumpError if no block is given" do - -> { @s.rb_need_block }.should raise_error(LocalJumpError) + lambda { @s.rb_need_block }.should raise_error(LocalJumpError) end it "does not raise a LocalJumpError if a block is given" do @@ -62,46 +53,18 @@ describe "C-API Kernel function" do i + 1 end.should == [2, 4, 6] end - - it "can pass extra data to the function" do - ary = [3] - @s.rb_block_call_extra_data(ary).should equal(ary) - end - end - - describe "rb_frame_this_func" do - it "returns the name of the method called" do - @s.rb_frame_this_func_test.should == :rb_frame_this_func_test - @s.rb_frame_this_func_test_again.should == :rb_frame_this_func_test_again - end end describe "rb_raise" do it "raises an exception" do - -> { @s.rb_raise({}) }.should raise_error(TypeError) + lambda { @s.rb_raise({}) }.should raise_error(TypeError) end it "terminates the function at the point it was called" do h = {} - -> { @s.rb_raise(h) }.should raise_error(TypeError) + lambda { @s.rb_raise(h) }.should raise_error(TypeError) h[:stage].should == :before end - - it "re-raises a rescued exception" do - -> do - begin - raise StandardError, "aaa" - rescue Exception - begin - @s.rb_raise({}) - rescue TypeError - end - - # should raise StandardError "aaa" - raise - end - end.should raise_error(StandardError, "aaa") - end end describe "rb_throw" do @@ -125,7 +88,7 @@ describe "C-API Kernel function" do end it "raises an ArgumentError if there is no catch block for the symbol" do - -> { @s.rb_throw(nil) }.should raise_error(ArgumentError) + lambda { @s.rb_throw(nil) }.should raise_error(ArgumentError) end end @@ -151,33 +114,43 @@ describe "C-API Kernel function" do end it "raises an ArgumentError if there is no catch block for the symbol" do - -> { @s.rb_throw(nil) }.should raise_error(ArgumentError) + lambda { @s.rb_throw(nil) }.should raise_error(ArgumentError) end end describe "rb_warn" do + before :each do + @stderr, $stderr = $stderr, IOStub.new + @verbose = $VERBOSE + end + + after :each do + $stderr = @stderr + $VERBOSE = @verbose + end + it "prints a message to $stderr if $VERBOSE evaluates to true" do - -> { - @s.rb_warn("This is a warning") - }.should complain(/warning: This is a warning/, verbose: true) + $VERBOSE = true + @s.rb_warn("This is a warning") + $stderr.should =~ /This is a warning/ end it "prints a message to $stderr if $VERBOSE evaluates to false" do - -> { - @s.rb_warn("This is a warning") - }.should complain(/warning: This is a warning/, verbose: false) + $VERBOSE = false + @s.rb_warn("This is a warning") + $stderr.should =~ /This is a warning/ end end describe "rb_sys_fail" do it "raises an exception from the value of errno" do - -> do + lambda do @s.rb_sys_fail("additional info") end.should raise_error(SystemCallError, /additional info/) end it "can take a NULL message" do - -> do + lambda do @s.rb_sys_fail(nil) end.should raise_error(Errno::EPERM) end @@ -185,49 +158,15 @@ describe "C-API Kernel function" do describe "rb_syserr_fail" do it "raises an exception from the given error" do - -> do + lambda do @s.rb_syserr_fail(Errno::EINVAL::Errno, "additional info") - end.should raise_error(Errno::EINVAL, "Invalid argument - additional info") + end.should raise_error(Errno::EINVAL, /additional info/) end it "can take a NULL message" do - -> do + lambda do @s.rb_syserr_fail(Errno::EINVAL::Errno, nil) - end.should raise_error(Errno::EINVAL, "Invalid argument") - end - - it "uses an 'unknown error' message when errno is unknown" do - platform_is_not :windows do - -> { @s.rb_syserr_fail(-10, nil) }.should raise_error(SystemCallError, /Unknown error(:)? -10/) - end - - platform_is :windows do - -> { @s.rb_syserr_fail(-1, nil) }.should raise_error(SystemCallError, "The operation completed successfully.") - end - end - end - - describe "rb_syserr_fail_str" do - it "raises an exception from the given error" do - -> do - @s.rb_syserr_fail_str(Errno::EINVAL::Errno, "additional info") - end.should raise_error(Errno::EINVAL, "Invalid argument - additional info") - end - - it "can take nil as a message" do - -> do - @s.rb_syserr_fail_str(Errno::EINVAL::Errno, nil) - end.should raise_error(Errno::EINVAL, "Invalid argument") - end - - it "uses an 'unknown error' message when errno is unknown" do - platform_is_not :windows do - -> { @s.rb_syserr_fail_str(-10, nil) }.should raise_error(SystemCallError, /Unknown error(:)? -10/) - end - - platform_is :windows do - -> { @s.rb_syserr_fail_str(-1, nil) }.should raise_error(SystemCallError, "The operation completed successfully.") - end + end.should raise_error(Errno::EINVAL) end end @@ -243,7 +182,7 @@ describe "C-API Kernel function" do end it "raises LocalJumpError when no block is given" do - -> { @s.rb_yield(1) }.should raise_error(LocalJumpError) + lambda { @s.rb_yield(1) }.should raise_error(LocalJumpError) end it "rb_yield to a block that breaks does not raise an error" do @@ -283,19 +222,7 @@ describe "C-API Kernel function" do end it "raises LocalJumpError when no block is given" do - -> { @s.rb_yield_splat([1, 2]) }.should raise_error(LocalJumpError) - end - end - - describe "rb_yield_values2" do - it "yields passed arguments" do - ret = nil - @s.rb_yield_values2([1, 2]) { |x, y| ret = x + y } - ret.should == 3 - end - - it "returns the result from block evaluation" do - @s.rb_yield_values2([1, 2]) { |x, y| x + y }.should == 3 + lambda { @s.rb_yield_splat([1, 2]) }.should raise_error(LocalJumpError) end end @@ -310,197 +237,109 @@ describe "C-API Kernel function" do @s.rb_yield_splat([1, 2]) { |x, y| x + y }.should == 3 end - it "passes arguments to a block accepting splatted args" do - @s.rb_yield_splat([1, 2]) { |*v| v }.should == [1, 2] - end - it "raises LocalJumpError when no block is given" do - -> { @s.rb_yield_splat([1, 2]) }.should raise_error(LocalJumpError) + lambda { @s.rb_yield_splat([1, 2]) }.should raise_error(LocalJumpError) end end describe "rb_protect" do it "will run a function with an argument" do proof = [] # Hold proof of work performed after the yield. - res = @s.rb_protect_yield(77, proof) { |x| x + 1 } - res.should == 78 + res = @s.rb_protect_yield(7, proof) { |x| x + 1 } + res.should == 8 proof[0].should == 23 end it "will allow cleanup code to run after break" do proof = [] # Hold proof of work performed after the yield. - @s.rb_protect_yield(77, proof) { |x| break } + @s.rb_protect_yield(7, proof) { |x| break } proof[0].should == 23 end it "will allow cleanup code to run after break with value" do proof = [] # Hold proof of work performed after the yield. - res = @s.rb_protect_yield(77, proof) { |x| break x + 1 } - res.should == 78 + res = @s.rb_protect_yield(7, proof) { |x| break x + 1 } + res.should == 8 proof[0].should == 23 end it "will allow cleanup code to run after a raise" do proof = [] # Hold proof of work performed after the yield. - -> do - @s.rb_protect_yield(77, proof) { |x| raise NameError } + lambda do + @s.rb_protect_yield(7, proof) { |x| raise NameError} end.should raise_error(NameError) proof[0].should == 23 end - - it "will return nil if an error was raised" do - proof = [] # Hold proof of work performed after the yield. - -> do - @s.rb_protect_yield(77, proof) { |x| raise NameError } - end.should raise_error(NameError) - proof[0].should == 23 - proof[1].should == nil - end - - it "accepts NULL as status and returns nil if it failed" do - @s.rb_protect_null_status(42) { |x| x + 1 }.should == 43 - @s.rb_protect_null_status(42) { |x| raise NameError }.should == nil - @s.rb_errinfo().should.is_a? NameError - ensure - @s.rb_set_errinfo(nil) - end - - it "populates rb_errinfo() with the captured exception" do - proof = [] - @s.rb_protect_ignore_status(77, proof) { |x| raise NameError } - @s.rb_errinfo().should.is_a? NameError - # Note: on CRuby $! is the NameError here, but not clear if that is desirable or bug - proof[0].should == 23 - proof[1].should == nil - ensure - @s.rb_set_errinfo(nil) - end - - end - - describe "rb_eval_string_protect" do - it "will evaluate the given string" do - proof = [] - res = @s.rb_eval_string_protect('1 + 7', proof) - proof.should == [23, 8] - end - - it "will allow cleanup code to be run when an exception is raised" do - proof = [] - -> do - @s.rb_eval_string_protect('raise RuntimeError', proof) - end.should raise_error(RuntimeError) - proof.should == [23, nil] - end end describe "rb_rescue" do before :each do - @proc = -> x { x } - @rescue_proc_returns_sentinel = -> *_ { :rescue_proc_executed } - @rescue_proc_returns_arg = -> *a { a } - @arg_error_proc = -> *_ { raise ArgumentError, '' } - @std_error_proc = -> *_ { raise StandardError, '' } - @exc_error_proc = -> *_ { raise Exception, '' } + @proc = lambda { |x| x } + @raise_proc_returns_sentinel = lambda {|*_| :raise_proc_executed } + @raise_proc_returns_arg = lambda {|*a| a } + @arg_error_proc = lambda { |*_| raise ArgumentError, '' } + @std_error_proc = lambda { |*_| raise StandardError, '' } + @exc_error_proc = lambda { |*_| raise Exception, '' } end it "executes passed function" do - @s.rb_rescue(@proc, :no_exc, @rescue_proc_returns_arg, :exc).should == :no_exc + @s.rb_rescue(@proc, :no_exc, @raise_proc_returns_arg, :exc).should == :no_exc end - it "executes the passed 'rescue function' if a StandardError exception is raised" do - @s.rb_rescue(@arg_error_proc, nil, @rescue_proc_returns_sentinel, :exc).should == :rescue_proc_executed - @s.rb_rescue(@std_error_proc, nil, @rescue_proc_returns_sentinel, :exc).should == :rescue_proc_executed + it "executes passed 'raise function' if a StandardError exception is raised" do + @s.rb_rescue(@arg_error_proc, nil, @raise_proc_returns_sentinel, :exc).should == :raise_proc_executed + @s.rb_rescue(@std_error_proc, nil, @raise_proc_returns_sentinel, :exc).should == :raise_proc_executed end - it "passes the user supplied argument to the 'rescue function' if a StandardError exception is raised" do - arg1, _ = @s.rb_rescue(@arg_error_proc, nil, @rescue_proc_returns_arg, :exc1) + it "passes the user supplied argument to the 'raise function' if a StandardError exception is raised" do + arg1, _ = @s.rb_rescue(@arg_error_proc, nil, @raise_proc_returns_arg, :exc1) arg1.should == :exc1 - arg2, _ = @s.rb_rescue(@std_error_proc, nil, @rescue_proc_returns_arg, :exc2) + arg2, _ = @s.rb_rescue(@std_error_proc, nil, @raise_proc_returns_arg, :exc2) arg2.should == :exc2 end - it "passes the raised exception to the 'rescue function' if a StandardError exception is raised" do - _, exc1 = @s.rb_rescue(@arg_error_proc, nil, @rescue_proc_returns_arg, :exc) + it "passes the raised exception to the 'raise function' if a StandardError exception is raised" do + _, exc1 = @s.rb_rescue(@arg_error_proc, nil, @raise_proc_returns_arg, :exc) exc1.class.should == ArgumentError - _, exc2 = @s.rb_rescue(@std_error_proc, nil, @rescue_proc_returns_arg, :exc) + _, exc2 = @s.rb_rescue(@std_error_proc, nil, @raise_proc_returns_arg, :exc) exc2.class.should == StandardError end it "raises an exception if passed function raises an exception other than StandardError" do - -> { @s.rb_rescue(@exc_error_proc, nil, @rescue_proc_returns_arg, nil) }.should raise_error(Exception) + lambda { @s.rb_rescue(@exc_error_proc, nil, @raise_proc_returns_arg, nil) }.should raise_error(Exception) end - it "raises an exception if any exception is raised inside the 'rescue function'" do - -> { @s.rb_rescue(@std_error_proc, nil, @std_error_proc, nil) }.should raise_error(StandardError) + it "raises an exception if any exception is raised inside 'raise function'" do + lambda { @s.rb_rescue(@std_error_proc, nil, @std_error_proc, nil) }.should raise_error(StandardError) end - it "sets $! and rb_errinfo() during the 'rescue function' execution" do - @s.rb_rescue(-> *_ { raise exception_class, '' }, nil, -> _, exc { - exc.should.is_a?(exception_class) - $!.should.equal?(exc) - @s.rb_errinfo.should.equal?(exc) - }, nil) - - @s.rb_rescue(-> _ { @s.rb_raise({}) }, nil, -> _, exc { - exc.should.is_a?(TypeError) - $!.should.equal?(exc) - @s.rb_errinfo.should.equal?(exc) - }, nil) - + it "makes $! available only during 'raise function' execution" do + @s.rb_rescue(@std_error_proc, nil, lambda { |*_| $! }, nil).class.should == StandardError $!.should == nil - @s.rb_errinfo.should == nil end it "returns the break value if the passed function yields to a block with a break" do def proc_caller - @s.rb_rescue(-> *_ { yield }, nil, @proc, nil) + @s.rb_rescue(lambda { |*_| yield }, nil, @proc, nil) end proc_caller { break :value }.should == :value end - - it "returns nil if the 'rescue function' is null" do - @s.rb_rescue(@std_error_proc, nil, nil, nil).should == nil - end end describe "rb_rescue2" do it "only rescues if one of the passed exceptions is raised" do - proc = -> x, _exc { x } - arg_error_proc = -> *_ { raise ArgumentError, '' } - run_error_proc = -> *_ { raise RuntimeError, '' } - type_error_proc = -> *_ { raise Exception, 'custom error' } + proc = lambda { |x| x } + arg_error_proc = lambda { |*_| raise ArgumentError, '' } + run_error_proc = lambda { |*_| raise RuntimeError, '' } + type_error_proc = lambda { |*_| raise TypeError, '' } @s.rb_rescue2(arg_error_proc, :no_exc, proc, :exc, ArgumentError, RuntimeError).should == :exc @s.rb_rescue2(run_error_proc, :no_exc, proc, :exc, ArgumentError, RuntimeError).should == :exc - -> { + lambda { @s.rb_rescue2(type_error_proc, :no_exc, proc, :exc, ArgumentError, RuntimeError) - }.should raise_error(Exception, 'custom error') - end - - it "raises TypeError if one of the passed exceptions is not a Module" do - -> { - @s.rb_rescue2(-> *_ { raise RuntimeError, "foo" }, :no_exc, -> x { x }, :exc, Object.new, 42) - }.should raise_error(TypeError, /class or module required/) - end - - it "sets $! and rb_errinfo() during the 'rescue function' execution" do - @s.rb_rescue2(-> *_ { raise exception_class, '' }, :no_exc, -> _, exc { - exc.should.is_a?(exception_class) - $!.should.equal?(exc) - @s.rb_errinfo.should.equal?(exc) - }, :exc, exception_class, ScriptError) - - @s.rb_rescue2(-> *_ { @s.rb_raise({}) }, :no_exc, -> _, exc { - exc.should.is_a?(TypeError) - $!.should.equal?(exc) - @s.rb_errinfo.should.equal?(exc) - }, :exc, TypeError, ArgumentError) - - $!.should == nil - @s.rb_errinfo.should == nil + }.should raise_error(TypeError) end end @@ -510,11 +349,11 @@ describe "C-API Kernel function" do end it "executes passed function" do - @s.rb_catch("foo", -> { 1 }).should == 1 + @s.rb_catch("foo", lambda { 1 }).should == 1 end it "terminates the function at the point it was called" do - proc = -> do + proc = lambda do ScratchPad << :before_throw throw :thrown_value ScratchPad << :after_throw @@ -524,7 +363,7 @@ describe "C-API Kernel function" do end it "raises an ArgumentError if the throw symbol isn't caught" do - -> { @s.rb_catch("foo", -> { throw :bar }) }.should raise_error(ArgumentError) + lambda { @s.rb_catch("foo", lambda { throw :bar }) }.should raise_error(ArgumentError) end end @@ -536,11 +375,11 @@ describe "C-API Kernel function" do end it "executes passed function" do - @s.rb_catch_obj(@tag, -> { 1 }).should == 1 + @s.rb_catch_obj(@tag, lambda { 1 }).should == 1 end it "terminates the function at the point it was called" do - proc = -> do + proc = lambda do ScratchPad << :before_throw throw @tag ScratchPad << :after_throw @@ -550,91 +389,36 @@ describe "C-API Kernel function" do end it "raises an ArgumentError if the throw symbol isn't caught" do - -> { @s.rb_catch("foo", -> { throw :bar }) }.should raise_error(ArgumentError) - end - end - - describe "rb_category_warn" do - it "emits a warning into stderr" do - Warning[:deprecated] = true - - -> { - @s.rb_category_warn_deprecated - }.should complain(/warning: foo/, verbose: true) - end - - it "supports printf format modifiers" do - Warning[:deprecated] = true - - -> { - @s.rb_category_warn_deprecated_with_integer_extra_value(42) - }.should complain(/warning: foo 42/, verbose: true) - end - - it "does not emits a warning when a category is disabled" do - Warning[:deprecated] = false - - -> { - @s.rb_category_warn_deprecated - }.should_not complain(verbose: true) - end - - it "does not emits a warning when $VERBOSE is nil" do - Warning[:deprecated] = true - - -> { - @s.rb_category_warn_deprecated - }.should_not complain(verbose: nil) + lambda { @s.rb_catch("foo", lambda { throw :bar }) }.should raise_error(ArgumentError) end end describe "rb_ensure" do it "executes passed function and returns its value" do - proc = -> x { x } + proc = lambda { |x| x } @s.rb_ensure(proc, :proc, proc, :ensure_proc).should == :proc end it "executes passed 'ensure function' when no exception is raised" do foo = nil - proc = -> *_ { } - ensure_proc = -> x { foo = x } + proc = lambda { |*_| } + ensure_proc = lambda { |x| foo = x } @s.rb_ensure(proc, nil, ensure_proc, :foo) foo.should == :foo end it "executes passed 'ensure function' when an exception is raised" do foo = nil - raise_proc = -> _ { raise exception_class } - ensure_proc = -> x { foo = x } - -> { - @s.rb_ensure(raise_proc, nil, ensure_proc, :foo) - }.should raise_error(exception_class) + raise_proc = lambda { raise '' } + ensure_proc = lambda { |x| foo = x } + @s.rb_ensure(raise_proc, nil, ensure_proc, :foo) rescue nil foo.should == :foo end - it "sets $! and rb_errinfo() during the 'ensure function' execution" do - -> { - @s.rb_ensure(-> _ { raise exception_class }, nil, -> _ { - $!.should.is_a?(exception_class) - @s.rb_errinfo.should.is_a?(exception_class) - }, nil) - }.should raise_error(exception_class) - - -> { - @s.rb_ensure(-> _ { @s.rb_raise({}) }, nil, -> _ { - $!.should.is_a?(TypeError) - @s.rb_errinfo.should.is_a?(TypeError) - }, nil) - }.should raise_error(TypeError) - - $!.should == nil - @s.rb_errinfo.should == nil - end - it "raises the same exception raised inside passed function" do - raise_proc = -> *_ { raise RuntimeError, 'foo' } - proc = -> *_ { } - -> { @s.rb_ensure(raise_proc, nil, proc, nil) }.should raise_error(RuntimeError, 'foo') + raise_proc = lambda { |*_| raise RuntimeError, 'foo' } + proc = lambda { |*_| } + lambda { @s.rb_ensure(raise_proc, nil, proc, nil) }.should raise_error(RuntimeError, 'foo') end end @@ -642,63 +426,13 @@ describe "C-API Kernel function" do it "evaluates a string of ruby code" do @s.rb_eval_string("1+1").should == 2 end - - it "captures local variables when called within a method" do - a = 2 - @s.rb_eval_string("a+1").should == 3 - end - end - - describe "rb_eval_cmd_kw" do - it "evaluates a string of ruby code" do - @s.rb_eval_cmd_kw("1+1", [], 0).should == 2 - end - - it "calls a proc with the supplied arguments" do - @s.rb_eval_cmd_kw(-> *x { x.map { |i| i + 1 } }, [1, 3, 7], 0).should == [2, 4, 8] - end - - it "calls a proc with keyword arguments if kw_splat is non zero" do - a_proc = -> *x, **y { - res = x.map { |i| i + 1 } - y.each { |k, v| res << k; res << v } - res - } - @s.rb_eval_cmd_kw(a_proc, [1, 3, 7, {a: 1, b: 2, c: 3}], 1).should == [2, 4, 8, :a, 1, :b, 2, :c, 3] - end end describe "rb_block_proc" do it "converts the implicit block into a proc" do - proc = @s.rb_block_proc { 1+1 } - proc.should be_kind_of(Proc) - proc.call.should == 2 - proc.should_not.lambda? - end - - it "passes through an existing lambda and does not convert to a proc" do - b = -> { 1+1 } - proc = @s.rb_block_proc(&b) - proc.should equal(b) - proc.call.should == 2 - proc.should.lambda? - end - end - - describe "rb_block_lambda" do - it "converts the implicit block into a lambda" do - proc = @s.rb_block_lambda { 1+1 } + proc = @s.rb_block_proc() { 1+1 } proc.should be_kind_of(Proc) proc.call.should == 2 - proc.should.lambda? - end - - it "passes through an existing Proc and does not convert to a lambda" do - b = proc { 1+1 } - proc = @s.rb_block_lambda(&b) - proc.should equal(b) - proc.call.should == 2 - proc.should_not.lambda? end end @@ -709,9 +443,25 @@ describe "C-API Kernel function" do end end - describe "rb_set_end_proc" do - it "runs a C function on shutdown" do - ruby_exe("require #{kernel_path.inspect}; CApiKernelSpecs.new.rb_set_end_proc(STDOUT)").should == "in write_io" + platform_is_not :windows do + describe "rb_set_end_proc" do + before :each do + @r, @w = IO.pipe + end + + after :each do + @r.close + @w.close + Process.wait @pid + end + + it "runs a C function on shutdown" do + @pid = fork { + @s.rb_set_end_proc(@w) + } + + @r.read(1).should == "e" + end end end @@ -729,82 +479,21 @@ describe "C-API Kernel function" do end end - describe "rb_funcallv" do - def empty - 42 + describe "rb_obj_method" do + it "returns the method object for a symbol" do + method = @s.rb_obj_method("test", :size) + method.owner.should == String + method.name.to_sym.should == :size end - def sum(a, b) - a + b - end - - it "calls a method" do - @s.rb_funcallv(self, :empty, []).should == 42 - @s.rb_funcallv(self, :sum, [1, 2]).should == 3 - end - - it "calls a private method" do - object = CApiKernelSpecs::ClassWithPrivateMethod.new - @s.rb_funcallv(object, :private_method, []).should == :private - end - - it "calls a protected method" do - object = CApiKernelSpecs::ClassWithProtectedMethod.new - @s.rb_funcallv(object, :protected_method, []).should == :protected - end - end - - describe "rb_funcallv_kw" do - it "passes keyword arguments to the callee" do - def m(*args, **kwargs) - [args, kwargs] - end - - @s.rb_funcallv_kw(self, :m, [{}]).should == [[], {}] - @s.rb_funcallv_kw(self, :m, [{a: 1}]).should == [[], {a: 1}] - @s.rb_funcallv_kw(self, :m, [{b: 2}, {a: 1}]).should == [[{b: 2}], {a: 1}] - @s.rb_funcallv_kw(self, :m, [{b: 2}, {}]).should == [[{b: 2}], {}] - end - - it "calls a private method" do - object = CApiKernelSpecs::ClassWithPrivateMethod.new - @s.rb_funcallv_kw(object, :private_method, [{}]).should == :private - end - - it "calls a protected method" do - object = CApiKernelSpecs::ClassWithProtectedMethod.new - @s.rb_funcallv_kw(object, :protected_method, [{}]).should == :protected - end - - it "raises TypeError if the last argument is not a Hash" do - def m(*args, **kwargs) - [args, kwargs] - end - - -> { - @s.rb_funcallv_kw(self, :m, [42]) - }.should raise_error(TypeError, 'no implicit conversion of Integer into Hash') - end - end - - describe "rb_keyword_given_p" do - it "returns whether keywords were given to the C extension method" do - h = {a: 1} - empty = {} - @s.rb_keyword_given_p(a: 1).should == true - @s.rb_keyword_given_p("foo" => "bar").should == true - @s.rb_keyword_given_p(**h).should == true - - @s.rb_keyword_given_p(h).should == false - @s.rb_keyword_given_p().should == false - @s.rb_keyword_given_p(**empty).should == false - - @s.rb_funcallv_kw(@s, :rb_keyword_given_p, [{a: 1}]).should == true - @s.rb_funcallv_kw(@s, :rb_keyword_given_p, [{}]).should == false + it "returns the method object for a string" do + method = @s.rb_obj_method("test", "size") + method.owner.should == String + method.name.to_sym.should == :size end end - describe "rb_funcallv_public" do + describe "rb_funcall3" do before :each do @obj = Object.new class << @obj @@ -815,121 +504,29 @@ describe "C-API Kernel function" do end it "calls a public method" do - @s.rb_funcallv_public(@obj, :method_public).should == :method_public + @s.rb_funcall3(@obj, :method_public).should == :method_public end - it "does not call a private method" do - -> { @s.rb_funcallv_public(@obj, :method_private) }.should raise_error(NoMethodError, /private/) + lambda { @s.rb_funcall3(@obj, :method_private) }.should raise_error(NoMethodError, /private/) end end - describe 'rb_funcall' do + describe 'rb_funcall_with_block' do before :each do @obj = Object.new class << @obj - def many_args(*args) - args - end + def method_public; yield end + def method_private; yield end + private :method_private end end - it "can call a public method with 15 arguments" do - @s.rb_funcall_many_args(@obj, :many_args).should == 15.downto(1).to_a - end - end - - describe 'rb_funcall_with_block' do it "calls a method with block" do - @obj = Object.new - class << @obj - def method_public(*args); [args, yield] end - end - - @s.rb_funcall_with_block(@obj, :method_public, [1, 2], proc { :result }).should == [[1, 2], :result] + @s.rb_funcall_with_block(@obj, :method_public, proc { :result }).should == :result end it "does not call a private method" do - object = CApiKernelSpecs::ClassWithPrivateMethod.new - - -> { - @s.rb_funcall_with_block(object, :private_method, [], proc { }) - }.should raise_error(NoMethodError, /private/) - end - - it "does not call a protected method" do - object = CApiKernelSpecs::ClassWithProtectedMethod.new - - -> { - @s.rb_funcall_with_block(object, :protected_method, [], proc { }) - }.should raise_error(NoMethodError, /protected/) - end - end - - describe 'rb_funcall_with_block_kw' do - it "calls a method with keyword arguments and a block" do - @obj = Object.new - class << @obj - def method_public(*args, **kw, &block); [args, kw, block.call] end - end - - @s.rb_funcall_with_block_kw(@obj, :method_public, [1, 2, {a: 2}], proc { :result }).should == [[1, 2], {a: 2}, :result] - end - - it "does not call a private method" do - object = CApiKernelSpecs::ClassWithPrivateMethod.new - - -> { - @s.rb_funcall_with_block_kw(object, :private_method, [{}], proc { }) - }.should raise_error(NoMethodError, /private/) - end - - it "does not call a protected method" do - object = CApiKernelSpecs::ClassWithProtectedMethod.new - - -> { - @s.rb_funcall_with_block_kw(object, :protected_method, [{}], proc { }) - }.should raise_error(NoMethodError, /protected/) - end - end - - describe "rb_check_funcall" do - it "calls a method" do - @s.rb_check_funcall(1, :+, [2]).should == 3 - end - - it "returns Qundef if the method is not defined" do - obj = Object.new - @s.rb_check_funcall(obj, :foo, []).should == :Qundef - end - - it "uses #respond_to? to check if the method is defined" do - ScratchPad.record [] - obj = Object.new - def obj.respond_to?(name, priv) - ScratchPad << name - name == :foo || super - end - def obj.method_missing(name, *args) - name == :foo ? [name, 42] : super - end - @s.rb_check_funcall(obj, :foo, []).should == [:foo, 42] - ScratchPad.recorded.should == [:foo] - end - - it "calls a private method" do - object = CApiKernelSpecs::ClassWithPrivateMethod.new - @s.rb_check_funcall(object, :private_method, []).should == :private - end - - it "calls a protected method" do - object = CApiKernelSpecs::ClassWithProtectedMethod.new - @s.rb_check_funcall(object, :protected_method, []).should == :protected - end - end - - describe "rb_str_format" do - it "returns a string according to format and arguments" do - @s.rb_str_format(3, [10, 2.5, "test"], "%d %f %s").should == "10 2.500000 test" + lambda { @s.rb_funcall_with_block(@obj, :method_private, proc { :result }) }.should raise_error(NoMethodError, /private/) end end end diff --git a/spec/ruby/optional/capi/language_spec.rb b/spec/ruby/optional/capi/language_spec.rb deleted file mode 100644 index f59b87f2a1..0000000000 --- a/spec/ruby/optional/capi/language_spec.rb +++ /dev/null @@ -1,37 +0,0 @@ -require_relative 'spec_helper' - -load_extension("language") - -describe "C language construct" do - before :each do - @s = CApiLanguageSpecs.new - end - - describe "switch (VALUE)" do - it "works for Qtrue" do - @s.switch(true).should == :true - end - - it "works for Qfalse" do - @s.switch(false).should == :false - end - - it "works for Qnil" do - @s.switch(nil).should == :nil - end - - it "works for Qundef" do - @s.switch(:undef).should == :undef - end - - it "works for the default case" do - @s.switch(Object.new).should == :default - end - end - - describe "local variable assignment with the same name as a global" do - it "works for rb_mProcess" do - @s.global_local_var.should.equal?(Process) - end - end -end diff --git a/spec/ruby/optional/capi/marshal_spec.rb b/spec/ruby/optional/capi/marshal_spec.rb index f15b6b705a..ee06f96b72 100644 --- a/spec/ruby/optional/capi/marshal_spec.rb +++ b/spec/ruby/optional/capi/marshal_spec.rb @@ -1,4 +1,4 @@ -require_relative 'spec_helper' +require File.expand_path('../spec_helper', __FILE__) load_extension("marshal") diff --git a/spec/ruby/optional/capi/module_spec.rb b/spec/ruby/optional/capi/module_spec.rb index d7c0ab9c52..fbb5bd690d 100644 --- a/spec/ruby/optional/capi/module_spec.rb +++ b/spec/ruby/optional/capi/module_spec.rb @@ -1,5 +1,5 @@ -require_relative 'spec_helper' -require_relative 'fixtures/module' +require File.expand_path('../spec_helper', __FILE__) +require File.expand_path('../fixtures/module', __FILE__) load_extension('module') compile_extension("module_under_autoload") @@ -30,20 +30,6 @@ describe "CApiModule" do }.should complain(/already initialized constant/) CApiModuleSpecs::C::Z.should == 8 end - - it "allows arbitrary names, including constant names not valid in Ruby" do - -> { - CApiModuleSpecs::C.const_set(:_INVALID, 1) - }.should raise_error(NameError, /wrong constant name/) - - @m.rb_const_set(CApiModuleSpecs::C, :_INVALID, 2) - @m.rb_const_get(CApiModuleSpecs::C, :_INVALID).should == 2 - - # Ruby-level should still not allow access - -> { - CApiModuleSpecs::C.const_get(:_INVALID) - }.should raise_error(NameError, /wrong constant name/) - end end describe "rb_define_module" do @@ -54,7 +40,7 @@ describe "CApiModule" do it "raises a TypeError if the constant is not a module" do ::CApiModuleSpecsGlobalConst = 7 - -> { @m.rb_define_module("CApiModuleSpecsGlobalConst") }.should raise_error(TypeError) + lambda { @m.rb_define_module("CApiModuleSpecsGlobalConst") }.should raise_error(TypeError) Object.send :remove_const, :CApiModuleSpecsGlobalConst end @@ -134,12 +120,8 @@ describe "CApiModule" do @m.rb_const_get(CApiModuleSpecs::A, :X).should == 1 end - it "returns a constant defined in the module for multiple constants" do - [:Q, :R, :S, :T].each { |x| @m.rb_const_get(CApiModuleSpecs::A, x).should == CApiModuleSpecs::A.const_get(x) } - end - it "returns a constant defined at toplevel" do - @m.rb_const_get(CApiModuleSpecs::A, :Integer).should == Integer + @m.rb_const_get(CApiModuleSpecs::A, :Fixnum).should == Fixnum end it "returns a constant defined in a superclass" do @@ -158,16 +140,6 @@ describe "CApiModule" do it "resolves autoload constants in Object" do @m.rb_const_get(Object, :CApiModuleSpecsAutoload).should == 123 end - - it "allows arbitrary names, including constant names not valid in Ruby" do - -> { - CApiModuleSpecs::A.const_get(:_INVALID) - }.should raise_error(NameError, /wrong constant name/) - - -> { - @m.rb_const_get(CApiModuleSpecs::A, :_INVALID) - }.should raise_error(NameError, /uninitialized constant/) - end end describe "rb_const_get_from" do @@ -180,8 +152,8 @@ describe "CApiModule" do end it "calls #const_missing if the constant is not defined in the class or ancestors" do - CApiModuleSpecs::M.should_receive(:const_missing).with(:Integer) - @m.rb_const_get_from(CApiModuleSpecs::M, :Integer) + CApiModuleSpecs::M.should_receive(:const_missing).with(:Fixnum) + @m.rb_const_get_from(CApiModuleSpecs::M, :Fixnum) end it "resolves autoload constants" do @@ -246,49 +218,11 @@ describe "CApiModule" do cls.new.test_method.should == :test_method end - it "returns the correct arity when argc of the method in class is 0" do - cls = Class.new - @m.rb_define_method(cls, "test_method") - cls.new.method(:test_method).arity.should == 0 - end - - it "returns the correct arity when argc of the method in class is 1" do - @m.rb_define_method_1required(42).should == 42 - @m.method(:rb_define_method_1required).arity.should == 1 - end - - it "returns the correct arity when argc of the method in class is 2" do - @m.rb_define_method_2required(1, 2).should == 2 - @m.method(:rb_define_method_2required).arity.should == 2 - end - - it "defines a method taking variable arguments as a C array if the argument count is -1" do - @m.rb_define_method_varargs_1(1, 3, 7, 4).should == [1, 3, 7, 4] - end - - it "returns the correct arity when argc of the method in class is -1" do - @m.method(:rb_define_method_varargs_1).arity.should == -1 - end - - it "defines a method taking variable arguments as a Ruby array if the argument count is -2" do - @m.rb_define_method_varargs_2(1, 3, 7, 4).should == [1, 3, 7, 4] - end - - it "returns the correct arity when argc of the method in class is -2" do - @m.method(:rb_define_method_varargs_2).arity.should == -1 - end - it "defines a method on a module" do mod = Module.new @m.rb_define_method(mod, "test_method") mod.should have_instance_method(:test_method) end - - it "returns the correct arity of the method in module" do - mod = Module.new - @m.rb_define_method(mod, "test_method") - mod.instance_method(:test_method).arity.should == 0 - end end describe "rb_define_module_function" do @@ -301,23 +235,12 @@ describe "CApiModule" do @mod.test_module_function.should == :test_method end - it "returns the correct arity of the module function" do - @mod.method(:test_module_function).arity.should == 0 - end - it "defines a private instance method" do cls = Class.new cls.include(@mod) cls.should have_private_instance_method(:test_module_function) end - - it "returns the correct arity for private instance method" do - cls = Class.new - cls.include(@mod) - - @mod.instance_method(:test_module_function).arity.should == 0 - end end describe "rb_define_private_method" do @@ -356,7 +279,7 @@ describe "CApiModule" do a = cls.new @m.rb_define_singleton_method a, "module_specs_singleton_method" a.module_specs_singleton_method.should == :test_method - -> { cls.new.module_specs_singleton_method }.should raise_error(NoMethodError) + lambda { cls.new.module_specs_singleton_method }.should raise_error(NoMethodError) end end @@ -375,13 +298,8 @@ describe "CApiModule" do @class.should_not have_instance_method(:ruby_test_method) end - it "undefines private methods also" do - @m.rb_undef_method @class, "initialize_copy" - -> { @class.new.dup }.should raise_error(NoMethodError) - end - it "does not raise exceptions when passed a missing name" do - -> { @m.rb_undef_method @class, "not_exist" }.should_not raise_error + lambda { @m.rb_undef_method @class, "not_exist" }.should_not raise_error end describe "when given a frozen Class" do @@ -389,12 +307,12 @@ describe "CApiModule" do @frozen = @class.dup.freeze end - it "raises a FrozenError when passed a name" do - -> { @m.rb_undef_method @frozen, "ruby_test_method" }.should raise_error(FrozenError) + it "raises a RuntimeError when passed a name" do + lambda { @m.rb_undef_method @frozen, "ruby_test_method" }.should raise_error(RuntimeError) end - it "raises a FrozenError when passed a missing name" do - -> { @m.rb_undef_method @frozen, "not_exist" }.should raise_error(FrozenError) + it "raises a RuntimeError when passed a missing name" do + lambda { @m.rb_undef_method @frozen, "not_exist" }.should raise_error(RuntimeError) end end end diff --git a/spec/ruby/optional/capi/mutex_spec.rb b/spec/ruby/optional/capi/mutex_spec.rb index 71a2212e36..c435d09eff 100644 --- a/spec/ruby/optional/capi/mutex_spec.rb +++ b/spec/ruby/optional/capi/mutex_spec.rb @@ -1,4 +1,4 @@ -require_relative 'spec_helper' +require File.expand_path('../spec_helper', __FILE__) load_extension("mutex") @@ -46,14 +46,14 @@ describe "C-API Mutex functions" do it "throws an exception when already locked in the same thread" do @m.lock - -> { @s.rb_mutex_lock(@m) }.should raise_error(ThreadError) + lambda { @s.rb_mutex_lock(@m) }.should raise_error(ThreadError) @m.locked?.should be_true end end describe "rb_mutex_unlock" do it "raises an exception when not locked" do - -> { @s.rb_mutex_unlock(@m) }.should raise_error(ThreadError) + lambda { @s.rb_mutex_unlock(@m) }.should raise_error(ThreadError) @m.locked?.should be_false end @@ -66,37 +66,23 @@ describe "C-API Mutex functions" do describe "rb_mutex_sleep" do it "throws an exception when the mutex is not locked" do - -> { @s.rb_mutex_sleep(@m, 0.1) }.should raise_error(ThreadError) + lambda { @s.rb_mutex_sleep(@m, 0.1) }.should raise_error(ThreadError) @m.locked?.should be_false end it "sleeps when the mutex is locked" do @m.lock - t1 = Process.clock_gettime(Process::CLOCK_MONOTONIC) - @s.rb_mutex_sleep(@m, 0.001) - t2 = Process.clock_gettime(Process::CLOCK_MONOTONIC) - (t2 - t1).should >= 0 + start = Time.now + @s.rb_mutex_sleep(@m, 0.1) + (Time.now - start).should be_close(0.1, 0.2) @m.locked?.should be_true end end describe "rb_mutex_synchronize" do it "calls the function while the mutex is locked" do - callback = -> { @m.locked?.should be_true } + callback = lambda { @m.locked?.should be_true } @s.rb_mutex_synchronize(@m, callback) end - - it "returns a value returned from a callback" do - callback = -> { :foo } - @s.rb_mutex_synchronize(@m, callback).should == :foo - end - - it "calls a C-function that accepts and returns non-VALUE values" do - @s.rb_mutex_synchronize_with_naughty_callback(@m).should == 42 - end - - it "calls a native function" do - @s.rb_mutex_synchronize_with_native_callback(@m, 42).should == 42 - end end end diff --git a/spec/ruby/optional/capi/numeric_spec.rb b/spec/ruby/optional/capi/numeric_spec.rb index e9667da5ba..1d4a44d7a2 100644 --- a/spec/ruby/optional/capi/numeric_spec.rb +++ b/spec/ruby/optional/capi/numeric_spec.rb @@ -1,4 +1,4 @@ -require_relative 'spec_helper' +require File.expand_path('../spec_helper', __FILE__) load_extension("numeric") @@ -7,131 +7,135 @@ describe "CApiNumericSpecs" do @s = CApiNumericSpecs.new end - describe "NUM2INT" do - it "raises a TypeError if passed nil" do - -> { @s.NUM2INT(nil) }.should raise_error(TypeError) - end + platform_is wordsize: 64 do + describe "rb_num2int" do + it "raises a TypeError if passed nil" do + lambda { @s.rb_num2int(nil) }.should raise_error(TypeError) + end - it "converts a Float" do - @s.NUM2INT(4.2).should == 4 - end + it "converts a Float" do + @s.rb_num2int(4.2).should == 4 + end - it "converts a Bignum" do - @s.NUM2INT(0x7fff_ffff).should == 0x7fff_ffff - end + it "converts a Bignum" do + @s.rb_num2int(0x7fff_ffff).should == 0x7fff_ffff + end - it "converts a Fixnum" do - @s.NUM2INT(5).should == 5 - end + it "converts a Fixnum" do + @s.rb_num2int(5).should == 5 + end - it "converts -1 to an signed number" do - @s.NUM2INT(-1).should == -1 - end + it "converts -1 to an signed number" do + @s.rb_num2int(-1).should == -1 + end - it "converts a negative Bignum into an signed number" do - @s.NUM2INT(-2147442171).should == -2147442171 - end + it "converts a negative Bignum into an signed number" do + @s.rb_num2int(-2147442171).should == -2147442171 + end - it "raises a RangeError if the value is more than 32bits" do - -> { @s.NUM2INT(0xffff_ffff+1) }.should raise_error(RangeError) - end + it "raises a RangeError if the value is more than 32bits" do + lambda { @s.rb_num2int(0xffff_ffff+1) }.should raise_error(RangeError) + end - it "calls #to_int to coerce the value" do - obj = mock("number") - obj.should_receive(:to_int).and_return(2) - @s.NUM2INT(obj).should == 2 + it "calls #to_int to coerce the value" do + obj = mock("number") + obj.should_receive(:to_int).and_return(2) + @s.rb_num2long(obj).should == 2 + end end end - describe "NUM2UINT" do - it "raises a TypeError if passed nil" do - -> { @s.NUM2UINT(nil) }.should raise_error(TypeError) - end + platform_is wordsize: 64 do + describe "rb_num2uint" do + it "raises a TypeError if passed nil" do + lambda { @s.rb_num2uint(nil) }.should raise_error(TypeError) + end - it "converts a Float" do - @s.NUM2UINT(4.2).should == 4 - end + it "converts a Float" do + @s.rb_num2uint(4.2).should == 4 + end - it "converts a Bignum" do - @s.NUM2UINT(0xffff_ffff).should == 0xffff_ffff - end + it "converts a Bignum" do + @s.rb_num2uint(0xffff_ffff).should == 0xffff_ffff + end - it "converts a Fixnum" do - @s.NUM2UINT(5).should == 5 - end + it "converts a Fixnum" do + @s.rb_num2uint(5).should == 5 + end - it "converts a negative number to the complement" do - @s.NUM2UINT(-1).should == 4294967295 - end + it "converts a negative number to the complement" do + @s.rb_num2uint(-1).should == 18446744073709551615 + end - it "converts a signed int value to the complement" do - @s.NUM2UINT(-0x8000_0000).should == 2147483648 - end + it "converts a signed int value to the complement" do + @s.rb_num2uint(-0x8000_0000).should == 18446744071562067968 + end - it "raises a RangeError if the value is more than 32bits" do - -> { @s.NUM2UINT(0xffff_ffff+1) }.should raise_error(RangeError) - end + it "raises a RangeError if the value is more than 32bits" do + lambda { @s.rb_num2uint(0xffff_ffff+1) }.should raise_error(RangeError) + end - it "raises a RangeError if the value is less than 32bits negative" do - -> { @s.NUM2UINT(-0x8000_0000-1) }.should raise_error(RangeError) - end + it "raises a RangeError if the value is less than 32bits negative" do + lambda { @s.rb_num2uint(-0x8000_0000-1) }.should raise_error(RangeError) + end - it "raises a RangeError if the value is more than 64bits" do - -> do - @s.NUM2UINT(0xffff_ffff_ffff_ffff+1) - end.should raise_error(RangeError) - end + it "raises a RangeError if the value is more than 64bits" do + lambda do + @s.rb_num2uint(0xffff_ffff_ffff_ffff+1) + end.should raise_error(RangeError) + end - it "calls #to_int to coerce the value" do - obj = mock("number") - obj.should_receive(:to_int).and_return(2) - @s.NUM2UINT(obj).should == 2 + it "calls #to_int to coerce the value" do + obj = mock("number") + obj.should_receive(:to_int).and_return(2) + @s.rb_num2uint(obj).should == 2 + end end end - describe "NUM2LONG" do + describe "rb_num2long" do it "raises a TypeError if passed nil" do - -> { @s.NUM2LONG(nil) }.should raise_error(TypeError) + lambda { @s.rb_num2long(nil) }.should raise_error(TypeError) end it "converts a Float" do - @s.NUM2LONG(4.2).should == 4 + @s.rb_num2long(4.2).should == 4 end it "converts a Bignum" do - @s.NUM2LONG(0x7fff_ffff).should == 0x7fff_ffff + @s.rb_num2long(0x7fff_ffff).should == 0x7fff_ffff end it "converts a Fixnum" do - @s.NUM2LONG(5).should == 5 + @s.rb_num2long(5).should == 5 end - platform_is c_long_size: 32 do + platform_is wordsize: 32 do it "converts -1 to an signed number" do - @s.NUM2LONG(-1).should == -1 + @s.rb_num2long(-1).should == -1 end it "converts a negative Bignum into an signed number" do - @s.NUM2LONG(-2147442171).should == -2147442171 + @s.rb_num2long(-2147442171).should == -2147442171 end it "raises a RangeError if the value is more than 32bits" do - -> { @s.NUM2LONG(0xffff_ffff+1) }.should raise_error(RangeError) + lambda { @s.rb_num2long(0xffff_ffff+1) }.should raise_error(RangeError) end end - platform_is c_long_size: 64 do + platform_is wordsize: 64 do it "converts -1 to an signed number" do - @s.NUM2LONG(-1).should == -1 + @s.rb_num2long(-1).should == -1 end it "converts a negative Bignum into an signed number" do - @s.NUM2LONG(-9223372036854734331).should == -9223372036854734331 + @s.rb_num2long(-9223372036854734331).should == -9223372036854734331 end it "raises a RangeError if the value is more than 64bits" do - -> do - @s.NUM2LONG(0xffff_ffff_ffff_ffff+1) + lambda do + @s.rb_num2long(0xffff_ffff_ffff_ffff+1) end.should raise_error(RangeError) end end @@ -139,117 +143,75 @@ describe "CApiNumericSpecs" do it "calls #to_int to coerce the value" do obj = mock("number") obj.should_receive(:to_int).and_return(2) - @s.NUM2LONG(obj).should == 2 - end - end - - describe "NUM2SHORT" do - it "raises a TypeError if passed nil" do - -> { @s.NUM2SHORT(nil) }.should raise_error(TypeError) - end - - it "converts a Float" do - @s.NUM2SHORT(4.2).should == 4 - end - - it "converts a Fixnum" do - @s.NUM2SHORT(5).should == 5 - end - - it "converts -1 to an signed number" do - @s.NUM2SHORT(-1).should == -1 - end - - it "raises a RangeError if the value is more than 32bits" do - -> { @s.NUM2SHORT(0xffff_ffff+1) }.should raise_error(RangeError) - end - - it "calls #to_int to coerce the value" do - obj = mock("number") - obj.should_receive(:to_int).and_return(2) - @s.NUM2SHORT(obj).should == 2 + @s.rb_num2long(obj).should == 2 end end - describe "INT2NUM" do + describe "rb_int2num" do it "raises a TypeError if passed nil" do - -> { @s.INT2NUM(nil) }.should raise_error(TypeError) + lambda { @s.rb_int2num(nil) }.should raise_error(TypeError) end it "converts a Float" do - @s.INT2NUM(4.2).should == 4 + @s.rb_int2num(4.2).should == 4 end it "raises a RangeError when passed a Bignum" do - -> { @s.INT2NUM(bignum_value) }.should raise_error(RangeError) + lambda { @s.rb_int2num(bignum_value) }.should raise_error(RangeError) end it "converts a Fixnum" do - @s.INT2NUM(5).should == 5 + @s.rb_int2num(5).should == 5 end it "converts a negative Fixnum" do - @s.INT2NUM(-11).should == -11 + @s.rb_int2num(-11).should == -11 end end - describe "NUM2ULONG" do + describe "rb_num2ulong" do it "raises a TypeError if passed nil" do - -> { @s.NUM2ULONG(nil) }.should raise_error(TypeError) + lambda { @s.rb_num2ulong(nil) }.should raise_error(TypeError) end it "converts a Float" do - @s.NUM2ULONG(4.2).should == 4 + @s.rb_num2ulong(4.2).should == 4 end it "converts a Bignum" do - @s.NUM2ULONG(0xffff_ffff).should == 0xffff_ffff + @s.rb_num2ulong(0xffff_ffff).should == 0xffff_ffff end it "converts a Fixnum" do - @s.NUM2ULONG(5).should == 5 + @s.rb_num2ulong(5).should == 5 end - platform_is c_long_size: 32 do + platform_is wordsize: 32 do it "converts -1 to an unsigned number" do - @s.NUM2ULONG(-1).should == 4294967295 + @s.rb_num2ulong(-1).should == 4294967295 end it "converts a negative Bignum into an unsigned number" do - @s.NUM2ULONG(-2147442171).should == 2147525125 - end - - it "converts positive Bignums if the values is less than 64bits" do - @s.NUM2ULONG(0xffff_ffff).should == 0xffff_ffff - @s.NUM2ULONG(2**30).should == 2**30 - @s.NUM2ULONG(fixnum_max+1).should == fixnum_max+1 - @s.NUM2ULONG(fixnum_max).should == fixnum_max + @s.rb_num2ulong(-2147442171).should == 2147525125 end it "raises a RangeError if the value is more than 32bits" do - -> { @s.NUM2ULONG(0xffff_ffff+1) }.should raise_error(RangeError) + lambda { @s.rb_num2ulong(0xffff_ffff+1) }.should raise_error(RangeError) end end - platform_is c_long_size: 64 do + platform_is wordsize: 64 do it "converts -1 to an unsigned number" do - @s.NUM2ULONG(-1).should == 18446744073709551615 + @s.rb_num2ulong(-1).should == 18446744073709551615 end it "converts a negative Bignum into an unsigned number" do - @s.NUM2ULONG(-9223372036854734331).should == 9223372036854817285 - end - - it "converts positive Bignums if the values is less than 64bits" do - @s.NUM2ULONG(0xffff_ffff_ffff_ffff).should == 0xffff_ffff_ffff_ffff - @s.NUM2ULONG(2**62).should == 2**62 - @s.NUM2ULONG(fixnum_max+1).should == fixnum_max+1 - @s.NUM2ULONG(fixnum_max).should == fixnum_max + @s.rb_num2ulong(-9223372036854734331).should == 9223372036854817285 end it "raises a RangeError if the value is more than 64bits" do - -> do - @s.NUM2ULONG(0xffff_ffff_ffff_ffff+1) + lambda do + @s.rb_num2ulong(0xffff_ffff_ffff_ffff+1) end.should raise_error(RangeError) end end @@ -257,80 +219,59 @@ describe "CApiNumericSpecs" do it "calls #to_int to coerce the value" do obj = mock("number") obj.should_receive(:to_int).and_return(2) - @s.NUM2ULONG(obj).should == 2 + @s.rb_num2ulong(obj).should == 2 end end describe "rb_Integer" do - it "creates an Integer from a String" do + it "creates a new Integer from a String" do i = @s.rb_Integer("8675309") - i.should == 8675309 + i.should be_kind_of(Integer) + i.should eql(8675309) end end describe "rb_ll2inum" do - it "creates a Fixnum from a small signed long long" do + it "creates a new Fixnum from a small signed long long" do i = @s.rb_ll2inum_14() - i.should == 14 - end - end - - describe "rb_ull2inum" do - it "creates a Fixnum from a small unsigned long long" do - i = @s.rb_ull2inum_14() - i.should == 14 - end - - it "creates a positive Bignum from a negative long long" do - i = @s.rb_ull2inum_n14() - i.should == (2 ** (@s.size_of_long_long * 8) - 14) + i.should be_kind_of(Fixnum) + i.should eql(14) end end describe "rb_int2inum" do - it "creates a Fixnum from a long" do + it "creates a new Fixnum from a long" do i = @s.rb_int2inum_14() - i.should == 14 - end - end - - describe "rb_uint2inum" do - it "creates a Fixnum from a long" do - i = @s.rb_uint2inum_14() - i.should == 14 - end - - it "creates a positive Bignum from a negative long" do - i = @s.rb_uint2inum_n14() - i.should == (2 ** (@s.size_of_VALUE * 8) - 14) + i.should be_kind_of(Fixnum) + i.should eql(14) end end - describe "NUM2DBL" do + describe "rb_num2dbl" do it "raises a TypeError if passed nil" do - -> { @s.NUM2DBL(nil) }.should raise_error(TypeError) + lambda { @s.rb_num2dbl(nil) }.should raise_error(TypeError) end it "raises a TypeError if passed a String" do - -> { @s.NUM2DBL("1.2") }.should raise_error(TypeError) + lambda { @s.rb_num2dbl("1.2") }.should raise_error(TypeError) end it "converts a Float" do - @s.NUM2DBL(4.2).should == 4.2 + @s.rb_num2dbl(4.2).should == 4.2 end it "converts a Bignum" do - @s.NUM2DBL(2**70).should == (2**70).to_f + @s.rb_num2dbl(2**70).should == (2**70).to_f end it "converts a Fixnum" do - @s.NUM2DBL(5).should == 5.0 + @s.rb_num2dbl(5).should == 5.0 end it "calls #to_f to coerce the value" do obj = mock("number") obj.should_receive(:to_f).and_return(2.0) - @s.NUM2DBL(obj).should == 2.0 + @s.rb_num2dbl(obj).should == 2.0 end end @@ -348,13 +289,13 @@ describe "CApiNumericSpecs" do end it "raises a TypeError when passed an empty String" do - -> { @s.NUM2CHR("") }.should raise_error(TypeError) + lambda { @s.NUM2CHR("") }.should raise_error(TypeError) end end describe "rb_num_zerodiv" do it "raises a RuntimeError" do - -> { @s.rb_num_zerodiv() }.should raise_error(ZeroDivisionError, 'divided by 0') + lambda { @s.rb_num_zerodiv() }.should raise_error(ZeroDivisionError, 'divided by 0') end end @@ -384,7 +325,7 @@ describe "CApiNumericSpecs" do end it "raises an ArgumentError when passed nil" do - -> { + lambda { @s.rb_cmpint(nil, 4) }.should raise_error(ArgumentError) end @@ -410,7 +351,7 @@ describe "CApiNumericSpecs" do obj = mock("rb_num_coerce_bin") obj.should_receive(:coerce).with(2).and_return(nil) - -> { @s.rb_num_coerce_bin(2, obj, :+) }.should raise_error(TypeError) + lambda { @s.rb_num_coerce_bin(2, obj, :+) }.should raise_error(TypeError) end end @@ -430,12 +371,23 @@ describe "CApiNumericSpecs" do @s.rb_num_coerce_cmp(2, obj, :<=>).should == -1 end - it "lets the exception go through if #coerce raises an exception" do - obj = mock("rb_num_coerce_cmp") - obj.should_receive(:coerce).with(2).and_raise(RuntimeError.new("my error")) - -> { - @s.rb_num_coerce_cmp(2, obj, :<=>) - }.should raise_error(RuntimeError, "my error") + ruby_version_is ""..."2.5" do + it "returns nil if passed nil" do + -> { + @result = @s.rb_num_coerce_cmp(nil, 2, :<=>) + }.should complain(/comparison operators will no more rescue exceptions/) + @result.should be_nil + end + end + + ruby_version_is "2.5" do + it "lets the exception go through if #coerce raises an exception" do + obj = mock("rb_num_coerce_cmp") + obj.should_receive(:coerce).with(2).and_raise(RuntimeError.new("my error")) + -> { + @s.rb_num_coerce_cmp(2, obj, :<=>) + }.should raise_error(RuntimeError, "my error") + end end it "returns nil if #coerce does not return an Array" do @@ -467,14 +419,14 @@ describe "CApiNumericSpecs" do obj.should_receive(:coerce).with(2).and_return([obj, 2]) obj.should_receive(:<).with(2).and_return(nil) - -> { @s.rb_num_coerce_relop(2, obj, :<) }.should raise_error(ArgumentError) + lambda { @s.rb_num_coerce_relop(2, obj, :<) }.should raise_error(ArgumentError) end it "raises an ArgumentError if #coerce does not return an Array" do obj = mock("rb_num_coerce_relop") obj.should_receive(:coerce).with(2).and_return(nil) - -> { @s.rb_num_coerce_relop(2, obj, :<) }.should raise_error(ArgumentError) + lambda { @s.rb_num_coerce_relop(2, obj, :<) }.should raise_error(ArgumentError) end end diff --git a/spec/ruby/optional/capi/object_spec.rb b/spec/ruby/optional/capi/object_spec.rb index 7bc7bd992a..97bff38ec0 100644 --- a/spec/ruby/optional/capi/object_spec.rb +++ b/spec/ruby/optional/capi/object_spec.rb @@ -1,5 +1,4 @@ -require_relative 'spec_helper' -require_relative 'fixtures/object' +require File.expand_path('../spec_helper', __FILE__) load_extension("object") @@ -31,7 +30,6 @@ describe "CApiObject" do class ObjectTest def initialize @foo = 7 - yield if block_given? end def foo @@ -90,15 +88,6 @@ describe "CApiObject" do o.initialized.should be_true o.arguments.should == [:one, :two] end - - it "passes the block to #initialize" do - v = nil - o = @o.rb_obj_alloc(ObjectTest) - @o.rb_obj_call_init(o, 0, []) do - v = :foo - end - v.should == :foo - end end describe "rb_is_instance_of" do @@ -121,16 +110,6 @@ describe "CApiObject" do @o.rb_respond_to(ObjectTest.new, :foo).should == true @o.rb_respond_to(ObjectTest.new, :bar).should == false end - - it "can be used with primitives" do - @o.rb_respond_to(true, :object_id).should == true - @o.rb_respond_to(14, :succ).should == true - end - - it "returns 0 if the method has been defined as rb_f_notimplement" do - @o.respond_to?(:not_implemented_method).should == false - @o.rb_respond_to(@o, :not_implemented_method).should == false - end end describe "rb_obj_respond_to" do @@ -172,20 +151,6 @@ describe "CApiObject" do end end - describe "rb_obj_method" do - it "returns the method object for a symbol" do - method = @o.rb_obj_method("test", :size) - method.owner.should == String - method.name.to_sym.should == :size - end - - it "returns the method object for a string" do - method = @o.rb_obj_method("test", "size") - method.owner.should == String - method.name.to_sym.should == :size - end - end - describe "rb_method_boundp" do it "returns true when the given method is bound" do @o.rb_method_boundp(Object, :class, true).should == true @@ -219,7 +184,7 @@ describe "CApiObject" do end it "requires a ruby file" do - $:.unshift __dir__ + $:.unshift File.dirname(__FILE__) @o.rb_require() $foo.should == 7 end @@ -265,7 +230,7 @@ describe "CApiObject" do obj = mock("rb_check_convert_type") obj.should_receive(:to_array).and_return("string") - -> do + lambda do @o.rb_check_convert_type(obj, "Array", "to_array") end.should raise_error(TypeError) end @@ -290,7 +255,7 @@ describe "CApiObject" do obj = mock("rb_convert_type") obj.should_receive(:to_array).and_return(nil) - -> do + lambda do @o.rb_convert_type(obj, "Array", "to_array") end.should raise_error(TypeError) end @@ -299,7 +264,7 @@ describe "CApiObject" do obj = mock("rb_convert_type") obj.should_receive(:to_array).and_return("string") - -> do + lambda do @o.rb_convert_type(obj, "Array", "to_array") end.should raise_error(TypeError) end @@ -343,13 +308,13 @@ describe "CApiObject" do it "sends #to_ary to the argument and raises TypeError if it's not a kind of Array" do obj = mock("to_ary") obj.should_receive(:to_ary).and_return(Object.new) - -> { @o.rb_check_array_type obj }.should raise_error(TypeError) + lambda { @o.rb_check_array_type obj }.should raise_error(TypeError) end it "does not rescue exceptions raised by #to_ary" do obj = mock("to_ary") - obj.should_receive(:to_ary).and_raise(FrozenError) - -> { @o.rb_check_array_type obj }.should raise_error(FrozenError) + obj.should_receive(:to_ary).and_raise(RuntimeError) + lambda { @o.rb_check_array_type obj }.should raise_error(RuntimeError) end end @@ -391,13 +356,13 @@ describe "CApiObject" do it "sends #to_str to the argument and raises TypeError if it's not a kind of String" do obj = mock("to_str") obj.should_receive(:to_str).and_return(Object.new) - -> { @o.rb_check_string_type obj }.should raise_error(TypeError) + lambda { @o.rb_check_string_type obj }.should raise_error(TypeError) end it "does not rescue exceptions raised by #to_str" do obj = mock("to_str") obj.should_receive(:to_str).and_raise(RuntimeError) - -> { @o.rb_check_string_type obj }.should raise_error(RuntimeError) + lambda { @o.rb_check_string_type obj }.should raise_error(RuntimeError) end end @@ -449,6 +414,13 @@ describe "CApiObject" do end describe "FL_TEST" do + it "returns correct status for FL_TAINT" do + obj = Object.new + @o.FL_TEST(obj, "FL_TAINT").should == 0 + obj.taint + @o.FL_TEST(obj, "FL_TAINT").should_not == 0 + end + it "returns correct status for FL_FREEZE" do obj = Object.new @o.FL_TEST(obj, "FL_FREEZE").should == 0 @@ -469,7 +441,7 @@ describe "CApiObject" do describe "rb_class_of" do it "returns the class of an object" do @o.rb_class_of(nil).should == NilClass - @o.rb_class_of(0).should == Integer + @o.rb_class_of(0).should == Fixnum @o.rb_class_of(0.1).should == Float @o.rb_class_of(ObjectTest.new).should == ObjectTest end @@ -482,31 +454,12 @@ describe "CApiObject" do end end - describe "rb_obj_class" do - it "returns the class of an object" do - @o.rb_obj_class(nil).should == NilClass - @o.rb_obj_class(0).should == Integer - @o.rb_obj_class(0.1).should == Float - @o.rb_obj_class(ObjectTest.new).should == ObjectTest - end - - it "does not return the singleton class if it exists" do - o = ObjectTest.new - o.singleton_class - @o.rb_obj_class(o).should equal ObjectTest - end - end - describe "rb_obj_classname" do it "returns the class name of an object" do @o.rb_obj_classname(nil).should == 'NilClass' - @o.rb_obj_classname(0).should == 'Integer' + @o.rb_obj_classname(0).should == Fixnum.to_s @o.rb_obj_classname(0.1).should == 'Float' @o.rb_obj_classname(ObjectTest.new).should == 'ObjectTest' - - o = ObjectTest.new - o.singleton_class - @o.rb_obj_classname(o).should == 'ObjectTest' end end @@ -520,58 +473,20 @@ describe "CApiObject" do @o.rb_is_type_array([]).should == true @o.rb_is_type_array(DescArray.new).should == true @o.rb_is_type_module(ObjectTest).should == false - @o.rb_is_type_module(Module.new).should == true @o.rb_is_type_class(ObjectTest).should == true @o.rb_is_type_data(Time.now).should == true end - - it "returns T_FILE for instances of IO and subclasses" do - STDERR.class.should == IO - @o.rb_is_rb_type_p_file(STDERR).should == true - - File.open(__FILE__) do |f| - f.class.should == File - @o.rb_is_rb_type_p_file(f).should == true - end - - require 'socket' - TCPServer.open(0) do |s| - @o.rb_is_rb_type_p_file(s).should == true - end - end - end - - describe "rb_check_type" do - it "checks if the object is of the given type" do - @o.rb_check_type(nil, nil).should == true - @o.rb_check_type(ObjectTest.new, Object.new).should == true - @o.rb_check_type([], []).should == true - @o.rb_check_type(Class.new(Array).new, []).should == true - @o.rb_check_type(ObjectTest, Object).should == true - end - - it "raises an exception if the object is not of the expected type" do - -> { - @o.rb_check_type([], Object.new) - }.should raise_error(TypeError, 'wrong argument type Array (expected Object)') - - -> { - @o.rb_check_type(ObjectTest, Module.new) - }.should raise_error(TypeError, 'wrong argument type Class (expected Module)') - - -> { - @o.rb_check_type(nil, "string") - }.should raise_error(TypeError, 'wrong argument type nil (expected String)') - end end describe "rb_type_p" do it "returns whether object is of the given type" do + class DescArray < Array + end @o.rb_is_rb_type_p_nil(nil).should == true @o.rb_is_rb_type_p_object([]).should == false @o.rb_is_rb_type_p_object(ObjectTest.new).should == true @o.rb_is_rb_type_p_array([]).should == true - @o.rb_is_rb_type_p_array(Class.new(Array).new).should == true + @o.rb_is_rb_type_p_array(DescArray.new).should == true @o.rb_is_rb_type_p_module(ObjectTest).should == false @o.rb_is_rb_type_p_class(ObjectTest).should == true @o.rb_is_rb_type_p_data(Time.now).should == true @@ -580,10 +495,12 @@ describe "CApiObject" do describe "BUILTIN_TYPE" do it "returns the type constant for the object" do + class DescArray < Array + end @o.rb_is_builtin_type_object([]).should == false @o.rb_is_builtin_type_object(ObjectTest.new).should == true @o.rb_is_builtin_type_array([]).should == true - @o.rb_is_builtin_type_array(Class.new(Array).new).should == true + @o.rb_is_builtin_type_array(DescArray.new).should == true @o.rb_is_builtin_type_module(ObjectTest).should == false @o.rb_is_builtin_type_class(ObjectTest).should == true @o.rb_is_builtin_type_data(Time.now).should == true @@ -653,12 +570,62 @@ describe "CApiObject" do end describe "OBJ_TAINT" do + it "taints the object" do + obj = mock("tainted") + @o.OBJ_TAINT(obj) + obj.tainted?.should be_true + end end describe "OBJ_TAINTED" do + it "returns C true if the object is tainted" do + obj = mock("tainted") + obj.taint + @o.OBJ_TAINTED(obj).should be_true + end + + it "returns C false if the object is not tainted" do + obj = mock("untainted") + @o.OBJ_TAINTED(obj).should be_false + end end describe "OBJ_INFECT" do + it "does not taint the first argument if the second argument is not tainted" do + host = mock("host") + source = mock("source") + @o.OBJ_INFECT(host, source) + host.tainted?.should be_false + end + + it "taints the first argument if the second argument is tainted" do + host = mock("host") + source = mock("source").taint + @o.OBJ_INFECT(host, source) + host.tainted?.should be_true + end + + it "does not untrust the first argument if the second argument is trusted" do + host = mock("host") + source = mock("source") + @o.OBJ_INFECT(host, source) + host.untrusted?.should be_false + end + + it "untrusts the first argument if the second argument is untrusted" do + host = mock("host") + source = mock("source").untrust + @o.OBJ_INFECT(host, source) + host.untrusted?.should be_true + end + + it "propagates both taint and distrust" do + host = mock("host") + source = mock("source").taint.untrust + @o.OBJ_INFECT(host, source) + host.tainted?.should be_true + host.untrusted?.should be_true + end end describe "rb_obj_freeze" do @@ -672,7 +639,7 @@ describe "CApiObject" do describe "rb_obj_instance_eval" do it "evaluates the block in the object context, that includes private methods" do obj = ObjectTest - -> do + lambda do @o.rb_obj_instance_eval(obj) { include Kernel } end.should_not raise_error(NoMethodError) end @@ -686,22 +653,32 @@ describe "CApiObject" do end it "returns false if object passed to it is not frozen" do - obj = +"" + obj = "" @o.rb_obj_frozen_p(obj).should == false end end describe "rb_obj_taint" do + it "marks the object passed as tainted" do + obj = "" + obj.tainted?.should == false + @o.rb_obj_taint(obj) + obj.tainted?.should == true + end + + it "raises a RuntimeError if the object passed is frozen" do + lambda { @o.rb_obj_taint("".freeze) }.should raise_error(RuntimeError) + end end describe "rb_check_frozen" do - it "raises a FrozenError if the obj is frozen" do - -> { @o.rb_check_frozen("".freeze) }.should raise_error(FrozenError) + it "raises a RuntimeError if the obj is frozen" do + lambda { @o.rb_check_frozen("".freeze) }.should raise_error(RuntimeError) end it "does nothing when object isn't frozen" do - obj = +"" - -> { @o.rb_check_frozen(obj) }.should_not raise_error(TypeError) + obj = "" + lambda { @o.rb_check_frozen(obj) }.should_not raise_error(TypeError) end end @@ -741,23 +718,23 @@ describe "CApiObject" do it "raises a TypeError if #to_int does not return an Integer" do x = mock("to_int") x.should_receive(:to_int).and_return("5") - -> { @o.rb_to_int(x) }.should raise_error(TypeError) + lambda { @o.rb_to_int(x) }.should raise_error(TypeError) end it "raises a TypeError if called with nil" do - -> { @o.rb_to_int(nil) }.should raise_error(TypeError) + lambda { @o.rb_to_int(nil) }.should raise_error(TypeError) end it "raises a TypeError if called with true" do - -> { @o.rb_to_int(true) }.should raise_error(TypeError) + lambda { @o.rb_to_int(true) }.should raise_error(TypeError) end it "raises a TypeError if called with false" do - -> { @o.rb_to_int(false) }.should raise_error(TypeError) + lambda { @o.rb_to_int(false) }.should raise_error(TypeError) end it "raises a TypeError if called with a String" do - -> { @o.rb_to_int("1") }.should raise_error(TypeError) + lambda { @o.rb_to_int("1") }.should raise_error(TypeError) end end @@ -793,7 +770,7 @@ describe "CApiObject" do end it "raises a TypeError if arg is no class or module" do - ->{ + lambda{ @o.rb_class_inherited_p(1, 2) }.should raise_error(TypeError) end @@ -828,15 +805,6 @@ describe "CApiObject" do end end - describe "rb_ivar_count" do - it "returns the number of instance variables" do - obj = Object.new - @o.rb_ivar_count(obj).should == 0 - obj.instance_variable_set(:@foo, 42) - @o.rb_ivar_count(obj).should == 1 - end - end - describe "rb_ivar_get" do it "returns the instance variable on an object" do @o.rb_ivar_get(@test, :@foo).should == @test.instance_eval { @foo } @@ -845,16 +813,6 @@ describe "CApiObject" do it "returns nil if the instance variable has not been initialized" do @o.rb_ivar_get(@test, :@bar).should == nil end - - it "returns nil if the instance variable has not been initialized and is not a valid Ruby name" do - @o.rb_ivar_get(@test, :bar).should == nil - @o.rb_ivar_get(@test, :mesg).should == nil - end - - it 'returns the instance variable when it is not a valid Ruby name' do - @o.rb_ivar_set(@test, :foo, 27) - @o.rb_ivar_get(@test, :foo).should == 27 - end end describe "rb_ivar_set" do @@ -862,15 +820,6 @@ describe "CApiObject" do @o.rb_ivar_set(@test, :@foo, 42).should == 42 @test.instance_eval { @foo }.should == 42 end - - it "sets and returns the instance variable on an object" do - @o.rb_ivar_set(@test, :@foo, 42).should == 42 - @test.instance_eval { @foo }.should == 42 - end - - it 'sets and returns the instance variable when it is not a valid Ruby name' do - @o.rb_ivar_set(@test, :foo, 27).should == 27 - end end describe "rb_ivar_defined" do @@ -881,133 +830,6 @@ describe "CApiObject" do it "returns false if the instance variable is not defined" do @o.rb_ivar_defined(@test, :@bar).should == false end - - it "does not throw an error if the instance variable is not a valid Ruby name" do - @o.rb_ivar_defined(@test, :bar).should == false - @o.rb_ivar_defined(@test, :mesg).should == false - end - end - - # The `generic_iv_tbl` table and `*_generic_ivar` functions are for mutable - # objects which do not store ivars directly in MRI such as RString, because - # there is no member iv_index_tbl (ivar table) such as in RObject and RClass. - - describe "rb_copy_generic_ivar for objects which do not store ivars directly" do - it "copies the instance variables from one object to another" do - original = +"abc" - original.instance_variable_set(:@foo, :bar) - clone = +"def" - @o.rb_copy_generic_ivar(clone, original) - clone.instance_variable_get(:@foo).should == :bar - end - end - - describe "rb_free_generic_ivar for objects which do not store ivars directly" do - it "removes the instance variables from an object" do - o = +"abc" - o.instance_variable_set(:@baz, :flibble) - @o.rb_free_generic_ivar(o) - o.instance_variables.should == [] - end - end - end - - describe "allocator accessors" do - describe "rb_define_alloc_func" do - it "sets up the allocator" do - klass = Class.new - @o.rb_define_alloc_func(klass) - obj = klass.allocate - obj.class.should.equal?(klass) - obj.should have_instance_variable(:@from_custom_allocator) - end - - it "sets up the allocator for a subclass of String" do - klass = Class.new(String) - @o.rb_define_alloc_func(klass) - obj = klass.allocate - obj.class.should.equal?(klass) - obj.should have_instance_variable(:@from_custom_allocator) - obj.should == "" - end - - it "sets up the allocator for a subclass of Array" do - klass = Class.new(Array) - @o.rb_define_alloc_func(klass) - obj = klass.allocate - obj.class.should.equal?(klass) - obj.should have_instance_variable(:@from_custom_allocator) - obj.should == [] - end - end - - describe "rb_get_alloc_func" do - it "gets the allocator that is defined directly on a class" do - klass = Class.new - @o.rb_define_alloc_func(klass) - @o.speced_allocator?(Object).should == false - @o.speced_allocator?(klass).should == true - end - - it "gets the allocator that is inherited" do - parent = Class.new - @o.rb_define_alloc_func(parent) - klass = Class.new(parent) - @o.speced_allocator?(Object).should == false - @o.speced_allocator?(klass).should == true - end - end - - describe "rb_undef_alloc_func" do - it "makes rb_get_alloc_func() return NULL for a class without a custom allocator" do - klass = Class.new - @o.rb_undef_alloc_func(klass) - @o.custom_alloc_func?(klass).should == false - end - - it "undefs the allocator for the class" do - klass = Class.new - @o.rb_define_alloc_func(klass) - @o.speced_allocator?(klass).should == true - @o.rb_undef_alloc_func(klass) - @o.custom_alloc_func?(klass).should == false - end - - it "undefs the allocator for a class that inherits a allocator" do - parent = Class.new - @o.rb_define_alloc_func(parent) - klass = Class.new(parent) - @o.speced_allocator?(klass).should == true - @o.rb_undef_alloc_func(klass) - @o.custom_alloc_func?(klass).should == false - - @o.speced_allocator?(parent).should == true - end - end - - describe "rb_ivar_foreach" do - it "calls the callback function for each instance variable on an object" do - o = CApiObjectSpecs::IVars.new - ary = @o.rb_ivar_foreach(o) - ary.should == [:@a, 3, :@b, 7, :@c, 4] - end - - it "calls the callback function for each cvar and ivar on a class" do - exp = [:@@cvar, :foo, :@@cvar2, :bar, :@ivar, :baz] - exp.unshift(:__classpath__, 'CApiObjectSpecs::CVars') if RUBY_VERSION < "3.3" - - ary = @o.rb_ivar_foreach(CApiObjectSpecs::CVars) - ary.should == exp - end - - it "calls the callback function for each cvar and ivar on a module" do - exp = [:@@mvar, :foo, :@@mvar2, :bar, :@ivar, :baz] - exp.unshift(:__classpath__, 'CApiObjectSpecs::MVars') if RUBY_VERSION < "3.3" - - ary = @o.rb_ivar_foreach(CApiObjectSpecs::MVars) - ary.should == exp - end - end end end diff --git a/spec/ruby/optional/capi/proc_spec.rb b/spec/ruby/optional/capi/proc_spec.rb index 8b94432f3e..ca6163b574 100644 --- a/spec/ruby/optional/capi/proc_spec.rb +++ b/spec/ruby/optional/capi/proc_spec.rb @@ -1,5 +1,5 @@ -require_relative 'spec_helper' -require_relative 'fixtures/proc' +require File.expand_path('../spec_helper', __FILE__) +require File.expand_path('../fixtures/proc', __FILE__) load_extension("proc") @@ -7,8 +7,6 @@ describe "C-API Proc function" do before :each do @p = CApiProcSpecs.new @prc = @p.rb_proc_new - @prc2 = @p.rb_proc_new_argv_n - @prc3 = @p.rb_proc_new_argc end describe "rb_proc_new" do @@ -17,7 +15,6 @@ describe "C-API Proc function" do end it "calls the C function wrapped by the Proc instance when sent #call" do - @p.rb_proc_new_arg.call().should == nil @prc.call(:foo_bar).should == ":foo_bar" @prc.call([:foo, :bar]).should == "[:foo, :bar]" end @@ -27,30 +24,6 @@ describe "C-API Proc function" do @prc[[:foo, :bar]].should == "[:foo, :bar]" end - it "calls the C function with the arg count in argc" do - @prc3.call().should == 0 - @prc3.call(:foo).should == 1 - @prc3.call(:foo, :bar).should == 2 - end - - it "calls the C function with arguments in argv" do - @prc2.call(1, :foo).should == :foo - @prc2.call(2, :foo, :bar).should == :bar - -> { @prc2.call(3, :foo, :bar) }.should raise_error(ArgumentError) - end - - it "calls the C function with the block passed in blockarg" do - a_block = :foo.to_proc - @p.rb_proc_new_blockarg.call(&a_block).should == a_block - @p.rb_proc_new_blockarg.call().should == nil - end - - it "calls the C function and yields to the block passed in blockarg" do - @p.rb_proc_new_block_given_p.call() do - end.should == false - @p.rb_proc_new_block_given_p.call().should == false - end - it "returns a Proc instance correctly described in #inspect without source location" do @prc.inspect.should =~ /^#<Proc:([^ :@]*?)>$/ end @@ -81,78 +54,6 @@ describe "C-API Proc function" do @p.rb_proc_call(prc, [6, 7]).should == 42 end end - - describe "rb_proc_call_kw" do - it "passes keyword arguments to the proc" do - prc = proc { |*args, **kw| [args, kw] } - - @p.rb_proc_call_kw(prc, [{}]).should == [[], {}] - @p.rb_proc_call_kw(prc, [{a: 1}]).should == [[], {a: 1}] - @p.rb_proc_call_kw(prc, [{b: 2}, {a: 1}]).should == [[{b: 2}], {a: 1}] - @p.rb_proc_call_kw(prc, [{b: 2}, {}]).should == [[{b: 2}], {}] - end - - it "raises TypeError if the last argument is not a Hash" do - -> { - @p.rb_proc_call_kw(proc {}, [42]) - }.should raise_error(TypeError, 'no implicit conversion of Integer into Hash') - end - end - - describe "rb_proc_call_with_block" do - it "calls the Proc and passes arguments and a block" do - prc = Proc.new { |a, b, &block| block.call(a * b) } - @p.rb_proc_call_with_block(prc, [6, 7], proc { |n| n * 2 }).should == 6 * 7 * 2 - end - - it "calls the Proc and passes arguments when a block is nil" do - prc = Proc.new { |a, b| a * b } - @p.rb_proc_call_with_block(prc, [6, 7], nil).should == 6 * 7 - end - end - - describe "rb_proc_call_with_block_kw" do - it "passes keyword arguments and a block to the proc" do - prc = proc { |*args, **kw, &block| [args, kw, block.call(42)] } - block = proc { |n| n } - - @p.rb_proc_call_with_block_kw(prc, [{}], block).should == [[], {}, 42] - @p.rb_proc_call_with_block_kw(prc, [{a: 1}], block).should == [[], {a: 1}, 42] - @p.rb_proc_call_with_block_kw(prc, [{b: 2}, {a: 1}], block).should == [[{b: 2}], {a: 1}, 42] - @p.rb_proc_call_with_block_kw(prc, [{b: 2}, {}], block).should == [[{b: 2}], {}, 42] - end - - it "raises TypeError if the last argument is not a Hash" do - -> { - @p.rb_proc_call_with_block_kw(proc {}, [42], proc {}) - }.should raise_error(TypeError, 'no implicit conversion of Integer into Hash') - end - - it "passes keyword arguments to the proc when a block is nil" do - prc = proc { |*args, **kw| [args, kw] } - - @p.rb_proc_call_with_block_kw(prc, [{}], nil).should == [[], {}] - end - end - - describe "rb_obj_is_proc" do - it "returns true for Proc" do - prc = Proc.new {|a,b| a * b } - @p.rb_obj_is_proc(prc).should be_true - end - - it "returns true for subclass of Proc" do - prc = Class.new(Proc).new {} - @p.rb_obj_is_proc(prc).should be_true - end - - it "returns false for non Proc instances" do - @p.rb_obj_is_proc("aoeui").should be_false - @p.rb_obj_is_proc(123).should be_false - @p.rb_obj_is_proc(true).should be_false - @p.rb_obj_is_proc([]).should be_false - end - end end describe "C-API when calling Proc.new from a C function" do @@ -168,17 +69,40 @@ describe "C-API when calling Proc.new from a C function" do # For example: C -> Ruby <- C -> Ruby means a C function called into Ruby # code which returned to C, then C called into Ruby code again. + # Ruby -> C -> rb_funcall(Proc.new) + it "returns the Proc passed by the Ruby code calling the C function" do + prc = @p.rb_Proc_new(0) { :called } + prc.call.should == :called + end + + # Ruby -> C -> Ruby <- C -> rb_funcall(Proc.new) + it "returns the Proc passed to the Ruby method when the C function calls other Ruby methods before calling Proc.new" do + prc = @p.rb_Proc_new(1) { :called } + prc.call.should == :called + end + # Ruby -> C -> Ruby -> Proc.new it "raises an ArgumentError when the C function calls a Ruby method that calls Proc.new" do - -> { - @p.rb_Proc_new(2) { :called } - }.should raise_error(ArgumentError) + def @p.Proc_new() Proc.new end + lambda { @p.rb_Proc_new(2) { :called } }.should raise_error(ArgumentError) end # Ruby -> C -> Ruby -> C -> rb_funcall(Proc.new) it "raises an ArgumentError when the C function calls a Ruby method and that method calls a C function that calls Proc.new" do def @p.redispatch() rb_Proc_new(0) end - -> { @p.rb_Proc_new(3) { :called } }.should raise_error(ArgumentError) + lambda { @p.rb_Proc_new(3) { :called } }.should raise_error(ArgumentError) + end + + # Ruby -> C -> Ruby -> C (with new block) -> rb_funcall(Proc.new) + it "returns the most recent Proc passed when the Ruby method called the C function" do + prc = @p.rb_Proc_new(4) { :called } + prc.call.should == :calling_with_block + end + + # Ruby -> C -> Ruby -> C (with new block) <- Ruby <- C -> # rb_funcall(Proc.new) + it "returns the Proc passed from the original Ruby call to the C function" do + prc = @p.rb_Proc_new(5) { :called } + prc.call.should == :called end # Ruby -> C -> Ruby -> block_given? diff --git a/spec/ruby/optional/capi/rake_helper.rb b/spec/ruby/optional/capi/rake_helper.rb index c13f1189c5..2b7a6ccbe3 100644 --- a/spec/ruby/optional/capi/rake_helper.rb +++ b/spec/ruby/optional/capi/rake_helper.rb @@ -20,3 +20,4 @@ task default: [output] file output => [input] do sh build_cmd end + diff --git a/spec/ruby/optional/capi/range_spec.rb b/spec/ruby/optional/capi/range_spec.rb index 7a52dc7ff8..aa16f9773e 100644 --- a/spec/ruby/optional/capi/range_spec.rb +++ b/spec/ruby/optional/capi/range_spec.rb @@ -1,4 +1,4 @@ -require_relative 'spec_helper' +require File.expand_path('../spec_helper', __FILE__) load_extension("range") @@ -31,8 +31,8 @@ describe "C-API Range function" do end it "raises an ArgumentError when the given start and end can't be compared by using #<=>" do - -> { @s.rb_range_new(1, mock('x')) }.should raise_error(ArgumentError) - -> { @s.rb_range_new(mock('x'), mock('y')) }.should raise_error(ArgumentError) + lambda { @s.rb_range_new(1, mock('x')) }.should raise_error(ArgumentError) + lambda { @s.rb_range_new(mock('x'), mock('y')) }.should raise_error(ArgumentError) end end @@ -83,7 +83,7 @@ describe "C-API Range function" do it "raises a RangeError when not in range and err is 1" do r = -5..-1 - -> { @s.rb_range_beg_len(r, 0, 0, 1, 1) }.should raise_error(RangeError) + lambda { @s.rb_range_beg_len(r, 0, 0, 1, 1) }.should raise_error(RangeError) end it "returns nil when not in range and err is 0" do diff --git a/spec/ruby/optional/capi/rational_spec.rb b/spec/ruby/optional/capi/rational_spec.rb index 1c241ac48e..e3144666d3 100644 --- a/spec/ruby/optional/capi/rational_spec.rb +++ b/spec/ruby/optional/capi/rational_spec.rb @@ -1,4 +1,4 @@ -require_relative 'spec_helper' +require File.expand_path('../spec_helper', __FILE__) load_extension("rational") diff --git a/spec/ruby/optional/capi/rbasic_spec.rb b/spec/ruby/optional/capi/rbasic_spec.rb deleted file mode 100644 index 7b5b5b2fed..0000000000 --- a/spec/ruby/optional/capi/rbasic_spec.rb +++ /dev/null @@ -1,48 +0,0 @@ -require_relative 'spec_helper' -require_relative 'shared/rbasic' -load_extension("rbasic") -ruby_version_is ""..."3.4" do - load_extension("data") -end -load_extension("array") - -describe "RBasic support for regular objects" do - before :all do - @specs = CApiRBasicSpecs.new - @data = -> { [Object.new, Object.new] } - end - it_should_behave_like :rbasic -end - -ruby_version_is ""..."3.4" do - describe "RBasic support for RData" do - before :all do - @specs = CApiRBasicRDataSpecs.new - @wrapping = CApiWrappedStructSpecs.new - @data = -> { [@wrapping.wrap_struct(1024), @wrapping.wrap_struct(1025)] } - end - it_should_behave_like :rbasic - - it "supports user flags" do - obj, _ = @data.call - initial = @specs.get_flags(obj) - @specs.set_flags(obj, 1 << 14 | 1 << 16 | initial).should == 1 << 14 | 1 << 16 | initial - @specs.get_flags(obj).should == 1 << 14 | 1 << 16 | initial - @specs.set_flags(obj, initial).should == initial - end - - it "supports copying the flags from one object over to the other" do - obj1, obj2 = @data.call - initial = @specs.get_flags(obj1) - @specs.get_flags(obj2).should == initial - @specs.set_flags(obj1, 1 << 14 | 1 << 16 | initial) - @specs.get_flags(obj1).should == 1 << 14 | 1 << 16 | initial - - @specs.copy_flags(obj2, obj1) - @specs.get_flags(obj2).should == 1 << 14 | 1 << 16 | initial - @specs.set_flags(obj1, initial) - @specs.copy_flags(obj2, obj1) - @specs.get_flags(obj2).should == initial - end - end -end diff --git a/spec/ruby/optional/capi/regexp_spec.rb b/spec/ruby/optional/capi/regexp_spec.rb index af366e17a2..27b1627c50 100644 --- a/spec/ruby/optional/capi/regexp_spec.rb +++ b/spec/ruby/optional/capi/regexp_spec.rb @@ -1,4 +1,4 @@ -require_relative 'spec_helper' +require File.expand_path('../spec_helper', __FILE__) load_extension("regexp") @@ -9,20 +9,12 @@ describe "C-API Regexp function" do describe "rb_reg_new" do it "returns a new valid Regexp" do - my_re = @p.a_re("a", 0) + my_re = @p.a_re my_re.kind_of?(Regexp).should == true ('1a' =~ my_re).should == 1 ('1b' =~ my_re).should == nil my_re.source.should == 'a' end - - it "returns a Regexp with the given options" do - @p.a_re("a", 0).options == 0 - @p.a_re("a", Regexp::IGNORECASE).options.should == Regexp::IGNORECASE - @p.a_re("a", Regexp::EXTENDED).options.should == Regexp::EXTENDED - @p.a_re("a", Regexp::EXTENDED | Regexp::IGNORECASE).options.should == Regexp::EXTENDED | Regexp::IGNORECASE - @p.a_re("a", Regexp::MULTILINE).options.should == Regexp::MULTILINE - end end describe "rb_reg_nth_match" do @@ -75,54 +67,5 @@ describe "C-API Regexp function" do md = /c/.match('ab') @p.rb_backref_get.should == md end - - it "returns MatchData when used with rb_reg_match" do - @p.rb_reg_match_backref_get(/a/, 'ab')[0].should == 'a' - end - end - - describe "rb_backref_set" do - before :each do - @md = "foo".match(/foo/) - $~ = nil - end - - it "sets the value of $~" do - @p.rb_backref_set(@md) - @p.rb_backref_get.should == @md - $~.should == @md - end - - it "sets a Thread-local value" do - running = false - - thr = Thread.new do - @p.rb_backref_set(@md) - @p.rb_backref_get.should == @md - $~.should == @md - running = true - end - - Thread.pass while thr.status and !running - $~.should be_nil - - thr.join - end - end - - describe "rb_memicmp" do - it "returns 0 for identical strings" do - @p.rb_memcicmp('Hello', 'Hello').should == 0 - end - - it "returns 0 for strings which only differ in case" do - @p.rb_memcicmp('Hello', 'HELLO').should == 0 - @p.rb_memcicmp('HELLO', 'Hello').should == 0 - end - - it "returns the difference between the first non matching characters" do - @p.rb_memcicmp('Hello', 'HELLP').should == -1 - @p.rb_memcicmp('HELLp', 'Hello').should == 1 - end end end diff --git a/spec/ruby/optional/capi/shared/rbasic.rb b/spec/ruby/optional/capi/shared/rbasic.rb deleted file mode 100644 index 9d80a93e1d..0000000000 --- a/spec/ruby/optional/capi/shared/rbasic.rb +++ /dev/null @@ -1,28 +0,0 @@ -describe :rbasic, shared: true do - before :all do - specs = CApiRBasicSpecs.new - @taint = ruby_version_is(''...'3.1') ? specs.taint_flag : 0 - @freeze = specs.freeze_flag - end - - it "reports the appropriate FREEZE flag for the object when reading" do - obj, _ = @data.call - initial = @specs.get_flags(obj) - obj.freeze - (@specs.get_flags(obj) & 0xFFFF).should == (@freeze | initial) & 0xFFFF - end - - it "supports setting the FREEZE flag" do - obj, _ = @data.call - initial = @specs.get_flags(obj) - @specs.set_flags(obj, @freeze | initial).should == @freeze | initial - obj.should.frozen? - end - - it "supports retrieving the (meta)class" do - obj, _ = @data.call - @specs.get_klass(obj).should == obj.class - obj.singleton_class # ensure the singleton class exists - @specs.get_klass(obj).should == obj.singleton_class - end -end diff --git a/spec/ruby/optional/capi/spec_helper.rb b/spec/ruby/optional/capi/spec_helper.rb index 2691aa1332..373012a869 100644 --- a/spec/ruby/optional/capi/spec_helper.rb +++ b/spec/ruby/optional/capi/spec_helper.rb @@ -1,4 +1,4 @@ -# Require the main spec_helper.rb at the end to let `ruby ...spec.rb` work +require File.expand_path('../../../spec_helper', __FILE__) # MRI magic to use built but not installed ruby $extmk = false @@ -6,65 +6,43 @@ $extmk = false require 'rbconfig' OBJDIR ||= File.expand_path("../../../ext/#{RUBY_ENGINE}/#{RUBY_VERSION}", __FILE__) +mkdir_p(OBJDIR) + +def extension_path + File.expand_path("../ext", __FILE__) +end def object_path - path = OBJDIR - if ENV['SPEC_CAPI_CXX'] == 'true' - path = "#{path}/cxx" - end - mkdir_p(path) - path + OBJDIR end def compile_extension(name) debug = false - cxx = ENV['SPEC_CAPI_CXX'] == 'true' run_mkmf_in_process = RUBY_ENGINE == 'truffleruby' - core_ext_dir = File.expand_path("../ext", __FILE__) - - spec_caller_location = caller_locations.find { |c| c.path.end_with?('_spec.rb') } - spec_file_path = spec_caller_location.path - spec_ext_dir = File.expand_path("../ext", spec_file_path) - ext = "#{name}_spec" lib = "#{object_path}/#{ext}.#{RbConfig::CONFIG['DLEXT']}" - rubyhdrdir = RbConfig::CONFIG['rubyhdrdir'] - ruby_header = "#{rubyhdrdir}/ruby.h" - abi_header = "#{rubyhdrdir}/ruby/internal/abi.h" - - if RbConfig::CONFIG["ENABLE_SHARED"] == "yes" - # below is defined since 2.1, except for mswin, and maybe other platforms - libdirname = RbConfig::CONFIG.fetch 'libdirname', 'libdir' - libruby = "#{RbConfig::CONFIG[libdirname]}/#{RbConfig::CONFIG['LIBRUBY']}" + ruby_header = "#{RbConfig::CONFIG['rubyhdrdir']}/ruby.h" + libruby_so = RbConfig::CONFIG['LIBRUBY_SO'] + ruby_library = "#{RbConfig::CONFIG['libdir']}/#{libruby_so}" + unless libruby_so and File.exist?(ruby_library) + # Statically-compiled lib in the binary, ignore this check + ruby_library = nil end - begin - mtime = File.mtime(lib) - rescue Errno::ENOENT - # not found, then compile - else - case # if lib is older than headers, source or libruby, then recompile - when mtime <= File.mtime("#{core_ext_dir}/rubyspec.h") - when mtime <= File.mtime("#{spec_ext_dir}/#{ext}.c") - when mtime <= File.mtime(ruby_header) - when (mtime <= File.mtime(abi_header) rescue nil) - when libruby && mtime <= File.mtime(libruby) - else - return lib # up-to-date - end - end + return lib if File.exist?(lib) and + File.mtime(lib) > File.mtime("#{extension_path}/rubyspec.h") and + File.mtime(lib) > File.mtime("#{extension_path}/#{ext}.c") and + File.mtime(lib) > File.mtime(ruby_header) and + (!ruby_library || File.mtime(lib) > File.mtime(ruby_library)) and + true # sentinel # Copy needed source files to tmpdir tmpdir = tmp("cext_#{name}") Dir.mkdir(tmpdir) begin - ["#{core_ext_dir}/rubyspec.h", "#{spec_ext_dir}/#{ext}.c"].each do |file| - if cxx and file.end_with?('.c') - cp file, "#{tmpdir}/#{File.basename(file, '.c')}.cpp" - else - cp file, "#{tmpdir}/#{File.basename(file)}" - end + ["rubyspec.h", "#{ext}.c"].each do |file| + cp "#{extension_path}/#{file}", "#{tmpdir}/#{file}" end Dir.chdir(tmpdir) do @@ -74,14 +52,11 @@ def compile_extension(name) init_mkmf unless required create_makefile(ext, tmpdir) else - File.write("extconf.rb", <<-RUBY) - require 'mkmf' - $ruby = ENV.values_at('RUBY_EXE', 'RUBY_FLAGS').join(' ') + File.write("extconf.rb", "require 'mkmf'\n" + + "$ruby = ENV.values_at('RUBY_EXE', 'RUBY_FLAGS').join(' ')\n" + # MRI magic to consider building non-bundled extensions - $extout = nil - append_cflags '-Wno-declaration-after-statement' - create_makefile(#{ext.inspect}) - RUBY + "$extout = nil\n" + + "create_makefile(#{ext.inspect})\n") output = ruby_exe("extconf.rb") raise "extconf failed:\n#{output}" unless $?.success? $stderr.puts output if debug @@ -129,11 +104,9 @@ def setup_make end def load_extension(name) - ext_path = compile_extension(name) - require ext_path - ext_path -rescue LoadError => e - if %r{/usr/sbin/execerror ruby "\(ld 3 1 main ([/a-zA-Z0-9_\-.]+_spec\.so)"} =~ e.message + require compile_extension(name) +rescue LoadError + if %r{/usr/sbin/execerror ruby "\(ld 3 1 main ([/a-zA-Z0-9_\-.]+_spec\.so)"} =~ $!.message system('/usr/sbin/execerror', "#{RbConfig::CONFIG["bindir"]}/ruby", "(ld 3 1 main #{$1}") end raise @@ -141,7 +114,3 @@ end # Constants CAPI_SIZEOF_LONG = [0].pack('l!').size - -# Require the main spec_helper.rb only here so load_extension() is defined -# when running specs with `ruby ...spec.rb` -require_relative '../../spec_helper' diff --git a/spec/ruby/optional/capi/st_spec.rb b/spec/ruby/optional/capi/st_spec.rb index 8ca2950d83..7f2bc08c62 100644 --- a/spec/ruby/optional/capi/st_spec.rb +++ b/spec/ruby/optional/capi/st_spec.rb @@ -1,5 +1,5 @@ # encoding: utf-8 -require_relative 'spec_helper' +require File.expand_path('../spec_helper', __FILE__) load_extension('st') diff --git a/spec/ruby/optional/capi/string_spec.rb b/spec/ruby/optional/capi/string_spec.rb index 801d50f117..73e6deb498 100644 --- a/spec/ruby/optional/capi/string_spec.rb +++ b/spec/ruby/optional/capi/string_spec.rb @@ -1,34 +1,9 @@ # encoding: utf-8 -# frozen_string_literal: false -require_relative 'spec_helper' -require_relative '../../shared/string/times' +require File.expand_path('../spec_helper', __FILE__) +require File.expand_path('../../../shared/string/times', __FILE__) load_extension('string') -class CApiStringSpecs - class ValidTostrTest - def to_str - "ruby" - end - end - - class InvalidTostrTest - def to_str - [] - end - end - - class ToSOrInspect - def to_s - 'A string' - end - - def inspect - 'A different string' - end - end -end - describe :rb_str_new2, shared: true do it "returns a new string object calling strlen on the passed C string" do # Hardcoded to pass const char * = "hello\0invisible" @@ -36,7 +11,7 @@ describe :rb_str_new2, shared: true do end it "encodes the string with ASCII_8BIT" do - @s.send(@method, "hello").encoding.should == Encoding::BINARY + @s.send(@method, "hello").encoding.should == Encoding::ASCII_8BIT end end @@ -45,97 +20,42 @@ describe "C-API String function" do @s = CApiStringSpecs.new end - [Encoding::BINARY, Encoding::UTF_8].each do |enc| - describe "rb_str_set_len on a #{enc.name} String" do - before :each do - @str = "abcdefghij".dup.force_encoding(enc) - # Make sure to unshare the string - @s.rb_str_modify(@str) - end - - it "reduces the size of the string" do - @s.rb_str_set_len(@str, 5).should == "abcde" - end - - it "inserts a NULL byte at the length" do - @s.rb_str_set_len(@str, 5).should == "abcde" - @s.rb_str_set_len(@str, 8).should == "abcde\x00gh" - end - - it "updates the byte size" do - @s.rb_str_set_len(@str, 4) - @str.bytesize.should == 4 - @str.should == "abcd" - end - - it "invalidates the character size" do - @str.size.should == 10 - @s.rb_str_set_len(@str, 4) - @str.size.should == 4 - @str.should == "abcd" - end - - it "invalidates the code range" do - @s.rb_str_set_len(@str, 4) - @str.should.ascii_only? - end - - it "updates the string's attributes visible in C code" do - @s.rb_str_set_len_RSTRING_LEN(@str, 4).should == 4 - end - - it "can reveal characters written from C with RSTRING_PTR" do - @s.rb_str_set_len(@str, 1) - @str.should == "a" - - @s.RSTRING_PTR_set(@str, 1, 'B'.ord) - @s.RSTRING_PTR_set(@str, 2, 'C'.ord) - @s.rb_str_set_len(@str, 3) + class ValidTostrTest + def to_str + "ruby" + end + end - @str.bytesize.should == 3 - @str.should == "aBC" - end + class InvalidTostrTest + def to_str + [] end end - describe "rb_str_set_len on a UTF-16 String" do + describe "rb_str_set_len" do before :each do - @str = "abcdefghij".dup.force_encoding(Encoding::UTF_16BE) - # Make sure to unshare the string - @s.rb_str_modify(@str) + # Make a completely new copy of the string + # for every example (#dup doesn't cut it). + @str = "abcdefghij"[0..-1] end - it "inserts two NULL bytes at the length" do - @s.rb_str_set_len(@str, 4).b.should == "abcd".b - @s.rb_str_set_len(@str, 8).b.should == "abcd\x00\x00gh".b + it "reduces the size of the string" do + @s.rb_str_set_len(@str, 5).should == "abcde" end - end - describe "rb_str_set_len on a UTF-32 String" do - before :each do - @str = "abcdefghijkl".dup.force_encoding(Encoding::UTF_32BE) - # Make sure to unshare the string - @s.rb_str_modify(@str) + it "inserts a NULL byte at the length" do + @s.rb_str_set_len(@str, 5).should == "abcde" + @s.rb_str_set_len(@str, 8).should == "abcde\x00gh" end - it "inserts four NULL bytes at the length" do - @s.rb_str_set_len(@str, 4).b.should == "abcd".b - @s.rb_str_set_len(@str, 12).b.should == "abcd\x00\x00\x00\x00ijkl".b + it "updates the string's attributes visible in C code" do + @s.rb_str_set_len_RSTRING_LEN(@str, 4).should == 4 end end describe "rb_str_buf_new" do it "returns the equivalent of an empty string" do - buf = @s.rb_str_buf_new(10, nil) - buf.should == "" - buf.bytesize.should == 0 - buf.size.should == 0 - @s.RSTRING_LEN(buf).should == 0 - end - - it "returns a string with the given capacity" do - buf = @s.rb_str_buf_new(256, nil) - @s.rb_str_capacity(buf).should >= 256 + @s.rb_str_buf_new(10, nil).should == "" end it "returns a string that can be appended to" do @@ -163,19 +83,6 @@ describe "C-API String function" do str[0, 6].should == "abcd\x00f" @s.RSTRING_LEN(str).should == 8 end - - it "can be used as a general buffer and reveal characters with rb_str_set_len" do - str = @s.rb_str_buf_new(10, "abcdef") - - @s.RSTRING_PTR_set(str, 0, 195) - @s.RSTRING_PTR_set(str, 1, 169) - @s.rb_str_set_len(str, 2) - - str.force_encoding(Encoding::UTF_8) - str.bytesize.should == 2 - str.size.should == 1 - str.should == "é" - end end describe "rb_str_buf_new2" do @@ -185,25 +92,7 @@ describe "C-API String function" do end end - describe "rb_str_tmp_new" do - it "returns a hidden string (RBasic->klass is NULL)" do - @s.rb_str_tmp_new_klass(4).should == false - end - - it "returns a new String object filled with \\0 bytes" do - s = @s.rb_str_tmp_new(4) - s.encoding.should == Encoding::BINARY - s.bytesize.should == 4 - s.size.should == 4 - s.should == "\x00\x00\x00\x00" - end - end - describe "rb_str_new" do - it "creates a new String with BINARY Encoding" do - @s.rb_str_new("", 0).encoding.should == Encoding::BINARY - end - it "returns a new string object from a char buffer of len characters" do @s.rb_str_new("hello", 3).should == "hel" end @@ -212,11 +101,6 @@ describe "C-API String function" do @s.rb_str_new("hello", 0).should == "" end - it "copy length bytes and does not stop at the first \\0 byte" do - @s.rb_str_new("he\x00llo", 6).should == "he\x00llo" - @s.rb_str_new_native("he\x00llo", 6).should == "he\x00llo" - end - it "returns a string from an offset char buffer" do @s.rb_str_new_offset("hello", 1, 3).should == "ell" end @@ -226,36 +110,28 @@ describe "C-API String function" do it_behaves_like :rb_str_new2, :rb_str_new2 end + describe "rb_str_new" do + it "creates a new String with ASCII-8BIT Encoding" do + @s.rb_str_new("", 0).encoding.should == Encoding::ASCII_8BIT + end + end + describe "rb_str_new_cstr" do it_behaves_like :rb_str_new2, :rb_str_new_cstr end describe "rb_usascii_str_new" do it "creates a new String with US-ASCII Encoding from a char buffer of len characters" do - str = "abc".dup.force_encoding("us-ascii") + str = "abc".force_encoding("us-ascii") result = @s.rb_usascii_str_new("abcdef", 3) result.should == str result.encoding.should == Encoding::US_ASCII end end - describe "rb_usascii_str_new_lit" do - it "returns a US-ASCII string of the correct characters" do - str = @s.rb_usascii_str_new_lit - str.should == "nokogiri" - str.encoding.should == Encoding::US_ASCII - end - - it "returns US-ASCII string for non-US-ASCII string literal" do - str = @s.rb_usascii_str_new_lit_non_ascii - str.should == "r\xC3\xA9sum\xC3\xA9".dup.force_encoding(Encoding::US_ASCII) - str.encoding.should == Encoding::US_ASCII - end - end - describe "rb_usascii_str_new_cstr" do it "creates a new String with US-ASCII Encoding" do - str = "abc".dup.force_encoding("us-ascii") + str = "abc".force_encoding("us-ascii") result = @s.rb_usascii_str_new_cstr("abc") result.should == str result.encoding.should == Encoding::US_ASCII @@ -312,7 +188,7 @@ describe "C-API String function" do str1 = "hi" str2 = @s.rb_str_new3 str1 str1.should == str2 - str1.should_not equal str2 + str1.object_id.should_not == str2.object_id end end @@ -323,8 +199,8 @@ describe "C-API String function" do str2 = @s.rb_str_new4 str1 str1.should == str2 str1.should equal(str2) - str1.should.frozen? - str2.should.frozen? + str1.frozen?.should == true + str2.frozen?.should == true end it "returns a frozen copy of the string" do @@ -332,7 +208,7 @@ describe "C-API String function" do str2 = @s.rb_str_new4 str1 str1.should == str2 str1.should_not equal(str2) - str2.should.frozen? + str2.frozen?.should == true end end @@ -341,7 +217,7 @@ describe "C-API String function" do str1 = "hi" str2 = @s.rb_str_dup str1 str1.should == str2 - str1.should_not equal str2 + str1.object_id.should_not == str2.object_id end end @@ -356,13 +232,29 @@ describe "C-API String function" do end end + describe "rb_tainted_str_new" do + it "creates a new tainted String" do + newstring = @s.rb_tainted_str_new("test", 4) + newstring.should == "test" + newstring.tainted?.should be_true + end + end + + describe "rb_tainted_str_new2" do + it "creates a new tainted String" do + newstring = @s.rb_tainted_str_new2("test") + newstring.should == "test" + newstring.tainted?.should be_true + end + end + describe "rb_str_append" do it "appends a string to another string" do @s.rb_str_append("Hello", " Goodbye").should == "Hello Goodbye" end it "raises a TypeError trying to append non-String-like object" do - -> { @s.rb_str_append("Hello", 32323)}.should raise_error(TypeError) + lambda { @s.rb_str_append("Hello", 32323)}.should raise_error(TypeError) end it "changes Encoding if a string is appended to an empty string" do @@ -378,15 +270,7 @@ describe "C-API String function" do end describe "rb_str_times" do - it_behaves_like :string_times, :rb_str_times, -> str, times { @s.rb_str_times(str, times) } - end - - describe "rb_str_buf_append" do - it "concatenates a string to another string" do - str = "Your house " - @s.rb_str_buf_append(str, "is on fire?").should.equal?(str) - str.should == "Your house is on fire?" - end + it_behaves_like :string_times, :rb_str_times, ->(str, times) { @s.rb_str_times(str, times) } end describe "rb_str_buf_cat" do @@ -407,26 +291,6 @@ describe "C-API String function" do end end - describe "rb_str_cat_cstr" do - it "concatenates a C string literal to a ruby string" do - @s.rb_str_cat_cstr_constant("Your house is on fire").should == "Your house is on fire?" - end - - it "concatenates a variable C string to a ruby string" do - @s.rb_str_cat_cstr("Your house is on fire", "?").should == "Your house is on fire?" - end - end - - describe "rb_enc_str_buf_cat" do - it "concatenates a C string literal to a ruby string with the given encoding" do - input = "hello ".dup.force_encoding(Encoding::US_ASCII) - result = @s.rb_enc_str_buf_cat(input, "résumé", Encoding::UTF_8) - result.should == "hello résumé" - result.encoding.should == Encoding::UTF_8 - result.object_id.should == input.object_id - end - end - describe "rb_str_cmp" do it "returns 0 if two strings are identical" do @s.rb_str_cmp("ppp", "ppp").should == 0 @@ -449,20 +313,6 @@ describe "C-API String function" do end end - describe "rb_str_strlen" do - it 'returns 0 as the length of an empty string' do - @s.rb_str_strlen('').should == 0 - end - - it 'returns the number of characters in a string' do - @s.rb_str_strlen('hello').should == 5 - end - - it 'returns the number of characters in a string with multi-byte characters' do - @s.rb_str_strlen('こんにちは').should == 5 - end - end - describe "rb_str_split" do it "splits strings over a splitter" do @s.rb_str_split("Hello,Goodbye").should == ["Hello", "Goodbye"] @@ -491,7 +341,7 @@ describe "C-API String function" do end it "converts a C string to a Fixnum strictly if base is 0" do - -> { @s.rb_cstr2inum("1234a", 0) }.should raise_error(ArgumentError) + lambda { @s.rb_cstr2inum("1234a", 0) }.should raise_error(ArgumentError) end end @@ -509,14 +359,14 @@ describe "C-API String function" do end it "converts a C string to a Fixnum strictly" do - -> { @s.rb_cstr_to_inum("1234a", 10, true) }.should raise_error(ArgumentError) + lambda { @s.rb_cstr_to_inum("1234a", 10, true) }.should raise_error(ArgumentError) end end describe "rb_str_subseq" do it "returns a byte-indexed substring" do - str = "\x00\x01\x02\x03\x04".dup.force_encoding("binary") - @s.rb_str_subseq(str, 1, 2).should == "\x01\x02".dup.force_encoding("binary") + str = "\x00\x01\x02\x03\x04".force_encoding("binary") + @s.rb_str_subseq(str, 1, 2).should == "\x01\x02".force_encoding("binary") end end @@ -531,12 +381,12 @@ describe "C-API String function" do describe "rb_str_to_str" do it "calls #to_str to coerce the value to a String" do @s.rb_str_to_str("foo").should == "foo" - @s.rb_str_to_str(CApiStringSpecs::ValidTostrTest.new).should == "ruby" + @s.rb_str_to_str(ValidTostrTest.new).should == "ruby" end it "raises a TypeError if coercion fails" do - -> { @s.rb_str_to_str(0) }.should raise_error(TypeError) - -> { @s.rb_str_to_str(CApiStringSpecs::InvalidTostrTest.new) }.should raise_error(TypeError) + lambda { @s.rb_str_to_str(0) }.should raise_error(TypeError) + lambda { @s.rb_str_to_str(InvalidTostrTest.new) }.should raise_error(TypeError) end end @@ -552,7 +402,7 @@ describe "C-API String function" do it "allows changing the characters in the string" do str = "abc" - @s.RSTRING_PTR_assign(str, 'A'.ord) + @s.RSTRING_PTR_assign(str, 65) str.should == "AAA" end @@ -567,13 +417,6 @@ describe "C-API String function" do ret.should == str end - it "reflects changes from native memory and from String#setbyte in bounds" do - str = "abc" - from_rstring_ptr = @s.RSTRING_PTR_after_yield(str) { str.setbyte(1, 'B'.ord) } - from_rstring_ptr.should == "1B2" - str.should == "1B2" - end - it "returns a pointer to the contents of encoded pointer-sized string" do s = "70パク". encode(Encoding::UTF_16LE). @@ -586,40 +429,6 @@ describe "C-API String function" do end chars.should == [55, 48, 227, 131, 145, 227, 130, 175] end - - it "returns a pointer which can be cast and used as another type" do - s = "70パク". - encode(Encoding::UTF_16LE). - force_encoding(Encoding::UTF_16LE). - encode(Encoding::UTF_8) - - ints = [] - @s.RSTRING_PTR_iterate_uint32(s) do |i| - ints << i - end - ints.should == s.unpack('LL') - end - - it "allows a short memcpy to the string which may be converted to a single write operation by the compiler" do - str = " " - @s.RSTRING_PTR_short_memcpy(str).should == "Infinity" - end - - it "allows read() to update the string contents" do - filename = fixture(__FILE__, "read.txt") - str = "" - capacities = @s.RSTRING_PTR_read(str, filename) - capacities[0].should >= 30 - capacities[1].should >= 53 - capacities[0].should < capacities[1] - str.should == "fixture file contents to test read() with RSTRING_PTR" - end - - it "terminates the string with at least (encoding min length) \\0 bytes" do - @s.RSTRING_PTR_null_terminate("abc", 1).should == "\x00" - @s.RSTRING_PTR_null_terminate("abc".encode("UTF-16BE"), 2).should == "\x00\x00" - @s.RSTRING_PTR_null_terminate("abc".encode("UTF-32BE"), 4).should == "\x00\x00\x00\x00" - end end describe "RSTRING_LEN" do @@ -660,7 +469,7 @@ describe "C-API String function" do it "does not call #to_s on non-String objects" do str = mock("fake") str.should_not_receive(:to_s) - -> { @s.send(@method, str) }.should raise_error(TypeError) + lambda { @s.send(@method, str) }.should raise_error(TypeError) end end @@ -669,50 +478,22 @@ describe "C-API String function" do end describe "SafeStringValue" do - end - - describe "rb_str_modify" do - it "raises an error if the string is frozen" do - -> { @s.rb_str_modify("frozen".freeze) }.should raise_error(FrozenError) + it "raises for tained string when $SAFE is 1" do + Thread.new { + $SAFE = 1 + lambda { + @s.SafeStringValue("str".taint) + }.should raise_error(SecurityError) + }.join end - end - - describe "rb_str_modify_expand" do - it "grows the capacity to bytesize + expand, not changing the bytesize" do - str = @s.rb_str_buf_new(256, "abcd") - @s.rb_str_capacity(str).should >= 256 - - @s.rb_str_set_len(str, 3) - str.bytesize.should == 3 - @s.RSTRING_LEN(str).should == 3 - @s.rb_str_capacity(str).should >= 256 - - @s.rb_str_modify_expand(str, 4) - str.bytesize.should == 3 - @s.RSTRING_LEN(str).should == 3 - @s.rb_str_capacity(str).should >= 7 - @s.rb_str_modify_expand(str, 1024) - str.bytesize.should == 3 - @s.RSTRING_LEN(str).should == 3 - @s.rb_str_capacity(str).should >= 1027 - - @s.rb_str_modify_expand(str, 1) - str.bytesize.should == 3 - @s.RSTRING_LEN(str).should == 3 - @s.rb_str_capacity(str).should >= 4 - end - - it "raises an error if the string is frozen" do - -> { @s.rb_str_modify_expand("frozen".freeze, 10) }.should raise_error(FrozenError) - end + it_behaves_like :string_value_macro, :SafeStringValue end describe "rb_str_resize" do it "reduces the size of the string" do str = @s.rb_str_resize("test", 2) str.size.should == 2 - str.bytesize.should == 2 @s.RSTRING_LEN(str).should == 2 str.should == "te" end @@ -721,16 +502,10 @@ describe "C-API String function" do @s.rb_str_resize_RSTRING_LEN("test", 2).should == 2 end - it "copies the existing bytes" do - str = "t" - @s.rb_str_resize_copy(str).should == "test" - end - it "increases the size of the string" do - expected = "test".dup.force_encoding("US-ASCII") + expected = "test".force_encoding("US-ASCII") str = @s.rb_str_resize(expected.dup, 12) str.size.should == 12 - str.bytesize.should == 12 @s.RSTRING_LEN(str).should == 12 str[0, 4].should == expected end @@ -793,10 +568,14 @@ describe :rb_external_str_new, shared: true do @s.send(@method, "abc").encoding.should == Encoding::UTF_8 end - it "returns a binary encoded string if any non-ascii bytes are present and default external is US-ASCII" do + it "returns an ASCII-8BIT encoded string if any non-ascii bytes are present and default external is US-ASCII" do Encoding.default_external = "US-ASCII" x80 = [0x80].pack('C') - @s.send(@method, "#{x80}abc").encoding.should == Encoding::BINARY + @s.send(@method, "#{x80}abc").encoding.should == Encoding::ASCII_8BIT + end + + it "returns a tainted String" do + @s.send(@method, "abc").tainted?.should be_true end end @@ -848,22 +627,22 @@ describe "C-API String function" do s.encoding.should == Encoding::UTF_8 end - it "returns a binary encoded String if any non-ascii bytes are present and the specified encoding is US-ASCII" do + it "returns an ASCII-8BIT encoded String if any non-ascii bytes are present and the specified encoding is US-ASCII" do x80 = [0x80].pack('C') s = @s.rb_external_str_new_with_enc("#{x80}abc", 4, Encoding::US_ASCII) - s.encoding.should == Encoding::BINARY + s.encoding.should == Encoding::ASCII_8BIT end # it "transcodes a String to Encoding.default_internal if it is set" do # Encoding.default_internal = Encoding::EUC_JP # -# - a = "\xE3\x81\x82\xe3\x82\x8c".dup.force_encoding("utf-8") +# - a = "\xE3\x81\x82\xe3\x82\x8c".force_encoding("utf-8") # + a = [0xE3, 0x81, 0x82, 0xe3, 0x82, 0x8c].pack('C6').force_encoding("utf-8") # s = @s.rb_external_str_new_with_enc(a, a.bytesize, Encoding::UTF_8) # - -# - s.should == "\xA4\xA2\xA4\xEC".dup.force_encoding("euc-jp") -# + x = [0xA4, 0xA2, 0xA4, 0xEC].pack('C4')#.force_encoding('binary') +# - s.should == "\xA4\xA2\xA4\xEC".force_encoding("euc-jp") +# + x = [0xA4, 0xA2, 0xA4, 0xEC].pack('C4')#.force_encoding('ascii-8bit') # + s.should == x # s.encoding.should equal(Encoding::EUC_JP) # end @@ -877,12 +656,17 @@ describe "C-API String function" do s.should == x s.encoding.should equal(Encoding::EUC_JP) end + + it "returns a tainted String" do + s = @s.rb_external_str_new_with_enc("abc", 3, Encoding::US_ASCII) + s.tainted?.should be_true + end end describe "rb_locale_str_new" do it "returns a String with 'locale' encoding" do s = @s.rb_locale_str_new("abc", 3) - s.should == "abc".dup.force_encoding(Encoding.find("locale")) + s.should == "abc".force_encoding(Encoding.find("locale")) s.encoding.should equal(Encoding.find("locale")) end end @@ -890,37 +674,33 @@ describe "C-API String function" do describe "rb_locale_str_new_cstr" do it "returns a String with 'locale' encoding" do s = @s.rb_locale_str_new_cstr("abc") - s.should == "abc".dup.force_encoding(Encoding.find("locale")) + s.should == "abc".force_encoding(Encoding.find("locale")) s.encoding.should equal(Encoding.find("locale")) end end describe "rb_str_conv_enc" do it "returns the original String when to encoding is not specified" do - a = "abc".dup.force_encoding("us-ascii") + a = "abc".force_encoding("us-ascii") @s.rb_str_conv_enc(a, Encoding::US_ASCII, nil).should equal(a) end it "returns the original String if a transcoding error occurs" do - a = [0xEE].pack('C').force_encoding(Encoding::UTF_8) - @s.rb_str_conv_enc(a, Encoding::UTF_8, Encoding::EUC_JP).should.equal?(a) - a.encoding.should == Encoding::UTF_8 - - a = "\x80".b - @s.rb_str_conv_enc(a, Encoding::BINARY, Encoding::UTF_8).should.equal?(a) - a.encoding.should == Encoding::BINARY + a = [0xEE].pack('C').force_encoding("utf-8") + @s.rb_str_conv_enc(a, Encoding::UTF_8, Encoding::EUC_JP).should equal(a) end it "returns a transcoded String" do - a = "\xE3\x81\x82\xE3\x82\x8C".dup.force_encoding(Encoding::UTF_8) + a = "\xE3\x81\x82\xE3\x82\x8C".force_encoding("utf-8") result = @s.rb_str_conv_enc(a, Encoding::UTF_8, Encoding::EUC_JP) - result.should == [0xA4, 0xA2, 0xA4, 0xEC].pack('C4').force_encoding(Encoding::EUC_JP) - result.encoding.should == Encoding::EUC_JP + x = [0xA4, 0xA2, 0xA4, 0xEC].pack('C4').force_encoding('utf-8') + result.should == x.force_encoding("euc-jp") + result.encoding.should equal(Encoding::EUC_JP) end describe "when the String encoding is equal to the destination encoding" do it "returns the original String" do - a = "abc".dup.force_encoding("us-ascii") + a = "abc".force_encoding("us-ascii") @s.rb_str_conv_enc(a, Encoding::US_ASCII, Encoding::US_ASCII).should equal(a) end @@ -929,16 +709,16 @@ describe "C-API String function" do @s.rb_str_conv_enc(a, Encoding::UTF_8, Encoding::US_ASCII).should equal(a) end - it "returns the origin String if the destination encoding is BINARY" do - a = "abc".dup.force_encoding("binary") - @s.rb_str_conv_enc(a, Encoding::US_ASCII, Encoding::BINARY).should equal(a) + it "returns the origin String if the destination encoding is ASCII-8BIT" do + a = "abc".force_encoding("ascii-8bit") + @s.rb_str_conv_enc(a, Encoding::US_ASCII, Encoding::ASCII_8BIT).should equal(a) end end end describe "rb_str_conv_enc_opts" do it "returns the original String when to encoding is not specified" do - a = "abc".dup.force_encoding("us-ascii") + a = "abc".force_encoding("us-ascii") @s.rb_str_conv_enc_opts(a, Encoding::US_ASCII, nil, 0, nil).should equal(a) end @@ -949,7 +729,7 @@ describe "C-API String function" do end it "returns a transcoded String" do - a = "\xE3\x81\x82\xE3\x82\x8C".dup.force_encoding("utf-8") + a = "\xE3\x81\x82\xE3\x82\x8C".force_encoding("utf-8") result = @s.rb_str_conv_enc_opts(a, Encoding::UTF_8, Encoding::EUC_JP, 0, nil) x = [0xA4, 0xA2, 0xA4, 0xEC].pack('C4').force_encoding('utf-8') result.should == x.force_encoding("euc-jp") @@ -958,7 +738,7 @@ describe "C-API String function" do describe "when the String encoding is equal to the destination encoding" do it "returns the original String" do - a = "abc".dup.force_encoding("us-ascii") + a = "abc".force_encoding("us-ascii") @s.rb_str_conv_enc_opts(a, Encoding::US_ASCII, Encoding::US_ASCII, 0, nil).should equal(a) end @@ -969,10 +749,10 @@ describe "C-API String function" do Encoding::US_ASCII, 0, nil).should equal(a) end - it "returns the origin String if the destination encoding is BINARY" do - a = "abc".dup.force_encoding("binary") + it "returns the origin String if the destination encoding is ASCII-8BIT" do + a = "abc".force_encoding("ascii-8bit") @s.rb_str_conv_enc_opts(a, Encoding::US_ASCII, - Encoding::BINARY, 0, nil).should equal(a) + Encoding::ASCII_8BIT, 0, nil).should equal(a) end end end @@ -988,32 +768,11 @@ describe "C-API String function" do describe "rb_str_export_locale" do it "returns the original String with the locale encoding" do s = @s.rb_str_export_locale("abc") - s.should == "abc".dup.force_encoding(Encoding.find("locale")) + s.should == "abc".force_encoding(Encoding.find("locale")) s.encoding.should equal(Encoding.find("locale")) end end - describe "rb_str_export_to_enc" do - it "returns a copy of an ascii string converted to the new encoding" do - source = "A simple string".encode(Encoding::US_ASCII) - result = @s.rb_str_export_to_enc(source, Encoding::UTF_8) - result.should == source.encode(Encoding::UTF_8) - result.encoding.should == Encoding::UTF_8 - end - - it "returns the source string if it can not be converted" do - source = ["00ff"].pack("H*"); - result = @s.rb_str_export_to_enc(source, Encoding::UTF_8) - result.should equal(source) - end - - it "does not alter the source string if it can not be converted" do - source = ["00ff"].pack("H*"); - result = @s.rb_str_export_to_enc(source, Encoding::UTF_8) - source.bytes.should == [0, 255] - end -end - describe "rb_sprintf" do it "replaces the parts like sprintf" do @s.rb_sprintf1("Awesome %s is replaced", "string").should == "Awesome string is replaced" @@ -1024,73 +783,6 @@ end s = "Awesome %s is here with %s" @s.rb_sprintf2(s, "string", "content").should == "Awesome string is here with content" end - - it "formats a string VALUE using to_s if sign not specified in format" do - s = 'Result: A string.' - @s.rb_sprintf3(CApiStringSpecs::ToSOrInspect.new).should == s - end - - it "formats a string VALUE using inspect if sign specified in format" do - s = 'Result: A different string.' - @s.rb_sprintf4(CApiStringSpecs::ToSOrInspect.new).should == s - end - - it "formats a TrueClass VALUE as `TrueClass` if sign not specified in format" do - s = 'Result: TrueClass.' - @s.rb_sprintf3(true.class).should == s - end - - ruby_bug "#19167", ""..."3.2" do - it "formats a TrueClass VALUE as 'true' if sign specified in format" do - s = 'Result: TrueClass.' - @s.rb_sprintf4(true.class).should == s - end - end - - it "truncates a string to a supplied precision if that is shorter than the string" do - s = 'Result: Hel.' - @s.rb_sprintf5(0, 3, "Hello").should == s - end - - it "does not truncates a string to a supplied precision if that is longer than the string" do - s = 'Result: Hello.' - @s.rb_sprintf5(0, 8, "Hello").should == s - end - - it "pads a string to a supplied width if that is longer than the string" do - s = 'Result: Hello.' - @s.rb_sprintf5(8, 5, "Hello").should == s - end - - it "truncates a VALUE string to a supplied precision if that is shorter than the VALUE string" do - s = 'Result: Hel.' - @s.rb_sprintf6(0, 3, "Hello").should == s - end - - it "does not truncates a VALUE string to a supplied precision if that is longer than the VALUE string" do - s = 'Result: Hello.' - @s.rb_sprintf6(0, 8, "Hello").should == s - end - - it "pads a VALUE string to a supplied width if that is longer than the VALUE string" do - s = 'Result: Hello.' - @s.rb_sprintf6(8, 5, "Hello").should == s - end - - it "can format a nil VALUE as a pointer and gives the same output as sprintf in C" do - res = @s.rb_sprintf7("%p", nil); - res[0].should == res[1] - end - - it "can format a string VALUE as a pointer and gives the same output as sprintf in C" do - res = @s.rb_sprintf7("%p", "Hello") - res[0].should == res[1] - end - - it "can format a raw number a pointer and gives the same output as sprintf in C" do - res = @s.rb_sprintf7("%p", 0x223643); - res[0].should == res[1] - end end describe "rb_vsprintf" do @@ -1106,233 +798,15 @@ end end it "tries to convert the passed argument to a string by calling #to_str first" do - @s.rb_String(CApiStringSpecs::ValidTostrTest.new).should == "ruby" + @s.rb_String(ValidTostrTest.new).should == "ruby" end it "raises a TypeError if #to_str does not return a string" do - -> { @s.rb_String(CApiStringSpecs::InvalidTostrTest.new) }.should raise_error(TypeError) + lambda { @s.rb_String(InvalidTostrTest.new) }.should raise_error(TypeError) end it "tries to convert the passed argument to a string by calling #to_s" do - @s.rb_String({"bar" => "foo"}).should == {"bar" => "foo"}.to_s - end - end - - describe "rb_string_value_cstr" do - it "returns a non-null pointer for a simple string" do - @s.rb_string_value_cstr("Hello").should == true - end - - it "returns a non-null pointer for a UTF-16 string" do - @s.rb_string_value_cstr("Hello".encode('UTF-16BE')).should == true - end - - it "raises an error if a string contains a null" do - -> { @s.rb_string_value_cstr("Hello\0 with a null.") }.should raise_error(ArgumentError) - end - - it "raises an error if a UTF-16 string contains a null" do - -> { @s.rb_string_value_cstr("Hello\0 with a null.".encode('UTF-16BE')) }.should raise_error(ArgumentError) - end - - end - - describe "rb_str_drop_bytes" do - it "drops N characters for an ASCII string" do - str = "12345678".encode("US-ASCII") - @s.rb_str_drop_bytes(str, 4) - str.should == "5678".encode("US-ASCII") - end - - it "drop N/2 characters for a UTF-16 string" do - str = "12345678".encode("UTF-16LE") - @s.rb_str_drop_bytes(str, 4) - str.should == "345678".encode("UTF-16LE") - end - - it "drop N/4 characters for a UTF-32 string" do - str = "12345678".encode("UTF-32LE") - @s.rb_str_drop_bytes(str, 4) - str.should == "2345678".encode("UTF-32LE") - end - end - - describe "rb_utf8_str_new_static" do - it "returns a UTF-8 string of the correct characters and length" do - str = @s.rb_utf8_str_new_static - str.should == "nokogiri" - str.encoding.should == Encoding::UTF_8 - end - end - - describe "rb_utf8_str_new" do - it "returns a UTF-8 string of the correct characters and length" do - str = @s.rb_utf8_str_new - str.should == "nokogiri" - str.encoding.should == Encoding::UTF_8 - end - end - - describe "rb_utf8_str_new_cstr" do - it "returns a UTF-8 string of the correct characters and length" do - str = @s.rb_utf8_str_new_cstr - str.should == "nokogiri" - str.encoding.should == Encoding::UTF_8 - end - end - - describe "rb_str_vcatf" do - it "appends the message to the string" do - @s.rb_str_vcatf("").should == "fmt 42 7 number" - - str = "test " - @s.rb_str_vcatf(str) - str.should == "test fmt 42 7 number" - end - end - - describe "rb_str_catf" do - it "appends the message to the string" do - @s.rb_str_catf("").should == "fmt 41 6 number" - - str = "test " - @s.rb_str_catf(str) - str.should == "test fmt 41 6 number" - end - end - - describe "rb_str_locktmp" do - it "raises an error when trying to lock an already locked string" do - str = "test" - @s.rb_str_locktmp(str).should == str - -> { @s.rb_str_locktmp(str) }.should raise_error(RuntimeError, 'temporal locking already locked string') - end - - it "locks a string so that modifications would raise an error" do - str = "test" - @s.rb_str_locktmp(str).should == str - -> { str.upcase! }.should raise_error(RuntimeError, 'can\'t modify string; temporarily locked') - end - end - - describe "rb_str_unlocktmp" do - it "unlocks a locked string" do - str = "test" - @s.rb_str_locktmp(str) - @s.rb_str_unlocktmp(str).should == str - str.upcase!.should == "TEST" - end - - it "raises an error when trying to unlock an already unlocked string" do - -> { @s.rb_str_unlocktmp("test") }.should raise_error(RuntimeError, 'temporal unlocking already unlocked string') - end - end - - describe "rb_enc_interned_str_cstr" do - it "returns a frozen string" do - str = "hello" - val = @s.rb_enc_interned_str_cstr(str, Encoding::US_ASCII) - - val.should.is_a?(String) - val.encoding.should == Encoding::US_ASCII - val.should.frozen? - end - - it "returns the same frozen string" do - str = "hello" - result1 = @s.rb_enc_interned_str_cstr(str, Encoding::US_ASCII) - result2 = @s.rb_enc_interned_str_cstr(str, Encoding::US_ASCII) - result1.should.equal?(result2) - end - - it "returns different frozen strings for different encodings" do - str = "hello" - result1 = @s.rb_enc_interned_str_cstr(str, Encoding::US_ASCII) - result2 = @s.rb_enc_interned_str_cstr(str, Encoding::UTF_8) - result1.should_not.equal?(result2) - end - - it "returns the same string as String#-@" do - @s.rb_enc_interned_str_cstr("hello", Encoding::UTF_8).should.equal?(-"hello") - end - - ruby_bug "#20322", ""..."3.4" do - it "uses the default encoding if encoding is null" do - str = "hello" - val = @s.rb_enc_interned_str_cstr(str, nil) - val.encoding.should == Encoding::ASCII_8BIT - end - end - end - - describe "rb_enc_interned_str" do - it "returns a frozen string" do - str = "hello" - val = @s.rb_enc_interned_str(str, str.bytesize, Encoding::US_ASCII) - - val.should.is_a?(String) - val.encoding.should == Encoding::US_ASCII - val.should.frozen? - end - - it "returns the same frozen string" do - str = "hello" - result1 = @s.rb_enc_interned_str(str, str.bytesize, Encoding::US_ASCII) - result2 = @s.rb_enc_interned_str(str, str.bytesize, Encoding::US_ASCII) - result1.should.equal?(result2) - end - - it "returns different frozen strings for different encodings" do - str = "hello" - result1 = @s.rb_enc_interned_str(str, str.bytesize, Encoding::US_ASCII) - result2 = @s.rb_enc_interned_str(str, str.bytesize, Encoding::UTF_8) - result1.should_not.equal?(result2) - end - - it 'returns the same string when using non-ascii characters' do - str = 'こんにちは' - result1 = @s.rb_enc_interned_str(str, str.bytesize, Encoding::UTF_8) - result2 = @s.rb_enc_interned_str(str, str.bytesize, Encoding::UTF_8) - result1.should.equal?(result2) - end - - it "returns the same string as String#-@" do - str = "hello" - @s.rb_enc_interned_str(str, str.bytesize, Encoding::UTF_8).should.equal?(-str) - end - - ruby_bug "#20322", ""..."3.4" do - it "uses the default encoding if encoding is null" do - str = "hello" - val = @s.rb_enc_interned_str(str, str.bytesize, nil) - val.encoding.should == Encoding::ASCII_8BIT - end - end - end - - describe "rb_str_to_interned_str" do - it "returns a frozen string" do - str = "hello" - result = @s.rb_str_to_interned_str(str) - result.should.is_a?(String) - result.should.frozen? - end - - it "returns the same frozen string" do - str = "hello" - result1 = @s.rb_str_to_interned_str(str) - result2 = @s.rb_str_to_interned_str(str) - result1.should.equal?(result2) - end - - it "returns different frozen strings for different encodings" do - result1 = @s.rb_str_to_interned_str("hello".dup.force_encoding(Encoding::US_ASCII)) - result2 = @s.rb_str_to_interned_str("hello".dup.force_encoding(Encoding::UTF_8)) - result1.should_not.equal?(result2) - end - - it "returns the same string as String#-@" do - @s.rb_str_to_interned_str("hello").should.equal?(-"hello") + @s.rb_String({"bar" => "foo"}).should == '{"bar"=>"foo"}' end end end diff --git a/spec/ruby/optional/capi/struct_spec.rb b/spec/ruby/optional/capi/struct_spec.rb index 0e9e366908..9a0eafeb7b 100644 --- a/spec/ruby/optional/capi/struct_spec.rb +++ b/spec/ruby/optional/capi/struct_spec.rb @@ -1,4 +1,4 @@ -require_relative 'spec_helper' +require File.expand_path('../spec_helper', __FILE__) load_extension("struct") @@ -62,9 +62,9 @@ describe "C-API Struct function" do end describe "C-API Struct function" do - before :all do + before :each do @s = CApiStructSpecs.new - @struct = @s.rb_struct_define_under(CApiStructSpecs, "CAPIStructUnder", "a", "b", "c") + @struct = @s.rb_struct_define_under(CApiStructSpecs, "CAPIStruct", "a", "b", "c") end describe "rb_struct_define_under" do @@ -80,15 +80,11 @@ describe "C-API Struct function" do it "has a value of nil for the member of a newly created instance" do # Verify that attributes are on an instance basis - CApiStructSpecs::CAPIStructUnder.new.b.should be_nil - end - - it "does not create a constant scoped under Struct for the named Struct" do - Struct.should_not have_constant(:CAPIStructUnder) + CApiStructSpecs::CAPIStruct.new.b.should be_nil end it "creates a constant scoped under the namespace of the given class" do - CApiStructSpecs.should have_constant(:CAPIStructUnder) + CApiStructSpecs.should have_constant(:CAPIStruct) end it "returns the member names as Symbols" do @@ -106,11 +102,11 @@ describe "C-API Struct function" do describe "rb_struct_define" do it "raises an ArgumentError if arguments contain duplicate member name" do - -> { @s.rb_struct_define(nil, "a", "b", "a") }.should raise_error(ArgumentError) + lambda { @s.rb_struct_define(nil, "a", "b", "a") }.should raise_error(ArgumentError) end it "raises a NameError if an invalid constant name is given" do - -> { @s.rb_struct_define("foo", "a", "b", "c") }.should raise_error(NameError) + lambda { @s.rb_struct_define("foo", "a", "b", "c") }.should raise_error(NameError) end end @@ -131,12 +127,12 @@ describe "C-API Struct function" do end it "raises a NameError if the struct member does not exist" do - -> { @s.rb_struct_aref(@struct, :d) }.should raise_error(NameError) + lambda { @s.rb_struct_aref(@struct, :d) }.should raise_error(NameError) end it "raises an IndexError if the given index is out of range" do - -> { @s.rb_struct_aref(@struct, -4) }.should raise_error(IndexError) - -> { @s.rb_struct_aref(@struct, 3) }.should raise_error(IndexError) + lambda { @s.rb_struct_aref(@struct, -4) }.should raise_error(IndexError) + lambda { @s.rb_struct_aref(@struct, 3) }.should raise_error(IndexError) end end @@ -147,7 +143,7 @@ describe "C-API Struct function" do end it "raises a NameError if the struct member does not exist" do - -> { @s.rb_struct_getmember(@struct, :d) }.should raise_error(NameError) + lambda { @s.rb_struct_getmember(@struct, :d) }.should raise_error(NameError) end end @@ -180,17 +176,17 @@ describe "C-API Struct function" do end it "raises a NameError if the struct member does not exist" do - -> { @s.rb_struct_aset(@struct, :d, 1) }.should raise_error(NameError) + lambda { @s.rb_struct_aset(@struct, :d, 1) }.should raise_error(NameError) end it "raises an IndexError if the given index is out of range" do - -> { @s.rb_struct_aset(@struct, -4, 1) }.should raise_error(IndexError) - -> { @s.rb_struct_aset(@struct, 3, 1) }.should raise_error(IndexError) + lambda { @s.rb_struct_aset(@struct, -4, 1) }.should raise_error(IndexError) + lambda { @s.rb_struct_aset(@struct, 3, 1) }.should raise_error(IndexError) end - it "raises a FrozenError if the struct is frozen" do + it "raises a RuntimeError if the struct is frozen" do @struct.freeze - -> { @s.rb_struct_aset(@struct, :a, 1) }.should raise_error(FrozenError) + lambda { @s.rb_struct_aset(@struct, :a, 1) }.should raise_error(RuntimeError) end end @@ -203,9 +199,11 @@ describe "C-API Struct function" do end end - describe "rb_struct_size" do - it "returns the number of struct members" do - @s.rb_struct_size(@struct).should == 3 + ruby_version_is "2.4" do + describe "rb_struct_size" do + it "returns the number of struct members" do + @s.rb_struct_size(@struct).should == 3 + end end end end diff --git a/spec/ruby/optional/capi/symbol_spec.rb b/spec/ruby/optional/capi/symbol_spec.rb index 12c93c9f27..b6532f4a4e 100644 --- a/spec/ruby/optional/capi/symbol_spec.rb +++ b/spec/ruby/optional/capi/symbol_spec.rb @@ -1,5 +1,5 @@ # -*- encoding: utf-8 -*- -require_relative 'spec_helper' +require File.expand_path('../spec_helper', __FILE__) load_extension('symbol') @@ -8,16 +8,6 @@ describe "C-API Symbol function" do @s = CApiSymbolSpecs.new end - describe "SYMBOL_P" do - it "returns true for a Symbol" do - @s.SYMBOL_P(:foo).should == true - end - - it "returns false for non-Symbols" do - @s.SYMBOL_P('bar').should == false - end - end - describe "rb_intern" do it "converts a string to a symbol, uniquely" do @s.rb_intern("test_symbol").should == :test_symbol @@ -61,10 +51,6 @@ describe "C-API Symbol function" do it "converts a symbol to a C char array" do @s.rb_id2name(:test_symbol).should == "test_symbol" end - - it "returns (char*) NULL for (ID) 0" do - @s.rb_id2name_id_zero.should == nil - end end describe "rb_id2str" do @@ -76,10 +62,6 @@ describe "C-API Symbol function" do str = "test_symbol".encode(Encoding::UTF_16LE) @s.rb_id2str(str.to_sym).encoding.should == Encoding::UTF_16LE end - - it "returns (VALUE) 0 = Qfalse for (ID) 0" do - @s.rb_id2str_id_zero.should == false - end end describe "rb_intern_str" do @@ -89,19 +71,6 @@ describe "C-API Symbol function" do end end - describe "rb_check_symbol_cstr" do - it "returns a Symbol if a Symbol already exists for the given C string" do - sym = :test_symbol - @s.rb_check_symbol_cstr('test_symbol').should == sym - end - - it "returns nil if the Symbol does not exist yet and does not create it" do - str = "symbol_does_not_exist_#{Object.new.object_id}_#{rand}" - @s.rb_check_symbol_cstr(str).should == nil # does not create the Symbol - @s.rb_check_symbol_cstr(str).should == nil - end - end - describe "rb_is_const_id" do it "returns true given a const-like symbol" do @s.rb_is_const_id(:Foo).should == true @@ -161,20 +130,4 @@ describe "C-API Symbol function" do @s.rb_sym2str(:bacon).should == "bacon" end end - - describe "rb_to_symbol" do - it "returns a Symbol for a Symbol" do - @s.rb_to_symbol(:foo).should == :foo - end - - it "returns a Symbol for a String" do - @s.rb_to_symbol("foo").should == :foo - end - - it "coerces to Symbol using to_str" do - o = mock('o') - o.should_receive(:to_str).and_return("foo") - @s.rb_to_symbol(o).should == :foo - end - end end diff --git a/spec/ruby/optional/capi/thread_spec.rb b/spec/ruby/optional/capi/thread_spec.rb index af641f0564..ab3d609bcf 100644 --- a/spec/ruby/optional/capi/thread_spec.rb +++ b/spec/ruby/optional/capi/thread_spec.rb @@ -1,5 +1,5 @@ -require_relative 'spec_helper' -require_relative '../../core/thread/shared/wakeup' +require File.expand_path('../spec_helper', __FILE__) +require File.expand_path('../../../core/thread/shared/wakeup', __FILE__) load_extension("thread") @@ -24,7 +24,7 @@ describe "C-API Thread function" do it "sleeps the current thread for the give amount of time" do start = Time.now @t.rb_thread_wait_for(0, 100_000) - (Time.now - start).should be_close(0.1, TIME_TOLERANCE) + (Time.now - start).should be_close(0.1, 0.2) end end @@ -70,7 +70,7 @@ describe "C-API Thread function" do describe "rb_thread_create" do it "creates a new thread" do obj = Object.new - proc = -> x { ScratchPad.record x } + proc = lambda { |x| ScratchPad.record x } thr = @t.rb_thread_create(proc, obj) thr.should be_kind_of(Thread) thr.join @@ -78,20 +78,20 @@ describe "C-API Thread function" do end it "handles throwing an exception in the thread" do - prc = -> x { + prc = lambda { |x| Thread.current.report_on_exception = false raise "my error" } thr = @t.rb_thread_create(prc, nil) thr.should be_kind_of(Thread) - -> { + lambda { thr.join }.should raise_error(RuntimeError, "my error") end it "sets the thread's group" do - thr = @t.rb_thread_create(-> x { }, nil) + thr = @t.rb_thread_create(lambda { |x| }, nil) begin thread_group = thr.group thread_group.should be_an_instance_of(ThreadGroup) @@ -101,24 +101,14 @@ describe "C-API Thread function" do end end - describe "ruby_native_thread_p" do - it "returns non-zero for a ruby thread" do - @t.ruby_native_thread_p.should be_true - end - - it "returns zero for a non ruby thread" do - @t.ruby_native_thread_p_new_thread.should be_false - end - end - describe "rb_thread_call_without_gvl" do - it "runs a C function with the global lock unlocked and can be woken by Thread#wakeup" do + it "runs a C function with the global lock unlocked" do thr = Thread.new do @t.rb_thread_call_without_gvl end # Wait until it's blocking... - Thread.pass until thr.stop? + Thread.pass while thr.status and thr.status != "sleep" # The thread status is set to sleep by rb_thread_call_without_gvl(), # but the thread might not be in the blocking read(2) yet, so wait a bit. @@ -127,60 +117,10 @@ describe "C-API Thread function" do # Wake it up, causing the unblock function to be run. thr.wakeup - # Make sure it stopped and we got a proper value - thr.value.should be_true - end - - platform_is_not :windows do - it "runs a C function with the global lock unlocked and can be woken by a signal" do - # Ruby signal handlers run on the main thread, so we need to reverse roles here and have a thread interrupt us - thr = Thread.current - thr.should == Thread.main - - going_to_block = false - interrupter = Thread.new do - # Wait until it's blocking... - Thread.pass until going_to_block and thr.stop? - - # The thread status is set to sleep by rb_thread_call_without_gvl(), - # but the thread might not be in the blocking read(2) yet, so wait a bit. - sleep 0.1 - - # Wake it up by sending a signal - done = false - prev_handler = Signal.trap(:HUP) { done = true } - begin - Process.kill :HUP, Process.pid - sleep 0.001 until done - ensure - Signal.trap(:HUP, prev_handler) - end - end - - going_to_block = true - # Make sure it stopped and we got a proper value - @t.rb_thread_call_without_gvl.should be_true - - interrupter.join - end - end - - it "runs a C function with the global lock unlocked and unlocks IO with the generic RUBY_UBF_IO" do - thr = Thread.new do - @t.rb_thread_call_without_gvl_with_ubf_io - end - - # Wait until it's blocking... - Thread.pass until thr.stop? - - # The thread status is set to sleep by rb_thread_call_without_gvl(), - # but the thread might not be in the blocking read(2) yet, so wait a bit. - sleep 0.1 - - # Wake it up, causing the unblock function to be run. - thr.wakeup + # Make sure it stopped + thr.join(1).should_not be_nil - # Make sure it stopped and we got a proper value + # And we got a proper value thr.value.should be_true end end diff --git a/spec/ruby/optional/capi/time_spec.rb b/spec/ruby/optional/capi/time_spec.rb index ca5fc5952a..c8f0d9d4e0 100644 --- a/spec/ruby/optional/capi/time_spec.rb +++ b/spec/ruby/optional/capi/time_spec.rb @@ -1,4 +1,4 @@ -require_relative 'spec_helper' +require File.expand_path('../spec_helper', __FILE__) load_extension("time") @@ -96,9 +96,9 @@ describe "CApiTimeSpecs" do end it "throws an argument error for a negative value" do - -> { @s.rb_time_interval(-1232141421) }.should raise_error(ArgumentError) - -> { @s.rb_time_interval(Rational(-3, 2)) }.should raise_error(ArgumentError) - -> { @s.rb_time_interval(-1.5) }.should raise_error(ArgumentError) + lambda { @s.rb_time_interval(-1232141421) }.should raise_error(ArgumentError) + lambda { @s.rb_time_interval(Rational(-3, 2)) }.should raise_error(ArgumentError) + lambda { @s.rb_time_interval(-1.5) }.should raise_error(ArgumentError) end end @@ -129,13 +129,13 @@ describe "CApiTimeSpecs" do end it "throws an argument error for a negative value" do - -> { @s.rb_time_interval(-1232141421) }.should raise_error(ArgumentError) - -> { @s.rb_time_interval(Rational(-3, 2)) }.should raise_error(ArgumentError) - -> { @s.rb_time_interval(-1.5) }.should raise_error(ArgumentError) + lambda { @s.rb_time_interval(-1232141421) }.should raise_error(ArgumentError) + lambda { @s.rb_time_interval(Rational(-3, 2)) }.should raise_error(ArgumentError) + lambda { @s.rb_time_interval(-1.5) }.should raise_error(ArgumentError) end it "throws an argument error when given a Time instance" do - -> { @s.rb_time_interval(Time.now) }.should raise_error(TypeError) + lambda { @s.rb_time_interval(Time.now) }.should raise_error(TypeError) end end @@ -165,28 +165,30 @@ describe "CApiTimeSpecs" do usec.should == 500000 end - it "creates a timeval for a negative Fixnum" do - sec, usec = @s.rb_time_timeval(-1232141421) - sec.should be_kind_of(Integer) - sec.should == -1232141421 - usec.should be_kind_of(Integer) - usec.should == 0 - end + platform_is_not :mingw32 do + it "creates a timeval for a negative Fixnum" do + sec, usec = @s.rb_time_timeval(-1232141421) + sec.should be_kind_of(Integer) + sec.should == -1232141421 + usec.should be_kind_of(Integer) + usec.should == 0 + end - it "creates a timeval for a negative Float" do - sec, usec = @s.rb_time_timeval(-1.5) - sec.should be_kind_of(Integer) - sec.should == -2 - usec.should be_kind_of(Integer) - usec.should == 500000 - end + it "creates a timeval for a negative Float" do + sec, usec = @s.rb_time_timeval(-1.5) + sec.should be_kind_of(Integer) + sec.should == -2 + usec.should be_kind_of(Integer) + usec.should == 500000 + end - it "creates a timeval for a negative Rational" do - sec, usec = @s.rb_time_timeval(Rational(-3, 2)) - sec.should be_kind_of(Integer) - sec.should == -2 - usec.should be_kind_of(Integer) - usec.should == 500000 + it "creates a timeval for a negative Rational" do + sec, usec = @s.rb_time_timeval(Rational(-3, 2)) + sec.should be_kind_of(Integer) + sec.should == -2 + usec.should be_kind_of(Integer) + usec.should == 500000 + end end it "creates a timeval from a Time object" do @@ -222,28 +224,30 @@ describe "CApiTimeSpecs" do nsec.should == 500000000 end - it "creates a timespec for a negative Fixnum" do - sec, nsec = @s.rb_time_timespec(-1232141421) - sec.should be_kind_of(Integer) - sec.should == -1232141421 - nsec.should be_kind_of(Integer) - nsec.should == 0 - end + platform_is_not :mingw32 do + it "creates a timespec for a negative Fixnum" do + sec, nsec = @s.rb_time_timespec(-1232141421) + sec.should be_kind_of(Integer) + sec.should == -1232141421 + nsec.should be_kind_of(Integer) + nsec.should == 0 + end - it "creates a timespec for a negative Float" do - sec, nsec = @s.rb_time_timespec(-1.5) - sec.should be_kind_of(Integer) - sec.should == -2 - nsec.should be_kind_of(Integer) - nsec.should == 500000000 - end + it "creates a timespec for a negative Float" do + sec, nsec = @s.rb_time_timespec(-1.5) + sec.should be_kind_of(Integer) + sec.should == -2 + nsec.should be_kind_of(Integer) + nsec.should == 500000000 + end - it "creates a timespec for a negative Rational" do - sec, nsec = @s.rb_time_timespec(Rational(-3, 2)) - sec.should be_kind_of(Integer) - sec.should == -2 - nsec.should be_kind_of(Integer) - nsec.should == 500000000 + it "creates a timespec for a negative Rational" do + sec, nsec = @s.rb_time_timespec(Rational(-3, 2)) + sec.should be_kind_of(Integer) + sec.should == -2 + nsec.should be_kind_of(Integer) + nsec.should == 500000000 + end end it "creates a timespec from a Time object" do @@ -254,48 +258,45 @@ describe "CApiTimeSpecs" do end end - describe "rb_time_timespec_new" do - it "returns a time object with the given timespec and UTC offset" do - @s.rb_time_timespec_new(1447087832, 476451125, 32400).should == Time.at(1447087832, 476451.125).localtime(32400) - end - - describe "when offset given is within range of -86400 and 86400 (exclusive)" do - it "sets time's is_gmt to false" do - @s.rb_time_timespec_new(1447087832, 476451125, 0).gmt?.should be_false + ruby_version_is "2.3" do + describe "rb_time_timespec_new" do + it "returns a time object with the given timespec and UTC offset" do + @s.rb_time_timespec_new(1447087832, 476451125, 32400).should == Time.at(1447087832, 476451.125).localtime(32400) end - it "sets time's offset to the offset given" do - @s.rb_time_timespec_new(1447087832, 476451125, 86399).gmtoff.should == 86399 - end - end + describe "when offset given is within range of -86400 and 86400 (exclusive)" do + it "sets time's is_gmt to false" do + @s.rb_time_timespec_new(1447087832, 476451125, 0).gmt?.should be_false + end - it "returns time object in UTC if offset given equals INT_MAX - 1" do - @s.rb_time_timespec_new(1447087832, 476451125, 0x7ffffffe).utc?.should be_true - end + it "sets time's offset to the offset given" do + @s.rb_time_timespec_new(1447087832, 476451125, 86399).gmtoff.should == 86399 + end + end - it "returns time object in localtime if offset given equals INT_MAX" do - @s.rb_time_timespec_new(1447087832, 476451125, 0x7fffffff).should == Time.at(1447087832, 476451.125).localtime - t = Time.now - @s.rb_time_timespec_new(t.tv_sec, t.tv_nsec, 0x7fffffff).gmtoff.should == t.gmtoff - end + it "returns time object in UTC if offset given equals INT_MAX - 1" do + @s.rb_time_timespec_new(1447087832, 476451125, 0x7ffffffe).utc?.should be_true + end - it "raises an ArgumentError if offset passed is not within range of -86400 and 86400 (exclusive)" do - -> { @s.rb_time_timespec_new(1447087832, 476451125, 86400) }.should raise_error(ArgumentError) - -> { @s.rb_time_timespec_new(1447087832, 476451125, -86400) }.should raise_error(ArgumentError) - end + it "returns time object in localtime if offset given equals INT_MAX" do + @s.rb_time_timespec_new(1447087832, 476451125, 0x7fffffff).should == Time.at(1447087832, 476451.125).localtime + t = Time.now + @s.rb_time_timespec_new(t.tv_sec, t.tv_nsec, 0x7fffffff).gmtoff.should == t.gmtoff + end - it "doesn't call Time.at directly" do - Time.should_not_receive(:at) - @s.rb_time_timespec_new(1447087832, 476451125, 32400).should be_kind_of(Time) + it "raises an ArgumentError if offset passed is not within range of -86400 and 86400 (exclusive)" do + lambda { @s.rb_time_timespec_new(1447087832, 476451125, 86400) }.should raise_error(ArgumentError) + lambda { @s.rb_time_timespec_new(1447087832, 476451125, -86400) }.should raise_error(ArgumentError) + end end - end - describe "rb_timespec_now" do - it "fills a struct timespec with the current time" do - now = Time.now - time = @s.rb_time_from_timespec(now.utc_offset) - time.should be_an_instance_of(Time) - (time - now).should be_close(0, TIME_TOLERANCE) + describe "rb_timespec_now" do + it "fills a struct timespec with the current time" do + now = Time.now + time = @s.rb_time_from_timespec(now.utc_offset) + time.should be_an_instance_of(Time) + (time - now).should be_close(0, 10) + end end end end diff --git a/spec/ruby/optional/capi/tracepoint_spec.rb b/spec/ruby/optional/capi/tracepoint_spec.rb deleted file mode 100644 index 2043b7c941..0000000000 --- a/spec/ruby/optional/capi/tracepoint_spec.rb +++ /dev/null @@ -1,56 +0,0 @@ -require_relative 'spec_helper' - -load_extension("tracepoint") - -describe "CApiTracePointSpecs" do - before :each do - @s = CApiTracePointSpecs.new - end - - after :each do - @trace.disable if @trace and @trace.enabled? - end - - describe "rb_tracepoint_new" do - it "returns a tracepoint object" do - @trace = @s.rb_tracepoint_new(7) - @trace.should be_an_instance_of(TracePoint) - @trace.should_not.enabled? - end - - it "traces lines when given RUBY_EVENT_LINE" do - @trace = @s.rb_tracepoint_new(8) - @trace.enable - @s.callback_called?.should == 8 - end - end - - describe "rb_tracepoint_disable" do - it "disables an enabled TracePoint" do - @trace = @s.rb_tracepoint_new(9) - @trace.should_not.enabled? - @trace.enable - @trace.should.enabled? - @s.rb_tracepoint_disable(@trace).should == false - @trace.should_not.enabled? - end - end - - describe "rb_tracepoint_enable" do - it "enables a disabled TracePoint" do - @trace = @s.rb_tracepoint_new(10) - @trace.should_not.enabled? - @s.rb_tracepoint_enable(@trace).should == true - @trace.should.enabled? - end - end - - describe "rb_tracepoint_enabled_p" do - it "returns correct enabled status" do - @trace = @s.rb_tracepoint_new(11) - @s.rb_tracepoint_enabled_p(@trace).should == false - @trace.enable - @s.rb_tracepoint_enabled_p(@trace).should == true - end - end -end diff --git a/spec/ruby/optional/capi/typed_data_spec.rb b/spec/ruby/optional/capi/typed_data_spec.rb index 6d1398a1a0..1b2852d349 100644 --- a/spec/ruby/optional/capi/typed_data_spec.rb +++ b/spec/ruby/optional/capi/typed_data_spec.rb @@ -1,5 +1,4 @@ -require_relative 'spec_helper' -require 'objspace' +require File.expand_path('../spec_helper', __FILE__) load_extension("typed_data") @@ -8,14 +7,6 @@ describe "CApiAllocTypedSpecs (a class with an alloc func defined)" do @s = CApiAllocTypedSpecs.new @s.typed_wrapped_data.should == 42 # not defined in initialize end - - it "uses the specified memsize function for ObjectSpace.memsize" do - @s = CApiAllocTypedSpecs.new - # The defined memsize function for the type should return 42 as - # the size, and this should be added to the size of the object as - # known by Ruby. - ObjectSpace.memsize_of(@s).should > 42 - end end describe "CApiWrappedTypedStruct" do @@ -30,7 +21,7 @@ describe "CApiWrappedTypedStruct" do it "throws an exception for a wrong type" do a = @s.typed_wrap_struct(1024) - -> { @s.typed_get_struct_other(a) }.should raise_error(TypeError) + lambda { @s.typed_get_struct_other(a) }.should raise_error(TypeError) end it "unwraps data for a parent type" do @@ -38,6 +29,11 @@ describe "CApiWrappedTypedStruct" do @s.typed_get_struct_parent(a).should == 1024 end + it "allows for using NULL as the klass for Data_Wrap_Struct" do + a = @s.typed_wrap_struct_null(1024) + @s.typed_get_struct(a).should == 1024 + end + describe "RTYPEDATA" do it "returns the struct data" do a = @s.typed_wrap_struct(1024) @@ -57,44 +53,4 @@ describe "CApiWrappedTypedStruct" do @s.typed_get_struct_data_ptr(a).should == 1024 end end - - describe "rb_check_type" do - it "raises an exception when checking typed data objects" do - -> { - a = @s.typed_wrap_struct(1024) - @s.rb_check_type(a, a) - }.should raise_error(TypeError) { |e| - e.message.should == 'wrong argument type Object (expected Data)' - } - end - end - - describe "rb_check_typeddata" do - it "returns data pointer when the struct has the given type" do - a = @s.typed_wrap_struct(1024) - @s.rb_check_typeddata_same_type(a).should == true - end - - it "returns data pointer when the parent struct has the given type" do - a = @s.typed_wrap_struct(1024) - @s.rb_check_typeddata_same_type_parent(a).should == true - end - - it "raises an error for different types" do - a = @s.typed_wrap_struct(1024) - -> { @s.rb_check_typeddata_different_type(a) }.should raise_error(TypeError) - end - end - - describe "RTYPEDDATA_P" do - it "returns true for a typed data" do - a = @s.typed_wrap_struct(1024) - @s.RTYPEDDATA_P(a).should == true - end - - it "returns false for an untyped data object" do - a = @s.untyped_wrap_struct(1024) - @s.RTYPEDDATA_P(a).should == false - end - end end diff --git a/spec/ruby/optional/capi/util_spec.rb b/spec/ruby/optional/capi/util_spec.rb index 6cf064bf97..b2ef2ba92e 100644 --- a/spec/ruby/optional/capi/util_spec.rb +++ b/spec/ruby/optional/capi/util_spec.rb @@ -1,4 +1,4 @@ -require_relative 'spec_helper' +require File.expand_path('../spec_helper', __FILE__) load_extension('util') @@ -9,23 +9,18 @@ describe "C-API Util function" do describe "rb_scan_args" do before :each do - @prc = -> { 1 } + @prc = lambda { 1 } @acc = [] ScratchPad.record @acc end it "assigns the required arguments scanned" do - obj = Object.new - @o.rb_scan_args([obj, 2], "2", 2, @acc).should == 2 - ScratchPad.recorded.should == [obj, 2] + @o.rb_scan_args([1, 2], "2", 2, @acc).should == 2 + ScratchPad.recorded.should == [1, 2] end it "raises an ArgumentError if there are insufficient arguments" do - -> { @o.rb_scan_args([1, 2], "3", 0, @acc) }.should raise_error(ArgumentError) - end - - it "raises an ArgumentError if there are too many arguments" do - -> { @o.rb_scan_args([1, 2, 3, 4], "3", 0, @acc) }.should raise_error(ArgumentError) + lambda { @o.rb_scan_args([1, 2], "3", 0, @acc) }.should raise_error(ArgumentError) end it "assigns the required and optional arguments scanned" do @@ -48,7 +43,7 @@ describe "C-API Util function" do ScratchPad.recorded.should == [1, 2, [3, 4]] end - it "assigns the required and optional arguments and empty Array when there are no arguments to splat" do + it "assigns the required and optional arguments and and empty Array when there are no arguments to splat" do @o.rb_scan_args([1, 2], "11*", 3, @acc).should == 2 ScratchPad.recorded.should == [1, 2, []] end @@ -100,13 +95,13 @@ describe "C-API Util function" do it "assigns Hash arguments" do h = {a: 1, b: 2} - @o.rb_scan_args([h], "k0:", 1, @acc).should == 0 + @o.rb_scan_args([h], "0:", 1, @acc).should == 0 ScratchPad.recorded.should == [h] end it "assigns required and Hash arguments" do h = {a: 1, b: 2} - @o.rb_scan_args([1, h], "k1:", 2, @acc).should == 1 + @o.rb_scan_args([1, h], "1:", 2, @acc).should == 1 ScratchPad.recorded.should == [1, h] end @@ -115,104 +110,45 @@ describe "C-API Util function" do ScratchPad.recorded.should == [1, nil] end - it "rejects the use of nil as a hash" do - -> { - @o.rb_scan_args([1, nil], "1:", 2, @acc).should == 1 - }.should raise_error(ArgumentError) - ScratchPad.recorded.should == [] - end - - it "assigns required and optional arguments with no hash argument given" do - @o.rb_scan_args([1, 7, 4], "21:", 3, @acc).should == 3 - ScratchPad.recorded.should == [1, 7, 4] - end - - it "assigns optional arguments with no hash argument given" do - @o.rb_scan_args([1, 7], "02:", 3, @acc).should == 2 - ScratchPad.recorded.should == [1, 7, nil] - end - - it "assigns optional arguments with no hash argument given and rejects the use of optional nil argument as a hash" do - -> { - @o.rb_scan_args([1, nil], "02:", 3, @acc).should == 2 - }.should_not complain - - ScratchPad.recorded.should == [1, nil, nil] + it "assigns required and Hash arguments with nil Hash" do + @o.rb_scan_args([1, nil], "1:", 2, @acc).should == 1 + ScratchPad.recorded.should == [1, nil] end it "assigns required, optional, splat, post-splat, Hash and block arguments" do h = {a: 1, b: 2} - @o.rb_scan_args([1, 2, 3, 4, 5, h], "k11*1:&", 6, @acc, &@prc).should == 5 + @o.rb_scan_args([1, 2, 3, 4, 5, h], "11*1:&", 6, @acc, &@prc).should == 5 ScratchPad.recorded.should == [1, 2, [3, 4], 5, h, @prc] end - it "does not reject non-symbol keys in keyword arguments" do + # r43934 + it "rejects non-keyword arguments" do h = {1 => 2, 3 => 4} - @o.rb_scan_args([h], "k0:", 1, @acc).should == 0 - ScratchPad.recorded.should == [h] + lambda { + @o.rb_scan_args([h], "0:", 1, @acc) + }.should raise_error(ArgumentError) + ScratchPad.recorded.should == [] end - it "does not reject non-symbol keys in keyword arguments with required argument" do + it "rejects required and non-keyword arguments" do h = {1 => 2, 3 => 4} - @o.rb_scan_args([1, h], "k1:", 2, @acc).should == 1 - ScratchPad.recorded.should == [1, h] + lambda { + @o.rb_scan_args([1, h], "1:", 2, @acc) + }.should raise_error(ArgumentError) + ScratchPad.recorded.should == [] end - it "considers keyword arguments with non-symbol keys as keywords when using splat and post arguments" do + it "considers the hash as a post argument when there is a splat" do h = {1 => 2, 3 => 4} - @o.rb_scan_args([1, 2, 3, 4, 5, h], "k11*1:&", 6, @acc, &@prc).should == 5 - ScratchPad.recorded.should == [1, 2, [3, 4], 5, h, @prc] + @o.rb_scan_args([1, 2, 3, 4, 5, h], "11*1:&", 6, @acc, &@prc).should == 6 + ScratchPad.recorded.should == [1, 2, [3, 4, 5], h, nil, @prc] end end - describe "rb_get_kwargs" do - it "extracts required arguments in the order requested" do - h = { :a => 7, :b => 5 } - @o.rb_get_kwargs(h, [:b, :a], 2, 0).should == [5, 7] - h.should == {} - end - - it "extracts required and optional arguments in the order requested" do - h = { :a => 7, :c => 12, :b => 5 } - @o.rb_get_kwargs(h, [:b, :a, :c], 2, 1).should == [5, 7, 12] - h.should == {} - end - - it "accepts nil instead of a hash when only optional arguments are requested" do - h = nil - @o.rb_get_kwargs(h, [:b, :a, :c], 0, 3).should == [] - h.should == nil - end - - it "raises an error if a required argument is not in the hash" do - h = { :a => 7, :c => 12, :b => 5 } - -> { @o.rb_get_kwargs(h, [:b, :d], 2, 0) }.should raise_error(ArgumentError, /missing keyword: :?d/) - h.should == {:a => 7, :c => 12} - end - - it "does not raise an error for an optional argument not in the hash" do - h = { :a => 7, :b => 5 } - @o.rb_get_kwargs(h, [:b, :a, :c], 2, 1).should == [5, 7] - h.should == {} - end - - it "raises an error if there are additional arguments and optional is positive" do - h = { :a => 7, :c => 12, :b => 5 } - -> { @o.rb_get_kwargs(h, [:b, :a], 2, 0) }.should raise_error(ArgumentError, /unknown keyword: :?c/) - h.should == {:c => 12} - end - - it "leaves additional arguments in the hash if optional is negative" do - h = { :a => 7, :c => 12, :b => 5 } - @o.rb_get_kwargs(h, [:b, :a], 2, -1).should == [5, 7] - h.should == {:c => 12} - end - end - - platform_is c_long_size: 64 do + platform_is wordsize: 64 do describe "rb_long2int" do it "raises a RangeError if the value is outside the range of a C int" do - -> { @o.rb_long2int(0xffff_ffff_ffff) }.should raise_error(RangeError) + lambda { @o.rb_long2int(0xffff_ffff_ffff) }.should raise_error(RangeError) end end @@ -258,37 +194,7 @@ describe "C-API Util function" do describe "rb_sourceline" do it "returns the current ruby file" do - @o.rb_sourceline.should be_kind_of(Integer) - end - end - - # ruby/util.h redefines strtod as a macro calling ruby_strtod - - describe "strtod" do - it "converts a string to a double and returns the remaining string" do - d, s = @o.strtod("14.25test") - d.should == 14.25 - s.should == "test" - end - - it "returns 0 and the full string if there's no numerical value" do - d, s = @o.strtod("test") - d.should == 0 - s.should == "test" - end - end - - describe "ruby_strtod" do - it "converts a string to a double and returns the remaining string" do - d, s = @o.ruby_strtod("14.25test") - d.should == 14.25 - s.should == "test" - end - - it "returns 0 and the full string if there's no numerical value" do - d, s = @o.ruby_strtod("test") - d.should == 0 - s.should == "test" + @o.rb_sourceline.should be_kind_of(Fixnum) end end |
