Skip to content

Commit 6872c72

Browse files
authored
Merge pull request #309 from ruby-rice/dev
Dev
2 parents b7da1c7 + 10ec140 commit 6872c72

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+1381
-778
lines changed

CHANGELOG.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
## 4.6.2 (2025-07-29)
1+
## 4.6.2 (2025-07-31)
22
Updates:
33
* Refactor Native wrappers - functions, methods, attributes and procs - to enable introspection API
4-
* Add a new Ruby Introspection API that exposes Rice internals to Ruby
5-
* Using the new API, add support for generating RBS files
4+
* Add a new Introspection API that exposes Rice internals to Ruby
5+
* Using the new API, add support for generating RBS files from extensions
66
* Don't create attribute writers for const attributes
77
* Support attribute setters for Enums
88
* Support wrapping std::vector<std::unique_ptr<T>>
9-
* Update Array#push to not always copy C++ values. This lays the foundation for extracting references and pointers and from tuples, variants and optionals.
10-
* Add most basic support for creating std::filesystem::path instances
9+
* Update Array#push to not always copy C++ instances. This lays the foundation for extracting references and pointers and from tuples, variants and optionals.
10+
* Add very basic support for creating std::filesystem::path instances
1111
* Remove toy samples and test libraries. These are replaced by a new gem that wraps the BitMapPlusPlus library (https://github.com/baderouaich/BitmapPlusPlus)
1212
* Add support for std::runtime_error since some libraries use that as a base exception class (thus when Rice wraps custom exceptions it also needs to wrap the base class)
1313

Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ FileUtils.mkdir_p(include_dir) #unless File.exists?('include')
8484
desc "Update rice header files"
8585
task :headers do
8686
FileUtils.rm_rf(File.join(include_dir, "rice", "*"))
87-
path = File.join(__dir__, 'lib', 'make_rice_headers.rb')
87+
path = File.join(__dir__, 'lib', 'rice', 'make_rice_headers.rb')
8888
# Execute make_rice_headers.rb
8989
run_command(Gem.ruby, path)
9090
end
File renamed without changes.

doc/bindings/type_conversions.rst

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,22 @@ Next, we need to write C++ code that converts the ``std::deque<int>`` to a Ruby
6969
class To_Ruby<std::deque<int>>
7070
{
7171
public:
72+
explicit To_Ruby(Arg* arg) : arg_(arg)
73+
{
74+
}
75+
7276
VALUE convert(const std::deque<int>& deque)
7377
{
74-
// Notice we wrap Ruby API calls with protect in case Ruby throws an exception.
75-
// If you do not use protect and Ruby throws an exception then your program
76-
// *will* crash.
77-
VALUE result = protect(rb_ary_new2, deque.size());
78+
Array result;
7879
7980
for (int element : deque)
8081
{
81-
// Convert the C++ int to a Ruby integer
82-
VALUE value = To_Ruby<int>::convert(element, takeOwnership);
83-
// Now add it to the Ruby array
84-
detail::protect(rb_ary_push, result, value));
82+
result.push(element, true);
8583
}
8684
return result;
8785
}
86+
private:
87+
Arg* arg_ = nullptr;
8888
};
8989
}
9090
@@ -106,6 +106,10 @@ Last, if we want to convert a Ruby array to a ``std::deque<int>``, then we need
106106
class From_Ruby<std::deque<int>>
107107
{
108108
public:
109+
explicit To_Ruby(Arg* arg) : arg_(arg)
110+
{
111+
}
112+
109113
Convertible is_convertible(VALUE value)
110114
{
111115
switch (rb_type(value))
@@ -120,27 +124,22 @@ Last, if we want to convert a Ruby array to a ``std::deque<int>``, then we need
120124
121125
std::deque<int> convert(VALUE value)
122126
{
123-
// Make sure array is really an array - if not this call will
124-
// throw a Ruby exception so we need to protect it
125-
detail::protect(rb_check_type, array, (int)T_ARRAY);
126-
127-
long size = protect(rb_array_len, value);
128-
std::deque<int> result(size);
127+
Array array(value);
128+
std::deque<int> result(array.size());
129129
130130
for (long i=0; i<size; i++)
131131
{
132-
// Get the array element
133-
VALUE value = protect(rb_ary_entry, value, i);
134-
135132
// Convert the Ruby int to a C++ int
136-
int element = From_Ruby<int>::convert(value);
133+
int element = From_Ruby<int>::convert(array[i]);
137134
138135
// Add it to our deque
139136
result[i] = element;
140137
}
141138
142139
return result;
143140
}
141+
private:
142+
Arg* arg_ = nullptr;
144143
};
145144
}
146145
@@ -164,8 +163,6 @@ Expanding on our example above:
164163
class From_Ruby<std::deque<int>>
165164
{
166165
public:
167-
From_Ruby() = default;
168-
169166
explicit From_Ruby(Arg* arg) : arg_(arg)
170167
{
171168
}

doc/introspection_api/overview.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ Rice exposes its internals to Ruby via an introspection API. This API is used by
88

99
Enabling
1010
--------
11-
The introspection API is an opt-in feature and therefore must be enabled. To do this requires adding the ``<rice/ruby_api.hpp>`` header file to your extension and then calling ``Init_Ruby_Api``:
11+
The introspection API is an opt-in feature and therefore must be enabled. To do this requires adding the ``<rice/api.hpp>`` header file to your extension and then calling ``Init_Rice_Api``:
1212

1313
.. code-block:: cpp
1414
15-
#include <rice/ruby_api.hpp>
15+
#include <rice/api.hpp>
1616
1717
extern "C"
1818
void Init_My_Exgtension()
1919
{
2020
// Enable Introspection API
21-
Init_Ruby_Api();
21+
Init_Rice_Api();
2222
2323
<Add Rice Binding Code>
2424
}

doc/packaging/rbs.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ Rice of course knows a lot about your extension's types! Starting with Rice 4.7,
88

99
Generating RBS Files
1010
--------------------
11-
Use the ``rice_rbs`` script in the bin folder to generate RBS files for your extension.
11+
Use the ``rice-rbs`` script in the bin folder to generate RBS files for your extension.
1212

1313
To run it:
1414

1515
.. code-block:: bash
1616
17-
rice_rbs [options] <rice_extension_library>
17+
rice-rbs [options] <rice_extension_library>
1818
1919
Options:
2020
-o, --output=path Output directory

include/rice/ruby_api.hpp renamed to include/rice/api.hpp

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
#ifndef Rice__Ruby_Api_hpp_
2-
#define Rice__Ruby_Api_hpp_
1+
#ifndef Rice__Api_hpp_
2+
#define Rice__Api_hpp_
33

44

55
// ========= NativeRegistry.hpp =========
6-
#ifndef Rice_Ruby_Api_Native_Registry_hpp
7-
#define Rice_Ruby_Api_Native_Registry_hpp
6+
#ifndef Rice_Api_Native_Registry_hpp
7+
#define Rice_Api_Native_Registry_hpp
88

99
extern "C"
1010
void Init_Native_Registry();
@@ -35,8 +35,8 @@ inline void Init_Native_Registry()
3535
}
3636

