Skip to content

Commit 7351cae

Browse files
committed
Implement Hash.ruby2_keywords_hash?(hash) and Hash.ruby2_keywords_hash(hash)
* It is used by pp.rb in stdlib, so we need it for nice error messages in MSpec. * Not using it for foo(*args) calls yet.
1 parent 55de909 commit 7351cae

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
fails:Hash.ruby2_keywords_hash? returns false if the Hash is not a keywords Hash
21
fails:Hash.ruby2_keywords_hash? returns true if the Hash is a keywords Hash
3-
fails:Hash.ruby2_keywords_hash? raises TypeError for non-Hash
4-
fails:Hash.ruby2_keywords_hash returns a copy of a Hash and marks the copy as a keywords Hash
5-
fails:Hash.ruby2_keywords_hash raises TypeError for non-Hash
6-
fails:Hash.ruby2_keywords_hash returns an instance of the subclass if called on an instance of a subclass of Hash

src/main/java/org/truffleruby/core/hash/HashNodes.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,23 @@ public boolean isSmallArrayOfPairs(Object[] args, RubyLanguage language) {
173173

174174
}
175175

176+
@CoreMethod(names = "ruby2_keywords_hash?", onSingleton = true, required = 1)
177+
public abstract static class IsRuby2KeywordsHashNode extends CoreMethodArrayArgumentsNode {
178+
@Specialization
179+
protected boolean isRuby2KeywordsHash(RubyHash hash) {
180+
return hash.ruby2_keywords;
181+
}
182+
}
183+
184+
@Primitive(name = "hash_mark_ruby2_keywords")
185+
public abstract static class HashMarkRuby2KeywordsNode extends CoreMethodArrayArgumentsNode {
186+
@Specialization
187+
protected RubyHash markRuby2Keywords(RubyHash hash) {
188+
hash.ruby2_keywords = true;
189+
return hash;
190+
}
191+
}
192+
176193
@ImportStatic(HashGuards.class)
177194
public abstract static class HashLookupOrExecuteDefaultNode extends RubyContextNode {
178195

src/main/java/org/truffleruby/core/hash/RubyHash.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class RubyHash extends RubyDynamicObject implements ObjectGraphNode {
2929
public Entry firstInSequence;
3030
public Entry lastInSequence;
3131
public boolean compareByIdentity;
32+
public boolean ruby2_keywords = false;
3233

3334
public RubyHash(
3435
RubyClass rubyClass,

src/main/ruby/truffleruby/core/hash.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ def self._constructor_fallback(*args)
103103
end
104104
private_class_method :_constructor_fallback
105105

106+
def self.ruby2_keywords_hash(hash)
107+
Primitive.hash_mark_ruby2_keywords(hash.dup)
108+
end
109+
106110
alias_method :store, :[]=
107111

108112
# Used internally to get around subclasses redefining #[]=

0 commit comments

Comments
 (0)