Skip to content

Commit 1a96f98

Browse files
eregonfniephaus
authored andcommitted
[GR-58266] Prepare strscan.rb to be upstreamed
PullRequest: truffleruby/4485
2 parents e88b353 + 61b4cf7 commit 1a96f98

Some content is hidden

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

53 files changed

+556
-381
lines changed

lib/mri/monitor.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ def mon_owned?
214214
def mon_synchronize(&block)
215215
Primitive.monitor_synchronize(@mon_mutex, block)
216216
end
217-
Truffle::Graal.always_split instance_method(:mon_synchronize)
217+
Primitive.always_split self, :mon_synchronize
218218
alias synchronize mon_synchronize
219219

220220
#

lib/truffle/strscan.rb

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,12 @@ def charpos
9999
def check(pattern)
100100
scan_internal pattern, false, true, true
101101
end
102+
Primitive.always_split self, :check
102103

103104
def check_until(pattern)
104105
scan_internal pattern, false, true, false
105106
end
107+
Primitive.always_split self, :check_until
106108

107109
def clear
108110
warn 'StringScanner#clear is obsolete; use #terminate instead' if $VERBOSE
@@ -128,6 +130,7 @@ def eos?
128130
def exist?(pattern)
129131
scan_internal pattern, false, false, false
130132
end
133+
Primitive.always_split self, :exist?
131134

132135
def fixed_anchor?
133136
@fixed_anchor
@@ -192,6 +195,7 @@ def inspect
192195
def match?(pattern)
193196
scan_internal pattern, false, false, true
194197
end
198+
Primitive.always_split self, :match?
195199

196200
def matched
197201
@match&.to_s
@@ -247,6 +251,7 @@ def restsize
247251
def scan(pattern)
248252
scan_internal pattern, true, true, true
249253
end
254+
Primitive.always_split self, :scan
250255

251256
def scan_byte
252257
if eos?
@@ -277,21 +282,24 @@ def scan_integer(base: 10)
277282
end
278283

279284
if substr
280-
Primitive.string_to_inum(substr, base, true, true)
285+
substr.to_i(base)
281286
end
282287
end
283288

284289
def scan_until(pattern)
285290
scan_internal pattern, true, true, false
286291
end
292+
Primitive.always_split self, :scan_until
287293

288294
def scan_full(pattern, advance_pos, getstr)
289295
scan_internal pattern, advance_pos, getstr, true
290296
end
297+
Primitive.always_split self, :scan_full
291298

292299
def search_full(pattern, advance_pos, getstr)
293300
scan_internal pattern, advance_pos, getstr, false
294301
end
302+
Primitive.always_split self, :search_full
295303

296304
def self.must_C_version
297305
self
@@ -304,10 +312,12 @@ def size
304312
def skip(pattern)
305313
scan_internal pattern, true, false, true
306314
end
315+
Primitive.always_split self, :skip
307316

308317
def skip_until(pattern)
309318
scan_internal pattern, true, false, false
310319
end
320+
Primitive.always_split self, :skip_until
311321

312322
def string
313323
@original
@@ -332,7 +342,7 @@ def terminate
332342
end
333343

334344
def unscan
335-
raise ScanError if Primitive.nil?(@match)
345+
raise ScanError unless @match
336346
@pos = @prev_pos
337347
@prev_pos = nil
338348
@match = nil
@@ -358,7 +368,7 @@ def peep(len)
358368
peek len
359369
end
360370

361-
private def scan_check_args(pattern, headonly)
371+
private def scan_check_args(pattern)
362372
unless Primitive.is_a?(pattern, Regexp) || Primitive.is_a?(pattern, String)
363373
raise TypeError, "bad pattern argument: #{pattern.inspect}"
364374
end
@@ -369,15 +379,18 @@ def peep(len)
369379
# This method is kept very small so that it should fit within 100
370380
# AST nodes and can be split. This is done to avoid indirect calls
371381
# to TRegex.
372-
private def scan_internal(pattern, advance_pos, getstr, headonly)
373-
scan_check_args(pattern, headonly)
382+
private def scan_internal(pattern, advance_pos, getstr, only_match_at_start)
383+
scan_check_args(pattern)
374384