3737
// ========= TypeRegistry.hpp =========
38-
#ifndef Rice_Ruby_Api_Type_Registry_hpp
39-
#define Rice_Ruby_Api_Type_Registry_hpp
38+
#ifndef Rice_Api_Type_Registry_hpp
39+
#define Rice_Api_Type_Registry_hpp
4040

4141
extern "C"
4242
void Init_Type_Registry();
@@ -56,8 +56,8 @@ void Init_Type_Registry()
5656
}
5757

5858
// ========= Registries.hpp =========
59-
#ifndef Rice_Ruby_Api_Registries_hpp
60-
#define Rice_Ruby_Api_Registries_hpp
59+
#ifndef Rice_Api_Registries_hpp
60+
#define Rice_Api_Registries_hpp
6161

6262
void Init_Registries();
6363

@@ -72,14 +72,13 @@ inline void Init_Registries()
7272

7373
define_class_under<detail::Registries>(rb_mRice, "Registries").
7474
define_singleton_attr("instance", &detail::Registries::instance, AttrAccess::Read).
75-
define_attr("instances", &detail::Registries::instances, AttrAccess::Read).
7675
define_attr("natives", &detail::Registries::natives, AttrAccess::Read).
7776
define_attr("types", &detail::Registries::types, AttrAccess::Read);
7877
}
7978

8079
// ========= Arg.hpp =========
81-
#ifndef Rice_Ruby_Api_Arg_hpp
82-
#define Rice_Ruby_Api_Arg_hpp
80+
#ifndef Rice_Api_Arg_hpp
81+
#define Rice_Api_Arg_hpp
8382

8483
extern "C"
8584
void Init_Arg();
@@ -98,8 +97,8 @@ inline void Init_Arg()
9897
}
9998

10099
// ========= Parameter.hpp =========
101-
#ifndef Rice_Ruby_Api_Parameter_hpp
102-
#define Rice_Ruby_Api_Parameter_hpp
100+
#ifndef Rice_Api_Parameter_hpp
101+
#define Rice_Api_Parameter_hpp
103102

104103
extern "C"
105104
void Init_Parameter();
@@ -115,12 +114,13 @@ inline void Init_Parameter()
115114

116115
define_class_under<detail::ParameterAbstract>(rb_mRice, "Parameter").
117116
define_attr("arg", &detail::ParameterAbstract::arg).
118-
define_method("klass", &detail::ParameterAbstract::rubyTypeName);
117+
define_method("klass", &detail::ParameterAbstract::rubyKlass, Return().setValue()).
118+
define_method("cpp_klass", &detail::ParameterAbstract::cppTypeName);
119119
}
120120

121121
// ========= Native.hpp =========
122-
#ifndef Rice_Ruby_Api_Native_Function_hpp
123-
#define Rice_Ruby_Api_Native_Function_hpp
122+
#ifndef Rice_Api_Native_Function_hpp
123+
#define Rice_Api_Native_Function_hpp
124124

125125
extern "C"
126126
void Init_Native();
@@ -147,7 +147,7 @@ inline void Init_Native()
147147
define_class_under<detail::Native>(rb_mRice, "Native").
148148
define_method("name", &detail::Native::name).
149149
define_method("kind", &detail::Native::kind).
150-
define_method("return_type", &detail::Native::rubyReturnType).
150+
define_method("return_klass", &detail::Native::returnKlass).
151151
define_method("parameters", &detail::Native::parameters).
152152
define_method("to_s", [](detail::Native& self) -> std::string
153153
{
@@ -182,7 +182,7 @@ inline void Init_Native()
182182
}
183183

184184
extern "C"
185-
inline void Init_Ruby_Api()
185+
inline void Init_Rice_Api()
186186
{
187187
Init_Registries();
188188
Init_Native_Registry();
@@ -191,5 +191,4 @@ inline void Init_Ruby_Api()
191191
Init_Parameter();
192192
Init_Native();
193193
}
194-
195194
#endif

0 commit comments

Comments
 (0)