Skip to content

Commit 68f5b26

Browse files
committed
[GR-17457] Rename object primitives
PullRequest: truffleruby/3778
2 parents 8e58131 + c09463d commit 68f5b26

Some content is hidden

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

81 files changed

+638
-640
lines changed

doc/contributor/primitives.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Primitives
2+
3+
## Naming
4+
5+
Primitives are generally named `#{module or class to which it belongs}_#{name of the operation}` such as `string_start_with?`.
6+
7+
The `object_` prefix should only be used for instance variables-related operations, like `object_ivars`.
8+
9+
For primitives which are not specific to a module or class, use no prefix, such as `Primitive.is_a?`/`Primitive.equal?`.
10+
11+
For primitives used in many places it is nice to have a shorter name.
12+
OTOH for primitives used in a single place, e.g., to implement part of the logic in Ruby, then a longer name is fine.

lib/mri/monitor.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ def initialize(...)
241241
# Initializes the MonitorMixin after being included in a class or when an
242242
# object has been extended with the MonitorMixin
243243
def mon_initialize
244-
if defined?(@mon_mutex) && Primitive.object_equal(@mon_mutex_owner_object, self)
244+
if defined?(@mon_mutex) && Primitive.equal?(@mon_mutex_owner_object, self)
245245
raise ThreadError, 'already initialized'
246246
end
247247
@mon_mutex = Thread::Mutex.new

lib/truffle/digest.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def size
138138
alias_method :length, :size
139139

140140
def ==(other)
141-
if Primitive.object_kind_of?(other, Digest::Instance)
141+
if Primitive.is_a?(other, Digest::Instance)
142142
self_str = self.digest
143143
other_str = other.digest
144144
else

lib/truffle/io/wait.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ def wait_priority(timeout = nil)
5252
def wait(*args)
5353
ensure_open
5454

55-
if args.size != 2 || Primitive.object_kind_of?(args[0], Symbol) || Primitive.object_kind_of?(args[1], Symbol)
55+
if args.size != 2 || Primitive.is_a?(args[0], Symbol) || Primitive.is_a?(args[1], Symbol)
5656
# Slow/messy path:
5757
timeout = :undef
5858
events = 0
5959
args.each do |arg|
60-
if Primitive.object_kind_of?(arg, Symbol)
60+
if Primitive.is_a?(arg, Symbol)
6161
events |= case arg
6262
when :r, :read, :readable then IO::READABLE
6363
when :w, :write, :writable then IO::WRITABLE

lib/truffle/objspace.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def count_nodes(nodes = {})
5656

5757
def count_objects_size(hash = {})
5858
ObjectSpace.each_object do |obj|
59-
class_name = Primitive.object_class(obj).name
59+
class_name = Primitive.class(obj).name
6060
if class_name
6161
class_name_sym = class_name.to_sym
6262
hash[class_name_sym] = hash.fetch(class_name_sym, 0) + ObjectSpace.memsize_of(obj)
@@ -69,7 +69,7 @@ def count_objects_size(hash = {})
6969

7070
def count_tdata_objects(hash = {})
7171
ObjectSpace.each_object do |object|
72-
klass = Primitive.object_class(object)
72+
klass = Primitive.class(object)
7373
hash[klass] ||= 0
7474
hash[klass] += 1
7575
end
@@ -78,11 +78,11 @@ def count_tdata_objects(hash = {})
7878

7979
# Helper method for ObjectSpace.dump
8080
def _dump(object, output)
81-
if Primitive.object_kind_of?(output, String)
81+
if Primitive.is_a?(output, String)
8282
require 'json'
8383
json = {
8484
address: '0x' + object.object_id.to_s(16),
85-
class: '0x' + Primitive.object_class(object).object_id.to_s(16),
85+
class: '0x' + Primitive.class(object).object_id.to_s(16),
8686
memsize: memsize_of(object),
8787
flags: { }
8888
}
@@ -120,7 +120,7 @@ def _dump(object, output)
120120

121121
# Helper method for ObjectSpace.dump_all
122122
def _dump_all(output, full, since)
123-
if Primitive.object_kind_of?(output, String)
123+
if Primitive.is_a?(output, String)
124124
objects = []
125125
ObjectSpace.each_object do |object|
126126
objects.push dump(object)

lib/truffle/pathname.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,7 @@ module Kernel
10921092
#
10931093
# This method is available since 1.8.5.
10941094
def Pathname(path) # :doc:
1095-
return path if Primitive.class_of(path) == Pathname
1095+
return path if Primitive.metaclass(path) == Pathname
10961096
Pathname.new(path)
10971097
end
10981098
module_function :Pathname

lib/truffle/strscan.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ def peep(len)
333333
private def scan_internal(pattern, advance_pos, getstr, headonly)
334334
scan_check_args(pattern, headonly)
335335

336-
if Primitive.object_kind_of?(pattern, String)
336+
if Primitive.is_a?(pattern, String)
337337
md = scan_internal_string_pattern(pattern)
338338
else
339339
start = @fixed_anchor ? 0 : @pos

lib/truffle/truffle/cext.rb

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def rbimpl_rtypeddata_p(obj)
266266
end
267267