375385
if Primitive.is_a?(pattern, String)
376-
md = scan_internal_string_pattern(pattern, headonly)
386+
md = scan_internal_string_pattern(pattern, only_match_at_start)
377387
else
378388
start = @fixed_anchor ? 0 : @pos
379-
md = Truffle::RegexpOperations.match_in_region pattern, @string, @pos, @string.bytesize, headonly, start
380-
Primitive.matchdata_fixup_positions(md, start) if md
389+
if only_match_at_start
390+
md = Primitive.regexp_match_at_start(pattern, @string, @pos, start)
391+
else
392+
md = Primitive.regexp_search_with_start(pattern, @string, @pos, start)
393+
end
381394
end
382395

383396
if md
@@ -387,11 +400,12 @@ def peep(len)
387400
@match = nil
388401
end
389402
end
403+
Primitive.always_split self, :scan_internal
390404

391-
private def scan_internal_string_pattern(pattern, headonly)
405+
private def scan_internal_string_pattern(pattern, only_match_at_start)
392406
pos = @pos
393407

394-
if headonly
408+
if only_match_at_start
395409
if @string.byteslice(pos..).start_with?(pattern)
396410
Primitive.matchdata_create_single_group(pattern, @string.dup, pos, pos + pattern.bytesize)
397411
else

lib/truffle/truffle/cext.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,7 +1193,7 @@ def rb_hash_foreach(hash, func, farg)
11931193
end
11941194
end
11951195
end
1196-
Truffle::Graal.always_split instance_method(:rb_hash_foreach)
1196+
Primitive.always_split self, :rb_hash_foreach
11971197

11981198
def rb_path_to_class(path)
11991199
begin
@@ -1264,7 +1264,7 @@ def rb_protect(function, arg, write_status, status)
12641264
Truffle::Interop.execute_without_conversion(write_status, status, pos)
12651265
res
12661266
end
1267-
Truffle::Graal.always_split instance_method(:rb_protect)
1267+
Primitive.always_split self, :rb_protect
12681268

12691269
def rb_jump_tag(pos)
12701270
if pos > 0
@@ -1279,12 +1279,12 @@ def rb_jump_tag(pos)
12791279
def rb_yield(value)
12801280
rb_block_proc.call(value)
12811281
end
1282-
Truffle::Graal.always_split instance_method(:rb_yield)
1282+
Primitive.always_split self, :rb_yield
12831283

12841284
def rb_yield_splat(values)
12851285
rb_block_proc.call(*values)
12861286
end
1287-
Truffle::Graal.always_split instance_method(:rb_yield_splat)
1287+
Primitive.always_split self, :rb_yield_splat
12881288

12891289
def rb_ivar_lookup(object, name, default_value)
12901290
# TODO CS 24-Jul-16 races - needs a new primitive or be defined in Java?
@@ -1649,7 +1649,7 @@ def rb_mutex_synchronize(mutex, func, arg)
16491649
Primitive.interop_execute(POINTER_TO_POINTER_WRAPPER, [func, arg])
16501650
end
16511651
end
1652-
Truffle::Graal.always_split instance_method(:rb_mutex_synchronize)
1652+
Primitive.always_split self, :rb_mutex_synchronize
16531653

16541654
def rb_gc_enable
16551655
GC.enable
@@ -1879,7 +1879,7 @@ def rb_ensure(b_proc, data1, e_proc, data2)
18791879
end
18801880
end
18811881
end
1882-
Truffle::Graal.always_split instance_method(:rb_ensure)
1882+
Primitive.always_split self, :rb_ensure
18831883

