Skip to content

Commit c57ca79

Browse files
committed
[GR-40333] Fiber.current and Fiber#transfer are available without require 'fiber'
PullRequest: truffleruby/3667
2 parents ec715aa + 1f82025 commit c57ca79

File tree

9 files changed

+29
-32
lines changed

9 files changed

+29
-32
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ Compatibility:
8080
* Implement `UnboundMethod#original_name` (@paracycle, @nirvdrum).
8181
* Implement `Thread#native_thread_id` method (#2733, @horakivo).
8282
* Modify `Struct#{inspect,to_s}` to match MRI when the struct is nested inside of an anonymous class or module (@st0012, @nirvdrum).
83+
* `Fiber.current` and `Fiber#transfer` are available without `require 'fiber'` like in CRuby 3.1 (#2733, @eregon).
8384

8485
Performance:
8586

lib/truffle/fiber.rb

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1 @@
1-
# truffleruby_primitives: true
2-
3-
# Copyright (c) 2021, 2023 Oracle and/or its affiliates. All rights reserved. This
4-
# code is released under a tri EPL/GPL/LGPL license. You can use it,
5-
# redistribute it and/or modify it under the terms of the:
6-
#
7-
# Eclipse Public License version 2.0, or
8-
# GNU General Public License version 2, or
9-
# GNU Lesser General Public License version 2.1.
10-
11-
class Fiber
12-
def self.current
13-
Primitive.fiber_current
14-
end
15-
end
1+
# Provided by default

spec/ruby/core/kernel/require_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@
1616
Kernel.should have_private_instance_method(:require)
1717
end
1818

19+
provided = %w[complex enumerator rational thread ruby2_keywords]
20+
ruby_version_is "3.1" do
21+
provided << "fiber"
22+
end
23+
24+
it "#{provided.join(', ')} are already required" do
25+
out = ruby_exe("puts $LOADED_FEATURES", options: '--disable-gems --disable-did-you-mean')
26+
features = out.lines.map { |line| File.basename(line.chomp, '.*') }
27+
features -= %w[encdb transdb] # Ignore CRuby internals
28+
features.sort.should == provided.sort
29+
30+
code = provided.map { |f| "puts require #{f.inspect}\n" }.join
31+
required = ruby_exe(code, options: '--disable-gems')
32+
required.should == "false\n" * provided.size
33+
end
34+
1935
it_behaves_like :kernel_require_basic, :require, CodeLoadingSpecs::Method.new
2036
it_behaves_like :kernel_require, :require, CodeLoadingSpecs::Method.new
2137
end

spec/ruby/core/kernel/shared/require.rb

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -546,20 +546,6 @@
546546
ScratchPad.recorded.should == []
547547
end
548548

549-
provided = %w[complex enumerator rational thread]
550-
provided << 'ruby2_keywords'
551-
552-
it "#{provided.join(', ')} are already required" do
553-
features = ruby_exe("puts $LOADED_FEATURES", options: '--disable-gems')
554-
provided.each { |feature|
555-
features.should =~ /\b#{feature}\.(rb|so|jar)$/
556-
}
557-
558-
code = provided.map { |f| "puts require #{f.inspect}\n" }.join
559-
required = ruby_exe(code, options: '--disable-gems')
560-
required.should == "false\n" * provided.size
561-
end
562-
563549
it "unicode_normalize is part of core and not $LOADED_FEATURES" do
564550
features = ruby_exe("puts $LOADED_FEATURES", options: '--disable-gems')
565551
features.lines.each { |feature|

spec/ruby/library/fiber/current_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
require 'fiber'
44

55
describe "Fiber.current" do
6+
ruby_version_is "3.1" do
7+
it "is available without an extra require" do
8+
ruby_exe("print Fiber.current.class", options: '--disable-gems --disable-did-you-mean').should == "Fiber"
9+
end
10+
end
11+
612
it "returns the root Fiber when called outside of a Fiber" do
713
root = Fiber.current
814
root.should be_an_instance_of(Fiber)

spec/tags/core/kernel/require_tags.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ fails:Kernel#require (non-extensioned path) loads a .rb extensioned file when a
66
fails:Kernel#require (non-extensioned path) does not load a feature twice when $LOAD_PATH has been modified
77
slow:Kernel#require ($LOADED_FEATURES) complex, enumerator, rational, thread, ruby2_keywords are already required
88
slow:Kernel.require ($LOADED_FEATURES) complex, enumerator, rational, thread, ruby2_keywords are already required
9+
slow:Kernel#require complex, enumerator, rational, thread, ruby2_keywords, fiber are already required
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
slow:Fiber.current is available without an extra require

src/main/java/org/truffleruby/core/fiber/FiberNodes.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import org.truffleruby.annotations.Primitive;
1717
import org.truffleruby.builtins.CoreMethodArrayArgumentsNode;
1818
import org.truffleruby.builtins.PrimitiveArrayArgumentsNode;
19-
import org.truffleruby.builtins.PrimitiveNode;
2019
import org.truffleruby.core.array.RubyArray;
2120
import org.truffleruby.core.cast.SingleValueCastNode;
2221
import org.truffleruby.core.cast.SingleValueCastNodeGen;
@@ -321,8 +320,8 @@ protected boolean alive(RubyFiber fiber) {
321320

322321
}
323322

324-
@Primitive(name = "fiber_current")
325-
public abstract static class CurrentNode extends PrimitiveNode {
323+
@CoreMethod(names = "current", onSingleton = true)
324+
public abstract static class CurrentNode extends CoreMethodArrayArgumentsNode {
326325

327326
@Specialization
328327
protected RubyFiber current() {

src/main/ruby/truffleruby/post-boot/post-boot.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
begin
1515
require 'enumerator'
1616
require 'thread'
17+
require 'fiber'
1718
require 'rational'
1819
require 'complex'
1920
rescue LoadError => e

0 commit comments

Comments
 (0)