268268
def ensure_class(obj, klass, message = 'expected class %s, but object class is %s')
269-
raise TypeError, format(message, klass, Primitive.object_class(obj)) unless Primitive.object_kind_of?(obj, klass)
269+
raise TypeError, format(message, klass, Primitive.class(obj)) unless Primitive.is_a?(obj, klass)
270270
end
271271

272272
def rb_method_boundp(klass, id, ex)
@@ -278,11 +278,11 @@ def rb_method_boundp(klass, id, ex)
278278
end
279279

280280
def rb_obj_is_instance_of(object, ruby_class)
281-
Primitive.object_class(object) == ruby_class
281+
Primitive.class(object) == ruby_class
282282
end
283283

284284
def rb_obj_is_kind_of(object, ruby_class)
285-
Primitive.object_kind_of?(object, ruby_class)
285+
Primitive.is_a?(object, ruby_class)
286286
end
287287

288288
def SYMBOL_P(value)
@@ -310,7 +310,7 @@ def RB_FIXNUM_P(value)
310310
end
311311

312312
def RB_FLOAT_TYPE_P(value)
313-
Primitive.object_kind_of?(value, Float)
313+
Primitive.is_a?(value, Float)
314314
end
315315

316316
def rb_integer_type_p(value)
@@ -406,7 +406,7 @@ def rb_num_coerce_relop(x, y, func)
406406
if Primitive.nil?(ary) && !raise_error
407407
return nil
408408
end
409-
if !Primitive.object_kind_of?(ary, Array) || ary.size != 2
409+
if !Primitive.is_a?(ary, Array) || ary.size != 2
410410
raise TypeError, 'coerce must return [x, y]'
411411
end
412412
ary
@@ -443,15 +443,15 @@ def rb_hash_start(h)
443443
end
444444

445445
def rb_obj_class(object)
446-
Primitive.object_class(object)
446+
Primitive.class(object)
447447
end
448448

449449
def rb_obj_classname(object)
450-
Primitive.object_class(object).name
450+
Primitive.class(object).name
451451
end
452452

453453
def rb_class_of(object)
454-
Primitive.class_of(object)
454+
Primitive.metaclass(object)
455455
end
456456

457457
def rb_class_real(ruby_class)
@@ -785,7 +785,7 @@ def coderange_java_to_rb(cr)
785785
end
786786

787787
def RB_ENC_CODERANGE(obj)
788-
if Primitive.object_kind_of?(obj, String)
788+
if Primitive.is_a?(obj, String)
789789
rb_enc_str_coderange(obj)
790790
else
791791
raise "Unknown coderange for obj with class `#{obj.class}`"
@@ -847,7 +847,7 @@ def rb_enc_get_index(obj)
847847
0
848848
end
849849
end
850-
enc = rb_enc_to_index(enc) if Primitive.object_kind_of?(enc, Encoding)
850+
enc = rb_enc_to_index(enc) if Primitive.is_a?(enc, Encoding)
851851
enc
852852
end
853853

@@ -1007,7 +1007,7 @@ def rb_path_to_class(path)
10071007
rescue NameError
10081008
raise ArgumentError, "undefined class/module #{path}"
10091009
end
1010-
unless Primitive.object_kind_of?(const, ::Module)
1010+
unless Primitive.is_a?(const, ::Module)
10111011
raise TypeError, "#{path} does not refer to class/module"
10121012
end
10131013
const
@@ -1059,7 +1059,7 @@ def rb_protect(function, arg, write_status, status)
10591059
res = Truffle::Interop.execute_without_conversion(function, arg)
10601060
end
10611061

1062-
unless Primitive.object_equal(nil, e)
1062+
unless Primitive.equal?(nil, e)
10631063
store_exception(e)
10641064
pos = extract_tag(e)
10651065
Primitive.thread_set_exception(extract_ruby_exception(e))
@@ -1129,7 +1129,7 @@ def rb_exc_raise(exception)
11291129
end
11301130

11311131
def rb_set_errinfo(error)
1132-
if Primitive.nil?(error) || Primitive.object_kind_of?(error, Exception)
1132+
if Primitive.nil?(error) || Primitive.is_a?(error, Exception)
11331133
Primitive.thread_set_exception(error)
11341134
else
11351135
raise TypeError, 'assigning non-exception to ?!'
@@ -1255,7 +1255,7 @@ def rb_io_puts(out, args)
12551255
end
12561256

12571257
def rb_equal(a, b)
1258-
Primitive.object_same_or_equal(a, b)
1258+
Primitive.same_or_equal?(a, b)
12591259
end
12601260

12611261
def rb_obj_call_init(obj, args, block)
@@ -1487,7 +1487,7 @@ def rb_data_object_wrap(ruby_class, data, mark, free)
14871487
data_holder = Primitive.data_holder_create(data, mark, free)
14881488
Primitive.object_hidden_var_set object, DATA_HOLDER, data_holder
14891489

1490-
Primitive.object_space_define_data_finalizer object, data_holder unless Truffle::Interop.null?(free)
1490+
Primitive.objectspace_define_data_finalizer object, data_holder unless Truffle::Interop.null?(free)
14911491