18841884
def rb_rescue(b_proc, data1, r_proc, data2)
18851885
begin
@@ -1898,7 +1898,7 @@ def rb_rescue(b_proc, data1, r_proc, data2)
18981898
end
18991899
end
19001900
end
1901-
Truffle::Graal.always_split instance_method(:rb_rescue)
1901+
Primitive.always_split self, :rb_rescue
19021902

19031903
def rb_rescue2(b_proc, data1, r_proc, data2, rescued)
19041904
begin
@@ -1913,7 +1913,7 @@ def rb_rescue2(b_proc, data1, r_proc, data2, rescued)
19131913
end
19141914
end
19151915
end
1916-
Truffle::Graal.always_split instance_method(:rb_rescue2)
1916+
Primitive.always_split self, :rb_rescue2
19171917

19181918
def rb_exec_recursive(func, obj, arg)
19191919
result = nil
@@ -1932,7 +1932,7 @@ def rb_exec_recursive(func, obj, arg)
19321932
result
19331933
end
19341934
end
1935-
Truffle::Graal.always_split instance_method(:rb_exec_recursive)
1935+
Primitive.always_split self, :rb_exec_recursive
19361936

19371937
def rb_catch_obj(tag, func, data)
19381938
use_cext_lock = Primitive.use_cext_lock?

spec/truffle/array/steal_array_storage.rb renamed to spec/truffle/array/steal_array_storage_spec.rb

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,25 @@ def storage(ary)
1616
Truffle::Debug.array_storage(ary)
1717
end
1818

19-
before :each do
20-
@array = %i[first second third]
21-
end
22-
2319
guard -> { !Truffle::Boot.get_option('chaos-data') } do
2420
it "should no-op when called on itself" do
25-
copy = @array.dup
21+
array = %i[first second third]
22+
Primitive.steal_array_storage(array, array)
2623

27-
Primitive.steal_array_storage(@array, @array)
28-
29-
storage(@array).should == "Object[]"
30-
@array.should == copy
24+
storage(array).should == "Object[]"
25+
array.should == %i[first second third]
3126
end
3227

3328
it "should take ownership of the store" do
29+
array = %i[first second third]
3430
other = [1, 2, 3, 4, 5]
35-
other_copy = other.dup
36-
37-
Primitive.steal_array_storage(@array, other)
31+
Primitive.steal_array_storage(array, other)
3832

39-
storage(@array).should == "int[]"
40-
@array.should == other_copy
33+
storage(array).should == "int[]"
34+
array.should == [1, 2, 3, 4, 5]
4135

42-
storage(other).should == "null"
43-
other.empty?.should == true
36+
storage(other).should == "empty"
37+
other.should.empty?
4438
end
4539
end
4640
end

spec/truffle/splitting_spec.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@
3535
Regexp.instance_method(:match?),
3636
Truffle::RegexpOperations.method(:match),
3737
Truffle::RegexpOperations.method(:match?),
38-
Truffle::RegexpOperations.method(:search_region),
39-
Truffle::RegexpOperations.method(:match_in_region),
4038
4139
String.instance_method(:[]),
4240
Truffle::StringOperations.method(:subpattern),

spec/truffleruby.mspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ load "#{__dir__}/ruby/default.mspec"
88
# Don't run ruby/spec as root on TruffleRuby
99
raise 'ruby/spec is not designed to be run as root on TruffleRuby' if Process.uid == 0
1010

11+
ENV['TRUFFLERUBY_ALLOW_PRIVATE_PRIMITIVES_IN'] = "#{__dir__}/truffle/"
12+
1113
class MSpecScript
1214
def self.windows?
1315
ENV.key?('WINDIR') || ENV.key?('windir')

src/annotations/java/org/truffleruby/annotations/Primitive.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,7 @@
3333

3434
/** Use these names in Ruby core methods stubs, ignore argument names in Java specializations. */
3535
String[] argumentNames() default {};
36+
37+
/** Whether this Primitive can be used outside the TruffleRuby repository. */
38+
boolean isPublic() default false;
3639
}

