Skip to content

Commit e4f49f0

Browse files
committed
[GR-45621] Implement the Data class from Ruby 3.2 (#3341)
PullRequest: truffleruby/4081
2 parents 65e954b + 4bdec42 commit e4f49f0

File tree

14 files changed

+307
-44
lines changed

14 files changed

+307
-44
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Compatibility:
2727
* Display "unhandled exception" as the message for `RuntimeError` instances with an empty message (#3255, @nirvdrum).
2828
* Set `RbConfig::CONFIG['configure_args']` for openssl and libyaml (#3170, #3303, @eregon).
2929
* Support `Socket.sockaddr_in(port, Socket::INADDR_ANY)` (#3361, @mtortonesi).
30+
* Implement the `Data` class from Ruby 3.2 (#3039, @moste00, @eregon).
3031

3132
Performance:
3233

lib/mri/pp.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ def pretty_print_cycle(q) # :nodoc:
419419
class Data # :nodoc:
420420
def pretty_print(q) # :nodoc:
421421
q.group(1, sprintf("#<data %s", PP.mcall(self, Kernel, :class).name), '>') {
422-
q.seplist(PP.mcall(self, Data, :members), lambda { q.text "," }) {|member|
422+
q.seplist(PP.mcall(self, Kernel, :class).members, lambda { q.text "," }) {|member|
423423
q.breakable
424424
q.text member.to_s
425425
q.text '='

lib/truffle/socket/ifaddr.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2929

3030
class Socket < BasicSocket
31-
class Ifaddr < Data
31+
class Ifaddr
3232
attr_reader :addr, :broadaddr, :dstaddr, :flags, :ifindex, :name, :netmask
3333

3434
def initialize(addr: nil, broadaddr: nil, dstaddr: nil, flags: nil, ifindex: nil, name: nil, netmask: nil)

lib/truffle/truffle/cext.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ module Truffle::CExt
107107
RUBY_FIXNUM_MAX = (1 << 62) - 1
108108

109109
# This list of types is derived from MRI's error.c builtin_types array.
110+
# It has the same indices as the ruby_value_type enum.
110111
BUILTIN_TYPES = [
111112
'',
112113
'Object',
@@ -342,8 +343,6 @@ def rb_tr_find_type(value)
342343
T_FIXNUM
343344
when Time
344345
T_DATA
345-
when Data
346-
T_DATA
347346
when BasicObject
348347
# See #rb_tr_cached_type, the final type must be calculated for each object.
349348
T_NONE

spec/ruby/core/data/initialize_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@
3131
data.unit.should == "km"
3232
end
3333

34+
it "accepts String keyword arguments" do
35+
data = DataSpecs::Measure.new("amount" => 42, "unit" => "km")
36+
37+
data.amount.should == 42
38+
data.unit.should == "km"
39+
end
40+
3441
it "raises ArgumentError if no arguments are given" do
3542
-> {
3643
DataSpecs::Measure.new

spec/ruby/core/data/with_spec.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
require_relative '../../spec_helper'
2+
require_relative 'fixtures/classes'
3+
4+
ruby_version_is "3.2" do
5+
describe "Data#with" do
6+
it "returns self if given no arguments" do
7+
data = DataSpecs::Measure.new(amount: 42, unit: "km")
8+
data = data.with.should.equal?(data)
9+
end
10+
11+
it "accepts keyword arguments" do
12+
data = DataSpecs::Measure.new(amount: 42, unit: "km")
13+
data = data.with(amount: 4, unit: "m")
14+
15+
data.amount.should == 4
16+
data.unit.should == "m"
17+
end
18+
19+
it "accepts String keyword arguments" do
20+
data = DataSpecs::Measure.new(amount: 42, unit: "km")
21+
data = data.with("amount" => 4, "unit" => "m")
22+
23+
data.amount.should == 4
24+
data.unit.should == "m"
25+
end
26+
27+
it "raises ArgumentError if no keyword arguments are given" do
28+
data = DataSpecs::Measure.new(amount: 42, unit: "km")
29+
30+
-> {
31+
data.with(4, "m")
32+
}.should raise_error(ArgumentError, "wrong number of arguments (given 2, expected 0)")
33+
end
34+
end
35+
end

spec/tags/core/data/define_tags.txt

Lines changed: 0 additions & 5 deletions
This file was deleted.

spec/tags/core/data/initialize_tags.txt

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/main/java/org/truffleruby/cext/CExtNodes.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,6 +1188,16 @@ int sourceLine() {
11881188

11891189
}
11901190

1191+
@CoreMethod(names = "rb_is_local_id", onSingleton = true, required = 1)
1192+
public abstract static class IsLocalIdNode extends CoreMethodArrayArgumentsNode {
1193+
1194+
@Specialization
1195+
boolean isLocalId(RubySymbol symbol) {
1196+
return symbol.getType() == IdentifierType.LOCAL;
1197+
}
1198+
1199+
}
1200+
11911201
@CoreMethod(names = "rb_is_instance_id", onSingleton = true, required = 1)
11921202
public abstract static class IsInstanceIdNode extends CoreMethodArrayArgumentsNode {
11931203

src/main/java/org/truffleruby/core/CoreLibrary.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,6 @@ public CoreLibrary(RubyContext context, RubyLanguage language) {
389389
arrayClass = defineClass("Array");
390390
bindingClass = defineClass("Binding");
391391
defineClass("ConditionVariable");
392-
defineClass("Data"); // Needed by Socket::Ifaddr and defined in core MRI
393392
dirClass = defineClass("Dir");
394393
encodingClass = defineClass("Encoding");
395394
enumeratorClass = defineClass("Enumerator");
@@ -1048,9 +1047,10 @@ public boolean isTruffleBootMainMethod(SharedMethodInfo info) {
10481047
"/core/truffle/polyglot.rb",
10491048
"/core/truffle/polyglot_methods.rb",
10501049
"/core/posix.rb",
1050+
"/core/data.rb",
1051+
"/core/truffle/queue_operations.rb",
10511052
"/core/main.rb",
10521053
"/core/post.rb",
1053-
"/core/truffle/queue_operations.rb",
10541054
POST_BOOT_FILE
10551055
};
10561056

0 commit comments

Comments
 (0)