14921492
define_marker object, mark
14931493

@@ -1502,7 +1502,7 @@ def rb_data_typed_object_wrap(ruby_class, data, data_type, mark, free, size)
15021502
Primitive.object_hidden_var_set object, DATA_HOLDER, data_holder
15031503
Primitive.object_hidden_var_set object, DATA_MEMSIZER, data_sizer(size, data_holder) unless Truffle::Interop.null?(size)
15041504

1505-
Primitive.object_space_define_data_finalizer object, data_holder unless Truffle::Interop.null?(free)
1505+
Primitive.objectspace_define_data_finalizer object, data_holder unless Truffle::Interop.null?(free)
15061506

15071507
define_marker object, mark
15081508

@@ -1544,7 +1544,7 @@ def rb_tr_error(message)
15441544
def test_kwargs(kwargs, raise_error)
15451545
return false if kwargs.nil?
15461546

1547-
if Primitive.object_kind_of?(kwargs, Hash) && kwargs.keys.all? { |k| Primitive.object_kind_of?(k, Symbol) }
1547+
if Primitive.is_a?(kwargs, Hash) && kwargs.keys.all? { |k| Primitive.is_a?(k, Symbol) }
15481548
true
15491549
elsif raise_error
15501550
raise ArgumentError, "the value is not a Hash with all keys being Symbols as kwargs requires: #{kwargs}"
@@ -1698,7 +1698,7 @@ def rb_time_num_new(timev, off)
16981698

16991699
def rb_time_interval_acceptable(time_val)
17001700
# TODO (pitr-ch 09-Mar-2017): more precise error messages
1701-
raise TypeError, 'cannot be Time' if Primitive.object_kind_of?(time_val, Time)
1701+
raise TypeError, 'cannot be Time' if Primitive.is_a?(time_val, Time)
17021702
raise ArgumentError, 'cannot be negative' if time_val < 0
17031703
end
17041704

@@ -1789,7 +1789,7 @@ def rb_obj_as_string(object)
17891789
end
17901790

17911791
def rb_class_inherited_p(ruby_module, object)
1792-
if Primitive.object_kind_of?(object, Module)
1792+
if Primitive.is_a?(object, Module)
17931793
ruby_module <= object
17941794
else
17951795
raise TypeError

lib/truffle/truffle/cext_structs.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ def polyglot_read_member(name)
244244
when 'flags'
245245
compute_flags
246246
when 'klass'
247-
Primitive.cext_wrap(Primitive.class_of(@object))
247+
Primitive.cext_wrap(Primitive.metaclass(@object))
248248
else
249249
raise Truffle::Interop::UnknownIdentifierException
250250
end

src/main/java/org/truffleruby/core/array/ArrayNodes.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2380,14 +2380,14 @@ private Entry(RubyArray array, int index) {
23802380
@Specialization(guards = "!canContainObject.execute(array)", limit = "1")
23812381
protected boolean flattenHelperPrimitive(RubyArray array, RubyArray out, int maxLevels,
23822382
@Cached @Exclusive ArrayAppendManyNode concat,
2383-
@Cached @Exclusive TypeNodes.CanContainObjectNode canContainObject) {
2383+
@Cached @Exclusive ArrayCanContainObjectNode canContainObject) {
23842384
concat.executeAppendMany(out, array);
23852385
return false;
23862386
}
23872387

23882388
@Specialization(replaces = "flattenHelperPrimitive")
23892389
protected boolean flattenHelper(RubyArray array, RubyArray out, int maxLevels,
2390-
@Cached @Exclusive TypeNodes.CanContainObjectNode canContainObject,
2390+
@Cached @Exclusive ArrayCanContainObjectNode canContainObject,
23912391
@Cached @Exclusive ArrayAppendManyNode concat,
23922392
@Cached AtNode at,
23932393
@Cached DispatchNode convert,
@@ -2457,4 +2457,29 @@ private static void add(EconomicSet<RubyArray> set, RubyArray array) {
24572457
set.add(array);
24582458
}
24592459
}
2460+
2461+
@Primitive(name = "array_can_contain_object?")
2462+
@ImportStatic(ArrayGuards.class)
2463+
public abstract static class ArrayCanContainObjectNode extends PrimitiveArrayArgumentsNode {
2464+
2465+
@NeverDefault
2466+
public static ArrayCanContainObjectNode create() {
2467+
return ArrayNodesFactory.ArrayCanContainObjectNodeFactory.create(null);
2468+
}
2469+
2470+
public abstract boolean execute(RubyArray array);
2471+
2472+
@Specialization(limit = "storageStrategyLimit()")
2473+
protected boolean array(RubyArray array,
2474+
@Bind("array.getStore()") Object store,
2475+
@CachedLibrary("store") ArrayStoreLibrary stores) {
2476+
return !stores.isPrimitive(store);
2477+
}
2478+
2479+
@Specialization(guards = "!isRubyArray(array)")
2480+
protected boolean other(Object array) {
2481+
return true;
2482+
}
2483+
2484+
}
24602485
}

0 commit comments

Comments
 (0)