src/main/java/org/truffleruby/RubyLanguage.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ public abstract static class RubySourceOptions {
208208
* {@link TranslatorEnvironment#newFrameDescriptorBuilderForBlock(BlockDescriptorInfo)}. */
209209
public static final FrameDescriptor EMPTY_FRAME_DESCRIPTOR = new FrameDescriptor(nil);
210210

211+
public static final String INTERNAL_CORE_PREFIX = "<internal:core> ";
212+
211213
/** Global cache of call targets that {@code RBSprintfCompiler.compile} returns */
212214
public static final Map<TStringWithEncoding, RootCallTarget> sprintfCompilerCallTargets = new ConcurrentHashMap<>();
213215

@@ -284,6 +286,7 @@ private RubyThread getOrCreateForeignThread(RubyContext context, Thread thread)
284286
@CompilationFinal public LanguageOptions options;
285287
@CompilationFinal private String rubyHome;
286288
@CompilationFinal public String cextPath;
289+
public String[] allowPrivatePrimitivesPrefixes;
287290

288291
private TruffleFile rubyHomeTruffleFile;
289292

@@ -777,13 +780,36 @@ private void setRubyHome(TruffleFile home) {
777780
rubyHomeTruffleFile = home;
778781
rubyHome = home.getPath();
779782
cextPath = rubyHome + "/lib/truffle/truffle/cext_ruby.rb";
783+
784+
String allowIn = System.getenv("TRUFFLERUBY_ALLOW_PRIVATE_PRIMITIVES_IN");
785+
if (allowIn != null) {
786+
if (!allowIn.endsWith("/spec/truffle/") && !allowIn.endsWith("/test/truffle/compiler/") &&
787+
!allowIn.endsWith("/bench/metrics/")) {
788+
throw CompilerDirectives
789+
.shouldNotReachHere("Invalid value for TRUFFLERUBY_ALLOW_PRIMITIVES_IN: " + allowIn);
790+
}
791+
792+
allowPrivatePrimitivesPrefixes = new String[]{
793+
rubyHome + "/lib/truffle/",
794+
rubyHome + "/lib/patches/",
795+
rubyHome + "/lib/mri/",
796+
allowIn,
797+
};
798+
} else {
799+
allowPrivatePrimitivesPrefixes = new String[]{
800+
rubyHome + "/lib/truffle/",
801+
rubyHome + "/lib/patches/",
802+
rubyHome + "/lib/mri/",
803+
};
804+
}
780805
}
781806

782807
private void resetRubyHome() {
783808
assert Thread.holdsLock(this);
784809
rubyHomeTruffleFile = null;
785810
rubyHome = null;
786811
cextPath = null;
812+
allowPrivatePrimitivesPrefixes = null;
787813
}
788814

789815
private TruffleFile findRubyHome(Env env) {
@@ -954,7 +980,7 @@ public static String getPath(Source source) {
954980
public String getSourcePath(Source source) {
955981
final String path = getPath(source);
956982
if (path.startsWith(coreLoadPath)) {
957-
return "<internal:core> " + path.substring(coreLoadPath.length() + 1);
983+
return INTERNAL_CORE_PREFIX + path.substring(coreLoadPath.length() + 1);
958984
} else {
959985
return path;
960986
}

src/main/java/org/truffleruby/builtins/PrimitiveManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public PrimitiveNodeConstructor getPrimitive(String name) {
4343
}
4444
}
4545

46-
throw new Error("Primitive :" + name + " not found");
46+
throw new Error("Primitive." + name + " not found");
4747
}
4848

4949
public void addLazyPrimitive(String primitive, String nodeFactoryClass) {

src/main/java/org/truffleruby/builtins/PrimitiveNodeConstructor.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public PrimitiveNodeConstructor(Primitive annotation, NodeFactory<? extends Ruby
2828
this.factory = factory;
2929
}
3030

31+
public boolean isPublic() {
32+
return annotation.isPublic();
33+
}
34+
3135
public int getPrimitiveArity() {
3236
return factory.getExecutionSignature().size();
3337
}

0 commit comments

Comments
 (0)